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 24 public class SetHomepagePreference extends DialogPreference { 25 private static final String DEFAULT_HOMEPAGE = AboutPages.HOME; 26 27 private final SharedPreferences prefs; 28 29 private RadioGroup homepageLayout; 30 private RadioButton defaultRadio; 31 private RadioButton distributionRadio; 32 private RadioButton userAddressRadio; 33 private EditText homepageEditText; 34 35 // This is the url that 1) was loaded from prefs or, 2) stored 36 // when the user pressed the "default homepage" checkbox. 37 private String storedUrl; 38 SetHomepagePreference(final Context context, final AttributeSet attrs)39 public SetHomepagePreference(final Context context, final AttributeSet attrs) { 40 super(context, attrs); 41 prefs = GeckoSharedPrefs.forProfile(context); 42 } 43 44 @Override onPrepareDialogBuilder(AlertDialog.Builder builder)45 protected void onPrepareDialogBuilder(AlertDialog.Builder builder) { 46 // Without this GB devices have a black background to the dialog. 47 builder.setInverseBackgroundForced(true); 48 } 49 50 @Override onBindDialogView(final View view)51 protected void onBindDialogView(final View view) { 52 super.onBindDialogView(view); 53 54 homepageLayout = (RadioGroup) view.findViewById(R.id.homepage_layout); 55 defaultRadio = (RadioButton) view.findViewById(R.id.radio_default); 56 distributionRadio = (RadioButton) view.findViewById(R.id.radio_distribution); 57 userAddressRadio = (RadioButton) view.findViewById(R.id.radio_user_address); 58 homepageEditText = (EditText) view.findViewById(R.id.edittext_user_address); 59 60 storedUrl = prefs.getString(GeckoPreferences.PREFS_HOMEPAGE, DEFAULT_HOMEPAGE); 61 62 homepageLayout.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 63 @Override 64 public void onCheckedChanged(final RadioGroup radioGroup, final int checkedId) { 65 if (checkedId == R.id.radio_user_address) { 66 homepageEditText.setVisibility(View.VISIBLE); 67 openKeyboardAndSelectAll(getContext(), homepageEditText); 68 } else { 69 homepageEditText.setVisibility(View.GONE); 70 } 71 } 72 }); 73 setUIState(storedUrl); 74 } 75 setUIState(final String url)76 private void setUIState(final String url) { 77 if (prefs.contains(GeckoPreferences.PREFS_DIST_HOMEPAGE_NAME) && 78 prefs.contains(GeckoPreferences.PREFS_DIST_HOMEPAGE)) { 79 distributionRadio.setText(prefs.getString(GeckoPreferences.PREFS_DIST_HOMEPAGE_NAME, "")); 80 } else { 81 distributionRadio.setVisibility(View.GONE); 82 } 83 if (isUrlDefaultHomepage(url)) { 84 defaultRadio.setChecked(true); 85 } else if (distributionRadio.getVisibility() == View.VISIBLE && 86 isUrlDistributionHomepage(url)) { 87 distributionRadio.setChecked(true); 88 } else { 89 userAddressRadio.setChecked(true); 90 homepageEditText.setText(url); 91 } 92 } 93 isUrlDefaultHomepage(final String url)94 private boolean isUrlDefaultHomepage(final String url) { 95 return TextUtils.isEmpty(url) || DEFAULT_HOMEPAGE.equals(url); 96 } 97 isUrlDistributionHomepage(final String url)98 private boolean isUrlDistributionHomepage(final String url) { 99 String distributionHomepage = prefs.getString(GeckoPreferences.PREFS_DIST_HOMEPAGE, ""); 100 return distributionHomepage.equals(url); 101 } 102 openKeyboardAndSelectAll(final Context context, final View viewToFocus)103 private static void openKeyboardAndSelectAll(final Context context, final View viewToFocus) { 104 viewToFocus.requestFocus(); 105 viewToFocus.post(new Runnable() { 106 @Override 107 public void run() { 108 InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); 109 imm.showSoftInput(viewToFocus, InputMethodManager.SHOW_IMPLICIT); 110 // android:selectAllOnFocus doesn't work for the initial focus: 111 // I'm not sure why. We manually selectAll instead. 112 if (viewToFocus instanceof EditText) { 113 ((EditText) viewToFocus).selectAll(); 114 } 115 } 116 }); 117 } 118 119 @Override onDialogClosed(final boolean positiveResult)120 protected void onDialogClosed(final boolean positiveResult) { 121 super.onDialogClosed(positiveResult); 122 if (positiveResult) { 123 final SharedPreferences.Editor editor = prefs.edit(); 124 final String homePageEditTextValue = homepageEditText.getText().toString(); 125 final String newPrefValue; 126 if (homepageLayout.getCheckedRadioButtonId() == R.id.radio_distribution) { 127 newPrefValue = prefs.getString(GeckoPreferences.PREFS_DIST_HOMEPAGE, ""); 128 editor.putString(GeckoPreferences.PREFS_HOMEPAGE, newPrefValue); 129 } else if (homepageLayout.getCheckedRadioButtonId() == R.id.radio_default || 130 isUrlDefaultHomepage(homePageEditTextValue)) { 131 newPrefValue = ""; 132 editor.remove(GeckoPreferences.PREFS_HOMEPAGE); 133 } else { 134 newPrefValue = homePageEditTextValue; 135 editor.putString(GeckoPreferences.PREFS_HOMEPAGE, newPrefValue); 136 } 137 editor.apply(); 138 139 if (getOnPreferenceChangeListener() != null) { 140 getOnPreferenceChangeListener().onPreferenceChange(this, newPrefValue); 141 } 142 } 143 } 144 } 145