add battery charging state
This commit is contained in:
parent
55ea10e88f
commit
099cb6c4e4
|
@ -59,7 +59,7 @@ android {
|
|||
|
||||
defaultConfig {
|
||||
applicationId "info.nightscout.androidaps"
|
||||
minSdkVersion 21
|
||||
minSdkVersion 23
|
||||
targetSdkVersion 25
|
||||
multiDexEnabled true
|
||||
versionCode 1500
|
||||
|
|
|
@ -2,14 +2,12 @@ package info.nightscout.androidaps.events;
|
|||
|
||||
public class EventChargingState {
|
||||
|
||||
boolean isCharging = false;
|
||||
boolean isPlugged = false;
|
||||
public boolean isCharging = false;
|
||||
|
||||
public EventChargingState() {}
|
||||
|
||||
public EventChargingState(boolean isCharging, boolean isPlugged) {
|
||||
public EventChargingState(boolean isCharging) {
|
||||
this.isCharging = isCharging;
|
||||
this.isPlugged = isPlugged;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import android.content.IntentFilter;
|
|||
import android.content.ServiceConnection;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.os.BatteryManager;
|
||||
import android.os.Handler;
|
||||
import android.os.HandlerThread;
|
||||
import android.os.IBinder;
|
||||
|
@ -21,7 +22,6 @@ import org.slf4j.LoggerFactory;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import info.nightscout.androidaps.BuildConfig;
|
||||
import info.nightscout.androidaps.Config;
|
||||
import info.nightscout.androidaps.Constants;
|
||||
import info.nightscout.androidaps.MainApp;
|
||||
|
@ -37,6 +37,7 @@ import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientN
|
|||
import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientStatus;
|
||||
import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientUpdateGUI;
|
||||
import info.nightscout.androidaps.plugins.NSClientInternal.services.NSClientService;
|
||||
import info.nightscout.androidaps.receivers.ChargingStateReceiver;
|
||||
import info.nightscout.androidaps.receivers.NetworkChangeReceiver;
|
||||
import info.nightscout.utils.SP;
|
||||
import info.nightscout.utils.ToastUtils;
|
||||
|
@ -60,12 +61,16 @@ public class NSClientPlugin extends PluginBase {
|
|||
|
||||
public boolean paused = false;
|
||||
public boolean allowed = true;
|
||||
public boolean allowedChargingsState = true;
|
||||
public boolean allowedNetworkState = true;
|
||||
boolean autoscroll = true;
|
||||
|
||||
public String status = "";
|
||||
|
||||
public NSClientService nsClientService = null;
|
||||
NetworkChangeReceiver networkChangeReceiver = new NetworkChangeReceiver();
|
||||
|
||||
private NetworkChangeReceiver networkChangeReceiver = new NetworkChangeReceiver();
|
||||
private ChargingStateReceiver chargingStateReceiver = new ChargingStateReceiver();
|
||||
|
||||
private NSClientPlugin() {
|
||||
super(new PluginDescription()
|
||||
|
@ -97,6 +102,12 @@ public class NSClientPlugin extends PluginBase {
|
|||
context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
|
||||
super.onStart();
|
||||
|
||||
registerReceivers();
|
||||
|
||||
}
|
||||
|
||||
protected void registerReceivers() {
|
||||
Context context = MainApp.instance().getApplicationContext();
|
||||
// register NetworkChangeReceiver --> https://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html
|
||||
// Nougat is not providing Connectivity-Action anymore ;-(
|
||||
context.registerReceiver(networkChangeReceiver,
|
||||
|
@ -104,9 +115,19 @@ public class NSClientPlugin extends PluginBase {
|
|||
context.registerReceiver(networkChangeReceiver,
|
||||
new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
|
||||
|
||||
EventNetworkChange event = networkChangeReceiver.grabNetworkStatus(MainApp.instance().getApplicationContext());
|
||||
EventNetworkChange event = networkChangeReceiver.grabNetworkStatus(context);
|
||||
if (event != null)
|
||||
MainApp.bus().post(event);
|
||||
|
||||
context.registerReceiver(chargingStateReceiver,
|
||||
new IntentFilter(Intent.ACTION_POWER_CONNECTED));
|
||||
context.registerReceiver(chargingStateReceiver,
|
||||
new IntentFilter(Intent.ACTION_POWER_DISCONNECTED));
|
||||
|
||||
EventChargingState eventChargingState = chargingStateReceiver.grabChargingState(context);
|
||||
if (eventChargingState != null)
|
||||
MainApp.bus().post(eventChargingState);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -115,6 +136,7 @@ public class NSClientPlugin extends PluginBase {
|
|||
Context context = MainApp.instance().getApplicationContext();
|
||||
context.unbindService(mConnection);
|
||||
context.unregisterReceiver(networkChangeReceiver);
|
||||
context.unregisterReceiver(chargingStateReceiver);
|
||||
}
|
||||
|
||||
private ServiceConnection mConnection = new ServiceConnection() {
|
||||
|
@ -141,16 +163,53 @@ public class NSClientPlugin extends PluginBase {
|
|||
EventNetworkChange event = networkChangeReceiver.grabNetworkStatus(MainApp.instance().getApplicationContext());
|
||||
if (event != null)
|
||||
MainApp.bus().post(event);
|
||||
} else if (ev.isChanged(R.string.key_ns_chargingonly)) {
|
||||
EventChargingState event = chargingStateReceiver.grabChargingState(MainApp.instance().getApplicationContext());
|
||||
if (event != null)
|
||||
MainApp.bus().post(event);
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onStatusEvent(final EventChargingState ev) {
|
||||
boolean newChargingState = calculateStatus(ev);
|
||||
|
||||
if (newChargingState != allowedChargingsState) {
|
||||
allowedChargingsState = newChargingState;
|
||||
processStateChange();
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onStatusEvent(final EventNetworkChange ev) {
|
||||
boolean newNetworkState = calculateStatus(ev);
|
||||
|
||||
if (newNetworkState != allowedNetworkState) {
|
||||
allowedNetworkState = newNetworkState;
|
||||
processStateChange();
|
||||
}
|
||||
}
|
||||
|
||||
private void processStateChange() {
|
||||
boolean newAllowedState = allowedChargingsState && allowedNetworkState;
|
||||
if (newAllowedState != allowed) {
|
||||
allowed = newAllowedState;
|
||||
MainApp.bus().post(new EventPreferenceChange(R.string.key_nsclientinternal_paused));
|
||||
}
|
||||
}
|
||||
|
||||
private boolean calculateStatus(final EventChargingState ev) {
|
||||
boolean chargingOnly = SP.getBoolean(R.string.ns_chargingonly, false);
|
||||
|
||||
boolean newAllowedState = true;
|
||||
|
||||
if (!ev.isCharging && chargingOnly) newAllowedState = false;
|
||||
|
||||
return newAllowedState;
|
||||
}
|
||||
|
||||
|
||||
private boolean calculateStatus(final EventNetworkChange ev) {
|
||||
boolean wifiOnly = SP.getBoolean(R.string.key_ns_wifionly, false);
|
||||
String allowedSSIDs = SP.getString(R.string.key_ns_wifi_ssids, "");
|
||||
boolean allowRoaming = SP.getBoolean(R.string.key_ns_allowroaming, true);
|
||||
|
@ -162,10 +221,7 @@ public class NSClientPlugin extends PluginBase {
|
|||
newAllowedState = false;
|
||||
if (!allowRoaming && ev.roaming) newAllowedState = false;
|
||||
|
||||
if (newAllowedState != allowed) {
|
||||
allowed = newAllowedState;
|
||||
MainApp.bus().post(new EventPreferenceChange(R.string.key_nsclientinternal_paused));
|
||||
}
|
||||
return newAllowedState;
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
|
|
|
@ -3,38 +3,30 @@ 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;
|
||||
EventChargingState event = grabChargingState(context);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public EventChargingState grabChargingState(Context context) {
|
||||
BatteryManager bm = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
|
||||
|
||||
int status = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_STATUS);
|
||||
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING
|
||||
|| status == BatteryManager.BATTERY_STATUS_FULL;
|
||||
|
||||
EventChargingState event = new EventChargingState(isCharging);
|
||||
return event;
|
||||
}
|
||||
|
||||
}
|
|
@ -1019,8 +1019,10 @@
|
|||
<string name="key_ns_wifionly" translatable="false">ns_wifionly</string>
|
||||
<string name="key_ns_wifi_ssids" translatable="false">ns_wifi_ssids</string>
|
||||
<string name="key_ns_allowroaming" translatable="false">ns_allowroaming</string>
|
||||
<string name="key_ns_chargingonly" translatable="false">ns_chargingonly</string>
|
||||
<string name="ns_wifionly">Use WiFi connection only</string>
|
||||
<string name="ns_wifi_ssids">WiFi SSID</string>
|
||||
<string name="ns_chargingonly">Only if charging</string>
|
||||
<string name="connectionsettings_title">Connection settings</string>
|
||||
<string name="ns_wifi_allowedssids">Allowed SSIDs (semicolon separated)</string>
|
||||
<string name="ns_allowroaming">Allow connection in roaming</string>
|
||||
|
|
|
@ -103,6 +103,11 @@
|
|||
android:key="@string/key_ns_allowroaming"
|
||||
android:title="@string/ns_allowroaming" />
|
||||
|
||||
<SwitchPreference
|
||||
android:defaultValue="false"
|
||||
android:key="@string/key_ns_chargingonly"
|
||||
android:title="@string/ns_chargingonly" />
|
||||
|
||||
</PreferenceScreen>
|
||||
|
||||
<PreferenceScreen android:title="@string/advancedsettings_title">
|
||||
|
|
Loading…
Reference in a new issue