From 41b293dfc249ab38fba30726d2970c01c40cd3b9 Mon Sep 17 00:00:00 2001 From: Nevin Chen Date: Wed, 24 May 2017 11:23:10 +0800 Subject: [PATCH] Bug 1361664 - Part1. Udate MMA API, make init() accept activity. r=maliu MozReview-Commit-ID: AcfoeCPV8SV --HG-- extra : rebase_source : 6441ed54f9e19b6c384281084699e5247ec5d3f6 --- .../java/org/mozilla/gecko/BrowserApp.java | 2 +- .../org/mozilla/gecko/mma/MmaDelegate.java | 41 +++++++++++++------ .../org/mozilla/gecko/mma/MmaInterface.java | 7 ++-- .../org/mozilla/gecko/mma/MmaLeanplumImp.java | 27 +++++++++--- .../org/mozilla/gecko/mma/MmaStubImp.java | 3 +- .../com/leanplum/LeanplumActivityHelper.java | 2 +- 6 files changed, 58 insertions(+), 24 deletions(-) diff --git a/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java b/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java index 3d83f37f9964..ae640436c7b2 100644 --- a/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java +++ b/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java @@ -895,7 +895,7 @@ public class BrowserApp extends GeckoApp 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; } diff --git a/mobile/android/base/java/org/mozilla/gecko/mma/MmaDelegate.java b/mobile/android/base/java/org/mozilla/gecko/mma/MmaDelegate.java index 3c85890bbbb6..ba99cd173ebb 100644 --- a/mobile/android/base/java/org/mozilla/gecko/mma/MmaDelegate.java +++ b/mobile/android/base/java/org/mozilla/gecko/mma/MmaDelegate.java @@ -6,8 +6,8 @@ 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; @@ -15,35 +15,50 @@ 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); + } } } diff --git a/mobile/android/base/java/org/mozilla/gecko/mma/MmaInterface.java b/mobile/android/base/java/org/mozilla/gecko/mma/MmaInterface.java index 70efa3e4ff52..6d8e62a8a244 100644 --- a/mobile/android/base/java/org/mozilla/gecko/mma/MmaInterface.java +++ b/mobile/android/base/java/org/mozilla/gecko/mma/MmaInterface.java @@ -6,18 +6,19 @@ 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(); } diff --git a/mobile/android/base/java/org/mozilla/gecko/mma/MmaLeanplumImp.java b/mobile/android/base/java/org/mozilla/gecko/mma/MmaLeanplumImp.java index d975a161317f..615bf5d8cf76 100644 --- a/mobile/android/base/java/org/mozilla/gecko/mma/MmaLeanplumImp.java +++ b/mobile/android/base/java/org/mozilla/gecko/mma/MmaLeanplumImp.java @@ -6,6 +6,7 @@ package org.mozilla.gecko.mma; +import android.app.Activity; import android.app.Application; import android.content.Context; @@ -13,16 +14,21 @@ 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); @@ -30,7 +36,18 @@ public class MmaLeanplumImp implements MmaInterface { Leanplum.setAppIdForDevelopmentMode(MmaConstants.MOZ_LEANPLUM_SDK_CLIENTID, MmaConstants.MOZ_LEANPLUM_SDK_KEY); } - Leanplum.start(application); + + Map 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 diff --git a/mobile/android/base/java/org/mozilla/gecko/mma/MmaStubImp.java b/mobile/android/base/java/org/mozilla/gecko/mma/MmaStubImp.java index 511f9acae58a..a4252075694f 100644 --- a/mobile/android/base/java/org/mozilla/gecko/mma/MmaStubImp.java +++ b/mobile/android/base/java/org/mozilla/gecko/mma/MmaStubImp.java @@ -6,13 +6,14 @@ 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) { } diff --git a/mobile/android/thirdparty/com/leanplum/LeanplumActivityHelper.java b/mobile/android/thirdparty/com/leanplum/LeanplumActivityHelper.java index 09e40cb2036a..9b28f98208da 100644 --- a/mobile/android/thirdparty/com/leanplum/LeanplumActivityHelper.java +++ b/mobile/android/thirdparty/com/leanplum/LeanplumActivityHelper.java @@ -209,7 +209,7 @@ public class LeanplumActivityHelper { } } - private static void onResume(Activity activity) { + public static void onResume(Activity activity) { isActivityPaused = false; currentActivity = activity; if (LeanplumInternal.isPaused() || LeanplumInternal.hasStartedInBackground()) {