1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 package org.mozilla.gecko.preferences;
6 
7 import org.mozilla.gecko.AboutPages;
8 import org.mozilla.gecko.GeckoSharedPrefs;
9 import org.mozilla.gecko.R;
10 
11 import android.app.AlertDialog;
12 import android.content.Context;
13 import android.content.SharedPreferences;
14 import android.preference.DialogPreference;
15 import android.text.TextUtils;
16 import android.util.AttributeSet;
17 import android.view.View;
18 import android.view.inputmethod.InputMethodManager;
19 import android.widget.EditText;
20 import android.widget.RadioButton;
21 import android.widget.RadioGroup;
22 
23 public class SetHomepagePreference extends DialogPreference {
24     private static final String DEFAULT_HOMEPAGE = AboutPages.HOME;
25 
26     private final SharedPreferences prefs;
27 
28     private RadioGroup homepageLayout;
29     private RadioButton defaultRadio;
30     private RadioButton userAddressRadio;
31     private EditText homepageEditText;
32 
33     // This is the url that 1) was loaded from prefs or, 2) stored
34     // when the user pressed the "default homepage" checkbox.
35     private String storedUrl;
36 
SetHomepagePreference(final Context context, final AttributeSet attrs)37     public SetHomepagePreference(final Context context, final AttributeSet attrs) {
38         super(context, attrs);
39         prefs = GeckoSharedPrefs.forProfile(context);
40     }
41 
42     @Override
onPrepareDialogBuilder(AlertDialog.Builder builder)43     protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
44         // Without this GB devices have a black background to the dialog.
45         builder.setInverseBackgroundForced(true);
46     }
47 
48     @Override
onBindDialogView(final View view)49     protected void onBindDialogView(final View view) {
50         super.onBindDialogView(view);
51 
52         homepageLayout = (RadioGroup) view.findViewById(R.id.homepage_layout);
53         defaultRadio = (RadioButton) view.findViewById(R.id.radio_default);
54         userAddressRadio = (RadioButton) view.findViewById(R.id.radio_user_address);
55         homepageEditText = (EditText) view.findViewById(R.id.edittext_user_address);
56 
57         storedUrl = prefs.getString(GeckoPreferences.PREFS_HOMEPAGE, DEFAULT_HOMEPAGE);
58 
59         homepageLayout.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
60             @Override
61             public void onCheckedChanged(final RadioGroup radioGroup, final int checkedId) {
62                 if (checkedId == R.id.radio_user_address) {
63                     homepageEditText.setVisibility(View.VISIBLE);
64                     openKeyboardAndSelectAll(getContext(), homepageEditText);
65                 } else {
66                     homepageEditText.setVisibility(View.GONE);
67                 }
68             }
69         });
70         setUIState(storedUrl);
71     }
72 
setUIState(final String url)73     private void setUIState(final String url) {
74         if (isUrlDefaultHomepage(url)) {
75             defaultRadio.setChecked(true);
76         } else {
77             userAddressRadio.setChecked(true);
78             homepageEditText.setText(url);
79         }
80     }
81 
isUrlDefaultHomepage(final String url)82     private boolean isUrlDefaultHomepage(final String url) {
83         return TextUtils.isEmpty(url) || DEFAULT_HOMEPAGE.equals(url);
84     }
85 
openKeyboardAndSelectAll(final Context context, final View viewToFocus)86     private static void openKeyboardAndSelectAll(final Context context, final View viewToFocus) {
87         viewToFocus.requestFocus();
88         viewToFocus.post(new Runnable() {
89             @Override
90             public void run() {
91                 InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
92                 imm.showSoftInput(viewToFocus, InputMethodManager.SHOW_IMPLICIT);
93                 // android:selectAllOnFocus doesn't work for the initial focus:
94                 // I'm not sure why. We manually selectAll instead.
95                 if (viewToFocus instanceof EditText) {
96                     ((EditText) viewToFocus).selectAll();
97                 }
98             }
99         });
100     }
101 
102     @Override
onDialogClosed(final boolean positiveResult)103     protected void onDialogClosed(final boolean positiveResult) {
104         super.onDialogClosed(positiveResult);
105         if (positiveResult) {
106             final SharedPreferences.Editor editor = prefs.edit();
107             final String homePageEditTextValue = homepageEditText.getText().toString();
108             final String newPrefValue;
109             if (homepageLayout.getCheckedRadioButtonId() == R.id.radio_default ||
110                     isUrlDefaultHomepage(homePageEditTextValue)) {
111                 newPrefValue = "";
112                 editor.remove(GeckoPreferences.PREFS_HOMEPAGE);
113             } else {
114                 newPrefValue = homePageEditTextValue;
115                 editor.putString(GeckoPreferences.PREFS_HOMEPAGE, newPrefValue);
116             }
117             editor.apply();
118 
119             if (getOnPreferenceChangeListener() != null) {
120                 getOnPreferenceChangeListener().onPreferenceChange(this, newPrefValue);
121             }
122         }
123     }
124 }
125