Home | History | Annotate | Download | only in accessibility
      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 
     17 package com.android.inputmethod.accessibility;
     18 
     19 import android.content.Context;
     20 import android.inputmethodservice.InputMethodService;
     21 import android.media.AudioManager;
     22 import android.os.Build;
     23 import android.os.SystemClock;
     24 import android.provider.Settings;
     25 import android.support.v4.view.accessibility.AccessibilityEventCompat;
     26 import android.text.TextUtils;
     27 import android.util.Log;
     28 import android.view.MotionEvent;
     29 import android.view.View;
     30 import android.view.ViewGroup;
     31 import android.view.ViewParent;
     32 import android.view.accessibility.AccessibilityEvent;
     33 import android.view.accessibility.AccessibilityManager;
     34 import android.view.inputmethod.EditorInfo;
     35 
     36 import com.android.inputmethod.compat.SettingsSecureCompatUtils;
     37 import com.android.inputmethod.latin.R;
     38 import com.android.inputmethod.latin.SuggestedWords;
     39 import com.android.inputmethod.latin.utils.InputTypeUtils;
     40 
     41 public final class AccessibilityUtils {
     42     private static final String TAG = AccessibilityUtils.class.getSimpleName();
     43     private static final String CLASS = AccessibilityUtils.class.getClass().getName();
     44     private static final String PACKAGE =
     45             AccessibilityUtils.class.getClass().getPackage().getName();
     46 
     47     private static final AccessibilityUtils sInstance = new AccessibilityUtils();
     48 
     49     private Context mContext;
     50     private AccessibilityManager mAccessibilityManager;
     51     private AudioManager mAudioManager;
     52 
     53     /** The most recent auto-correction. */
     54     private String mAutoCorrectionWord;
     55 
     56     /** The most recent typed word for auto-correction. */
     57     private String mTypedWord;
     58 
     59     /*
     60      * Setting this constant to {@code false} will disable all keyboard
     61      * accessibility code, regardless of whether Accessibility is turned on in
     62      * the system settings. It should ONLY be used in the event of an emergency.
     63      */
     64     private static final boolean ENABLE_ACCESSIBILITY = true;
     65 
     66     public static void init(final InputMethodService inputMethod) {
     67         if (!ENABLE_ACCESSIBILITY) return;
     68 
     69         // These only need to be initialized if the kill switch is off.
     70         sInstance.initInternal(inputMethod);
     71         KeyCodeDescriptionMapper.init();
     72         AccessibleKeyboardViewProxy.init(inputMethod);
     73     }
     74 
     75     public static AccessibilityUtils getInstance() {
     76         return sInstance;
     77     }
     78 
     79     private AccessibilityUtils() {
     80         // This class is not publicly instantiable.
     81     }
     82 
     83     private void initInternal(final Context context) {
     84         mContext = context;
     85         mAccessibilityManager =
     86                 (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
     87         mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
     88     }
     89 
     90     /**
     91      * Returns {@code true} if accessibility is enabled. Currently, this means
     92      * that the kill switch is off and system accessibility is turned on.
     93      *
     94      * @return {@code true} if accessibility is enabled.
     95      */
     96     public boolean isAccessibilityEnabled() {
     97         return ENABLE_ACCESSIBILITY && mAccessibilityManager.isEnabled();
     98     }
     99 
    100     /**
    101      * Returns {@code true} if touch exploration is enabled. Currently, this
    102      * means that the kill switch is off, the device supports touch exploration,
    103      * and system accessibility is turned on.
    104      *
    105      * @return {@code true} if touch exploration is enabled.
    106      */
    107     public boolean isTouchExplorationEnabled() {
    108         return isAccessibilityEnabled() && mAccessibilityManager.isTouchExplorationEnabled();
    109     }
    110 
    111     /**
    112      * Returns {@true} if the provided event is a touch exploration (e.g. hover)
    113      * event. This is used to determine whether the event should be processed by
    114      * the touch exploration code within the keyboard.
    115      *
    116      * @param event The event to check.
    117      * @return {@true} is the event is a touch exploration event
    118      */
    119     public boolean isTouchExplorationEvent(final MotionEvent event) {
    120         final int action = event.getAction();
    121         return action == MotionEvent.ACTION_HOVER_ENTER
    122                 || action == MotionEvent.ACTION_HOVER_EXIT
    123                 || action == MotionEvent.ACTION_HOVER_MOVE;
    124     }
    125 
    126     /**
    127      * Returns whether the device should obscure typed password characters.
    128      * Typically this means speaking "dot" in place of non-control characters.
    129      *
    130      * @return {@code true} if the device should obscure password characters.
    131      */
    132     @SuppressWarnings("deprecation")
    133     public boolean shouldObscureInput(final EditorInfo editorInfo) {
    134         if (editorInfo == null) return false;
    135 
    136         // The user can optionally force speaking passwords.
    137         if (SettingsSecureCompatUtils.ACCESSIBILITY_SPEAK_PASSWORD != null) {
    138             final boolean speakPassword = Settings.Secure.getInt(mContext.getContentResolver(),
    139                     SettingsSecureCompatUtils.ACCESSIBILITY_SPEAK_PASSWORD, 0) != 0;
    140             if (speakPassword) return false;
    141         }
    142 
    143         // Always speak if the user is listening through headphones.
    144         if (mAudioManager.isWiredHeadsetOn() || mAudioManager.isBluetoothA2dpOn()) {
    145             return false;
    146         }
    147 
    148         // Don't speak if the IME is connected to a password field.
    149         return InputTypeUtils.isPasswordInputType(editorInfo.inputType);
    150     }
    151 
    152     /**
    153      * Sets the current auto-correction word and typed word. These may be used
    154      * to provide the user with a spoken description of what auto-correction
    155      * will occur when a key is typed.
    156      *
    157      * @param suggestedWords the list of suggested auto-correction words
    158      * @param typedWord the currently typed word
    159      */
    160     public void setAutoCorrection(final SuggestedWords suggestedWords, final String typedWord) {
    161         if (suggestedWords != null && suggestedWords.mWillAutoCorrect) {
    162             mAutoCorrectionWord = suggestedWords.getWord(SuggestedWords.INDEX_OF_AUTO_CORRECTION);
    163             mTypedWord = typedWord;
    164         } else {
    165             mAutoCorrectionWord = null;
    166             mTypedWord = null;
    167         }
    168     }
    169 
    170     /**
    171      * Obtains a description for an auto-correction key, taking into account the
    172      * currently typed word and auto-correction.
    173      *
    174      * @param keyCodeDescription spoken description of the key that will insert
    175      *            an auto-correction
    176      * @param shouldObscure whether the key should be obscured
    177      * @return a description including a description of the auto-correction, if
    178      *         needed
    179      */
    180     public String getAutoCorrectionDescription(
    181             final String keyCodeDescription, final boolean shouldObscure) {
    182         if (!TextUtils.isEmpty(mAutoCorrectionWord)) {
    183             if (!TextUtils.equals(mAutoCorrectionWord, mTypedWord)) {
    184                 if (shouldObscure) {
    185                     // This should never happen, but just in case...
    186                     return mContext.getString(R.string.spoken_auto_correct_obscured,
    187                             keyCodeDescription);
    188                 }
    189                 return mContext.getString(R.string.spoken_auto_correct, keyCodeDescription,
    190                         mTypedWord, mAutoCorrectionWord);
    191             }
    192         }
    193 
    194         return keyCodeDescription;
    195     }
    196 
    197     /**
    198      * Sends the specified text to the {@link AccessibilityManager} to be
    199      * spoken.
    200      *
    201      * @param view The source view.
    202      * @param text The text to speak.
    203      */
    204     public void announceForAccessibility(final View view, final CharSequence text) {
    205         if (!mAccessibilityManager.isEnabled()) {
    206             Log.e(TAG, "Attempted to speak when accessibility was disabled!");
    207             return;
    208         }
    209 
    210         // The following is a hack to avoid using the heavy-weight TextToSpeech
    211         // class. Instead, we're just forcing a fake AccessibilityEvent into
    212         // the screen reader to make it speak.
    213         final AccessibilityEvent event = AccessibilityEvent.obtain();
    214 
    215         event.setPackageName(PACKAGE);
    216         event.setClassName(CLASS);
    217         event.setEventTime(SystemClock.uptimeMillis());
    218         event.setEnabled(true);
    219         event.getText().add(text);
    220 
    221         // Platforms starting at SDK version 16 (Build.VERSION_CODES.JELLY_BEAN) should use
    222         // announce events.
    223         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
    224             event.setEventType(AccessibilityEventCompat.TYPE_ANNOUNCEMENT);
    225         } else {
    226             event.setEventType(AccessibilityEvent.TYPE_VIEW_FOCUSED);
    227         }
    228 
    229         final ViewParent viewParent = view.getParent();
    230         if ((viewParent == null) || !(viewParent instanceof ViewGroup)) {
    231             Log.e(TAG, "Failed to obtain ViewParent in announceForAccessibility");
    232             return;
    233         }
    234 
    235         viewParent.requestSendAccessibilityEvent(view, event);
    236     }
    237 
    238     /**
    239      * Handles speaking the "connect a headset to hear passwords" notification
    240      * when connecting to a password field.
    241      *
    242      * @param view The source view.
    243      * @param editorInfo The input connection's editor info attribute.
    244      * @param restarting Whether the connection is being restarted.
    245      */
    246     public void onStartInputViewInternal(final View view, final EditorInfo editorInfo,
    247             final boolean restarting) {
    248         if (shouldObscureInput(editorInfo)) {
    249             final CharSequence text = mContext.getText(R.string.spoken_use_headphones);
    250             announceForAccessibility(view, text);
    251         }
    252     }
    253 
    254     /**
    255      * Sends the specified {@link AccessibilityEvent} if accessibility is
    256      * enabled. No operation if accessibility is disabled.
    257      *
    258      * @param event The event to send.
    259      */
    260     public void requestSendAccessibilityEvent(final AccessibilityEvent event) {
    261         if (mAccessibilityManager.isEnabled()) {
    262             mAccessibilityManager.sendAccessibilityEvent(event);
    263         }
    264     }
    265 }
    266