blob: f6a6c03c10c655be0ffe1df0c159afe5e75eda83 [file] [log] [blame]
Dirk Vogt74af2bf2016-05-31 11:21:46 +02001/*
2 * Copyright (C) 2012 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.fairphone.setupwizard.util;
18
19import android.accessibilityservice.AccessibilityServiceInfo;
20import android.app.ActivityManager;
21import android.content.ComponentName;
22import android.content.ContentResolver;
23import android.content.Context;
24import android.content.pm.ServiceInfo;
25import android.media.AudioManager;
26import android.media.Ringtone;
27import android.media.RingtoneManager;
28import android.os.Handler;
29import android.os.Message;
30import android.os.RemoteException;
31import android.os.ServiceManager;
32import android.os.UserManager;
33import android.provider.Settings;
34import android.speech.tts.TextToSpeech;
35import android.util.MathUtils;
36import android.view.IWindowManager;
37import android.view.MotionEvent;
38import android.view.accessibility.AccessibilityManager;
39import android.view.accessibility.IAccessibilityManager;
40
41import com.android.internal.R;
42
43import java.util.ArrayList;
44import java.util.Iterator;
45import java.util.List;
46
47public class EnableAccessibilityController {
48
49 private static final int SPEAK_WARNING_DELAY_MILLIS = 5000;
50 private static final int ENABLE_ACCESSIBILITY_DELAY_MILLIS = 12000;
51
52 public static final int MESSAGE_SPEAK_WARNING = 1;
53 public static final int MESSAGE_SPEAK_ENABLE_CANCELED = 2;
54 public static final int MESSAGE_ENABLE_ACCESSIBILITY = 3;
55
56 private final Handler mHandler = new Handler() {
57 @Override
58 public void handleMessage(Message message) {
59 switch (message.what) {
60 case MESSAGE_SPEAK_WARNING: {
61 String text = mContext.getString(R.string.continue_to_enable_accessibility);
62 mTts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
63 } break;
64 case MESSAGE_SPEAK_ENABLE_CANCELED: {
65 String text = mContext.getString(R.string.enable_accessibility_canceled);
66 mTts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
67 } break;
68 case MESSAGE_ENABLE_ACCESSIBILITY: {
69 enableAccessibility();
70 mTone.play();
71 mTts.speak(mContext.getString(R.string.accessibility_enabled),
72 TextToSpeech.QUEUE_FLUSH, null);
73 } break;
74 }
75 }
76 };
77
78 private final IWindowManager mWindowManager = IWindowManager.Stub.asInterface(
79 ServiceManager.getService("window"));
80
81 private final IAccessibilityManager mAccessibilityManager = IAccessibilityManager
82 .Stub.asInterface(ServiceManager.getService("accessibility"));
83
84
85 private final Context mContext;
86 private final UserManager mUserManager;
87 private final TextToSpeech mTts;
88 private final Ringtone mTone;
89
90 private final float mTouchSlop;
91
92 private boolean mDestroyed;
93
94 private float mFirstPointerDownX;
95 private float mFirstPointerDownY;
96 private float mSecondPointerDownX;
97 private float mSecondPointerDownY;
98
99 private static EnableAccessibilityController sInstance;
100
101 private EnableAccessibilityController(Context context) {
102 mContext = context;
103 mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
104 mTts = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
105 @Override
106 public void onInit(int status) {
107 if (mDestroyed) {
108 mTts.shutdown();
109 }
110 }
111 });
112 mTone = RingtoneManager.getRingtone(context, Settings.System.DEFAULT_NOTIFICATION_URI);
113 mTone.setStreamType(AudioManager.STREAM_MUSIC);
114 mTouchSlop = context.getResources().getDimensionPixelSize(
115 R.dimen.accessibility_touch_slop);
116 }
117
118 public static EnableAccessibilityController getInstance(Context context) {
119 if (sInstance == null) {
120 sInstance = new EnableAccessibilityController(context);
121 }
122 return sInstance;
123 }
124
125 public static boolean canEnableAccessibilityViaGesture(Context context) {
126 AccessibilityManager accessibilityManager = AccessibilityManager.getInstance(context);
127 // Accessibility is enabled and there is an enabled speaking
128 // accessibility service, then we have nothing to do.
129 if (accessibilityManager.isEnabled()
130 && !accessibilityManager.getEnabledAccessibilityServiceList(
131 AccessibilityServiceInfo.FEEDBACK_SPOKEN).isEmpty()) {
132 return false;
133 }
134
135 // If there is a speaking service
136 // installed we are good to go, otherwise there is nothing to do.
137 return getInstalledSpeakingAccessibilityServices(context).isEmpty();
138 }
139
140 private static List<AccessibilityServiceInfo> getInstalledSpeakingAccessibilityServices(
141 Context context) {
142 List<AccessibilityServiceInfo> services = new ArrayList<AccessibilityServiceInfo>();
143 services.addAll(AccessibilityManager.getInstance(context)
144 .getInstalledAccessibilityServiceList());
145 Iterator<AccessibilityServiceInfo> iterator = services.iterator();
146 while (iterator.hasNext()) {
147 AccessibilityServiceInfo service = iterator.next();
148 if ((service.feedbackType & AccessibilityServiceInfo.FEEDBACK_SPOKEN) == 0) {
149 iterator.remove();
150 }
151 }
152 return services;
153 }
154
155 public void onDestroy() {
156 mDestroyed = true;
157 }
158
159 public boolean onInterceptTouchEvent(MotionEvent event) {
160 if (event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN
161 && event.getPointerCount() == 2) {
162 mFirstPointerDownX = event.getX(0);
163 mFirstPointerDownY = event.getY(0);
164 mSecondPointerDownX = event.getX(1);
165 mSecondPointerDownY = event.getY(1);
166 mHandler.sendEmptyMessageDelayed(MESSAGE_SPEAK_WARNING,
167 SPEAK_WARNING_DELAY_MILLIS);
168 mHandler.sendEmptyMessageDelayed(MESSAGE_ENABLE_ACCESSIBILITY,
169 ENABLE_ACCESSIBILITY_DELAY_MILLIS);
170 return true;
171 }
172 return false;
173 }
174
175 public boolean onTouchEvent(MotionEvent event) {
176 final int pointerCount = event.getPointerCount();
177 final int action = event.getActionMasked();
178 switch (action) {
179 case MotionEvent.ACTION_POINTER_DOWN: {
180 if (pointerCount > 2) {
181 cancel();
182 }
183 }
184 break;
185 case MotionEvent.ACTION_MOVE: {
186 //We only care about a 2 fingered move
187 if (pointerCount < 2) {
188 cancel();
189 return false;
190 }
191 final float firstPointerMove = MathUtils.dist(event.getX(0),
192 event.getY(0), mFirstPointerDownX, mFirstPointerDownY);
193 if (Math.abs(firstPointerMove) > mTouchSlop) {
194 cancel();
195 }
196 final float secondPointerMove = MathUtils.dist(event.getX(1),
197 event.getY(1), mSecondPointerDownX, mSecondPointerDownY);
198 if (Math.abs(secondPointerMove) > mTouchSlop) {
199 cancel();
200 }
201 }
202 break;
203 case MotionEvent.ACTION_POINTER_UP:
204 case MotionEvent.ACTION_CANCEL: {
205 cancel();
206 }
207 break;
208 }
209 return true;
210 }
211
212 private void cancel() {
213 if (mHandler.hasMessages(MESSAGE_SPEAK_WARNING)) {
214 mHandler.removeMessages(MESSAGE_SPEAK_WARNING);
215 } else if (mHandler.hasMessages(MESSAGE_ENABLE_ACCESSIBILITY)) {
216 mHandler.sendEmptyMessage(MESSAGE_SPEAK_ENABLE_CANCELED);
217 }
218 mHandler.removeMessages(MESSAGE_ENABLE_ACCESSIBILITY);
219 }
220
221 private void enableAccessibility() {
222 List<AccessibilityServiceInfo> services = getInstalledSpeakingAccessibilityServices(
223 mContext);
224 if (services.isEmpty()) {
225 return;
226 }
227 boolean keyguardLocked = false;
228 try {
229 keyguardLocked = mWindowManager.isKeyguardLocked();
230 } catch (RemoteException re) {
231 /* ignore */
232 }
233
234 final boolean hasMoreThanOneUser = mUserManager.getUsers().size() > 1;
235
236 AccessibilityServiceInfo service = services.get(0);
237 boolean enableTouchExploration = (service.flags
238 & AccessibilityServiceInfo.FLAG_REQUEST_TOUCH_EXPLORATION_MODE) != 0;
239 // Try to find a service supporting explore by touch.
240 if (!enableTouchExploration) {
241 final int serviceCount = services.size();
242 for (int i = 1; i < serviceCount; i++) {
243 AccessibilityServiceInfo candidate = services.get(i);
244 if ((candidate.flags & AccessibilityServiceInfo
245 .FLAG_REQUEST_TOUCH_EXPLORATION_MODE) != 0) {
246 enableTouchExploration = true;
247 service = candidate;
248 break;
249 }
250 }
251 }
252
253 ServiceInfo serviceInfo = service.getResolveInfo().serviceInfo;
254 ComponentName componentName = new ComponentName(serviceInfo.packageName, serviceInfo.name);
255 if (!keyguardLocked || !hasMoreThanOneUser) {
256 final int userId = ActivityManager.getCurrentUser();
257 String enabledServiceString = componentName.flattenToString();
258 ContentResolver resolver = mContext.getContentResolver();
259 // Enable one speaking accessibility service.
260 Settings.Secure.putStringForUser(resolver,
261 Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
262 enabledServiceString, userId);
263 // Allow the services we just enabled to toggle touch exploration.
264 Settings.Secure.putStringForUser(resolver,
265 Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES,
266 enabledServiceString, userId);
267 // Enable touch exploration.
268 if (enableTouchExploration) {
269 Settings.Secure.putIntForUser(resolver, Settings.Secure.TOUCH_EXPLORATION_ENABLED,
270 1, userId);
271 }
272 // Enable accessibility script injection (AndroidVox) for web content.
273 Settings.Secure.putIntForUser(resolver, Settings.Secure.ACCESSIBILITY_SCRIPT_INJECTION,
274 1, userId);
275 // Turn on accessibility mode last.
276 Settings.Secure.putIntForUser(resolver, Settings.Secure.ACCESSIBILITY_ENABLED,
277 1, userId);
278 } else if (keyguardLocked) {
279 try {
280 mAccessibilityManager.temporaryEnableAccessibilityStateUntilKeyguardRemoved(
281 componentName, enableTouchExploration);
282 } catch (RemoteException re) {
283 /* ignore */
284 }
285 }
286 }
287}