diff --git a/app/src/main/java/org/mozilla/fenix/FenixApplication.kt b/app/src/main/java/org/mozilla/fenix/FenixApplication.kt index f1f8aa437..dc4cecb98 100644 --- a/app/src/main/java/org/mozilla/fenix/FenixApplication.kt +++ b/app/src/main/java/org/mozilla/fenix/FenixApplication.kt @@ -5,10 +5,7 @@ package org.mozilla.fenix import android.annotation.SuppressLint -import android.app.ActivityManager import android.app.Application -import android.content.Context -import android.os.Process import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch @@ -18,6 +15,8 @@ import mozilla.components.service.fretboard.storage.flatfile.FlatFileExperimentS import mozilla.components.support.base.log.Log import mozilla.components.support.base.log.logger.Logger import mozilla.components.support.base.log.sink.AndroidLogSink +import mozilla.components.support.ktx.android.content.isMainProcess +import mozilla.components.support.ktx.android.content.runOnlyInMainProcess import mozilla.components.support.rustlog.RustLog import org.mozilla.fenix.components.Components import java.io.File @@ -34,7 +33,7 @@ open class FenixApplication : Application() { setupLogging(megazordEnabled) setupCrashReporting() - if (!isMainProcess(this)) { + if (!isMainProcess()) { // If this is not the main process then do not continue with the initialization here. Everything that // follows only needs to be done in our app's main process and should not be done in other processes like // a GeckoView child process or the crash handling process. Most importantly we never want to end up in a @@ -130,25 +129,11 @@ open class FenixApplication : Application() { false } } -} -/** - * Are we running in the main process? - * - * Let's move this code to Android Components: - * https://github.com/mozilla-mobile/android-components/issues/2207 - */ -private fun isMainProcess(context: Context): Boolean { - val pid = Process.myPid() - - val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) - as ActivityManager - - activityManager.runningAppProcesses?.forEach { processInfo -> - if (processInfo.pid == pid) { - return processInfo.processName == context.packageName + override fun onTrimMemory(level: Int) { + super.onTrimMemory(level) + runOnlyInMainProcess { + components.core.sessionManager.onLowMemory() } } - - return false } diff --git a/app/src/main/java/org/mozilla/fenix/browser/BrowserFragment.kt b/app/src/main/java/org/mozilla/fenix/browser/BrowserFragment.kt index eb4a4073b..5c8cfd823 100644 --- a/app/src/main/java/org/mozilla/fenix/browser/BrowserFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/browser/BrowserFragment.kt @@ -4,7 +4,6 @@ package org.mozilla.fenix.browser -import android.content.ComponentCallbacks2 import android.content.Context import android.content.Intent import android.net.Uri @@ -56,7 +55,7 @@ import org.mozilla.fenix.ext.share import org.mozilla.fenix.mvi.ActionBusFactory import org.mozilla.fenix.mvi.getAutoDisposeObservable -class BrowserFragment : Fragment(), BackHandler, ComponentCallbacks2 { +class BrowserFragment : Fragment(), BackHandler { private lateinit var toolbarComponent: ToolbarComponent private val sessionFeature = ViewBoundFeatureWrapper() @@ -284,10 +283,6 @@ class BrowserFragment : Fragment(), BackHandler, ComponentCallbacks2 { promptsFeature.withFeature { it.onActivityResult(requestCode, resultCode, data) } } - override fun onTrimMemory(level: Int) { - requireComponents.core.sessionManager.onLowMemory() - } - // This method triggers the complexity warning. However it's actually not that hard to understand. @SuppressWarnings("ComplexMethod") private fun handleToolbarItemInteraction(action: SearchAction.ToolbarMenuItemTapped) {