android: Add method to check for connectivity to NetworkManager
[strongswan.git] / src / frontends / android / jni / libandroidbridge / kernel / network_manager.c
index f8e560b..372b25c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012-2013 Tobias Brunner
+ * Copyright (C) 2012-2015 Tobias Brunner
  * Hochschule fuer Technik Rapperswil
  *
  * This program is free software; you can redistribute it and/or modify it
@@ -90,8 +90,8 @@ METHOD(network_manager_t, add_connectivity_cb, void,
                                this->connectivity_cb.cb = cb;
                                this->connectivity_cb.data = data;
                        }
-                       androidjni_detach_thread();
                }
+               androidjni_detach_thread();
        }
        this->mutex->unlock(this->mutex);
 }
@@ -132,6 +132,28 @@ METHOD(network_manager_t, remove_connectivity_cb, void,
        this->mutex->unlock(this->mutex);
 }
 
+METHOD(network_manager_t, is_connected, bool,
+       private_network_manager_t *this)
+{
+       JNIEnv *env;
+       jmethodID method_id;
+       bool connected = FALSE;
+
+       androidjni_attach_thread(&env);
+       method_id = (*env)->GetMethodID(env, this->cls, "isConnected", "()Z");
+       if (!method_id)
+       {
+               androidjni_exception_occurred(env);
+       }
+       else
+       {
+               connected = (*env)->CallBooleanMethod(env, this->obj, method_id);
+               connected = !androidjni_exception_occurred(env) && connected;
+       }
+       androidjni_detach_thread();
+       return connected;
+}
+
 METHOD(network_manager_t, destroy, void,
        private_network_manager_t *this)
 {
@@ -174,6 +196,7 @@ network_manager_t *network_manager_create(jobject context)
                .public = {
                        .add_connectivity_cb = _add_connectivity_cb,
                        .remove_connectivity_cb = _remove_connectivity_cb,
+                       .is_connected = _is_connected,
                        .destroy = _destroy,
                },
                .mutex = mutex_create(MUTEX_TYPE_DEFAULT),