Skip to content

Commit

Permalink
feat: log on low memory level
Browse files Browse the repository at this point in the history
  • Loading branch information
growse committed Oct 2, 2024
1 parent bb71eff commit 79c46a2
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion project/app/src/main/java/org/owntracks/android/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 79c46a2

Please sign in to comment.