Merge remote-tracking branch 'origin/dagger3' into rs

This commit is contained in:
Milos Kozak 2020-03-31 21:16:37 +02:00
commit 295ae79d20
16 changed files with 108 additions and 207 deletions

View file

@ -125,12 +125,6 @@ android {
jvmTarget = '1.8' jvmTarget = '1.8'
} }
lintOptions { lintOptions {
// TODO remove once wear dependency com.google.android.gms:play-services-wearable:7.3.0
// has been upgraded (requiring significant code changes), which currently fails release
// build with a deprecation warning
// abortOnError false
// (disabled entirely to avoid reports on the error, which would still be displayed
// and it's easy to overlook that it's ignored)
checkReleaseBuilds false checkReleaseBuilds false
disable 'MissingTranslation' disable 'MissingTranslation'
disable 'ExtraTranslation' disable 'ExtraTranslation'
@ -300,11 +294,6 @@ dependencies {
} }
testImplementation "org.skyscreamer:jsonassert:1.5.0" testImplementation "org.skyscreamer:jsonassert:1.5.0"
testImplementation "org.hamcrest:hamcrest-all:1.3" testImplementation "org.hamcrest:hamcrest-all:1.3"
/*
testImplementation("uk.org.lidalia:slf4j-test:1.2.0") {
exclude group: "com.google.guava", module: "guava"
}
*/
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

View file

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

View file

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

View file

@ -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.general.nsclient.services.NSClientService import info.nightscout.androidaps.plugins.general.nsclient.services.NSClientService
import info.nightscout.androidaps.plugins.general.overview.notifications.DismissNotificationService
import info.nightscout.androidaps.plugins.general.persistentNotification.DummyService import info.nightscout.androidaps.plugins.general.persistentNotification.DummyService
import info.nightscout.androidaps.plugins.general.wear.wearintegration.WatchUpdaterService import info.nightscout.androidaps.plugins.general.wear.wearintegration.WatchUpdaterService
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.RileyLinkService import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.RileyLinkService
@ -25,6 +26,7 @@ abstract class ServicesModule {
@ContributesAndroidInjector abstract fun contributesAbstractDanaRExecutionService(): AbstractDanaRExecutionService @ContributesAndroidInjector abstract fun contributesAbstractDanaRExecutionService(): AbstractDanaRExecutionService
@ContributesAndroidInjector abstract fun contributesAlarmSoundService(): AlarmSoundService @ContributesAndroidInjector abstract fun contributesAlarmSoundService(): AlarmSoundService
@ContributesAndroidInjector abstract fun contributesDataService(): DataService @ContributesAndroidInjector abstract fun contributesDataService(): DataService
@ContributesAndroidInjector abstract fun contributesDismissNotificationService(): DismissNotificationService
@ContributesAndroidInjector abstract fun contributesDummyService(): DummyService @ContributesAndroidInjector abstract fun contributesDummyService(): DummyService
@ContributesAndroidInjector abstract fun contributesLocationService(): LocationService @ContributesAndroidInjector abstract fun contributesLocationService(): LocationService
@ContributesAndroidInjector abstract fun contributesNSClientService(): NSClientService @ContributesAndroidInjector abstract fun contributesNSClientService(): NSClientService

View file

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

View file

@ -1,38 +0,0 @@
package info.nightscout.androidaps.plugins.general.overview.notifications;
import android.app.IntentService;
import android.app.PendingIntent;
import android.content.Intent;
import androidx.annotation.Nullable;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.plugins.general.overview.events.EventDismissNotification;
public class DismissNotificationService extends IntentService {
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* @param name Used to name the worker thread, important only for debugging.
*/
public DismissNotificationService(String name) {
super(name);
}
public DismissNotificationService() {
super("DismissNotificationService");
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
RxBus.Companion.getINSTANCE().send(new EventDismissNotification(intent.getIntExtra("alertID", -1)));
}
public static PendingIntent deleteIntent(int id) {
Intent intent = new Intent(MainApp.instance(), DismissNotificationService.class);
intent.putExtra("alertID", id);
return PendingIntent.getService(MainApp.instance(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
}

View file

@ -0,0 +1,15 @@
package info.nightscout.androidaps.plugins.general.overview.notifications
import android.content.Intent
import dagger.android.DaggerIntentService
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.general.overview.events.EventDismissNotification
import javax.inject.Inject
class DismissNotificationService : DaggerIntentService(DismissNotificationService::class.simpleName) {
@Inject lateinit var rxBus: RxBusWrapper
override fun onHandleIntent(intent: Intent) {
rxBus.send(EventDismissNotification(intent.getIntExtra("alertID", -1)))
}
}

View file

@ -3,6 +3,7 @@ package info.nightscout.androidaps.plugins.general.overview.notifications
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.NotificationChannel import android.app.NotificationChannel
import android.app.NotificationManager import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.media.AudioManager import android.media.AudioManager
@ -16,7 +17,6 @@ import android.widget.TextView
import androidx.cardview.widget.CardView import androidx.cardview.widget.CardView
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.logging.LTag
@ -36,7 +36,7 @@ class NotificationStore @Inject constructor(
private val sp: SP, private val sp: SP,
private val rxBus: RxBusWrapper, private val rxBus: RxBusWrapper,
private val resourceHelper: ResourceHelper, private val resourceHelper: ResourceHelper,
private val mainApp: MainApp private val context: Context
) { ) {
var store: MutableList<Notification> = ArrayList() var store: MutableList<Notification> = ArrayList()
@ -66,15 +66,15 @@ class NotificationStore @Inject constructor(
if (sp.getBoolean(R.string.key_raise_notifications_as_android_notifications, false) && n !is NotificationWithAction) { if (sp.getBoolean(R.string.key_raise_notifications_as_android_notifications, false) && n !is NotificationWithAction) {
raiseSystemNotification(n) raiseSystemNotification(n)
if (usesChannels && n.soundId != null && n.soundId != 0) { if (usesChannels && n.soundId != null && n.soundId != 0) {
val alarm = Intent(mainApp, AlarmSoundService::class.java) val alarm = Intent(context, AlarmSoundService::class.java)
alarm.putExtra("soundid", n.soundId) alarm.putExtra("soundid", n.soundId)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) mainApp.startForegroundService(alarm) else mainApp.startService(alarm) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) context.startForegroundService(alarm) else context.startService(alarm)
} }
} else { } else {
if (n.soundId != null && n.soundId != 0) { if (n.soundId != null && n.soundId != 0) {
val alarm = Intent(mainApp, AlarmSoundService::class.java) val alarm = Intent(context, AlarmSoundService::class.java)
alarm.putExtra("soundid", n.soundId) alarm.putExtra("soundid", n.soundId)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) mainApp.startForegroundService(alarm) else mainApp.startService(alarm) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) context.startForegroundService(alarm) else context.startService(alarm)
} }
} }
Collections.sort(store, NotificationComparator()) Collections.sort(store, NotificationComparator())
@ -85,8 +85,8 @@ class NotificationStore @Inject constructor(
for (i in store.indices) { for (i in store.indices) {
if (store[i].id == id) { if (store[i].id == id) {
if (store[i].soundId != null) { if (store[i].soundId != null) {
val alarm = Intent(mainApp, AlarmSoundService::class.java) val alarm = Intent(context, AlarmSoundService::class.java)
mainApp.stopService(alarm) context.stopService(alarm)
} }
store.removeAt(i) store.removeAt(i)
return true return true
@ -108,16 +108,16 @@ class NotificationStore @Inject constructor(
} }
private fun raiseSystemNotification(n: Notification) { private fun raiseSystemNotification(n: Notification) {
val mgr = mainApp.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val mgr = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val largeIcon = resourceHelper.decodeResource(resourceHelper.getIcon()) val largeIcon = resourceHelper.decodeResource(resourceHelper.getIcon())
val smallIcon = resourceHelper.getNotificationIcon() val smallIcon = resourceHelper.getNotificationIcon()
val sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM) val sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)
val notificationBuilder = NotificationCompat.Builder(mainApp, CHANNEL_ID) val notificationBuilder = NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(smallIcon) .setSmallIcon(smallIcon)
.setLargeIcon(largeIcon) .setLargeIcon(largeIcon)
.setContentText(n.text) .setContentText(n.text)
.setPriority(NotificationCompat.PRIORITY_MAX) .setPriority(NotificationCompat.PRIORITY_MAX)
.setDeleteIntent(DismissNotificationService.deleteIntent(n.id)) .setDeleteIntent(deleteIntent(n.id))
if (n.level == Notification.URGENT) { if (n.level == Notification.URGENT) {
notificationBuilder.setVibrate(longArrayOf(1000, 1000, 1000, 1000)) notificationBuilder.setVibrate(longArrayOf(1000, 1000, 1000, 1000))
.setContentTitle(resourceHelper.gs(R.string.urgent_alarm)) .setContentTitle(resourceHelper.gs(R.string.urgent_alarm))
@ -129,10 +129,16 @@ class NotificationStore @Inject constructor(
mgr.notify(n.id, notificationBuilder.build()) mgr.notify(n.id, notificationBuilder.build())
} }
private fun deleteIntent(id: Int): PendingIntent {
val intent = Intent(context, DismissNotificationService::class.java)
intent.putExtra("alertID", id)
return PendingIntent.getService(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
fun createNotificationChannel() { fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
usesChannels = true usesChannels = true
val mNotificationManager = mainApp.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
@SuppressLint("WrongConstant") val channel = NotificationChannel(CHANNEL_ID, @SuppressLint("WrongConstant") val channel = NotificationChannel(CHANNEL_ID,
CHANNEL_ID, CHANNEL_ID,
NotificationManager.IMPORTANCE_HIGH) NotificationManager.IMPORTANCE_HIGH)
@ -159,17 +165,19 @@ class NotificationStore @Inject constructor(
clone.addAll(store) clone.addAll(store)
return clone return clone
} }
/*
private fun unSnooze() { /*
if (sp.getBoolean(R.string.key_nsalarm_staledata, false)) { private fun unSnooze() {
val notification = Notification(Notification.NSALARM, resourceHelper.gs(R.string.nsalarm_staledata), Notification.URGENT) if (sp.getBoolean(R.string.key_nsalarm_staledata, false)) {
sp.putLong(R.string.key_snoozedTo, System.currentTimeMillis()) val notification = Notification(Notification.NSALARM, resourceHelper.gs(R.string.nsalarm_staledata), Notification.URGENT)
add(notification) sp.putLong(R.string.key_snoozedTo, System.currentTimeMillis())
aapsLogger.debug(LTag.NOTIFICATION, "Snoozed to current time and added back notification!") add(notification)
aapsLogger.debug(LTag.NOTIFICATION, "Snoozed to current time and added back notification!")
}
} }
} */
*/
inner class NotificationRecyclerViewAdapter internal constructor(private val notificationsList: List<Notification>) : RecyclerView.Adapter<NotificationRecyclerViewAdapter.NotificationsViewHolder>() { inner class NotificationRecyclerViewAdapter internal constructor(private val notificationsList: List<Notification>) : RecyclerView.Adapter<NotificationRecyclerViewAdapter.NotificationsViewHolder>() {
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): NotificationsViewHolder { override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): NotificationsViewHolder {
val v = LayoutInflater.from(viewGroup.context).inflate(R.layout.overview_notification_item, viewGroup, false) val v = LayoutInflater.from(viewGroup.context).inflate(R.layout.overview_notification_item, viewGroup, false)
return NotificationsViewHolder(v) return NotificationsViewHolder(v)

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

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,7 +2,7 @@ package info.nightscout.androidaps.receivers
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import androidx.legacy.content.WakefulBroadcastReceiver import androidx.legacy.content.*
import dagger.android.AndroidInjection import dagger.android.AndroidInjection
import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.logging.LTag

View file

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

View file

@ -1,59 +0,0 @@
package android.util;
import java.time.LocalDateTime;
/**
* Created by andy on 3/10/19.
*/
public class Log {
// 03-10 13:44:42.847 12790-12888/info.nightscout.androidaps D/MedtronicHistoryData:
static boolean isLoggingEnabled = false;
public static void setLoggingEnabled(boolean enabled) {
isLoggingEnabled = enabled;
}
private void writeLog(String type, String tag, String message) {
if (isLoggingEnabled) {
LocalDateTime ldt = LocalDateTime.now();
System.out.println("DEBUG: " + tag + ": " + message);
}
}
public static int d(String tag, String msg) {
System.out.println("DEBUG: " + tag + ": " + msg);
return 0;
}
public static int v(String tag, String msg) {
System.out.println("VERBOSE: " + tag + ": " + msg);
return 0;
}
public static int i(String tag, String msg) {
System.out.println("INFO: " + tag + ": " + msg);
return 0;
}
public static int w(String tag, String msg) {
System.out.println("WARN: " + tag + ": " + msg);
return 0;
}
public static int e(String tag, String msg) {
System.out.println("ERROR: " + tag + ": " + msg);
return 0;
}
// add other methods if required...
}

View file

@ -37,7 +37,7 @@ open class TriggerTestBase : TestBaseWithProfile() {
@Before @Before
fun prepareMock1() { fun prepareMock1() {
receiverStatusStore = ReceiverStatusStore(context) receiverStatusStore = ReceiverStatusStore(context, rxBus)
} }
var injector: HasAndroidInjector = HasAndroidInjector { var injector: HasAndroidInjector = HasAndroidInjector {

View file

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