android: Add method to check for connectivity to NetworkManager

This commit is contained in:
Tobias Brunner
2015-07-28 13:27:33 +02:00
parent 5da31733de
commit 41b59a3443
3 changed files with 46 additions and 7 deletions
@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2012-2013 Tobias Brunner * Copyright (C) 2012-2015 Tobias Brunner
* Hochschule fuer Technik Rapperswil * Hochschule fuer Technik Rapperswil
* *
* This program is free software; you can redistribute it and/or modify it * 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.cb = cb;
this->connectivity_cb.data = data; this->connectivity_cb.data = data;
} }
androidjni_detach_thread();
} }
androidjni_detach_thread();
} }
this->mutex->unlock(this->mutex); this->mutex->unlock(this->mutex);
} }
@@ -132,6 +132,28 @@ METHOD(network_manager_t, remove_connectivity_cb, void,
this->mutex->unlock(this->mutex); 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, METHOD(network_manager_t, destroy, void,
private_network_manager_t *this) private_network_manager_t *this)
{ {
@@ -174,6 +196,7 @@ network_manager_t *network_manager_create(jobject context)
.public = { .public = {
.add_connectivity_cb = _add_connectivity_cb, .add_connectivity_cb = _add_connectivity_cb,
.remove_connectivity_cb = _remove_connectivity_cb, .remove_connectivity_cb = _remove_connectivity_cb,
.is_connected = _is_connected,
.destroy = _destroy, .destroy = _destroy,
}, },
.mutex = mutex_create(MUTEX_TYPE_DEFAULT), .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2012-2013 Tobias Brunner * Copyright (C) 2012-2015 Tobias Brunner
* Hochschule fuer Technik Rapperswil * Hochschule fuer Technik Rapperswil
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
@@ -65,6 +65,13 @@ struct network_manager_t {
void (*remove_connectivity_cb)(network_manager_t *this, void (*remove_connectivity_cb)(network_manager_t *this,
connectivity_cb_t cb); 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 * Destroy a network_manager_t instance
*/ */
@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2012-2013 Tobias Brunner * Copyright (C) 2012-2015 Tobias Brunner
* Hochschule fuer Technik Rapperswil * Hochschule fuer Technik Rapperswil
* *
* This program is free software; you can redistribute it and/or modify it * 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); 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 @Override
public void onReceive(Context context, Intent intent) public void onReceive(Context context, Intent intent)
{ {
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); networkChanged(!isConnected());
NetworkInfo info = cm.getActiveNetworkInfo();
networkChanged(info == null || !info.isConnected());
} }
/** /**