AccessibilityPreferencesFragment.java revision 40badf29facf6b5ca894a092062cc27897d2b434
1/*
2 * Copyright (C) 2011 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.R;
21
22import android.content.res.Resources;
23import android.os.Bundle;
24import android.preference.Preference;
25import android.preference.PreferenceFragment;
26import android.util.Log;
27import android.view.View;
28
29public class AccessibilityPreferencesFragment extends PreferenceFragment
30        implements Preference.OnPreferenceChangeListener {
31
32    @Override
33    public void onCreate(Bundle savedInstanceState) {
34        super.onCreate(savedInstanceState);
35        addPreferencesFromResource(R.xml.accessibility_preferences);
36
37        Preference e = findPreference(BrowserSettings.PREF_TEXT_SIZE);
38        e.setOnPreferenceChangeListener(this);
39        e.setSummary(getVisualTextSizeName(
40                getPreferenceScreen().getSharedPreferences()
41                .getString(BrowserSettings.PREF_TEXT_SIZE, null)) );
42    }
43
44    @Override
45    public void onViewCreated(View view, Bundle savedInstanceState) {
46        super.onViewCreated(view, savedInstanceState);
47        getListView().setItemsCanFocus(true);
48    }
49
50    @Override
51    public boolean onPreferenceChange(Preference pref, Object objValue) {
52        if (getActivity() == null) {
53            // We aren't attached, so don't accept preferences changes from the
54            // invisible UI.
55            return false;
56        }
57
58        if (pref.getKey().equals(BrowserSettings.PREF_TEXT_SIZE)) {
59            pref.setSummary(getVisualTextSizeName((String) objValue));
60            return true;
61        }
62        return false;
63    }
64
65    private CharSequence getVisualTextSizeName(String enumName) {
66        Resources res = getActivity().getResources();
67        CharSequence[] visualNames = res.getTextArray(R.array.pref_text_size_choices);
68        CharSequence[] enumNames = res.getTextArray(R.array.pref_text_size_values);
69
70        // Sanity check
71        if (visualNames.length != enumNames.length) {
72            return "";
73        }
74
75        int length = enumNames.length;
76        for (int i = 0; i < length; i++) {
77            if (enumNames[i].equals(enumName)) {
78                return visualNames[i];
79            }
80        }
81
82        return "";
83    }
84
85}