Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.appops; |
| 18 | |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 19 | import static com.android.systemui.Dependency.BG_LOOPER_NAME; |
| 20 | |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 21 | import android.app.AppOpsManager; |
| 22 | import android.content.Context; |
Philip P. Moltmann | e3dbb5a | 2019-03-07 10:06:11 -0800 | [diff] [blame] | 23 | import android.content.pm.PackageManager; |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 24 | import android.os.Handler; |
| 25 | import android.os.Looper; |
| 26 | import android.os.UserHandle; |
| 27 | import android.util.ArrayMap; |
| 28 | import android.util.ArraySet; |
| 29 | import android.util.Log; |
| 30 | |
| 31 | import com.android.internal.annotations.GuardedBy; |
| 32 | import com.android.internal.annotations.VisibleForTesting; |
Fabian Kozynski | b7b5879 | 2019-01-09 13:07:11 -0500 | [diff] [blame] | 33 | import com.android.systemui.Dumpable; |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 34 | |
Fabian Kozynski | b7b5879 | 2019-01-09 13:07:11 -0500 | [diff] [blame] | 35 | import java.io.FileDescriptor; |
| 36 | import java.io.PrintWriter; |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 37 | import java.util.ArrayList; |
| 38 | import java.util.List; |
| 39 | import java.util.Set; |
| 40 | |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 41 | import javax.inject.Inject; |
| 42 | import javax.inject.Named; |
| 43 | import javax.inject.Singleton; |
| 44 | |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 45 | /** |
| 46 | * Controller to keep track of applications that have requested access to given App Ops |
| 47 | * |
| 48 | * It can be subscribed to with callbacks. Additionally, it passes on the information to |
| 49 | * NotificationPresenter to be displayed to the user. |
| 50 | */ |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 51 | @Singleton |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 52 | public class AppOpsControllerImpl implements AppOpsController, |
Fabian Kozynski | 510585d | 2018-12-19 13:59:17 -0500 | [diff] [blame] | 53 | AppOpsManager.OnOpActiveChangedListener, |
Fabian Kozynski | b7b5879 | 2019-01-09 13:07:11 -0500 | [diff] [blame] | 54 | AppOpsManager.OnOpNotedListener, Dumpable { |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 55 | |
Fabian Kozynski | 510585d | 2018-12-19 13:59:17 -0500 | [diff] [blame] | 56 | private static final long NOTED_OP_TIME_DELAY_MS = 5000; |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 57 | private static final String TAG = "AppOpsControllerImpl"; |
| 58 | private static final boolean DEBUG = false; |
| 59 | private final Context mContext; |
| 60 | |
Fabian Kozynski | 510585d | 2018-12-19 13:59:17 -0500 | [diff] [blame] | 61 | private final AppOpsManager mAppOps; |
| 62 | private H mBGHandler; |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 63 | private final List<AppOpsController.Callback> mCallbacks = new ArrayList<>(); |
| 64 | private final ArrayMap<Integer, Set<Callback>> mCallbacksByCode = new ArrayMap<>(); |
Fabian Kozynski | 510585d | 2018-12-19 13:59:17 -0500 | [diff] [blame] | 65 | |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 66 | @GuardedBy("mActiveItems") |
| 67 | private final List<AppOpItem> mActiveItems = new ArrayList<>(); |
Fabian Kozynski | 510585d | 2018-12-19 13:59:17 -0500 | [diff] [blame] | 68 | @GuardedBy("mNotedItems") |
| 69 | private final List<AppOpItem> mNotedItems = new ArrayList<>(); |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 70 | |
Fabian Kozynski | 0b4592c | 2018-12-20 12:58:45 -0500 | [diff] [blame] | 71 | protected static final int[] OPS = new int[] { |
| 72 | AppOpsManager.OP_CAMERA, |
| 73 | AppOpsManager.OP_SYSTEM_ALERT_WINDOW, |
| 74 | AppOpsManager.OP_RECORD_AUDIO, |
| 75 | AppOpsManager.OP_COARSE_LOCATION, |
| 76 | AppOpsManager.OP_FINE_LOCATION |
| 77 | }; |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 78 | |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 79 | @Inject |
| 80 | public AppOpsControllerImpl(Context context, @Named(BG_LOOPER_NAME) Looper bgLooper) { |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 81 | mContext = context; |
| 82 | mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); |
| 83 | mBGHandler = new H(bgLooper); |
| 84 | final int numOps = OPS.length; |
| 85 | for (int i = 0; i < numOps; i++) { |
| 86 | mCallbacksByCode.put(OPS[i], new ArraySet<>()); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | @VisibleForTesting |
Fabian Kozynski | 510585d | 2018-12-19 13:59:17 -0500 | [diff] [blame] | 91 | protected void setBGHandler(H handler) { |
| 92 | mBGHandler = handler; |
| 93 | } |
| 94 | |
| 95 | @VisibleForTesting |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 96 | protected void setListening(boolean listening) { |
| 97 | if (listening) { |
| 98 | mAppOps.startWatchingActive(OPS, this); |
Fabian Kozynski | 0b4592c | 2018-12-20 12:58:45 -0500 | [diff] [blame] | 99 | mAppOps.startWatchingNoted(OPS, this); |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 100 | } else { |
| 101 | mAppOps.stopWatchingActive(this); |
Fabian Kozynski | 510585d | 2018-12-19 13:59:17 -0500 | [diff] [blame] | 102 | mAppOps.stopWatchingNoted(this); |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Adds a callback that will get notifified when an AppOp of the type the controller tracks |
| 108 | * changes |
| 109 | * |
| 110 | * @param callback Callback to report changes |
| 111 | * @param opsCodes App Ops the callback is interested in checking |
| 112 | * |
| 113 | * @see #removeCallback(int[], Callback) |
| 114 | */ |
| 115 | @Override |
| 116 | public void addCallback(int[] opsCodes, AppOpsController.Callback callback) { |
| 117 | boolean added = false; |
| 118 | final int numCodes = opsCodes.length; |
| 119 | for (int i = 0; i < numCodes; i++) { |
| 120 | if (mCallbacksByCode.containsKey(opsCodes[i])) { |
| 121 | mCallbacksByCode.get(opsCodes[i]).add(callback); |
| 122 | added = true; |
| 123 | } else { |
| 124 | if (DEBUG) Log.wtf(TAG, "APP_OP " + opsCodes[i] + " not supported"); |
| 125 | } |
| 126 | } |
| 127 | if (added) mCallbacks.add(callback); |
| 128 | if (!mCallbacks.isEmpty()) setListening(true); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Removes a callback from those notified when an AppOp of the type the controller tracks |
| 133 | * changes |
| 134 | * |
| 135 | * @param callback Callback to stop reporting changes |
| 136 | * @param opsCodes App Ops the callback was interested in checking |
| 137 | * |
| 138 | * @see #addCallback(int[], Callback) |
| 139 | */ |
| 140 | @Override |
| 141 | public void removeCallback(int[] opsCodes, AppOpsController.Callback callback) { |
| 142 | final int numCodes = opsCodes.length; |
| 143 | for (int i = 0; i < numCodes; i++) { |
| 144 | if (mCallbacksByCode.containsKey(opsCodes[i])) { |
| 145 | mCallbacksByCode.get(opsCodes[i]).remove(callback); |
| 146 | } |
| 147 | } |
| 148 | mCallbacks.remove(callback); |
| 149 | if (mCallbacks.isEmpty()) setListening(false); |
| 150 | } |
| 151 | |
Fabian Kozynski | 510585d | 2018-12-19 13:59:17 -0500 | [diff] [blame] | 152 | private AppOpItem getAppOpItem(List<AppOpItem> appOpList, int code, int uid, |
| 153 | String packageName) { |
| 154 | final int itemsQ = appOpList.size(); |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 155 | for (int i = 0; i < itemsQ; i++) { |
Fabian Kozynski | 510585d | 2018-12-19 13:59:17 -0500 | [diff] [blame] | 156 | AppOpItem item = appOpList.get(i); |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 157 | if (item.getCode() == code && item.getUid() == uid |
| 158 | && item.getPackageName().equals(packageName)) { |
| 159 | return item; |
| 160 | } |
| 161 | } |
| 162 | return null; |
| 163 | } |
| 164 | |
| 165 | private boolean updateActives(int code, int uid, String packageName, boolean active) { |
| 166 | synchronized (mActiveItems) { |
Fabian Kozynski | 510585d | 2018-12-19 13:59:17 -0500 | [diff] [blame] | 167 | AppOpItem item = getAppOpItem(mActiveItems, code, uid, packageName); |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 168 | if (item == null && active) { |
| 169 | item = new AppOpItem(code, uid, packageName, System.currentTimeMillis()); |
| 170 | mActiveItems.add(item); |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 171 | if (DEBUG) Log.w(TAG, "Added item: " + item.toString()); |
| 172 | return true; |
| 173 | } else if (item != null && !active) { |
| 174 | mActiveItems.remove(item); |
| 175 | if (DEBUG) Log.w(TAG, "Removed item: " + item.toString()); |
| 176 | return true; |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 177 | } |
| 178 | return false; |
| 179 | } |
| 180 | } |
| 181 | |
Fabian Kozynski | 510585d | 2018-12-19 13:59:17 -0500 | [diff] [blame] | 182 | private void removeNoted(int code, int uid, String packageName) { |
| 183 | AppOpItem item; |
| 184 | synchronized (mNotedItems) { |
| 185 | item = getAppOpItem(mNotedItems, code, uid, packageName); |
| 186 | if (item == null) return; |
| 187 | mNotedItems.remove(item); |
| 188 | if (DEBUG) Log.w(TAG, "Removed item: " + item.toString()); |
| 189 | } |
| 190 | notifySuscribers(code, uid, packageName, false); |
| 191 | } |
| 192 | |
| 193 | private void addNoted(int code, int uid, String packageName) { |
| 194 | AppOpItem item; |
| 195 | synchronized (mNotedItems) { |
| 196 | item = getAppOpItem(mNotedItems, code, uid, packageName); |
| 197 | if (item == null) { |
| 198 | item = new AppOpItem(code, uid, packageName, System.currentTimeMillis()); |
| 199 | mNotedItems.add(item); |
| 200 | if (DEBUG) Log.w(TAG, "Added item: " + item.toString()); |
| 201 | } |
| 202 | } |
| 203 | mBGHandler.scheduleRemoval(item, NOTED_OP_TIME_DELAY_MS); |
| 204 | } |
| 205 | |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 206 | /** |
Philip P. Moltmann | e3dbb5a | 2019-03-07 10:06:11 -0800 | [diff] [blame] | 207 | * Does the app-op item refer to a user sensitive permission. Only user sensitive permission |
| 208 | * should be shown to the user by default. |
| 209 | * |
| 210 | * @param item The item |
| 211 | * |
| 212 | * @return {@code true} iff the app-op item is user sensitive |
| 213 | */ |
| 214 | private boolean isUserSensitive(AppOpItem item) { |
| 215 | String permission = AppOpsManager.opToPermission(item.getCode()); |
| 216 | if (permission == null) { |
| 217 | return false; |
| 218 | } |
| 219 | int permFlags = mContext.getPackageManager().getPermissionFlags(permission, |
| 220 | item.getPackageName(), UserHandle.getUserHandleForUid(item.getUid())); |
| 221 | return (permFlags & PackageManager.FLAG_PERMISSION_USER_SENSITIVE_WHEN_GRANTED) != 0; |
| 222 | } |
| 223 | |
| 224 | /** |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 225 | * Returns a copy of the list containing all the active AppOps that the controller tracks. |
| 226 | * |
| 227 | * @return List of active AppOps information |
| 228 | */ |
| 229 | public List<AppOpItem> getActiveAppOps() { |
Philip P. Moltmann | e3dbb5a | 2019-03-07 10:06:11 -0800 | [diff] [blame] | 230 | return getActiveAppOpsForUser(UserHandle.USER_ALL); |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Returns a copy of the list containing all the active AppOps that the controller tracks, for |
| 235 | * a given user id. |
| 236 | * |
Philip P. Moltmann | e3dbb5a | 2019-03-07 10:06:11 -0800 | [diff] [blame] | 237 | * @param userId User id to track, can be {@link UserHandle#USER_ALL} |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 238 | * |
| 239 | * @return List of active AppOps information for that user id |
| 240 | */ |
| 241 | public List<AppOpItem> getActiveAppOpsForUser(int userId) { |
| 242 | List<AppOpItem> list = new ArrayList<>(); |
| 243 | synchronized (mActiveItems) { |
| 244 | final int numActiveItems = mActiveItems.size(); |
| 245 | for (int i = 0; i < numActiveItems; i++) { |
| 246 | AppOpItem item = mActiveItems.get(i); |
Philip P. Moltmann | e3dbb5a | 2019-03-07 10:06:11 -0800 | [diff] [blame] | 247 | if ((userId == UserHandle.USER_ALL || UserHandle.getUserId(item.getUid()) == userId) |
| 248 | && isUserSensitive(item)) { |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 249 | list.add(item); |
| 250 | } |
| 251 | } |
| 252 | } |
Fabian Kozynski | 510585d | 2018-12-19 13:59:17 -0500 | [diff] [blame] | 253 | synchronized (mNotedItems) { |
| 254 | final int numNotedItems = mNotedItems.size(); |
| 255 | for (int i = 0; i < numNotedItems; i++) { |
| 256 | AppOpItem item = mNotedItems.get(i); |
Philip P. Moltmann | e3dbb5a | 2019-03-07 10:06:11 -0800 | [diff] [blame] | 257 | if ((userId == UserHandle.USER_ALL || UserHandle.getUserId(item.getUid()) == userId) |
| 258 | && isUserSensitive(item)) { |
Fabian Kozynski | 510585d | 2018-12-19 13:59:17 -0500 | [diff] [blame] | 259 | list.add(item); |
| 260 | } |
| 261 | } |
| 262 | } |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 263 | return list; |
| 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public void onOpActiveChanged(int code, int uid, String packageName, boolean active) { |
| 268 | if (updateActives(code, uid, packageName, active)) { |
Fabian Kozynski | 510585d | 2018-12-19 13:59:17 -0500 | [diff] [blame] | 269 | notifySuscribers(code, uid, packageName, active); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | @Override |
Fabian Kozynski | 0b4592c | 2018-12-20 12:58:45 -0500 | [diff] [blame] | 274 | public void onOpNoted(int code, int uid, String packageName, int result) { |
Fabian Kozynski | 510585d | 2018-12-19 13:59:17 -0500 | [diff] [blame] | 275 | if (DEBUG) { |
| 276 | Log.w(TAG, "Op: " + code + " with result " + AppOpsManager.MODE_NAMES[result]); |
| 277 | } |
| 278 | if (result != AppOpsManager.MODE_ALLOWED) return; |
Fabian Kozynski | 0b4592c | 2018-12-20 12:58:45 -0500 | [diff] [blame] | 279 | addNoted(code, uid, packageName); |
| 280 | notifySuscribers(code, uid, packageName, true); |
Fabian Kozynski | 510585d | 2018-12-19 13:59:17 -0500 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | private void notifySuscribers(int code, int uid, String packageName, boolean active) { |
| 284 | if (mCallbacksByCode.containsKey(code)) { |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 285 | for (Callback cb: mCallbacksByCode.get(code)) { |
| 286 | cb.onActiveStateChanged(code, uid, packageName, active); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
Fabian Kozynski | b7b5879 | 2019-01-09 13:07:11 -0500 | [diff] [blame] | 291 | @Override |
| 292 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 293 | pw.println("AppOpsController state:"); |
| 294 | pw.println(" Active Items:"); |
| 295 | for (int i = 0; i < mActiveItems.size(); i++) { |
| 296 | final AppOpItem item = mActiveItems.get(i); |
| 297 | pw.print(" "); pw.println(item.toString()); |
| 298 | } |
| 299 | pw.println(" Noted Items:"); |
| 300 | for (int i = 0; i < mNotedItems.size(); i++) { |
| 301 | final AppOpItem item = mNotedItems.get(i); |
| 302 | pw.print(" "); pw.println(item.toString()); |
| 303 | } |
| 304 | |
| 305 | } |
| 306 | |
Fabian Kozynski | 510585d | 2018-12-19 13:59:17 -0500 | [diff] [blame] | 307 | protected final class H extends Handler { |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 308 | H(Looper looper) { |
| 309 | super(looper); |
| 310 | } |
| 311 | |
| 312 | public void scheduleRemoval(AppOpItem item, long timeToRemoval) { |
| 313 | removeCallbacksAndMessages(item); |
| 314 | postDelayed(new Runnable() { |
| 315 | @Override |
| 316 | public void run() { |
Fabian Kozynski | 510585d | 2018-12-19 13:59:17 -0500 | [diff] [blame] | 317 | removeNoted(item.getCode(), item.getUid(), item.getPackageName()); |
Fabian Kozynski | 1f32cf0 | 2018-10-23 12:23:31 -0400 | [diff] [blame] | 318 | } |
| 319 | }, item, timeToRemoval); |
| 320 | } |
| 321 | } |
| 322 | } |