diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventChargingState.java b/app/src/main/java/info/nightscout/androidaps/events/EventChargingState.java new file mode 100644 index 0000000000..e0d839322f --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventChargingState.java @@ -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; + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientPlugin.java index 004c7646f5..4854165799 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientPlugin.java @@ -3,7 +3,10 @@ package info.nightscout.androidaps.plugins.NSClientInternal; import android.content.ComponentName; import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.content.ServiceConnection; +import android.net.ConnectivityManager; +import android.net.wifi.WifiManager; import android.os.Handler; import android.os.HandlerThread; import android.os.IBinder; @@ -61,6 +64,7 @@ public class NSClientPlugin extends PluginBase { public String status = ""; public NSClientService nsClientService = null; + NetworkChangeReceiver networkChangeReceiver = new NetworkChangeReceiver(); private NSClientPlugin() { super(new PluginDescription() @@ -92,7 +96,14 @@ public class NSClientPlugin extends PluginBase { context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE); 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) 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_allowroaming) ) { - EventNetworkChange event = NetworkChangeReceiver.grabNetworkStatus(); + EventNetworkChange event = networkChangeReceiver.grabNetworkStatus(MainApp.instance().getApplicationContext()); if (event != null) MainApp.bus().post(event); } diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/ChargingStateReceiver.java b/app/src/main/java/info/nightscout/androidaps/receivers/ChargingStateReceiver.java new file mode 100644 index 0000000000..b2d51ff1fb --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/receivers/ChargingStateReceiver.java @@ -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); + } + +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/NetworkChangeReceiver.java b/app/src/main/java/info/nightscout/androidaps/receivers/NetworkChangeReceiver.java index b3a99b66ff..4e01066bf9 100644 --- a/app/src/main/java/info/nightscout/androidaps/receivers/NetworkChangeReceiver.java +++ b/app/src/main/java/info/nightscout/androidaps/receivers/NetworkChangeReceiver.java @@ -8,7 +8,6 @@ import android.net.NetworkInfo; import android.net.wifi.SupplicantState; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; -import android.os.PowerManager; import android.support.annotation.Nullable; import org.slf4j.Logger; @@ -18,27 +17,21 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.events.EventNetworkChange; public class NetworkChangeReceiver extends BroadcastReceiver { + private static Logger log = LoggerFactory.getLogger(NetworkChangeReceiver.class); @Override public void onReceive(final Context context, final Intent intent) { - PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); - if (pm == null) return; - PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "NetworkChangeReceiver"); - wl.acquire(10000); - - EventNetworkChange event = grabNetworkStatus(); + EventNetworkChange event = grabNetworkStatus(context); if (event != null) MainApp.bus().post(event); - - wl.release(); } @Nullable - public static EventNetworkChange grabNetworkStatus() { + public EventNetworkChange grabNetworkStatus(final Context context) { 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; NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); @@ -54,6 +47,7 @@ public class NetworkChangeReceiver extends BroadcastReceiver { log.debug("NETCHANGE: Wifi connected. SSID: " + event.ssid); } } + if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) { event.mobileConnected = true; event.roaming = activeNetwork.isRoaming(); @@ -62,6 +56,7 @@ public class NetworkChangeReceiver extends BroadcastReceiver { } else { log.debug("NETCHANGE: Disconnected."); } + return event; } } \ No newline at end of file