NSClient connection settings

This commit is contained in:
Milos Kozak 2018-04-22 18:24:06 +02:00
parent 68a5523325
commit 6851adbfd3
7 changed files with 162 additions and 7 deletions

View file

@ -15,9 +15,10 @@
<uses-permission android:name="android.permission.SEND_MMS" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.permission.PROVIDE_BACKGROUND" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="sugar.free.sightremote.HISTORY_BROADCASTS" />
<uses-permission-sdk-23 android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
@ -112,6 +113,14 @@
</intent-filter>
</receiver>
<!-- Network change local receiver -->
<receiver android:name=".receivers.NetworkChangeReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
<!-- Service processing incomming data -->
<service
android:name=".Services.DataService"

View file

@ -0,0 +1,10 @@
package info.nightscout.androidaps.events;
public class EventNetworkChange extends Event {
public boolean mobileConnected = false;
public boolean wifiConnected = false;
public String ssid;
public boolean roaming = false;
}

View file

@ -24,6 +24,8 @@ 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.EventNetworkChange;
import info.nightscout.androidaps.events.EventPreferenceChange;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PluginDescription;
import info.nightscout.androidaps.interfaces.PluginType;
@ -31,6 +33,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.NetworkChangeReceiver;
import info.nightscout.utils.SP;
import info.nightscout.utils.ToastUtils;
@ -52,13 +55,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;
public NSClientPlugin() {
private NSClientPlugin() {
super(new PluginDescription()
.mainType(PluginType.GENERAL)
.fragmentClass(NSClientFragment.class.getName())
@ -87,6 +91,10 @@ public class NSClientPlugin extends PluginBase {
Intent intent = new Intent(context, NSClientService.class);
context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
super.onStart();
EventNetworkChange event = NetworkChangeReceiver.grabNetworkStatus();
if (event != null)
MainApp.bus().post(event);
}
@Override
@ -111,9 +119,39 @@ public class NSClientPlugin extends PluginBase {
}
};
@SuppressWarnings("UnusedParameters")
@Subscribe
public void onStatusEvent(final EventAppExit e) {
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);
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)
MainApp.instance().getApplicationContext().unbindService(mConnection);
}

View file

@ -203,7 +203,10 @@ 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).paused) {
if (!MainApp.getSpecificPlugin(NSClientPlugin.class).allowed) {
MainApp.bus().post(new EventNSClientNewLog("NSCLIENT", "not allowed"));
MainApp.bus().post(new EventNSClientStatus("Not allowed"));
} else if (MainApp.getSpecificPlugin(NSClientPlugin.class).paused) {
MainApp.bus().post(new EventNSClientNewLog("NSCLIENT", "paused"));
MainApp.bus().post(new EventNSClientStatus("Paused"));
} else if (!nsEnabled) {
@ -652,7 +655,7 @@ public class NSClientService extends Service {
}
//MainApp.bus().post(new EventNSClientNewLog("NSCLIENT", "onDataUpdate end");
} finally {
if (wakeLock.isHeld()) wakeLock.release();
if (wakeLock.isHeld()) wakeLock.release();
}
}

View file

@ -0,0 +1,67 @@
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.PowerManager;
import android.support.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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();
if (event != null)
MainApp.bus().post(event);
wl.release();
}
@Nullable
public static EventNetworkChange grabNetworkStatus() {
EventNetworkChange event = new EventNetworkChange();
ConnectivityManager cm = (ConnectivityManager) MainApp.instance().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null) return null;
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null) {
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
event.wifiConnected = true;
WifiManager wifiManager = (WifiManager) MainApp.instance().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifiManager != null) {
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if (wifiInfo.getSupplicantState() == SupplicantState.COMPLETED) {
event.ssid = wifiInfo.getSSID();
}
log.debug("NETCHANGE: Wifi connected. SSID: " + event.ssid);
}
}
if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
event.mobileConnected = true;
event.roaming = activeNetwork.isRoaming();
log.debug("NETCHANGE: Mobile connected. Roaming: " + event.roaming);
}
} else {
log.debug("NETCHANGE: Disconnected.");
}
return event;
}
}

View file

@ -1016,4 +1016,12 @@
<string name="secondcarbsincrement">Second carbs increment</string>
<string name="thirdcarbsincrement">Third carbs increment</string>
<string name="cgm">CGM</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_allowroaming" translatable="false">ns_allowroaming</string>
<string name="ns_wifionly">Use WiFi connection only</string>
<string name="ns_wifi_ssids">WiFi SSID</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>
</resources>

View file

@ -85,7 +85,27 @@
</PreferenceScreen>
<PreferenceScreen android:title="@string/advancedsettings_title">
<PreferenceScreen android:title="@string/connectionsettings_title">
<SwitchPreference
android:defaultValue="false"
android:key="@string/key_ns_wifionly"
android:title="@string/ns_wifionly" />
<EditTextPreference
android:dialogMessage="@string/ns_wifi_allowedssids"
android:inputType="text"
android:key="@string/key_ns_wifi_ssids"
android:title="@string/ns_wifi_ssids" />
<SwitchPreference
android:defaultValue="true"
android:key="@string/key_ns_allowroaming"
android:title="@string/ns_allowroaming" />
</PreferenceScreen>
<PreferenceScreen android:title="@string/advancedsettings_title">
<SwitchPreference
android:defaultValue="true"