Bug 1361755 - Allow distributions to specify a homepage option in prefs. r?snorp draft
authorMichael Kaply <mozilla@kaply.com>
Wed, 03 May 2017 15:47:09 -0500
changeset 587158 b0d1b87f30624f64e20ebc4a0bf6cae0ceb70624
parent 586973 7b8937970f9ca85db88cb2496f2112175fd847c8
child 631205 595e37588c3e9e71185b40e0e24031fa1183c9fa
push id61636
push usermozilla@kaply.com
push dateWed, 31 May 2017 16:35:49 +0000
reviewerssnorp
bugs1361755
milestone55.0a1
Bug 1361755 - Allow distributions to specify a homepage option in prefs. r?snorp MozReview-Commit-ID: KjdZvHNG6VR
mobile/android/base/java/org/mozilla/gecko/preferences/GeckoPreferences.java
mobile/android/base/java/org/mozilla/gecko/preferences/SetHomepagePreference.java
mobile/android/base/resources/layout/preference_set_homepage.xml
--- a/mobile/android/base/java/org/mozilla/gecko/preferences/GeckoPreferences.java
+++ b/mobile/android/base/java/org/mozilla/gecko/preferences/GeckoPreferences.java
@@ -165,16 +165,18 @@ public class GeckoPreferences
     public static final String PREFS_ACTIVITY_STREAM = NON_PREF_PREFIX + "experiments.activitystream";
     public static final String PREFS_CATEGORY_EXPERIMENTAL_FEATURES = NON_PREF_PREFIX + "category_experimental";
     public static final String PREFS_COMPACT_TABS = NON_PREF_PREFIX + "compact_tabs";
     public static final String PREFS_SHOW_QUIT_MENU = NON_PREF_PREFIX + "distribution.show_quit_menu";
     public static final String PREFS_SEARCH_SUGGESTIONS_ENABLED = "browser.search.suggest.enabled";
     public static final String PREFS_DEFAULT_BROWSER = NON_PREF_PREFIX + "default_browser.link";
     public static final String PREFS_SYSTEM_FONT_SIZE = NON_PREF_PREFIX + "font.size.use_system_font_size";
     public static final String PREFS_SET_AS_HOMEPAGE = NON_PREF_PREFIX + "distribution.set_as_homepage";
+    public static final String PREFS_DIST_HOMEPAGE = NON_PREF_PREFIX + "distribution.homepage";
+    public static final String PREFS_DIST_HOMEPAGE_NAME = NON_PREF_PREFIX + "distribution.homepage.name";
 
     private static final String ACTION_STUMBLER_UPLOAD_PREF = "STUMBLER_PREF";
 
 
     // This isn't a Gecko pref, even if it looks like one.
     private static final String PREFS_BROWSER_LOCALE = "locale";
 
     public static final String PREFS_RESTORE_SESSION = NON_PREF_PREFIX + "restoreSession3";
--- a/mobile/android/base/java/org/mozilla/gecko/preferences/SetHomepagePreference.java
+++ b/mobile/android/base/java/org/mozilla/gecko/preferences/SetHomepagePreference.java
@@ -15,23 +15,25 @@ import android.preference.DialogPreferen
 import android.text.TextUtils;
 import android.util.AttributeSet;
 import android.view.View;
 import android.view.inputmethod.InputMethodManager;
 import android.widget.EditText;
 import android.widget.RadioButton;
 import android.widget.RadioGroup;
 
+
 public class SetHomepagePreference extends DialogPreference {
     private static final String DEFAULT_HOMEPAGE = AboutPages.HOME;
 
     private final SharedPreferences prefs;
 
     private RadioGroup homepageLayout;
     private RadioButton defaultRadio;
+    private RadioButton distributionRadio;
     private RadioButton userAddressRadio;
     private EditText homepageEditText;
 
     // This is the url that 1) was loaded from prefs or, 2) stored
     // when the user pressed the "default homepage" checkbox.
     private String storedUrl;
 
     public SetHomepagePreference(final Context context, final AttributeSet attrs) {
@@ -46,16 +48,17 @@ public class SetHomepagePreference exten
     }
 
     @Override
     protected void onBindDialogView(final View view) {
         super.onBindDialogView(view);
 
         homepageLayout = (RadioGroup) view.findViewById(R.id.homepage_layout);
         defaultRadio = (RadioButton) view.findViewById(R.id.radio_default);
+        distributionRadio = (RadioButton) view.findViewById(R.id.radio_distribution);
         userAddressRadio = (RadioButton) view.findViewById(R.id.radio_user_address);
         homepageEditText = (EditText) view.findViewById(R.id.edittext_user_address);
 
         storedUrl = prefs.getString(GeckoPreferences.PREFS_HOMEPAGE, DEFAULT_HOMEPAGE);
 
         homepageLayout.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
             @Override
             public void onCheckedChanged(final RadioGroup radioGroup, final int checkedId) {
@@ -66,28 +69,42 @@ public class SetHomepagePreference exten
                     homepageEditText.setVisibility(View.GONE);
                 }
             }
         });
         setUIState(storedUrl);
     }
 
     private void setUIState(final String url) {
+        if (prefs.contains(GeckoPreferences.PREFS_DIST_HOMEPAGE_NAME) &&
+            prefs.contains(GeckoPreferences.PREFS_DIST_HOMEPAGE)) {
+            distributionRadio.setText(prefs.getString(GeckoPreferences.PREFS_DIST_HOMEPAGE_NAME, ""));
+        } else {
+            distributionRadio.setVisibility(View.GONE);
+        }
         if (isUrlDefaultHomepage(url)) {
             defaultRadio.setChecked(true);
+        } else if (distributionRadio.getVisibility() == View.VISIBLE &&
+                   isUrlDistributionHomepage(url)) {
+            distributionRadio.setChecked(true);
         } else {
             userAddressRadio.setChecked(true);
             homepageEditText.setText(url);
         }
     }
 
     private boolean isUrlDefaultHomepage(final String url) {
         return TextUtils.isEmpty(url) || DEFAULT_HOMEPAGE.equals(url);
     }
 
+    private boolean isUrlDistributionHomepage(final String url) {
+        String distributionHomepage = prefs.getString(GeckoPreferences.PREFS_DIST_HOMEPAGE, "");
+        return distributionHomepage.equals(url);
+    }
+
     private static void openKeyboardAndSelectAll(final Context context, final View viewToFocus) {
         viewToFocus.requestFocus();
         viewToFocus.post(new Runnable() {
             @Override
             public void run() {
                 InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                 imm.showSoftInput(viewToFocus, InputMethodManager.SHOW_IMPLICIT);
                 // android:selectAllOnFocus doesn't work for the initial focus:
@@ -101,18 +118,21 @@ public class SetHomepagePreference exten
 
     @Override
     protected void onDialogClosed(final boolean positiveResult) {
         super.onDialogClosed(positiveResult);
         if (positiveResult) {
             final SharedPreferences.Editor editor = prefs.edit();
             final String homePageEditTextValue = homepageEditText.getText().toString();
             final String newPrefValue;
-            if (homepageLayout.getCheckedRadioButtonId() == R.id.radio_default ||
-                    isUrlDefaultHomepage(homePageEditTextValue)) {
+            if (homepageLayout.getCheckedRadioButtonId() == R.id.radio_distribution) {
+                newPrefValue = prefs.getString(GeckoPreferences.PREFS_DIST_HOMEPAGE, "");
+                editor.putString(GeckoPreferences.PREFS_HOMEPAGE, newPrefValue);
+            } else if (homepageLayout.getCheckedRadioButtonId() == R.id.radio_default ||
+                       isUrlDefaultHomepage(homePageEditTextValue)) {
                 newPrefValue = "";
                 editor.remove(GeckoPreferences.PREFS_HOMEPAGE);
             } else {
                 newPrefValue = homePageEditTextValue;
                 editor.putString(GeckoPreferences.PREFS_HOMEPAGE, newPrefValue);
             }
             editor.apply();
 
--- a/mobile/android/base/resources/layout/preference_set_homepage.xml
+++ b/mobile/android/base/resources/layout/preference_set_homepage.xml
@@ -1,27 +1,35 @@
 <?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/. -->
 <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
+            xmlns:tools="http://schemas.android.com/tools"
             android:id="@+id/homepage_layout"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:paddingLeft="20dp"
             android:paddingRight="20dp"
             android:orientation="vertical">
 
     <RadioButton android:id="@+id/radio_default"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_marginTop="8dp"
                  android:text="@string/home_homepage_radio_default"
                  android:textColor="@color/text_and_tabs_tray_grey"/>
 
+    <RadioButton android:id="@+id/radio_distribution"
+                 android:layout_width="wrap_content"
+                 android:layout_height="wrap_content"
+                 android:layout_marginTop="8dp"
+                 tools:text="Distribution Homepage"
+                 android:textColor="@color/text_and_tabs_tray_grey"/>
+
     <RadioButton android:id="@+id/radio_user_address"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_marginTop="8dp"
                  android:text="@string/home_homepage_radio_user_address"
                  android:textColor="@color/text_and_tabs_tray_grey"/>
 
     <!-- RadioGroup is a LinearLayout under the hood, so including this View is fine.