android: Add method to check for connectivity to NetworkManager
authorTobias Brunner <tobias@strongswan.org>
Tue, 21 Jul 2015 09:05:10 +0000 (11:05 +0200)
committerTobias Brunner <tobias@strongswan.org>
Tue, 28 Jul 2015 11:27:33 +0000 (13:27 +0200)
src/frontends/android/jni/libandroidbridge/kernel/network_manager.c
src/frontends/android/jni/libandroidbridge/kernel/network_manager.h
src/frontends/android/src/org/strongswan/android/logic/NetworkManager.java

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),
index abca239..9a6a715 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
@@ -66,6 +66,13 @@ struct network_manager_t {
                                                                   connectivity_cb_t cb);
 
        /**
+        * Check whether we currently have connectivity
+        *
+        * @return                                      TRUE if currently connected
+        */
+       bool (*is_connected)(network_manager_t *this);
+
+       /**
         * Destroy a network_manager_t instance
         */
        void (*destroy)(network_manager_t *this);
index 8ea07f4..ebe1d00 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
@@ -42,12 +42,21 @@ public class NetworkManager extends BroadcastReceiver
                mContext.unregisterReceiver(this);
        }
 
+       public boolean isConnected()
+       {
+               ConnectivityManager cm = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
+               NetworkInfo info = null;
+               if (cm != null)
+               {
+                       info = cm.getActiveNetworkInfo();
+               }
+               return info != null && info.isConnected();
+       }
+
        @Override
        public void onReceive(Context context, Intent intent)
        {
-               ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
-               NetworkInfo info = cm.getActiveNetworkInfo();
-               networkChanged(info == null || !info.isConnected());
+               networkChanged(!isConnected());
        }
 
        /**