PageRenderTime 30ms CodeModel.GetById 11ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/ime/latinime/src/com/googlecode/eyesfree/inputmethod/latin/AccessibilityUtils.java

http://eyes-free.googlecode.com/
Java | 198 lines | 131 code | 21 blank | 46 comment | 25 complexity | aceb7dcfef8b88e15c37ad4342c177e5 MD5 | raw file
  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.googlecode.eyesfree.inputmethod.latin;
 18
 19import android.content.Context;
 20import android.content.res.Resources;
 21import android.inputmethodservice.Keyboard;
 22import android.inputmethodservice.Keyboard.Key;
 23import android.os.SystemClock;
 24import android.provider.Settings;
 25import android.view.accessibility.AccessibilityEvent;
 26import android.view.accessibility.AccessibilityManager;
 27
 28import com.googlecode.eyesfree.inputmethod.KeyCodeDescriptionMapper;
 29
 30/**
 31 * Utility functions for accessibility support.
 32 */
 33public class AccessibilityUtils {
 34    private final Context mContext;
 35    private final AccessibilityManager mAccessibilityManager;
 36    private final KeyCodeDescriptionMapper mKeyCodeMapper;
 37
 38    public AccessibilityUtils(Context context) {
 39        mContext = context;
 40        mAccessibilityManager = (AccessibilityManager) context
 41                .getSystemService(Context.ACCESSIBILITY_SERVICE);
 42        mKeyCodeMapper = KeyCodeDescriptionMapper.getInstance(context);
 43    }
 44
 45    /**
 46     * Returns true if accessibility is enabled.
 47     *
 48     * @return true is accessibility is enabled.
 49     */
 50    public boolean isAccessibilityEnabled() {
 51        return mAccessibilityManager.isEnabled();
 52    }
 53
 54    /**
 55     * Speaks a key's action after it has been released. Does not speak letter
 56     * keys since typed keys are already spoken aloud by TalkBack.
 57     *
 58     * @param key The primary code of the released key.
 59     * @param switcher The input method's {@link KeyboardSwitcher}.
 60     */
 61    public void onRelease(Key key, KeyboardSwitcher switcher) {
 62        if (!mAccessibilityManager.isEnabled()) {
 63            return;
 64        }
 65        final int primaryCode = key.codes[0];
 66        int resId = -1;
 67        switch (primaryCode) {
 68            case Keyboard.KEYCODE_SHIFT: {
 69                if (switcher.isAlphabetMode()) {
 70                    if (switcher.isShiftedOrShiftLocked()) {
 71                        if (switcher.isShiftLocked()) {
 72                            resId = R.string.spoken_description_shift_locked;
 73                        } else {
 74                            resId = R.string.spoken_description_shift_on;
 75                        }
 76                    } else {
 77                        resId = R.string.spoken_description_shift_off;
 78                    }
 79                } else {
 80                    if (switcher.isShiftedOrShiftLocked()) {
 81                        resId = R.string.spoken_description_alt_on;
 82                    } else {
 83                        resId = R.string.spoken_description_alt_off;
 84                    }
 85                }
 86                break;
 87            }
 88            case Keyboard.KEYCODE_MODE_CHANGE: {
 89                if (switcher.isAlphabetMode()) {
 90                    resId = R.string.spoken_description_symbols_off;
 91                } else {
 92                    resId = R.string.spoken_description_symbols_on;
 93                }
 94                break;
 95            }
 96            case LatinKeyboardView.KEYCODE_BACK:
 97                resId = R.string.spoken_description_back;
 98                break;
 99            case LatinKeyboardView.KEYCODE_HOME:
100                resId = R.string.spoken_description_home;
101                break;
102            case LatinKeyboardView.KEYCODE_SEARCH:
103                resId = R.string.spoken_description_search;
104                break;
105            case LatinKeyboardView.KEYCODE_MENU:
106                resId = R.string.spoken_description_menu;
107                break;
108            case LatinKeyboardView.KEYCODE_CALL:
109                resId = R.string.spoken_description_call;
110                break;
111            case LatinKeyboardView.KEYCODE_ENDCALL:
112                resId = R.string.spoken_description_end_call;
113                break;
114        }
115
116        if (resId >= 0) {
117            speakDescription(mContext.getResources().getText(resId));
118        }
119    }
120
121    /**
122     * Speak key description for accessibility. If a key has an explicit
123     * description defined in keycodes.xml, that will be used. Otherwise, if the
124     * key is a Unicode character, then its character will be used.
125     *
126     * @param res The resources for the current context.
127     * @param key The primary code of the pressed key.
128     * @param switcher The input method's {@link KeyboardSwitcher}.
129     */
130    public void onPress(Resources res, KeyboardSwitcher switcher, Key key) {
131        if (!mAccessibilityManager.isEnabled()) {
132            return;
133        }
134
135        final CharSequence description = mKeyCodeMapper.getDescriptionForKey(res, switcher, key);
136
137        if (description != null) {
138            speakDescription(description);
139        }
140    }
141
142    /**
143     * Sends a character sequence to be read aloud.
144     *
145     * @param description The {@link CharSequence} to be read aloud.
146     */
147    public void speakDescription(CharSequence description) {
148        if (!mAccessibilityManager.isEnabled()) {
149            return;
150        }
151
152        // TODO Contact Loquendo so we can remove this workaround.
153        if (Character.isLetterOrDigit(description.charAt(0))) {
154            description = description + ".";
155        }
156
157        // TODO We need to add an AccessibilityEvent type for IMEs.
158        AccessibilityEvent event = AccessibilityEvent
159                .obtain(AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED);
160        event.setPackageName(mContext.getPackageName());
161        event.setClassName(getClass().getName());
162        event.setEventTime(SystemClock.uptimeMillis());
163        event.setBeforeText("");
164        event.setAddedCount(description.length());
165        event.setRemovedCount(0);
166        event.setFromIndex(0);
167        event.getText().add(description);
168
169        mAccessibilityManager.sendAccessibilityEvent(event);
170    }
171
172    public static boolean isAccessibilityEnabled(Context context) {
173        final int accessibilityEnabled = Settings.Secure.getInt(context.getContentResolver(),
174                Settings.Secure.ACCESSIBILITY_ENABLED, 0);
175
176        return accessibilityEnabled == 1;
177    }
178
179    public static boolean isInputMethodEnabled(Context context, Class<?> imeClass) {
180        final String targetImePackage = imeClass.getPackage().getName();
181        final String targetImeClass = imeClass.getSimpleName();
182        final String enabledImeIds = Settings.Secure.getString(context.getContentResolver(),
183                Settings.Secure.ENABLED_INPUT_METHODS);
184
185        return enabledImeIds != null && enabledImeIds.contains(targetImePackage)
186                && enabledImeIds.contains(targetImeClass);
187    }
188
189    public static boolean isInputMethodDefault(Context context, Class<?> imeClass) {
190        final String targetImePackage = imeClass.getPackage().getName();
191        final String targetImeClass = imeClass.getSimpleName();
192        final String defaultImeId = Settings.Secure.getString(context.getContentResolver(),
193                Settings.Secure.DEFAULT_INPUT_METHOD);
194
195        return defaultImeId != null && defaultImeId.contains(targetImePackage)
196                && defaultImeId.contains(targetImeClass);
197    }
198}