PageRenderTime 50ms CodeModel.GetById 7ms app.highlight 37ms RepoModel.GetById 1ms app.codeStats 0ms

/talkback_preics/src/com/google/android/marvin/talkback/TalkBackPreferencesActivity.java

http://eyes-free.googlecode.com/
Java | 224 lines | 142 code | 27 blank | 55 comment | 8 complexity | 796cef0123f466c8bd36d7441d8a4304 MD5 | raw file
  1/*
  2 * Copyright 2010 Google Inc.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5 * use this file except in compliance with the License. You may obtain a copy of
  6 * 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, WITHOUT
 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 13 * License for the specific language governing permissions and limitations under
 14 * the License.
 15 */
 16
 17package com.google.android.marvin.talkback;
 18
 19import android.app.AlertDialog;
 20import android.content.ActivityNotFoundException;
 21import android.content.DialogInterface;
 22import android.content.Intent;
 23import android.os.Build;
 24import android.os.Bundle;
 25import android.os.Handler;
 26import android.preference.CheckBoxPreference;
 27import android.preference.ListPreference;
 28import android.preference.Preference;
 29import android.preference.PreferenceActivity;
 30import android.preference.PreferenceScreen;
 31import android.preference.Preference.OnPreferenceChangeListener;
 32
 33/**
 34 * Activity used to set TalkBack's service preferences. This activity is loaded
 35 * when the TalkBackService is first connected to and allows the user to select
 36 * a font zoom size.
 37 * 
 38 * @author dmazzoni@google.com (Dominic Mazzoni)
 39 */
 40public class TalkBackPreferencesActivity extends PreferenceActivity {
 41
 42    private static final String ACTION_ACCESSIBILITY_SETTINGS = "android.settings.ACCESSIBILITY_SETTINGS";
 43
 44    private ListPreference mScreenOff = null;
 45
 46    private String[] mScreenOffStrings = null;
 47
 48    private ListPreference mRinger = null;
 49
 50    private String[] mRingerStrings = null;
 51
 52    private CheckBoxPreference mCallerId = null;
 53
 54    private CheckBoxPreference mTtsExtended = null;
 55
 56    private CheckBoxPreference mProximity = null;
 57
 58    private PreferenceScreen mManagePluginsScreen = null;
 59
 60    private Handler mHandler = null;
 61
 62    /**
 63     * Loads the preferences from the XML preference definition and defines an
 64     * onPreferenceChangeListener for the font zoom size that restarts the
 65     * TalkBack service.
 66     */
 67    @Override
 68    public void onCreate(Bundle icicle) {
 69        super.onCreate(icicle);
 70        if (Build.VERSION.SDK_INT < 8) {
 71            addPreferencesFromResource(R.xml.preferences_prefroyo);
 72        } else {
 73            addPreferencesFromResource(R.xml.preferences);
 74        }
 75
 76        mHandler = new Handler();
 77
 78        int valueIndex;
 79
 80        mScreenOff = (ListPreference) findPreference(getString(R.string.pref_speak_screenoff_key));
 81        mScreenOffStrings = getResources().getStringArray(R.array.pref_speak_screenoff_entries);
 82        valueIndex = mScreenOff.findIndexOfValue(mScreenOff.getValue());
 83        if (valueIndex >= 0) {
 84            mScreenOff.setSummary(mScreenOffStrings[valueIndex]);
 85        }
 86        mScreenOff.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
 87            public boolean onPreferenceChange(Preference preference, Object newValue) {
 88                int valueIndex = mScreenOff.findIndexOfValue((String) newValue);
 89                mScreenOff.setSummary(mScreenOffStrings[valueIndex]);
 90                deferReloadPreferences();
 91                return true;
 92            }
 93        });
 94
 95        mRinger = (ListPreference) findPreference(getString(R.string.pref_speak_ringer_key));
 96        mRingerStrings = getResources().getStringArray(R.array.pref_speak_ringer_entries);
 97        valueIndex = mRinger.findIndexOfValue(mRinger.getValue());
 98        if (valueIndex >= 0) {
 99            mRinger.setSummary(mRingerStrings[valueIndex]);
100        }
101        mRinger.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
102            public boolean onPreferenceChange(Preference preference, Object newValue) {
103                int valueIndex = mRinger.findIndexOfValue((String) newValue);
104                mRinger.setSummary(mRingerStrings[valueIndex]);
105                deferReloadPreferences();
106                return true;
107            }
108        });
109
110        mCallerId = (CheckBoxPreference) findPreference(getString(R.string.pref_caller_id_key));
111        mCallerId.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
112            public boolean onPreferenceChange(Preference preference, Object newValue) {
113                deferReloadPreferences();
114                return true;
115            }
116        });
117
118        // TTS Extended only exists pre-Froyo.
119        if (Build.VERSION.SDK_INT < 8) {
120            mTtsExtended = (CheckBoxPreference) findPreference(getString(R.string.pref_tts_extended_key));
121            mTtsExtended.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
122                public boolean onPreferenceChange(Preference preference, Object newValue) {
123                    deferReloadPreferences();
124                    return true;
125                }
126            });
127        }
128
129        mProximity = (CheckBoxPreference) findPreference(getString(R.string.pref_proximity_key));
130        mProximity.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
131            public boolean onPreferenceChange(Preference preference, Object newValue) {
132                deferReloadPreferences();
133                return true;
134            }
135        });
136
137        mManagePluginsScreen = (PreferenceScreen) findPreference(getString(R.string.pref_manage_plugins_key));
138    }
139
140    /**
141     * Checks if the TalkBack service is currently running and displays an
142     * AlertDialog directing the user to enable TalkBack from the accessibility
143     * settings menu.
144     */
145    @Override
146    public void onResume() {
147        super.onRestart();
148        if (!TalkBackService.isServiceInitialized()) {
149            createInactiveServiceAlert().show();
150        }
151        setManagePluginsPreferenceState();
152    }
153
154    /**
155     * Sets the state of the manage plug-ins preference.
156     */
157    private void setManagePluginsPreferenceState() {
158        boolean hasInstalledPlugins = !PluginManager.getPlugins(getPackageManager()).isEmpty();
159        mManagePluginsScreen.setEnabled(hasInstalledPlugins);
160        if (hasInstalledPlugins) {
161            mManagePluginsScreen.setIntent(new Intent(this, PluginPreferencesActivity.class));
162        }
163    }
164
165    /**
166     * Tell the service to reload preferences, but defer until the next event
167     * loop, because onPreferenceChange gets called before the preference
168     * changes, not after.
169     */
170    private void deferReloadPreferences() {
171        mHandler.post(new Runnable() {
172            public void run() {
173                if (TalkBackService.isServiceInitialized()) {
174                    TalkBackService.getInstance().reloadPreferences();
175                }
176            }
177        });
178    }
179
180    /**
181     * Constructs an AlertDialog that displays a warning when TalkBack is
182     * disabled. Clicking the "Yes" button launches the accessibility settings
183     * menu to enable the Talkback service.
184     * 
185     * @return an AlertDialog containing a warning message about TalkBack's
186     *         disabled state
187     */
188    private AlertDialog createInactiveServiceAlert() {
189        return new AlertDialog.Builder(this).setTitle(
190                getString(R.string.title_talkback_inactive_alert)).setMessage(
191                getString(R.string.message_talkback_inactive_alert)).setCancelable(false)
192                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
193                    public void onClick(DialogInterface dialog, int id) {
194                        /*
195                         * There is no guarantee that an accessibility settings
196                         * menu exists, so if the ACTION_ACCESSIBILITY_SETTINGS
197                         * intent doesn't match an activity, simply start the
198                         * main settings activity.
199                         */
200                        Intent launchSettings = new Intent(ACTION_ACCESSIBILITY_SETTINGS);
201                        try {
202                            startActivity(launchSettings);
203                        } catch (ActivityNotFoundException ae) {
204                            showNoAccessibilityWarning();
205                        }
206                        dialog.dismiss();
207                    }
208                }).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
209                    public void onClick(DialogInterface dialog, int id) {
210                        dialog.cancel();
211                    }
212                }).create();
213    }
214
215    private void showNoAccessibilityWarning() {
216        new AlertDialog.Builder(this).setTitle(getString(R.string.title_no_accessibility_alert))
217                .setMessage(getString(R.string.message_no_accessibility_alert)).setPositiveButton(
218                        android.R.string.ok, new DialogInterface.OnClickListener() {
219                            public void onClick(DialogInterface dialog, int id) {
220                                TalkBackPreferencesActivity.this.finish();
221                            }
222                        }).create().show();
223    }
224}