From 6b80645e06cb32dffcc6ae871e11fb3c04d4d20b Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 31 Mar 2020 20:22:32 +0200 Subject: [PATCH] ChargingStateReceiver refactor --- .../info/nightscout/androidaps/MainApp.java | 8 +++- .../dependencyInjection/ReceiversModule.kt | 2 + .../nsclient/NsClientReceiverDelegate.java | 18 +------- .../general/tidepool/TidepoolPlugin.kt | 3 +- .../receivers/ChargingStateReceiver.java | 45 ------------------- .../receivers/ChargingStateReceiver.kt | 34 ++++++++++++++ .../receivers/ReceiverStatusStore.kt | 13 +++++- .../nsclient/NsClientReceiverDelegateTest.kt | 13 ++---- 8 files changed, 60 insertions(+), 76 deletions(-) delete mode 100644 app/src/main/java/info/nightscout/androidaps/receivers/ChargingStateReceiver.java create mode 100644 app/src/main/java/info/nightscout/androidaps/receivers/ChargingStateReceiver.kt diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 2d601689d7..6270ac2ffb 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -96,6 +96,7 @@ import info.nightscout.androidaps.receivers.ChargingStateReceiver; import info.nightscout.androidaps.receivers.DataReceiver; import info.nightscout.androidaps.receivers.KeepAliveReceiver; import info.nightscout.androidaps.receivers.NetworkChangeReceiver; +import info.nightscout.androidaps.receivers.ReceiverStatusStore; import info.nightscout.androidaps.receivers.TimeDateOrTZChangeReceiver; import info.nightscout.androidaps.services.Intents; import info.nightscout.androidaps.utils.ActivityMonitor; @@ -121,6 +122,7 @@ public class MainApp extends DaggerApplication { @Inject PluginStore pluginStore; @Inject public HasAndroidInjector injector; @Inject AAPSLogger aapsLogger; + @Inject ReceiverStatusStore receiverStatusStore; @Inject ActivityMonitor activityMonitor; @Inject FabricPrivacy fabricPrivacy; @Inject ResourceHelper resourceHelper; @@ -349,7 +351,11 @@ public class MainApp extends DaggerApplication { filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); registerReceiver(new NetworkChangeReceiver(), filter); - registerReceiver(new ChargingStateReceiver(), new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); + filter = new IntentFilter(); + filter.addAction(Intent.ACTION_POWER_CONNECTED); + filter.addAction(Intent.ACTION_POWER_DISCONNECTED); + filter.addAction(Intent.ACTION_BATTERY_CHANGED); + registerReceiver(new ChargingStateReceiver(), filter); } @Deprecated diff --git a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/ReceiversModule.kt b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/ReceiversModule.kt index 9427d5555a..6c6890fa74 100644 --- a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/ReceiversModule.kt +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/ReceiversModule.kt @@ -3,6 +3,7 @@ package info.nightscout.androidaps.dependencyInjection import dagger.Module import dagger.android.ContributesAndroidInjector import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.RileyLinkBluetoothStateReceiver +import info.nightscout.androidaps.receivers.ChargingStateReceiver import info.nightscout.androidaps.receivers.DataReceiver import info.nightscout.androidaps.receivers.KeepAliveReceiver import info.nightscout.androidaps.receivers.NetworkChangeReceiver @@ -12,6 +13,7 @@ import info.nightscout.androidaps.receivers.TimeDateOrTZChangeReceiver @Suppress("unused") abstract class ReceiversModule { + @ContributesAndroidInjector abstract fun contributesChargingStateReceiver(): ChargingStateReceiver @ContributesAndroidInjector abstract fun contributesDataReceiver(): DataReceiver @ContributesAndroidInjector abstract fun contributesKeepAliveReceiver(): KeepAliveReceiver @ContributesAndroidInjector abstract fun contributesNetworkChangeReceiver(): NetworkChangeReceiver diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegate.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegate.java index 35454f1406..3bc2bde171 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegate.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegate.java @@ -1,7 +1,5 @@ package info.nightscout.androidaps.plugins.general.nsclient; -import android.content.Context; - import javax.inject.Inject; import javax.inject.Singleton; @@ -9,10 +7,7 @@ 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.logging.AAPSLogger; import info.nightscout.androidaps.plugins.bus.RxBusWrapper; -import info.nightscout.androidaps.receivers.ChargingStateReceiver; -import info.nightscout.androidaps.receivers.NetworkChangeReceiver; import info.nightscout.androidaps.receivers.ReceiverStatusStore; import info.nightscout.androidaps.utils.resources.ResourceHelper; import info.nightscout.androidaps.utils.sharedPreferences.SP; @@ -24,8 +19,6 @@ class NsClientReceiverDelegate { private boolean allowedNetworkState = true; boolean allowed = true; - private AAPSLogger aapsLogger; - private Context context; private RxBusWrapper rxBus; private ResourceHelper resourceHelper; private SP sp; @@ -33,15 +26,11 @@ class NsClientReceiverDelegate { @Inject public NsClientReceiverDelegate( - AAPSLogger aapsLogger, - Context context, RxBusWrapper rxBus, ResourceHelper resourceHelper, SP sp, ReceiverStatusStore receiverStatusStore ) { - this.aapsLogger = aapsLogger; - this.context = context; this.rxBus = rxBus; this.resourceHelper = resourceHelper; this.sp = sp; @@ -51,10 +40,6 @@ class NsClientReceiverDelegate { void grabReceiversState() { receiverStatusStore.updateNetworkStatus(); - - EventChargingState eventChargingState = ChargingStateReceiver.grabChargingState(context); - rxBus.send(eventChargingState); - } void onStatusEvent(EventPreferenceChange ev) { @@ -64,8 +49,7 @@ class NsClientReceiverDelegate { ) { receiverStatusStore.updateNetworkStatus(); } else if (ev.isChanged(resourceHelper, R.string.key_ns_chargingonly)) { - EventChargingState event = ChargingStateReceiver.grabChargingState(context); - rxBus.send(event); + receiverStatusStore.broadcastChargingState(); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolPlugin.kt index 9d1a66c29e..701221e361 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolPlugin.kt @@ -25,7 +25,6 @@ import info.nightscout.androidaps.plugins.general.tidepool.events.EventTidepoolR import info.nightscout.androidaps.plugins.general.tidepool.events.EventTidepoolStatus import info.nightscout.androidaps.plugins.general.tidepool.events.EventTidepoolUpdateGUI import info.nightscout.androidaps.plugins.general.tidepool.utils.RateLimit -import info.nightscout.androidaps.receivers.ChargingStateReceiver import info.nightscout.androidaps.receivers.ReceiverStatusStore import info.nightscout.androidaps.utils.FabricPrivacy import info.nightscout.androidaps.utils.HtmlHelper @@ -103,7 +102,7 @@ class TidepoolPlugin @Inject constructor( if (bgReading!!.date < uploadChunk.getLastEnd()) uploadChunk.setLastEnd(bgReading.date) if (isEnabled(PluginType.GENERAL) - && (!sp.getBoolean(R.string.key_tidepool_only_while_charging, false) || ChargingStateReceiver.isCharging()) + && (!sp.getBoolean(R.string.key_tidepool_only_while_charging, false) || receiverStatusStore.isCharging) && (!sp.getBoolean(R.string.key_tidepool_only_while_unmetered, false) || receiverStatusStore.isWifiConnected) && rateLimit.rateLimit("tidepool-new-data-upload", T.mins(4).secs().toInt())) doUpload() diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/ChargingStateReceiver.java b/app/src/main/java/info/nightscout/androidaps/receivers/ChargingStateReceiver.java deleted file mode 100644 index bf8e199c58..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/receivers/ChargingStateReceiver.java +++ /dev/null @@ -1,45 +0,0 @@ -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.events.EventChargingState; -import info.nightscout.androidaps.plugins.bus.RxBus; - -public class ChargingStateReceiver extends BroadcastReceiver { - - private static EventChargingState lastEvent; - - @Override - public void onReceive(Context context, Intent intent) { - EventChargingState event = grabChargingState(context); - - if (event != null) - RxBus.Companion.getINSTANCE().send(event); - lastEvent = event; - } - - public static EventChargingState grabChargingState(Context context) { - BatteryManager bm = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE); - - if (bm == null) - return new EventChargingState(false); - - 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; - } - - static public boolean isCharging() { - return lastEvent != null && lastEvent.isCharging(); - } - - static public EventChargingState getLastEvent() { - return lastEvent; - } -} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/ChargingStateReceiver.kt b/app/src/main/java/info/nightscout/androidaps/receivers/ChargingStateReceiver.kt new file mode 100644 index 0000000000..f6456fc749 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/receivers/ChargingStateReceiver.kt @@ -0,0 +1,34 @@ +package info.nightscout.androidaps.receivers + +import android.content.Context +import android.content.Intent +import android.content.IntentFilter +import android.os.BatteryManager +import dagger.android.DaggerBroadcastReceiver +import info.nightscout.androidaps.events.EventChargingState +import info.nightscout.androidaps.logging.AAPSLogger +import info.nightscout.androidaps.logging.LTag +import info.nightscout.androidaps.plugins.bus.RxBusWrapper +import javax.inject.Inject + +class ChargingStateReceiver : DaggerBroadcastReceiver() { + @Inject lateinit var aapsLogger: AAPSLogger + @Inject lateinit var rxBus: RxBusWrapper + @Inject lateinit var receiverStatusStore: ReceiverStatusStore + + override fun onReceive(context: Context, intent: Intent) { + super.onReceive(context, intent) + rxBus.send(grabChargingState(context)) + aapsLogger.debug(LTag.CORE, receiverStatusStore.lastChargingEvent!!.toString()) + } + + private fun grabChargingState(context: Context): EventChargingState { + val batteryStatus: Intent? = IntentFilter(Intent.ACTION_BATTERY_CHANGED).let { iFilter -> + context.registerReceiver(null, iFilter) + } + val status: Int = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1) ?: -1 + val isCharging: Boolean = status == BatteryManager.BATTERY_STATUS_CHARGING + || status == BatteryManager.BATTERY_STATUS_FULL + return EventChargingState(isCharging).also { receiverStatusStore.lastChargingEvent = it } + } +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/ReceiverStatusStore.kt b/app/src/main/java/info/nightscout/androidaps/receivers/ReceiverStatusStore.kt index 97df6107a7..a55b95f650 100644 --- a/app/src/main/java/info/nightscout/androidaps/receivers/ReceiverStatusStore.kt +++ b/app/src/main/java/info/nightscout/androidaps/receivers/ReceiverStatusStore.kt @@ -2,12 +2,14 @@ package info.nightscout.androidaps.receivers import android.content.Context import android.content.Intent +import info.nightscout.androidaps.events.EventChargingState import info.nightscout.androidaps.events.EventNetworkChange +import info.nightscout.androidaps.plugins.bus.RxBusWrapper import javax.inject.Inject import javax.inject.Singleton @Singleton -class ReceiverStatusStore @Inject constructor(val context: Context) { +class ReceiverStatusStore @Inject constructor(val context: Context, val rxBus: RxBusWrapper) { var lastNetworkEvent: EventNetworkChange? = null @@ -20,4 +22,13 @@ class ReceiverStatusStore @Inject constructor(val context: Context) { fun updateNetworkStatus() { context.sendBroadcast(Intent(context, NetworkChangeReceiver::class.java)) } + + var lastChargingEvent: EventChargingState? = null + + val isCharging: Boolean + get() = lastChargingEvent?.isCharging ?: false + + fun broadcastChargingState() { + lastChargingEvent?.let { rxBus.send(it) } + } } \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegateTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegateTest.kt index 436d8481e7..7388178df6 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegateTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegateTest.kt @@ -6,7 +6,6 @@ import info.nightscout.androidaps.R import info.nightscout.androidaps.TestBase import info.nightscout.androidaps.events.EventChargingState import info.nightscout.androidaps.events.EventNetworkChange -import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.plugins.bus.RxBusWrapper import info.nightscout.androidaps.receivers.ReceiverStatusStore import info.nightscout.androidaps.utils.resources.ResourceHelper @@ -23,7 +22,6 @@ import org.mockito.ArgumentMatchers.anyString import org.mockito.Mock import org.mockito.Mockito import org.mockito.Mockito.`when` -import org.mockito.Mockito.mock import org.powermock.api.mockito.PowerMockito import org.powermock.core.classloader.annotations.PrepareForTest import org.powermock.modules.junit4.PowerMockRunner @@ -36,24 +34,19 @@ class NsClientReceiverDelegateTest : TestBase() { @Mock lateinit var sp: SP @Mock lateinit var resourceHelper: ResourceHelper - lateinit var receiverStatusStore : ReceiverStatusStore + lateinit var receiverStatusStore: ReceiverStatusStore val rxBus: RxBusWrapper = RxBusWrapper() private var sut: NsClientReceiverDelegate? = null @Before fun prepare() { - receiverStatusStore = ReceiverStatusStore(context) - System.setProperty("disableFirebase", "true") - PowerMockito.mockStatic(MainApp::class.java) - val mainApp: MainApp = mock(MainApp::class.java) - `when`(MainApp.instance()).thenReturn(mainApp) - PowerMockito.mockStatic(SP::class.java) + receiverStatusStore = ReceiverStatusStore(context, rxBus) `when`(sp.getLong(anyInt(), anyLong())).thenReturn(0L) `when`(sp.getBoolean(anyInt(), anyBoolean())).thenReturn(false) `when`(sp.getInt(anyInt(), anyInt())).thenReturn(0) `when`(sp.getString(anyInt(), anyString())).thenReturn("") - sut = NsClientReceiverDelegate(aapsLogger, context, rxBus, resourceHelper, sp, receiverStatusStore) + sut = NsClientReceiverDelegate(rxBus, resourceHelper, sp, receiverStatusStore) } @Test fun testCalculateStatusChargingState() {