From ec3ebd17dbdc17ba436e2dd7925e63224d933375 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 25 May 2020 13:53:59 +0200 Subject: [PATCH] History activity performance improvement --- .../historyBrowser/HistoryBrowseActivity.kt | 181 ++++++++++-------- .../iobCobCalculator/IobCobOref1Thread.java | 2 + .../iob/iobCobCalculator/IobCobThread.java | 2 + .../events/EventAutosensBgLoaded.kt | 6 + .../androidaps/utils/FabricPrivacy.kt | 1 + 5 files changed, 113 insertions(+), 79 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/events/EventAutosensBgLoaded.kt diff --git a/app/src/main/java/info/nightscout/androidaps/historyBrowser/HistoryBrowseActivity.kt b/app/src/main/java/info/nightscout/androidaps/historyBrowser/HistoryBrowseActivity.kt index 5d0df4dbf3..f484c086a3 100644 --- a/app/src/main/java/info/nightscout/androidaps/historyBrowser/HistoryBrowseActivity.kt +++ b/app/src/main/java/info/nightscout/androidaps/historyBrowser/HistoryBrowseActivity.kt @@ -3,7 +3,6 @@ package info.nightscout.androidaps.historyBrowser import android.app.DatePickerDialog import android.graphics.Color import android.os.Bundle -import android.os.SystemClock import android.util.DisplayMetrics import android.view.ViewGroup import android.widget.LinearLayout @@ -15,12 +14,13 @@ import info.nightscout.androidaps.activities.NoSplashAppCompatActivity import info.nightscout.androidaps.events.EventCustomCalculationFinished import info.nightscout.androidaps.events.EventRefreshOverview import info.nightscout.androidaps.interfaces.ActivePluginProvider +import info.nightscout.androidaps.interfaces.ProfileFunction import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.bus.RxBusWrapper -import info.nightscout.androidaps.interfaces.ProfileFunction import info.nightscout.androidaps.plugins.general.overview.OverviewMenus import info.nightscout.androidaps.plugins.general.overview.graphData.GraphData +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensBgLoaded import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventIobCalculationProgress import info.nightscout.androidaps.utils.DateUtil @@ -33,6 +33,7 @@ import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.sharedPreferences.SP import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.CompositeDisposable +import io.reactivex.schedulers.Schedulers import kotlinx.android.synthetic.main.activity_historybrowse.* import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope @@ -74,12 +75,10 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() { historybrowse_left.setOnClickListener { start -= T.hours(rangeToDisplay.toLong()).msecs() - updateGUI("onClickLeft") runCalculation("onClickLeft") } historybrowse_right.setOnClickListener { start += T.hours(rangeToDisplay.toLong()).msecs() - updateGUI("onClickRight") runCalculation("onClickRight") } historybrowse_end.setOnClickListener { @@ -90,13 +89,12 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() { calendar[Calendar.MINUTE] = 0 calendar[Calendar.HOUR_OF_DAY] = 0 start = calendar.timeInMillis - updateGUI("onClickEnd") runCalculation("onClickEnd") } historybrowse_zoom.setOnClickListener { rangeToDisplay += 6 rangeToDisplay = if (rangeToDisplay > 24) 6 else rangeToDisplay - updateGUI("rangeChange") + updateGUI("rangeChange", false) } historybrowse_zoom.setOnLongClickListener { val calendar = Calendar.getInstance() @@ -106,7 +104,6 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() { calendar[Calendar.MINUTE] = 0 calendar[Calendar.HOUR_OF_DAY] = 0 start = calendar.timeInMillis - updateGUI("resetToMidnight") runCalculation("onLongClickZoom") true } @@ -120,7 +117,6 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() { cal.set(Calendar.DAY_OF_MONTH, dayOfMonth) start = cal.timeInMillis historybrowse_date?.text = dateUtil.dateAndTimeString(start) - updateGUI("onClickDate") runCalculation("onClickDate") } @@ -144,6 +140,11 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() { overviewMenus.setupChartMenu(overview_chartMenuButton) prepareGraphs() + savedInstanceState?.let { bundle -> + rangeToDisplay = bundle.getInt("rangeToDisplay", 0) + start = bundle.getLong("start", 0) + } + } public override fun onPause() { @@ -156,12 +157,21 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() { super.onResume() disposable.add(rxBus .toObservable(EventAutosensCalculationFinished::class.java) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe({ event: EventAutosensCalculationFinished -> + .observeOn(Schedulers.io()) + .subscribe({ // catch only events from iobCobCalculatorPluginHistory - if (event.cause === eventCustomCalculationFinished) { - aapsLogger.debug(LTag.AUTOSENS, "EventAutosensCalculationFinished") - updateGUI("EventAutosensCalculationFinished") + if (it.cause is EventCustomCalculationFinished) { + updateGUI("EventAutosensCalculationFinished", bgOnly = false) + } + }) { fabricPrivacy::logException } + ) + disposable.add(rxBus + .toObservable(EventAutosensBgLoaded::class.java) + .observeOn(Schedulers.io()) + .subscribe({ + // catch only events from iobCobCalculatorPluginHistory + if (it.cause is EventCustomCalculationFinished) { + updateGUI("EventAutosensCalculationFinished", bgOnly = true) } }) { fabricPrivacy::logException } ) @@ -174,21 +184,32 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() { .toObservable(EventRefreshOverview::class.java) .observeOn(AndroidSchedulers.mainThread()) .subscribe({ - prepareGraphs() - updateGUI("EventRefreshOverview") + if (it.now) { + prepareGraphs() + updateGUI("EventRefreshOverview", bgOnly = false) + } }) { fabricPrivacy::logException } ) - // set start of current day - val calendar = Calendar.getInstance() - calendar.timeInMillis = System.currentTimeMillis() - calendar[Calendar.MILLISECOND] = 0 - calendar[Calendar.SECOND] = 0 - calendar[Calendar.MINUTE] = 0 - calendar[Calendar.HOUR_OF_DAY] = 0 - start = calendar.timeInMillis - runCalculation("onResume") - SystemClock.sleep(1000) - updateGUI("onResume") + if (start == 0L) { + // set start of current day + val calendar = Calendar.getInstance() + calendar.timeInMillis = System.currentTimeMillis() + calendar[Calendar.MILLISECOND] = 0 + calendar[Calendar.SECOND] = 0 + calendar[Calendar.MINUTE] = 0 + calendar[Calendar.HOUR_OF_DAY] = 0 + start = calendar.timeInMillis + runCalculation("onResume") + } else { + updateGUI("onResume", bgOnly = false) + } + } + + override fun onSaveInstanceState(outState: Bundle) { + super.onSaveInstanceState(outState) + outState.putInt("rangeToDisplay", rangeToDisplay) + outState.putLong("start", start) + } private fun prepareGraphs() { @@ -221,29 +242,30 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() { } private fun runCalculation(from: String) { - treatmentsPluginHistory.initializeData(start - T.hours(8).msecs()) - val end = start + T.hours(rangeToDisplay.toLong()).msecs() - iobCobCalculatorPluginHistory.stopCalculation(from) - iobCobCalculatorPluginHistory.clearCache() - iobCobCalculatorPluginHistory.runCalculation(from, end, true, false, eventCustomCalculationFinished) + GlobalScope.launch(Dispatchers.Default) { + treatmentsPluginHistory.initializeData(start - T.hours(8).msecs()) + val end = start + T.hours(rangeToDisplay.toLong()).msecs() + iobCobCalculatorPluginHistory.stopCalculation(from) + iobCobCalculatorPluginHistory.clearCache() + iobCobCalculatorPluginHistory.runCalculation(from, end, true, false, eventCustomCalculationFinished) + } } - @Synchronized - fun updateGUI(from: String) { + fun updateGUI(from: String, bgOnly: Boolean) { aapsLogger.debug(LTag.UI, "updateGUI from: $from") val pump = activePlugin.activePump val profile = profileFunction.getProfile() - historybrowse_noprofile?.visibility = (profile == null).toVisibility() - profile ?: return - val lowLine = defaultValueHelper.determineLowLine() val highLine = defaultValueHelper.determineHighLine() - historybrowse_date?.text = dateUtil.dateAndTimeString(start) - historybrowse_zoom?.text = rangeToDisplay.toString() GlobalScope.launch(Dispatchers.Main) { + historybrowse_noprofile?.visibility = (profile == null).toVisibility() + profile ?: return@launch + historybrowse_bggraph ?: return@launch + historybrowse_date?.text = dateUtil.dateAndTimeString(start) + historybrowse_zoom?.text = rangeToDisplay.toString() val graphData = GraphData(injector, historybrowse_bggraph, iobCobCalculatorPluginHistory, treatmentsPluginHistory) val secondaryGraphsData: ArrayList = ArrayList() @@ -263,61 +285,62 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() { // set manual x bounds to have nice steps graphData.formatAxis(fromTime, toTime) - // Treatments - graphData.addTreatments(fromTime, toTime) - if (overviewMenus.setting[0][OverviewMenus.CharType.ACT.ordinal]) - graphData.addActivity(fromTime, toTime, false, 0.8) - - // add basal data - if (pump.pumpDescription.isTempBasalCapable && overviewMenus.setting[0][OverviewMenus.CharType.BAS.ordinal]) { - graphData.addBasals(fromTime, toTime, lowLine / graphData.maxY / 1.2) - } - // add target line graphData.addTargetLine(fromTime, toTime, profile, null) // **** NOW line **** graphData.addNowLine(pointer) - // ------------------ 2nd graph - for (g in 0 until secondaryGraphs.size) { - val secondGraphData = GraphData(injector, secondaryGraphs[g], iobCobCalculatorPluginHistory, treatmentsPluginHistory) - var useIobForScale = false - var useCobForScale = false - var useDevForScale = false - var useRatioForScale = false - var useDSForScale = false - var useIAForScale = false - var useABSForScale = false - when { - overviewMenus.setting[g + 1][OverviewMenus.CharType.IOB.ordinal] -> useIobForScale = true - overviewMenus.setting[g + 1][OverviewMenus.CharType.COB.ordinal] -> useCobForScale = true - overviewMenus.setting[g + 1][OverviewMenus.CharType.DEV.ordinal] -> useDevForScale = true - overviewMenus.setting[g + 1][OverviewMenus.CharType.SEN.ordinal] -> useRatioForScale = true - overviewMenus.setting[g + 1][OverviewMenus.CharType.ACT.ordinal] -> useIAForScale = true - overviewMenus.setting[g + 1][OverviewMenus.CharType.ABS.ordinal] -> useABSForScale = true - overviewMenus.setting[g + 1][OverviewMenus.CharType.DEVSLOPE.ordinal] -> useDSForScale = true + if (!bgOnly) { + // Treatments + graphData.addTreatments(fromTime, toTime) + if (overviewMenus.setting[0][OverviewMenus.CharType.ACT.ordinal]) + graphData.addActivity(fromTime, toTime, false, 0.8) + + // add basal data + if (pump.pumpDescription.isTempBasalCapable && overviewMenus.setting[0][OverviewMenus.CharType.BAS.ordinal]) { + graphData.addBasals(fromTime, toTime, lowLine / graphData.maxY / 1.2) } + // ------------------ 2nd graph + for (g in 0 until secondaryGraphs.size) { + val secondGraphData = GraphData(injector, secondaryGraphs[g], iobCobCalculatorPluginHistory, treatmentsPluginHistory) + var useIobForScale = false + var useCobForScale = false + var useDevForScale = false + var useRatioForScale = false + var useDSForScale = false + var useIAForScale = false + var useABSForScale = false + when { + overviewMenus.setting[g + 1][OverviewMenus.CharType.IOB.ordinal] -> useIobForScale = true + overviewMenus.setting[g + 1][OverviewMenus.CharType.COB.ordinal] -> useCobForScale = true + overviewMenus.setting[g + 1][OverviewMenus.CharType.DEV.ordinal] -> useDevForScale = true + overviewMenus.setting[g + 1][OverviewMenus.CharType.SEN.ordinal] -> useRatioForScale = true + overviewMenus.setting[g + 1][OverviewMenus.CharType.ACT.ordinal] -> useIAForScale = true + overviewMenus.setting[g + 1][OverviewMenus.CharType.ABS.ordinal] -> useABSForScale = true + overviewMenus.setting[g + 1][OverviewMenus.CharType.DEVSLOPE.ordinal] -> useDSForScale = true + } - if (overviewMenus.setting[g + 1][OverviewMenus.CharType.IOB.ordinal]) secondGraphData.addIob(fromTime, toTime, useIobForScale, 1.0, overviewMenus.setting[g + 1][OverviewMenus.CharType.PRE.ordinal]) - if (overviewMenus.setting[g + 1][OverviewMenus.CharType.COB.ordinal]) secondGraphData.addCob(fromTime, toTime, useCobForScale, if (useCobForScale) 1.0 else 0.5) - if (overviewMenus.setting[g + 1][OverviewMenus.CharType.DEV.ordinal]) secondGraphData.addDeviations(fromTime, toTime, useDevForScale, 1.0) - if (overviewMenus.setting[g + 1][OverviewMenus.CharType.SEN.ordinal]) secondGraphData.addRatio(fromTime, toTime, useRatioForScale, 1.0) - if (overviewMenus.setting[g + 1][OverviewMenus.CharType.ACT.ordinal]) secondGraphData.addActivity(fromTime, toTime, useIAForScale, 0.8) - if (overviewMenus.setting[g + 1][OverviewMenus.CharType.ABS.ordinal]) secondGraphData.addAbsIob(fromTime, toTime, useABSForScale, 1.0) - if (overviewMenus.setting[g + 1][OverviewMenus.CharType.DEVSLOPE.ordinal] && buildHelper.isDev()) secondGraphData.addDeviationSlope(fromTime, toTime, useDSForScale, 1.0) + if (overviewMenus.setting[g + 1][OverviewMenus.CharType.IOB.ordinal]) secondGraphData.addIob(fromTime, toTime, useIobForScale, 1.0, overviewMenus.setting[g + 1][OverviewMenus.CharType.PRE.ordinal]) + if (overviewMenus.setting[g + 1][OverviewMenus.CharType.COB.ordinal]) secondGraphData.addCob(fromTime, toTime, useCobForScale, if (useCobForScale) 1.0 else 0.5) + if (overviewMenus.setting[g + 1][OverviewMenus.CharType.DEV.ordinal]) secondGraphData.addDeviations(fromTime, toTime, useDevForScale, 1.0) + if (overviewMenus.setting[g + 1][OverviewMenus.CharType.SEN.ordinal]) secondGraphData.addRatio(fromTime, toTime, useRatioForScale, 1.0) + if (overviewMenus.setting[g + 1][OverviewMenus.CharType.ACT.ordinal]) secondGraphData.addActivity(fromTime, toTime, useIAForScale, 0.8) + if (overviewMenus.setting[g + 1][OverviewMenus.CharType.ABS.ordinal]) secondGraphData.addAbsIob(fromTime, toTime, useABSForScale, 1.0) + if (overviewMenus.setting[g + 1][OverviewMenus.CharType.DEVSLOPE.ordinal] && buildHelper.isDev()) secondGraphData.addDeviationSlope(fromTime, toTime, useDSForScale, 1.0) - // set manual x bounds to have nice steps - secondGraphData.formatAxis(fromTime, toTime) - secondGraphData.addNowLine(pointer) - secondaryGraphsData.add(secondGraphData) + // set manual x bounds to have nice steps + secondGraphData.formatAxis(fromTime, toTime) + secondGraphData.addNowLine(pointer) + secondaryGraphsData.add(secondGraphData) + } } } // finally enforce drawing of graphs in UI thread graphData.performUpdate() for (g in 0 until secondaryGraphs.size) { secondaryGraphsLabel[g].text = overviewMenus.enabledTypes(g + 1) - secondaryGraphs[g].visibility = ( + secondaryGraphs[g].visibility = (!bgOnly && ( overviewMenus.setting[g + 1][OverviewMenus.CharType.IOB.ordinal] || overviewMenus.setting[g + 1][OverviewMenus.CharType.COB.ordinal] || overviewMenus.setting[g + 1][OverviewMenus.CharType.DEV.ordinal] || @@ -325,7 +348,7 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() { overviewMenus.setting[g + 1][OverviewMenus.CharType.ACT.ordinal] || overviewMenus.setting[g + 1][OverviewMenus.CharType.ABS.ordinal] || overviewMenus.setting[g + 1][OverviewMenus.CharType.DEVSLOPE.ordinal] - ).toVisibility() + )).toVisibility() secondaryGraphsData[g].performUpdate() } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobOref1Thread.java b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobOref1Thread.java index ef891ff62c..eb2e4841bc 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobOref1Thread.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobOref1Thread.java @@ -30,6 +30,7 @@ import info.nightscout.androidaps.interfaces.ProfileFunction; import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.general.overview.notifications.Notification; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.data.AutosensData; +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensBgLoaded; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventIobCalculationProgress; import info.nightscout.androidaps.plugins.sensitivity.SensitivityAAPSPlugin; @@ -114,6 +115,7 @@ public class IobCobOref1Thread extends Thread { if (bgDataReload) { iobCobCalculatorPlugin.loadBgData(end); iobCobCalculatorPlugin.createBucketedData(); + rxBus.send(new EventAutosensBgLoaded(cause)); } List bucketed_data = iobCobCalculatorPlugin.getBucketedData(); LongSparseArray autosensDataTable = iobCobCalculatorPlugin.getAutosensDataTable(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobThread.java b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobThread.java index d5af67e3b8..6ea2b7f8b3 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobThread.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobThread.java @@ -28,6 +28,7 @@ import info.nightscout.androidaps.interfaces.ProfileFunction; import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.general.overview.notifications.Notification; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.data.AutosensData; +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensBgLoaded; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventIobCalculationProgress; import info.nightscout.androidaps.plugins.sensitivity.SensitivityAAPSPlugin; @@ -111,6 +112,7 @@ public class IobCobThread extends Thread { if (bgDataReload) { iobCobCalculatorPlugin.loadBgData(end); iobCobCalculatorPlugin.createBucketedData(); + rxBus.send(new EventAutosensBgLoaded(cause)); } List bucketed_data = iobCobCalculatorPlugin.getBucketedData(); LongSparseArray autosensDataTable = iobCobCalculatorPlugin.getAutosensDataTable(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/events/EventAutosensBgLoaded.kt b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/events/EventAutosensBgLoaded.kt new file mode 100644 index 0000000000..b02e96b67f --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/events/EventAutosensBgLoaded.kt @@ -0,0 +1,6 @@ +package info.nightscout.androidaps.plugins.iob.iobCobCalculator.events + +import info.nightscout.androidaps.events.Event +import info.nightscout.androidaps.events.EventLoop + +class EventAutosensBgLoaded(var cause: Event) : EventLoop() diff --git a/core/src/main/java/info/nightscout/androidaps/utils/FabricPrivacy.kt b/core/src/main/java/info/nightscout/androidaps/utils/FabricPrivacy.kt index 9468f21bd4..c5e16878f5 100644 --- a/core/src/main/java/info/nightscout/androidaps/utils/FabricPrivacy.kt +++ b/core/src/main/java/info/nightscout/androidaps/utils/FabricPrivacy.kt @@ -79,6 +79,7 @@ class FabricPrivacy @Inject constructor( try { val crashlytics = Crashlytics.getInstance() crashlytics.core.logException(throwable) + aapsLogger.debug(LTag.CORE, "Exception: ", throwable) } catch (e: NullPointerException) { aapsLogger.debug(LTag.CORE, "Ignoring opted out non-initialized log: $throwable") } catch (e: IllegalStateException) {