Bug 1454686 - Part 3 - Small refactoring: extract isMmaEnabled and renamed initSwitchboard; r?mcomella draft
authorPetru Lingurar <petru.lingurar@softvision.ro>
Mon, 07 May 2018 16:44:35 +0300
changeset 792038 fba2858f87f560295ec1b0434156c38d463ebbba
parent 792036 c3a54a3842ae144e4c96d3fd89001e8249f893f6
child 792044 345e23fa6c0700aa7130cd45479b9f519535b5b5
push id108963
push userplingurar@mozilla.com
push dateMon, 07 May 2018 13:45:42 +0000
reviewersmcomella
bugs1454686
milestone61.0a1
Bug 1454686 - Part 3 - Small refactoring: extract isMmaEnabled and renamed initSwitchboard; r?mcomella With the adding of the new preference that Mma depends on we need to have only one place where all the conditions for considering MmaIsAvailable are checked. Also renamed initSwitchboard from BrowserApp() to better express that this method is also responsible for starting Mma - Leanplum. MozReview-Commit-ID: BCJqM9b5JbW
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/preferences/GeckoPreferences.java
--- a/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
+++ b/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
@@ -113,17 +113,16 @@ import org.mozilla.gecko.icons.IconsHelp
 import org.mozilla.gecko.icons.decoders.FaviconDecoder;
 import org.mozilla.gecko.icons.decoders.IconDirectoryEntry;
 import org.mozilla.gecko.icons.decoders.LoadFaviconResult;
 import org.mozilla.gecko.lwt.LightweightTheme;
 import org.mozilla.gecko.media.VideoPlayer;
 import org.mozilla.gecko.menu.GeckoMenu;
 import org.mozilla.gecko.menu.GeckoMenuItem;
 import org.mozilla.gecko.mma.MmaDelegate;
-import org.mozilla.gecko.mozglue.GeckoLoader;
 import org.mozilla.gecko.mozglue.SafeIntent;
 import org.mozilla.gecko.notifications.NotificationHelper;
 import org.mozilla.gecko.overlays.ui.ShareDialog;
 import org.mozilla.gecko.permissions.Permissions;
 import org.mozilla.gecko.preferences.ClearOnShutdownPref;
 import org.mozilla.gecko.preferences.GeckoPreferences;
 import org.mozilla.gecko.promotion.AddToHomeScreenPromotion;
 import org.mozilla.gecko.promotion.ReaderViewBookmarkPromotion;
@@ -742,17 +741,17 @@ public class BrowserApp extends GeckoApp
         app.prepareLightweightTheme();
 
         super.onCreate(savedInstanceState);
 
         if (mIsAbortingAppLaunch) {
           return;
         }
 
-        initSwitchboard(this, intent, isInAutomation);
+        initSwitchboardAndMma(this, intent, isInAutomation);
         initTelemetryUploader(isInAutomation);
 
         mBrowserChrome = (ViewGroup) findViewById(R.id.browser_chrome);
         mActionBarFlipper = (ViewFlipper) findViewById(R.id.browser_actionbar);
         mActionBar = (ActionModeCompatView) findViewById(R.id.actionbar);
 
         mVideoPlayer = (VideoPlayer) findViewById(R.id.video_player);
         mVideoPlayer.setFullScreenListener(new VideoPlayer.FullScreenListener() {
@@ -995,34 +994,33 @@ public class BrowserApp extends GeckoApp
         final String installerPackageName = getPackageManager().getInstallerPackageName(getPackageName());
         Telemetry.sendUIEvent(TelemetryContract.Event.LAUNCH, TelemetryContract.Method.SYSTEM, "installer_" + installerPackageName);
     }
 
     /**
      * Initializes the default Switchboard URLs the first time.
      * @param intent
      */
-    private void initSwitchboard(final Context context, final SafeIntent intent, final boolean isInAutomation) {
+    private void initSwitchboardAndMma(final Context context, final SafeIntent intent, final boolean isInAutomation) {
         if (isInAutomation) {
             Log.d(LOGTAG, "Switchboard disabled - in automation");
             return;
         } else if (!AppConstants.MOZ_SWITCHBOARD) {
             Log.d(LOGTAG, "Switchboard compile-time disabled");
             return;
         }
 
         final String serverExtra = intent.getStringExtra(INTENT_KEY_SWITCHBOARD_SERVER);
         final String serverUrl = TextUtils.isEmpty(serverExtra) ? SWITCHBOARD_SERVER : serverExtra;
         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)) {
+                if (GeckoPreferences.isMmaAvailable(context)) {
                     // Do LeanPlum start/init here
                     MmaDelegate.init(BrowserApp.this);
                 }
                 return null;
             }
         }.execute();
     }
 
--- a/mobile/android/base/java/org/mozilla/gecko/mma/MmaDelegate.java
+++ b/mobile/android/base/java/org/mozilla/gecko/mma/MmaDelegate.java
@@ -168,28 +168,27 @@ public class MmaDelegate {
             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) {
-
         if (context == null) {
             return false;
         }
-        final boolean healthReport = GeckoPreferences.getBooleanPref(context, GeckoPreferences.PREFS_HEALTHREPORT_UPLOAD_ENABLED, true);
-        final boolean inExperiment = SwitchBoard.isInExperiment(context, Experiments.LEANPLUM);
+
+        final boolean isMmaAvailable = GeckoPreferences.isMmaAvailable(context);
+
         final Tab selectedTab = Tabs.getInstance().getSelectedTab();
-
         // if selected tab is private, mma should be disabled.
         final boolean isInPrivateBrowsing = selectedTab != null && selectedTab.isPrivate();
-        // only check Gecko Pref when Gecko is running
-        return inExperiment && healthReport && !isInPrivateBrowsing;
+
+        return isMmaAvailable && !isInPrivateBrowsing;
     }
 
     public static boolean isDefaultBrowser(Context context) {
         final Intent viewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.mozilla.org"));
         final ResolveInfo info = context.getPackageManager().resolveActivity(viewIntent, PackageManager.MATCH_DEFAULT_ONLY);
         if (info == null) {
             // No default is set
             return false;
--- a/mobile/android/base/java/org/mozilla/gecko/preferences/GeckoPreferences.java
+++ b/mobile/android/base/java/org/mozilla/gecko/preferences/GeckoPreferences.java
@@ -25,16 +25,17 @@ import android.preference.EditTextPrefer
 import android.preference.ListPreference;
 import android.preference.Preference;
 import android.preference.Preference.OnPreferenceChangeListener;
 import android.preference.Preference.OnPreferenceClickListener;
 import android.preference.PreferenceActivity;
 import android.preference.PreferenceGroup;
 import android.preference.SwitchPreference;
 import android.preference.TwoStatePreference;
+import android.support.annotation.NonNull;
 import android.support.design.widget.Snackbar;
 import android.support.design.widget.TextInputLayout;
 import android.support.v4.content.LocalBroadcastManager;
 import android.support.v7.app.ActionBar;
 import android.text.Editable;
 import android.text.InputType;
 import android.text.TextUtils;
 import android.text.TextWatcher;
@@ -1437,9 +1438,22 @@ public class GeckoPreferences
         }
 
         intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT, GeckoPreferenceFragment.class.getName());
 
         Bundle fragmentArgs = new Bundle();
         fragmentArgs.putString("resource", resource);
         intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS, fragmentArgs);
     }
+
+    /**
+     * Get if Mma is available for the device and enabled by the user.
+     */
+    public static boolean isMmaAvailable(@NonNull Context context) {
+        final boolean isInMmaExperiment = SwitchBoard.isInExperiment(context, Experiments.LEANPLUM);
+        final boolean isHealthReportEnabled = GeckoPreferences.getBooleanPref(context,
+                GeckoPreferences.PREFS_HEALTHREPORT_UPLOAD_ENABLED, true);
+        final boolean areMmaMessagesAllowed = GeckoPreferences.getBooleanPref(context,
+                GeckoPreferences.PREFS_NOTIFICATIONS_FEATURES_TIPS, true);
+
+        return isHealthReportEnabled && isInMmaExperiment && areMmaMessagesAllowed;
+    }
 }