Bug 1454686 - Part 4 - Respond to changes in the new preference state state; r?mcomella draft
authorPetru Lingurar <petru.lingurar@softvision.ro>
Mon, 07 May 2018 17:25:16 +0300
changeset 792044 345e23fa6c0700aa7130cd45479b9f519535b5b5
parent 792038 fba2858f87f560295ec1b0434156c38d463ebbba
child 792050 ed84fd5ca68d3a2dbbd60dd0b2e228e4981ec798
push id108968
push userplingurar@mozilla.com
push dateMon, 07 May 2018 14:25:58 +0000
reviewersmcomella
bugs1454686
milestone61.0a1
Bug 1454686 - Part 4 - Respond to changes in the new preference state state; r?mcomella Because Mma cannot work if Health Report is disabled by the user (Settings - Privacy) we will treat toggling Health Report on/off the same as we treat toggling the new preference from Settings - Notifications. Toggling Health Report on will inform about the need to start LeanPlum (useful if the user did not explicitly stopped LP notifications but only Health Report which in turn disabled LeanPlum also) but there are other checks made afterwards to decide if LP can and should be enabled. Toggling any of these preferences will trigger an event caught by BrowserApp which can either - immediately initialize LeanPlum (if the toggle was off LP is not running) as it would normally do when the app first starts - stop LeanPlum reporting to servers, flush the per-session available messages and resets the LP started status so that it can be restarted in the same app session (like if the user toggles the feature again) MozReview-Commit-ID: 1SmhN0NucWW
mobile/android/base/java/org/mozilla/gecko/BrowserApp.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
@@ -276,16 +276,19 @@ public class BrowserApp extends GeckoApp
     public static final String TAB_HISTORY_FRAGMENT_TAG = "tabHistoryFragment";
 
     // When the static action bar is shown, only the real toolbar chrome should be
     // shown when the toolbar is visible. Causing the toolbar animator to also
     // show the snapshot causes the content to shift under the users finger.
     // See: Bug 1358554
     private boolean mShowingToolbarChromeForActionBar;
 
+    private SafeIntent safeStartingIntent;
+    private boolean isInAutomation;
+
     private static class MenuItemInfo implements Parcelable {
         public int id;
         public String label;
         public boolean checkable;
         public boolean checked;
         public boolean enabled = true;
         public boolean visible = true;
         public int parent;
@@ -718,20 +721,20 @@ public class BrowserApp extends GeckoApp
     }
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
         final Context appContext = getApplicationContext();
 
         showSplashScreen = true;
 
-        final SafeIntent intent = new SafeIntent(getIntent());
-        final boolean isInAutomation = IntentUtils.getIsInAutomationFromEnvironment(intent);
-
-        GeckoProfile.setIntentArgs(intent.getStringExtra("args"));
+        safeStartingIntent = new SafeIntent(getIntent());
+        isInAutomation = IntentUtils.getIsInAutomationFromEnvironment(safeStartingIntent);
+
+        GeckoProfile.setIntentArgs(safeStartingIntent.getStringExtra("args"));
 
         if (!isInAutomation && AppConstants.MOZ_ANDROID_DOWNLOAD_CONTENT_SERVICE) {
             // Kick off download of app content as early as possible so that in the best case it's
             // available before the user starts using the browser.
             DownloadContentService.startStudy(this);
         }
 
         // This has to be prepared prior to calling GeckoApp.onCreate, because
@@ -741,17 +744,17 @@ public class BrowserApp extends GeckoApp
         app.prepareLightweightTheme();
 
         super.onCreate(savedInstanceState);
 
         if (mIsAbortingAppLaunch) {
           return;
         }
 
-        initSwitchboardAndMma(this, intent, isInAutomation);
+        initSwitchboardAndMma(this, safeStartingIntent, 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() {
@@ -814,28 +817,28 @@ public class BrowserApp extends GeckoApp
                         GeckoAppShell.getHapticFeedbackDelegate().performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
                         fragment.show(R.id.tab_history_panel, fragmentManager.beginTransaction(), TAB_HISTORY_FRAGMENT_TAG);
                     }
                 });
             }
         });
         mBrowserToolbar.setTabHistoryController(tabHistoryController);
 
-        final String action = intent.getAction();
+        final String action = safeStartingIntent.getAction();
         if (Intent.ACTION_VIEW.equals(action)) {
             // Show the target URL immediately in the toolbar.
-            mBrowserToolbar.setTitle(intent.getDataString());
-
-            showTabQueuePromptIfApplicable(intent);
+            mBrowserToolbar.setTitle(safeStartingIntent.getDataString());
+
+            showTabQueuePromptIfApplicable(safeStartingIntent);
         } else if (ACTION_VIEW_MULTIPLE.equals(action) && savedInstanceState == null) {
             // We only want to handle this intent if savedInstanceState is null. In the case where
             // savedInstanceState is not null this activity is being re-created and we already
             // opened tabs for the URLs the last time. Our session store will take care of restoring
             // them.
-            openMultipleTabsFromIntent(intent);
+            openMultipleTabsFromIntent(safeStartingIntent);
         } else if (GuestSession.NOTIFICATION_INTENT.equals(action)) {
             GuestSession.onNotificationIntentReceived(this);
         } else if (TabQueueHelper.LOAD_URLS_ACTION.equals(action)) {
             Telemetry.sendUIEvent(TelemetryContract.Event.ACTION, TelemetryContract.Method.NOTIFICATION, "tabqueue");
         }
 
         if (HardwareUtils.isTablet()) {
             mTabStrip = (TabStripInterface) (((ViewStub) findViewById(R.id.tablet_tab_strip)).inflate());
@@ -895,16 +898,17 @@ public class BrowserApp extends GeckoApp
             "Tab:Added",
             "Video:Play",
             "CharEncoding:Data",
             "CharEncoding:State",
             "Settings:Show",
             "Updater:Launch",
             "Sanitize:Finished",
             "Sanitize:OpenTabs",
+            "NotificationSettings:FeatureTipsStatusUpdated",
             null);
 
         EventDispatcher.getInstance().registerBackgroundThreadListener(this,
             "Experiments:GetActive",
             "Experiments:SetOverride",
             "Experiments:ClearOverride",
             "Favicon:Request",
             "Feedback:MaybeLater",
@@ -1727,16 +1731,17 @@ public class BrowserApp extends GeckoApp
             "Tab:Added",
             "Video:Play",
             "CharEncoding:Data",
             "CharEncoding:State",
             "Settings:Show",
             "Updater:Launch",
             "Sanitize:Finished",
             "Sanitize:OpenTabs",
+            "NotificationSettings:FeatureTipsStatusUpdated",
             null);
 
         EventDispatcher.getInstance().unregisterBackgroundThreadListener(this,
             "Experiments:GetActive",
             "Experiments:SetOverride",
             "Experiments:ClearOverride",
             "Favicon:Request",
             "Feedback:MaybeLater",
@@ -2311,16 +2316,26 @@ public class BrowserApp extends GeckoApp
                             contentProviderClient,
                             location, hasImage, metadata);
                 } finally {
                     contentProviderClient.release();
                 }
 
                 break;
 
+            case "NotificationSettings:FeatureTipsStatusUpdated":
+                if (message.getBoolean("isMmaEnabled")) {
+                    initSwitchboardAndMma(this, safeStartingIntent, isInAutomation);
+                } else {
+                    MmaDelegate.stopReportingEvents();
+                    MmaDelegate.stopShowingSessionMessages();
+                    MmaDelegate.allowRestartInCurrentAppSession();
+                }
+                break;
+
             default:
                 super.handleMessage(event, message, callback);
                 break;
         }
     }
 
     /**
      * Use a dummy Intent to do a default browser check.
--- a/mobile/android/base/java/org/mozilla/gecko/preferences/GeckoPreferences.java
+++ b/mobile/android/base/java/org/mozilla/gecko/preferences/GeckoPreferences.java
@@ -1123,33 +1123,38 @@ public class GeckoPreferences
             setCharEncodingState(((String) newValue).equals("true"));
         } else if (PREFS_UPDATER_AUTODOWNLOAD.equals(prefName)) {
             UpdateServiceHelper.setAutoDownloadPolicy(this, UpdateService.AutoDownloadPolicy.get((String) newValue));
         } else if (PREFS_UPDATER_URL.equals(prefName)) {
             UpdateServiceHelper.setUpdateUrl(this, (String) newValue);
         } else if (PREFS_HEALTHREPORT_UPLOAD_ENABLED.equals(prefName)) {
             final Boolean newBooleanValue = (Boolean) newValue;
             AdjustConstants.getAdjustHelper().setEnabled(newBooleanValue);
-            if (!newBooleanValue) {
-                MmaDelegate.stop();
-            }
+            // If Health Report has been disabled Mma should also be stopped.
+            // If it was just enabled, we should also try to start Mma and not wait until the app is restarted.
+            // The process will check if all the other requirements are met before actually starting Mma.
+            informMmaStatusChanged(newBooleanValue);
         } else if (PREFS_GEO_REPORTING.equals(prefName)) {
             if ((Boolean) newValue) {
                 enableStumbler((CheckBoxPreference) preference);
                 return false;
             } else {
                 broadcastStumblerPref(GeckoPreferences.this, false);
                 return true;
             }
         } else if (PREFS_TAB_QUEUE.equals(prefName)) {
             if ((Boolean) newValue && !TabQueueHelper.canDrawOverlays(this)) {
                 Intent promptIntent = new Intent(this, TabQueuePrompt.class);
                 startActivityForResult(promptIntent, REQUEST_CODE_TAB_QUEUE);
                 return false;
             }
+        } else if (PREFS_NOTIFICATIONS_FEATURES_TIPS.equals(prefName)) {
+            // isChecked() returns the old value that hasn't yet been updated
+            final boolean isMmaEnabled = !((SwitchPreference) preference).isChecked();
+            informMmaStatusChanged(isMmaEnabled);
         } else if (HANDLERS.containsKey(prefName)) {
             PrefHandler handler = HANDLERS.get(prefName);
             handler.onChange(this, preference, newValue);
         } else if (PREFS_SEARCH_SUGGESTIONS_ENABLED.equals(prefName)) {
             // Tell Gecko to transmit the current search engine data again, so
             // BrowserSearch is notified immediately about the new enabled state.
             EventDispatcher.getInstance().dispatch("SearchEngines:GetVisible", null);
         }
@@ -1187,16 +1192,22 @@ public class GeckoPreferences
                     @Override
                     public void run() {
                         preference.setChecked(true);
                         broadcastStumblerPref(GeckoPreferences.this, true);
                     }
                 });
     }
 
+    private void informMmaStatusChanged(boolean newStatus) {
+        final GeckoBundle newStatusBundle = new GeckoBundle(1);
+        newStatusBundle.putBoolean("isMmaEnabled", newStatus);
+        EventDispatcher.getInstance().dispatch("NotificationSettings:FeatureTipsStatusUpdated", newStatusBundle);
+    }
+
     private TextInputLayout getTextBox(int aHintText) {
         final EditText input = new EditText(this);
         int inputtype = InputType.TYPE_CLASS_TEXT;
         inputtype |= InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
         input.setInputType(inputtype);
 
         input.setHint(aHintText);