Use on Rx for NetworkChange
This commit is contained in:
parent
2fe276d101
commit
9aa77122ba
|
@ -204,7 +204,7 @@ class ObjectivesFragment : Fragment() {
|
|||
holder.accomplished.setTextColor(-0x3e3e3f)
|
||||
holder.verify.setOnClickListener {
|
||||
holder.verify.visibility = View.INVISIBLE
|
||||
NetworkChangeReceiver.fetch()
|
||||
NetworkChangeReceiver.grabNetworkStatus(context)
|
||||
if (objectives_fake.isChecked) {
|
||||
objective.accomplishedOn = DateUtil.now()
|
||||
scrollToCurrentObjective()
|
||||
|
@ -236,7 +236,7 @@ class ObjectivesFragment : Fragment() {
|
|||
}
|
||||
holder.start.setOnClickListener {
|
||||
holder.start.visibility = View.INVISIBLE
|
||||
NetworkChangeReceiver.fetch()
|
||||
NetworkChangeReceiver.grabNetworkStatus(context)
|
||||
if (objectives_fake.isChecked) {
|
||||
objective.startedOn = DateUtil.now()
|
||||
scrollToCurrentObjective()
|
||||
|
|
|
@ -89,7 +89,7 @@ public class NSClientPlugin extends PluginBase {
|
|||
}
|
||||
|
||||
nsClientReceiverDelegate =
|
||||
new NsClientReceiverDelegate(MainApp.instance().getApplicationContext());
|
||||
new NsClientReceiverDelegate();
|
||||
}
|
||||
|
||||
public boolean isAllowed() {
|
||||
|
@ -104,7 +104,7 @@ public class NSClientPlugin extends PluginBase {
|
|||
context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
|
||||
super.onStart();
|
||||
|
||||
nsClientReceiverDelegate.registerReceivers();
|
||||
nsClientReceiverDelegate.grabReceiversState();
|
||||
disposable.add(RxBus.INSTANCE
|
||||
.toObservable(EventNSClientStatus.class)
|
||||
.observeOn(Schedulers.io())
|
||||
|
@ -129,7 +129,6 @@ public class NSClientPlugin extends PluginBase {
|
|||
.subscribe(event -> {
|
||||
if (nsClientService != null) {
|
||||
MainApp.instance().getApplicationContext().unbindService(mConnection);
|
||||
nsClientReceiverDelegate.unregisterReceivers();
|
||||
}
|
||||
}, FabricPrivacy::logException)
|
||||
);
|
||||
|
@ -152,7 +151,6 @@ public class NSClientPlugin extends PluginBase {
|
|||
@Override
|
||||
protected void onStop() {
|
||||
MainApp.instance().getApplicationContext().unbindService(mConnection);
|
||||
nsClientReceiverDelegate.unregisterReceivers();
|
||||
disposable.clear();
|
||||
super.onStop();
|
||||
}
|
||||
|
|
|
@ -18,44 +18,19 @@ import info.nightscout.androidaps.utils.SP;
|
|||
|
||||
class NsClientReceiverDelegate {
|
||||
|
||||
private final Context context;
|
||||
|
||||
private NetworkChangeReceiver networkChangeReceiver = new NetworkChangeReceiver();
|
||||
private ChargingStateReceiver chargingStateReceiver = new ChargingStateReceiver();
|
||||
|
||||
private boolean allowedChargingState = true;
|
||||
private boolean allowedNetworkState = true;
|
||||
boolean allowed = true;
|
||||
|
||||
NsClientReceiverDelegate(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
void registerReceivers() {
|
||||
void grabReceiversState() {
|
||||
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)
|
||||
RxBus.INSTANCE.send(event);
|
||||
EventNetworkChange event = NetworkChangeReceiver.grabNetworkStatus(context);
|
||||
if (event != null) RxBus.INSTANCE.send(event);
|
||||
|
||||
context.registerReceiver(chargingStateReceiver,
|
||||
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
||||
EventChargingState eventChargingState = ChargingStateReceiver.grabChargingState(context);
|
||||
if (eventChargingState != null) RxBus.INSTANCE.send(eventChargingState);
|
||||
|
||||
EventChargingState eventChargingState = chargingStateReceiver.grabChargingState(context);
|
||||
if (eventChargingState != null)
|
||||
RxBus.INSTANCE.send(eventChargingState);
|
||||
|
||||
}
|
||||
|
||||
void unregisterReceivers() {
|
||||
context.unregisterReceiver(networkChangeReceiver);
|
||||
context.unregisterReceiver(chargingStateReceiver);
|
||||
}
|
||||
|
||||
void onStatusEvent(EventPreferenceChange ev) {
|
||||
|
@ -63,11 +38,11 @@ class NsClientReceiverDelegate {
|
|||
ev.isChanged(R.string.key_ns_wifi_ssids) ||
|
||||
ev.isChanged(R.string.key_ns_allowroaming)
|
||||
) {
|
||||
EventNetworkChange event = networkChangeReceiver.grabNetworkStatus(MainApp.instance().getApplicationContext());
|
||||
EventNetworkChange event = NetworkChangeReceiver.grabNetworkStatus(MainApp.instance().getApplicationContext());
|
||||
if (event != null)
|
||||
RxBus.INSTANCE.send(event);
|
||||
} else if (ev.isChanged(R.string.key_ns_chargingonly)) {
|
||||
EventChargingState event = chargingStateReceiver.grabChargingState(MainApp.instance().getApplicationContext());
|
||||
EventChargingState event = ChargingStateReceiver.grabChargingState(MainApp.instance().getApplicationContext());
|
||||
if (event != null)
|
||||
RxBus.INSTANCE.send(event);
|
||||
}
|
||||
|
@ -91,7 +66,7 @@ class NsClientReceiverDelegate {
|
|||
}
|
||||
}
|
||||
|
||||
void processStateChange() {
|
||||
private void processStateChange() {
|
||||
boolean newAllowedState = allowedChargingState && allowedNetworkState;
|
||||
if (newAllowedState != allowed) {
|
||||
allowed = newAllowedState;
|
||||
|
@ -101,7 +76,6 @@ class NsClientReceiverDelegate {
|
|||
|
||||
boolean calculateStatus(final EventChargingState ev) {
|
||||
boolean chargingOnly = SP.getBoolean(R.string.key_ns_chargingonly, false);
|
||||
|
||||
boolean newAllowedState = true;
|
||||
|
||||
if (!ev.isCharging() && chargingOnly) {
|
||||
|
@ -129,8 +103,6 @@ class NsClientReceiverDelegate {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
return newAllowedState;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ public class ChargingStateReceiver extends BroadcastReceiver {
|
|||
lastEvent = event;
|
||||
}
|
||||
|
||||
public EventChargingState grabChargingState(Context context) {
|
||||
public static EventChargingState grabChargingState(Context context) {
|
||||
BatteryManager bm = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
|
||||
|
||||
if (bm == null)
|
||||
|
|
|
@ -26,11 +26,6 @@ public class NetworkChangeReceiver extends BroadcastReceiver {
|
|||
|
||||
public static final NetworkChangeReceiver instance = new NetworkChangeReceiver();
|
||||
|
||||
// TODO: Split NSClient into network state component that can be used by several plugins and logic for plugin
|
||||
public static void fetch() {
|
||||
new NetworkChangeReceiver().grabNetworkStatus(MainApp.instance().getApplicationContext());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(final Context context, final Intent intent) {
|
||||
EventNetworkChange event = grabNetworkStatus(context);
|
||||
|
@ -39,7 +34,7 @@ public class NetworkChangeReceiver extends BroadcastReceiver {
|
|||
}
|
||||
|
||||
@Nullable
|
||||
public EventNetworkChange grabNetworkStatus(final Context context) {
|
||||
public static EventNetworkChange grabNetworkStatus(final Context context) {
|
||||
EventNetworkChange event = new EventNetworkChange();
|
||||
|
||||
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
|
|
|
@ -33,9 +33,7 @@ public class NsClientReceiverDelegateTest {
|
|||
AAPSMocker.mockMainApp();
|
||||
AAPSMocker.mockApplicationContext();
|
||||
|
||||
Context context = MainApp.instance().getApplicationContext();
|
||||
|
||||
sut = new NsClientReceiverDelegate(context);
|
||||
sut = new NsClientReceiverDelegate();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in a new issue