Bug 1201181 - Add "Set a Homepage" item in Settings > Customize > Home; r=mfinkle
authorMartyn Haigh <mhaigh@mozilla.org>
Mon, 14 Sep 2015 14:36:38 +0100
changeset 294876 9212e656007346226fea7f34c5c28c59d2bdb3f0
parent 294875 f0544edc121ea85cd58d813470c320286e1ed4ce
child 294877 c02886680b09ced8b1c359ebf698b7f1ba827085
push id5245
push userraliiev@mozilla.com
push dateThu, 29 Oct 2015 11:30:51 +0000
treeherdermozilla-beta@dac831dc1bd0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle
bugs1201181
milestone43.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 1201181 - Add &quot;Set a Homepage&quot; item in Settings &gt; Customize &gt; Home; r=mfinkle
mobile/android/base/locales/en-US/android_strings.dtd
mobile/android/base/moz.build
mobile/android/base/preferences/GeckoPreferences.java
mobile/android/base/preferences/SetHomepagePreference.java
mobile/android/base/resources/layout/preference_set_homepage.xml
mobile/android/base/resources/xml/preferences_home.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
@@ -184,16 +184,19 @@
      selected to be added to the home page. -->
 <!ENTITY home_add_panel_installed "\'&formatS;\' added to homepage">
 <!ENTITY pref_category_home_content_settings "Content settings">
 <!ENTITY pref_home_updates "Automatic updates">
 <!ENTITY pref_home_updates_enabled "Enabled">
 <!ENTITY pref_home_updates_wifi "Only over Wi-Fi">
 <!ENTITY pref_home_suggested_sites "Show site suggestions">
 <!ENTITY pref_home_suggested_sites_summary "Display shortcuts to sites on your homepage that we think you might find interesting">
+<!ENTITY pref_category_home_homepage "Homepage">
+<!ENTITY home_homepage_title "Set a Homepage">
+<!ENTITY home_homepage_use_current_tab "Use current tab">
 
 <!-- Localization note: These are shown in the left sidebar on tablets -->
 <!ENTITY pref_header_customize "Customize">
 <!ENTITY pref_header_display "Display">
 <!ENTITY pref_header_privacy_short "Privacy">
 <!ENTITY pref_header_help "Help">
 <!ENTITY pref_header_language "Language">
 <!ENTITY pref_header_vendor "&vendorShortName;">
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -414,16 +414,17 @@ gbjar.sources += [
     'preferences/ModifiableHintPreference.java',
     'preferences/MultiChoicePreference.java',
     'preferences/MultiPrefMultiChoicePreference.java',
     'preferences/PanelsPreference.java',
     'preferences/PanelsPreferenceCategory.java',
     'preferences/PrivateDataPreference.java',
     'preferences/SearchEnginePreference.java',
     'preferences/SearchPreferenceCategory.java',
+    'preferences/SetHomepagePreference.java',
     'preferences/SyncPreference.java',
     'PrefsHelper.java',
     'PrintHelper.java',
     'PrivateTab.java',
     'prompts/ColorPickerInput.java',
     'prompts/IconGridInput.java',
     'prompts/IntentChooserPrompt.java',
     'prompts/IntentHandler.java',
--- a/mobile/android/base/preferences/GeckoPreferences.java
+++ b/mobile/android/base/preferences/GeckoPreferences.java
@@ -1,25 +1,16 @@
 /* -*- 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.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Locale;
-import java.util.Map;
-
-import android.os.Build;
-
-import org.json.JSONObject;
+import org.mozilla.gecko.AboutPages;
 import org.mozilla.gecko.AppConstants;
 import org.mozilla.gecko.AppConstants.Versions;
 import org.mozilla.gecko.BrowserApp;
 import org.mozilla.gecko.BrowserLocaleManager;
 import org.mozilla.gecko.DataReportingNotification;
 import org.mozilla.gecko.EventDispatcher;
 import org.mozilla.gecko.GeckoActivityStatus;
 import org.mozilla.gecko.GeckoAppShell;
@@ -38,33 +29,34 @@ import org.mozilla.gecko.TelemetryContra
 import org.mozilla.gecko.background.common.GlobalConstants;
 import org.mozilla.gecko.background.healthreport.HealthReportConstants;
 import org.mozilla.gecko.db.BrowserContract.SuggestedSites;
 import org.mozilla.gecko.restrictions.Restriction;
 import org.mozilla.gecko.updater.UpdateService;
 import org.mozilla.gecko.updater.UpdateServiceHelper;
 import org.mozilla.gecko.util.GeckoEventListener;
 import org.mozilla.gecko.util.HardwareUtils;
+import org.mozilla.gecko.util.InputOptionsUtils;
 import org.mozilla.gecko.util.ThreadUtils;
-import org.mozilla.gecko.util.InputOptionsUtils;
 import org.mozilla.gecko.widget.FloatingHintEditText;
 
 import android.app.ActionBar;
 import android.app.AlertDialog;
 import android.app.Dialog;
 import android.app.Fragment;
 import android.app.FragmentManager;
 import android.app.NotificationManager;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
 import android.content.res.Configuration;
+import android.os.Build;
 import android.os.Bundle;
 import android.preference.CheckBoxPreference;
 import android.preference.EditTextPreference;
 import android.preference.ListPreference;
 import android.preference.Preference;
 import android.preference.Preference.OnPreferenceChangeListener;
 import android.preference.Preference.OnPreferenceClickListener;
 import android.preference.PreferenceActivity;
@@ -79,16 +71,24 @@ import android.util.Log;
 import android.view.MenuItem;
 import android.view.View;
 import android.widget.AdapterView;
 import android.widget.EditText;
 import android.widget.LinearLayout;
 import android.widget.ListAdapter;
 import android.widget.ListView;
 import android.widget.Toast;
+import org.json.JSONObject;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
 
 public class GeckoPreferences
 extends PreferenceActivity
 implements
 GeckoActivityStatus,
 GeckoEventListener,
 OnPreferenceChangeListener,
 OnSharedPreferenceChangeListener
@@ -665,17 +665,17 @@ OnSharedPreferenceChangeListener
       * @param preferences The android.preference.PreferenceGroup to initialize
       * @param prefs An ArrayList to fill with Gecko preferences that need to be
       *        initialized
       * @return The integer id for the PrefsHelper.PrefHandlerBase listener added
       *         to monitor changes to Gecko prefs.
       */
     private void setupPreferences(PreferenceGroup preferences, ArrayList<String> prefs) {
         for (int i = 0; i < preferences.getPreferenceCount(); i++) {
-            Preference pref = preferences.getPreference(i);
+            final Preference pref = preferences.getPreference(i);
 
             // Eliminate locale switching if necessary.
             // This logic will need to be extended when
             // content language selection (Bug 881510) is implemented.
             if (!localeSwitchingIsEnabled &&
                 "preferences_locale".equals(pref.getExtras().getString("resource"))) {
                 preferences.removePreference(pref);
                 i--;
@@ -873,31 +873,49 @@ OnSharedPreferenceChangeListener
                         continue;
                     }
                 } else if (PREFS_MP_ENABLED.equals(key)) {
                     if (!RestrictedProfiles.isAllowed(this, Restriction.DISALLOW_MASTER_PASSWORD)) {
                         preferences.removePreference(pref);
                         i--;
                         continue;
                     }
+                } else if (PREFS_HOMEPAGE.equals(key)) {
+                    String setUrl = GeckoSharedPrefs.forProfile(getBaseContext()).getString(PREFS_HOMEPAGE, AboutPages.HOME);
+                    setHomePageSummary(pref, setUrl);
+                    pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
+                        @Override
+                        public boolean onPreferenceChange(final Preference preference, final Object newValue) {
+                            setHomePageSummary(pref, String.valueOf(newValue));
+                            return true;
+                        }
+                    });
                 }
 
                 // Some Preference UI elements are not actually preferences,
                 // but they require a key to work correctly. For example,
                 // "Clear private data" requires a key for its state to be
                 // saved when the orientation changes. It uses the
                 // "android.not_a_preference.privacy.clear" key - which doesn't
                 // exist in Gecko - to satisfy this requirement.
                 if (isGeckoPref(key)) {
                     prefs.add(key);
                 }
             }
         }
     }
 
+    private void setHomePageSummary(Preference pref, String value) {
+        if (!TextUtils.isEmpty(value)) {
+            pref.setSummary(value);
+        } else {
+            pref.setSummary(AboutPages.HOME);
+        }
+    }
+
     private boolean isGeckoPref(String key) {
         if (TextUtils.isEmpty(key)) {
             return false;
         }
 
         if (key.startsWith(NON_PREF_PREFIX)) {
             return false;
         }
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/preferences/SetHomepagePreference.java
@@ -0,0 +1,105 @@
+/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; 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 org.mozilla.gecko.AboutPages;
+import org.mozilla.gecko.GeckoSharedPrefs;
+import org.mozilla.gecko.R;
+import org.mozilla.gecko.Tabs;
+
+import android.app.AlertDialog;
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.preference.DialogPreference;
+import android.text.TextUtils;
+import android.util.AttributeSet;
+import android.view.View;
+import android.view.inputmethod.InputMethodManager;
+import android.widget.CheckBox;
+import android.widget.CompoundButton;
+import android.widget.EditText;
+
+public class SetHomepagePreference extends DialogPreference {
+    SharedPreferences prefs;
+    EditText homepageTextEdit;
+    CheckBox useCurrentTabCheck;
+
+    public SetHomepagePreference(final Context context, final AttributeSet attrs) {
+        super(context, attrs);
+
+        setPersistent(false);
+        setDialogLayoutResource(R.layout.preference_set_homepage);
+
+        prefs = GeckoSharedPrefs.forProfile(context);
+    }
+
+    @Override
+    protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
+        // Without this GB devices have a black background to the dialog.
+        builder.setInverseBackgroundForced(true);
+    }
+
+    @Override
+    protected void onBindDialogView(final View view) {
+        super.onBindDialogView(view);
+
+        homepageTextEdit = (EditText) view.findViewById(R.id.homepage_url);
+        homepageTextEdit.requestFocus();
+
+        homepageTextEdit.post(new Runnable() {
+            @Override
+            public void run() {
+                InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
+                imm.showSoftInput(homepageTextEdit, InputMethodManager.SHOW_IMPLICIT);
+                homepageTextEdit.selectAll();
+            }
+        });
+
+        final String url = prefs.getString(GeckoPreferences.PREFS_HOMEPAGE, AboutPages.HOME);
+        if (!AboutPages.HOME.equals(url)) {
+            homepageTextEdit.setText(url);
+        }
+
+        useCurrentTabCheck = (CheckBox) view.findViewById(R.id.use_current_checkbox);
+
+        view.findViewById(R.id.use_current_title).setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(final View v) {
+                useCurrentTabCheck.toggle();
+            }
+        });
+
+        useCurrentTabCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
+            @Override
+            public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
+                homepageTextEdit.setEnabled(!isChecked);
+                if (isChecked) {
+                    homepageTextEdit.setText(Tabs.getInstance().getSelectedTab().getURL());
+                } else {
+                    homepageTextEdit.selectAll();
+                }
+            }
+        });
+    }
+
+    @Override
+    protected void onDialogClosed(final boolean positiveResult) {
+        super.onDialogClosed(positiveResult);
+        if (positiveResult) {
+            final SharedPreferences.Editor editor = prefs.edit();
+            String newValue = homepageTextEdit.getText().toString();
+            if (AboutPages.HOME.equals(newValue) || TextUtils.isEmpty(newValue)) {
+                editor.remove(GeckoPreferences.PREFS_HOMEPAGE);
+                newValue = "";
+            } else {
+                editor.putString(GeckoPreferences.PREFS_HOMEPAGE, newValue);
+            }
+            editor.apply();
+            if (getOnPreferenceChangeListener() != null) {
+                getOnPreferenceChangeListener().onPreferenceChange(this, newValue);
+            }
+        }
+    }
+}
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/resources/layout/preference_set_homepage.xml
@@ -0,0 +1,40 @@
+<?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/. -->
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+              xmlns:tools="http://schemas.android.com/tools"
+              android:layout_width="match_parent"
+              android:layout_height="match_parent"
+              android:paddingLeft="20dp"
+              android:paddingRight="20dp"
+              android:orientation="vertical">
+
+    <EditText
+            android:id="@+id/homepage_url"
+            android:layout_width="fill_parent"
+            android:layout_height="wrap_content"
+            android:inputType="textUri"
+            android:layout_marginTop="8dp" />
+
+    <LinearLayout android:layout_width="wrap_content"
+                  android:layout_height="wrap_content"
+                  android:orientation="horizontal"
+                  android:gravity="center_vertical">
+
+        <CheckBox android:layout_width="wrap_content"
+                  android:layout_height="wrap_content"
+                  android:layout_marginTop="8dp"
+                  android:id="@+id/use_current_checkbox" />
+
+        <TextView android:id="@+id/use_current_title"
+                  android:layout_width="wrap_content"
+                  android:layout_height="wrap_content"
+                  android:layout_marginTop="8dp"
+                  android:text="@string/home_homepage_use_current_tab"
+
+                  tools:text="Use current tab" />
+
+    </LinearLayout>
+
+</LinearLayout>
--- a/mobile/android/base/resources/xml/preferences_home.xml
+++ b/mobile/android/base/resources/xml/preferences_home.xml
@@ -1,18 +1,25 @@
 <?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/. -->
 
 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
-                  xmlns:gecko="http://schemas.android.com/apk/res-auto"
                   android:title="@string/pref_category_home"
                   android:enabled="false">
 
+    <PreferenceCategory android:title="@string/pref_category_home_homepage">
+
+        <org.mozilla.gecko.preferences.SetHomepagePreference
+                android:key="android.not_a_preference.homepage"
+                android:title="@string/home_homepage_title" />
+
+    </PreferenceCategory>
+
     <org.mozilla.gecko.preferences.PanelsPreferenceCategory
         android:title="@string/pref_category_home_panels"/>
 
     <PreferenceCategory android:title="@string/pref_category_home_content_settings">
 
         <CheckBoxPreference android:key="android.not_a_preference.home_suggested_sites"
                             android:title="@string/pref_home_suggested_sites"
                             android:summary="@string/pref_home_suggested_sites_summary"
--- a/mobile/android/base/strings.xml.in
+++ b/mobile/android/base/strings.xml.in
@@ -166,16 +166,19 @@
   <string name="home_add_panel_empty">&home_add_panel_empty;</string>
   <string name="home_add_panel_installed">&home_add_panel_installed;</string>
   <string name="pref_category_home_content_settings">&pref_category_home_content_settings;</string>
   <string name="pref_home_updates">&pref_home_updates;</string>
   <string name="pref_home_updates_enabled">&pref_home_updates_enabled;</string>
   <string name="pref_home_updates_wifi">&pref_home_updates_wifi;</string>
   <string name="pref_home_suggested_sites">&pref_home_suggested_sites;</string>
   <string name="pref_home_suggested_sites_summary">&pref_home_suggested_sites_summary;</string>
+  <string name="pref_category_home_homepage">&pref_category_home_homepage;</string>
+  <string name="home_homepage_title">&home_homepage_title;</string>
+  <string name="home_homepage_use_current_tab">&home_homepage_use_current_tab;</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_language">&pref_header_language;</string>
   <string name="pref_header_vendor">&pref_header_vendor;</string>
   <string name="pref_header_devtools">&pref_header_devtools;</string>