From 79c46a23b9f899ff858470bcadfd71b844d0a28b Mon Sep 17 00:00:00 2001 From: Andrew Rowson Date: Wed, 2 Oct 2024 23:50:33 +0100 Subject: [PATCH] feat: log on low memory level --- .../main/java/org/owntracks/android/App.kt | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/project/app/src/main/java/org/owntracks/android/App.kt b/project/app/src/main/java/org/owntracks/android/App.kt index af7f7f8682..5e985914ec 100644 --- a/project/app/src/main/java/org/owntracks/android/App.kt +++ b/project/app/src/main/java/org/owntracks/android/App.kt @@ -6,6 +6,7 @@ import android.app.Application import android.app.Notification import android.app.NotificationChannel import android.app.NotificationManager +import android.content.ComponentCallbacks2 import android.content.Context import android.content.pm.PackageManager import android.os.Build @@ -48,7 +49,11 @@ import org.owntracks.android.test.SimpleIdlingResource import timber.log.Timber @HiltAndroidApp -class App : Application(), Configuration.Provider, Preferences.OnPreferenceChangeListener { +class App : + Application(), + Configuration.Provider, + Preferences.OnPreferenceChangeListener, + ComponentCallbacks2 { @Inject lateinit var preferences: Preferences @Inject lateinit var workerFactory: HiltWorkerFactory @@ -311,6 +316,25 @@ class App : Application(), Configuration.Provider, Preferences.OnPreferenceChang } } + override fun onTrimMemory(level: Int) { + Timber.w( + "onTrimMemory notified ${getAvailableMemory().run { "isLowMemory: $lowMemory availMem: ${android.text.format.Formatter.formatShortFileSize(applicationContext,availMem)}, threshold: ${android.text.format.Formatter.formatShortFileSize(applicationContext,threshold)} totalMemory: ${android.text.format.Formatter.formatShortFileSize(applicationContext,totalMem)} " }}") + super.onTrimMemory(level) + } + + override fun onLowMemory() { + Timber.w( + "onLowMemory notified ${getAvailableMemory().run { "isLowMemory: $lowMemory availMem: ${android.text.format.Formatter.formatShortFileSize(applicationContext,availMem)}, threshold: ${android.text.format.Formatter.formatShortFileSize(applicationContext,threshold)} totalMemory: ${android.text.format.Formatter.formatShortFileSize(applicationContext,totalMem)} " }}") + super.onLowMemory() + } + + private fun getAvailableMemory(): ActivityManager.MemoryInfo { + val activityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager + return ActivityManager.MemoryInfo().also { memoryInfo -> + activityManager.getMemoryInfo(memoryInfo) + } + } + companion object { const val NOTIFICATION_CHANNEL_ONGOING = "O" const val NOTIFICATION_CHANNEL_EVENTS = "E"