PrivacySecurityPreferencesFragment.java revision 570d8144c4e1683bf45376e8c4aca894be915485
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
17package com.android.browser.preferences;
18
19import com.android.browser.BrowserSettings;
20import com.android.browser.GoogleAccountLogin;
21import com.android.browser.R;
22
23import android.accounts.Account;
24import android.app.Activity;
25import android.content.Intent;
26import android.os.Bundle;
27import android.preference.ListPreference;
28import android.preference.Preference;
29import android.preference.PreferenceFragment;
30
31public class PrivacySecurityPreferencesFragment extends PreferenceFragment
32        implements Preference.OnPreferenceChangeListener {
33
34    private BrowserSettings mSettings;
35
36    @Override
37    public void onCreate(Bundle savedInstanceState) {
38        super.onCreate(savedInstanceState);
39
40        mSettings = BrowserSettings.getInstance();
41
42        // Load the preferences from an XML resource
43        addPreferencesFromResource(R.xml.privacy_security_preferences);
44
45        Preference e = findPreference(BrowserSettings.PREF_CLEAR_HISTORY);
46        e.setOnPreferenceChangeListener(this);
47    }
48
49    @Override
50    public void onResume() {
51        super.onResume();
52        setupAutoLoginPreference();
53    }
54
55    void setupAutoLoginPreference() {
56        ListPreference autologinPref = (ListPreference) findPreference(
57                BrowserSettings.PREF_AUTOLOGIN_ACCOUNT);
58        autologinPref.setOnPreferenceChangeListener(this);
59        updateAutoLoginSummary(autologinPref);
60        Account[] accounts = GoogleAccountLogin.getAccounts(getActivity());
61        // +1 for disable
62        CharSequence[] names = new CharSequence[accounts.length + 1];
63        CharSequence[] values = new CharSequence[names.length];
64        int i = 0;
65        int defaultAccount = 0;
66        for (Account a : accounts) {
67            values[i] = names[i] = a.name;
68            i++;
69        }
70        names[i] = getResources().getString(R.string.pref_autologin_disable);
71        values[i] = "";
72        autologinPref.setEntries(names);
73        autologinPref.setEntryValues(values);
74        autologinPref.setValue(BrowserSettings.getInstance()
75                .getAutoLoginAccount(getActivity()));
76    }
77
78    private void updateAutoLoginSummary(Preference pref) {
79        if (!mSettings.isAutoLoginEnabled()) {
80            pref.setSummary(R.string.pref_autologin_disable);
81        } else {
82            String account = mSettings.getAutoLoginAccount(getActivity());
83            if (account == null) {
84                pref.setSummary(R.string.pref_autologin_no_account);
85            } else {
86                pref.setSummary(getString(R.string.pref_autologin_summary, account));
87            }
88        }
89    }
90
91    @Override
92    public boolean onPreferenceChange(Preference pref, Object objValue) {
93        if (pref.getKey().equals(BrowserSettings.PREF_CLEAR_HISTORY)
94                && ((Boolean) objValue).booleanValue() == true) {
95            // Need to tell the browser to remove the parent/child relationship
96            // between tabs
97            getActivity().setResult(Activity.RESULT_OK, (new Intent()).putExtra(Intent.EXTRA_TEXT,
98                    pref.getKey()));
99            return true;
100        } else if (pref.getKey().equals(BrowserSettings.PREF_AUTOLOGIN_ACCOUNT)) {
101            String account = (String) objValue;
102            if (account.length() == 0) {
103                // Disable
104                mSettings.setAutoLoginEnabled(getActivity(), false);
105            } else {
106                mSettings.setAutoLoginEnabled(getActivity(), true);
107            }
108            mSettings.setAutoLoginAccount(getActivity(), account);
109            updateAutoLoginSummary(pref);
110            return true;
111        }
112
113        return false;
114    }
115
116}
117