-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
88224f5
commit 24d2892
Showing
14 changed files
with
226 additions
and
111 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
54 changes: 54 additions & 0 deletions
54
android/app-newm/src/main/java/io/newm/utils/AppForegroundBackgroundTracker.kt
This file contains 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,54 @@ | ||
package io.newm.utils | ||
|
||
import android.app.Activity | ||
import android.app.Application | ||
import android.os.Bundle | ||
import io.newm.shared.NewmAppLogger | ||
import io.newm.shared.public.analytics.NewmAppAnalyticsTracker | ||
|
||
class AppForegroundBackgroundTracker( | ||
private val analyticsTracker: NewmAppAnalyticsTracker, | ||
private val logger: NewmAppLogger | ||
) : Application.ActivityLifecycleCallbacks { | ||
|
||
private val TAG: String = AppForegroundBackgroundTracker::class.java.simpleName | ||
private var activityReferences = 0 | ||
private var isActivityChangingConfigurations = false | ||
|
||
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { | ||
if (activityReferences == 0 && !isActivityChangingConfigurations) { | ||
// Track initial app launch when first activity is created | ||
try { | ||
analyticsTracker.trackAppLaunch() | ||
} catch (e: Exception) { | ||
logger.error(TAG, "Error tracking app launch", e) | ||
} | ||
} | ||
activityReferences++ | ||
} | ||
|
||
override fun onActivityStarted(activity: Activity) { | ||
activityReferences++ | ||
} | ||
|
||
override fun onActivityResumed(activity: Activity) {} | ||
|
||
override fun onActivityPaused(activity: Activity) {} | ||
|
||
override fun onActivityStopped(activity: Activity) { | ||
activityReferences-- | ||
isActivityChangingConfigurations = activity.isChangingConfigurations | ||
if (activityReferences == 0 && !isActivityChangingConfigurations) { | ||
// Track app closure when all activities are stopped and none are changing configurations | ||
try { | ||
analyticsTracker.trackAppClose() | ||
} catch (e: Exception) { | ||
logger.error(TAG, "Error tracking app close", e) | ||
} | ||
} | ||
} | ||
|
||
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {} | ||
|
||
override fun onActivityDestroyed(activity: Activity) {} | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.