Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

package com.facebook.react.modules.appstate

import android.app.ActivityManager
import com.facebook.common.logging.FLog
import com.facebook.fbreact.specs.NativeAppStateSpec
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.Callback
Expand All @@ -26,11 +28,27 @@ internal class AppStateModule(reactContext: ReactApplicationContext) :
init {
reactContext.addLifecycleEventListener(this)
reactContext.addWindowFocusChangeListener(this)
val isAppForegroundedByMemoryState = isAppForegroundedByMemoryState()
// pasten: temporary debug log - remove after we validate with real users
FLog.w("AppStateModule", "initial isAppForegroundedByMemoryState = $isAppForegroundedByMemoryState, " +
"reactContext.lifecycleState = ${reactContext.lifecycleState}")

appState =
if (reactContext.lifecycleState === LifecycleState.RESUMED) APP_STATE_ACTIVE
// pasten: during cold start appState=APP_STATE_BACKGROUND while tha is actually in the foreground
// best effort foreground detection when LifecycleState.BEFORE_CREATE (which is the initial state)
else if (reactContext.lifecycleState === LifecycleState.BEFORE_CREATE && isAppForegroundedByMemoryState) {
APP_STATE_ACTIVE
} else if (isAppForegroundedByMemoryState) APP_STATE_ACTIVE
else APP_STATE_BACKGROUND
}

private fun isAppForegroundedByMemoryState(): Boolean {
return ActivityManager.RunningAppProcessInfo().apply {
ActivityManager.getMyMemoryState(this)
}.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
}

public override fun getTypedExportedConstants(): Map<String, Any> =
mapOf(INITIAL_STATE to appState)

Expand Down
Loading