ChargingStateReceiver refactor
This commit is contained in:
parent
e5d7ffb53f
commit
6b80645e06
8 changed files with 60 additions and 76 deletions
|
@ -96,6 +96,7 @@ import info.nightscout.androidaps.receivers.ChargingStateReceiver;
|
||||||
import info.nightscout.androidaps.receivers.DataReceiver;
|
import info.nightscout.androidaps.receivers.DataReceiver;
|
||||||
import info.nightscout.androidaps.receivers.KeepAliveReceiver;
|
import info.nightscout.androidaps.receivers.KeepAliveReceiver;
|
||||||
import info.nightscout.androidaps.receivers.NetworkChangeReceiver;
|
import info.nightscout.androidaps.receivers.NetworkChangeReceiver;
|
||||||
|
import info.nightscout.androidaps.receivers.ReceiverStatusStore;
|
||||||
import info.nightscout.androidaps.receivers.TimeDateOrTZChangeReceiver;
|
import info.nightscout.androidaps.receivers.TimeDateOrTZChangeReceiver;
|
||||||
import info.nightscout.androidaps.services.Intents;
|
import info.nightscout.androidaps.services.Intents;
|
||||||
import info.nightscout.androidaps.utils.ActivityMonitor;
|
import info.nightscout.androidaps.utils.ActivityMonitor;
|
||||||
|
@ -121,6 +122,7 @@ public class MainApp extends DaggerApplication {
|
||||||
@Inject PluginStore pluginStore;
|
@Inject PluginStore pluginStore;
|
||||||
@Inject public HasAndroidInjector injector;
|
@Inject public HasAndroidInjector injector;
|
||||||
@Inject AAPSLogger aapsLogger;
|
@Inject AAPSLogger aapsLogger;
|
||||||
|
@Inject ReceiverStatusStore receiverStatusStore;
|
||||||
@Inject ActivityMonitor activityMonitor;
|
@Inject ActivityMonitor activityMonitor;
|
||||||
@Inject FabricPrivacy fabricPrivacy;
|
@Inject FabricPrivacy fabricPrivacy;
|
||||||
@Inject ResourceHelper resourceHelper;
|
@Inject ResourceHelper resourceHelper;
|
||||||
|
@ -349,7 +351,11 @@ public class MainApp extends DaggerApplication {
|
||||||
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
|
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
|
||||||
registerReceiver(new NetworkChangeReceiver(), filter);
|
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
|
@Deprecated
|
||||||
|
|
|
@ -3,6 +3,7 @@ package info.nightscout.androidaps.dependencyInjection
|
||||||
import dagger.Module
|
import dagger.Module
|
||||||
import dagger.android.ContributesAndroidInjector
|
import dagger.android.ContributesAndroidInjector
|
||||||
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.RileyLinkBluetoothStateReceiver
|
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.DataReceiver
|
||||||
import info.nightscout.androidaps.receivers.KeepAliveReceiver
|
import info.nightscout.androidaps.receivers.KeepAliveReceiver
|
||||||
import info.nightscout.androidaps.receivers.NetworkChangeReceiver
|
import info.nightscout.androidaps.receivers.NetworkChangeReceiver
|
||||||
|
@ -12,6 +13,7 @@ import info.nightscout.androidaps.receivers.TimeDateOrTZChangeReceiver
|
||||||
@Suppress("unused")
|
@Suppress("unused")
|
||||||
abstract class ReceiversModule {
|
abstract class ReceiversModule {
|
||||||
|
|
||||||
|
@ContributesAndroidInjector abstract fun contributesChargingStateReceiver(): ChargingStateReceiver
|
||||||
@ContributesAndroidInjector abstract fun contributesDataReceiver(): DataReceiver
|
@ContributesAndroidInjector abstract fun contributesDataReceiver(): DataReceiver
|
||||||
@ContributesAndroidInjector abstract fun contributesKeepAliveReceiver(): KeepAliveReceiver
|
@ContributesAndroidInjector abstract fun contributesKeepAliveReceiver(): KeepAliveReceiver
|
||||||
@ContributesAndroidInjector abstract fun contributesNetworkChangeReceiver(): NetworkChangeReceiver
|
@ContributesAndroidInjector abstract fun contributesNetworkChangeReceiver(): NetworkChangeReceiver
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
package info.nightscout.androidaps.plugins.general.nsclient;
|
package info.nightscout.androidaps.plugins.general.nsclient;
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
|
@ -9,10 +7,7 @@ import info.nightscout.androidaps.R;
|
||||||
import info.nightscout.androidaps.events.EventChargingState;
|
import info.nightscout.androidaps.events.EventChargingState;
|
||||||
import info.nightscout.androidaps.events.EventNetworkChange;
|
import info.nightscout.androidaps.events.EventNetworkChange;
|
||||||
import info.nightscout.androidaps.events.EventPreferenceChange;
|
import info.nightscout.androidaps.events.EventPreferenceChange;
|
||||||
import info.nightscout.androidaps.logging.AAPSLogger;
|
|
||||||
import info.nightscout.androidaps.plugins.bus.RxBusWrapper;
|
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.receivers.ReceiverStatusStore;
|
||||||
import info.nightscout.androidaps.utils.resources.ResourceHelper;
|
import info.nightscout.androidaps.utils.resources.ResourceHelper;
|
||||||
import info.nightscout.androidaps.utils.sharedPreferences.SP;
|
import info.nightscout.androidaps.utils.sharedPreferences.SP;
|
||||||
|
@ -24,8 +19,6 @@ class NsClientReceiverDelegate {
|
||||||
private boolean allowedNetworkState = true;
|
private boolean allowedNetworkState = true;
|
||||||
boolean allowed = true;
|
boolean allowed = true;
|
||||||
|
|
||||||
private AAPSLogger aapsLogger;
|
|
||||||
private Context context;
|
|
||||||
private RxBusWrapper rxBus;
|
private RxBusWrapper rxBus;
|
||||||
private ResourceHelper resourceHelper;
|
private ResourceHelper resourceHelper;
|
||||||
private SP sp;
|
private SP sp;
|
||||||
|
@ -33,15 +26,11 @@ class NsClientReceiverDelegate {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public NsClientReceiverDelegate(
|
public NsClientReceiverDelegate(
|
||||||
AAPSLogger aapsLogger,
|
|
||||||
Context context,
|
|
||||||
RxBusWrapper rxBus,
|
RxBusWrapper rxBus,
|
||||||
ResourceHelper resourceHelper,
|
ResourceHelper resourceHelper,
|
||||||
SP sp,
|
SP sp,
|
||||||
ReceiverStatusStore receiverStatusStore
|
ReceiverStatusStore receiverStatusStore
|
||||||
) {
|
) {
|
||||||
this.aapsLogger = aapsLogger;
|
|
||||||
this.context = context;
|
|
||||||
this.rxBus = rxBus;
|
this.rxBus = rxBus;
|
||||||
this.resourceHelper = resourceHelper;
|
this.resourceHelper = resourceHelper;
|
||||||
this.sp = sp;
|
this.sp = sp;
|
||||||
|
@ -51,10 +40,6 @@ class NsClientReceiverDelegate {
|
||||||
void grabReceiversState() {
|
void grabReceiversState() {
|
||||||
|
|
||||||
receiverStatusStore.updateNetworkStatus();
|
receiverStatusStore.updateNetworkStatus();
|
||||||
|
|
||||||
EventChargingState eventChargingState = ChargingStateReceiver.grabChargingState(context);
|
|
||||||
rxBus.send(eventChargingState);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void onStatusEvent(EventPreferenceChange ev) {
|
void onStatusEvent(EventPreferenceChange ev) {
|
||||||
|
@ -64,8 +49,7 @@ class NsClientReceiverDelegate {
|
||||||
) {
|
) {
|
||||||
receiverStatusStore.updateNetworkStatus();
|
receiverStatusStore.updateNetworkStatus();
|
||||||
} else if (ev.isChanged(resourceHelper, R.string.key_ns_chargingonly)) {
|
} else if (ev.isChanged(resourceHelper, R.string.key_ns_chargingonly)) {
|
||||||
EventChargingState event = ChargingStateReceiver.grabChargingState(context);
|
receiverStatusStore.broadcastChargingState();
|
||||||
rxBus.send(event);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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.EventTidepoolStatus
|
||||||
import info.nightscout.androidaps.plugins.general.tidepool.events.EventTidepoolUpdateGUI
|
import info.nightscout.androidaps.plugins.general.tidepool.events.EventTidepoolUpdateGUI
|
||||||
import info.nightscout.androidaps.plugins.general.tidepool.utils.RateLimit
|
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.receivers.ReceiverStatusStore
|
||||||
import info.nightscout.androidaps.utils.FabricPrivacy
|
import info.nightscout.androidaps.utils.FabricPrivacy
|
||||||
import info.nightscout.androidaps.utils.HtmlHelper
|
import info.nightscout.androidaps.utils.HtmlHelper
|
||||||
|
@ -103,7 +102,7 @@ class TidepoolPlugin @Inject constructor(
|
||||||
if (bgReading!!.date < uploadChunk.getLastEnd())
|
if (bgReading!!.date < uploadChunk.getLastEnd())
|
||||||
uploadChunk.setLastEnd(bgReading.date)
|
uploadChunk.setLastEnd(bgReading.date)
|
||||||
if (isEnabled(PluginType.GENERAL)
|
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)
|
&& (!sp.getBoolean(R.string.key_tidepool_only_while_unmetered, false) || receiverStatusStore.isWifiConnected)
|
||||||
&& rateLimit.rateLimit("tidepool-new-data-upload", T.mins(4).secs().toInt()))
|
&& rateLimit.rateLimit("tidepool-new-data-upload", T.mins(4).secs().toInt()))
|
||||||
doUpload()
|
doUpload()
|
||||||
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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 }
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,12 +2,14 @@ package info.nightscout.androidaps.receivers
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import info.nightscout.androidaps.events.EventChargingState
|
||||||
import info.nightscout.androidaps.events.EventNetworkChange
|
import info.nightscout.androidaps.events.EventNetworkChange
|
||||||
|
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
class ReceiverStatusStore @Inject constructor(val context: Context) {
|
class ReceiverStatusStore @Inject constructor(val context: Context, val rxBus: RxBusWrapper) {
|
||||||
|
|
||||||
var lastNetworkEvent: EventNetworkChange? = null
|
var lastNetworkEvent: EventNetworkChange? = null
|
||||||
|
|
||||||
|
@ -20,4 +22,13 @@ class ReceiverStatusStore @Inject constructor(val context: Context) {
|
||||||
fun updateNetworkStatus() {
|
fun updateNetworkStatus() {
|
||||||
context.sendBroadcast(Intent(context, NetworkChangeReceiver::class.java))
|
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) }
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -6,7 +6,6 @@ import info.nightscout.androidaps.R
|
||||||
import info.nightscout.androidaps.TestBase
|
import info.nightscout.androidaps.TestBase
|
||||||
import info.nightscout.androidaps.events.EventChargingState
|
import info.nightscout.androidaps.events.EventChargingState
|
||||||
import info.nightscout.androidaps.events.EventNetworkChange
|
import info.nightscout.androidaps.events.EventNetworkChange
|
||||||
import info.nightscout.androidaps.logging.AAPSLogger
|
|
||||||
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
|
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
|
||||||
import info.nightscout.androidaps.receivers.ReceiverStatusStore
|
import info.nightscout.androidaps.receivers.ReceiverStatusStore
|
||||||
import info.nightscout.androidaps.utils.resources.ResourceHelper
|
import info.nightscout.androidaps.utils.resources.ResourceHelper
|
||||||
|
@ -23,7 +22,6 @@ import org.mockito.ArgumentMatchers.anyString
|
||||||
import org.mockito.Mock
|
import org.mockito.Mock
|
||||||
import org.mockito.Mockito
|
import org.mockito.Mockito
|
||||||
import org.mockito.Mockito.`when`
|
import org.mockito.Mockito.`when`
|
||||||
import org.mockito.Mockito.mock
|
|
||||||
import org.powermock.api.mockito.PowerMockito
|
import org.powermock.api.mockito.PowerMockito
|
||||||
import org.powermock.core.classloader.annotations.PrepareForTest
|
import org.powermock.core.classloader.annotations.PrepareForTest
|
||||||
import org.powermock.modules.junit4.PowerMockRunner
|
import org.powermock.modules.junit4.PowerMockRunner
|
||||||
|
@ -36,24 +34,19 @@ class NsClientReceiverDelegateTest : TestBase() {
|
||||||
@Mock lateinit var sp: SP
|
@Mock lateinit var sp: SP
|
||||||
@Mock lateinit var resourceHelper: ResourceHelper
|
@Mock lateinit var resourceHelper: ResourceHelper
|
||||||
|
|
||||||
lateinit var receiverStatusStore : ReceiverStatusStore
|
lateinit var receiverStatusStore: ReceiverStatusStore
|
||||||
val rxBus: RxBusWrapper = RxBusWrapper()
|
val rxBus: RxBusWrapper = RxBusWrapper()
|
||||||
|
|
||||||
private var sut: NsClientReceiverDelegate? = null
|
private var sut: NsClientReceiverDelegate? = null
|
||||||
|
|
||||||
@Before fun prepare() {
|
@Before fun prepare() {
|
||||||
receiverStatusStore = ReceiverStatusStore(context)
|
receiverStatusStore = ReceiverStatusStore(context, rxBus)
|
||||||
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)
|
|
||||||
`when`(sp.getLong(anyInt(), anyLong())).thenReturn(0L)
|
`when`(sp.getLong(anyInt(), anyLong())).thenReturn(0L)
|
||||||
`when`(sp.getBoolean(anyInt(), anyBoolean())).thenReturn(false)
|
`when`(sp.getBoolean(anyInt(), anyBoolean())).thenReturn(false)
|
||||||
`when`(sp.getInt(anyInt(), anyInt())).thenReturn(0)
|
`when`(sp.getInt(anyInt(), anyInt())).thenReturn(0)
|
||||||
`when`(sp.getString(anyInt(), anyString())).thenReturn("")
|
`when`(sp.getString(anyInt(), anyString())).thenReturn("")
|
||||||
|
|
||||||
sut = NsClientReceiverDelegate(aapsLogger, context, rxBus, resourceHelper, sp, receiverStatusStore)
|
sut = NsClientReceiverDelegate(rxBus, resourceHelper, sp, receiverStatusStore)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test fun testCalculateStatusChargingState() {
|
@Test fun testCalculateStatusChargingState() {
|
||||||
|
|
Loading…
Reference in a new issue