Bug 910189: Part 1 - Add "restore defaults" hooks into settings. r=margaret
☠☠ backed out by e04fb3e318fc ☠ ☠
authorChenxia Liu <liuche@mozilla.com>
Wed, 18 Dec 2013 15:21:14 -0800
changeset 161198 c572890f16a29e29443e3e5605ccd7ec2d8f7e2a
parent 161118 657e3059fee2807614efe5d07b60aa0bb863a1fb
child 161199 30258cd308b53567e9aa6b3c1050bfd06d61716b
push id25871
push usercbook@mozilla.com
push dateThu, 19 Dec 2013 09:34:02 +0000
treeherdermozilla-central@5c7fa2bfea8b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmargaret
bugs910189
milestone29.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 910189: Part 1 - Add "restore defaults" hooks into settings. r=margaret
mobile/android/base/locales/en-US/android_strings.dtd
mobile/android/base/preferences/GeckoPreferenceFragment.java
mobile/android/base/resources/menu-v11/preferences_search_menu.xml
mobile/android/base/resources/xml-v11/preferences_search.xml
mobile/android/base/resources/xml/preferences_search.xml
mobile/android/base/strings.xml.in
--- a/mobile/android/base/locales/en-US/android_strings.dtd
+++ b/mobile/android/base/locales/en-US/android_strings.dtd
@@ -68,16 +68,19 @@
 <!ENTITY pref_category_customize "Customize">
 <!ENTITY pref_category_search2 "Search settings">
 <!ENTITY pref_category_display "Display">
 <!ENTITY pref_category_privacy_short "Privacy">
 <!ENTITY pref_category_vendor "&vendorShortName;">
 <!ENTITY pref_category_datareporting "Data choices">
 <!ENTITY pref_category_installed_search_engines "Installed search engines">
 <!ENTITY pref_category_add_search_providers "Add more search providers">
+<!ENTITY pref_category_search_restore_defaults "Restore search engines">
+<!ENTITY pref_search_restore_defaults "Restore defaults">
+<!ENTITY pref_search_restore_defaults_summary "Restore defaults">
 <!-- Localization note (pref_search_tip) : "TIP" as in "hint", "clue" etc. Displayed as an
      advisory message on the customise search providers settings page explaining how to add new
      search providers.-->
 <!ENTITY pref_search_tip "TIP: Add any website to your list of search providers by long-pressing on its search field.">
 <!ENTITY pref_category_devtools "Developer tools">
 <!ENTITY pref_developer_remotedebugging "Remote debugging">
 <!ENTITY pref_developer_remotedebugging_docs "Learn more">
 <!ENTITY pref_remember_signons "Remember passwords">
--- a/mobile/android/base/preferences/GeckoPreferenceFragment.java
+++ b/mobile/android/base/preferences/GeckoPreferenceFragment.java
@@ -1,62 +1,117 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
  * 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.lang.reflect.Field;
+
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.PrefsHelper;
 
+import android.app.Activity;
 import android.preference.Preference;
 import android.preference.PreferenceActivity;
 import android.preference.PreferenceCategory;
 import android.preference.PreferenceFragment;
 import android.preference.PreferenceScreen;
 import android.os.Bundle;
 import android.util.Log;
+import android.view.Menu;
+import android.view.MenuInflater;
+import android.view.ViewConfiguration;
 
 /* 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 {
 
     private static final String LOGTAG = "GeckoPreferenceFragment";
     private int mPrefsRequestId = 0;
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        String resourceName = getArguments().getString("resource");
+
+        int res = getResource();
 
-        int res = 0;
-        if (resourceName != null) {
-            // Fetch resource id by resource name.
-            res = getActivity().getResources().getIdentifier(resourceName,
-                                                             "xml",
-                                                             getActivity().getPackageName());
+        // Display a menu for Search preferences.
+        if (res == R.xml.preferences_search) {
+            setHasOptionsMenu(true);
         }
 
-        if (res == 0) {
-            // The resource was invalid. Use the default resource.
-            Log.e(LOGTAG, "Failed to find resource: " + resourceName + ". Displaying default settings.");
-
-            boolean isMultiPane = ((PreferenceActivity) getActivity()).onIsMultiPane();
-            res = isMultiPane ? R.xml.preferences_customize_tablet : R.xml.preferences;
-        }
         addPreferencesFromResource(res);
 
         PreferenceScreen screen = getPreferenceScreen();
         setPreferenceScreen(screen);
         mPrefsRequestId = ((GeckoPreferences)getActivity()).setupPreferences(screen);
     }
 
+    /*
+     * 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;
+
+        String resourceName = getArguments().getString("resource");
+        if (resourceName != null) {
+            // Fetch resource id by resource name.
+            resid = getActivity().getResources().getIdentifier(resourceName,
+                                                             "xml",
+                                                             getActivity().getPackageName());
+        }
+
+        if (resid == 0) {
+            // The resource was invalid. Use the default resource.
+            Log.e(LOGTAG, "Failed to find resource: " + resourceName + ". Displaying default settings.");
+
+            boolean isMultiPane = ((PreferenceActivity) getActivity()).onIsMultiPane();
+            resid = isMultiPane ? R.xml.preferences_customize_tablet : R.xml.preferences;
+        }
+
+        return resid;
+    }
+
+    @Override
+    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
+        super.onCreateOptionsMenu(menu, inflater);
+        inflater.inflate(R.menu.preferences_search_menu, menu);
+    }
+
     @Override
     public void onDestroy() {
         super.onDestroy();
         if (mPrefsRequestId > 0) {
             PrefsHelper.removeObserver(mPrefsRequestId);
         }
     }
+
+    @Override
+    public void onAttach(Activity activity) {
+        super.onAttach(activity);
+        showOverflowMenu(activity);
+    }
+
+    /*
+     * Force the overflow 3-dot menu to be displayed if it isn't already displayed.
+     *
+     * This is an ugly hack for 4.0+ Android devices that don't have a dedicated menu button
+     * because Android does not provide a public API to display the ActionBar overflow menu.
+     */
+    private void showOverflowMenu(Activity activity) {
+        try {
+            ViewConfiguration config = ViewConfiguration.get(activity);
+            Field menuOverflow = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
+            if (menuOverflow != null) {
+                menuOverflow.setAccessible(true);
+                menuOverflow.setBoolean(config, false);
+            }
+        } catch (Exception e) {
+            Log.d(LOGTAG, "Failed to force overflow menu, ignoring.");
+        }
+    }
 }
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/resources/menu-v11/preferences_search_menu.xml
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 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/. -->
+
+<menu xmlns:android="http://schemas.android.com/apk/res/android">
+
+    <item android:id="@+id/restore_defaults"
+          android:title="@string/pref_search_restore_defaults" />
+
+</menu>
copy from mobile/android/base/resources/xml/preferences_search.xml
copy to mobile/android/base/resources/xml-v11/preferences_search.xml
--- a/mobile/android/base/resources/xml/preferences_search.xml
+++ b/mobile/android/base/resources/xml/preferences_search.xml
@@ -11,16 +11,23 @@
     <CheckBoxPreference android:key="browser.search.suggest.enabled"
                         android:title="@string/pref_search_suggestions"
                         android:defaultValue="false"
                         android:persistent="false" />
 
     <org.mozilla.gecko.preferences.SearchPreferenceCategory
                         android:title="@string/pref_category_installed_search_engines"/>
 
+    <PreferenceCategory android:title="@string/pref_category_search_restore_defaults">
+
+        <Preference android:key="android.not_a_preference.search.restore_defaults"
+                    android:title="@string/pref_search_restore_defaults_summary" />
+
+    </PreferenceCategory>
+
     <PreferenceCategory android:title="@string/pref_category_add_search_providers">
 
         <Preference android:layout="@layout/preference_search_tip"
                     android:enabled="false"
                     android:selectable="false"/>
 
     </PreferenceCategory>
 
--- a/mobile/android/base/strings.xml.in
+++ b/mobile/android/base/strings.xml.in
@@ -84,17 +84,21 @@
   <string name="pref_category_customize">&pref_category_customize;</string>
   <string name="pref_category_search">&pref_category_search2;</string>
   <string name="pref_category_display">&pref_category_display;</string>
   <string name="pref_category_privacy_short">&pref_category_privacy_short;</string>
   <string name="pref_category_vendor">&pref_category_vendor;</string>
   <string name="pref_category_datareporting">&pref_category_datareporting;</string>
   <string name="pref_category_installed_search_engines">&pref_category_installed_search_engines;</string>
   <string name="pref_category_add_search_providers">&pref_category_add_search_providers;</string>
+  <string name="pref_category_search_restore_defaults">&pref_category_search_restore_defaults;</string>
+  <string name="pref_search_restore_defaults">&pref_search_restore_defaults;</string>
+  <string name="pref_search_restore_defaults_summary">&pref_search_restore_defaults_summary;</string>
   <string name="pref_search_tip">&pref_search_tip;</string>
+
   <string name="pref_category_devtools">&pref_category_devtools;</string>
   <string name="pref_developer_remotedebugging">&pref_developer_remotedebugging;</string>
   <string name="pref_developer_remotedebugging_docs">&pref_developer_remotedebugging_docs;</string>
 
   <string name="pref_header_customize">&pref_header_customize;</string>
   <string name="pref_header_display">&pref_header_display;</string>
   <string name="pref_header_privacy_short">&pref_header_privacy_short;</string>
   <string name="pref_header_vendor">&pref_header_vendor;</string>