Bug 1273689 - Upload in onStop for first run. r=sebastian
authorMichael Comella <michael.l.comella@gmail.com>
Tue, 31 May 2016 15:32:41 -0700
changeset 338907 11a6d5a59addedc2f282e9c4d33cc2884dad00cf
parent 338906 e28024927ff6727dd04f58244fe663f9a0290043
child 338908 8644fcfe29da58befd2c3ea28e230570570af925
push id6249
push userjlund@mozilla.com
push dateMon, 01 Aug 2016 13:59:36 +0000
treeherdermozilla-beta@bad9d4f5bf7e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssebastian
bugs1273689
milestone49.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1273689 - Upload in onStop for first run. r=sebastian MozReview-Commit-ID: 4UReiDeNCg4
mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryCorePingUploadDelegate.java
--- a/mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryCorePingUploadDelegate.java
+++ b/mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryCorePingUploadDelegate.java
@@ -28,29 +28,57 @@ import java.lang.ref.WeakReference;
 
 /**
  * An activity-lifecycle delegate for uploading the core ping.
  */
 public class TelemetryCorePingUploadDelegate extends BrowserAppDelegate {
     private static final String LOGTAG = StringUtils.safeSubstring(
             "Gecko" + TelemetryCorePingUploadDelegate.class.getSimpleName(), 0, 23);
 
+    private static final String PREF_IS_FIRST_RUN = "telemetry-isFirstRun";
+
     private TelemetryDispatcher telemetryDispatcher; // lazy
     private final SessionMeasurements sessionMeasurements = new SessionMeasurements();
 
     @Override
     public void onStart(final BrowserApp browserApp) {
         // We don't upload in onCreate because that's only called when the Activity needs to be instantiated
         // and it's possible the system will never free the Activity from memory.
         //
         // We don't upload in onResume/onPause because that will be called each time the Activity is obscured,
         // including by our own Activities/dialogs, and there is no reason to upload each time we're unobscured.
         //
         // We're left with onStart/onStop and we upload in onStart because onStop is not guaranteed to be called
         // and we want to upload the first run ASAP (e.g. to get install data before the app may crash).
+        uploadPing(browserApp);
+    }
+
+    @Override
+    public void onStop(final BrowserApp browserApp) {
+        // We've decided to upload primarily in onStart (see note there). However, if it's the first run,
+        // it's possible a user used fennec and decided never to return to it again - it'd be great to get
+        // their session information before they decided to give it up so we upload here on first run.
+        //
+        // Caveats:
+        //   * onStop is not guaranteed to be called in low memory conditions so it's possible we won't upload,
+        // but it's better than it was before.
+        //   * Besides first run (because of this call), we can never get the user's *last* session data.
+        //
+        // If we are really interested in the user's last session data, we could consider uploading in onStop
+        // but it's less robust (see discussion in bug 1277091).
+        final SharedPreferences sharedPrefs = getSharedPreferences(browserApp);
+        if (sharedPrefs.getBoolean(PREF_IS_FIRST_RUN, true)) {
+            sharedPrefs.edit()
+                    .putBoolean(PREF_IS_FIRST_RUN, false)
+                    .apply();
+            uploadPing(browserApp);
+        }
+    }
+
+    private void uploadPing(final BrowserApp browserApp) {
         final SearchEngineManager searchEngineManager = browserApp.getSearchEngineManager();
         searchEngineManager.getEngine(new UploadTelemetryCorePingCallback(browserApp));
     }
 
     @Override
     public void onResume(BrowserApp browserApp) {
         sessionMeasurements.recordSessionStart();
     }