Merge latest dev version

This commit is contained in:
Markus M. May 2018-05-01 21:56:33 +02:00
commit 12d80e96ef
11 changed files with 347 additions and 54 deletions

View file

@ -59,7 +59,7 @@ android {
defaultConfig {
applicationId "info.nightscout.androidaps"
minSdkVersion 21
minSdkVersion 23
targetSdkVersion 25
multiDexEnabled true
versionCode 1500

View file

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

View file

@ -5,6 +5,10 @@ public class EventNetworkChange extends Event {
public boolean mobileConnected = false;
public boolean wifiConnected = false;
public String ssid;
public String ssid = "";
public boolean roaming = false;
public String getSsid() {
return ssid.replace("SSID: ","").replaceAll("\"","");
}
}

View file

@ -18,12 +18,12 @@ 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;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventAppExit;
import info.nightscout.androidaps.events.EventChargingState;
import info.nightscout.androidaps.events.EventNetworkChange;
import info.nightscout.androidaps.events.EventPreferenceChange;
import info.nightscout.androidaps.interfaces.PluginBase;
@ -33,7 +33,6 @@ 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.NetworkChangeReceiver;
import info.nightscout.utils.SP;
import info.nightscout.utils.ToastUtils;
@ -55,13 +54,14 @@ public class NSClientPlugin extends PluginBase {
Spanned textLog = Html.fromHtml("");
public boolean paused = false;
public boolean allowed = true;
boolean autoscroll = true;
public String status = "";
public NSClientService nsClientService = null;
private NsClientReceiverDelegate nsClientReceiverDelegate;
private NSClientPlugin() {
super(new PluginDescription()
.mainType(PluginType.GENERAL)
@ -82,8 +82,16 @@ public class NSClientPlugin extends PluginBase {
handlerThread.start();
handler = new Handler(handlerThread.getLooper());
}
nsClientReceiverDelegate =
new NsClientReceiverDelegate(MainApp.instance().getApplicationContext(), MainApp.bus());
}
public boolean isAllowed() {
return nsClientReceiverDelegate.allowed;
}
@Override
protected void onStart() {
MainApp.bus().register(this);
@ -92,9 +100,7 @@ public class NSClientPlugin extends PluginBase {
context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
super.onStart();
EventNetworkChange event = NetworkChangeReceiver.grabNetworkStatus();
if (event != null)
MainApp.bus().post(event);
nsClientReceiverDelegate.registerReceivers();
}
@Override
@ -102,8 +108,26 @@ public class NSClientPlugin extends PluginBase {
MainApp.bus().unregister(this);
Context context = MainApp.instance().getApplicationContext();
context.unbindService(mConnection);
nsClientReceiverDelegate.unregisterReceivers();
}
@Subscribe
public void onStatusEvent(EventPreferenceChange ev) {
nsClientReceiverDelegate.onStatusEvent(ev);
}
@Subscribe
public void onStatusEvent(final EventChargingState ev) {
nsClientReceiverDelegate.onStatusEvent(ev);
}
@Subscribe
public void onStatusEvent(final EventNetworkChange ev) {
nsClientReceiverDelegate.onStatusEvent(ev);
}
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
@ -118,42 +142,13 @@ public class NSClientPlugin extends PluginBase {
nsClientService = mLocalBinder.getServiceInstance();
}
};
@Subscribe
public void onStatusEvent(EventPreferenceChange ev) {
if (ev.isChanged(R.string.key_ns_wifionly) ||
ev.isChanged(R.string.key_ns_wifi_ssids) ||
ev.isChanged(R.string.key_ns_allowroaming)
) {
EventNetworkChange event = NetworkChangeReceiver.grabNetworkStatus();
if (event != null)
MainApp.bus().post(event);
}
}
@Subscribe
public void onStatusEvent(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) && !wifiOnly;
boolean newAllowedState = true;
if (!ev.wifiConnected && wifiOnly) newAllowedState = false;
if (ev.wifiConnected && !allowedSSIDs.isEmpty() && !allowedSSIDs.contains(ev.ssid))
newAllowedState = false;
if (!allowRoaming && ev.roaming) newAllowedState = false;
if (newAllowedState != allowed) {
allowed = newAllowedState;
MainApp.bus().post(new EventPreferenceChange(R.string.key_nsclientinternal_paused));
}
}
@Subscribe
public void onStatusEvent(final EventAppExit ignored) {
if (nsClientService != null)
if (nsClientService != null) {
MainApp.instance().getApplicationContext().unbindService(mConnection);
nsClientReceiverDelegate.unregisterReceivers();
}
}
@Subscribe

View file

@ -0,0 +1,132 @@
package info.nightscout.androidaps.plugins.NSClientInternal;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import com.squareup.otto.Bus;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventChargingState;
import info.nightscout.androidaps.events.EventNetworkChange;
import info.nightscout.androidaps.events.EventPreferenceChange;
import info.nightscout.androidaps.receivers.ChargingStateReceiver;
import info.nightscout.androidaps.receivers.NetworkChangeReceiver;
import info.nightscout.utils.SP;
class NsClientReceiverDelegate {
private final Context context;
private final Bus bus;
private NetworkChangeReceiver networkChangeReceiver = new NetworkChangeReceiver();
private ChargingStateReceiver chargingStateReceiver = new ChargingStateReceiver();
private boolean allowedChargingState = true;
private boolean allowedNetworkState = true;
boolean allowed = true;
NsClientReceiverDelegate(Context context, Bus bus) {
this.context = context;
this.bus = bus;
}
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,
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
context.registerReceiver(networkChangeReceiver,
new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
EventNetworkChange event = networkChangeReceiver.grabNetworkStatus(context);
if (event != null)
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)
bus.post(eventChargingState);
}
void unregisterReceivers() {
context.unregisterReceiver(networkChangeReceiver);
context.unregisterReceiver(chargingStateReceiver);
}
void onStatusEvent(EventPreferenceChange ev) {
if (ev.isChanged(R.string.key_ns_wifionly) ||
ev.isChanged(R.string.key_ns_wifi_ssids) ||
ev.isChanged(R.string.key_ns_allowroaming)
) {
EventNetworkChange event = networkChangeReceiver.grabNetworkStatus(MainApp.instance().getApplicationContext());
if (event != null)
bus.post(event);
} else if (ev.isChanged(R.string.key_ns_chargingonly)) {
EventChargingState event = chargingStateReceiver.grabChargingState(MainApp.instance().getApplicationContext());
if (event != null)
bus.post(event);
}
}
void onStatusEvent(final EventChargingState ev) {
boolean newChargingState = calculateStatus(ev);
if (newChargingState != allowedChargingState) {
allowedChargingState = newChargingState;
processStateChange();
}
}
void onStatusEvent(final EventNetworkChange ev) {
boolean newNetworkState = calculateStatus(ev);
if (newNetworkState != allowedNetworkState) {
allowedNetworkState = newNetworkState;
processStateChange();
}
}
void processStateChange() {
boolean newAllowedState = allowedChargingState && allowedNetworkState;
if (newAllowedState != allowed) {
allowed = newAllowedState;
bus.post(new EventPreferenceChange(R.string.key_nsclientinternal_paused));
}
}
boolean calculateStatus(final EventChargingState ev) {
boolean chargingOnly = SP.getBoolean(R.string.key_ns_chargingonly, false);
boolean newAllowedState = true;
if (!ev.isCharging && chargingOnly) newAllowedState = false;
return newAllowedState;
}
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);
boolean newAllowedState = true;
if (!ev.wifiConnected && wifiOnly) newAllowedState = false;
if (ev.wifiConnected && !allowedSSIDs.trim().isEmpty() && !allowedSSIDs.contains(ev.ssid))
newAllowedState = false;
if (!allowRoaming && ev.roaming) newAllowedState = false;
return newAllowedState;
}
}

View file

@ -33,7 +33,6 @@ import info.nightscout.androidaps.db.DbRequest;
import info.nightscout.androidaps.events.EventAppExit;
import info.nightscout.androidaps.events.EventConfigBuilderChange;
import info.nightscout.androidaps.events.EventPreferenceChange;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.plugins.NSClientInternal.NSClientPlugin;
import info.nightscout.androidaps.plugins.NSClientInternal.UploadQueue;
@ -203,7 +202,7 @@ public class NSClientService extends Service {
nsAPIhashCode = Hashing.sha1().hashString(nsAPISecret, Charsets.UTF_8).toString();
MainApp.bus().post(new EventNSClientStatus("Initializing"));
if (!MainApp.getSpecificPlugin(NSClientPlugin.class).allowed) {
if (!MainApp.getSpecificPlugin(NSClientPlugin.class).isAllowed()) {
MainApp.bus().post(new EventNSClientNewLog("NSCLIENT", "not allowed"));
MainApp.bus().post(new EventNSClientStatus("Not allowed"));
} else if (MainApp.getSpecificPlugin(NSClientPlugin.class).paused) {
@ -251,6 +250,7 @@ public class NSClientService extends Service {
private Emitter.Listener onDisconnect = new Emitter.Listener() {
@Override
public void call(Object... args) {
log.debug("disconnect reason: {}", args);
MainApp.bus().post(new EventNSClientNewLog("NSCLIENT", "disconnect event"));
}
};

View file

@ -0,0 +1,32 @@
package info.nightscout.androidaps.receivers;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.events.EventChargingState;
public class ChargingStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
EventChargingState event = grabChargingState(context);
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;
}
}

View file

@ -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;
}
}

View file

@ -1020,8 +1020,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>

View file

@ -105,6 +105,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">

View file

@ -0,0 +1,115 @@
package info.nightscout.androidaps.plugins.NSClientInternal;
import android.content.Context;
import com.squareup.otto.Bus;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import info.AAPSMocker;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventChargingState;
import info.nightscout.androidaps.events.EventNetworkChange;
import info.nightscout.utils.SP;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class, SP.class, Context.class})
public class NsClientReceiverDelegateTest {
private NsClientReceiverDelegate sut;
@Before
public void prepare() {
AAPSMocker.mockMainApp();
AAPSMocker.mockApplicationContext();
Bus bus = MainApp.bus();
Context context = MainApp.instance().getApplicationContext();
sut = new NsClientReceiverDelegate(context, bus);
}
@Test
public void testCalculateStatusChargingState() {
PowerMockito.mockStatic(SP.class);
when(SP.getBoolean(anyInt(), anyBoolean())).thenReturn(false);
EventChargingState ev = new EventChargingState(true);
assertTrue(sut.calculateStatus(ev));
ev = new EventChargingState(false);
assertTrue(sut.calculateStatus(ev));
when(SP.getBoolean(anyInt(), anyBoolean())).thenReturn(true);
ev = new EventChargingState(true);
assertTrue(sut.calculateStatus(ev));
ev = new EventChargingState(false);
assertTrue(!sut.calculateStatus(ev));
}
@Test
public void testCalculateStatusNetworkState() {
PowerMockito.mockStatic(SP.class);
// wifiOnly = false
// allowRoaming = false as well
when(SP.getBoolean(anyInt(), anyBoolean())).thenReturn(false);
when(SP.getString(anyInt(), anyString())).thenReturn("");
EventNetworkChange ev = new EventNetworkChange();
ev.ssid = "<unknown ssid>";
ev.mobileConnected = true;
ev.wifiConnected = true;
assertTrue(sut.calculateStatus(ev));
ev.wifiConnected = false;
assertTrue(sut.calculateStatus(ev));
// wifiOnly = true
// allowRoaming = true as well
when(SP.getBoolean(anyInt(), anyBoolean())).thenReturn(true);
ev.wifiConnected = true;
assertTrue(sut.calculateStatus(ev));
ev.wifiConnected = false;
assertTrue(!sut.calculateStatus(ev));
// wifiOnly = false
// allowRoaming = false as well
when(SP.getBoolean(anyInt(), anyBoolean())).thenReturn(false);
ev.wifiConnected = false;
ev.roaming = true;
assertTrue(!sut.calculateStatus(ev));
// wifiOnly = false
// allowRoaming = true
when(SP.getBoolean(R.string.key_ns_wifionly, false)).thenReturn(false);
when(SP.getBoolean(R.string.key_ns_allowroaming, true)).thenReturn(true);
ev.wifiConnected = false;
ev.roaming = true;
assertTrue(sut.calculateStatus(ev));
// wifiOnly = true
// allowRoaming = true
when(SP.getBoolean(R.string.key_ns_wifionly, false)).thenReturn(true);
when(SP.getBoolean(R.string.key_ns_allowroaming, true)).thenReturn(true);
ev.wifiConnected = false;
ev.roaming = true;
assertTrue(!sut.calculateStatus(ev));
// wifiOnly = true
// allowRoaming = true
when(SP.getBoolean(R.string.key_ns_wifionly, false)).thenReturn(true);
when(SP.getBoolean(R.string.key_ns_allowroaming, true)).thenReturn(true);
ev.wifiConnected = true;
ev.roaming = true;
assertTrue(sut.calculateStatus(ev));
}
}