AccessibilityPreferencesFragment.java revision 8fc22a1c7846b4c7c7b74d1ec59ffc59c2695c15
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    }
47
48    @Override
49    public boolean onPreferenceChange(Preference pref, Object objValue) {
50        if (getActivity() == null) {
51            // We aren't attached, so don't accept preferences changes from the
52            // invisible UI.
53            return false;
54        }
55
56        if (pref.getKey().equals(PreferenceKeys.PREF_TEXT_SIZE)) {
57            pref.setSummary(getVisualTextSizeName((String) objValue));
58            return true;
59        }
60        return false;
61    }
62
63    private CharSequence getVisualTextSizeName(String enumName) {
64        Resources res = getActivity().getResources();
65        CharSequence[] visualNames = res.getTextArray(R.array.pref_text_size_choices);
66        CharSequence[] enumNames = res.getTextArray(R.array.pref_text_size_values);
67
68        // Sanity check
69        if (visualNames.length != enumNames.length) {
70            return "";
71        }
72
73        int length = enumNames.length;
74        for (int i = 0; i < length; i++) {
75            if (enumNames[i].equals(enumName)) {
76                return visualNames[i];
77            }
78        }
79
80        return "";
81    }
82
83}