ChargingStateReceiver refactor

This commit is contained in:
Milos Kozak 2020-03-31 20:22:32 +02:00
parent e5d7ffb53f
commit 6b80645e06
8 changed files with 60 additions and 76 deletions

View file

@ -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

View file

@ -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

View file

@ -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();
}
}

View file

@ -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()

View file

@ -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;
}
}

View file

@ -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 }
}
}

View file

@ -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) }
}
}

View file

@ -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
@ -42,18 +40,13 @@ class NsClientReceiverDelegateTest : TestBase() {
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() {