This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merged AppAllSourceStartTelemetry.kt and AppStartupTypeTelemetry.kt i…
…nto one metric: AppStartupTelemetry.kt wrote test cases for AppStartupTelemetry.kt
- Loading branch information
Showing
14 changed files
with
316 additions
and
414 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
59 changes: 0 additions & 59 deletions
59
app/src/main/java/org/mozilla/fenix/components/metrics/AppAllSourceStartTelemetry.kt
This file was deleted.
Oops, something went wrong.
111 changes: 111 additions & 0 deletions
111
app/src/main/java/org/mozilla/fenix/components/metrics/AppStartupTelemetry.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,111 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package org.mozilla.fenix.components.metrics | ||
|
||
import android.content.Intent | ||
import androidx.annotation.VisibleForTesting | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.LifecycleObserver | ||
import androidx.lifecycle.OnLifecycleEvent | ||
import androidx.lifecycle.ProcessLifecycleOwner | ||
import mozilla.components.support.utils.SafeIntent | ||
import org.mozilla.fenix.components.metrics.Event.AppOpenedAllSourceStartup | ||
import org.mozilla.fenix.components.metrics.Event.AppOpenedAllSourceStartup.Source | ||
import org.mozilla.fenix.components.metrics.Event.AppOpenedAllSourceStartup.Type | ||
import org.mozilla.fenix.components.metrics.Event.AppOpenedAllSourceStartup.Type.COLD | ||
import org.mozilla.fenix.components.metrics.Event.AppOpenedAllSourceStartup.Type.WARM | ||
|
||
/** | ||
* Tracks application startup source, type, and whether or not activity has savedInstance to restore | ||
* the activity from. Sample metric = [source = COLD, type = APP_ICON, hasSavedInstance = false]. | ||
* The basic idea is to collect these metrics from different phases of startup through | ||
* [AppOpenedAllSourceStartup]and finally report them on Activity's onResume() function. | ||
* If application wasn't backgrounded before onResume() is called, we don't report the metric. | ||
* | ||
*/ | ||
class AppStartupTelemetry(private val metrics: MetricController) : LifecycleObserver { | ||
|
||
init { | ||
ProcessLifecycleOwner.get().lifecycle.addObserver(this) | ||
} | ||
|
||
private var appOpenedAllSourceStartup = AppOpenedAllSourceStartup() | ||
|
||
/** default value is true to capture the first launch of the application. */ | ||
private var isMetricRecordedSinceAppWasForegrounded = false | ||
private var wasAppCreateCalledBeforeActivityCreate = false | ||
|
||
fun onFenixApplicationOnCreate() { | ||
wasAppCreateCalledBeforeActivityCreate = true | ||
} | ||
|
||
fun onHomeActivityOnCreate(safeIntent: SafeIntent, hasSavedInstanceState: Boolean) { | ||
appOpenedAllSourceStartup.hasSavedInstance = hasSavedInstanceState | ||
setAppStartupType() | ||
setStartupSourceFromIntent(safeIntent, false) | ||
} | ||
|
||
fun onExternalAppBrowserOnCreate(safeIntent: SafeIntent, hasSavedInstanceState: Boolean) { | ||
appOpenedAllSourceStartup.hasSavedInstance = hasSavedInstanceState | ||
setAppStartupType() | ||
setStartupSourceFromIntent(safeIntent, true) | ||
} | ||
|
||
fun onHomeActivityOnRestart() { | ||
appOpenedAllSourceStartup.type = Type.HOT | ||
appOpenedAllSourceStartup.hasSavedInstance = false | ||
} | ||
|
||
fun receivedIntentInHomeActivity(safeIntent: SafeIntent) { | ||
setStartupSourceFromIntent(safeIntent, false) | ||
} | ||
|
||
private fun setAppStartupType() { | ||
appOpenedAllSourceStartup.type = if (wasAppCreateCalledBeforeActivityCreate) COLD else WARM | ||
wasAppCreateCalledBeforeActivityCreate = false | ||
} | ||
|
||
private fun setStartupSourceFromIntent( | ||
intent: SafeIntent, | ||
isExternalAppBrowserActivity: Boolean | ||
) { | ||
appOpenedAllSourceStartup.source = when { | ||
isExternalAppBrowserActivity -> Source.CUSTOM_TAB | ||
intent.isLauncherIntent -> Source.APP_ICON | ||
intent.action == Intent.ACTION_VIEW -> Source.LINK | ||
else -> Source.UNKNOWN | ||
} | ||
} | ||
|
||
/** | ||
* The reason we record metric on resume is because we need to wait for onNewIntent(), and | ||
* we are not guaranteed that onNewIntent() will be called before or after onStart(). | ||
* However we are guaranteed onResume() will be called after onNewIntent(). Source: | ||
* https://developer.android.com/reference/android/app/Activity#onNewIntent(android.content.Intent) | ||
* | ||
*/ | ||
fun onHomeActivityOnResume() { | ||
recordMetric() | ||
} | ||
|
||
private fun recordMetric() { | ||
if (!isMetricRecordedSinceAppWasForegrounded) { | ||
metrics.track(appOpenedAllSourceStartup) | ||
isMetricRecordedSinceAppWasForegrounded = true | ||
} | ||
// we don't want any weird previous states to persist on our next metric record. | ||
appOpenedAllSourceStartup = AppOpenedAllSourceStartup() | ||
} | ||
|
||
@OnLifecycleEvent(Lifecycle.Event.ON_STOP) | ||
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) | ||
fun onApplicationOnStop() { | ||
|
||
// application was backgrounded, we need to record the new metric type if | ||
// application was to come to foreground again. | ||
// Therefore we set the isMetricRecorded flag to false. | ||
isMetricRecordedSinceAppWasForegrounded = false | ||
} | ||
} |
83 changes: 0 additions & 83 deletions
83
app/src/main/java/org/mozilla/fenix/components/metrics/AppStartupTypeTelemetry.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.