ActivityMonitor injection

This commit is contained in:
Milos Kozak 2019-12-31 09:41:02 +01:00
parent 7706e51954
commit 1610470089
4 changed files with 38 additions and 28 deletions

View file

@ -124,8 +124,8 @@ public class MainApp extends DaggerApplication {
public static boolean devBranch; public static boolean devBranch;
public static boolean engineeringMode; public static boolean engineeringMode;
@Inject @Inject AAPSLogger aapsLogger;
AAPSLogger aapsLogger; @Inject ActivityMonitor activityMonitor;
@Inject ActionsPlugin actionsPlugin; @Inject ActionsPlugin actionsPlugin;
@Inject AutomationPlugin automationPlugin; @Inject AutomationPlugin automationPlugin;
@ -191,7 +191,7 @@ public class MainApp extends DaggerApplication {
log.error("Error with Fabric init! " + e); log.error("Error with Fabric init! " + e);
} }
registerActivityLifecycleCallbacks(ActivityMonitor.INSTANCE); registerActivityLifecycleCallbacks(activityMonitor);
mFirebaseAnalytics = FirebaseAnalytics.getInstance(this); mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
mFirebaseAnalytics.setAnalyticsCollectionEnabled(!Boolean.getBoolean("disableFirebase") && FabricPrivacy.fabricEnabled()); mFirebaseAnalytics.setAnalyticsCollectionEnabled(!Boolean.getBoolean("disableFirebase") && FabricPrivacy.fabricEnabled());
@ -479,7 +479,7 @@ public class MainApp extends DaggerApplication {
if (timeDateOrTZChangeReceiver != null) if (timeDateOrTZChangeReceiver != null)
unregisterReceiver(timeDateOrTZChangeReceiver); unregisterReceiver(timeDateOrTZChangeReceiver);
unregisterActivityLifecycleCallbacks(ActivityMonitor.INSTANCE); unregisterActivityLifecycleCallbacks(activityMonitor);
KeepAliveReceiver.cancelAlarm(this); KeepAliveReceiver.cancelAlarm(this);
super.onTerminate(); super.onTerminate();
} }

View file

@ -13,6 +13,7 @@ import javax.inject.Inject
class StatsActivity : NoSplashAppCompatActivity() { class StatsActivity : NoSplashAppCompatActivity() {
@Inject lateinit var tddCalculator: TddCalculator @Inject lateinit var tddCalculator: TddCalculator
@Inject lateinit var resourceHelper: ResourceHelper @Inject lateinit var resourceHelper: ResourceHelper
@Inject lateinit var activityMonitor: ActivityMonitor
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -20,12 +21,12 @@ class StatsActivity : NoSplashAppCompatActivity() {
stats_tdds.text = tddCalculator.stats() stats_tdds.text = tddCalculator.stats()
stats_tir.text = TirCalculator.stats() stats_tir.text = TirCalculator.stats()
stats_activity.text = ActivityMonitor.stats() stats_activity.text = activityMonitor.stats()
ok.setOnClickListener { finish() } ok.setOnClickListener { finish() }
stats_reset.setOnClickListener { stats_reset.setOnClickListener {
OKDialog.showConfirmation(this, resourceHelper.gs(R.string.doyouwantresetstats), Runnable { OKDialog.showConfirmation(this, resourceHelper.gs(R.string.doyouwantresetstats), Runnable {
ActivityMonitor.reset() activityMonitor.reset()
recreate() recreate()
}) })
} }

View file

@ -22,6 +22,7 @@ class SurveyActivity : NoSplashAppCompatActivity() {
@Inject lateinit var configBuilderPlugin: ConfigBuilderPlugin @Inject lateinit var configBuilderPlugin: ConfigBuilderPlugin
@Inject lateinit var tddCalculator: TddCalculator @Inject lateinit var tddCalculator: TddCalculator
@Inject lateinit var profileFunction: ProfileFunction @Inject lateinit var profileFunction: ProfileFunction
@Inject lateinit var activityMonitor: ActivityMonitor
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -35,7 +36,7 @@ class SurveyActivity : NoSplashAppCompatActivity() {
survey_tdds.text = tddCalculator.stats() survey_tdds.text = tddCalculator.stats()
survey_tir.text = TirCalculator.stats() survey_tir.text = TirCalculator.stats()
survey_activity.text = ActivityMonitor.stats() survey_activity.text = activityMonitor.stats()
survey_profile.setOnClickListener { survey_profile.setOnClickListener {
val age = SafeParse.stringToDouble(survey_age.text.toString()) val age = SafeParse.stringToDouble(survey_age.text.toString())

View file

@ -4,33 +4,41 @@ import android.app.Activity
import android.app.Application import android.app.Application
import android.os.Bundle import android.os.Bundle
import android.text.Spanned import android.text.Spanned
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
import info.nightscout.androidaps.logging.L import info.nightscout.androidaps.logging.AAPSLogger
import org.slf4j.LoggerFactory import info.nightscout.androidaps.logging.LTag
import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.sharedPreferences.SP
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class ActivityMonitor @Inject constructor(
private var aapsLogger: AAPSLogger,
private val resourceHelper: ResourceHelper,
private var sp: SP
) : Application.ActivityLifecycleCallbacks {
object ActivityMonitor : Application.ActivityLifecycleCallbacks {
private val log = LoggerFactory.getLogger(L.CORE)
override fun onActivityPaused(activity: Activity?) { override fun onActivityPaused(activity: Activity?) {
val name = activity?.javaClass?.simpleName ?: return val name = activity?.javaClass?.simpleName ?: return
val resumed = SP.getLong("Monitor_" + name + "_" + "resumed", 0) val resumed = sp.getLong("Monitor_" + name + "_" + "resumed", 0)
if (resumed == 0L) { if (resumed == 0L) {
log.debug("onActivityPaused: $name resumed == 0") aapsLogger.debug(LTag.UI, "onActivityPaused: $name resumed == 0")
return return
} }
val elapsed = DateUtil.now() - resumed val elapsed = DateUtil.now() - resumed
val total = SP.getLong("Monitor_" + name + "_total", 0) val total = sp.getLong("Monitor_" + name + "_total", 0)
if (total == 0L) { if (total == 0L) {
SP.putLong("Monitor_" + name + "_start", DateUtil.now()) sp.putLong("Monitor_" + name + "_start", DateUtil.now())
} }
SP.putLong("Monitor_" + name + "_total", total + elapsed) sp.putLong("Monitor_" + name + "_total", total + elapsed)
log.debug("onActivityPaused: $name elapsed=$elapsed total=${total + elapsed}") aapsLogger.debug(LTag.UI, "onActivityPaused: $name elapsed=$elapsed total=${total + elapsed}")
} }
override fun onActivityResumed(activity: Activity?) { override fun onActivityResumed(activity: Activity?) {
val name = activity?.javaClass?.simpleName ?: return val name = activity?.javaClass?.simpleName ?: return
log.debug("onActivityResumed: $name") aapsLogger.debug(LTag.UI, "onActivityResumed: $name")
SP.putLong("Monitor_" + name + "_" + "resumed", DateUtil.now()) sp.putLong("Monitor_" + name + "_" + "resumed", DateUtil.now())
} }
override fun onActivityStarted(activity: Activity?) { override fun onActivityStarted(activity: Activity?) {
@ -48,14 +56,14 @@ object ActivityMonitor : Application.ActivityLifecycleCallbacks {
override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) { override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) {
} }
fun toText(): String { private fun toText(): String {
val keys: Map<String, *> = SP.getAll() val keys: Map<String, *> = sp.getAll()
var result = "" var result = ""
for ((key, value) in keys) for ((key, value) in keys)
if (key.startsWith("Monitor") && key.endsWith("total")) { if (key.startsWith("Monitor") && key.endsWith("total")) {
val activity = key.split("_")[1].replace("Activity", "") val activity = key.split("_")[1].replace("Activity", "")
val duration = DateUtil.niceTimeScalar(value as Long) val duration = DateUtil.niceTimeScalar(value as Long)
val start = SP.getLong(key.replace("total", "start"), 0) val start = sp.getLong(key.replace("total", "start"), 0)
val days = T.msecs(DateUtil.now() - start).days() val days = T.msecs(DateUtil.now() - start).days()
result += "<b><span style=\"color:yellow\">$activity:</span></b> <b>$duration</b> in <b>$days</b> days<br>" result += "<b><span style=\"color:yellow\">$activity:</span></b> <b>$duration</b> in <b>$days</b> days<br>"
} }
@ -63,16 +71,16 @@ object ActivityMonitor : Application.ActivityLifecycleCallbacks {
} }
fun stats(): Spanned { fun stats(): Spanned {
return HtmlHelper.fromHtml("<br><b>" + MainApp.gs(R.string.activitymonitor) + ":</b><br>" + toText()) return HtmlHelper.fromHtml("<br><b>" + resourceHelper.gs(R.string.activitymonitor) + ":</b><br>" + toText())
} }
fun reset() { fun reset() {
val keys: Map<String, *> = SP.getAll() val keys: Map<String, *> = sp.getAll()
for ((key, _) in keys) for ((key, _) in keys)
if (key.startsWith("Monitor") && key.endsWith("total")) { if (key.startsWith("Monitor") && key.endsWith("total")) {
SP.remove(key) sp.remove(key)
SP.remove(key.replace("total", "start")) sp.remove(key.replace("total", "start"))
SP.remove(key.replace("total", "resumed")) sp.remove(key.replace("total", "resumed"))
} }
} }
} }