Home | History | Annotate | Download | only in preferences
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License
     15  */
     16 
     17 package com.android.browser.preferences;
     18 
     19 import android.app.AlertDialog;
     20 import android.content.DialogInterface;
     21 import android.content.DialogInterface.OnClickListener;
     22 import android.content.res.Resources;
     23 import android.os.Bundle;
     24 import android.preference.ListPreference;
     25 import android.preference.Preference;
     26 import android.preference.PreferenceFragment;
     27 import android.preference.PreferenceScreen;
     28 import android.text.InputType;
     29 import android.text.TextUtils;
     30 import android.util.Log;
     31 import android.view.KeyEvent;
     32 import android.view.WindowManager;
     33 import android.view.inputmethod.EditorInfo;
     34 import android.widget.EditText;
     35 import android.widget.TextView;
     36 import android.widget.TextView.OnEditorActionListener;
     37 
     38 import com.android.browser.BrowserPreferencesPage;
     39 import com.android.browser.BrowserSettings;
     40 import com.android.browser.PreferenceKeys;
     41 import com.android.browser.R;
     42 import com.android.browser.UrlUtils;
     43 import com.android.browser.homepages.HomeProvider;
     44 
     45 public class GeneralPreferencesFragment extends PreferenceFragment
     46         implements Preference.OnPreferenceChangeListener {
     47 
     48     static final String TAG = "PersonalPreferencesFragment";
     49 
     50     static final String BLANK_URL = "about:blank";
     51     static final String CURRENT = "current";
     52     static final String BLANK = "blank";
     53     static final String DEFAULT = "default";
     54     static final String MOST_VISITED = "most_visited";
     55     static final String OTHER = "other";
     56 
     57     static final String PREF_HOMEPAGE_PICKER = "homepage_picker";
     58 
     59     String[] mChoices, mValues;
     60     String mCurrentPage;
     61 
     62     @Override
     63     public void onCreate(Bundle savedInstanceState) {
     64         super.onCreate(savedInstanceState);
     65         Resources res = getActivity().getResources();
     66         mChoices = res.getStringArray(R.array.pref_homepage_choices);
     67         mValues = res.getStringArray(R.array.pref_homepage_values);
     68         mCurrentPage = getActivity().getIntent()
     69                 .getStringExtra(BrowserPreferencesPage.CURRENT_PAGE);
     70 
     71         // Load the XML preferences file
     72         addPreferencesFromResource(R.xml.general_preferences);
     73 
     74         ListPreference pref = (ListPreference) findPreference(PREF_HOMEPAGE_PICKER);
     75         pref.setSummary(getHomepageSummary());
     76         pref.setPersistent(false);
     77         pref.setValue(getHomepageValue());
     78         pref.setOnPreferenceChangeListener(this);
     79     }
     80 
     81     @Override
     82     public boolean onPreferenceChange(Preference pref, Object objValue) {
     83         if (getActivity() == null) {
     84             // We aren't attached, so don't accept preferences changes from the
     85             // invisible UI.
     86             Log.w("PageContentPreferencesFragment", "onPreferenceChange called from detached fragment!");
     87             return false;
     88         }
     89 
     90         if (pref.getKey().equals(PREF_HOMEPAGE_PICKER)) {
     91             BrowserSettings settings = BrowserSettings.getInstance();
     92             if (CURRENT.equals(objValue)) {
     93                 settings.setHomePage(mCurrentPage);
     94             }
     95             if (BLANK.equals(objValue)) {
     96                 settings.setHomePage(BLANK_URL);
     97             }
     98             if (DEFAULT.equals(objValue)) {
     99                 settings.setHomePage(BrowserSettings.getFactoryResetHomeUrl(
    100                         getActivity()));
    101             }
    102             if (MOST_VISITED.equals(objValue)) {
    103                 settings.setHomePage(HomeProvider.MOST_VISITED);
    104             }
    105             if (OTHER.equals(objValue)) {
    106                 promptForHomepage((ListPreference) pref);
    107                 return false;
    108             }
    109             pref.setSummary(getHomepageSummary());
    110             ((ListPreference)pref).setValue(getHomepageValue());
    111             return false;
    112         }
    113 
    114         return true;
    115     }
    116 
    117     void promptForHomepage(final ListPreference pref) {
    118         final BrowserSettings settings = BrowserSettings.getInstance();
    119         final EditText editText = new EditText(getActivity());
    120         editText.setInputType(InputType.TYPE_CLASS_TEXT
    121                 | InputType.TYPE_TEXT_VARIATION_URI);
    122         editText.setText(settings.getHomePage());
    123         editText.setSelectAllOnFocus(true);
    124         editText.setSingleLine(true);
    125         editText.setImeActionLabel(null, EditorInfo.IME_ACTION_DONE);
    126         final AlertDialog dialog = new AlertDialog.Builder(getActivity())
    127                 .setView(editText)
    128                 .setPositiveButton(android.R.string.ok, new OnClickListener() {
    129                     @Override
    130                     public void onClick(DialogInterface dialog, int which) {
    131                         String homepage = editText.getText().toString().trim();
    132                         homepage = UrlUtils.smartUrlFilter(homepage);
    133                         settings.setHomePage(homepage);
    134                         pref.setValue(getHomepageValue());
    135                         pref.setSummary(getHomepageSummary());
    136                     }
    137                 })
    138                 .setNegativeButton(android.R.string.cancel, new OnClickListener() {
    139                     @Override
    140                     public void onClick(DialogInterface dialog, int which) {
    141                         dialog.cancel();
    142                     }
    143                 })
    144                 .setTitle(R.string.pref_set_homepage_to)
    145                 .create();
    146         editText.setOnEditorActionListener(new OnEditorActionListener() {
    147             @Override
    148             public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    149                 if (actionId == EditorInfo.IME_ACTION_DONE) {
    150                     dialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick();
    151                     return true;
    152                 }
    153                 return false;
    154             }
    155         });
    156         dialog.getWindow().setSoftInputMode(
    157                 WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    158         dialog.show();
    159     }
    160 
    161     String getHomepageValue() {
    162         BrowserSettings settings = BrowserSettings.getInstance();
    163         String homepage = settings.getHomePage();
    164         if (TextUtils.isEmpty(homepage) || BLANK_URL.endsWith(homepage)) {
    165             return BLANK;
    166         }
    167         if (HomeProvider.MOST_VISITED.equals(homepage)) {
    168             return MOST_VISITED;
    169         }
    170         String defaultHomepage = BrowserSettings.getFactoryResetHomeUrl(
    171                 getActivity());
    172         if (TextUtils.equals(defaultHomepage, homepage)) {
    173             return DEFAULT;
    174         }
    175         if (TextUtils.equals(mCurrentPage, homepage)) {
    176             return CURRENT;
    177         }
    178         return OTHER;
    179     }
    180 
    181     String getHomepageSummary() {
    182         BrowserSettings settings = BrowserSettings.getInstance();
    183         if (settings.useMostVisitedHomepage()) {
    184             return getHomepageLabel(MOST_VISITED);
    185         }
    186         String homepage = settings.getHomePage();
    187         if (TextUtils.isEmpty(homepage) || BLANK_URL.equals(homepage)) {
    188             return getHomepageLabel(BLANK);
    189         }
    190         return homepage;
    191     }
    192 
    193     String getHomepageLabel(String value) {
    194         for (int i = 0; i < mValues.length; i++) {
    195             if (value.equals(mValues[i])) {
    196                 return mChoices[i];
    197             }
    198         }
    199         return null;
    200     }
    201 
    202     @Override
    203     public void onResume() {
    204         super.onResume();
    205 
    206         refreshUi();
    207     }
    208 
    209     void refreshUi() {
    210         PreferenceScreen autoFillSettings =
    211                 (PreferenceScreen)findPreference(PreferenceKeys.PREF_AUTOFILL_PROFILE);
    212         autoFillSettings.setDependency(PreferenceKeys.PREF_AUTOFILL_ENABLED);
    213     }
    214 }
    215