Use on Rx for NetworkChange

This commit is contained in:
Milos Kozak 2019-11-21 00:07:50 +01:00
parent 2fe276d101
commit 9aa77122ba
6 changed files with 15 additions and 52 deletions

View file

@ -204,7 +204,7 @@ class ObjectivesFragment : Fragment() {
holder.accomplished.setTextColor(-0x3e3e3f) holder.accomplished.setTextColor(-0x3e3e3f)
holder.verify.setOnClickListener { holder.verify.setOnClickListener {
holder.verify.visibility = View.INVISIBLE holder.verify.visibility = View.INVISIBLE
NetworkChangeReceiver.fetch() NetworkChangeReceiver.grabNetworkStatus(context)
if (objectives_fake.isChecked) { if (objectives_fake.isChecked) {
objective.accomplishedOn = DateUtil.now() objective.accomplishedOn = DateUtil.now()
scrollToCurrentObjective() scrollToCurrentObjective()
@ -236,7 +236,7 @@ class ObjectivesFragment : Fragment() {
} }
holder.start.setOnClickListener { holder.start.setOnClickListener {
holder.start.visibility = View.INVISIBLE holder.start.visibility = View.INVISIBLE
NetworkChangeReceiver.fetch() NetworkChangeReceiver.grabNetworkStatus(context)
if (objectives_fake.isChecked) { if (objectives_fake.isChecked) {
objective.startedOn = DateUtil.now() objective.startedOn = DateUtil.now()
scrollToCurrentObjective() scrollToCurrentObjective()

View file

@ -89,7 +89,7 @@ public class NSClientPlugin extends PluginBase {
} }
nsClientReceiverDelegate = nsClientReceiverDelegate =
new NsClientReceiverDelegate(MainApp.instance().getApplicationContext()); new NsClientReceiverDelegate();
} }
public boolean isAllowed() { public boolean isAllowed() {
@ -104,7 +104,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();
nsClientReceiverDelegate.registerReceivers(); nsClientReceiverDelegate.grabReceiversState();
disposable.add(RxBus.INSTANCE disposable.add(RxBus.INSTANCE
.toObservable(EventNSClientStatus.class) .toObservable(EventNSClientStatus.class)
.observeOn(Schedulers.io()) .observeOn(Schedulers.io())
@ -129,7 +129,6 @@ public class NSClientPlugin extends PluginBase {
.subscribe(event -> { .subscribe(event -> {
if (nsClientService != null) { if (nsClientService != null) {
MainApp.instance().getApplicationContext().unbindService(mConnection); MainApp.instance().getApplicationContext().unbindService(mConnection);
nsClientReceiverDelegate.unregisterReceivers();
} }
}, FabricPrivacy::logException) }, FabricPrivacy::logException)
); );
@ -152,7 +151,6 @@ public class NSClientPlugin extends PluginBase {
@Override @Override
protected void onStop() { protected void onStop() {
MainApp.instance().getApplicationContext().unbindService(mConnection); MainApp.instance().getApplicationContext().unbindService(mConnection);
nsClientReceiverDelegate.unregisterReceivers();
disposable.clear(); disposable.clear();
super.onStop(); super.onStop();
} }

View file

@ -18,44 +18,19 @@ import info.nightscout.androidaps.utils.SP;
class NsClientReceiverDelegate { class NsClientReceiverDelegate {
private final Context context;
private NetworkChangeReceiver networkChangeReceiver = new NetworkChangeReceiver();
private ChargingStateReceiver chargingStateReceiver = new ChargingStateReceiver();
private boolean allowedChargingState = true; private boolean allowedChargingState = true;
private boolean allowedNetworkState = true; private boolean allowedNetworkState = true;
boolean allowed = true; boolean allowed = true;
NsClientReceiverDelegate(Context context) { void grabReceiversState() {
this.context = context;
}
void registerReceivers() {
Context context = MainApp.instance().getApplicationContext(); 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); EventNetworkChange event = NetworkChangeReceiver.grabNetworkStatus(context);
if (event != null) if (event != null) RxBus.INSTANCE.send(event);
RxBus.INSTANCE.send(event);
context.registerReceiver(chargingStateReceiver, EventChargingState eventChargingState = ChargingStateReceiver.grabChargingState(context);
new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 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) { void onStatusEvent(EventPreferenceChange ev) {
@ -63,11 +38,11 @@ class NsClientReceiverDelegate {
ev.isChanged(R.string.key_ns_wifi_ssids) || ev.isChanged(R.string.key_ns_wifi_ssids) ||
ev.isChanged(R.string.key_ns_allowroaming) ev.isChanged(R.string.key_ns_allowroaming)
) { ) {
EventNetworkChange event = networkChangeReceiver.grabNetworkStatus(MainApp.instance().getApplicationContext()); EventNetworkChange event = NetworkChangeReceiver.grabNetworkStatus(MainApp.instance().getApplicationContext());
if (event != null) if (event != null)
RxBus.INSTANCE.send(event); RxBus.INSTANCE.send(event);
} else if (ev.isChanged(R.string.key_ns_chargingonly)) { } 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) if (event != null)
RxBus.INSTANCE.send(event); RxBus.INSTANCE.send(event);
} }
@ -91,7 +66,7 @@ class NsClientReceiverDelegate {
} }
} }
void processStateChange() { private void processStateChange() {
boolean newAllowedState = allowedChargingState && allowedNetworkState; boolean newAllowedState = allowedChargingState && allowedNetworkState;
if (newAllowedState != allowed) { if (newAllowedState != allowed) {
allowed = newAllowedState; allowed = newAllowedState;
@ -101,7 +76,6 @@ class NsClientReceiverDelegate {
boolean calculateStatus(final EventChargingState ev) { boolean calculateStatus(final EventChargingState ev) {
boolean chargingOnly = SP.getBoolean(R.string.key_ns_chargingonly, false); boolean chargingOnly = SP.getBoolean(R.string.key_ns_chargingonly, false);
boolean newAllowedState = true; boolean newAllowedState = true;
if (!ev.isCharging() && chargingOnly) { if (!ev.isCharging() && chargingOnly) {
@ -129,8 +103,6 @@ class NsClientReceiverDelegate {
} }
} }
return newAllowedState; return newAllowedState;
} }
} }

View file

@ -21,7 +21,7 @@ public class ChargingStateReceiver extends BroadcastReceiver {
lastEvent = event; lastEvent = event;
} }
public EventChargingState grabChargingState(Context context) { public static EventChargingState grabChargingState(Context context) {
BatteryManager bm = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE); BatteryManager bm = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
if (bm == null) if (bm == null)

View file

@ -26,11 +26,6 @@ public class NetworkChangeReceiver extends BroadcastReceiver {
public static final NetworkChangeReceiver instance = new NetworkChangeReceiver(); 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 @Override
public void onReceive(final Context context, final Intent intent) { public void onReceive(final Context context, final Intent intent) {
EventNetworkChange event = grabNetworkStatus(context); EventNetworkChange event = grabNetworkStatus(context);
@ -39,7 +34,7 @@ public class NetworkChangeReceiver extends BroadcastReceiver {
} }
@Nullable @Nullable
public EventNetworkChange grabNetworkStatus(final Context context) { public static EventNetworkChange grabNetworkStatus(final Context context) {
EventNetworkChange event = new EventNetworkChange(); EventNetworkChange event = new EventNetworkChange();
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

View file

@ -33,9 +33,7 @@ public class NsClientReceiverDelegateTest {
AAPSMocker.mockMainApp(); AAPSMocker.mockMainApp();
AAPSMocker.mockApplicationContext(); AAPSMocker.mockApplicationContext();
Context context = MainApp.instance().getApplicationContext(); sut = new NsClientReceiverDelegate();
sut = new NsClientReceiverDelegate(context);
} }
@Test @Test