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"