Bug 1454686 - Small refactoring of Mma related methods; 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 if Mma is available are checked - GeckoPreferences.isMmaAvailableAndEnabled()
Added only one place from where the availability of the LP experiments should be checked as that currently involves two checks - MmaDelegate.isMmaExperimentEnabled(..)
Also renamed isMmaEnabled() from MmaDelegate() and initSwitchboard from BrowserApp() to better express what those methods do.
MozReview-Commit-ID: BCJqM9b5JbW
***
--- 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.isMmaAvailableAndEnabled(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
@@ -110,17 +110,17 @@ public class MmaDelegate {
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)) {
+ if (!isMmaAllowed(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
@@ -131,73 +131,84 @@ public class MmaDelegate {
}
}
sharedPreferences.edit().putBoolean(KEY_ANDROID_PREF_BOOLEAN_FENNEC_IS_DEFAULT, isFennecDefaultBrowser).apply();
}
static void trackJustInstalledPackage(@NonNull final Context context, @NonNull final String packageName,
final boolean firstTimeInstall) {
- if (!isMmaEnabled(context)) {
+ if (!isMmaAllowed(context)) {
return;
}
if (packageName.equals(PACKAGE_NAME_FOCUS) && firstTimeInstall) {
// Already know Mma is enabled, safe to call directly and avoid a superfluous check
mmaHelper.event(INSTALLED_FOCUS);
} else if (packageName.equals(PACKAGE_NAME_KLAR) && firstTimeInstall) {
mmaHelper.event(INSTALLED_KLAR);
}
}
public static void track(String event) {
- if (applicationContext != null && isMmaEnabled(applicationContext)) {
+ if (applicationContext != null && isMmaAllowed(applicationContext)) {
mmaHelper.event(event);
}
}
public static void track(String event, long value) {
- if (applicationContext != null && isMmaEnabled(applicationContext)) {
+ if (applicationContext != null && isMmaAllowed(applicationContext)) {
mmaHelper.event(event, value);
}
}
- // isMmaEnabled should use pass-in context. The context comes from
+ /**
+ * Convenience method to check if the Switchboard experiments related to MMA are enabled for us.<br>
+ * <ul>It will return <code>true</code> if we are in any of the following experiments:
+ * <li>{@link Experiments#LEANPLUM}</li>
+ * <li>{@link Experiments#LEANPLUM_DEBUG}</li>
+ * and <code>false</code> otherwise.</ul>
+ */
+ public static boolean isMmaExperimentEnabled(Context context) {
+ return SwitchBoard.isInExperiment(context, Experiments.LEANPLUM) ||
+ SwitchBoard.isInExperiment(context, Experiments.LEANPLUM_DEBUG);
+ }
+
+ // isMmaAllowed 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) {
-
+ private static boolean isMmaAllowed(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.isMmaAvailableAndEnabled(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;
}
final String packageName = info.activityInfo.packageName;
return (TextUtils.equals(packageName, context.getPackageName()));
}
// Always use pass-in context. Do not use applicationContext here. applicationContext will be null if MmaDelegate.init() is not called.
public static boolean handleGcmMessage(@NonNull Context context, String from, @NonNull Bundle bundle) {
- if (isMmaEnabled(context)) {
+ if (isMmaAllowed(context)) {
mmaHelper.setCustomIcon(R.drawable.ic_status_logo);
return mmaHelper.handleGcmMessage(context, from, bundle);
} else {
return false;
}
}
private static String getDeviceId(Activity activity) {
--- 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;
@@ -812,28 +813,26 @@ public class GeckoPreferences
}
} else if (PREFS_COMPACT_TABS.equals(key)) {
if (HardwareUtils.isTablet()) {
preferences.removePreference(pref);
i--;
continue;
}
} else if (PREFS_NOTIFICATIONS_FEATURES_TIPS.equals(key)) {
- final boolean isLeanplumAvailable =
- SwitchBoard.isInExperiment(this, Experiments.LEANPLUM) ||
- SwitchBoard.isInExperiment(this, Experiments.LEANPLUM_DEBUG);
+ final boolean isLeanplumAvailable = MmaDelegate.isMmaExperimentEnabled(this);
if (!isLeanplumAvailable) {
preferences.removePreference(pref);
i--;
continue;
}
// Mma can only work if Health Report is enabled
- boolean isHealthReportEnabled = isHealthReportEnabled(getApplicationContext());
+ boolean isHealthReportEnabled = isHealthReportEnabled(this);
if (!isHealthReportEnabled) {
((SwitchPreference) pref).setChecked(isHealthReportEnabled);
pref.setEnabled(isHealthReportEnabled);
}
}
// Some Preference UI elements are not actually preferences,
// but they require a key to work correctly. For example,
@@ -1436,13 +1435,25 @@ 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 isMmaAvailableAndEnabled(@NonNull Context context) {
+ final boolean isInMmaExperiment = MmaDelegate.isMmaExperimentEnabled(context);
+ final boolean isHealthReportEnabled = isHealthReportEnabled(context);
+ final boolean areMmaMessagesAllowed = GeckoPreferences.getBooleanPref(context,
+ GeckoPreferences.PREFS_NOTIFICATIONS_FEATURES_TIPS, true);
+
+ return isHealthReportEnabled && isInMmaExperiment && areMmaMessagesAllowed;
+ }
+
public static boolean isHealthReportEnabled(Context context) {
// Health Report is enabled by default so we'll return true if the preference is not found
return GeckoPreferences.getBooleanPref(context, PREFS_HEALTHREPORT_UPLOAD_ENABLED, true);
}
}