Bug 1227348 - Add default homepage radio buttons to homepage preference. r=margaret

--HG--
extra : commitid : EyUoDoJAiHG
extra : rebase_source : 1e8c7e7a2ebdc357a24cd68f668cd670c4916f18
This commit is contained in:
Michael Comella 2015-12-01 14:03:45 -08:00
parent 278a244439
commit 6092606eef
4 changed files with 114 additions and 29 deletions

View File

@ -191,6 +191,21 @@
<!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">
<!-- Localization note (home_homepage_radio_user_address): The user will see a series of radio
buttons to choose the homepage they'd like to start on. When they click the radio
button for this string, they will use the built-in default Firefox homepage (about:home). -->
<!ENTITY home_homepage_radio_default "&brandShortName; Home">
<!-- Localization note (home_homepage_radio_user_address): The user will see a series of radio
buttons to choose the homepage they'd like to start on. When they click the radio
button for this string, a text field will appear below the radio button and allow the
user to insert an address of their choice. -->
<!ENTITY home_homepage_radio_user_address "Custom">
<!-- Localization note (home_homepage_hint_user_address): The user will see a series of
radio buttons to choose the homepage they'd like to start on. When they click a
particular radio button, a text field will appear below the radio button and allow the
user to insert an address of their choice. This string is the hint text to that
text field. -->
<!ENTITY home_homepage_hint_user_address "Enter address or search term">
<!-- Localization note: These are shown in the left sidebar on tablets -->
<!ENTITY pref_header_general "General">

View File

@ -17,10 +17,22 @@ 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 EditText homepageTextEdit;
private RadioGroup homepageLayout;
private RadioButton defaultRadio;
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) {
super(context, attrs);
@ -41,39 +53,75 @@ public class SetHomepagePreference extends DialogPreference {
protected void onBindDialogView(final View view) {
super.onBindDialogView(view);
homepageTextEdit = (EditText) view.findViewById(R.id.homepage_url);
homepageTextEdit.requestFocus();
homepageLayout = (RadioGroup) view.findViewById(R.id.homepage_layout);
defaultRadio = (RadioButton) view.findViewById(R.id.radio_default);
userAddressRadio = (RadioButton) view.findViewById(R.id.radio_user_address);
homepageEditText = (EditText) view.findViewById(R.id.edittext_user_address);
homepageTextEdit.post(new Runnable() {
storedUrl = prefs.getString(GeckoPreferences.PREFS_HOMEPAGE, DEFAULT_HOMEPAGE);
homepageLayout.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(homepageTextEdit, InputMethodManager.SHOW_IMPLICIT);
homepageTextEdit.selectAll();
public void onCheckedChanged(final RadioGroup radioGroup, final int checkedId) {
if (checkedId == R.id.radio_user_address) {
homepageEditText.setVisibility(View.VISIBLE);
openKeyboardAndSelectAll(getContext(), homepageEditText);
} else {
homepageEditText.setVisibility(View.GONE);
}
}
});
setUIState(storedUrl);
}
final String url = prefs.getString(GeckoPreferences.PREFS_HOMEPAGE, AboutPages.HOME);
if (!AboutPages.HOME.equals(url)) {
homepageTextEdit.setText(url);
private void setUIState(final String url) {
if (isUrlDefaultHomepage(url)) {
defaultRadio.setChecked(true);
} else {
userAddressRadio.setChecked(true);
homepageEditText.setText(url);
}
}
private boolean isUrlDefaultHomepage(final String url) {
return TextUtils.isEmpty(url) || DEFAULT_HOMEPAGE.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:
// I'm not sure why. We manually selectAll instead.
if (viewToFocus instanceof EditText) {
((EditText) viewToFocus).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)) {
final String homePageEditTextValue = homepageEditText.getText().toString();
final String newPrefValue;
if (homepageLayout.getCheckedRadioButtonId() == R.id.radio_default ||
isUrlDefaultHomepage(homePageEditTextValue)) {
newPrefValue = "";
editor.remove(GeckoPreferences.PREFS_HOMEPAGE);
newValue = "";
} else {
editor.putString(GeckoPreferences.PREFS_HOMEPAGE, newValue);
newPrefValue = homePageEditTextValue;
editor.putString(GeckoPreferences.PREFS_HOMEPAGE, newPrefValue);
}
editor.apply();
if (getOnPreferenceChangeListener() != null) {
getOnPreferenceChangeListener().onPreferenceChange(this, newValue);
getOnPreferenceChangeListener().onPreferenceChange(this, newPrefValue);
}
}
}

View File

@ -2,18 +2,37 @@
<!-- 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"
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"
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/homepage_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textUri"
android:layout_marginTop="8dp" />
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:orientation="vertical">
</LinearLayout>
<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_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.
The visibility changes with RadioButton state so we hide it to start. -->
<EditText android:id="@+id/edittext_user_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:inputType="textUri"
android:hint="@string/home_homepage_hint_user_address"
android:textColorHint="@color/disabled_grey"
android:visibility="gone"/>
</RadioGroup>

View File

@ -184,6 +184,9 @@
<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_radio_default">&home_homepage_radio_default;</string>
<string name="home_homepage_radio_user_address">&home_homepage_radio_user_address;</string>
<string name="home_homepage_hint_user_address">&home_homepage_hint_user_address;</string>
<string name="pref_header_general">&pref_header_general;</string>
<string name="pref_header_search">&pref_header_search;</string>