Bug 1423045 - Drop an event when User changes default browser to Firefox; r?mcomella draft
authorPetru Lingurar <petru.lingurar@softvision.ro>
Wed, 18 Apr 2018 15:21:12 +0300
changeset 784344 2f583ddf0524c09e6c3b952258df88cef60533e5
parent 784298 4af4ae0aee552a99a995ce4b32198b98294a95f7
push id106898
push userplingurar@mozilla.com
push dateWed, 18 Apr 2018 12:23:07 +0000
reviewersmcomella
bugs1423045
milestone61.0a1
Bug 1423045 - Drop an event when User changes default browser to Firefox; r?mcomella Use SharedPreference to ensure we won't loose previous state if Fennec is killed, set as default, restarted. The default browser status will only be set once, when the app is resumed, as it cannot change while the app is in foreground. We will track "E_Changed_Default_To_Fennec" only if Fennec wasn't previously the default browser. The method to track the event is safe to be called even before the mma init process is finished as LeanPlum postpones the track operation until it has actually been started. Refactored MmaDelegate to not use a WeakReference for application context anymore as that should exist for the entire time the app is open, and only in that timeframe the MmaDelegate methods that use that context can be called. MozReview-Commit-ID: JMJJclWj9fq
mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
mobile/android/base/java/org/mozilla/gecko/mma/MmaDelegate.java
mobile/android/docs/mma.rst
--- a/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
+++ b/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
@@ -1369,16 +1369,17 @@ public class BrowserApp extends GeckoApp
             }
         });
 
         for (final BrowserAppDelegate delegate : delegates) {
             delegate.onStart(this);
         }
 
         MmaDelegate.track(MmaDelegate.RESUMED_FROM_BACKGROUND);
+        MmaDelegate.notifyDefaultBrowserStatus(this);
     }
 
     @Override
     public void onStop() {
         super.onStop();
         if (mIsAbortingAppLaunch) {
             return;
         }
--- a/mobile/android/base/java/org/mozilla/gecko/mma/MmaDelegate.java
+++ b/mobile/android/base/java/org/mozilla/gecko/mma/MmaDelegate.java
@@ -24,17 +24,16 @@ import org.mozilla.gecko.R;
 import org.mozilla.gecko.Tab;
 import org.mozilla.gecko.Tabs;
 import org.mozilla.gecko.activitystream.homepanel.ActivityStreamConfiguration;
 import org.mozilla.gecko.fxa.FirefoxAccounts;
 import org.mozilla.gecko.preferences.GeckoPreferences;
 import org.mozilla.gecko.switchboard.SwitchBoard;
 import org.mozilla.gecko.util.ContextUtils;
 
-import java.lang.ref.WeakReference;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.UUID;
 
 
 public class MmaDelegate {
 
     public static final String READER_AVAILABLE = "E_Reader_Available";
@@ -45,39 +44,41 @@ public class MmaDelegate {
     public static final String INTERACT_WITH_SEARCH_URL_AREA = "E_Interact_With_Search_URL_Area";
     public static final String SCREENSHOT = "E_Screenshot";
     public static final String SAVED_LOGIN_AND_PASSWORD = "E_Saved_Login_And_Password";
     public static final String LAUNCH_BUT_NOT_DEFAULT_BROWSER = "E_Launch_But_Not_Default_Browser";
     public static final String LAUNCH_BROWSER = "E_Launch_Browser";
     public static final String RESUMED_FROM_BACKGROUND = "E_Resumed_From_Background";
     public static final String NEW_TAB = "E_Opened_New_Tab";
     public static final String DISMISS_ONBOARDING = "E_Dismiss_Onboarding";
-
+    public static final String CHANGED_DEFAULT_TO_FENNEC = "E_Changed_Default_To_Fennec";
 
     public static final String USER_ATT_FOCUS_INSTALLED = "Focus Installed";
     public static final String USER_ATT_KLAR_INSTALLED = "Klar Installed";
     public static final String USER_ATT_POCKET_INSTALLED = "Pocket Installed";
     public static final String USER_ATT_DEFAULT_BROWSER = "Default Browser";
     public static final String USER_ATT_SIGNED_IN = "Signed In Sync";
     public static final String USER_ATT_POCKET_TOP_SITES = "Pocket in Top Sites";
 
     public static final String PACKAGE_NAME_KLAR = "org.mozilla.klar";
     public static final String PACKAGE_NAME_FOCUS = "org.mozilla.focus";
     public static final String PACKAGE_NAME_POCKET = "com.ideashower.readitlater.pro";
 
     private static final String TAG = "MmaDelegate";
 
     public static final String KEY_ANDROID_PREF_STRING_LEANPLUM_DEVICE_ID = "android.not_a_preference.leanplum.device_id";
+    private static final String KEY_ANDROID_PREF_BOOLEAN_FENNEC_IS_DEFAULT = "android.not_a_preference.fennec.default.browser.status";
+
     private static final String DEBUG_LEANPLUM_DEVICE_ID = "8effda84-99df-11e7-abc4-cec278b6b50a";
 
     private static final MmaInterface mmaHelper = MmaConstants.getMma();
-    private static WeakReference<Context> applicationContext;
+    private static Context applicationContext;
 
     public static void init(Activity activity) {
-        applicationContext = new WeakReference<>(activity.getApplicationContext());
+        applicationContext = activity.getApplicationContext();
         // Since user attributes are gathered in Fennec, not in MMA implementation,
         // we gather the information here then pass to mmaHelper.init()
         // Note that generateUserAttribute always return a non null HashMap.
         final Map<String, Object> attributes = gatherUserAttributes(activity);
         final String deviceId = getDeviceId(activity);
         mmaHelper.setDeviceId(deviceId);
         PrefsHelper.setPref(GeckoPreferences.PREFS_MMA_DEVICE_ID, deviceId);
         // above two config setup required to be invoked before mmaHelper.init.
@@ -105,25 +106,45 @@ public class MmaDelegate {
         attributes.put(USER_ATT_POCKET_INSTALLED, ContextUtils.isPackageInstalled(context, PACKAGE_NAME_POCKET));
         attributes.put(USER_ATT_DEFAULT_BROWSER, isDefaultBrowser(context));
         attributes.put(USER_ATT_SIGNED_IN, FirefoxAccounts.firefoxAccountsExist(context));
         attributes.put(USER_ATT_POCKET_TOP_SITES, ActivityStreamConfiguration.isPocketRecommendingTopSites(context));
 
         return attributes;
     }
 
+    public static void notifyDefaultBrowserStatus(Activity activity) {
+        if (!isMmaEnabled(activity)) {
+            return;
+        }
+
+        final SharedPreferences sharedPreferences = activity.getPreferences(Context.MODE_PRIVATE);
+        final boolean isFennecDefaultBrowser = isDefaultBrowser(activity);
+
+        // Only if this is not the first run of LeanPlum and we previously tracked default browser status
+        // we can check for changes
+        if (sharedPreferences.contains(KEY_ANDROID_PREF_BOOLEAN_FENNEC_IS_DEFAULT)) {
+            // Will only inform LeanPlum of the event if Fennec was not previously the default browser
+            if (!sharedPreferences.getBoolean(KEY_ANDROID_PREF_BOOLEAN_FENNEC_IS_DEFAULT, true) && isFennecDefaultBrowser) {
+                track(CHANGED_DEFAULT_TO_FENNEC);
+            }
+        }
+
+        sharedPreferences.edit().putBoolean(KEY_ANDROID_PREF_BOOLEAN_FENNEC_IS_DEFAULT, isFennecDefaultBrowser).apply();
+    }
+
     public static void track(String event) {
-        if (applicationContext != null && isMmaEnabled(applicationContext.get())) {
+        if (applicationContext != null && isMmaEnabled(applicationContext)) {
             mmaHelper.event(event);
         }
     }
 
 
     public static void track(String event, long value) {
-        if (applicationContext != null && isMmaEnabled(applicationContext.get())) {
+        if (applicationContext != null && isMmaEnabled(applicationContext)) {
             mmaHelper.event(event, value);
         }
     }
 
     // isMmaEnabled should use pass-in context. The context comes from
     // 1. track(), it's called from UI (where we inject events). Context is from weak reference to Activity (assigned in init())
     // 2. handleGcmMessage(), it's called from GcmListenerService (where we handle GCM messages). Context is from the Service
     private static boolean isMmaEnabled(Context context) {
--- a/mobile/android/docs/mma.rst
+++ b/mobile/android/docs/mma.rst
@@ -155,16 +155,20 @@ List of current Events related data that
 * The user just dismissed on-boarding
 {
   "event" : "E_Dismiss_Onboarding"
 }
 * The user just resumed the app from background
 {
   "event" : "E_Resumed_From_Background"
 }
+* User set Fennec as default browser and resumed the app
+{
+  "event" : "E_Changed_Default_To_Fennec"
+}
 
 Deep Links:
 Deep links are actions that can point Fennec to open certain pages or load features such as `show bookmark list` or
 `open a SUMO page`. When users see a prompt Leanplum message, they can click the button(s) on it. These buttons can
 trigger the following deep links
 * Link to Set Default Browser settings (firefox://default_browser)
 * Link to specific Add-on page (http://link_to_the_add_on_page)
 * Link to sync signup/sign in (firefox://sign_up)