Bug 1401130 - Show title correctly for deep links. r?rnewman,jwu draft
authorNevin Chen <cnevinchen@gmail.com>
Tue, 19 Sep 2017 17:37:53 +0800
changeset 667565 fd7aab61692359db1132dd41ba391ba61251affb
parent 666884 e4261f5b96ebfd63e7cb8af3035ff9fea90c74a5
child 732428 c1d202ce1767fda92e0b2ef4087471b99e164b21
push id80759
push userbmo:cnevinchen@gmail.com
push dateWed, 20 Sep 2017 09:25:05 +0000
reviewersrnewman, jwu
bugs1401130
milestone57.0a1
Bug 1401130 - Show title correctly for deep links. r?rnewman,jwu MozReview-Commit-ID: 6ktOxxsqkTK
mobile/android/base/java/org/mozilla/gecko/preferences/GeckoPreferenceFragment.java
mobile/android/base/java/org/mozilla/gecko/preferences/GeckoPreferences.java
--- a/mobile/android/base/java/org/mozilla/gecko/preferences/GeckoPreferenceFragment.java
+++ b/mobile/android/base/java/org/mozilla/gecko/preferences/GeckoPreferenceFragment.java
@@ -2,46 +2,45 @@
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.preferences;
 
 import java.util.Locale;
 
-import org.mozilla.gecko.AppConstants.Versions;
 import org.mozilla.gecko.BrowserLocaleManager;
 import org.mozilla.gecko.GeckoApplication;
 import org.mozilla.gecko.GeckoSharedPrefs;
 import org.mozilla.gecko.LocaleManager;
 import org.mozilla.gecko.PrefsHelper;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.Telemetry;
 import org.mozilla.gecko.TelemetryContract;
 import org.mozilla.gecko.TelemetryContract.Method;
 import org.mozilla.gecko.fxa.AccountLoader;
 import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount;
 
 import android.accounts.Account;
-import android.app.ActionBar;
 import android.app.Activity;
 import android.app.LoaderManager;
 import android.content.Context;
 import android.content.Loader;
 import android.content.res.Configuration;
 import android.content.res.Resources;
 import android.os.Bundle;
-import android.preference.PreferenceActivity;
 import android.preference.PreferenceFragment;
 import android.preference.PreferenceScreen;
+import android.support.annotation.CheckResult;
 import android.util.Log;
 import android.view.Menu;
 import android.view.MenuInflater;
 
-import com.squareup.leakcanary.RefWatcher;
+import static org.mozilla.gecko.preferences.GeckoPreferences.INTENT_EXTRA_HEADER_ACTION;
+import static org.mozilla.gecko.preferences.GeckoPreferences.INTENT_EXTRA_RESOURCES;
 
 /* A simple implementation of PreferenceFragment for large screen devices
  * This will strip category headers (so that they aren't shown to the user twice)
  * as well as initializing Gecko prefs when a fragment is shown.
 */
 public class GeckoPreferenceFragment extends PreferenceFragment {
 
     public static final int ACCOUNT_LOADER_ID = 1;
@@ -85,70 +84,31 @@ public class GeckoPreferenceFragment ext
         if (res == R.xml.preferences_search) {
             setHasOptionsMenu(true);
         }
 
         addPreferencesFromResource(res);
 
         PreferenceScreen screen = getPreferenceScreen();
         setPreferenceScreen(screen);
-        mPrefsRequest = ((GeckoPreferences)getActivity()).setupPreferences(screen);
+        mPrefsRequest = ((GeckoPreferences) getActivity()).setupPreferences(screen);
         syncPreference = (SyncPreference) findPreference(GeckoPreferences.PREFS_SYNC);
     }
 
     /**
-     * Return the title to use for this preference fragment.
-     *
-     * We only return titles for the preference screens that are
-     * launched directly, and thus might need to be redisplayed.
-     *
-     * This method sets the title that you see on non-multi-pane devices.
-     */
-    private String getTitle() {
-        final int res = getResource();
-        if (res == R.xml.preferences) {
-            return getString(R.string.settings_title);
-        }
-
-        if (res == R.xml.preferences_general) {
-            return getString(R.string.pref_category_general);
-        }
-
-        if (res == R.xml.preferences_home) {
-            return getString((R.string.pref_category_home));
-        }
-
-        // We can launch this category from the Data Reporting notification.
-        if (res == R.xml.preferences_privacy) {
-            return getString(R.string.pref_category_privacy_short);
-        }
-
-        // We can launch this category from the the magnifying glass in the quick search bar.
-        if (res == R.xml.preferences_search) {
-            return getString(R.string.pref_category_search);
-        }
-
-        if (res == R.xml.preferences_notifications) {
-            return getString(R.string.pref_category_notifications);
-        }
-
-        return null;
-    }
-
-    /**
      * Return the header id for this preference fragment. This allows
      * us to select the correct header when launching a preference
      * screen directly.
-     *
-     * We only return titles for the preference screens that are
-     * launched directly.
+     * <p>
+     * We only return top level headers accordingly and return -1 otherwise.
      */
+    @CheckResult
     private int getHeader() {
         final int res = getResource();
-        if (res == R.xml.preferences) {
+        if (res == R.xml.preferences || res == R.xml.preferences_home || res == R.xml.preferences_locale) {
             return R.id.pref_header_general;
         }
 
         // We can launch this category from the Data Reporting notification.
         if (res == R.xml.preferences_privacy) {
             return R.id.pref_header_privacy;
         }
 
@@ -156,37 +116,59 @@ public class GeckoPreferenceFragment ext
         if (res == R.xml.preferences_search) {
             return R.id.pref_header_search;
         }
 
         if (res == R.xml.preferences_notifications) {
             return R.id.pref_header_notifications;
         }
 
+        if (res == R.xml.preferences_accessibility) {
+            return R.id.pref_header_accessibility;
+        }
+
+        if (res == R.xml.preferences_advanced) {
+            return R.id.pref_header_advanced;
+        }
+
+        if (res == R.xml.preferences_privacy_clear_tablet) {
+            return R.id.pref_header_clear_private_data;
+        }
+
+        if (res == R.xml.preferences_default_browser_tablet) {
+            return R.id.pref_header_default_browser;
+        }
+
         return -1;
     }
 
-    private void updateTitle() {
-        final String newTitle = getTitle();
-        if (newTitle == null) {
-            Log.d(LOGTAG, "No new title to show.");
+    // normally preferenceFragment will handle setOnPreferenceTreeClickListener, show breadCrumbs
+    // and switchToHeader automatically. We don't need to switch header inside the fragment.
+    // But sometimes we show the fragment directly from GeckoPreferences's setResourceToOpen, e.g.
+    // 1. Gecko Event "Settings:Show"
+    // 2. Deep Links
+    // 3. Search EngineBar...
+    // So we'll handle that case here:
+    private void handleNoneClickAction() {
+        final GeckoPreferences activity = (GeckoPreferences) getActivity();
+        final CharSequence title = getPreferenceScreen().getTitle();
+
+        final boolean nonClickAction = getArguments().getBoolean(INTENT_EXTRA_HEADER_ACTION);
+        if (!nonClickAction) {
             return;
         }
+        activity.showBreadCrumbs(title, title);
+        if (!activity.onIsMultiPane()) {
+            return;
+        }
+        final int header = getHeader();
+        if ((header > 0)) {
+            activity.switchToHeader(header);
+        }
 
-        final GeckoPreferences activity = (GeckoPreferences) getActivity();
-        if (activity.isMultiPane()) {
-            // In a multi-pane activity, the title is "Settings", and the action
-            // bar is along the top of the screen. We don't want to change those.
-            activity.showBreadCrumbs(newTitle, newTitle);
-            activity.switchToHeader(getHeader());
-            return;
-        }
-
-        Log.v(LOGTAG, "Setting activity title to " + newTitle);
-        activity.setTitle(newTitle);
     }
 
     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
         accountLoaderCallbacks = new AccountLoaderCallbacks();
         getLoaderManager().initLoader(ACCOUNT_LOADER_ID, null, accountLoaderCallbacks);
     }
@@ -212,29 +194,30 @@ public class GeckoPreferenceFragment ext
             Log.d(LOGTAG, "Locale changed: " + currentLocale);
             this.lastLocale = currentLocale;
 
             // Rebuild the list to reflect the current locale.
             getPreferenceScreen().removeAll();
             addPreferencesFromResource(getResource());
         }
 
-        // Fix the parent title regardless.
-        updateTitle();
+        // Fix the UI regardless.
+        handleNoneClickAction();
+
     }
 
     /*
      * Get the resource from Fragment arguments and return it.
      *
      * If no resource can be found, return the resource id of the default preference screen.
      */
     private int getResource() {
         int resid = 0;
 
-        final String resourceName = getArguments().getString("resource");
+        final String resourceName = getArguments().getString(INTENT_EXTRA_RESOURCES);
         final Activity activity = getActivity();
 
         if (resourceName != null) {
             // Fetch resource id by resource name.
             final Resources resources = activity.getResources();
             final String packageName = activity.getPackageName();
             resid = resources.getIdentifier(resourceName, "xml", packageName);
         }
--- a/mobile/android/base/java/org/mozilla/gecko/preferences/GeckoPreferences.java
+++ b/mobile/android/base/java/org/mozilla/gecko/preferences/GeckoPreferences.java
@@ -104,16 +104,17 @@ public class GeckoPreferences
     // We have a white background, which makes transitions on
     // some devices look bad. Don't use transitions on those
     // devices.
     private static final boolean NO_TRANSITIONS = HardwareUtils.IS_KINDLE_DEVICE;
     private static final int NO_SUCH_ID = 0;
 
     public static final String NON_PREF_PREFIX = "android.not_a_preference.";
     public static final String INTENT_EXTRA_RESOURCES = "resource";
+    public static final String INTENT_EXTRA_HEADER_ACTION = "non-click-action";
     public static final String PREFS_TRACKING_PROTECTION_PROMPT_SHOWN = NON_PREF_PREFIX + "trackingProtectionPromptShown";
     public static final String PREFS_HEALTHREPORT_UPLOAD_ENABLED = NON_PREF_PREFIX + "healthreport.uploadEnabled";
     public static final String PREFS_SYNC = NON_PREF_PREFIX + "sync";
 
     private static boolean sIsCharEncodingEnabled;
     private boolean mInitialized;
     private PrefsHelper.PrefHandler mPrefsRequest;
     private List<Header> mHeaders;
@@ -1423,12 +1424,13 @@ public class GeckoPreferences
         }
         if (resource == null) {
             return;
         }
 
         intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT, GeckoPreferenceFragment.class.getName());
 
         Bundle fragmentArgs = new Bundle();
-        fragmentArgs.putString("resource", resource);
+        fragmentArgs.putString(INTENT_EXTRA_RESOURCES, resource);
+        fragmentArgs.putBoolean(INTENT_EXTRA_HEADER_ACTION, true);
         intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS, fragmentArgs);
     }
 }