First step to use network activity receiver

This commit is contained in:
Markus M. May 2018-04-25 20:49:03 +02:00
parent 6851adbfd3
commit 3fb17e6869
4 changed files with 74 additions and 13 deletions

View file

@ -0,0 +1,15 @@
package info.nightscout.androidaps.events;
public class EventChargingState {
boolean isCharging = false;
boolean isPlugged = false;
public EventChargingState() {}
public EventChargingState(boolean isCharging, boolean isPlugged) {
this.isCharging = isCharging;
this.isPlugged = isPlugged;
}
}

View file

@ -3,7 +3,10 @@ package info.nightscout.androidaps.plugins.NSClientInternal;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection; import android.content.ServiceConnection;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Handler; import android.os.Handler;
import android.os.HandlerThread; import android.os.HandlerThread;
import android.os.IBinder; import android.os.IBinder;
@ -61,6 +64,7 @@ public class NSClientPlugin extends PluginBase {
public String status = ""; public String status = "";
public NSClientService nsClientService = null; public NSClientService nsClientService = null;
NetworkChangeReceiver networkChangeReceiver = new NetworkChangeReceiver();
private NSClientPlugin() { private NSClientPlugin() {
super(new PluginDescription() super(new PluginDescription()
@ -92,7 +96,14 @@ public class NSClientPlugin extends PluginBase {
context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE); context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
super.onStart(); super.onStart();
EventNetworkChange event = NetworkChangeReceiver.grabNetworkStatus(); // register NetworkChangeReceiver --> https://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html
// Nougat is not providing Connectivity-Action anymore ;-(
context.registerReceiver(networkChangeReceiver,
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
context.registerReceiver(networkChangeReceiver,
new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
EventNetworkChange event = networkChangeReceiver.grabNetworkStatus(MainApp.instance().getApplicationContext());
if (event != null) if (event != null)
MainApp.bus().post(event); MainApp.bus().post(event);
} }
@ -125,7 +136,7 @@ public class NSClientPlugin extends PluginBase {
ev.isChanged(R.string.key_ns_wifi_ssids) || ev.isChanged(R.string.key_ns_wifi_ssids) ||
ev.isChanged(R.string.key_ns_allowroaming) ev.isChanged(R.string.key_ns_allowroaming)
) { ) {
EventNetworkChange event = NetworkChangeReceiver.grabNetworkStatus(); EventNetworkChange event = networkChangeReceiver.grabNetworkStatus(MainApp.instance().getApplicationContext());
if (event != null) if (event != null)
MainApp.bus().post(event); MainApp.bus().post(event);
} }

View file

@ -0,0 +1,40 @@
package info.nightscout.androidaps.receivers;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.BatteryManager;
import android.os.PowerManager;
import android.support.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.events.EventChargingState;
import info.nightscout.androidaps.events.EventNetworkChange;
public class ChargingStateReceiver extends BroadcastReceiver {
private static Logger log = LoggerFactory.getLogger(ChargingStateReceiver.class);
@Override
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
EventChargingState event = new EventChargingState(isCharging, usbCharge || acCharge);
if (event != null)
MainApp.bus().post(event);
}
}

View file

@ -8,7 +8,6 @@ import android.net.NetworkInfo;
import android.net.wifi.SupplicantState; import android.net.wifi.SupplicantState;
import android.net.wifi.WifiInfo; import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.os.PowerManager;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -18,27 +17,21 @@ import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.events.EventNetworkChange; import info.nightscout.androidaps.events.EventNetworkChange;
public class NetworkChangeReceiver extends BroadcastReceiver { public class NetworkChangeReceiver extends BroadcastReceiver {
private static Logger log = LoggerFactory.getLogger(NetworkChangeReceiver.class); private static Logger log = LoggerFactory.getLogger(NetworkChangeReceiver.class);
@Override @Override
public void onReceive(final Context context, final Intent intent) { public void onReceive(final Context context, final Intent intent) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); EventNetworkChange event = grabNetworkStatus(context);
if (pm == null) return;
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "NetworkChangeReceiver");
wl.acquire(10000);
EventNetworkChange event = grabNetworkStatus();
if (event != null) if (event != null)
MainApp.bus().post(event); MainApp.bus().post(event);
wl.release();
} }
@Nullable @Nullable
public static EventNetworkChange grabNetworkStatus() { public EventNetworkChange grabNetworkStatus(final Context context) {
EventNetworkChange event = new EventNetworkChange(); EventNetworkChange event = new EventNetworkChange();
ConnectivityManager cm = (ConnectivityManager) MainApp.instance().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE); ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null) return null; if (cm == null) return null;
NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
@ -54,6 +47,7 @@ public class NetworkChangeReceiver extends BroadcastReceiver {
log.debug("NETCHANGE: Wifi connected. SSID: " + event.ssid); log.debug("NETCHANGE: Wifi connected. SSID: " + event.ssid);
} }
} }
if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) { if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
event.mobileConnected = true; event.mobileConnected = true;
event.roaming = activeNetwork.isRoaming(); event.roaming = activeNetwork.isRoaming();
@ -62,6 +56,7 @@ public class NetworkChangeReceiver extends BroadcastReceiver {
} else { } else {
log.debug("NETCHANGE: Disconnected."); log.debug("NETCHANGE: Disconnected.");
} }
return event; return event;
} }
} }