svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 2009, 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 | |
Jeff Brown | 6e6cd7a | 2011-03-30 03:27:08 -0700 | [diff] [blame] | 17 | package com.android.server.accessibility; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 18 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 19 | import static android.accessibilityservice.AccessibilityServiceInfo.DEFAULT; |
Svetoslav Ganov | e4abc51 | 2012-05-09 11:02:38 -0700 | [diff] [blame] | 20 | import static android.accessibilityservice.AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 21 | |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 22 | import android.Manifest; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 23 | import android.accessibilityservice.AccessibilityService; |
| 24 | import android.accessibilityservice.AccessibilityServiceInfo; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 25 | import android.accessibilityservice.IAccessibilityServiceClient; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 26 | import android.accessibilityservice.IAccessibilityServiceConnection; |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 27 | import android.app.AlertDialog; |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 28 | import android.app.PendingIntent; |
Svetoslav Ganov | 5c89f44 | 2012-05-15 13:28:09 -0700 | [diff] [blame] | 29 | import android.app.StatusBarManager; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 30 | import android.content.BroadcastReceiver; |
| 31 | import android.content.ComponentName; |
| 32 | import android.content.ContentResolver; |
| 33 | import android.content.Context; |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 34 | import android.content.DialogInterface; |
| 35 | import android.content.DialogInterface.OnClickListener; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 36 | import android.content.Intent; |
| 37 | import android.content.IntentFilter; |
| 38 | import android.content.ServiceConnection; |
| 39 | import android.content.pm.PackageManager; |
| 40 | import android.content.pm.ResolveInfo; |
Svetoslav Ganov | 53e184d | 2012-05-16 15:57:10 -0700 | [diff] [blame] | 41 | import android.content.pm.ServiceInfo; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 42 | import android.database.ContentObserver; |
Svetoslav Ganov | 7961be7 | 2011-06-21 12:31:56 -0700 | [diff] [blame] | 43 | import android.graphics.Rect; |
Svetoslav Ganov | 005b83b | 2012-04-16 18:17:17 -0700 | [diff] [blame] | 44 | import android.hardware.input.InputManager; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 45 | import android.net.Uri; |
| 46 | import android.os.Binder; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 47 | import android.os.Build; |
Svetoslav Ganov | aa780c1 | 2012-04-19 23:01:39 -0700 | [diff] [blame] | 48 | import android.os.Bundle; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 49 | import android.os.Handler; |
| 50 | import android.os.IBinder; |
Svetoslav Ganov | 1cf70bb | 2012-08-06 10:53:34 -0700 | [diff] [blame] | 51 | import android.os.Looper; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 52 | import android.os.Message; |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 53 | import android.os.Process; |
| 54 | import android.os.RemoteCallbackList; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 55 | import android.os.RemoteException; |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 56 | import android.os.ServiceManager; |
Svetoslav Ganov | 005b83b | 2012-04-16 18:17:17 -0700 | [diff] [blame] | 57 | import android.os.SystemClock; |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 58 | import android.os.UserHandle; |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 59 | import android.os.UserManager; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 60 | import android.provider.Settings; |
| 61 | import android.text.TextUtils; |
| 62 | import android.text.TextUtils.SimpleStringSplitter; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 63 | import android.util.Slog; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 64 | import android.util.SparseArray; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 65 | import android.view.IWindow; |
Svetoslav Ganov | c9c9a48 | 2012-07-16 08:46:07 -0700 | [diff] [blame] | 66 | import android.view.IWindowManager; |
Svetoslav Ganov | 005b83b | 2012-04-16 18:17:17 -0700 | [diff] [blame] | 67 | import android.view.InputDevice; |
| 68 | import android.view.KeyCharacterMap; |
| 69 | import android.view.KeyEvent; |
Svetoslav Ganov | 1cf70bb | 2012-08-06 10:53:34 -0700 | [diff] [blame] | 70 | import android.view.WindowInfo; |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 71 | import android.view.WindowManager; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 72 | import android.view.accessibility.AccessibilityEvent; |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 73 | import android.view.accessibility.AccessibilityInteractionClient; |
Svetoslav Ganov | 00aabf7 | 2011-07-21 11:35:03 -0700 | [diff] [blame] | 74 | import android.view.accessibility.AccessibilityManager; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 75 | import android.view.accessibility.AccessibilityNodeInfo; |
| 76 | import android.view.accessibility.IAccessibilityInteractionConnection; |
| 77 | import android.view.accessibility.IAccessibilityInteractionConnectionCallback; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 78 | import android.view.accessibility.IAccessibilityManager; |
| 79 | import android.view.accessibility.IAccessibilityManagerClient; |
| 80 | |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 81 | import com.android.internal.R; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 82 | import com.android.internal.content.PackageMonitor; |
Svetoslav Ganov | c682fc9 | 2012-06-04 14:02:09 -0700 | [diff] [blame] | 83 | import com.android.internal.statusbar.IStatusBarService; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 84 | |
| 85 | import org.xmlpull.v1.XmlPullParserException; |
| 86 | |
Svetoslav Ganov | cc4053e | 2011-05-23 13:37:44 -0700 | [diff] [blame] | 87 | import java.io.IOException; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 88 | import java.util.ArrayList; |
| 89 | import java.util.Arrays; |
| 90 | import java.util.HashMap; |
| 91 | import java.util.HashSet; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 92 | import java.util.Iterator; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 93 | import java.util.List; |
| 94 | import java.util.Map; |
| 95 | import java.util.Set; |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 96 | import java.util.concurrent.CopyOnWriteArrayList; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * This class is instantiated by the system as a system level service and can be |
| 100 | * accessed only by the system. The task of this service is to be a centralized |
| 101 | * event dispatch for {@link AccessibilityEvent}s generated across all processes |
| 102 | * on the device. Events are dispatched to {@link AccessibilityService}s. |
| 103 | * |
| 104 | * @hide |
| 105 | */ |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 106 | public class AccessibilityManagerService extends IAccessibilityManager.Stub { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 107 | |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 108 | private static final boolean DEBUG = false; |
| 109 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 110 | private static final String LOG_TAG = "AccessibilityManagerService"; |
| 111 | |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 112 | // TODO: This is arbitrary. When there is time implement this by watching |
| 113 | // when that accessibility services are bound. |
Svetoslav Ganov | 59f0769 | 2012-10-01 19:01:32 -0700 | [diff] [blame] | 114 | private static final int WAIT_FOR_USER_STATE_FULLY_INITIALIZED_MILLIS = 3000; |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 115 | |
Svetoslav Ganov | 0d04e24 | 2012-02-21 13:46:36 -0800 | [diff] [blame] | 116 | private static final String FUNCTION_REGISTER_UI_TEST_AUTOMATION_SERVICE = |
| 117 | "registerUiTestAutomationService"; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 118 | |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 119 | private static final String TEMPORARY_ENABLE_ACCESSIBILITY_UNTIL_KEYGUARD_REMOVED = |
| 120 | "temporaryEnableAccessibilityStateUntilKeyguardRemoved"; |
| 121 | |
Svetoslav Ganov | 9a4c5cd | 2012-05-30 14:06:32 -0700 | [diff] [blame] | 122 | private static final char COMPONENT_NAME_SEPARATOR = ':'; |
| 123 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 124 | private static final int OWN_PROCESS_ID = android.os.Process.myPid(); |
| 125 | |
Svetoslav Ganov | fefd20e | 2012-04-19 21:44:35 -0700 | [diff] [blame] | 126 | private static int sIdCounter = 0; |
| 127 | |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 128 | private static int sNextWindowId; |
| 129 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 130 | private final Context mContext; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 131 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 132 | private final Object mLock = new Object(); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 133 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 134 | private final SimpleStringSplitter mStringColonSplitter = |
| 135 | new SimpleStringSplitter(COMPONENT_NAME_SEPARATOR); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 136 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 137 | private final List<AccessibilityServiceInfo> mEnabledServicesForFeedbackTempList = |
| 138 | new ArrayList<AccessibilityServiceInfo>(); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 139 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 140 | private final PackageManager mPackageManager; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 141 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 142 | private final IWindowManager mWindowManagerService; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 143 | |
| 144 | private final SecurityPolicy mSecurityPolicy; |
| 145 | |
Svetoslav Ganov | 1cf70bb | 2012-08-06 10:53:34 -0700 | [diff] [blame] | 146 | private final MainHandler mMainHandler; |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 147 | |
Svetoslav Ganov | 9b666d0 | 2012-02-10 14:55:41 -0800 | [diff] [blame] | 148 | private Service mUiAutomationService; |
| 149 | |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 150 | private Service mQueryBridge; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 151 | |
Svetoslav Ganov | 9a4c5cd | 2012-05-30 14:06:32 -0700 | [diff] [blame] | 152 | private AlertDialog mEnableTouchExplorationDialog; |
| 153 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 154 | private AccessibilityInputFilter mInputFilter; |
| 155 | |
| 156 | private boolean mHasInputFilter; |
| 157 | |
| 158 | private final RemoteCallbackList<IAccessibilityManagerClient> mGlobalClients = |
| 159 | new RemoteCallbackList<IAccessibilityManagerClient>(); |
| 160 | |
| 161 | private final SparseArray<AccessibilityConnectionWrapper> mGlobalInteractionConnections = |
| 162 | new SparseArray<AccessibilityConnectionWrapper>(); |
| 163 | |
| 164 | private final SparseArray<IBinder> mGlobalWindowTokens = new SparseArray<IBinder>(); |
| 165 | |
| 166 | private final SparseArray<UserState> mUserStates = new SparseArray<UserState>(); |
| 167 | |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 168 | private final TempUserStateChangeMemento mTempStateChangeForCurrentUserMemento = |
| 169 | new TempUserStateChangeMemento(); |
| 170 | |
Svetoslav Ganov | a8afa69 | 2012-09-25 13:03:18 -0700 | [diff] [blame] | 171 | private int mCurrentUserId = UserHandle.USER_OWNER; |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 172 | |
| 173 | private UserState getCurrentUserStateLocked() { |
| 174 | return getUserStateLocked(mCurrentUserId); |
| 175 | } |
| 176 | |
| 177 | private UserState getUserStateLocked(int userId) { |
| 178 | UserState state = mUserStates.get(userId); |
| 179 | if (state == null) { |
| 180 | state = new UserState(userId); |
| 181 | mUserStates.put(userId, state); |
| 182 | } |
| 183 | return state; |
| 184 | } |
| 185 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 186 | /** |
| 187 | * Creates a new instance. |
| 188 | * |
| 189 | * @param context A {@link Context} instance. |
| 190 | */ |
Jeff Brown | 6e6cd7a | 2011-03-30 03:27:08 -0700 | [diff] [blame] | 191 | public AccessibilityManagerService(Context context) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 192 | mContext = context; |
| 193 | mPackageManager = mContext.getPackageManager(); |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 194 | mWindowManagerService = (IWindowManager) ServiceManager.getService(Context.WINDOW_SERVICE); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 195 | mSecurityPolicy = new SecurityPolicy(); |
Svetoslav Ganov | 1cf70bb | 2012-08-06 10:53:34 -0700 | [diff] [blame] | 196 | mMainHandler = new MainHandler(mContext.getMainLooper()); |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 197 | registerBroadcastReceivers(); |
| 198 | new AccessibilityContentObserver(mMainHandler).register( |
| 199 | context.getContentResolver()); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 202 | private void registerBroadcastReceivers() { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 203 | PackageMonitor monitor = new PackageMonitor() { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 204 | @Override |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 205 | public void onSomePackagesChanged() { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 206 | synchronized (mLock) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 207 | if (getChangingUserId() != mCurrentUserId) { |
| 208 | return; |
| 209 | } |
Svetoslav Ganov | 4074e8a | 2012-05-23 11:52:36 -0700 | [diff] [blame] | 210 | // We will update when the automation service dies. |
| 211 | if (mUiAutomationService == null) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 212 | UserState userState = getCurrentUserStateLocked(); |
| 213 | populateInstalledAccessibilityServiceLocked(userState); |
| 214 | manageServicesLocked(userState); |
Svetoslav Ganov | 4074e8a | 2012-05-23 11:52:36 -0700 | [diff] [blame] | 215 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 216 | } |
| 217 | } |
Svetoslav Ganov | 00aabf7 | 2011-07-21 11:35:03 -0700 | [diff] [blame] | 218 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 219 | @Override |
Svetoslav Ganov | d07d60b | 2011-09-14 11:41:29 -0700 | [diff] [blame] | 220 | public void onPackageRemoved(String packageName, int uid) { |
| 221 | synchronized (mLock) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 222 | final int userId = getChangingUserId(); |
| 223 | if (userId != mCurrentUserId) { |
| 224 | return; |
| 225 | } |
| 226 | UserState state = getUserStateLocked(userId); |
| 227 | Iterator<ComponentName> it = state.mEnabledServices.iterator(); |
Svetoslav Ganov | d07d60b | 2011-09-14 11:41:29 -0700 | [diff] [blame] | 228 | while (it.hasNext()) { |
| 229 | ComponentName comp = it.next(); |
| 230 | String compPkg = comp.getPackageName(); |
| 231 | if (compPkg.equals(packageName)) { |
| 232 | it.remove(); |
Svetoslav Ganov | 9a4c5cd | 2012-05-30 14:06:32 -0700 | [diff] [blame] | 233 | // Update the enabled services setting. |
| 234 | persistComponentNamesToSettingLocked( |
| 235 | Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 236 | state.mEnabledServices, userId); |
Svetoslav Ganov | 9a4c5cd | 2012-05-30 14:06:32 -0700 | [diff] [blame] | 237 | // Update the touch exploration granted services setting. |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 238 | state.mTouchExplorationGrantedServices.remove(comp); |
Svetoslav Ganov | 9a4c5cd | 2012-05-30 14:06:32 -0700 | [diff] [blame] | 239 | persistComponentNamesToSettingLocked( |
| 240 | Settings.Secure. |
| 241 | TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES, |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 242 | state.mEnabledServices, userId); |
Svetoslav Ganov | d07d60b | 2011-09-14 11:41:29 -0700 | [diff] [blame] | 243 | return; |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | @Override |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 250 | public boolean onHandleForceStop(Intent intent, String[] packages, |
| 251 | int uid, boolean doit) { |
| 252 | synchronized (mLock) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 253 | final int userId = getChangingUserId(); |
| 254 | if (userId != mCurrentUserId) { |
| 255 | return false; |
| 256 | } |
| 257 | UserState state = getUserStateLocked(userId); |
| 258 | Iterator<ComponentName> it = state.mEnabledServices.iterator(); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 259 | while (it.hasNext()) { |
| 260 | ComponentName comp = it.next(); |
| 261 | String compPkg = comp.getPackageName(); |
| 262 | for (String pkg : packages) { |
| 263 | if (compPkg.equals(pkg)) { |
| 264 | if (!doit) { |
| 265 | return true; |
| 266 | } |
| 267 | it.remove(); |
Svetoslav Ganov | 9a4c5cd | 2012-05-30 14:06:32 -0700 | [diff] [blame] | 268 | persistComponentNamesToSettingLocked( |
| 269 | Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 270 | state.mEnabledServices, userId); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 274 | return false; |
| 275 | } |
| 276 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 277 | }; |
| 278 | |
| 279 | // package changes |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 280 | monitor.register(mContext, null, UserHandle.ALL, true); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 281 | |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 282 | // user change and unlock |
| 283 | IntentFilter intentFilter = new IntentFilter(); |
| 284 | intentFilter.addAction(Intent.ACTION_USER_SWITCHED); |
| 285 | intentFilter.addAction(Intent.ACTION_USER_REMOVED); |
| 286 | intentFilter.addAction(Intent.ACTION_USER_PRESENT); |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 287 | |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 288 | mContext.registerReceiverAsUser(new BroadcastReceiver() { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 289 | @Override |
| 290 | public void onReceive(Context context, Intent intent) { |
| 291 | String action = intent.getAction(); |
| 292 | if (Intent.ACTION_USER_SWITCHED.equals(action)) { |
| 293 | switchUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0)); |
| 294 | } else if (Intent.ACTION_USER_REMOVED.equals(action)) { |
| 295 | removeUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0)); |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 296 | } else if (Intent.ACTION_USER_PRESENT.equals(action)) { |
| 297 | restoreStateFromMementoIfNeeded(); |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 298 | } |
| 299 | } |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 300 | }, UserHandle.ALL, intentFilter, null, null); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 303 | public int addClient(IAccessibilityManagerClient client, int userId) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 304 | synchronized (mLock) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 305 | final int resolvedUserId = mSecurityPolicy |
| 306 | .resolveCallingUserIdEnforcingPermissionsLocked(userId); |
| 307 | // If the client is from a process that runs across users such as |
| 308 | // the system UI or the system we add it to the global state that |
| 309 | // is shared across users. |
| 310 | UserState userState = getUserStateLocked(resolvedUserId); |
| 311 | if (mSecurityPolicy.isCallerInteractingAcrossUsers(userId)) { |
| 312 | mGlobalClients.register(client); |
Svetoslav Ganov | a8afa69 | 2012-09-25 13:03:18 -0700 | [diff] [blame] | 313 | if (DEBUG) { |
| 314 | Slog.i(LOG_TAG, "Added global client for pid:" + Binder.getCallingPid()); |
| 315 | } |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 316 | return getClientState(userState); |
| 317 | } else { |
| 318 | userState.mClients.register(client); |
| 319 | // If this client is not for the current user we do not |
| 320 | // return a state since it is not for the foreground user. |
| 321 | // We will send the state to the client on a user switch. |
Svetoslav Ganov | a8afa69 | 2012-09-25 13:03:18 -0700 | [diff] [blame] | 322 | if (DEBUG) { |
| 323 | Slog.i(LOG_TAG, "Added user client for pid:" + Binder.getCallingPid() |
| 324 | + " and userId:" + mCurrentUserId); |
| 325 | } |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 326 | return (resolvedUserId == mCurrentUserId) ? getClientState(userState) : 0; |
| 327 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 331 | public boolean sendAccessibilityEvent(AccessibilityEvent event, int userId) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 332 | synchronized (mLock) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 333 | final int resolvedUserId = mSecurityPolicy |
| 334 | .resolveCallingUserIdEnforcingPermissionsLocked(userId); |
| 335 | // This method does nothing for a background user. |
| 336 | if (resolvedUserId != mCurrentUserId) { |
| 337 | return true; // yes, recycle the event |
| 338 | } |
Svetoslav Ganov | eeee4d2 | 2011-06-10 20:51:30 -0700 | [diff] [blame] | 339 | if (mSecurityPolicy.canDispatchAccessibilityEvent(event)) { |
Svetoslav Ganov | a8afa69 | 2012-09-25 13:03:18 -0700 | [diff] [blame] | 340 | mSecurityPolicy.updateEventSourceLocked(event); |
| 341 | mMainHandler.obtainMessage(MainHandler.MSG_UPDATE_ACTIVE_WINDOW, |
| 342 | event.getWindowId(), event.getEventType()).sendToTarget(); |
Svetoslav Ganov | eeee4d2 | 2011-06-10 20:51:30 -0700 | [diff] [blame] | 343 | notifyAccessibilityServicesDelayedLocked(event, false); |
| 344 | notifyAccessibilityServicesDelayedLocked(event, true); |
| 345 | } |
Svetoslav Ganov | 8678347 | 2012-06-06 21:12:20 -0700 | [diff] [blame] | 346 | if (mHasInputFilter && mInputFilter != null) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 347 | mMainHandler.obtainMessage(MainHandler.MSG_SEND_ACCESSIBILITY_EVENT_TO_INPUT_FILTER, |
Svetoslav Ganov | e45c0b2 | 2012-06-08 17:44:29 -0700 | [diff] [blame] | 348 | AccessibilityEvent.obtain(event)).sendToTarget(); |
Svetoslav Ganov | 8678347 | 2012-06-06 21:12:20 -0700 | [diff] [blame] | 349 | } |
Svetoslav Ganov | cd94caf | 2012-06-03 19:34:34 -0700 | [diff] [blame] | 350 | event.recycle(); |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 351 | getUserStateLocked(resolvedUserId).mHandledFeedbackTypes = 0; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 352 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 353 | return (OWN_PROCESS_ID != Binder.getCallingPid()); |
| 354 | } |
| 355 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 356 | public List<AccessibilityServiceInfo> getInstalledAccessibilityServiceList(int userId) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 357 | synchronized (mLock) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 358 | final int resolvedUserId = mSecurityPolicy |
| 359 | .resolveCallingUserIdEnforcingPermissionsLocked(userId); |
| 360 | return getUserStateLocked(resolvedUserId).mInstalledServices; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 364 | public List<AccessibilityServiceInfo> getEnabledAccessibilityServiceList(int feedbackType, |
| 365 | int userId) { |
| 366 | List<AccessibilityServiceInfo> result = null; |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 367 | synchronized (mLock) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 368 | final int resolvedUserId = mSecurityPolicy |
| 369 | .resolveCallingUserIdEnforcingPermissionsLocked(userId); |
| 370 | result = mEnabledServicesForFeedbackTempList; |
| 371 | result.clear(); |
| 372 | List<Service> services = getUserStateLocked(resolvedUserId).mServices; |
Svetoslav Ganov | cc4053e | 2011-05-23 13:37:44 -0700 | [diff] [blame] | 373 | while (feedbackType != 0) { |
| 374 | final int feedbackTypeBit = (1 << Integer.numberOfTrailingZeros(feedbackType)); |
| 375 | feedbackType &= ~feedbackTypeBit; |
Svetoslav Ganov | f9886f3 | 2011-05-10 15:28:33 -0700 | [diff] [blame] | 376 | final int serviceCount = services.size(); |
| 377 | for (int i = 0; i < serviceCount; i++) { |
| 378 | Service service = services.get(i); |
Svetoslav Ganov | c321c19 | 2011-06-03 19:43:23 -0700 | [diff] [blame] | 379 | if ((service.mFeedbackType & feedbackTypeBit) != 0) { |
Svetoslav Ganov | f9886f3 | 2011-05-10 15:28:33 -0700 | [diff] [blame] | 380 | result.add(service.mAccessibilityServiceInfo); |
| 381 | } |
Svetoslav Ganov | cc4053e | 2011-05-23 13:37:44 -0700 | [diff] [blame] | 382 | } |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 383 | } |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 384 | } |
Svetoslav Ganov | f9886f3 | 2011-05-10 15:28:33 -0700 | [diff] [blame] | 385 | return result; |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 388 | public void interrupt(int userId) { |
| 389 | CopyOnWriteArrayList<Service> services; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 390 | synchronized (mLock) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 391 | final int resolvedUserId = mSecurityPolicy |
| 392 | .resolveCallingUserIdEnforcingPermissionsLocked(userId); |
| 393 | // This method does nothing for a background user. |
| 394 | if (resolvedUserId != mCurrentUserId) { |
| 395 | return; |
| 396 | } |
| 397 | services = getUserStateLocked(resolvedUserId).mServices; |
| 398 | } |
| 399 | for (int i = 0, count = services.size(); i < count; i++) { |
| 400 | Service service = services.get(i); |
| 401 | try { |
| 402 | service.mServiceInterface.onInterrupt(); |
| 403 | } catch (RemoteException re) { |
| 404 | Slog.e(LOG_TAG, "Error during sending interrupt request to " |
| 405 | + service.mService, re); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 410 | public int addAccessibilityInteractionConnection(IWindow windowToken, |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 411 | IAccessibilityInteractionConnection connection, int userId) throws RemoteException { |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 412 | synchronized (mLock) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 413 | final int resolvedUserId = mSecurityPolicy |
| 414 | .resolveCallingUserIdEnforcingPermissionsLocked(userId); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 415 | final int windowId = sNextWindowId++; |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 416 | // If the window is from a process that runs across users such as |
| 417 | // the system UI or the system we add it to the global state that |
| 418 | // is shared across users. |
| 419 | if (mSecurityPolicy.isCallerInteractingAcrossUsers(userId)) { |
| 420 | AccessibilityConnectionWrapper wrapper = new AccessibilityConnectionWrapper( |
| 421 | windowId, connection, UserHandle.USER_ALL); |
| 422 | wrapper.linkToDeath(); |
| 423 | mGlobalInteractionConnections.put(windowId, wrapper); |
| 424 | mGlobalWindowTokens.put(windowId, windowToken.asBinder()); |
Svetoslav Ganov | a8afa69 | 2012-09-25 13:03:18 -0700 | [diff] [blame] | 425 | if (DEBUG) { |
| 426 | Slog.i(LOG_TAG, "Added global connection for pid:" + Binder.getCallingPid() |
| 427 | + " with windowId: " + windowId); |
| 428 | } |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 429 | } else { |
| 430 | AccessibilityConnectionWrapper wrapper = new AccessibilityConnectionWrapper( |
| 431 | windowId, connection, resolvedUserId); |
| 432 | wrapper.linkToDeath(); |
| 433 | UserState userState = getUserStateLocked(resolvedUserId); |
| 434 | userState.mInteractionConnections.put(windowId, wrapper); |
| 435 | userState.mWindowTokens.put(windowId, windowToken.asBinder()); |
Svetoslav Ganov | a8afa69 | 2012-09-25 13:03:18 -0700 | [diff] [blame] | 436 | if (DEBUG) { |
| 437 | Slog.i(LOG_TAG, "Added user connection for pid:" + Binder.getCallingPid() |
| 438 | + " with windowId: " + windowId + " and userId:" + mCurrentUserId); |
| 439 | } |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 440 | } |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 441 | if (DEBUG) { |
| 442 | Slog.i(LOG_TAG, "Adding interaction connection to windowId: " + windowId); |
| 443 | } |
| 444 | return windowId; |
| 445 | } |
| 446 | } |
| 447 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 448 | public void removeAccessibilityInteractionConnection(IWindow window) { |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 449 | synchronized (mLock) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 450 | mSecurityPolicy.resolveCallingUserIdEnforcingPermissionsLocked( |
| 451 | UserHandle.getCallingUserId()); |
| 452 | IBinder token = window.asBinder(); |
Svetoslav Ganov | a8afa69 | 2012-09-25 13:03:18 -0700 | [diff] [blame] | 453 | final int removedWindowId = removeAccessibilityInteractionConnectionInternalLocked( |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 454 | token, mGlobalWindowTokens, mGlobalInteractionConnections); |
Svetoslav Ganov | a8afa69 | 2012-09-25 13:03:18 -0700 | [diff] [blame] | 455 | if (removedWindowId >= 0) { |
| 456 | if (DEBUG) { |
| 457 | Slog.i(LOG_TAG, "Removed global connection for pid:" + Binder.getCallingPid() |
| 458 | + " with windowId: " + removedWindowId); |
| 459 | } |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 460 | return; |
| 461 | } |
| 462 | final int userCount = mUserStates.size(); |
| 463 | for (int i = 0; i < userCount; i++) { |
| 464 | UserState userState = mUserStates.valueAt(i); |
Svetoslav Ganov | a8afa69 | 2012-09-25 13:03:18 -0700 | [diff] [blame] | 465 | final int removedWindowIdForUser = |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 466 | removeAccessibilityInteractionConnectionInternalLocked( |
| 467 | token, userState.mWindowTokens, userState.mInteractionConnections); |
Svetoslav Ganov | a8afa69 | 2012-09-25 13:03:18 -0700 | [diff] [blame] | 468 | if (removedWindowIdForUser >= 0) { |
| 469 | if (DEBUG) { |
| 470 | Slog.i(LOG_TAG, "Removed user connection for pid:" + Binder.getCallingPid() |
| 471 | + " with windowId: " + removedWindowIdForUser + " and userId:" |
| 472 | + mUserStates.keyAt(i)); |
| 473 | } |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 474 | return; |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
Svetoslav Ganov | a8afa69 | 2012-09-25 13:03:18 -0700 | [diff] [blame] | 480 | private int removeAccessibilityInteractionConnectionInternalLocked(IBinder windowToken, |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 481 | SparseArray<IBinder> windowTokens, |
| 482 | SparseArray<AccessibilityConnectionWrapper> interactionConnections) { |
| 483 | final int count = windowTokens.size(); |
| 484 | for (int i = 0; i < count; i++) { |
| 485 | if (windowTokens.valueAt(i) == windowToken) { |
| 486 | final int windowId = windowTokens.keyAt(i); |
| 487 | windowTokens.removeAt(i); |
| 488 | AccessibilityConnectionWrapper wrapper = interactionConnections.get(windowId); |
| 489 | wrapper.unlinkToDeath(); |
| 490 | interactionConnections.remove(windowId); |
Svetoslav Ganov | a8afa69 | 2012-09-25 13:03:18 -0700 | [diff] [blame] | 491 | return windowId; |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 492 | } |
| 493 | } |
Svetoslav Ganov | a8afa69 | 2012-09-25 13:03:18 -0700 | [diff] [blame] | 494 | return -1; |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 497 | public void registerUiTestAutomationService(IAccessibilityServiceClient serviceClient, |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 498 | AccessibilityServiceInfo accessibilityServiceInfo) { |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 499 | mSecurityPolicy.enforceCallingPermission(Manifest.permission.RETRIEVE_WINDOW_CONTENT, |
Svetoslav Ganov | 0d04e24 | 2012-02-21 13:46:36 -0800 | [diff] [blame] | 500 | FUNCTION_REGISTER_UI_TEST_AUTOMATION_SERVICE); |
Svetoslav Ganov | e8f9535 | 2011-07-06 17:24:03 -0700 | [diff] [blame] | 501 | ComponentName componentName = new ComponentName("foo.bar", |
| 502 | "AutomationAccessibilityService"); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 503 | synchronized (mLock) { |
Svetoslav Ganov | b6eca6e | 2011-09-27 10:47:58 -0700 | [diff] [blame] | 504 | // If an automation services is connected to the system all services are stopped |
| 505 | // so the automation one is the only one running. Settings are not changed so when |
| 506 | // the automation service goes away the state is restored from the settings. |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 507 | UserState userState = getCurrentUserStateLocked(); |
| 508 | unbindAllServicesLocked(userState); |
Svetoslav Ganov | b6eca6e | 2011-09-27 10:47:58 -0700 | [diff] [blame] | 509 | |
Svetoslav Ganov | b6eca6e | 2011-09-27 10:47:58 -0700 | [diff] [blame] | 510 | // If necessary enable accessibility and announce that. |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 511 | if (!userState.mIsAccessibilityEnabled) { |
| 512 | userState.mIsAccessibilityEnabled = true; |
Svetoslav Ganov | b6eca6e | 2011-09-27 10:47:58 -0700 | [diff] [blame] | 513 | } |
Svetoslav Ganov | 657968a | 2012-09-21 19:08:35 -0700 | [diff] [blame] | 514 | // No touch exploration. |
| 515 | userState.mIsTouchExplorationEnabled = false; |
| 516 | |
| 517 | // Hook the automation service up. |
| 518 | mUiAutomationService = new Service(mCurrentUserId, componentName, |
| 519 | accessibilityServiceInfo, true); |
| 520 | mUiAutomationService.onServiceConnected(componentName, serviceClient.asBinder()); |
| 521 | |
| 522 | updateInputFilterLocked(userState); |
| 523 | scheduleSendStateToClientsLocked(userState); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 524 | } |
Svetoslav Ganov | d116d7c | 2011-11-21 18:41:59 -0800 | [diff] [blame] | 525 | } |
| 526 | |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 527 | public void temporaryEnableAccessibilityStateUntilKeyguardRemoved( |
| 528 | ComponentName service, boolean touchExplorationEnabled) { |
| 529 | mSecurityPolicy.enforceCallingPermission( |
| 530 | Manifest.permission.TEMPORARY_ENABLE_ACCESSIBILITY, |
| 531 | TEMPORARY_ENABLE_ACCESSIBILITY_UNTIL_KEYGUARD_REMOVED); |
| 532 | try { |
| 533 | if (!mWindowManagerService.isKeyguardLocked()) { |
| 534 | return; |
| 535 | } |
| 536 | } catch (RemoteException re) { |
| 537 | return; |
| 538 | } |
| 539 | synchronized (mLock) { |
| 540 | UserState userState = getCurrentUserStateLocked(); |
| 541 | // Stash the old state so we can restore it when the keyguard is gone. |
| 542 | mTempStateChangeForCurrentUserMemento.initialize(mCurrentUserId, getCurrentUserStateLocked()); |
| 543 | // Set the temporary state. |
| 544 | userState.mIsAccessibilityEnabled = true; |
| 545 | userState.mIsTouchExplorationEnabled= touchExplorationEnabled; |
| 546 | userState.mIsDisplayMagnificationEnabled = false; |
| 547 | userState.mEnabledServices.clear(); |
| 548 | userState.mEnabledServices.add(service); |
| 549 | userState.mTouchExplorationGrantedServices.clear(); |
| 550 | userState.mTouchExplorationGrantedServices.add(service); |
| 551 | // Update the internal state. |
| 552 | performServiceManagementLocked(userState); |
| 553 | updateInputFilterLocked(userState); |
| 554 | scheduleSendStateToClientsLocked(userState); |
| 555 | } |
| 556 | } |
| 557 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 558 | public void unregisterUiTestAutomationService(IAccessibilityServiceClient serviceClient) { |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 559 | synchronized (mLock) { |
Svetoslav Ganov | 9b666d0 | 2012-02-10 14:55:41 -0800 | [diff] [blame] | 560 | // Automation service is not bound, so pretend it died to perform clean up. |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 561 | if (mUiAutomationService != null |
| 562 | && mUiAutomationService.mServiceInterface == serviceClient) { |
Svetoslav Ganov | 9b666d0 | 2012-02-10 14:55:41 -0800 | [diff] [blame] | 563 | mUiAutomationService.binderDied(); |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 568 | boolean onGesture(int gestureId) { |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 569 | synchronized (mLock) { |
Svetoslav Ganov | fefd20e | 2012-04-19 21:44:35 -0700 | [diff] [blame] | 570 | boolean handled = notifyGestureLocked(gestureId, false); |
| 571 | if (!handled) { |
| 572 | handled = notifyGestureLocked(gestureId, true); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 573 | } |
Svetoslav Ganov | fefd20e | 2012-04-19 21:44:35 -0700 | [diff] [blame] | 574 | return handled; |
| 575 | } |
| 576 | } |
| 577 | |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 578 | /** |
Svetoslav Ganov | 8678347 | 2012-06-06 21:12:20 -0700 | [diff] [blame] | 579 | * Gets the bounds of the accessibility focus in the active window. |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 580 | * |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 581 | * @param outBounds The output to which to write the focus bounds. |
Svetoslav Ganov | 8678347 | 2012-06-06 21:12:20 -0700 | [diff] [blame] | 582 | * @return Whether accessibility focus was found and the bounds are populated. |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 583 | */ |
Svetoslav Ganov | 8678347 | 2012-06-06 21:12:20 -0700 | [diff] [blame] | 584 | boolean getAccessibilityFocusBoundsInActiveWindow(Rect outBounds) { |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 585 | // Instead of keeping track of accessibility focus events per |
| 586 | // window to be able to find the focus in the active window, |
| 587 | // we take a stateless approach and look it up. This is fine |
| 588 | // since we do this only when the user clicks/long presses. |
| 589 | Service service = getQueryBridge(); |
| 590 | final int connectionId = service.mId; |
| 591 | AccessibilityInteractionClient client = AccessibilityInteractionClient.getInstance(); |
| 592 | client.addConnection(connectionId, service); |
| 593 | try { |
| 594 | AccessibilityNodeInfo root = AccessibilityInteractionClient.getInstance() |
| 595 | .getRootInActiveWindow(connectionId); |
| 596 | if (root == null) { |
| 597 | return false; |
| 598 | } |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 599 | AccessibilityNodeInfo focus = root.findFocus( |
| 600 | AccessibilityNodeInfo.FOCUS_ACCESSIBILITY); |
| 601 | if (focus == null) { |
| 602 | return false; |
| 603 | } |
| 604 | focus.getBoundsInScreen(outBounds); |
| 605 | return true; |
| 606 | } finally { |
| 607 | client.removeConnection(connectionId); |
| 608 | } |
| 609 | } |
| 610 | |
Svetoslav Ganov | 8678347 | 2012-06-06 21:12:20 -0700 | [diff] [blame] | 611 | /** |
| 612 | * Gets the bounds of the active window. |
| 613 | * |
| 614 | * @param outBounds The output to which to write the bounds. |
| 615 | */ |
Svetoslav Ganov | c9c9a48 | 2012-07-16 08:46:07 -0700 | [diff] [blame] | 616 | boolean getActiveWindowBounds(Rect outBounds) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 617 | IBinder token; |
Svetoslav Ganov | 8678347 | 2012-06-06 21:12:20 -0700 | [diff] [blame] | 618 | synchronized (mLock) { |
| 619 | final int windowId = mSecurityPolicy.mActiveWindowId; |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 620 | token = mGlobalWindowTokens.get(windowId); |
| 621 | if (token == null) { |
| 622 | token = getCurrentUserStateLocked().mWindowTokens.get(windowId); |
Svetoslav Ganov | c9c9a48 | 2012-07-16 08:46:07 -0700 | [diff] [blame] | 623 | } |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 624 | } |
| 625 | WindowInfo info = null; |
| 626 | try { |
| 627 | info = mWindowManagerService.getWindowInfo(token); |
| 628 | if (info != null) { |
| 629 | outBounds.set(info.frame); |
| 630 | return true; |
| 631 | } |
| 632 | } catch (RemoteException re) { |
| 633 | /* ignore */ |
| 634 | } finally { |
| 635 | if (info != null) { |
| 636 | info.recycle(); |
| 637 | } |
| 638 | } |
| 639 | return false; |
| 640 | } |
| 641 | |
| 642 | int getActiveWindowId() { |
| 643 | return mSecurityPolicy.mActiveWindowId; |
| 644 | } |
| 645 | |
| 646 | private void switchUser(int userId) { |
| 647 | synchronized (mLock) { |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 648 | // The user switched so we do not need to restore the current user |
| 649 | // state since we will fully rebuild it when he becomes current again. |
| 650 | mTempStateChangeForCurrentUserMemento.clear(); |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 651 | |
| 652 | // Disconnect from services for the old user. |
| 653 | UserState oldUserState = getUserStateLocked(mCurrentUserId); |
| 654 | unbindAllServicesLocked(oldUserState); |
| 655 | |
| 656 | // Disable the local managers for the old user. |
| 657 | if (oldUserState.mClients.getRegisteredCallbackCount() > 0) { |
| 658 | mMainHandler.obtainMessage(MainHandler.MSG_SEND_CLEARED_STATE_TO_CLIENTS_FOR_USER, |
| 659 | oldUserState.mUserId, 0).sendToTarget(); |
| 660 | } |
| 661 | |
Svetoslav Ganov | 59f0769 | 2012-10-01 19:01:32 -0700 | [diff] [blame] | 662 | // Announce user changes only if more that one exist. |
| 663 | UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
| 664 | final boolean announceNewUser = userManager.getUsers().size() > 1; |
| 665 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 666 | // The user changed. |
| 667 | mCurrentUserId = userId; |
| 668 | |
| 669 | // Recreate the internal state for the new user. |
| 670 | mMainHandler.obtainMessage(MainHandler.MSG_SEND_RECREATE_INTERNAL_STATE, |
| 671 | mCurrentUserId, 0).sendToTarget(); |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 672 | |
Svetoslav Ganov | 59f0769 | 2012-10-01 19:01:32 -0700 | [diff] [blame] | 673 | if (announceNewUser) { |
| 674 | // Schedule announcement of the current user if needed. |
| 675 | mMainHandler.sendEmptyMessageDelayed(MainHandler.MSG_ANNOUNCE_NEW_USER_IF_NEEDED, |
| 676 | WAIT_FOR_USER_STATE_FULLY_INITIALIZED_MILLIS); |
| 677 | } |
Svetoslav Ganov | 8678347 | 2012-06-06 21:12:20 -0700 | [diff] [blame] | 678 | } |
| 679 | } |
| 680 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 681 | private void removeUser(int userId) { |
| 682 | synchronized (mLock) { |
| 683 | mUserStates.remove(userId); |
| 684 | } |
Svetoslav Ganov | 385d9f2 | 2012-06-07 16:35:04 -0700 | [diff] [blame] | 685 | } |
| 686 | |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 687 | private void restoreStateFromMementoIfNeeded() { |
| 688 | synchronized (mLock) { |
| 689 | if (mTempStateChangeForCurrentUserMemento.mUserId != UserHandle.USER_NULL) { |
| 690 | UserState userState = getCurrentUserStateLocked(); |
| 691 | // Restore the state from the memento. |
| 692 | mTempStateChangeForCurrentUserMemento.applyTo(userState); |
| 693 | mTempStateChangeForCurrentUserMemento.clear(); |
| 694 | // Update the internal state. |
| 695 | performServiceManagementLocked(userState); |
| 696 | updateInputFilterLocked(userState); |
| 697 | scheduleSendStateToClientsLocked(userState); |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 702 | private Service getQueryBridge() { |
| 703 | if (mQueryBridge == null) { |
| 704 | AccessibilityServiceInfo info = new AccessibilityServiceInfo(); |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 705 | mQueryBridge = new Service(UserHandle.USER_NULL, null, info, true); |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 706 | } |
| 707 | return mQueryBridge; |
| 708 | } |
| 709 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 710 | private boolean notifyGestureLocked(int gestureId, boolean isDefault) { |
Svetoslav Ganov | fefd20e | 2012-04-19 21:44:35 -0700 | [diff] [blame] | 711 | // TODO: Now we are giving the gestures to the last enabled |
| 712 | // service that can handle them which is the last one |
| 713 | // in our list since we write the last enabled as the |
| 714 | // last record in the enabled services setting. Ideally, |
| 715 | // the user should make the call which service handles |
| 716 | // gestures. However, only one service should handle |
Svetoslav Ganov | e4abc51 | 2012-05-09 11:02:38 -0700 | [diff] [blame] | 717 | // gestures to avoid user frustration when different |
| 718 | // behavior is observed from different combinations of |
Svetoslav Ganov | fefd20e | 2012-04-19 21:44:35 -0700 | [diff] [blame] | 719 | // enabled accessibility services. |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 720 | UserState state = getCurrentUserStateLocked(); |
| 721 | for (int i = state.mServices.size() - 1; i >= 0; i--) { |
| 722 | Service service = state.mServices.get(i); |
Svetoslav Ganov | a43ef3d | 2012-07-12 13:07:55 -0700 | [diff] [blame] | 723 | if (service.mRequestTouchExplorationMode && service.mIsDefault == isDefault) { |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 724 | service.notifyGesture(gestureId); |
Svetoslav Ganov | fefd20e | 2012-04-19 21:44:35 -0700 | [diff] [blame] | 725 | return true; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 726 | } |
| 727 | } |
| 728 | return false; |
| 729 | } |
| 730 | |
Svetoslav Ganov | d116d7c | 2011-11-21 18:41:59 -0800 | [diff] [blame] | 731 | /** |
| 732 | * Removes an AccessibilityInteractionConnection. |
| 733 | * |
| 734 | * @param windowId The id of the window to which the connection is targeted. |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 735 | * @param userId The id of the user owning the connection. UserHandle.USER_ALL |
| 736 | * if global. |
Svetoslav Ganov | d116d7c | 2011-11-21 18:41:59 -0800 | [diff] [blame] | 737 | */ |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 738 | private void removeAccessibilityInteractionConnectionLocked(int windowId, int userId) { |
| 739 | if (userId == UserHandle.USER_ALL) { |
| 740 | mGlobalWindowTokens.remove(windowId); |
| 741 | mGlobalInteractionConnections.remove(windowId); |
| 742 | } else { |
| 743 | UserState userState = getCurrentUserStateLocked(); |
| 744 | userState.mWindowTokens.remove(windowId); |
| 745 | userState.mInteractionConnections.remove(windowId); |
| 746 | } |
Svetoslav Ganov | d116d7c | 2011-11-21 18:41:59 -0800 | [diff] [blame] | 747 | if (DEBUG) { |
| 748 | Slog.i(LOG_TAG, "Removing interaction connection to windowId: " + windowId); |
| 749 | } |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 750 | } |
| 751 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 752 | private void populateInstalledAccessibilityServiceLocked(UserState userState) { |
| 753 | userState.mInstalledServices.clear(); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 754 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 755 | List<ResolveInfo> installedServices = mPackageManager.queryIntentServicesAsUser( |
Svetoslav Ganov | cc4053e | 2011-05-23 13:37:44 -0700 | [diff] [blame] | 756 | new Intent(AccessibilityService.SERVICE_INTERFACE), |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 757 | PackageManager.GET_SERVICES | PackageManager.GET_META_DATA, |
| 758 | mCurrentUserId); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 759 | |
| 760 | for (int i = 0, count = installedServices.size(); i < count; i++) { |
Svetoslav Ganov | cc4053e | 2011-05-23 13:37:44 -0700 | [diff] [blame] | 761 | ResolveInfo resolveInfo = installedServices.get(i); |
Svetoslav Ganov | 53e184d | 2012-05-16 15:57:10 -0700 | [diff] [blame] | 762 | ServiceInfo serviceInfo = resolveInfo.serviceInfo; |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 763 | if (!android.Manifest.permission.BIND_ACCESSIBILITY_SERVICE.equals( |
| 764 | serviceInfo.permission)) { |
Svetoslav Ganov | 53e184d | 2012-05-16 15:57:10 -0700 | [diff] [blame] | 765 | Slog.w(LOG_TAG, "Skipping accessibilty service " + new ComponentName( |
| 766 | serviceInfo.packageName, serviceInfo.name).flattenToShortString() |
| 767 | + ": it does not require the permission " |
| 768 | + android.Manifest.permission.BIND_ACCESSIBILITY_SERVICE); |
| 769 | continue; |
| 770 | } |
Svetoslav Ganov | cc4053e | 2011-05-23 13:37:44 -0700 | [diff] [blame] | 771 | AccessibilityServiceInfo accessibilityServiceInfo; |
| 772 | try { |
| 773 | accessibilityServiceInfo = new AccessibilityServiceInfo(resolveInfo, mContext); |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 774 | userState.mInstalledServices.add(accessibilityServiceInfo); |
Svetoslav Ganov | cc4053e | 2011-05-23 13:37:44 -0700 | [diff] [blame] | 775 | } catch (XmlPullParserException xppe) { |
| 776 | Slog.e(LOG_TAG, "Error while initializing AccessibilityServiceInfo", xppe); |
| 777 | } catch (IOException ioe) { |
| 778 | Slog.e(LOG_TAG, "Error while initializing AccessibilityServiceInfo", ioe); |
| 779 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 783 | private void populateEnabledAccessibilityServicesLocked(UserState userState) { |
Svetoslav Ganov | 9a4c5cd | 2012-05-30 14:06:32 -0700 | [diff] [blame] | 784 | populateComponentNamesFromSettingLocked( |
| 785 | Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 786 | userState.mUserId, |
| 787 | userState.mEnabledServices); |
Svetoslav Ganov | 9a4c5cd | 2012-05-30 14:06:32 -0700 | [diff] [blame] | 788 | } |
| 789 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 790 | private void populateTouchExplorationGrantedAccessibilityServicesLocked( |
| 791 | UserState userState) { |
Svetoslav Ganov | 9a4c5cd | 2012-05-30 14:06:32 -0700 | [diff] [blame] | 792 | populateComponentNamesFromSettingLocked( |
| 793 | Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES, |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 794 | userState.mUserId, |
| 795 | userState.mTouchExplorationGrantedServices); |
Svetoslav Ganov | 9a4c5cd | 2012-05-30 14:06:32 -0700 | [diff] [blame] | 796 | } |
| 797 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 798 | /** |
| 799 | * Performs {@link AccessibilityService}s delayed notification. The delay is configurable |
| 800 | * and denotes the period after the last event before notifying the service. |
| 801 | * |
| 802 | * @param event The event. |
| 803 | * @param isDefault True to notify default listeners, not default services. |
| 804 | */ |
| 805 | private void notifyAccessibilityServicesDelayedLocked(AccessibilityEvent event, |
| 806 | boolean isDefault) { |
Charles Chen | 85b598b | 2009-07-29 17:23:50 -0700 | [diff] [blame] | 807 | try { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 808 | UserState state = getCurrentUserStateLocked(); |
| 809 | for (int i = 0, count = state.mServices.size(); i < count; i++) { |
| 810 | Service service = state.mServices.get(i); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 811 | |
Charles Chen | 85b598b | 2009-07-29 17:23:50 -0700 | [diff] [blame] | 812 | if (service.mIsDefault == isDefault) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 813 | if (canDispathEventLocked(service, event, state.mHandledFeedbackTypes)) { |
| 814 | state.mHandledFeedbackTypes |= service.mFeedbackType; |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 815 | service.notifyAccessibilityEvent(event); |
Charles Chen | 85b598b | 2009-07-29 17:23:50 -0700 | [diff] [blame] | 816 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 817 | } |
| 818 | } |
Charles Chen | 85b598b | 2009-07-29 17:23:50 -0700 | [diff] [blame] | 819 | } catch (IndexOutOfBoundsException oobe) { |
| 820 | // An out of bounds exception can happen if services are going away |
| 821 | // as the for loop is running. If that happens, just bail because |
| 822 | // there are no more services to notify. |
| 823 | return; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 824 | } |
| 825 | } |
| 826 | |
| 827 | /** |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 828 | * Adds a service for a user. |
Svetoslav Ganov | f9886f3 | 2011-05-10 15:28:33 -0700 | [diff] [blame] | 829 | * |
| 830 | * @param service The service to add. |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 831 | * @param userId The user id. |
Svetoslav Ganov | f9886f3 | 2011-05-10 15:28:33 -0700 | [diff] [blame] | 832 | */ |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 833 | private void tryAddServiceLocked(Service service, int userId) { |
Svetoslav Ganov | f9886f3 | 2011-05-10 15:28:33 -0700 | [diff] [blame] | 834 | try { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 835 | UserState userState = getUserStateLocked(userId); |
Svetoslav Ganov | 1f22b6a | 2012-09-28 09:27:35 -0700 | [diff] [blame] | 836 | if (userState.mServices.contains(service)) { |
Svetoslav Ganov | f9886f3 | 2011-05-10 15:28:33 -0700 | [diff] [blame] | 837 | return; |
| 838 | } |
| 839 | service.linkToOwnDeath(); |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 840 | userState.mServices.add(service); |
| 841 | userState.mComponentNameToServiceMap.put(service.mComponentName, service); |
| 842 | updateInputFilterLocked(userState); |
Svetoslav Ganov | 9a4c5cd | 2012-05-30 14:06:32 -0700 | [diff] [blame] | 843 | tryEnableTouchExplorationLocked(service); |
Svetoslav Ganov | f9886f3 | 2011-05-10 15:28:33 -0700 | [diff] [blame] | 844 | } catch (RemoteException e) { |
| 845 | /* do nothing */ |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | /** |
| 850 | * Removes a service. |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 851 | * |
| 852 | * @param service The service. |
| 853 | * @return True if the service was removed, false otherwise. |
| 854 | */ |
Svetoslav Ganov | f9886f3 | 2011-05-10 15:28:33 -0700 | [diff] [blame] | 855 | private boolean tryRemoveServiceLocked(Service service) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 856 | UserState userState = getUserStateLocked(service.mUserId); |
| 857 | final boolean removed = userState.mServices.remove(service); |
Svetoslav Ganov | f9886f3 | 2011-05-10 15:28:33 -0700 | [diff] [blame] | 858 | if (!removed) { |
| 859 | return false; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 860 | } |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 861 | userState.mComponentNameToServiceMap.remove(service.mComponentName); |
Svetoslav Ganov | f9886f3 | 2011-05-10 15:28:33 -0700 | [diff] [blame] | 862 | service.unlinkToOwnDeath(); |
Svetoslav Ganov | d116d7c | 2011-11-21 18:41:59 -0800 | [diff] [blame] | 863 | service.dispose(); |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 864 | updateInputFilterLocked(userState); |
Svetoslav Ganov | 9a4c5cd | 2012-05-30 14:06:32 -0700 | [diff] [blame] | 865 | tryDisableTouchExplorationLocked(service); |
Svetoslav Ganov | f9886f3 | 2011-05-10 15:28:33 -0700 | [diff] [blame] | 866 | return removed; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | /** |
| 870 | * Determines if given event can be dispatched to a service based on the package of the |
| 871 | * event source and already notified services for that event type. Specifically, a |
| 872 | * service is notified if it is interested in events from the package and no other service |
| 873 | * providing the same feedback type has been notified. Exception are services the |
| 874 | * provide generic feedback (feedback type left as a safety net for unforeseen feedback |
| 875 | * types) which are always notified. |
| 876 | * |
| 877 | * @param service The potential receiver. |
| 878 | * @param event The event. |
| 879 | * @param handledFeedbackTypes The feedback types for which services have been notified. |
| 880 | * @return True if the listener should be notified, false otherwise. |
| 881 | */ |
| 882 | private boolean canDispathEventLocked(Service service, AccessibilityEvent event, |
| 883 | int handledFeedbackTypes) { |
| 884 | |
Svetoslav Ganov | 1f22b6a | 2012-09-28 09:27:35 -0700 | [diff] [blame] | 885 | if (!service.canReceiveEvents()) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 886 | return false; |
| 887 | } |
| 888 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 889 | if (!event.isImportantForAccessibility() |
| 890 | && !service.mIncludeNotImportantViews) { |
| 891 | return false; |
| 892 | } |
| 893 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 894 | int eventType = event.getEventType(); |
| 895 | if ((service.mEventTypes & eventType) != eventType) { |
| 896 | return false; |
| 897 | } |
| 898 | |
| 899 | Set<String> packageNames = service.mPackageNames; |
| 900 | CharSequence packageName = event.getPackageName(); |
| 901 | |
| 902 | if (packageNames.isEmpty() || packageNames.contains(packageName)) { |
| 903 | int feedbackType = service.mFeedbackType; |
| 904 | if ((handledFeedbackTypes & feedbackType) != feedbackType |
| 905 | || feedbackType == AccessibilityServiceInfo.FEEDBACK_GENERIC) { |
| 906 | return true; |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | return false; |
| 911 | } |
| 912 | |
| 913 | /** |
| 914 | * Manages services by starting enabled ones and stopping disabled ones. |
| 915 | */ |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 916 | private void manageServicesLocked(UserState userState) { |
| 917 | final int enabledInstalledServicesCount = updateServicesStateLocked(userState); |
Svetoslav Ganov | 1e741b2 | 2011-09-20 15:50:07 -0700 | [diff] [blame] | 918 | // No enabled installed services => disable accessibility to avoid |
Svetoslav Ganov | b6eca6e | 2011-09-27 10:47:58 -0700 | [diff] [blame] | 919 | // sending accessibility events with no recipient across processes. |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 920 | if (userState.mIsAccessibilityEnabled && enabledInstalledServicesCount == 0) { |
| 921 | Settings.Secure.putIntForUser(mContext.getContentResolver(), |
| 922 | Settings.Secure.ACCESSIBILITY_ENABLED, 0, userState.mUserId); |
Svetoslav Ganov | 1e741b2 | 2011-09-20 15:50:07 -0700 | [diff] [blame] | 923 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | /** |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 927 | * Unbinds all bound services for a user. |
| 928 | * |
| 929 | * @param userState The user state. |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 930 | */ |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 931 | private void unbindAllServicesLocked(UserState userState) { |
| 932 | List<Service> services = userState.mServices; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 933 | for (int i = 0, count = services.size(); i < count; i++) { |
| 934 | Service service = services.get(i); |
Svetoslav Ganov | 6ff5f10 | 2011-01-10 13:12:55 -0800 | [diff] [blame] | 935 | if (service.unbind()) { |
| 936 | i--; |
| 937 | count--; |
| 938 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 939 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | /** |
Svetoslav Ganov | 9a4c5cd | 2012-05-30 14:06:32 -0700 | [diff] [blame] | 943 | * Populates a set with the {@link ComponentName}s stored in a colon |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 944 | * separated value setting for a given user. |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 945 | * |
Svetoslav Ganov | 9a4c5cd | 2012-05-30 14:06:32 -0700 | [diff] [blame] | 946 | * @param settingName The setting to parse. |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 947 | * @param userId The user id. |
Svetoslav Ganov | 9a4c5cd | 2012-05-30 14:06:32 -0700 | [diff] [blame] | 948 | * @param outComponentNames The output component names. |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 949 | */ |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 950 | private void populateComponentNamesFromSettingLocked(String settingName, int userId, |
Svetoslav Ganov | 9a4c5cd | 2012-05-30 14:06:32 -0700 | [diff] [blame] | 951 | Set<ComponentName> outComponentNames) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 952 | String settingValue = Settings.Secure.getStringForUser(mContext.getContentResolver(), |
| 953 | settingName, userId); |
Svetoslav Ganov | 9a4c5cd | 2012-05-30 14:06:32 -0700 | [diff] [blame] | 954 | outComponentNames.clear(); |
Svetoslav Ganov | 9a4c5cd | 2012-05-30 14:06:32 -0700 | [diff] [blame] | 955 | if (settingValue != null) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 956 | TextUtils.SimpleStringSplitter splitter = mStringColonSplitter; |
Svetoslav Ganov | 9a4c5cd | 2012-05-30 14:06:32 -0700 | [diff] [blame] | 957 | splitter.setString(settingValue); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 958 | while (splitter.hasNext()) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 959 | String str = splitter.next(); |
| 960 | if (str == null || str.length() <= 0) { |
| 961 | continue; |
| 962 | } |
| 963 | ComponentName enabledService = ComponentName.unflattenFromString(str); |
| 964 | if (enabledService != null) { |
Svetoslav Ganov | 9a4c5cd | 2012-05-30 14:06:32 -0700 | [diff] [blame] | 965 | outComponentNames.add(enabledService); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 966 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 967 | } |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | /** |
Svetoslav Ganov | 9a4c5cd | 2012-05-30 14:06:32 -0700 | [diff] [blame] | 972 | * Persists the component names in the specified setting in a |
| 973 | * colon separated fashion. |
| 974 | * |
| 975 | * @param settingName The setting name. |
| 976 | * @param componentNames The component names. |
| 977 | */ |
| 978 | private void persistComponentNamesToSettingLocked(String settingName, |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 979 | Set<ComponentName> componentNames, int userId) { |
Svetoslav Ganov | 9a4c5cd | 2012-05-30 14:06:32 -0700 | [diff] [blame] | 980 | StringBuilder builder = new StringBuilder(); |
| 981 | for (ComponentName componentName : componentNames) { |
| 982 | if (builder.length() > 0) { |
| 983 | builder.append(COMPONENT_NAME_SEPARATOR); |
| 984 | } |
| 985 | builder.append(componentName.flattenToShortString()); |
| 986 | } |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 987 | Settings.Secure.putStringForUser(mContext.getContentResolver(), |
| 988 | settingName, builder.toString(), userId); |
Svetoslav Ganov | 9a4c5cd | 2012-05-30 14:06:32 -0700 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | /** |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 992 | * Updates the state of each service by starting (or keeping running) enabled ones and |
| 993 | * stopping the rest. |
| 994 | * |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 995 | * @param userState The user state for which to do that. |
Svetoslav Ganov | 1e741b2 | 2011-09-20 15:50:07 -0700 | [diff] [blame] | 996 | * @return The number of enabled installed services. |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 997 | */ |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 998 | private int updateServicesStateLocked(UserState userState) { |
| 999 | Map<ComponentName, Service> componentNameToServiceMap = |
| 1000 | userState.mComponentNameToServiceMap; |
| 1001 | boolean isEnabled = userState.mIsAccessibilityEnabled; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1002 | |
Svetoslav Ganov | 1e741b2 | 2011-09-20 15:50:07 -0700 | [diff] [blame] | 1003 | int enabledInstalledServices = 0; |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1004 | for (int i = 0, count = userState.mInstalledServices.size(); i < count; i++) { |
| 1005 | AccessibilityServiceInfo installedService = userState.mInstalledServices.get(i); |
Svetoslav Ganov | cc4053e | 2011-05-23 13:37:44 -0700 | [diff] [blame] | 1006 | ComponentName componentName = ComponentName.unflattenFromString( |
| 1007 | installedService.getId()); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1008 | Service service = componentNameToServiceMap.get(componentName); |
| 1009 | |
Svetoslav Ganov | f2245aa | 2010-12-28 15:48:52 -0800 | [diff] [blame] | 1010 | if (isEnabled) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1011 | if (userState.mEnabledServices.contains(componentName)) { |
Svetoslav Ganov | 563d784 | 2011-01-06 17:40:26 -0800 | [diff] [blame] | 1012 | if (service == null) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1013 | service = new Service(userState.mUserId, componentName, |
| 1014 | installedService, false); |
Svetoslav Ganov | 563d784 | 2011-01-06 17:40:26 -0800 | [diff] [blame] | 1015 | } |
| 1016 | service.bind(); |
Svetoslav Ganov | 1e741b2 | 2011-09-20 15:50:07 -0700 | [diff] [blame] | 1017 | enabledInstalledServices++; |
Svetoslav Ganov | 35bfede | 2011-07-14 17:57:06 -0700 | [diff] [blame] | 1018 | } else { |
Svetoslav Ganov | 563d784 | 2011-01-06 17:40:26 -0800 | [diff] [blame] | 1019 | if (service != null) { |
| 1020 | service.unbind(); |
| 1021 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1022 | } |
| 1023 | } else { |
| 1024 | if (service != null) { |
| 1025 | service.unbind(); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1026 | } |
| 1027 | } |
| 1028 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1029 | |
Svetoslav Ganov | 1e741b2 | 2011-09-20 15:50:07 -0700 | [diff] [blame] | 1030 | return enabledInstalledServices; |
Svetoslav Ganov | 37fedf8 | 2011-09-14 10:44:09 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1033 | private void scheduleSendStateToClientsLocked(UserState userState) { |
| 1034 | if (mGlobalClients.getRegisteredCallbackCount() > 0 |
| 1035 | || userState.mClients.getRegisteredCallbackCount() > 0) { |
| 1036 | final int clientState = getClientState(userState); |
| 1037 | mMainHandler.obtainMessage(MainHandler.MSG_SEND_STATE_TO_CLIENTS, |
| 1038 | clientState, userState.mUserId) .sendToTarget(); |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | private void updateInputFilterLocked(UserState userState) { |
| 1043 | boolean setInputFilter = false; |
| 1044 | AccessibilityInputFilter inputFilter = null; |
| 1045 | synchronized (mLock) { |
| 1046 | if ((userState.mIsAccessibilityEnabled && userState.mIsTouchExplorationEnabled) |
| 1047 | || userState.mIsDisplayMagnificationEnabled) { |
| 1048 | if (!mHasInputFilter) { |
| 1049 | mHasInputFilter = true; |
| 1050 | if (mInputFilter == null) { |
| 1051 | mInputFilter = new AccessibilityInputFilter(mContext, |
| 1052 | AccessibilityManagerService.this); |
| 1053 | } |
| 1054 | inputFilter = mInputFilter; |
| 1055 | setInputFilter = true; |
| 1056 | } |
| 1057 | int flags = 0; |
| 1058 | if (userState.mIsDisplayMagnificationEnabled) { |
| 1059 | flags |= AccessibilityInputFilter.FLAG_FEATURE_SCREEN_MAGNIFIER; |
| 1060 | } |
| 1061 | if (userState.mIsTouchExplorationEnabled) { |
| 1062 | flags |= AccessibilityInputFilter.FLAG_FEATURE_TOUCH_EXPLORATION; |
| 1063 | } |
| 1064 | mInputFilter.setEnabledFeatures(flags); |
| 1065 | } else { |
| 1066 | if (mHasInputFilter) { |
| 1067 | mHasInputFilter = false; |
| 1068 | mInputFilter.setEnabledFeatures(0); |
| 1069 | inputFilter = null; |
| 1070 | setInputFilter = true; |
| 1071 | } |
| 1072 | } |
| 1073 | } |
| 1074 | if (setInputFilter) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1075 | try { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1076 | mWindowManagerService.setInputFilter(inputFilter); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1077 | } catch (RemoteException re) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1078 | /* ignore */ |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1079 | } |
| 1080 | } |
| 1081 | } |
| 1082 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1083 | private void showEnableTouchExplorationDialog(final Service service) { |
| 1084 | String label = service.mResolveInfo.loadLabel( |
| 1085 | mContext.getPackageManager()).toString(); |
| 1086 | synchronized (mLock) { |
| 1087 | final UserState state = getCurrentUserStateLocked(); |
| 1088 | if (state.mIsTouchExplorationEnabled) { |
| 1089 | return; |
| 1090 | } |
| 1091 | if (mEnableTouchExplorationDialog != null |
| 1092 | && mEnableTouchExplorationDialog.isShowing()) { |
| 1093 | return; |
| 1094 | } |
| 1095 | mEnableTouchExplorationDialog = new AlertDialog.Builder(mContext) |
| 1096 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 1097 | .setPositiveButton(android.R.string.ok, new OnClickListener() { |
| 1098 | @Override |
| 1099 | public void onClick(DialogInterface dialog, int which) { |
| 1100 | // The user allowed the service to toggle touch exploration. |
| 1101 | state.mTouchExplorationGrantedServices.add(service.mComponentName); |
| 1102 | persistComponentNamesToSettingLocked( |
| 1103 | Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES, |
| 1104 | state.mTouchExplorationGrantedServices, state.mUserId); |
| 1105 | // Enable touch exploration. |
| 1106 | Settings.Secure.putIntForUser(mContext.getContentResolver(), |
| 1107 | Settings.Secure.TOUCH_EXPLORATION_ENABLED, 1, |
| 1108 | service.mUserId); |
| 1109 | } |
| 1110 | }) |
| 1111 | .setNegativeButton(android.R.string.cancel, new OnClickListener() { |
| 1112 | @Override |
| 1113 | public void onClick(DialogInterface dialog, int which) { |
| 1114 | dialog.dismiss(); |
| 1115 | } |
| 1116 | }) |
| 1117 | .setTitle(R.string.enable_explore_by_touch_warning_title) |
| 1118 | .setMessage(mContext.getString( |
| 1119 | R.string.enable_explore_by_touch_warning_message, label)) |
| 1120 | .create(); |
| 1121 | mEnableTouchExplorationDialog.getWindow().setType( |
| 1122 | WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG); |
| 1123 | mEnableTouchExplorationDialog.setCanceledOnTouchOutside(true); |
| 1124 | mEnableTouchExplorationDialog.show(); |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | private int getClientState(UserState userState) { |
| 1129 | int clientState = 0; |
| 1130 | if (userState.mIsAccessibilityEnabled) { |
| 1131 | clientState |= AccessibilityManager.STATE_FLAG_ACCESSIBILITY_ENABLED; |
Svetoslav Ganov | 35bfede | 2011-07-14 17:57:06 -0700 | [diff] [blame] | 1132 | } |
Svetoslav Ganov | 00aabf7 | 2011-07-21 11:35:03 -0700 | [diff] [blame] | 1133 | // Touch exploration relies on enabled accessibility. |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1134 | if (userState.mIsAccessibilityEnabled && userState.mIsTouchExplorationEnabled) { |
| 1135 | clientState |= AccessibilityManager.STATE_FLAG_TOUCH_EXPLORATION_ENABLED; |
Svetoslav Ganov | 00aabf7 | 2011-07-21 11:35:03 -0700 | [diff] [blame] | 1136 | } |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1137 | return clientState; |
Svetoslav Ganov | 35bfede | 2011-07-14 17:57:06 -0700 | [diff] [blame] | 1138 | } |
| 1139 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1140 | private void recreateInternalStateLocked(UserState userState) { |
| 1141 | populateInstalledAccessibilityServiceLocked(userState); |
| 1142 | populateEnabledAccessibilityServicesLocked(userState); |
| 1143 | populateTouchExplorationGrantedAccessibilityServicesLocked(userState); |
| 1144 | |
| 1145 | handleTouchExplorationEnabledSettingChangedLocked(userState); |
| 1146 | handleDisplayMagnificationEnabledSettingChangedLocked(userState); |
| 1147 | handleAccessibilityEnabledSettingChangedLocked(userState); |
| 1148 | |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 1149 | performServiceManagementLocked(userState); |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1150 | updateInputFilterLocked(userState); |
| 1151 | scheduleSendStateToClientsLocked(userState); |
Svetoslav Ganov | 1cf70bb | 2012-08-06 10:53:34 -0700 | [diff] [blame] | 1152 | } |
| 1153 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1154 | private void handleAccessibilityEnabledSettingChangedLocked(UserState userState) { |
| 1155 | userState.mIsAccessibilityEnabled = Settings.Secure.getIntForUser( |
| 1156 | mContext.getContentResolver(), |
| 1157 | Settings.Secure.ACCESSIBILITY_ENABLED, 0, userState.mUserId) == 1; |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | private void performServiceManagementLocked(UserState userState) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1161 | if (userState.mIsAccessibilityEnabled ) { |
| 1162 | manageServicesLocked(userState); |
Svetoslav Ganov | b6eca6e | 2011-09-27 10:47:58 -0700 | [diff] [blame] | 1163 | } else { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1164 | unbindAllServicesLocked(userState); |
Svetoslav Ganov | b6eca6e | 2011-09-27 10:47:58 -0700 | [diff] [blame] | 1165 | } |
Svetoslav Ganov | 0d04e24 | 2012-02-21 13:46:36 -0800 | [diff] [blame] | 1166 | } |
| 1167 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1168 | private void handleTouchExplorationEnabledSettingChangedLocked(UserState userState) { |
| 1169 | userState.mIsTouchExplorationEnabled = Settings.Secure.getIntForUser( |
Svetoslav Ganov | 0d04e24 | 2012-02-21 13:46:36 -0800 | [diff] [blame] | 1170 | mContext.getContentResolver(), |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1171 | Settings.Secure.TOUCH_EXPLORATION_ENABLED, 0, userState.mUserId) == 1; |
Svetoslav Ganov | b6eca6e | 2011-09-27 10:47:58 -0700 | [diff] [blame] | 1172 | } |
| 1173 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1174 | private void handleDisplayMagnificationEnabledSettingChangedLocked(UserState userState) { |
| 1175 | userState.mIsDisplayMagnificationEnabled = Settings.Secure.getIntForUser( |
Svetoslav Ganov | 1cf70bb | 2012-08-06 10:53:34 -0700 | [diff] [blame] | 1176 | mContext.getContentResolver(), |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1177 | Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED, |
| 1178 | 0, userState.mUserId) == 1; |
Svetoslav Ganov | 1cf70bb | 2012-08-06 10:53:34 -0700 | [diff] [blame] | 1179 | } |
| 1180 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1181 | private void handleTouchExplorationGrantedAccessibilityServicesChangedLocked( |
| 1182 | UserState userState) { |
| 1183 | final int serviceCount = userState.mServices.size(); |
Svetoslav Ganov | a43ef3d | 2012-07-12 13:07:55 -0700 | [diff] [blame] | 1184 | for (int i = 0; i < serviceCount; i++) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1185 | Service service = userState.mServices.get(i); |
Svetoslav Ganov | a43ef3d | 2012-07-12 13:07:55 -0700 | [diff] [blame] | 1186 | if (service.mRequestTouchExplorationMode |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1187 | && userState.mTouchExplorationGrantedServices.contains( |
| 1188 | service.mComponentName)) { |
Svetoslav Ganov | a43ef3d | 2012-07-12 13:07:55 -0700 | [diff] [blame] | 1189 | tryEnableTouchExplorationLocked(service); |
| 1190 | return; |
| 1191 | } |
| 1192 | } |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1193 | if (userState.mIsTouchExplorationEnabled) { |
| 1194 | Settings.Secure.putIntForUser(mContext.getContentResolver(), |
| 1195 | Settings.Secure.TOUCH_EXPLORATION_ENABLED, 0, userState.mUserId); |
Svetoslav Ganov | a43ef3d | 2012-07-12 13:07:55 -0700 | [diff] [blame] | 1196 | } |
| 1197 | } |
| 1198 | |
Svetoslav Ganov | 9a4c5cd | 2012-05-30 14:06:32 -0700 | [diff] [blame] | 1199 | private void tryEnableTouchExplorationLocked(final Service service) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1200 | UserState userState = getUserStateLocked(service.mUserId); |
Svetoslav Ganov | 1f22b6a | 2012-09-28 09:27:35 -0700 | [diff] [blame] | 1201 | if (!userState.mIsTouchExplorationEnabled && service.mRequestTouchExplorationMode |
| 1202 | && service.canReceiveEvents()) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1203 | final boolean canToggleTouchExploration = |
| 1204 | userState.mTouchExplorationGrantedServices.contains(service.mComponentName); |
Svetoslav Ganov | 9a4c5cd | 2012-05-30 14:06:32 -0700 | [diff] [blame] | 1205 | if (!service.mIsAutomation && !canToggleTouchExploration) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1206 | showEnableTouchExplorationDialog(service); |
Svetoslav Ganov | 4074e8a | 2012-05-23 11:52:36 -0700 | [diff] [blame] | 1207 | } else { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1208 | Settings.Secure.putIntForUser(mContext.getContentResolver(), |
| 1209 | Settings.Secure.TOUCH_EXPLORATION_ENABLED, 1, userState.mUserId); |
Svetoslav Ganov | 4074e8a | 2012-05-23 11:52:36 -0700 | [diff] [blame] | 1210 | } |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 1211 | } |
| 1212 | } |
| 1213 | |
Svetoslav Ganov | 9a4c5cd | 2012-05-30 14:06:32 -0700 | [diff] [blame] | 1214 | private void tryDisableTouchExplorationLocked(Service service) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1215 | UserState userState = getUserStateLocked(service.mUserId); |
| 1216 | if (userState.mIsTouchExplorationEnabled) { |
| 1217 | final int serviceCount = userState.mServices.size(); |
| 1218 | for (int i = 0; i < serviceCount; i++) { |
| 1219 | Service other = userState.mServices.get(i); |
| 1220 | if (other != service && other.mRequestTouchExplorationMode) { |
| 1221 | return; |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 1222 | } |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 1223 | } |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1224 | Settings.Secure.putIntForUser(mContext.getContentResolver(), |
| 1225 | Settings.Secure.TOUCH_EXPLORATION_ENABLED, 0, userState.mUserId); |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 1226 | } |
| 1227 | } |
| 1228 | |
Svetoslav Ganov | d116d7c | 2011-11-21 18:41:59 -0800 | [diff] [blame] | 1229 | private class AccessibilityConnectionWrapper implements DeathRecipient { |
| 1230 | private final int mWindowId; |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1231 | private final int mUserId; |
Svetoslav Ganov | d116d7c | 2011-11-21 18:41:59 -0800 | [diff] [blame] | 1232 | private final IAccessibilityInteractionConnection mConnection; |
| 1233 | |
| 1234 | public AccessibilityConnectionWrapper(int windowId, |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1235 | IAccessibilityInteractionConnection connection, int userId) { |
Svetoslav Ganov | d116d7c | 2011-11-21 18:41:59 -0800 | [diff] [blame] | 1236 | mWindowId = windowId; |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1237 | mUserId = userId; |
Svetoslav Ganov | d116d7c | 2011-11-21 18:41:59 -0800 | [diff] [blame] | 1238 | mConnection = connection; |
| 1239 | } |
| 1240 | |
| 1241 | public void linkToDeath() throws RemoteException { |
| 1242 | mConnection.asBinder().linkToDeath(this, 0); |
| 1243 | } |
| 1244 | |
| 1245 | public void unlinkToDeath() { |
| 1246 | mConnection.asBinder().unlinkToDeath(this, 0); |
| 1247 | } |
| 1248 | |
| 1249 | @Override |
| 1250 | public void binderDied() { |
| 1251 | unlinkToDeath(); |
| 1252 | synchronized (mLock) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1253 | removeAccessibilityInteractionConnectionLocked(mWindowId, mUserId); |
Svetoslav Ganov | d116d7c | 2011-11-21 18:41:59 -0800 | [diff] [blame] | 1254 | } |
| 1255 | } |
| 1256 | } |
| 1257 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1258 | private final class MainHandler extends Handler { |
| 1259 | public static final int MSG_SEND_ACCESSIBILITY_EVENT_TO_INPUT_FILTER = 1; |
| 1260 | public static final int MSG_SEND_STATE_TO_CLIENTS = 2; |
| 1261 | public static final int MSG_SEND_CLEARED_STATE_TO_CLIENTS_FOR_USER = 3; |
| 1262 | public static final int MSG_SEND_RECREATE_INTERNAL_STATE = 4; |
Svetoslav Ganov | a8afa69 | 2012-09-25 13:03:18 -0700 | [diff] [blame] | 1263 | public static final int MSG_UPDATE_ACTIVE_WINDOW = 5; |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 1264 | public static final int MSG_ANNOUNCE_NEW_USER_IF_NEEDED = 6; |
Svetoslav Ganov | 1cf70bb | 2012-08-06 10:53:34 -0700 | [diff] [blame] | 1265 | |
| 1266 | public MainHandler(Looper looper) { |
| 1267 | super(looper); |
| 1268 | } |
| 1269 | |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 1270 | @Override |
| 1271 | public void handleMessage(Message msg) { |
| 1272 | final int type = msg.what; |
| 1273 | switch (type) { |
Svetoslav Ganov | e45c0b2 | 2012-06-08 17:44:29 -0700 | [diff] [blame] | 1274 | case MSG_SEND_ACCESSIBILITY_EVENT_TO_INPUT_FILTER: { |
| 1275 | AccessibilityEvent event = (AccessibilityEvent) msg.obj; |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1276 | synchronized (mLock) { |
| 1277 | if (mHasInputFilter && mInputFilter != null) { |
| 1278 | mInputFilter.notifyAccessibilityEvent(event); |
| 1279 | } |
Svetoslav Ganov | e45c0b2 | 2012-06-08 17:44:29 -0700 | [diff] [blame] | 1280 | } |
| 1281 | event.recycle(); |
| 1282 | } break; |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1283 | case MSG_SEND_STATE_TO_CLIENTS: { |
| 1284 | final int clientState = msg.arg1; |
| 1285 | final int userId = msg.arg2; |
| 1286 | sendStateToClients(clientState, mGlobalClients); |
| 1287 | sendStateToClientsForUser(clientState, userId); |
| 1288 | } break; |
| 1289 | case MSG_SEND_CLEARED_STATE_TO_CLIENTS_FOR_USER: { |
| 1290 | final int userId = msg.arg1; |
| 1291 | sendStateToClientsForUser(0, userId); |
| 1292 | } break; |
| 1293 | case MSG_SEND_RECREATE_INTERNAL_STATE: { |
| 1294 | final int userId = msg.arg1; |
Svetoslav Ganov | 1cf70bb | 2012-08-06 10:53:34 -0700 | [diff] [blame] | 1295 | synchronized (mLock) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1296 | UserState userState = getUserStateLocked(userId); |
| 1297 | recreateInternalStateLocked(userState); |
Svetoslav Ganov | 1cf70bb | 2012-08-06 10:53:34 -0700 | [diff] [blame] | 1298 | } |
| 1299 | } break; |
Svetoslav Ganov | a8afa69 | 2012-09-25 13:03:18 -0700 | [diff] [blame] | 1300 | case MSG_UPDATE_ACTIVE_WINDOW: { |
| 1301 | final int windowId = msg.arg1; |
| 1302 | final int eventType = msg.arg2; |
| 1303 | mSecurityPolicy.updateActiveWindow(windowId, eventType); |
| 1304 | } break; |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 1305 | case MSG_ANNOUNCE_NEW_USER_IF_NEEDED: { |
| 1306 | announceNewUserIfNeeded(); |
| 1307 | } break; |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | private void announceNewUserIfNeeded() { |
| 1312 | synchronized (mLock) { |
| 1313 | UserState userState = getCurrentUserStateLocked(); |
| 1314 | if (userState.mIsAccessibilityEnabled) { |
| 1315 | UserManager userManager = (UserManager) mContext.getSystemService( |
| 1316 | Context.USER_SERVICE); |
| 1317 | String message = mContext.getString(R.string.user_switched, |
| 1318 | userManager.getUserInfo(mCurrentUserId).name); |
| 1319 | AccessibilityEvent event = AccessibilityEvent.obtain( |
| 1320 | AccessibilityEvent.TYPE_ANNOUNCEMENT); |
| 1321 | event.getText().add(message); |
| 1322 | sendAccessibilityEvent(event, mCurrentUserId); |
| 1323 | } |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1324 | } |
| 1325 | } |
| 1326 | |
| 1327 | private void sendStateToClientsForUser(int clientState, int userId) { |
| 1328 | final UserState userState; |
| 1329 | synchronized (mLock) { |
| 1330 | userState = getUserStateLocked(userId); |
| 1331 | } |
| 1332 | sendStateToClients(clientState, userState.mClients); |
| 1333 | } |
| 1334 | |
| 1335 | private void sendStateToClients(int clientState, |
| 1336 | RemoteCallbackList<IAccessibilityManagerClient> clients) { |
| 1337 | try { |
| 1338 | final int userClientCount = clients.beginBroadcast(); |
| 1339 | for (int i = 0; i < userClientCount; i++) { |
| 1340 | IAccessibilityManagerClient client = clients.getBroadcastItem(i); |
| 1341 | try { |
| 1342 | client.setState(clientState); |
| 1343 | } catch (RemoteException re) { |
| 1344 | /* ignore */ |
| 1345 | } |
| 1346 | } |
| 1347 | } finally { |
| 1348 | clients.finishBroadcast(); |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 1349 | } |
| 1350 | } |
| 1351 | } |
| 1352 | |
Svetoslav Ganov | b6eca6e | 2011-09-27 10:47:58 -0700 | [diff] [blame] | 1353 | /** |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1354 | * This class represents an accessibility service. It stores all per service |
| 1355 | * data required for the service management, provides API for starting/stopping the |
| 1356 | * service and is responsible for adding/removing the service in the data structures |
| 1357 | * for service management. The class also exposes configuration interface that is |
| 1358 | * passed to the service it represents as soon it is bound. It also serves as the |
| 1359 | * connection for the service. |
| 1360 | */ |
Svetoslav Ganov | f9886f3 | 2011-05-10 15:28:33 -0700 | [diff] [blame] | 1361 | class Service extends IAccessibilityServiceConnection.Stub |
| 1362 | implements ServiceConnection, DeathRecipient { |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 1363 | |
| 1364 | // We pick the MSB to avoid collision since accessibility event types are |
| 1365 | // used as message types allowing us to remove messages per event type. |
| 1366 | private static final int MSG_ON_GESTURE = 0x80000000; |
| 1367 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1368 | final int mUserId; |
| 1369 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1370 | int mId = 0; |
| 1371 | |
Svetoslav Ganov | cc4053e | 2011-05-23 13:37:44 -0700 | [diff] [blame] | 1372 | AccessibilityServiceInfo mAccessibilityServiceInfo; |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 1373 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1374 | IBinder mService; |
| 1375 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1376 | IAccessibilityServiceClient mServiceInterface; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1377 | |
| 1378 | int mEventTypes; |
| 1379 | |
| 1380 | int mFeedbackType; |
| 1381 | |
| 1382 | Set<String> mPackageNames = new HashSet<String>(); |
| 1383 | |
| 1384 | boolean mIsDefault; |
| 1385 | |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 1386 | boolean mRequestTouchExplorationMode; |
| 1387 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1388 | boolean mIncludeNotImportantViews; |
| 1389 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1390 | long mNotificationTimeout; |
| 1391 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1392 | ComponentName mComponentName; |
| 1393 | |
| 1394 | Intent mIntent; |
| 1395 | |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1396 | boolean mCanRetrieveScreenContent; |
| 1397 | |
Svetoslav Ganov | e8f9535 | 2011-07-06 17:24:03 -0700 | [diff] [blame] | 1398 | boolean mIsAutomation; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1399 | |
Svetoslav Ganov | 7961be7 | 2011-06-21 12:31:56 -0700 | [diff] [blame] | 1400 | final Rect mTempBounds = new Rect(); |
| 1401 | |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 1402 | final ResolveInfo mResolveInfo; |
| 1403 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1404 | // the events pending events to be dispatched to this service |
| 1405 | final SparseArray<AccessibilityEvent> mPendingEvents = |
| 1406 | new SparseArray<AccessibilityEvent>(); |
| 1407 | |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 1408 | /** |
| 1409 | * Handler for delayed event dispatch. |
| 1410 | */ |
Svetoslav Ganov | ec2c171 | 2012-05-22 11:31:57 -0700 | [diff] [blame] | 1411 | public Handler mHandler = new Handler(mMainHandler.getLooper()) { |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 1412 | @Override |
| 1413 | public void handleMessage(Message message) { |
| 1414 | final int type = message.what; |
| 1415 | switch (type) { |
| 1416 | case MSG_ON_GESTURE: { |
| 1417 | final int gestureId = message.arg1; |
| 1418 | notifyGestureInternal(gestureId); |
| 1419 | } break; |
| 1420 | default: { |
| 1421 | final int eventType = type; |
| 1422 | notifyAccessibilityEventInternal(eventType); |
| 1423 | } break; |
| 1424 | } |
| 1425 | } |
| 1426 | }; |
| 1427 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1428 | public Service(int userId, ComponentName componentName, |
Svetoslav Ganov | e8f9535 | 2011-07-06 17:24:03 -0700 | [diff] [blame] | 1429 | AccessibilityServiceInfo accessibilityServiceInfo, boolean isAutomation) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1430 | mUserId = userId; |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 1431 | mResolveInfo = accessibilityServiceInfo.getResolveInfo(); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1432 | mId = sIdCounter++; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1433 | mComponentName = componentName; |
| 1434 | mAccessibilityServiceInfo = accessibilityServiceInfo; |
Svetoslav Ganov | e8f9535 | 2011-07-06 17:24:03 -0700 | [diff] [blame] | 1435 | mIsAutomation = isAutomation; |
| 1436 | if (!isAutomation) { |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1437 | mCanRetrieveScreenContent = accessibilityServiceInfo.getCanRetrieveWindowContent(); |
Svetoslav Ganov | a43ef3d | 2012-07-12 13:07:55 -0700 | [diff] [blame] | 1438 | mRequestTouchExplorationMode = |
Svetoslav Ganov | e4abc51 | 2012-05-09 11:02:38 -0700 | [diff] [blame] | 1439 | (accessibilityServiceInfo.flags |
| 1440 | & AccessibilityServiceInfo.FLAG_REQUEST_TOUCH_EXPLORATION_MODE) != 0; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1441 | mIntent = new Intent().setComponent(mComponentName); |
| 1442 | mIntent.putExtra(Intent.EXTRA_CLIENT_LABEL, |
| 1443 | com.android.internal.R.string.accessibility_binding_label); |
| 1444 | mIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity( |
| 1445 | mContext, 0, new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS), 0)); |
| 1446 | } else { |
| 1447 | mCanRetrieveScreenContent = true; |
| 1448 | } |
| 1449 | setDynamicallyConfigurableProperties(accessibilityServiceInfo); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1450 | } |
| 1451 | |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1452 | public void setDynamicallyConfigurableProperties(AccessibilityServiceInfo info) { |
Svetoslav Ganov | cc4053e | 2011-05-23 13:37:44 -0700 | [diff] [blame] | 1453 | mEventTypes = info.eventTypes; |
| 1454 | mFeedbackType = info.feedbackType; |
| 1455 | String[] packageNames = info.packageNames; |
| 1456 | if (packageNames != null) { |
| 1457 | mPackageNames.addAll(Arrays.asList(packageNames)); |
| 1458 | } |
| 1459 | mNotificationTimeout = info.notificationTimeout; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1460 | mIsDefault = (info.flags & DEFAULT) != 0; |
| 1461 | |
Guang Zhu | df549f8 | 2012-05-08 23:09:24 -0700 | [diff] [blame] | 1462 | if (mIsAutomation || info.getResolveInfo().serviceInfo.applicationInfo.targetSdkVersion |
Svetoslav Ganov | 5a48f97 | 2012-05-13 13:33:53 -0700 | [diff] [blame] | 1463 | >= Build.VERSION_CODES.JELLY_BEAN) { |
Guang Zhu | df549f8 | 2012-05-08 23:09:24 -0700 | [diff] [blame] | 1464 | mIncludeNotImportantViews = |
Svetoslav Ganov | e4abc51 | 2012-05-09 11:02:38 -0700 | [diff] [blame] | 1465 | (info.flags & FLAG_INCLUDE_NOT_IMPORTANT_VIEWS) != 0; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1466 | } |
Svetoslav Ganov | c321c19 | 2011-06-03 19:43:23 -0700 | [diff] [blame] | 1467 | |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 1468 | mRequestTouchExplorationMode = (info.flags |
| 1469 | & AccessibilityServiceInfo.FLAG_REQUEST_TOUCH_EXPLORATION_MODE) != 0; |
| 1470 | |
Svetoslav Ganov | 9a4c5cd | 2012-05-30 14:06:32 -0700 | [diff] [blame] | 1471 | // If this service is up and running we may have to enable touch |
| 1472 | // exploration, otherwise this will happen when the service connects. |
Svetoslav Ganov | c321c19 | 2011-06-03 19:43:23 -0700 | [diff] [blame] | 1473 | synchronized (mLock) { |
Svetoslav Ganov | 1f22b6a | 2012-09-28 09:27:35 -0700 | [diff] [blame] | 1474 | if (canReceiveEvents()) { |
Svetoslav Ganov | 9a4c5cd | 2012-05-30 14:06:32 -0700 | [diff] [blame] | 1475 | if (mRequestTouchExplorationMode) { |
| 1476 | tryEnableTouchExplorationLocked(this); |
| 1477 | } else { |
| 1478 | tryDisableTouchExplorationLocked(this); |
| 1479 | } |
| 1480 | } |
Svetoslav Ganov | c321c19 | 2011-06-03 19:43:23 -0700 | [diff] [blame] | 1481 | } |
Svetoslav Ganov | cc4053e | 2011-05-23 13:37:44 -0700 | [diff] [blame] | 1482 | } |
| 1483 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1484 | /** |
| 1485 | * Binds to the accessibility service. |
Svetoslav Ganov | 6ff5f10 | 2011-01-10 13:12:55 -0800 | [diff] [blame] | 1486 | * |
| 1487 | * @return True if binding is successful. |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1488 | */ |
Svetoslav Ganov | 6ff5f10 | 2011-01-10 13:12:55 -0800 | [diff] [blame] | 1489 | public boolean bind() { |
Svetoslav Ganov | e8f9535 | 2011-07-06 17:24:03 -0700 | [diff] [blame] | 1490 | if (!mIsAutomation && mService == null) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1491 | return mContext.bindService(mIntent, this, Context.BIND_AUTO_CREATE, mUserId); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1492 | } |
Svetoslav Ganov | 6ff5f10 | 2011-01-10 13:12:55 -0800 | [diff] [blame] | 1493 | return false; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | /** |
| 1497 | * Unbinds form the accessibility service and removes it from the data |
| 1498 | * structures for service management. |
Svetoslav Ganov | 6ff5f10 | 2011-01-10 13:12:55 -0800 | [diff] [blame] | 1499 | * |
| 1500 | * @return True if unbinding is successful. |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1501 | */ |
Svetoslav Ganov | 6ff5f10 | 2011-01-10 13:12:55 -0800 | [diff] [blame] | 1502 | public boolean unbind() { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1503 | if (mService != null) { |
Svetoslav Ganov | c321c19 | 2011-06-03 19:43:23 -0700 | [diff] [blame] | 1504 | synchronized (mLock) { |
| 1505 | tryRemoveServiceLocked(this); |
| 1506 | } |
Svetoslav Ganov | e8f9535 | 2011-07-06 17:24:03 -0700 | [diff] [blame] | 1507 | if (!mIsAutomation) { |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1508 | mContext.unbindService(this); |
| 1509 | } |
Svetoslav Ganov | 6ff5f10 | 2011-01-10 13:12:55 -0800 | [diff] [blame] | 1510 | return true; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1511 | } |
Svetoslav Ganov | 6ff5f10 | 2011-01-10 13:12:55 -0800 | [diff] [blame] | 1512 | return false; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1513 | } |
| 1514 | |
Svetoslav Ganov | 1f22b6a | 2012-09-28 09:27:35 -0700 | [diff] [blame] | 1515 | public boolean canReceiveEvents() { |
Svetoslav Ganov | c321c19 | 2011-06-03 19:43:23 -0700 | [diff] [blame] | 1516 | return (mEventTypes != 0 && mFeedbackType != 0 && mService != null); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1517 | } |
| 1518 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1519 | @Override |
| 1520 | public AccessibilityServiceInfo getServiceInfo() { |
| 1521 | synchronized (mLock) { |
| 1522 | return mAccessibilityServiceInfo; |
| 1523 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1524 | } |
| 1525 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1526 | @Override |
| 1527 | public void setServiceInfo(AccessibilityServiceInfo info) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1528 | final long identity = Binder.clearCallingIdentity(); |
| 1529 | try { |
| 1530 | synchronized (mLock) { |
| 1531 | // If the XML manifest had data to configure the service its info |
| 1532 | // should be already set. In such a case update only the dynamically |
| 1533 | // configurable properties. |
| 1534 | AccessibilityServiceInfo oldInfo = mAccessibilityServiceInfo; |
| 1535 | if (oldInfo != null) { |
| 1536 | oldInfo.updateDynamicallyConfigurableProperties(info); |
| 1537 | setDynamicallyConfigurableProperties(oldInfo); |
| 1538 | } else { |
| 1539 | setDynamicallyConfigurableProperties(info); |
| 1540 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1541 | } |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1542 | } finally { |
| 1543 | Binder.restoreCallingIdentity(identity); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1544 | } |
| 1545 | } |
| 1546 | |
| 1547 | @Override |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1548 | public void onServiceConnected(ComponentName componentName, IBinder service) { |
| 1549 | mService = service; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1550 | mServiceInterface = IAccessibilityServiceClient.Stub.asInterface(service); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1551 | try { |
Svetoslav Ganov | d116d7c | 2011-11-21 18:41:59 -0800 | [diff] [blame] | 1552 | mServiceInterface.setConnection(this, mId); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1553 | synchronized (mLock) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1554 | tryAddServiceLocked(this, mUserId); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1555 | } |
| 1556 | } catch (RemoteException re) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1557 | Slog.w(LOG_TAG, "Error while setting Controller for service: " + service, re); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1558 | } |
| 1559 | } |
| 1560 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1561 | @Override |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 1562 | public float findAccessibilityNodeInfoByViewId(int accessibilityWindowId, |
| 1563 | long accessibilityNodeId, int viewId, int interactionId, |
| 1564 | IAccessibilityInteractionConnectionCallback callback, long interrogatingTid) |
Svetoslav Ganov | 4a49d9f | 2011-07-17 12:22:08 -0700 | [diff] [blame] | 1565 | throws RemoteException { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1566 | final int resolvedWindowId; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1567 | IAccessibilityInteractionConnection connection = null; |
| 1568 | synchronized (mLock) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1569 | final int resolvedUserId = mSecurityPolicy |
| 1570 | .resolveCallingUserIdEnforcingPermissionsLocked( |
| 1571 | UserHandle.getCallingUserId()); |
| 1572 | if (resolvedUserId != mCurrentUserId) { |
| 1573 | return -1; |
| 1574 | } |
Svetoslav Ganov | 4a49d9f | 2011-07-17 12:22:08 -0700 | [diff] [blame] | 1575 | mSecurityPolicy.enforceCanRetrieveWindowContent(this); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1576 | final boolean permissionGranted = mSecurityPolicy.canRetrieveWindowContent(this); |
Svetoslav Ganov | e8f9535 | 2011-07-06 17:24:03 -0700 | [diff] [blame] | 1577 | if (!permissionGranted) { |
Svetoslav Ganov | 8bd6961 | 2011-08-23 13:40:30 -0700 | [diff] [blame] | 1578 | return 0; |
Svetoslav Ganov | e8f9535 | 2011-07-06 17:24:03 -0700 | [diff] [blame] | 1579 | } else { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1580 | resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId); |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 1581 | connection = getConnectionLocked(resolvedWindowId); |
Svetoslav Ganov | e8f9535 | 2011-07-06 17:24:03 -0700 | [diff] [blame] | 1582 | if (connection == null) { |
Svetoslav Ganov | 8bd6961 | 2011-08-23 13:40:30 -0700 | [diff] [blame] | 1583 | return 0; |
Svetoslav Ganov | e8f9535 | 2011-07-06 17:24:03 -0700 | [diff] [blame] | 1584 | } |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1585 | } |
| 1586 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1587 | final int flags = (mIncludeNotImportantViews) ? |
| 1588 | AccessibilityNodeInfo.INCLUDE_NOT_IMPORTANT_VIEWS : 0; |
Svetoslav Ganov | 8bd6961 | 2011-08-23 13:40:30 -0700 | [diff] [blame] | 1589 | final int interrogatingPid = Binder.getCallingPid(); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1590 | final long identityToken = Binder.clearCallingIdentity(); |
| 1591 | try { |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 1592 | connection.findAccessibilityNodeInfoByViewId(accessibilityNodeId, viewId, |
Svetoslav Ganov | c9c9a48 | 2012-07-16 08:46:07 -0700 | [diff] [blame] | 1593 | interactionId, callback, flags, interrogatingPid, interrogatingTid); |
| 1594 | return getCompatibilityScale(resolvedWindowId); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1595 | } catch (RemoteException re) { |
| 1596 | if (DEBUG) { |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 1597 | Slog.e(LOG_TAG, "Error findAccessibilityNodeInfoByViewId()."); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1598 | } |
| 1599 | } finally { |
| 1600 | Binder.restoreCallingIdentity(identityToken); |
| 1601 | } |
Svetoslav Ganov | c9c9a48 | 2012-07-16 08:46:07 -0700 | [diff] [blame] | 1602 | return 0; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1603 | } |
| 1604 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1605 | @Override |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 1606 | public float findAccessibilityNodeInfosByText(int accessibilityWindowId, |
| 1607 | long accessibilityNodeId, String text, int interactionId, |
Svetoslav Ganov | 8bd6961 | 2011-08-23 13:40:30 -0700 | [diff] [blame] | 1608 | IAccessibilityInteractionConnectionCallback callback, long interrogatingTid) |
| 1609 | throws RemoteException { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1610 | final int resolvedWindowId; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1611 | IAccessibilityInteractionConnection connection = null; |
| 1612 | synchronized (mLock) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1613 | final int resolvedUserId = mSecurityPolicy |
| 1614 | .resolveCallingUserIdEnforcingPermissionsLocked( |
| 1615 | UserHandle.getCallingUserId()); |
| 1616 | if (resolvedUserId != mCurrentUserId) { |
| 1617 | return -1; |
| 1618 | } |
Svetoslav Ganov | 4a49d9f | 2011-07-17 12:22:08 -0700 | [diff] [blame] | 1619 | mSecurityPolicy.enforceCanRetrieveWindowContent(this); |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1620 | resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId); |
Svetoslav Ganov | eeee4d2 | 2011-06-10 20:51:30 -0700 | [diff] [blame] | 1621 | final boolean permissionGranted = |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 1622 | mSecurityPolicy.canGetAccessibilityNodeInfoLocked(this, resolvedWindowId); |
Svetoslav Ganov | e8f9535 | 2011-07-06 17:24:03 -0700 | [diff] [blame] | 1623 | if (!permissionGranted) { |
Svetoslav Ganov | 8bd6961 | 2011-08-23 13:40:30 -0700 | [diff] [blame] | 1624 | return 0; |
Svetoslav Ganov | e8f9535 | 2011-07-06 17:24:03 -0700 | [diff] [blame] | 1625 | } else { |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 1626 | connection = getConnectionLocked(resolvedWindowId); |
Svetoslav Ganov | e8f9535 | 2011-07-06 17:24:03 -0700 | [diff] [blame] | 1627 | if (connection == null) { |
Svetoslav Ganov | 8bd6961 | 2011-08-23 13:40:30 -0700 | [diff] [blame] | 1628 | return 0; |
Svetoslav Ganov | e8f9535 | 2011-07-06 17:24:03 -0700 | [diff] [blame] | 1629 | } |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1630 | } |
| 1631 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1632 | final int flags = (mIncludeNotImportantViews) ? |
| 1633 | AccessibilityNodeInfo.INCLUDE_NOT_IMPORTANT_VIEWS : 0; |
Svetoslav Ganov | 8bd6961 | 2011-08-23 13:40:30 -0700 | [diff] [blame] | 1634 | final int interrogatingPid = Binder.getCallingPid(); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1635 | final long identityToken = Binder.clearCallingIdentity(); |
| 1636 | try { |
Svetoslav Ganov | c9c9a48 | 2012-07-16 08:46:07 -0700 | [diff] [blame] | 1637 | connection.findAccessibilityNodeInfosByText(accessibilityNodeId, text, |
| 1638 | interactionId, callback, flags, interrogatingPid, |
Svetoslav Ganov | 8678347 | 2012-06-06 21:12:20 -0700 | [diff] [blame] | 1639 | interrogatingTid); |
Svetoslav Ganov | c9c9a48 | 2012-07-16 08:46:07 -0700 | [diff] [blame] | 1640 | return getCompatibilityScale(resolvedWindowId); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1641 | } catch (RemoteException re) { |
| 1642 | if (DEBUG) { |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 1643 | Slog.e(LOG_TAG, "Error calling findAccessibilityNodeInfosByText()"); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1644 | } |
| 1645 | } finally { |
| 1646 | Binder.restoreCallingIdentity(identityToken); |
| 1647 | } |
Svetoslav Ganov | c9c9a48 | 2012-07-16 08:46:07 -0700 | [diff] [blame] | 1648 | return 0; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1649 | } |
| 1650 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1651 | @Override |
Svetoslav Ganov | 8bd6961 | 2011-08-23 13:40:30 -0700 | [diff] [blame] | 1652 | public float findAccessibilityNodeInfoByAccessibilityId(int accessibilityWindowId, |
Svetoslav Ganov | 0210785 | 2011-10-03 17:06:56 -0700 | [diff] [blame] | 1653 | long accessibilityNodeId, int interactionId, |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1654 | IAccessibilityInteractionConnectionCallback callback, int flags, |
| 1655 | long interrogatingTid) throws RemoteException { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1656 | final int resolvedWindowId; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1657 | IAccessibilityInteractionConnection connection = null; |
| 1658 | synchronized (mLock) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1659 | final int resolvedUserId = mSecurityPolicy |
| 1660 | .resolveCallingUserIdEnforcingPermissionsLocked( |
| 1661 | UserHandle.getCallingUserId()); |
| 1662 | if (resolvedUserId != mCurrentUserId) { |
| 1663 | return -1; |
| 1664 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1665 | mSecurityPolicy.enforceCanRetrieveWindowContent(this); |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1666 | resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1667 | final boolean permissionGranted = |
| 1668 | mSecurityPolicy.canGetAccessibilityNodeInfoLocked(this, resolvedWindowId); |
| 1669 | if (!permissionGranted) { |
| 1670 | return 0; |
| 1671 | } else { |
| 1672 | connection = getConnectionLocked(resolvedWindowId); |
| 1673 | if (connection == null) { |
| 1674 | return 0; |
| 1675 | } |
| 1676 | } |
| 1677 | } |
| 1678 | final int allFlags = flags | ((mIncludeNotImportantViews) ? |
| 1679 | AccessibilityNodeInfo.INCLUDE_NOT_IMPORTANT_VIEWS : 0); |
| 1680 | final int interrogatingPid = Binder.getCallingPid(); |
| 1681 | final long identityToken = Binder.clearCallingIdentity(); |
| 1682 | try { |
| 1683 | connection.findAccessibilityNodeInfoByAccessibilityId(accessibilityNodeId, |
Svetoslav Ganov | c9c9a48 | 2012-07-16 08:46:07 -0700 | [diff] [blame] | 1684 | interactionId, callback, allFlags, interrogatingPid, interrogatingTid); |
| 1685 | return getCompatibilityScale(resolvedWindowId); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1686 | } catch (RemoteException re) { |
| 1687 | if (DEBUG) { |
| 1688 | Slog.e(LOG_TAG, "Error calling findAccessibilityNodeInfoByAccessibilityId()"); |
| 1689 | } |
| 1690 | } finally { |
| 1691 | Binder.restoreCallingIdentity(identityToken); |
| 1692 | } |
Svetoslav Ganov | c9c9a48 | 2012-07-16 08:46:07 -0700 | [diff] [blame] | 1693 | return 0; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1694 | } |
| 1695 | |
| 1696 | @Override |
| 1697 | public float findFocus(int accessibilityWindowId, long accessibilityNodeId, |
| 1698 | int focusType, int interactionId, |
| 1699 | IAccessibilityInteractionConnectionCallback callback, long interrogatingTid) |
Svetoslav Ganov | 8bd6961 | 2011-08-23 13:40:30 -0700 | [diff] [blame] | 1700 | throws RemoteException { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1701 | final int resolvedWindowId; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1702 | IAccessibilityInteractionConnection connection = null; |
| 1703 | synchronized (mLock) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1704 | final int resolvedUserId = mSecurityPolicy |
| 1705 | .resolveCallingUserIdEnforcingPermissionsLocked( |
| 1706 | UserHandle.getCallingUserId()); |
| 1707 | if (resolvedUserId != mCurrentUserId) { |
| 1708 | return -1; |
| 1709 | } |
Svetoslav Ganov | 4a49d9f | 2011-07-17 12:22:08 -0700 | [diff] [blame] | 1710 | mSecurityPolicy.enforceCanRetrieveWindowContent(this); |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1711 | resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1712 | final boolean permissionGranted = |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 1713 | mSecurityPolicy.canGetAccessibilityNodeInfoLocked(this, resolvedWindowId); |
Svetoslav Ganov | e8f9535 | 2011-07-06 17:24:03 -0700 | [diff] [blame] | 1714 | if (!permissionGranted) { |
Svetoslav Ganov | 8bd6961 | 2011-08-23 13:40:30 -0700 | [diff] [blame] | 1715 | return 0; |
Svetoslav Ganov | e8f9535 | 2011-07-06 17:24:03 -0700 | [diff] [blame] | 1716 | } else { |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 1717 | connection = getConnectionLocked(resolvedWindowId); |
| 1718 | if (connection == null) { |
Svetoslav Ganov | 8bd6961 | 2011-08-23 13:40:30 -0700 | [diff] [blame] | 1719 | return 0; |
Svetoslav Ganov | e8f9535 | 2011-07-06 17:24:03 -0700 | [diff] [blame] | 1720 | } |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1721 | } |
| 1722 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1723 | final int flags = (mIncludeNotImportantViews) ? |
| 1724 | AccessibilityNodeInfo.INCLUDE_NOT_IMPORTANT_VIEWS : 0; |
Svetoslav Ganov | 8bd6961 | 2011-08-23 13:40:30 -0700 | [diff] [blame] | 1725 | final int interrogatingPid = Binder.getCallingPid(); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1726 | final long identityToken = Binder.clearCallingIdentity(); |
| 1727 | try { |
Svetoslav Ganov | c9c9a48 | 2012-07-16 08:46:07 -0700 | [diff] [blame] | 1728 | connection.findFocus(accessibilityNodeId, focusType, interactionId, callback, |
| 1729 | flags, interrogatingPid, interrogatingTid); |
| 1730 | return getCompatibilityScale(resolvedWindowId); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1731 | } catch (RemoteException re) { |
| 1732 | if (DEBUG) { |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1733 | Slog.e(LOG_TAG, "Error calling findAccessibilityFocus()"); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1734 | } |
| 1735 | } finally { |
| 1736 | Binder.restoreCallingIdentity(identityToken); |
| 1737 | } |
Svetoslav Ganov | c9c9a48 | 2012-07-16 08:46:07 -0700 | [diff] [blame] | 1738 | return 0; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1739 | } |
| 1740 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1741 | @Override |
| 1742 | public float focusSearch(int accessibilityWindowId, long accessibilityNodeId, |
| 1743 | int direction, int interactionId, |
| 1744 | IAccessibilityInteractionConnectionCallback callback, long interrogatingTid) |
| 1745 | throws RemoteException { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1746 | final int resolvedWindowId; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1747 | IAccessibilityInteractionConnection connection = null; |
| 1748 | synchronized (mLock) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1749 | final int resolvedUserId = mSecurityPolicy |
| 1750 | .resolveCallingUserIdEnforcingPermissionsLocked( |
| 1751 | UserHandle.getCallingUserId()); |
| 1752 | if (resolvedUserId != mCurrentUserId) { |
| 1753 | return -1; |
| 1754 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1755 | mSecurityPolicy.enforceCanRetrieveWindowContent(this); |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1756 | resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1757 | final boolean permissionGranted = |
| 1758 | mSecurityPolicy.canGetAccessibilityNodeInfoLocked(this, resolvedWindowId); |
| 1759 | if (!permissionGranted) { |
| 1760 | return 0; |
| 1761 | } else { |
| 1762 | connection = getConnectionLocked(resolvedWindowId); |
| 1763 | if (connection == null) { |
| 1764 | return 0; |
| 1765 | } |
| 1766 | } |
| 1767 | } |
| 1768 | final int flags = (mIncludeNotImportantViews) ? |
| 1769 | AccessibilityNodeInfo.INCLUDE_NOT_IMPORTANT_VIEWS : 0; |
| 1770 | final int interrogatingPid = Binder.getCallingPid(); |
| 1771 | final long identityToken = Binder.clearCallingIdentity(); |
| 1772 | try { |
Svetoslav Ganov | c9c9a48 | 2012-07-16 08:46:07 -0700 | [diff] [blame] | 1773 | connection.focusSearch(accessibilityNodeId, direction, interactionId, callback, |
| 1774 | flags, interrogatingPid, interrogatingTid); |
| 1775 | return getCompatibilityScale(resolvedWindowId); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1776 | } catch (RemoteException re) { |
| 1777 | if (DEBUG) { |
| 1778 | Slog.e(LOG_TAG, "Error calling accessibilityFocusSearch()"); |
| 1779 | } |
| 1780 | } finally { |
| 1781 | Binder.restoreCallingIdentity(identityToken); |
| 1782 | } |
Svetoslav Ganov | c9c9a48 | 2012-07-16 08:46:07 -0700 | [diff] [blame] | 1783 | return 0; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1784 | } |
| 1785 | |
| 1786 | @Override |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1787 | public boolean performAccessibilityAction(int accessibilityWindowId, |
Svetoslav Ganov | aa780c1 | 2012-04-19 23:01:39 -0700 | [diff] [blame] | 1788 | long accessibilityNodeId, int action, Bundle arguments, int interactionId, |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1789 | IAccessibilityInteractionConnectionCallback callback, long interrogatingTid) |
| 1790 | throws RemoteException { |
| 1791 | final int resolvedWindowId; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1792 | IAccessibilityInteractionConnection connection = null; |
| 1793 | synchronized (mLock) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1794 | final int resolvedUserId = mSecurityPolicy |
| 1795 | .resolveCallingUserIdEnforcingPermissionsLocked( |
| 1796 | UserHandle.getCallingUserId()); |
| 1797 | if (resolvedUserId != mCurrentUserId) { |
| 1798 | return false; |
| 1799 | } |
| 1800 | mSecurityPolicy.enforceCanRetrieveWindowContent(this); |
| 1801 | resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1802 | final boolean permissionGranted = mSecurityPolicy.canPerformActionLocked(this, |
Svetoslav Ganov | b7ff325 | 2012-04-24 18:40:07 -0700 | [diff] [blame] | 1803 | resolvedWindowId, action, arguments); |
Svetoslav Ganov | e8f9535 | 2011-07-06 17:24:03 -0700 | [diff] [blame] | 1804 | if (!permissionGranted) { |
| 1805 | return false; |
| 1806 | } else { |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 1807 | connection = getConnectionLocked(resolvedWindowId); |
| 1808 | if (connection == null) { |
Svetoslav Ganov | e8f9535 | 2011-07-06 17:24:03 -0700 | [diff] [blame] | 1809 | return false; |
| 1810 | } |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1811 | } |
| 1812 | } |
Svetoslav Ganov | 9bf21873c | 2012-05-22 17:43:23 -0700 | [diff] [blame] | 1813 | final int flags = (mIncludeNotImportantViews) ? |
| 1814 | AccessibilityNodeInfo.INCLUDE_NOT_IMPORTANT_VIEWS : 0; |
| 1815 | final int interrogatingPid = Binder.getCallingPid(); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1816 | final long identityToken = Binder.clearCallingIdentity(); |
| 1817 | try { |
Svetoslav Ganov | aa780c1 | 2012-04-19 23:01:39 -0700 | [diff] [blame] | 1818 | connection.performAccessibilityAction(accessibilityNodeId, action, arguments, |
| 1819 | interactionId, callback, flags, interrogatingPid, interrogatingTid); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1820 | } catch (RemoteException re) { |
| 1821 | if (DEBUG) { |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 1822 | Slog.e(LOG_TAG, "Error calling performAccessibilityAction()"); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1823 | } |
| 1824 | } finally { |
| 1825 | Binder.restoreCallingIdentity(identityToken); |
| 1826 | } |
Svetoslav Ganov | 8bd6961 | 2011-08-23 13:40:30 -0700 | [diff] [blame] | 1827 | return true; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1828 | } |
| 1829 | |
Svetoslav Ganov | e20a177 | 2012-09-25 16:07:46 -0700 | [diff] [blame] | 1830 | public boolean performGlobalAction(int action) { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1831 | synchronized (mLock) { |
| 1832 | final int resolvedUserId = mSecurityPolicy |
| 1833 | .resolveCallingUserIdEnforcingPermissionsLocked( |
| 1834 | UserHandle.getCallingUserId()); |
| 1835 | if (resolvedUserId != mCurrentUserId) { |
| 1836 | return false; |
| 1837 | } |
Svetoslav Ganov | 005b83b | 2012-04-16 18:17:17 -0700 | [diff] [blame] | 1838 | } |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1839 | final long identity = Binder.clearCallingIdentity(); |
| 1840 | try { |
| 1841 | switch (action) { |
| 1842 | case AccessibilityService.GLOBAL_ACTION_BACK: { |
| 1843 | sendDownAndUpKeyEvents(KeyEvent.KEYCODE_BACK); |
| 1844 | } return true; |
| 1845 | case AccessibilityService.GLOBAL_ACTION_HOME: { |
| 1846 | sendDownAndUpKeyEvents(KeyEvent.KEYCODE_HOME); |
| 1847 | } return true; |
| 1848 | case AccessibilityService.GLOBAL_ACTION_RECENTS: { |
| 1849 | openRecents(); |
| 1850 | } return true; |
| 1851 | case AccessibilityService.GLOBAL_ACTION_NOTIFICATIONS: { |
Svetoslav Ganov | e20a177 | 2012-09-25 16:07:46 -0700 | [diff] [blame] | 1852 | expandNotifications(); |
| 1853 | } return true; |
| 1854 | case AccessibilityService.GLOBAL_ACTION_QUICK_SETTINGS: { |
| 1855 | expandQuickSettings(); |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1856 | } return true; |
| 1857 | } |
| 1858 | return false; |
| 1859 | } finally { |
| 1860 | Binder.restoreCallingIdentity(identity); |
| 1861 | } |
Svetoslav Ganov | 005b83b | 2012-04-16 18:17:17 -0700 | [diff] [blame] | 1862 | } |
| 1863 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1864 | public void onServiceDisconnected(ComponentName componentName) { |
Svetoslav Ganov | f9886f3 | 2011-05-10 15:28:33 -0700 | [diff] [blame] | 1865 | /* do nothing - #binderDied takes care */ |
| 1866 | } |
| 1867 | |
| 1868 | public void linkToOwnDeath() throws RemoteException { |
| 1869 | mService.linkToDeath(this, 0); |
| 1870 | } |
| 1871 | |
| 1872 | public void unlinkToOwnDeath() { |
| 1873 | mService.unlinkToDeath(this, 0); |
| 1874 | } |
| 1875 | |
Svetoslav Ganov | d116d7c | 2011-11-21 18:41:59 -0800 | [diff] [blame] | 1876 | public void dispose() { |
| 1877 | try { |
| 1878 | // Clear the proxy in the other process so this |
| 1879 | // IAccessibilityServiceConnection can be garbage collected. |
| 1880 | mServiceInterface.setConnection(null, mId); |
| 1881 | } catch (RemoteException re) { |
| 1882 | /* ignore */ |
| 1883 | } |
| 1884 | mService = null; |
| 1885 | mServiceInterface = null; |
| 1886 | } |
| 1887 | |
Svetoslav Ganov | f9886f3 | 2011-05-10 15:28:33 -0700 | [diff] [blame] | 1888 | public void binderDied() { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1889 | synchronized (mLock) { |
Svetoslav Ganov | 0d04e24 | 2012-02-21 13:46:36 -0800 | [diff] [blame] | 1890 | // The death recipient is unregistered in tryRemoveServiceLocked |
Svetoslav Ganov | f9886f3 | 2011-05-10 15:28:33 -0700 | [diff] [blame] | 1891 | tryRemoveServiceLocked(this); |
Svetoslav Ganov | b6eca6e | 2011-09-27 10:47:58 -0700 | [diff] [blame] | 1892 | // We no longer have an automation service, so restore |
| 1893 | // the state based on values in the settings database. |
| 1894 | if (mIsAutomation) { |
Svetoslav Ganov | 9b666d0 | 2012-02-10 14:55:41 -0800 | [diff] [blame] | 1895 | mUiAutomationService = null; |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 1896 | recreateInternalStateLocked(getUserStateLocked(mUserId)); |
Svetoslav Ganov | b6eca6e | 2011-09-27 10:47:58 -0700 | [diff] [blame] | 1897 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1898 | } |
| 1899 | } |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1900 | |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 1901 | /** |
| 1902 | * Performs a notification for an {@link AccessibilityEvent}. |
| 1903 | * |
| 1904 | * @param event The event. |
| 1905 | */ |
| 1906 | public void notifyAccessibilityEvent(AccessibilityEvent event) { |
| 1907 | synchronized (mLock) { |
| 1908 | final int eventType = event.getEventType(); |
| 1909 | // Make a copy since during dispatch it is possible the event to |
| 1910 | // be modified to remove its source if the receiving service does |
| 1911 | // not have permission to access the window content. |
| 1912 | AccessibilityEvent newEvent = AccessibilityEvent.obtain(event); |
| 1913 | AccessibilityEvent oldEvent = mPendingEvents.get(eventType); |
| 1914 | mPendingEvents.put(eventType, newEvent); |
| 1915 | |
| 1916 | final int what = eventType; |
| 1917 | if (oldEvent != null) { |
| 1918 | mHandler.removeMessages(what); |
| 1919 | oldEvent.recycle(); |
| 1920 | } |
| 1921 | |
| 1922 | Message message = mHandler.obtainMessage(what); |
| 1923 | mHandler.sendMessageDelayed(message, mNotificationTimeout); |
| 1924 | } |
| 1925 | } |
| 1926 | |
| 1927 | /** |
| 1928 | * Notifies an accessibility service client for a scheduled event given the event type. |
| 1929 | * |
| 1930 | * @param eventType The type of the event to dispatch. |
| 1931 | */ |
| 1932 | private void notifyAccessibilityEventInternal(int eventType) { |
| 1933 | IAccessibilityServiceClient listener; |
| 1934 | AccessibilityEvent event; |
| 1935 | |
| 1936 | synchronized (mLock) { |
| 1937 | listener = mServiceInterface; |
| 1938 | |
| 1939 | // If the service died/was disabled while the message for dispatching |
| 1940 | // the accessibility event was propagating the listener may be null. |
| 1941 | if (listener == null) { |
| 1942 | return; |
| 1943 | } |
| 1944 | |
| 1945 | event = mPendingEvents.get(eventType); |
| 1946 | |
| 1947 | // Check for null here because there is a concurrent scenario in which this |
| 1948 | // happens: 1) A binder thread calls notifyAccessibilityServiceDelayedLocked |
| 1949 | // which posts a message for dispatching an event. 2) The message is pulled |
| 1950 | // from the queue by the handler on the service thread and the latter is |
| 1951 | // just about to acquire the lock and call this method. 3) Now another binder |
| 1952 | // thread acquires the lock calling notifyAccessibilityServiceDelayedLocked |
| 1953 | // so the service thread waits for the lock; 4) The binder thread replaces |
| 1954 | // the event with a more recent one (assume the same event type) and posts a |
| 1955 | // dispatch request releasing the lock. 5) Now the main thread is unblocked and |
| 1956 | // dispatches the event which is removed from the pending ones. 6) And ... now |
| 1957 | // the service thread handles the last message posted by the last binder call |
| 1958 | // but the event is already dispatched and hence looking it up in the pending |
| 1959 | // ones yields null. This check is much simpler that keeping count for each |
| 1960 | // event type of each service to catch such a scenario since only one message |
| 1961 | // is processed at a time. |
| 1962 | if (event == null) { |
| 1963 | return; |
| 1964 | } |
| 1965 | |
| 1966 | mPendingEvents.remove(eventType); |
| 1967 | if (mSecurityPolicy.canRetrieveWindowContent(this)) { |
| 1968 | event.setConnectionId(mId); |
| 1969 | } else { |
| 1970 | event.setSource(null); |
| 1971 | } |
| 1972 | event.setSealed(true); |
| 1973 | } |
| 1974 | |
| 1975 | try { |
| 1976 | listener.onAccessibilityEvent(event); |
| 1977 | if (DEBUG) { |
| 1978 | Slog.i(LOG_TAG, "Event " + event + " sent to " + listener); |
| 1979 | } |
| 1980 | } catch (RemoteException re) { |
| 1981 | Slog.e(LOG_TAG, "Error during sending " + event + " to " + listener, re); |
| 1982 | } finally { |
| 1983 | event.recycle(); |
| 1984 | } |
| 1985 | } |
| 1986 | |
| 1987 | public void notifyGesture(int gestureId) { |
| 1988 | mHandler.obtainMessage(MSG_ON_GESTURE, gestureId, 0).sendToTarget(); |
| 1989 | } |
| 1990 | |
| 1991 | private void notifyGestureInternal(int gestureId) { |
| 1992 | IAccessibilityServiceClient listener = mServiceInterface; |
| 1993 | if (listener != null) { |
| 1994 | try { |
| 1995 | listener.onGesture(gestureId); |
| 1996 | } catch (RemoteException re) { |
| 1997 | Slog.e(LOG_TAG, "Error during sending gesture " + gestureId |
| 1998 | + " to " + mService, re); |
| 1999 | } |
| 2000 | } |
| 2001 | } |
| 2002 | |
Svetoslav Ganov | 005b83b | 2012-04-16 18:17:17 -0700 | [diff] [blame] | 2003 | private void sendDownAndUpKeyEvents(int keyCode) { |
| 2004 | final long token = Binder.clearCallingIdentity(); |
| 2005 | |
| 2006 | // Inject down. |
| 2007 | final long downTime = SystemClock.uptimeMillis(); |
| 2008 | KeyEvent down = KeyEvent.obtain(downTime, downTime, KeyEvent.ACTION_DOWN, keyCode, 0, 0, |
| 2009 | KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FROM_SYSTEM, |
| 2010 | InputDevice.SOURCE_KEYBOARD, null); |
| 2011 | InputManager.getInstance().injectInputEvent(down, |
| 2012 | InputManager.INJECT_INPUT_EVENT_MODE_ASYNC); |
| 2013 | down.recycle(); |
| 2014 | |
| 2015 | // Inject up. |
| 2016 | final long upTime = SystemClock.uptimeMillis(); |
| 2017 | KeyEvent up = KeyEvent.obtain(downTime, upTime, KeyEvent.ACTION_UP, keyCode, 0, 0, |
| 2018 | KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FROM_SYSTEM, |
| 2019 | InputDevice.SOURCE_KEYBOARD, null); |
| 2020 | InputManager.getInstance().injectInputEvent(up, |
| 2021 | InputManager.INJECT_INPUT_EVENT_MODE_ASYNC); |
| 2022 | up.recycle(); |
| 2023 | |
| 2024 | Binder.restoreCallingIdentity(token); |
| 2025 | } |
| 2026 | |
Svetoslav Ganov | e20a177 | 2012-09-25 16:07:46 -0700 | [diff] [blame] | 2027 | private void expandNotifications() { |
Svetoslav Ganov | 5c89f44 | 2012-05-15 13:28:09 -0700 | [diff] [blame] | 2028 | final long token = Binder.clearCallingIdentity(); |
| 2029 | |
| 2030 | StatusBarManager statusBarManager = (StatusBarManager) mContext.getSystemService( |
| 2031 | android.app.Service.STATUS_BAR_SERVICE); |
Daniel Sandler | 11cf178 | 2012-09-27 14:03:08 -0400 | [diff] [blame] | 2032 | statusBarManager.expandNotificationsPanel(); |
Svetoslav Ganov | e20a177 | 2012-09-25 16:07:46 -0700 | [diff] [blame] | 2033 | |
| 2034 | Binder.restoreCallingIdentity(token); |
| 2035 | } |
| 2036 | |
| 2037 | private void expandQuickSettings() { |
| 2038 | final long token = Binder.clearCallingIdentity(); |
| 2039 | |
| 2040 | StatusBarManager statusBarManager = (StatusBarManager) mContext.getSystemService( |
| 2041 | android.app.Service.STATUS_BAR_SERVICE); |
Daniel Sandler | 11cf178 | 2012-09-27 14:03:08 -0400 | [diff] [blame] | 2042 | statusBarManager.expandSettingsPanel(); |
Svetoslav Ganov | 5c89f44 | 2012-05-15 13:28:09 -0700 | [diff] [blame] | 2043 | |
| 2044 | Binder.restoreCallingIdentity(token); |
| 2045 | } |
| 2046 | |
Svetoslav Ganov | c682fc9 | 2012-06-04 14:02:09 -0700 | [diff] [blame] | 2047 | private void openRecents() { |
| 2048 | final long token = Binder.clearCallingIdentity(); |
| 2049 | |
| 2050 | IStatusBarService statusBarService = IStatusBarService.Stub.asInterface( |
| 2051 | ServiceManager.getService("statusbar")); |
| 2052 | try { |
| 2053 | statusBarService.toggleRecentApps(); |
| 2054 | } catch (RemoteException e) { |
| 2055 | Slog.e(LOG_TAG, "Error toggling recent apps."); |
| 2056 | } |
| 2057 | |
| 2058 | Binder.restoreCallingIdentity(token); |
| 2059 | } |
| 2060 | |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 2061 | private IAccessibilityInteractionConnection getConnectionLocked(int windowId) { |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 2062 | if (DEBUG) { |
| 2063 | Slog.i(LOG_TAG, "Trying to get interaction connection to windowId: " + windowId); |
| 2064 | } |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 2065 | AccessibilityConnectionWrapper wrapper = mGlobalInteractionConnections.get(windowId); |
| 2066 | if (wrapper == null) { |
| 2067 | wrapper = getCurrentUserStateLocked().mInteractionConnections.get(windowId); |
| 2068 | } |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 2069 | if (wrapper != null && wrapper.mConnection != null) { |
| 2070 | return wrapper.mConnection; |
| 2071 | } |
| 2072 | if (DEBUG) { |
| 2073 | Slog.e(LOG_TAG, "No interaction connection to window: " + windowId); |
| 2074 | } |
| 2075 | return null; |
| 2076 | } |
| 2077 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 2078 | private int resolveAccessibilityWindowIdLocked(int accessibilityWindowId) { |
Svetoslav Ganov | 0d04e24 | 2012-02-21 13:46:36 -0800 | [diff] [blame] | 2079 | if (accessibilityWindowId == AccessibilityNodeInfo.ACTIVE_WINDOW_ID) { |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 2080 | return mSecurityPolicy.mActiveWindowId; |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 2081 | } |
| 2082 | return accessibilityWindowId; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 2083 | } |
Svetoslav Ganov | 7961be7 | 2011-06-21 12:31:56 -0700 | [diff] [blame] | 2084 | |
Svetoslav Ganov | 8bd6961 | 2011-08-23 13:40:30 -0700 | [diff] [blame] | 2085 | private float getCompatibilityScale(int windowId) { |
Svetoslav Ganov | c9c9a48 | 2012-07-16 08:46:07 -0700 | [diff] [blame] | 2086 | try { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 2087 | IBinder windowToken = mGlobalWindowTokens.get(windowId); |
| 2088 | if (windowToken != null) { |
| 2089 | return mWindowManagerService.getWindowCompatibilityScale(windowToken); |
| 2090 | } |
| 2091 | windowToken = getCurrentUserStateLocked().mWindowTokens.get(windowId); |
| 2092 | if (windowToken != null) { |
| 2093 | return mWindowManagerService.getWindowCompatibilityScale(windowToken); |
| 2094 | } |
Svetoslav Ganov | c9c9a48 | 2012-07-16 08:46:07 -0700 | [diff] [blame] | 2095 | } catch (RemoteException re) { |
| 2096 | /* ignore */ |
| 2097 | } |
| 2098 | return 1.0f; |
Svetoslav Ganov | 7961be7 | 2011-06-21 12:31:56 -0700 | [diff] [blame] | 2099 | } |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 2100 | } |
| 2101 | |
| 2102 | final class SecurityPolicy { |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 2103 | private static final int VALID_ACTIONS = |
| 2104 | AccessibilityNodeInfo.ACTION_CLICK |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 2105 | | AccessibilityNodeInfo.ACTION_LONG_CLICK |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 2106 | | AccessibilityNodeInfo.ACTION_FOCUS |
| 2107 | | AccessibilityNodeInfo.ACTION_CLEAR_FOCUS |
| 2108 | | AccessibilityNodeInfo.ACTION_SELECT |
| 2109 | | AccessibilityNodeInfo.ACTION_CLEAR_SELECTION |
| 2110 | | AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS |
Svetoslav Ganov | aa780c1 | 2012-04-19 23:01:39 -0700 | [diff] [blame] | 2111 | | AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS |
Svetoslav Ganov | 2b435aa | 2012-05-04 17:16:37 -0700 | [diff] [blame] | 2112 | | AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY |
| 2113 | | AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY |
Svetoslav Ganov | b7ff325 | 2012-04-24 18:40:07 -0700 | [diff] [blame] | 2114 | | AccessibilityNodeInfo.ACTION_NEXT_HTML_ELEMENT |
Svetoslav Ganov | a1dc761 | 2012-05-10 04:14:53 -0700 | [diff] [blame] | 2115 | | AccessibilityNodeInfo.ACTION_PREVIOUS_HTML_ELEMENT |
| 2116 | | AccessibilityNodeInfo.ACTION_SCROLL_FORWARD |
| 2117 | | AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 2118 | |
| 2119 | private static final int RETRIEVAL_ALLOWING_EVENT_TYPES = |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 2120 | AccessibilityEvent.TYPE_VIEW_CLICKED |
| 2121 | | AccessibilityEvent.TYPE_VIEW_FOCUSED |
| 2122 | | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER |
| 2123 | | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT |
| 2124 | | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED |
| 2125 | | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED |
| 2126 | | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED |
| 2127 | | AccessibilityEvent.TYPE_VIEW_SELECTED |
Svetoslav Ganov | a015617 | 2011-06-26 17:55:44 -0700 | [diff] [blame] | 2128 | | AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED |
| 2129 | | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 2130 | | AccessibilityEvent.TYPE_VIEW_SCROLLED |
| 2131 | | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED |
| 2132 | | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 2133 | |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 2134 | private int mActiveWindowId; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 2135 | |
Svetoslav Ganov | eeee4d2 | 2011-06-10 20:51:30 -0700 | [diff] [blame] | 2136 | private boolean canDispatchAccessibilityEvent(AccessibilityEvent event) { |
Svetoslav Ganov | 58fd9f8 | 2012-10-03 18:10:27 -0700 | [diff] [blame] | 2137 | final int eventType = event.getEventType(); |
| 2138 | switch (eventType) { |
| 2139 | // All events that are for changes in a global window |
| 2140 | // state should *always* be dispatched. |
| 2141 | case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED: |
| 2142 | case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED: |
| 2143 | // All events generated by the user touching the |
| 2144 | // screen should *always* be dispatched. |
| 2145 | case AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_START: |
| 2146 | case AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_END: |
| 2147 | case AccessibilityEvent.TYPE_GESTURE_DETECTION_START: |
| 2148 | case AccessibilityEvent.TYPE_GESTURE_DETECTION_END: |
| 2149 | case AccessibilityEvent.TYPE_TOUCH_INTERACTION_START: |
| 2150 | case AccessibilityEvent.TYPE_TOUCH_INTERACTION_END: |
| 2151 | // These will change the active window, so dispatch. |
| 2152 | case AccessibilityEvent.TYPE_VIEW_HOVER_ENTER: |
| 2153 | case AccessibilityEvent.TYPE_VIEW_HOVER_EXIT: { |
| 2154 | return true; |
| 2155 | } |
| 2156 | // All events for changes in window content should be |
| 2157 | // dispatched *only* if this window is the active one. |
| 2158 | default: |
| 2159 | return event.getWindowId() == mActiveWindowId; |
| 2160 | } |
Svetoslav Ganov | eeee4d2 | 2011-06-10 20:51:30 -0700 | [diff] [blame] | 2161 | } |
| 2162 | |
Svetoslav Ganov | a8afa69 | 2012-09-25 13:03:18 -0700 | [diff] [blame] | 2163 | public void updateEventSourceLocked(AccessibilityEvent event) { |
| 2164 | if ((event.getEventType() & RETRIEVAL_ALLOWING_EVENT_TYPES) == 0) { |
| 2165 | event.setSource(null); |
| 2166 | } |
| 2167 | } |
| 2168 | |
| 2169 | public void updateActiveWindow(int windowId, int eventType) { |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 2170 | // The active window is either the window that has input focus or |
| 2171 | // the window that the user is currently touching. If the user is |
| 2172 | // touching a window that does not have input focus as soon as the |
| 2173 | // the user stops touching that window the focused window becomes |
| 2174 | // the active one. |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 2175 | switch (eventType) { |
| 2176 | case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED: { |
| 2177 | if (getFocusedWindowId() == windowId) { |
| 2178 | mActiveWindowId = windowId; |
| 2179 | } |
| 2180 | } break; |
| 2181 | case AccessibilityEvent.TYPE_VIEW_HOVER_ENTER: |
| 2182 | case AccessibilityEvent.TYPE_VIEW_HOVER_EXIT: { |
| 2183 | mActiveWindowId = windowId; |
| 2184 | } break; |
Svetoslav Ganov | 76c0dd4 | 2012-09-24 19:16:16 -0700 | [diff] [blame] | 2185 | case AccessibilityEvent.TYPE_TOUCH_INTERACTION_END: { |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 2186 | mActiveWindowId = getFocusedWindowId(); |
| 2187 | } break; |
Svetoslav Ganov | 4e2a762 | 2011-07-26 20:08:46 -0700 | [diff] [blame] | 2188 | } |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 2189 | } |
| 2190 | |
| 2191 | public int getRetrievalAllowingWindowLocked() { |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 2192 | return mActiveWindowId; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 2193 | } |
| 2194 | |
| 2195 | public boolean canGetAccessibilityNodeInfoLocked(Service service, int windowId) { |
| 2196 | return canRetrieveWindowContent(service) && isRetrievalAllowingWindow(windowId); |
| 2197 | } |
| 2198 | |
Svetoslav Ganov | b7ff325 | 2012-04-24 18:40:07 -0700 | [diff] [blame] | 2199 | public boolean canPerformActionLocked(Service service, int windowId, int action, |
| 2200 | Bundle arguments) { |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 2201 | return canRetrieveWindowContent(service) |
| 2202 | && isRetrievalAllowingWindow(windowId) |
Svetoslav Ganov | afe8cf2 | 2012-04-28 13:26:57 -0700 | [diff] [blame] | 2203 | && isActionPermitted(action); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 2204 | } |
| 2205 | |
| 2206 | public boolean canRetrieveWindowContent(Service service) { |
| 2207 | return service.mCanRetrieveScreenContent; |
| 2208 | } |
| 2209 | |
Svetoslav Ganov | 4a49d9f | 2011-07-17 12:22:08 -0700 | [diff] [blame] | 2210 | public void enforceCanRetrieveWindowContent(Service service) throws RemoteException { |
| 2211 | // This happens due to incorrect registration so make it apparent. |
| 2212 | if (!canRetrieveWindowContent(service)) { |
| 2213 | Slog.e(LOG_TAG, "Accessibility serivce " + service.mComponentName + " does not " + |
| 2214 | "declare android:canRetrieveWindowContent."); |
| 2215 | throw new RemoteException(); |
| 2216 | } |
| 2217 | } |
| 2218 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 2219 | public int resolveCallingUserIdEnforcingPermissionsLocked(int userId) { |
| 2220 | final int callingUid = Binder.getCallingUid(); |
| 2221 | if (callingUid == Process.SYSTEM_UID |
| 2222 | || callingUid == Process.SHELL_UID) { |
| 2223 | return mCurrentUserId; |
| 2224 | } |
| 2225 | final int callingUserId = UserHandle.getUserId(callingUid); |
| 2226 | if (callingUserId == userId) { |
| 2227 | return userId; |
| 2228 | } |
| 2229 | if (!hasPermission(Manifest.permission.INTERACT_ACROSS_USERS) |
| 2230 | && !hasPermission(Manifest.permission.INTERACT_ACROSS_USERS_FULL)) { |
| 2231 | throw new SecurityException("Call from user " + callingUserId + " as user " |
| 2232 | + userId + " without permission INTERACT_ACROSS_USERS or " |
| 2233 | + "INTERACT_ACROSS_USERS_FULL not allowed."); |
| 2234 | } |
| 2235 | if (userId == UserHandle.USER_CURRENT |
| 2236 | || userId == UserHandle.USER_CURRENT_OR_SELF) { |
| 2237 | return mCurrentUserId; |
| 2238 | } |
| 2239 | throw new IllegalArgumentException("Calling user can be changed to only " |
| 2240 | + "UserHandle.USER_CURRENT or UserHandle.USER_CURRENT_OR_SELF."); |
| 2241 | } |
| 2242 | |
| 2243 | public boolean isCallerInteractingAcrossUsers(int userId) { |
| 2244 | final int callingUid = Binder.getCallingUid(); |
Svetoslav Ganov | a8afa69 | 2012-09-25 13:03:18 -0700 | [diff] [blame] | 2245 | return (Binder.getCallingPid() == android.os.Process.myPid() |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 2246 | || callingUid == Process.SHELL_UID |
| 2247 | || userId == UserHandle.USER_CURRENT |
| 2248 | || userId == UserHandle.USER_CURRENT_OR_SELF); |
| 2249 | } |
| 2250 | |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 2251 | private boolean isRetrievalAllowingWindow(int windowId) { |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 2252 | return (mActiveWindowId == windowId); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 2253 | } |
| 2254 | |
| 2255 | private boolean isActionPermitted(int action) { |
| 2256 | return (VALID_ACTIONS & action) != 0; |
| 2257 | } |
| 2258 | |
| 2259 | private void enforceCallingPermission(String permission, String function) { |
| 2260 | if (OWN_PROCESS_ID == Binder.getCallingPid()) { |
| 2261 | return; |
| 2262 | } |
Svetoslav Ganov | 9371a0a | 2012-09-21 18:20:37 -0700 | [diff] [blame] | 2263 | if (!hasPermission(permission)) { |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 2264 | throw new SecurityException("You do not have " + permission |
| 2265 | + " required to call " + function); |
| 2266 | } |
| 2267 | } |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 2268 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 2269 | private boolean hasPermission(String permission) { |
| 2270 | return mContext.checkCallingPermission(permission) == PackageManager.PERMISSION_GRANTED; |
| 2271 | } |
| 2272 | |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 2273 | private int getFocusedWindowId() { |
Svetoslav Ganov | c9c9a48 | 2012-07-16 08:46:07 -0700 | [diff] [blame] | 2274 | try { |
| 2275 | // We call this only on window focus change or after touch |
| 2276 | // exploration gesture end and the shown windows are not that |
| 2277 | // many, so the linear look up is just fine. |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 2278 | IBinder token = mWindowManagerService.getFocusedWindowToken(); |
Svetoslav Ganov | c9c9a48 | 2012-07-16 08:46:07 -0700 | [diff] [blame] | 2279 | if (token != null) { |
Svetoslav Ganov | a8afa69 | 2012-09-25 13:03:18 -0700 | [diff] [blame] | 2280 | synchronized (mLock) { |
| 2281 | int windowId = getFocusedWindowIdLocked(token, mGlobalWindowTokens); |
| 2282 | if (windowId < 0) { |
| 2283 | windowId = getFocusedWindowIdLocked(token, |
| 2284 | getCurrentUserStateLocked().mWindowTokens); |
Svetoslav Ganov | c9c9a48 | 2012-07-16 08:46:07 -0700 | [diff] [blame] | 2285 | } |
Svetoslav Ganov | a8afa69 | 2012-09-25 13:03:18 -0700 | [diff] [blame] | 2286 | return windowId; |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 2287 | } |
| 2288 | } |
Svetoslav Ganov | c9c9a48 | 2012-07-16 08:46:07 -0700 | [diff] [blame] | 2289 | } catch (RemoteException re) { |
| 2290 | /* ignore */ |
Svetoslav Ganov | a8afa69 | 2012-09-25 13:03:18 -0700 | [diff] [blame] | 2291 | } |
| 2292 | return -1; |
| 2293 | } |
| 2294 | |
| 2295 | private int getFocusedWindowIdLocked(IBinder token, SparseArray<IBinder> windows) { |
| 2296 | final int windowCount = windows.size(); |
| 2297 | for (int i = 0; i < windowCount; i++) { |
| 2298 | if (windows.valueAt(i) == token) { |
| 2299 | return windows.keyAt(i); |
| 2300 | } |
Svetoslav Ganov | e15ccb9 | 2012-05-16 15:48:55 -0700 | [diff] [blame] | 2301 | } |
| 2302 | return -1; |
| 2303 | } |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 2304 | } |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 2305 | |
| 2306 | private class UserState { |
| 2307 | public final int mUserId; |
| 2308 | |
| 2309 | public final CopyOnWriteArrayList<Service> mServices = new CopyOnWriteArrayList<Service>(); |
| 2310 | |
| 2311 | public final RemoteCallbackList<IAccessibilityManagerClient> mClients = |
| 2312 | new RemoteCallbackList<IAccessibilityManagerClient>(); |
| 2313 | |
| 2314 | public final Map<ComponentName, Service> mComponentNameToServiceMap = |
| 2315 | new HashMap<ComponentName, Service>(); |
| 2316 | |
| 2317 | public final List<AccessibilityServiceInfo> mInstalledServices = |
| 2318 | new ArrayList<AccessibilityServiceInfo>(); |
| 2319 | |
| 2320 | public final Set<ComponentName> mEnabledServices = new HashSet<ComponentName>(); |
| 2321 | |
| 2322 | public final Set<ComponentName> mTouchExplorationGrantedServices = |
| 2323 | new HashSet<ComponentName>(); |
| 2324 | |
| 2325 | public final SparseArray<AccessibilityConnectionWrapper> |
| 2326 | mInteractionConnections = |
| 2327 | new SparseArray<AccessibilityConnectionWrapper>(); |
| 2328 | |
| 2329 | public final SparseArray<IBinder> mWindowTokens = new SparseArray<IBinder>(); |
| 2330 | |
| 2331 | public int mHandledFeedbackTypes = 0; |
| 2332 | |
| 2333 | public boolean mIsAccessibilityEnabled; |
| 2334 | public boolean mIsTouchExplorationEnabled; |
| 2335 | public boolean mIsDisplayMagnificationEnabled; |
| 2336 | |
| 2337 | public UserState(int userId) { |
| 2338 | mUserId = userId; |
| 2339 | } |
| 2340 | } |
| 2341 | |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 2342 | private class TempUserStateChangeMemento { |
| 2343 | public int mUserId = UserHandle.USER_NULL; |
| 2344 | public boolean mIsAccessibilityEnabled; |
| 2345 | public boolean mIsTouchExplorationEnabled; |
| 2346 | public boolean mIsDisplayMagnificationEnabled; |
| 2347 | public final Set<ComponentName> mEnabledServices = new HashSet<ComponentName>(); |
| 2348 | public final Set<ComponentName> mTouchExplorationGrantedServices = |
| 2349 | new HashSet<ComponentName>(); |
| 2350 | |
| 2351 | public void initialize(int userId, UserState userState) { |
| 2352 | mUserId = userId; |
| 2353 | mIsAccessibilityEnabled = userState.mIsAccessibilityEnabled; |
| 2354 | mIsTouchExplorationEnabled = userState.mIsTouchExplorationEnabled; |
| 2355 | mIsDisplayMagnificationEnabled = userState.mIsDisplayMagnificationEnabled; |
| 2356 | mEnabledServices.clear(); |
| 2357 | mEnabledServices.addAll(userState.mEnabledServices); |
| 2358 | mTouchExplorationGrantedServices.clear(); |
| 2359 | mTouchExplorationGrantedServices.addAll(userState.mTouchExplorationGrantedServices); |
| 2360 | } |
| 2361 | |
| 2362 | public void applyTo(UserState userState) { |
| 2363 | userState.mIsAccessibilityEnabled = mIsAccessibilityEnabled; |
| 2364 | userState.mIsTouchExplorationEnabled = mIsTouchExplorationEnabled; |
| 2365 | userState.mIsDisplayMagnificationEnabled = mIsDisplayMagnificationEnabled; |
| 2366 | userState.mEnabledServices.clear(); |
| 2367 | userState.mEnabledServices.addAll(mEnabledServices); |
| 2368 | userState.mTouchExplorationGrantedServices.clear(); |
| 2369 | userState.mTouchExplorationGrantedServices.addAll(mTouchExplorationGrantedServices); |
| 2370 | } |
| 2371 | |
| 2372 | public void clear() { |
| 2373 | mUserId = UserHandle.USER_NULL; |
| 2374 | mIsAccessibilityEnabled = false; |
| 2375 | mIsTouchExplorationEnabled = false; |
| 2376 | mIsDisplayMagnificationEnabled = false; |
| 2377 | mEnabledServices.clear(); |
| 2378 | mTouchExplorationGrantedServices.clear(); |
| 2379 | } |
| 2380 | } |
| 2381 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 2382 | private final class AccessibilityContentObserver extends ContentObserver { |
| 2383 | |
| 2384 | private final Uri mAccessibilityEnabledUri = Settings.Secure.getUriFor( |
| 2385 | Settings.Secure.ACCESSIBILITY_ENABLED); |
| 2386 | |
| 2387 | private final Uri mTouchExplorationEnabledUri = Settings.Secure.getUriFor( |
| 2388 | Settings.Secure.TOUCH_EXPLORATION_ENABLED); |
| 2389 | |
| 2390 | private final Uri mDisplayMagnificationEnabledUri = Settings.Secure.getUriFor( |
| 2391 | Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED); |
| 2392 | |
| 2393 | private final Uri mEnabledAccessibilityServicesUri = Settings.Secure.getUriFor( |
| 2394 | Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES); |
| 2395 | |
| 2396 | private final Uri mTouchExplorationGrantedAccessibilityServicesUri = Settings.Secure |
| 2397 | .getUriFor(Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES); |
| 2398 | |
| 2399 | public AccessibilityContentObserver(Handler handler) { |
| 2400 | super(handler); |
| 2401 | } |
| 2402 | |
| 2403 | public void register(ContentResolver contentResolver) { |
| 2404 | contentResolver.registerContentObserver(mAccessibilityEnabledUri, |
| 2405 | false, this, UserHandle.USER_ALL); |
| 2406 | contentResolver.registerContentObserver(mTouchExplorationEnabledUri, |
| 2407 | false, this, UserHandle.USER_ALL); |
| 2408 | contentResolver.registerContentObserver(mDisplayMagnificationEnabledUri, |
| 2409 | false, this, UserHandle.USER_ALL); |
| 2410 | contentResolver.registerContentObserver(mEnabledAccessibilityServicesUri, |
| 2411 | false, this, UserHandle.USER_ALL); |
| 2412 | contentResolver.registerContentObserver( |
| 2413 | mTouchExplorationGrantedAccessibilityServicesUri, |
| 2414 | false, this, UserHandle.USER_ALL); |
| 2415 | } |
| 2416 | |
| 2417 | @Override |
| 2418 | public void onChange(boolean selfChange, Uri uri) { |
| 2419 | if (mAccessibilityEnabledUri.equals(uri)) { |
| 2420 | synchronized (mLock) { |
| 2421 | // We will update when the automation service dies. |
| 2422 | if (mUiAutomationService == null) { |
| 2423 | UserState userState = getCurrentUserStateLocked(); |
| 2424 | handleAccessibilityEnabledSettingChangedLocked(userState); |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 2425 | performServiceManagementLocked(userState); |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 2426 | updateInputFilterLocked(userState); |
| 2427 | scheduleSendStateToClientsLocked(userState); |
| 2428 | } |
| 2429 | } |
| 2430 | } else if (mTouchExplorationEnabledUri.equals(uri)) { |
| 2431 | synchronized (mLock) { |
| 2432 | // We will update when the automation service dies. |
| 2433 | if (mUiAutomationService == null) { |
| 2434 | UserState userState = getCurrentUserStateLocked(); |
| 2435 | handleTouchExplorationEnabledSettingChangedLocked(userState); |
| 2436 | updateInputFilterLocked(userState); |
| 2437 | scheduleSendStateToClientsLocked(userState); |
| 2438 | } |
| 2439 | } |
| 2440 | } else if (mDisplayMagnificationEnabledUri.equals(uri)) { |
| 2441 | synchronized (mLock) { |
| 2442 | // We will update when the automation service dies. |
| 2443 | if (mUiAutomationService == null) { |
| 2444 | UserState userState = getCurrentUserStateLocked(); |
| 2445 | handleDisplayMagnificationEnabledSettingChangedLocked(userState); |
| 2446 | updateInputFilterLocked(userState); |
| 2447 | scheduleSendStateToClientsLocked(userState); |
| 2448 | } |
| 2449 | } |
| 2450 | } else if (mEnabledAccessibilityServicesUri.equals(uri)) { |
| 2451 | synchronized (mLock) { |
| 2452 | // We will update when the automation service dies. |
| 2453 | if (mUiAutomationService == null) { |
| 2454 | UserState userState = getCurrentUserStateLocked(); |
| 2455 | populateEnabledAccessibilityServicesLocked(userState); |
| 2456 | manageServicesLocked(userState); |
| 2457 | } |
| 2458 | } |
| 2459 | } else if (mTouchExplorationGrantedAccessibilityServicesUri.equals(uri)) { |
| 2460 | synchronized (mLock) { |
| 2461 | // We will update when the automation service dies. |
| 2462 | if (mUiAutomationService == null) { |
| 2463 | UserState userState = getCurrentUserStateLocked(); |
| 2464 | populateTouchExplorationGrantedAccessibilityServicesLocked(userState); |
| 2465 | handleTouchExplorationGrantedAccessibilityServicesChangedLocked(userState); |
| 2466 | } |
| 2467 | } |
| 2468 | } |
| 2469 | } |
| 2470 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 2471 | } |