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.util.Log; 27 import android.view.MotionEvent; 28 import android.view.View; 29 import android.view.ViewGroup; 30 import android.view.ViewParent; 31 import android.view.accessibility.AccessibilityEvent; 32 import android.view.accessibility.AccessibilityManager; 33 import android.view.inputmethod.EditorInfo; 34 35 import com.android.inputmethod.compat.SettingsSecureCompatUtils; 36 import com.android.inputmethod.latin.InputTypeUtils; 37 import com.android.inputmethod.latin.R; 38 39 public final class AccessibilityUtils { 40 private static final String TAG = AccessibilityUtils.class.getSimpleName(); 41 private static final String CLASS = AccessibilityUtils.class.getClass().getName(); 42 private static final String PACKAGE = 43 AccessibilityUtils.class.getClass().getPackage().getName(); 44 45 private static final AccessibilityUtils sInstance = new AccessibilityUtils(); 46 47 private Context mContext; 48 private AccessibilityManager mAccessibilityManager; 49 private AudioManager mAudioManager; 50 51 /* 52 * Setting this constant to {@code false} will disable all keyboard 53 * accessibility code, regardless of whether Accessibility is turned on in 54 * the system settings. It should ONLY be used in the event of an emergency. 55 */ 56 private static final boolean ENABLE_ACCESSIBILITY = true; 57 init(final InputMethodService inputMethod)58 public static void init(final InputMethodService inputMethod) { 59 if (!ENABLE_ACCESSIBILITY) return; 60 61 // These only need to be initialized if the kill switch is off. 62 sInstance.initInternal(inputMethod); 63 KeyCodeDescriptionMapper.init(); 64 AccessibleKeyboardViewProxy.init(inputMethod); 65 } 66 getInstance()67 public static AccessibilityUtils getInstance() { 68 return sInstance; 69 } 70 AccessibilityUtils()71 private AccessibilityUtils() { 72 // This class is not publicly instantiable. 73 } 74 initInternal(final Context context)75 private void initInternal(final Context context) { 76 mContext = context; 77 mAccessibilityManager = 78 (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE); 79 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); 80 } 81 82 /** 83 * Returns {@code true} if accessibility is enabled. Currently, this means 84 * that the kill switch is off and system accessibility is turned on. 85 * 86 * @return {@code true} if accessibility is enabled. 87 */ isAccessibilityEnabled()88 public boolean isAccessibilityEnabled() { 89 return ENABLE_ACCESSIBILITY && mAccessibilityManager.isEnabled(); 90 } 91 92 /** 93 * Returns {@code true} if touch exploration is enabled. Currently, this 94 * means that the kill switch is off, the device supports touch exploration, 95 * and system accessibility is turned on. 96 * 97 * @return {@code true} if touch exploration is enabled. 98 */ isTouchExplorationEnabled()99 public boolean isTouchExplorationEnabled() { 100 return isAccessibilityEnabled() && mAccessibilityManager.isTouchExplorationEnabled(); 101 } 102 103 /** 104 * Returns {@true} if the provided event is a touch exploration (e.g. hover) 105 * event. This is used to determine whether the event should be processed by 106 * the touch exploration code within the keyboard. 107 * 108 * @param event The event to check. 109 * @return {@true} is the event is a touch exploration event 110 */ isTouchExplorationEvent(final MotionEvent event)111 public boolean isTouchExplorationEvent(final MotionEvent event) { 112 final int action = event.getAction(); 113 return action == MotionEvent.ACTION_HOVER_ENTER 114 || action == MotionEvent.ACTION_HOVER_EXIT 115 || action == MotionEvent.ACTION_HOVER_MOVE; 116 } 117 118 /** 119 * Returns whether the device should obscure typed password characters. 120 * Typically this means speaking "dot" in place of non-control characters. 121 * 122 * @return {@code true} if the device should obscure password characters. 123 */ 124 @SuppressWarnings("deprecation") shouldObscureInput(final EditorInfo editorInfo)125 public boolean shouldObscureInput(final EditorInfo editorInfo) { 126 if (editorInfo == null) return false; 127 128 // The user can optionally force speaking passwords. 129 if (SettingsSecureCompatUtils.ACCESSIBILITY_SPEAK_PASSWORD != null) { 130 final boolean speakPassword = Settings.Secure.getInt(mContext.getContentResolver(), 131 SettingsSecureCompatUtils.ACCESSIBILITY_SPEAK_PASSWORD, 0) != 0; 132 if (speakPassword) return false; 133 } 134 135 // Always speak if the user is listening through headphones. 136 if (mAudioManager.isWiredHeadsetOn() || mAudioManager.isBluetoothA2dpOn()) { 137 return false; 138 } 139 140 // Don't speak if the IME is connected to a password field. 141 return InputTypeUtils.isPasswordInputType(editorInfo.inputType); 142 } 143 144 /** 145 * Sends the specified text to the {@link AccessibilityManager} to be 146 * spoken. 147 * 148 * @param view The source view. 149 * @param text The text to speak. 150 */ announceForAccessibility(final View view, final CharSequence text)151 public void announceForAccessibility(final View view, final CharSequence text) { 152 if (!mAccessibilityManager.isEnabled()) { 153 Log.e(TAG, "Attempted to speak when accessibility was disabled!"); 154 return; 155 } 156 157 // The following is a hack to avoid using the heavy-weight TextToSpeech 158 // class. Instead, we're just forcing a fake AccessibilityEvent into 159 // the screen reader to make it speak. 160 final AccessibilityEvent event = AccessibilityEvent.obtain(); 161 162 event.setPackageName(PACKAGE); 163 event.setClassName(CLASS); 164 event.setEventTime(SystemClock.uptimeMillis()); 165 event.setEnabled(true); 166 event.getText().add(text); 167 168 // Platforms starting at SDK version 16 (Build.VERSION_CODES.JELLY_BEAN) should use 169 // announce events. 170 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 171 event.setEventType(AccessibilityEventCompat.TYPE_ANNOUNCEMENT); 172 } else { 173 event.setEventType(AccessibilityEvent.TYPE_VIEW_FOCUSED); 174 } 175 176 final ViewParent viewParent = view.getParent(); 177 if ((viewParent == null) || !(viewParent instanceof ViewGroup)) { 178 Log.e(TAG, "Failed to obtain ViewParent in announceForAccessibility"); 179 return; 180 } 181 182 viewParent.requestSendAccessibilityEvent(view, event); 183 } 184 185 /** 186 * Handles speaking the "connect a headset to hear passwords" notification 187 * when connecting to a password field. 188 * 189 * @param view The source view. 190 * @param editorInfo The input connection's editor info attribute. 191 * @param restarting Whether the connection is being restarted. 192 */ onStartInputViewInternal(final View view, final EditorInfo editorInfo, final boolean restarting)193 public void onStartInputViewInternal(final View view, final EditorInfo editorInfo, 194 final boolean restarting) { 195 if (shouldObscureInput(editorInfo)) { 196 final CharSequence text = mContext.getText(R.string.spoken_use_headphones); 197 announceForAccessibility(view, text); 198 } 199 } 200 201 /** 202 * Sends the specified {@link AccessibilityEvent} if accessibility is 203 * enabled. No operation if accessibility is disabled. 204 * 205 * @param event The event to send. 206 */ requestSendAccessibilityEvent(final AccessibilityEvent event)207 public void requestSendAccessibilityEvent(final AccessibilityEvent event) { 208 if (mAccessibilityManager.isEnabled()) { 209 mAccessibilityManager.sendAccessibilityEvent(event); 210 } 211 } 212 } 213