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