commit
e562d0f437
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -5,6 +5,10 @@ public class EventNetworkChange extends Event {
|
||||||
public boolean mobileConnected = false;
|
public boolean mobileConnected = false;
|
||||||
public boolean wifiConnected = false;
|
public boolean wifiConnected = false;
|
||||||
|
|
||||||
public String ssid;
|
public String ssid = "";
|
||||||
public boolean roaming = false;
|
public boolean roaming = false;
|
||||||
|
|
||||||
|
public String getSsid() {
|
||||||
|
return ssid.replace("SSID: ","").replaceAll("\"","");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,12 +18,12 @@ import org.slf4j.LoggerFactory;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import info.nightscout.androidaps.BuildConfig;
|
|
||||||
import info.nightscout.androidaps.Config;
|
import info.nightscout.androidaps.Config;
|
||||||
import info.nightscout.androidaps.Constants;
|
import info.nightscout.androidaps.Constants;
|
||||||
import info.nightscout.androidaps.MainApp;
|
import info.nightscout.androidaps.MainApp;
|
||||||
import info.nightscout.androidaps.R;
|
import info.nightscout.androidaps.R;
|
||||||
import info.nightscout.androidaps.events.EventAppExit;
|
import info.nightscout.androidaps.events.EventAppExit;
|
||||||
|
import info.nightscout.androidaps.events.EventChargingState;
|
||||||
import info.nightscout.androidaps.events.EventNetworkChange;
|
import info.nightscout.androidaps.events.EventNetworkChange;
|
||||||
import info.nightscout.androidaps.events.EventPreferenceChange;
|
import info.nightscout.androidaps.events.EventPreferenceChange;
|
||||||
import info.nightscout.androidaps.interfaces.PluginBase;
|
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.EventNSClientStatus;
|
||||||
import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientUpdateGUI;
|
import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientUpdateGUI;
|
||||||
import info.nightscout.androidaps.plugins.NSClientInternal.services.NSClientService;
|
import info.nightscout.androidaps.plugins.NSClientInternal.services.NSClientService;
|
||||||
import info.nightscout.androidaps.receivers.NetworkChangeReceiver;
|
|
||||||
import info.nightscout.utils.SP;
|
import info.nightscout.utils.SP;
|
||||||
import info.nightscout.utils.ToastUtils;
|
import info.nightscout.utils.ToastUtils;
|
||||||
|
|
||||||
|
@ -55,13 +54,14 @@ public class NSClientPlugin extends PluginBase {
|
||||||
Spanned textLog = Html.fromHtml("");
|
Spanned textLog = Html.fromHtml("");
|
||||||
|
|
||||||
public boolean paused = false;
|
public boolean paused = false;
|
||||||
public boolean allowed = true;
|
|
||||||
boolean autoscroll = true;
|
boolean autoscroll = true;
|
||||||
|
|
||||||
public String status = "";
|
public String status = "";
|
||||||
|
|
||||||
public NSClientService nsClientService = null;
|
public NSClientService nsClientService = null;
|
||||||
|
|
||||||
|
private NsClientReceiverDelegate nsClientReceiverDelegate;
|
||||||
|
|
||||||
private NSClientPlugin() {
|
private NSClientPlugin() {
|
||||||
super(new PluginDescription()
|
super(new PluginDescription()
|
||||||
.mainType(PluginType.GENERAL)
|
.mainType(PluginType.GENERAL)
|
||||||
|
@ -82,8 +82,16 @@ public class NSClientPlugin extends PluginBase {
|
||||||
handlerThread.start();
|
handlerThread.start();
|
||||||
handler = new Handler(handlerThread.getLooper());
|
handler = new Handler(handlerThread.getLooper());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
nsClientReceiverDelegate =
|
||||||
|
new NsClientReceiverDelegate(MainApp.instance().getApplicationContext(), MainApp.bus());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isAllowed() {
|
||||||
|
return nsClientReceiverDelegate.allowed;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onStart() {
|
protected void onStart() {
|
||||||
MainApp.bus().register(this);
|
MainApp.bus().register(this);
|
||||||
|
@ -92,9 +100,7 @@ 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();
|
nsClientReceiverDelegate.registerReceivers();
|
||||||
if (event != null)
|
|
||||||
MainApp.bus().post(event);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -102,8 +108,26 @@ public class NSClientPlugin extends PluginBase {
|
||||||
MainApp.bus().unregister(this);
|
MainApp.bus().unregister(this);
|
||||||
Context context = MainApp.instance().getApplicationContext();
|
Context context = MainApp.instance().getApplicationContext();
|
||||||
context.unbindService(mConnection);
|
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() {
|
private ServiceConnection mConnection = new ServiceConnection() {
|
||||||
|
|
||||||
public void onServiceDisconnected(ComponentName name) {
|
public void onServiceDisconnected(ComponentName name) {
|
||||||
|
@ -119,41 +143,12 @@ public class NSClientPlugin extends PluginBase {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@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
|
@Subscribe
|
||||||
public void onStatusEvent(final EventAppExit ignored) {
|
public void onStatusEvent(final EventAppExit ignored) {
|
||||||
if (nsClientService != null)
|
if (nsClientService != null) {
|
||||||
MainApp.instance().getApplicationContext().unbindService(mConnection);
|
MainApp.instance().getApplicationContext().unbindService(mConnection);
|
||||||
|
nsClientReceiverDelegate.unregisterReceivers();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Subscribe
|
@Subscribe
|
||||||
|
|
|
@ -0,0 +1,138 @@
|
||||||
|
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) {
|
||||||
|
if (!allowedSSIDs.trim().isEmpty() && !allowedSSIDs.contains(ev.ssid)) {
|
||||||
|
newAllowedState = false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if ((!allowRoaming && ev.roaming) || wifiOnly) {
|
||||||
|
newAllowedState = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return newAllowedState;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -33,7 +33,6 @@ import info.nightscout.androidaps.db.DbRequest;
|
||||||
import info.nightscout.androidaps.events.EventAppExit;
|
import info.nightscout.androidaps.events.EventAppExit;
|
||||||
import info.nightscout.androidaps.events.EventConfigBuilderChange;
|
import info.nightscout.androidaps.events.EventConfigBuilderChange;
|
||||||
import info.nightscout.androidaps.events.EventPreferenceChange;
|
import info.nightscout.androidaps.events.EventPreferenceChange;
|
||||||
import info.nightscout.androidaps.interfaces.PluginBase;
|
|
||||||
import info.nightscout.androidaps.interfaces.PluginType;
|
import info.nightscout.androidaps.interfaces.PluginType;
|
||||||
import info.nightscout.androidaps.plugins.NSClientInternal.NSClientPlugin;
|
import info.nightscout.androidaps.plugins.NSClientInternal.NSClientPlugin;
|
||||||
import info.nightscout.androidaps.plugins.NSClientInternal.UploadQueue;
|
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();
|
nsAPIhashCode = Hashing.sha1().hashString(nsAPISecret, Charsets.UTF_8).toString();
|
||||||
|
|
||||||
MainApp.bus().post(new EventNSClientStatus("Initializing"));
|
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 EventNSClientNewLog("NSCLIENT", "not allowed"));
|
||||||
MainApp.bus().post(new EventNSClientStatus("Not allowed"));
|
MainApp.bus().post(new EventNSClientStatus("Not allowed"));
|
||||||
} else if (MainApp.getSpecificPlugin(NSClientPlugin.class).paused) {
|
} else if (MainApp.getSpecificPlugin(NSClientPlugin.class).paused) {
|
||||||
|
@ -251,6 +250,7 @@ public class NSClientService extends Service {
|
||||||
private Emitter.Listener onDisconnect = new Emitter.Listener() {
|
private Emitter.Listener onDisconnect = new Emitter.Listener() {
|
||||||
@Override
|
@Override
|
||||||
public void call(Object... args) {
|
public void call(Object... args) {
|
||||||
|
log.debug("disconnect reason: {}", args);
|
||||||
MainApp.bus().post(new EventNSClientNewLog("NSCLIENT", "disconnect event"));
|
MainApp.bus().post(new EventNSClientNewLog("NSCLIENT", "disconnect event"));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1020,8 +1020,10 @@
|
||||||
<string name="key_ns_wifionly" translatable="false">ns_wifionly</string>
|
<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_wifi_ssids" translatable="false">ns_wifi_ssids</string>
|
||||||
<string name="key_ns_allowroaming" translatable="false">ns_allowroaming</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_wifionly">Use WiFi connection only</string>
|
||||||
<string name="ns_wifi_ssids">WiFi SSID</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="connectionsettings_title">Connection settings</string>
|
||||||
<string name="ns_wifi_allowedssids">Allowed SSIDs (semicolon separated)</string>
|
<string name="ns_wifi_allowedssids">Allowed SSIDs (semicolon separated)</string>
|
||||||
<string name="ns_allowroaming">Allow connection in roaming</string>
|
<string name="ns_allowroaming">Allow connection in roaming</string>
|
||||||
|
|
|
@ -105,6 +105,11 @@
|
||||||
android:key="@string/key_ns_allowroaming"
|
android:key="@string/key_ns_allowroaming"
|
||||||
android:title="@string/ns_allowroaming" />
|
android:title="@string/ns_allowroaming" />
|
||||||
|
|
||||||
|
<SwitchPreference
|
||||||
|
android:defaultValue="false"
|
||||||
|
android:key="@string/key_ns_chargingonly"
|
||||||
|
android:title="@string/ns_chargingonly" />
|
||||||
|
|
||||||
</PreferenceScreen>
|
</PreferenceScreen>
|
||||||
|
|
||||||
<PreferenceScreen android:title="@string/advancedsettings_title">
|
<PreferenceScreen android:title="@string/advancedsettings_title">
|
||||||
|
|
|
@ -0,0 +1,123 @@
|
||||||
|
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));
|
||||||
|
|
||||||
|
// wifiOnly = false
|
||||||
|
// allowRoaming = false
|
||||||
|
when(SP.getBoolean(R.string.key_ns_wifionly, false)).thenReturn(false);
|
||||||
|
when(SP.getBoolean(R.string.key_ns_allowroaming, true)).thenReturn(false);
|
||||||
|
ev.wifiConnected = true;
|
||||||
|
ev.roaming = true;
|
||||||
|
assertTrue(sut.calculateStatus(ev));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue