Bug 1361664 - Part1. Udate MMA API, make init() accept activity. r?maliu draft
authorNevin Chen <cnevinchen@gmail.com>
Wed, 24 May 2017 11:23:10 +0800
changeset 586189 248484ac438ec47fc7b91048c0ffaa8d6a559ad3
parent 586049 34ac1a5d6576d6775491c8a882710a1520551da6
child 586190 ae3d5b425ce894bdab1ff7d8e574e9ae3b58596b
push id61326
push userbmo:cnevinchen@gmail.com
push dateMon, 29 May 2017 22:42:47 +0000
reviewersmaliu
bugs1361664
milestone55.0a1
Bug 1361664 - Part1. Udate MMA API, make init() accept activity. r?maliu MozReview-Commit-ID: AcfoeCPV8SV
mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
mobile/android/base/java/org/mozilla/gecko/mma/MmaDelegate.java
mobile/android/base/java/org/mozilla/gecko/mma/MmaInterface.java
mobile/android/base/java/org/mozilla/gecko/mma/MmaLeanplumImp.java
mobile/android/base/java/org/mozilla/gecko/mma/MmaStubImp.java
mobile/android/thirdparty/com/leanplum/LeanplumActivityHelper.java
--- a/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
+++ b/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
@@ -890,17 +890,17 @@ public class BrowserApp extends GeckoApp
         new AsyncConfigLoader(context, serverUrl) {
             @Override
             protected Void doInBackground(Void... params) {
                 super.doInBackground(params);
                 SwitchBoard.loadConfig(context, serverUrl);
                 if (SwitchBoard.isInExperiment(context, Experiments.LEANPLUM) &&
                         GeckoPreferences.getBooleanPref(context, GeckoPreferences.PREFS_HEALTHREPORT_UPLOAD_ENABLED, true)) {
                     // Do LeanPlum start/init here
-                    MmaDelegate.init(BrowserApp.this.getApplication());
+                    MmaDelegate.init(BrowserApp.this);
                 }
                 return null;
             }
         }.execute();
     }
 
     private static void initTelemetryUploader(final boolean isInAutomation) {
         TelemetryUploadService.setDisabled(isInAutomation);
--- a/mobile/android/base/java/org/mozilla/gecko/mma/MmaDelegate.java
+++ b/mobile/android/base/java/org/mozilla/gecko/mma/MmaDelegate.java
@@ -1,49 +1,64 @@
 //#filter substitution
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
  * 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.gecko.mma;
 
-import android.app.Application;
-import android.content.Context;
+import android.app.Activity;
+import android.util.Log;
 
 import org.mozilla.gecko.MmaConstants;
 import org.mozilla.gecko.PrefsHelper;
 
 
 public class MmaDelegate {
 
-    private static final String ENABLE_PREF = "mma.enabled";
+    private static final String TAG = "MmaDelegate";
+    private static final String KEY_PREF_BOOLEAN_MMA_ENABLED = "mma.enabled";
+    private static final String[] PREFS = { KEY_PREF_BOOLEAN_MMA_ENABLED };
 
+
+    private static boolean isGeckoPrefOn = false;
     private static MmaInterface mmaHelper = MmaConstants.getMma();
 
-    private static final String[] prefs = { ENABLE_PREF };
 
-
-    public static void init(Application application) {
-        setupPrefHandler(application);
+    public static void init(Activity activity) {
+        setupPrefHandler(activity);
     }
 
     public static void stop() {
         mmaHelper.stop();
     }
 
-    private static void setupPrefHandler(final Application application) {
+    private static void setupPrefHandler(final Activity activity) {
         PrefsHelper.PrefHandler handler = new PrefsHelper.PrefHandlerBase() {
             @Override
             public void prefValue(String pref, boolean value) {
-                if (pref.equals(ENABLE_PREF)) {
+                if (pref.equals(KEY_PREF_BOOLEAN_MMA_ENABLED)) {
+                    Log.d(TAG, "prefValue() called with: pref = [" + pref + "], value = [" + value + "]");
                     if (value) {
-                        mmaHelper.init(application);
+                        mmaHelper.init(activity);
+                        isGeckoPrefOn = true;
                     } else {
-                        mmaHelper.stop();
+                        isGeckoPrefOn = false;
                     }
-
                 }
             }
         };
-        PrefsHelper.addObserver(prefs, handler);
+        PrefsHelper.addObserver(PREFS, handler);
+    }
+
+    public static void track(String event) {
+        if (isGeckoPrefOn) {
+            mmaHelper.track(event);
+        }
+    }
+
+    public static void track(String event, long value) {
+        if (isGeckoPrefOn) {
+            mmaHelper.track(event, value);
+        }
     }
 }
--- a/mobile/android/base/java/org/mozilla/gecko/mma/MmaInterface.java
+++ b/mobile/android/base/java/org/mozilla/gecko/mma/MmaInterface.java
@@ -1,23 +1,24 @@
 //#filter substitution
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
  * 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.gecko.mma;
 
+import android.app.Activity;
 import android.app.Application;
 import android.content.Context;
 
 
 public interface MmaInterface {
-    void init(Application application);
+    void init(Activity Activity);
 
     void start(Context context);
 
-    void track(String leanplumEvent);
+    void track(String mmaEvent);
 
-    void track(String leanplumEvent, double value);
+    void track(String mmaEvent, double value);
 
     void stop();
 }
--- a/mobile/android/base/java/org/mozilla/gecko/mma/MmaLeanplumImp.java
+++ b/mobile/android/base/java/org/mozilla/gecko/mma/MmaLeanplumImp.java
@@ -1,41 +1,58 @@
 //#filter substitution
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
  * 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.gecko.mma;
 
+import android.app.Activity;
 import android.app.Application;
 import android.content.Context;
 
 import com.leanplum.Leanplum;
 import com.leanplum.LeanplumActivityHelper;
 import com.leanplum.annotations.Parser;
 
+import org.mozilla.gecko.ActivityHandlerHelper;
 import org.mozilla.gecko.AppConstants;
 import org.mozilla.gecko.MmaConstants;
+import org.mozilla.gecko.util.ContextUtils;
+
+import java.util.HashMap;
+import java.util.Map;
 
 
 public class MmaLeanplumImp implements MmaInterface {
     @Override
-    public void init(Application application) {
-        Leanplum.setApplicationContext(application);
-        Parser.parseVariables(application);
-        LeanplumActivityHelper.enableLifecycleCallbacks(application);
+    public void init(Activity activity) {
+        Leanplum.setApplicationContext(activity.getApplicationContext());
+
+        LeanplumActivityHelper.enableLifecycleCallbacks(activity.getApplication());
 
         if (AppConstants.MOZILLA_OFFICIAL) {
             Leanplum.setAppIdForProductionMode(MmaConstants.MOZ_LEANPLUM_SDK_CLIENTID, MmaConstants.MOZ_LEANPLUM_SDK_KEY);
         } else {
             Leanplum.setAppIdForDevelopmentMode(MmaConstants.MOZ_LEANPLUM_SDK_CLIENTID, MmaConstants.MOZ_LEANPLUM_SDK_KEY);
         }
 
-        Leanplum.start(application);
+
+        Map<String, Object> attributes = new HashMap<>();
+        boolean installedFocus = ContextUtils.isPackageInstalled(activity, "org.mozilla.focus");
+        boolean installedKlar = ContextUtils.isPackageInstalled(activity, "org.mozilla.klar");
+        if (installedFocus || installedKlar) {
+            attributes.put("focus", "installed");
+        }
+        Leanplum.start(activity, attributes);
+        Leanplum.track("Launch");
+        // this is special to Leanplum. Since we defer LeanplumActivityHelper's onResume call till
+        // switchboard completes loading, we manually call it here.
+        LeanplumActivityHelper.onResume(activity);
     }
 
     @Override
     public void start(Context context) {
 
     }
 
     @Override
--- a/mobile/android/base/java/org/mozilla/gecko/mma/MmaStubImp.java
+++ b/mobile/android/base/java/org/mozilla/gecko/mma/MmaStubImp.java
@@ -1,23 +1,24 @@
 //#filter substitution
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
  * 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.gecko.mma;
 
+import android.app.Activity;
 import android.app.Application;
 import android.content.Context;
 
 
 public class MmaStubImp implements MmaInterface {
     @Override
-    public void init(Application application) {
+    public void init(Activity activity) {
 
     }
 
     @Override
     public void start(Context context) {
 
     }
 
--- a/mobile/android/thirdparty/com/leanplum/LeanplumActivityHelper.java
+++ b/mobile/android/thirdparty/com/leanplum/LeanplumActivityHelper.java
@@ -204,17 +204,17 @@ public class LeanplumActivityHelper {
       if (!registeredCallbacks) {
         onPause(activity);
       }
     } catch (Throwable t) {
       Util.handleException(t);
     }
   }
 
-  private static void onResume(Activity activity) {
+  public static void onResume(Activity activity) {
     isActivityPaused = false;
     currentActivity = activity;
     if (LeanplumInternal.isPaused() || LeanplumInternal.hasStartedInBackground()) {
       Leanplum.resume();
       LocationManager locationManager = ActionManager.getLocationManager();
       if (locationManager != null) {
         locationManager.updateGeofencing();
         locationManager.updateUserLocation();