Repository navigation
feat(device-utils): add synchronous first-launch tracking API #331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6c4fed9
feat(device-utils): add synchronous first-launch tracking API
huhuanming 334696d
fix(device-utils): address first-launch PR review findings
huhuanming 83c0b86
refactor(device-utils): use 3 independent entries and add resetFirstL…
huhuanming c7dcc35
Merge remote-tracking branch 'origin/main' into codex/device-utils-fi…
huhuanming 0699f99
fix(device-utils): bind reloaded JS bundle and reject trailing newlin…
huhuanming 7926ab0
fix(device-utils): harden reset, backup exclusion, and legacy seeding
huhuanming 497cc80
fix(device-utils): add initFirstLaunchForBundle for OTA reload bundle…
huhuanming 72f1b1d
fix(device-utils): clear active bundle on invalid version and documen…
huhuanming File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
.../android/src/main/java/com/margelo/nitro/reactnativedeviceutils/DeviceUtilsFirstLaunch.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| package com.margelo.nitro.reactnativedeviceutils | ||
|
|
||
| import android.content.Context | ||
| import android.content.pm.PackageManager | ||
| import java.io.File | ||
|
|
||
| /** | ||
| * Pure native process-wide singleton for first-launch tracking. | ||
| * Has no Nitro/JSI dependencies, so host apps can initialize it early in | ||
| * `Application.onCreate()` before React Native starts. | ||
| */ | ||
| object DeviceUtilsFirstLaunch { | ||
| private val lock = Any() | ||
|
|
||
| @Volatile | ||
| private var history: FirstLaunchHistory? = null | ||
|
|
||
| private fun baseDirectory(context: Context): File? { | ||
| val appContext = context.applicationContext ?: context | ||
| return appContext.noBackupFilesDir | ||
| } | ||
|
|
||
| /** | ||
| * Returns true if the persisted launched marker already exists on disk. | ||
| */ | ||
| @JvmStatic | ||
| fun hasPersistedState(context: Context): Boolean { | ||
| val base = baseDirectory(context) ?: return false | ||
| return File(File(base, FirstLaunchHistory.DIRECTORY_NAME), FirstLaunchHistory.LAUNCHED_FILE_NAME).exists() | ||
| } | ||
|
|
||
| /** | ||
| * Initializes the process-wide first-launch state. Subsequent calls are no-ops. | ||
| */ | ||
| @JvmStatic | ||
| @JvmOverloads | ||
| fun initialize( | ||
| context: Context, | ||
| isLegacyInstall: Boolean = false, | ||
| legacyAppVersion: String? = null, | ||
| ) { | ||
| initialize( | ||
| context = context, | ||
| isLegacyInstall = { isLegacyInstall }, | ||
| legacyAppVersion = { legacyAppVersion }, | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Kotlin-friendly initializer whose legacy-detection lambdas are only evaluated | ||
| * when no persisted launched marker exists yet. | ||
| */ | ||
| fun initialize( | ||
| context: Context, | ||
| isLegacyInstall: () -> Boolean, | ||
| legacyAppVersion: () -> String? = { null }, | ||
| ) { | ||
| if (history != null) return | ||
| synchronized(lock) { | ||
| if (history != null) return | ||
| val appContext = context.applicationContext ?: context | ||
| val base = baseDirectory(appContext) ?: return | ||
| val versionName = try { | ||
| @Suppress("DEPRECATION") | ||
| appContext.packageManager.getPackageInfo(appContext.packageName, 0).versionName | ||
| } catch (_: PackageManager.NameNotFoundException) { | ||
| null | ||
| } catch (_: SecurityException) { | ||
| null | ||
| } | ||
| val persistedExists = | ||
| File(File(base, FirstLaunchHistory.DIRECTORY_NAME), FirstLaunchHistory.LAUNCHED_FILE_NAME).exists() | ||
| val hasPreviousLaunch = if (persistedExists) false else runCatching(isLegacyInstall).getOrDefault(false) | ||
| val previousVersion = if (persistedExists) null else runCatching(legacyAppVersion).getOrNull() | ||
| history = FirstLaunchHistory( | ||
| baseDirectory = base, | ||
| appVersion = versionName, | ||
| previousAppVersion = previousVersion, | ||
| hasPreviousLaunch = hasPreviousLaunch, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Deletes persisted first-launch files and clears in-memory state so a subsequent | ||
| * JS restart within the same native process observes a fresh launch. | ||
| * Returns true if persisted files were removed (or already absent), false on failure. | ||
| */ | ||
| @JvmStatic | ||
| fun reset(context: Context): Boolean { | ||
| synchronized(lock) { | ||
|
huhuanming marked this conversation as resolved.
Resolved
|
||
| val base = baseDirectory(context) ?: return false | ||
| val storeDir = File(base, FirstLaunchHistory.DIRECTORY_NAME) | ||
| if (storeDir.exists()) { | ||
| val deleted = try { | ||
| storeDir.deleteRecursively() && !storeDir.exists() | ||
| } catch (_: SecurityException) { | ||
| false | ||
| } | ||
| if (!deleted) { | ||
| return false | ||
| } | ||
| } | ||
| history = null | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| internal fun isFirstLaunch(fallbackContext: Context? = null): Boolean = synchronized(lock) { | ||
| ensureInitializedLocked(fallbackContext) | ||
| history?.isFirstLaunch ?: false | ||
| } | ||
|
|
||
| internal fun isFirstLaunchForCurrentAppVersion(fallbackContext: Context? = null): Boolean = synchronized(lock) { | ||
| ensureInitializedLocked(fallbackContext) | ||
| history?.isFirstLaunchForCurrentAppVersion ?: false | ||
| } | ||
|
|
||
| internal fun initForBundle( | ||
| bundleVersion: String, | ||
| fallbackContext: Context? = null, | ||
| ) = synchronized(lock) { | ||
| ensureInitializedLocked(fallbackContext) | ||
| history?.initForBundle(bundleVersion) | ||
| } | ||
|
|
||
| internal fun isFirstLaunchForBundle( | ||
| bundleVersion: String?, | ||
| fallbackContext: Context? = null, | ||
| ): Boolean = synchronized(lock) { | ||
| ensureInitializedLocked(fallbackContext) | ||
| history?.isFirstLaunchForBundle(bundleVersion) ?: false | ||
| } | ||
|
|
||
| private fun ensureInitializedLocked(fallbackContext: Context?) { | ||
| if (history == null && fallbackContext != null) { | ||
| initialize(fallbackContext) | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.