Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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.server; |
| 18 | |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 19 | import android.Manifest; |
| 20 | import android.app.ActivityManager; |
| 21 | import android.app.ActivityThread; |
| 22 | import android.app.AppGlobals; |
| 23 | import android.app.AppOpsManager; |
| 24 | import android.content.Context; |
| 25 | import android.content.pm.ApplicationInfo; |
| 26 | import android.content.pm.IPackageManager; |
| 27 | import android.content.pm.PackageManager; |
| 28 | import android.content.pm.PackageManagerInternal; |
| 29 | import android.content.pm.UserInfo; |
| 30 | import android.media.AudioAttributes; |
| 31 | import android.os.AsyncTask; |
| 32 | import android.os.Binder; |
| 33 | import android.os.Bundle; |
| 34 | import android.os.Handler; |
| 35 | import android.os.IBinder; |
| 36 | import android.os.Process; |
| 37 | import android.os.RemoteException; |
| 38 | import android.os.ResultReceiver; |
| 39 | import android.os.ServiceManager; |
| 40 | import android.os.ShellCallback; |
| 41 | import android.os.ShellCommand; |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 42 | import android.os.SystemClock; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 43 | import android.os.UserHandle; |
| 44 | import android.os.UserManager; |
| 45 | import android.os.storage.StorageManagerInternal; |
| 46 | import android.util.ArrayMap; |
| 47 | import android.util.ArraySet; |
| 48 | import android.util.AtomicFile; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 49 | import android.util.Slog; |
| 50 | import android.util.SparseArray; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 51 | import android.util.SparseBooleanArray; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 52 | import android.util.SparseIntArray; |
| 53 | import android.util.TimeUtils; |
| 54 | import android.util.Xml; |
| 55 | |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 56 | import com.android.internal.annotations.VisibleForTesting; |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 57 | import com.android.internal.app.IAppOpsActiveCallback; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 58 | import com.android.internal.app.IAppOpsCallback; |
| 59 | import com.android.internal.app.IAppOpsService; |
| 60 | import com.android.internal.os.Zygote; |
| 61 | import com.android.internal.util.ArrayUtils; |
| 62 | import com.android.internal.util.DumpUtils; |
| 63 | import com.android.internal.util.FastXmlSerializer; |
| 64 | import com.android.internal.util.Preconditions; |
| 65 | import com.android.internal.util.XmlUtils; |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 66 | import com.android.internal.util.function.pooled.PooledLambda; |
Julia Reynolds | 6cb5fcc | 2018-02-27 17:33:52 -0500 | [diff] [blame] | 67 | |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 68 | import libcore.util.EmptyArray; |
| 69 | |
| 70 | import org.xmlpull.v1.XmlPullParser; |
| 71 | import org.xmlpull.v1.XmlPullParserException; |
| 72 | import org.xmlpull.v1.XmlSerializer; |
| 73 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 74 | import java.io.File; |
| 75 | import java.io.FileDescriptor; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 76 | import java.io.FileInputStream; |
| 77 | import java.io.FileNotFoundException; |
| 78 | import java.io.FileOutputStream; |
| 79 | import java.io.IOException; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 80 | import java.io.PrintWriter; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 81 | import java.nio.charset.StandardCharsets; |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 82 | import java.text.SimpleDateFormat; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 83 | import java.util.ArrayList; |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 84 | import java.util.Arrays; |
Svetoslav | 215b44a | 2015-08-04 19:03:40 -0700 | [diff] [blame] | 85 | import java.util.Collections; |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 86 | import java.util.Date; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 87 | import java.util.HashMap; |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 88 | import java.util.Iterator; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 89 | import java.util.List; |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 90 | import java.util.Map; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 91 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 92 | import static android.app.AppOpsManager.UID_STATE_BACKGROUND; |
| 93 | import static android.app.AppOpsManager.UID_STATE_CACHED; |
| 94 | import static android.app.AppOpsManager.UID_STATE_FOREGROUND; |
| 95 | import static android.app.AppOpsManager.UID_STATE_FOREGROUND_SERVICE; |
| 96 | import static android.app.AppOpsManager._NUM_UID_STATE; |
| 97 | import static android.app.AppOpsManager.UID_STATE_PERSISTENT; |
| 98 | import static android.app.AppOpsManager.UID_STATE_TOP; |
| 99 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 100 | public class AppOpsService extends IAppOpsService.Stub { |
| 101 | static final String TAG = "AppOps"; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 102 | static final boolean DEBUG = false; |
| 103 | |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 104 | private static final int NO_VERSION = -1; |
| 105 | /** Increment by one every time and add the corresponding upgrade logic in |
| 106 | * {@link #upgradeLocked(int)} below. The first version was 1 */ |
| 107 | private static final int CURRENT_VERSION = 1; |
| 108 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 109 | // Write at most every 30 minutes. |
| 110 | static final long WRITE_DELAY = DEBUG ? 1000 : 30*60*1000; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 111 | |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 112 | // How long we want for a drop in uid state to settle before applying it. |
| 113 | static final long STATE_SETTLE_TIME = 10*1000; |
| 114 | |
Svet Ganov | 3a95f83 | 2018-03-23 17:44:30 -0700 | [diff] [blame] | 115 | // Constant meaning that any UID should be matched when dispatching callbacks |
| 116 | private static final int UID_ANY = -2; |
| 117 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 118 | // Map from process states to the uid states we track. |
| 119 | private static final int[] PROCESS_STATE_TO_UID_STATE = new int[] { |
| 120 | UID_STATE_PERSISTENT, // ActivityManager.PROCESS_STATE_PERSISTENT |
| 121 | UID_STATE_PERSISTENT, // ActivityManager.PROCESS_STATE_PERSISTENT_UI |
| 122 | UID_STATE_TOP, // ActivityManager.PROCESS_STATE_TOP |
| 123 | UID_STATE_FOREGROUND_SERVICE, // ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE |
| 124 | UID_STATE_FOREGROUND, // ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE |
| 125 | UID_STATE_FOREGROUND, // ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND |
| 126 | UID_STATE_BACKGROUND, // ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND |
| 127 | UID_STATE_BACKGROUND, // ActivityManager.PROCESS_STATE_TRANSIENT_BACKGROUND |
| 128 | UID_STATE_BACKGROUND, // ActivityManager.PROCESS_STATE_BACKUP |
| 129 | UID_STATE_BACKGROUND, // ActivityManager.PROCESS_STATE_SERVICE |
| 130 | UID_STATE_BACKGROUND, // ActivityManager.PROCESS_STATE_RECEIVER |
| 131 | UID_STATE_CACHED, // ActivityManager.PROCESS_STATE_TOP_SLEEPING |
| 132 | UID_STATE_CACHED, // ActivityManager.PROCESS_STATE_HEAVY_WEIGHT |
| 133 | UID_STATE_CACHED, // ActivityManager.PROCESS_STATE_HOME |
| 134 | UID_STATE_CACHED, // ActivityManager.PROCESS_STATE_LAST_ACTIVITY |
| 135 | UID_STATE_CACHED, // ActivityManager.PROCESS_STATE_CACHED_ACTIVITY |
| 136 | UID_STATE_CACHED, // ActivityManager.PROCESS_STATE_CACHED_ACTIVITY_CLIENT |
| 137 | UID_STATE_CACHED, // ActivityManager.PROCESS_STATE_CACHED_RECENT |
| 138 | UID_STATE_CACHED, // ActivityManager.PROCESS_STATE_CACHED_EMPTY |
| 139 | UID_STATE_CACHED, // ActivityManager.PROCESS_STATE_NONEXISTENT |
| 140 | }; |
| 141 | |
| 142 | static final String[] UID_STATE_NAMES = new String[] { |
| 143 | "pers ", // UID_STATE_PERSISTENT |
| 144 | "top ", // UID_STATE_TOP |
| 145 | "fgsvc", // UID_STATE_FOREGROUND_SERVICE |
| 146 | "fg ", // UID_STATE_FOREGROUND |
| 147 | "bg ", // UID_STATE_BACKGROUND |
| 148 | "cch ", // UID_STATE_CACHED |
| 149 | }; |
| 150 | |
| 151 | static final String[] UID_STATE_TIME_ATTRS = new String[] { |
| 152 | "tp", // UID_STATE_PERSISTENT |
| 153 | "tt", // UID_STATE_TOP |
| 154 | "tfs", // UID_STATE_FOREGROUND_SERVICE |
| 155 | "tf", // UID_STATE_FOREGROUND |
| 156 | "tb", // UID_STATE_BACKGROUND |
| 157 | "tc", // UID_STATE_CACHED |
| 158 | }; |
| 159 | |
| 160 | static final String[] UID_STATE_REJECT_ATTRS = new String[] { |
| 161 | "rp", // UID_STATE_PERSISTENT |
| 162 | "rt", // UID_STATE_TOP |
| 163 | "rfs", // UID_STATE_FOREGROUND_SERVICE |
| 164 | "rf", // UID_STATE_FOREGROUND |
| 165 | "rb", // UID_STATE_BACKGROUND |
| 166 | "rc", // UID_STATE_CACHED |
| 167 | }; |
| 168 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 169 | Context mContext; |
| 170 | final AtomicFile mFile; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 171 | final Handler mHandler; |
| 172 | |
| 173 | boolean mWriteScheduled; |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 174 | boolean mFastWriteScheduled; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 175 | final Runnable mWriteRunner = new Runnable() { |
| 176 | public void run() { |
| 177 | synchronized (AppOpsService.this) { |
| 178 | mWriteScheduled = false; |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 179 | mFastWriteScheduled = false; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 180 | AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() { |
| 181 | @Override protected Void doInBackground(Void... params) { |
| 182 | writeState(); |
| 183 | return null; |
| 184 | } |
| 185 | }; |
| 186 | task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null); |
| 187 | } |
| 188 | } |
| 189 | }; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 190 | |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 191 | @VisibleForTesting |
| 192 | final SparseArray<UidState> mUidStates = new SparseArray<>(); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 193 | |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 194 | long mLastUptime; |
| 195 | |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 196 | /* |
| 197 | * These are app op restrictions imposed per user from various parties. |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 198 | */ |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 199 | private final ArrayMap<IBinder, ClientRestrictionState> mOpUserRestrictions = new ArrayMap<>(); |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 200 | |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 201 | @VisibleForTesting |
| 202 | static final class UidState { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 203 | public final int uid; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 204 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 205 | public int state = UID_STATE_CACHED; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 206 | public int pendingState = UID_STATE_CACHED; |
| 207 | public long pendingStateCommitTime; |
| 208 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 209 | public int startNesting; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 210 | public ArrayMap<String, Ops> pkgOps; |
| 211 | public SparseIntArray opModes; |
| 212 | |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 213 | public SparseBooleanArray foregroundOps; |
| 214 | |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 215 | public UidState(int uid) { |
| 216 | this.uid = uid; |
| 217 | } |
| 218 | |
| 219 | public void clear() { |
| 220 | pkgOps = null; |
| 221 | opModes = null; |
| 222 | } |
| 223 | |
| 224 | public boolean isDefault() { |
| 225 | return (pkgOps == null || pkgOps.isEmpty()) |
| 226 | && (opModes == null || opModes.size() <= 0); |
| 227 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 228 | |
| 229 | int evalMode(int mode) { |
| 230 | if (mode == AppOpsManager.MODE_FOREGROUND) { |
| 231 | return state <= UID_STATE_FOREGROUND_SERVICE |
| 232 | ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED; |
| 233 | } |
| 234 | return mode; |
| 235 | } |
| 236 | |
| 237 | public void evalForegroundOps() { |
| 238 | SparseBooleanArray which = null; |
| 239 | if (pkgOps != null) { |
| 240 | for (int i = pkgOps.size() - 1; i >= 0; i--) { |
| 241 | Ops ops = pkgOps.valueAt(i); |
| 242 | for (int j = ops.size() - 1; j >= 0; j--) { |
| 243 | if (ops.valueAt(j).mode == AppOpsManager.MODE_FOREGROUND) { |
| 244 | if (which == null) { |
| 245 | which = new SparseBooleanArray(); |
| 246 | } |
| 247 | which.put(ops.keyAt(j), true); |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | foregroundOps = which; |
| 253 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 256 | final static class Ops extends SparseArray<Op> { |
| 257 | final String packageName; |
| 258 | final UidState uidState; |
| 259 | final boolean isPrivileged; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 260 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 261 | Ops(String _packageName, UidState _uidState, boolean _isPrivileged) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 262 | packageName = _packageName; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 263 | uidState = _uidState; |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 264 | isPrivileged = _isPrivileged; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 268 | final static class Op { |
| 269 | final UidState uidState; |
| 270 | final int uid; |
| 271 | final String packageName; |
| 272 | final int op; |
| 273 | int proxyUid = -1; |
| 274 | String proxyPackageName; |
| 275 | int mode; |
| 276 | int duration; |
| 277 | long time[] = new long[_NUM_UID_STATE]; |
| 278 | long rejectTime[] = new long[_NUM_UID_STATE]; |
| 279 | int startNesting; |
| 280 | long startRealtime; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 281 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 282 | Op(UidState _uidState, String _packageName, int _op) { |
| 283 | uidState = _uidState; |
| 284 | uid = _uidState.uid; |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 285 | packageName = _packageName; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 286 | op = _op; |
David Braun | f5d8319 | 2013-09-16 13:43:51 -0700 | [diff] [blame] | 287 | mode = AppOpsManager.opToDefaultMode(op); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 288 | } |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 289 | |
| 290 | boolean hasAnyTime() { |
| 291 | for (int i = 0; i < AppOpsManager._NUM_UID_STATE; i++) { |
| 292 | if (time[i] != 0) { |
| 293 | return true; |
| 294 | } |
| 295 | if (rejectTime[i] != 0) { |
| 296 | return true; |
| 297 | } |
| 298 | } |
| 299 | return false; |
| 300 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 301 | |
| 302 | int getMode() { |
| 303 | return uidState.evalMode(mode); |
| 304 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 307 | final SparseArray<ArraySet<ModeCallback>> mOpModeWatchers = new SparseArray<>(); |
| 308 | final ArrayMap<String, ArraySet<ModeCallback>> mPackageModeWatchers = new ArrayMap<>(); |
| 309 | final ArrayMap<IBinder, ModeCallback> mModeWatchers = new ArrayMap<>(); |
| 310 | final ArrayMap<IBinder, SparseArray<ActiveCallback>> mActiveWatchers = new ArrayMap<>(); |
Dianne Hackborn | 68d7655 | 2017-02-27 15:32:03 -0800 | [diff] [blame] | 311 | final SparseArray<SparseArray<Restriction>> mAudioRestrictions = new SparseArray<>(); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 312 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 313 | final class ModeCallback implements DeathRecipient { |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 314 | final IAppOpsCallback mCallback; |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 315 | final int mWatchingUid; |
| 316 | final int mCallingUid; |
| 317 | final int mCallingPid; |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 318 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 319 | ModeCallback(IAppOpsCallback callback, int watchingUid, int callingUid, |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 320 | int callingPid) { |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 321 | mCallback = callback; |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 322 | mWatchingUid = watchingUid; |
| 323 | mCallingUid = callingUid; |
| 324 | mCallingPid = callingPid; |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 325 | try { |
| 326 | mCallback.asBinder().linkToDeath(this, 0); |
| 327 | } catch (RemoteException e) { |
| 328 | } |
| 329 | } |
| 330 | |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 331 | @Override |
| 332 | public String toString() { |
| 333 | StringBuilder sb = new StringBuilder(128); |
| 334 | sb.append("ModeCallback{"); |
| 335 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 336 | sb.append(" watchinguid="); |
| 337 | UserHandle.formatUid(sb, mWatchingUid); |
| 338 | sb.append(" from uid="); |
| 339 | UserHandle.formatUid(sb, mCallingUid); |
| 340 | sb.append(" pid="); |
| 341 | sb.append(mCallingPid); |
| 342 | sb.append('}'); |
| 343 | return sb.toString(); |
| 344 | } |
| 345 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 346 | void unlinkToDeath() { |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 347 | mCallback.asBinder().unlinkToDeath(this, 0); |
| 348 | } |
| 349 | |
| 350 | @Override |
| 351 | public void binderDied() { |
| 352 | stopWatchingMode(mCallback); |
| 353 | } |
| 354 | } |
| 355 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 356 | final class ActiveCallback implements DeathRecipient { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 357 | final IAppOpsActiveCallback mCallback; |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 358 | final int mWatchingUid; |
| 359 | final int mCallingUid; |
| 360 | final int mCallingPid; |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 361 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 362 | ActiveCallback(IAppOpsActiveCallback callback, int watchingUid, int callingUid, |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 363 | int callingPid) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 364 | mCallback = callback; |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 365 | mWatchingUid = watchingUid; |
| 366 | mCallingUid = callingUid; |
| 367 | mCallingPid = callingPid; |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 368 | try { |
| 369 | mCallback.asBinder().linkToDeath(this, 0); |
| 370 | } catch (RemoteException e) { |
| 371 | } |
| 372 | } |
| 373 | |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 374 | @Override |
| 375 | public String toString() { |
| 376 | StringBuilder sb = new StringBuilder(128); |
| 377 | sb.append("ActiveCallback{"); |
| 378 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 379 | sb.append(" watchinguid="); |
| 380 | UserHandle.formatUid(sb, mWatchingUid); |
| 381 | sb.append(" from uid="); |
| 382 | UserHandle.formatUid(sb, mCallingUid); |
| 383 | sb.append(" pid="); |
| 384 | sb.append(mCallingPid); |
| 385 | sb.append('}'); |
| 386 | return sb.toString(); |
| 387 | } |
| 388 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 389 | void destroy() { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 390 | mCallback.asBinder().unlinkToDeath(this, 0); |
| 391 | } |
| 392 | |
| 393 | @Override |
| 394 | public void binderDied() { |
| 395 | stopWatchingActive(mCallback); |
| 396 | } |
| 397 | } |
| 398 | |
Svet Ganov | a7a0db6 | 2018-02-27 20:08:01 -0800 | [diff] [blame] | 399 | final ArrayMap<IBinder, ClientState> mClients = new ArrayMap<>(); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 400 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 401 | final class ClientState extends Binder implements DeathRecipient { |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 402 | final ArrayList<Op> mStartedOps = new ArrayList<>(); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 403 | final IBinder mAppToken; |
| 404 | final int mPid; |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 405 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 406 | ClientState(IBinder appToken) { |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 407 | mAppToken = appToken; |
| 408 | mPid = Binder.getCallingPid(); |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 409 | // Watch only for remote processes dying |
| 410 | if (!(appToken instanceof Binder)) { |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 411 | try { |
| 412 | mAppToken.linkToDeath(this, 0); |
| 413 | } catch (RemoteException e) { |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 414 | /* do nothing */ |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | @Override |
| 420 | public String toString() { |
| 421 | return "ClientState{" + |
| 422 | "mAppToken=" + mAppToken + |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 423 | ", " + "pid=" + mPid + |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 424 | '}'; |
| 425 | } |
| 426 | |
| 427 | @Override |
| 428 | public void binderDied() { |
| 429 | synchronized (AppOpsService.this) { |
| 430 | for (int i=mStartedOps.size()-1; i>=0; i--) { |
Svet Ganov | a7a0db6 | 2018-02-27 20:08:01 -0800 | [diff] [blame] | 431 | finishOperationLocked(mStartedOps.get(i), /*finishNested*/ true); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 432 | } |
| 433 | mClients.remove(mAppToken); |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 438 | public AppOpsService(File storagePath, Handler handler) { |
Jeff Sharkey | 5f3e934 | 2017-03-13 14:53:11 -0600 | [diff] [blame] | 439 | LockGuard.installLock(this, LockGuard.INDEX_APP_OPS); |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 440 | mFile = new AtomicFile(storagePath, "appops"); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 441 | mHandler = handler; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 442 | readState(); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 443 | } |
David Braun | f5d8319 | 2013-09-16 13:43:51 -0700 | [diff] [blame] | 444 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 445 | public void publish(Context context) { |
| 446 | mContext = context; |
| 447 | ServiceManager.addService(Context.APP_OPS_SERVICE, asBinder()); |
| 448 | } |
| 449 | |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 450 | public void systemReady() { |
| 451 | synchronized (this) { |
| 452 | boolean changed = false; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 453 | for (int i = mUidStates.size() - 1; i >= 0; i--) { |
| 454 | UidState uidState = mUidStates.valueAt(i); |
| 455 | |
| 456 | String[] packageNames = getPackagesForUid(uidState.uid); |
| 457 | if (ArrayUtils.isEmpty(packageNames)) { |
| 458 | uidState.clear(); |
| 459 | mUidStates.removeAt(i); |
| 460 | changed = true; |
| 461 | continue; |
| 462 | } |
| 463 | |
| 464 | ArrayMap<String, Ops> pkgs = uidState.pkgOps; |
| 465 | if (pkgs == null) { |
| 466 | continue; |
| 467 | } |
| 468 | |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 469 | Iterator<Ops> it = pkgs.values().iterator(); |
| 470 | while (it.hasNext()) { |
| 471 | Ops ops = it.next(); |
Jeff Sharkey | e2ed23e | 2015-10-29 19:00:44 -0700 | [diff] [blame] | 472 | int curUid = -1; |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 473 | try { |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 474 | curUid = AppGlobals.getPackageManager().getPackageUid(ops.packageName, |
| 475 | PackageManager.MATCH_UNINSTALLED_PACKAGES, |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 476 | UserHandle.getUserId(ops.uidState.uid)); |
Jeff Sharkey | e2ed23e | 2015-10-29 19:00:44 -0700 | [diff] [blame] | 477 | } catch (RemoteException ignored) { |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 478 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 479 | if (curUid != ops.uidState.uid) { |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 480 | Slog.i(TAG, "Pruning old package " + ops.packageName |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 481 | + "/" + ops.uidState + ": new uid=" + curUid); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 482 | it.remove(); |
| 483 | changed = true; |
| 484 | } |
| 485 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 486 | |
| 487 | if (uidState.isDefault()) { |
| 488 | mUidStates.removeAt(i); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | if (changed) { |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 492 | scheduleFastWriteLocked(); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 493 | } |
| 494 | } |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 495 | |
Suprabh Shukla | aef2513 | 2017-01-23 18:09:03 -0800 | [diff] [blame] | 496 | PackageManagerInternal packageManagerInternal = LocalServices.getService( |
| 497 | PackageManagerInternal.class); |
| 498 | packageManagerInternal.setExternalSourcesPolicy( |
| 499 | new PackageManagerInternal.ExternalSourcesPolicy() { |
| 500 | @Override |
| 501 | public int getPackageTrustedToInstallApps(String packageName, int uid) { |
| 502 | int appOpMode = checkOperation(AppOpsManager.OP_REQUEST_INSTALL_PACKAGES, |
| 503 | uid, packageName); |
| 504 | switch (appOpMode) { |
| 505 | case AppOpsManager.MODE_ALLOWED: |
| 506 | return PackageManagerInternal.ExternalSourcesPolicy.USER_TRUSTED; |
| 507 | case AppOpsManager.MODE_ERRORED: |
| 508 | return PackageManagerInternal.ExternalSourcesPolicy.USER_BLOCKED; |
| 509 | default: |
| 510 | return PackageManagerInternal.ExternalSourcesPolicy.USER_DEFAULT; |
| 511 | } |
| 512 | } |
| 513 | }); |
| 514 | |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 515 | StorageManagerInternal storageManagerInternal = LocalServices.getService( |
| 516 | StorageManagerInternal.class); |
| 517 | storageManagerInternal.addExternalStoragePolicy( |
| 518 | new StorageManagerInternal.ExternalStorageMountPolicy() { |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 519 | @Override |
| 520 | public int getMountMode(int uid, String packageName) { |
| 521 | if (Process.isIsolated(uid)) { |
| 522 | return Zygote.MOUNT_EXTERNAL_NONE; |
| 523 | } |
| 524 | if (noteOperation(AppOpsManager.OP_READ_EXTERNAL_STORAGE, uid, |
| 525 | packageName) != AppOpsManager.MODE_ALLOWED) { |
| 526 | return Zygote.MOUNT_EXTERNAL_NONE; |
| 527 | } |
| 528 | if (noteOperation(AppOpsManager.OP_WRITE_EXTERNAL_STORAGE, uid, |
| 529 | packageName) != AppOpsManager.MODE_ALLOWED) { |
| 530 | return Zygote.MOUNT_EXTERNAL_READ; |
| 531 | } |
| 532 | return Zygote.MOUNT_EXTERNAL_WRITE; |
| 533 | } |
| 534 | |
| 535 | @Override |
| 536 | public boolean hasExternalStorage(int uid, String packageName) { |
| 537 | final int mountMode = getMountMode(uid, packageName); |
| 538 | return mountMode == Zygote.MOUNT_EXTERNAL_READ |
| 539 | || mountMode == Zygote.MOUNT_EXTERNAL_WRITE; |
| 540 | } |
| 541 | }); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | public void packageRemoved(int uid, String packageName) { |
| 545 | synchronized (this) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 546 | UidState uidState = mUidStates.get(uid); |
| 547 | if (uidState == null) { |
| 548 | return; |
| 549 | } |
| 550 | |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 551 | Ops ops = null; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 552 | |
| 553 | // Remove any package state if such. |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 554 | if (uidState.pkgOps != null) { |
| 555 | ops = uidState.pkgOps.remove(packageName); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | // If we just nuked the last package state check if the UID is valid. |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 559 | if (ops != null && uidState.pkgOps.isEmpty() |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 560 | && getPackagesForUid(uid).length <= 0) { |
| 561 | mUidStates.remove(uid); |
| 562 | } |
| 563 | |
Svet Ganov | a7a0db6 | 2018-02-27 20:08:01 -0800 | [diff] [blame] | 564 | // Finish ops other packages started on behalf of the package. |
| 565 | final int clientCount = mClients.size(); |
| 566 | for (int i = 0; i < clientCount; i++) { |
| 567 | final ClientState client = mClients.valueAt(i); |
| 568 | if (client.mStartedOps == null) { |
| 569 | continue; |
| 570 | } |
| 571 | final int opCount = client.mStartedOps.size(); |
| 572 | for (int j = opCount - 1; j >= 0; j--) { |
| 573 | final Op op = client.mStartedOps.get(j); |
| 574 | if (uid == op.uid && packageName.equals(op.packageName)) { |
| 575 | finishOperationLocked(op, /*finishNested*/ true); |
| 576 | client.mStartedOps.remove(j); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 577 | if (op.startNesting <= 0) { |
Svet Ganov | a7a0db6 | 2018-02-27 20:08:01 -0800 | [diff] [blame] | 578 | scheduleOpActiveChangedIfNeededLocked(op.op, |
| 579 | uid, packageName, false); |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 585 | if (ops != null) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 586 | scheduleFastWriteLocked(); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 587 | |
| 588 | final int opCount = ops.size(); |
| 589 | for (int i = 0; i < opCount; i++) { |
| 590 | final Op op = ops.valueAt(i); |
| 591 | if (op.duration == -1) { |
| 592 | scheduleOpActiveChangedIfNeededLocked( |
| 593 | op.op, op.uid, op.packageName, false); |
| 594 | } |
| 595 | } |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | public void uidRemoved(int uid) { |
| 601 | synchronized (this) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 602 | if (mUidStates.indexOfKey(uid) >= 0) { |
| 603 | mUidStates.remove(uid); |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 604 | scheduleFastWriteLocked(); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | } |
| 608 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 609 | public void updateUidProcState(int uid, int procState) { |
| 610 | synchronized (this) { |
| 611 | final UidState uidState = getUidStateLocked(uid, true); |
| 612 | final int newState = PROCESS_STATE_TO_UID_STATE[procState]; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 613 | if (uidState != null && uidState.pendingState != newState) { |
| 614 | if (newState < uidState.state) { |
| 615 | // We are moving to a more important state, always do it immediately. |
| 616 | uidState.state = newState; |
| 617 | uidState.pendingStateCommitTime = 0; |
| 618 | } else if (uidState.pendingStateCommitTime == 0) { |
| 619 | // We are moving to a less important state for the first time, |
| 620 | // delay the application for a bit. |
| 621 | uidState.pendingStateCommitTime = SystemClock.uptimeMillis() + STATE_SETTLE_TIME; |
| 622 | } |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 623 | if (uidState.startNesting != 0) { |
| 624 | // There is some actively running operation... need to find it |
| 625 | // and appropriately update its state. |
| 626 | final long now = System.currentTimeMillis(); |
| 627 | for (int i = uidState.pkgOps.size() - 1; i >= 0; i--) { |
| 628 | final Ops ops = uidState.pkgOps.valueAt(i); |
| 629 | for (int j = ops.size() - 1; j >= 0; j--) { |
| 630 | final Op op = ops.valueAt(j); |
| 631 | if (op.startNesting > 0) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 632 | op.time[uidState.pendingState] = now; |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 633 | op.time[newState] = now; |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 638 | uidState.pendingState = newState; |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 639 | } |
| 640 | } |
| 641 | } |
| 642 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 643 | public void shutdown() { |
| 644 | Slog.w(TAG, "Writing app ops before shutdown..."); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 645 | boolean doWrite = false; |
| 646 | synchronized (this) { |
| 647 | if (mWriteScheduled) { |
| 648 | mWriteScheduled = false; |
| 649 | doWrite = true; |
| 650 | } |
| 651 | } |
| 652 | if (doWrite) { |
| 653 | writeState(); |
| 654 | } |
| 655 | } |
| 656 | |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 657 | private ArrayList<AppOpsManager.OpEntry> collectOps(Ops pkgOps, int[] ops) { |
| 658 | ArrayList<AppOpsManager.OpEntry> resOps = null; |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 659 | final long elapsedNow = SystemClock.elapsedRealtime(); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 660 | if (ops == null) { |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 661 | resOps = new ArrayList<>(); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 662 | for (int j=0; j<pkgOps.size(); j++) { |
| 663 | Op curOp = pkgOps.valueAt(j); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 664 | long duration = curOp.duration == -1 |
| 665 | ? (elapsedNow - curOp.startRealtime) |
| 666 | : curOp.duration; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 667 | resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.mode, curOp.time, |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 668 | curOp.rejectTime, (int) duration, curOp.proxyUid, |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 669 | curOp.proxyPackageName)); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 670 | } |
| 671 | } else { |
| 672 | for (int j=0; j<ops.length; j++) { |
| 673 | Op curOp = pkgOps.get(ops[j]); |
| 674 | if (curOp != null) { |
| 675 | if (resOps == null) { |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 676 | resOps = new ArrayList<>(); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 677 | } |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 678 | long duration = curOp.duration == -1 |
| 679 | ? (elapsedNow - curOp.startRealtime) |
| 680 | : curOp.duration; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 681 | resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.mode, curOp.time, |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 682 | curOp.rejectTime, (int) duration, curOp.proxyUid, |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 683 | curOp.proxyPackageName)); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | } |
| 687 | return resOps; |
| 688 | } |
| 689 | |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 690 | private ArrayList<AppOpsManager.OpEntry> collectOps(SparseIntArray uidOps, int[] ops) { |
| 691 | ArrayList<AppOpsManager.OpEntry> resOps = null; |
| 692 | if (ops == null) { |
| 693 | resOps = new ArrayList<>(); |
| 694 | for (int j=0; j<uidOps.size(); j++) { |
| 695 | resOps.add(new AppOpsManager.OpEntry(uidOps.keyAt(j), uidOps.valueAt(j), |
| 696 | 0, 0, 0, -1, null)); |
| 697 | } |
| 698 | } else { |
| 699 | for (int j=0; j<ops.length; j++) { |
| 700 | int index = uidOps.indexOfKey(ops[j]); |
| 701 | if (index >= 0) { |
| 702 | if (resOps == null) { |
| 703 | resOps = new ArrayList<>(); |
| 704 | } |
| 705 | resOps.add(new AppOpsManager.OpEntry(uidOps.keyAt(index), uidOps.valueAt(index), |
| 706 | 0, 0, 0, -1, null)); |
| 707 | } |
| 708 | } |
| 709 | } |
| 710 | return resOps; |
| 711 | } |
| 712 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 713 | @Override |
| 714 | public List<AppOpsManager.PackageOps> getPackagesForOps(int[] ops) { |
| 715 | mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS, |
| 716 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 717 | ArrayList<AppOpsManager.PackageOps> res = null; |
| 718 | synchronized (this) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 719 | final int uidStateCount = mUidStates.size(); |
| 720 | for (int i = 0; i < uidStateCount; i++) { |
| 721 | UidState uidState = mUidStates.valueAt(i); |
| 722 | if (uidState.pkgOps == null || uidState.pkgOps.isEmpty()) { |
| 723 | continue; |
| 724 | } |
| 725 | ArrayMap<String, Ops> packages = uidState.pkgOps; |
| 726 | final int packageCount = packages.size(); |
| 727 | for (int j = 0; j < packageCount; j++) { |
| 728 | Ops pkgOps = packages.valueAt(j); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 729 | ArrayList<AppOpsManager.OpEntry> resOps = collectOps(pkgOps, ops); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 730 | if (resOps != null) { |
| 731 | if (res == null) { |
| 732 | res = new ArrayList<AppOpsManager.PackageOps>(); |
| 733 | } |
| 734 | AppOpsManager.PackageOps resPackage = new AppOpsManager.PackageOps( |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 735 | pkgOps.packageName, pkgOps.uidState.uid, resOps); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 736 | res.add(resPackage); |
| 737 | } |
| 738 | } |
| 739 | } |
| 740 | } |
| 741 | return res; |
| 742 | } |
| 743 | |
| 744 | @Override |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 745 | public List<AppOpsManager.PackageOps> getOpsForPackage(int uid, String packageName, |
| 746 | int[] ops) { |
| 747 | mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS, |
| 748 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 749 | String resolvedPackageName = resolvePackageName(uid, packageName); |
| 750 | if (resolvedPackageName == null) { |
| 751 | return Collections.emptyList(); |
| 752 | } |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 753 | synchronized (this) { |
Yohei Yukawa | a965d65 | 2017-10-12 15:02:26 -0700 | [diff] [blame] | 754 | Ops pkgOps = getOpsRawLocked(uid, resolvedPackageName, false /* edit */, |
| 755 | false /* uidMismatchExpected */); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 756 | if (pkgOps == null) { |
| 757 | return null; |
| 758 | } |
| 759 | ArrayList<AppOpsManager.OpEntry> resOps = collectOps(pkgOps, ops); |
| 760 | if (resOps == null) { |
| 761 | return null; |
| 762 | } |
| 763 | ArrayList<AppOpsManager.PackageOps> res = new ArrayList<AppOpsManager.PackageOps>(); |
| 764 | AppOpsManager.PackageOps resPackage = new AppOpsManager.PackageOps( |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 765 | pkgOps.packageName, pkgOps.uidState.uid, resOps); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 766 | res.add(resPackage); |
| 767 | return res; |
| 768 | } |
| 769 | } |
| 770 | |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 771 | @Override |
| 772 | public List<AppOpsManager.PackageOps> getUidOps(int uid, int[] ops) { |
| 773 | mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS, |
| 774 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 775 | synchronized (this) { |
| 776 | UidState uidState = getUidStateLocked(uid, false); |
| 777 | if (uidState == null) { |
| 778 | return null; |
| 779 | } |
| 780 | ArrayList<AppOpsManager.OpEntry> resOps = collectOps(uidState.opModes, ops); |
| 781 | if (resOps == null) { |
| 782 | return null; |
| 783 | } |
| 784 | ArrayList<AppOpsManager.PackageOps> res = new ArrayList<AppOpsManager.PackageOps>(); |
| 785 | AppOpsManager.PackageOps resPackage = new AppOpsManager.PackageOps( |
| 786 | null, uidState.uid, resOps); |
| 787 | res.add(resPackage); |
| 788 | return res; |
| 789 | } |
| 790 | } |
| 791 | |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 792 | private void pruneOp(Op op, int uid, String packageName) { |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 793 | if (!op.hasAnyTime()) { |
Yohei Yukawa | a965d65 | 2017-10-12 15:02:26 -0700 | [diff] [blame] | 794 | Ops ops = getOpsRawLocked(uid, packageName, false /* edit */, |
| 795 | false /* uidMismatchExpected */); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 796 | if (ops != null) { |
| 797 | ops.remove(op.op); |
| 798 | if (ops.size() <= 0) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 799 | UidState uidState = ops.uidState; |
| 800 | ArrayMap<String, Ops> pkgOps = uidState.pkgOps; |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 801 | if (pkgOps != null) { |
| 802 | pkgOps.remove(ops.packageName); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 803 | if (pkgOps.isEmpty()) { |
| 804 | uidState.pkgOps = null; |
| 805 | } |
| 806 | if (uidState.isDefault()) { |
| 807 | mUidStates.remove(uid); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 808 | } |
| 809 | } |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | } |
| 814 | |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 815 | @Override |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 816 | public void setUidMode(int code, int uid, int mode) { |
| 817 | if (Binder.getCallingPid() != Process.myPid()) { |
Dianne Hackborn | bf1b57d | 2018-03-07 12:42:47 -0800 | [diff] [blame] | 818 | mContext.enforcePermission(android.Manifest.permission.MANAGE_APP_OPS_MODES, |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 819 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 820 | } |
| 821 | verifyIncomingOp(code); |
| 822 | code = AppOpsManager.opToSwitch(code); |
| 823 | |
| 824 | synchronized (this) { |
| 825 | final int defaultMode = AppOpsManager.opToDefaultMode(code); |
| 826 | |
| 827 | UidState uidState = getUidStateLocked(uid, false); |
| 828 | if (uidState == null) { |
| 829 | if (mode == defaultMode) { |
| 830 | return; |
| 831 | } |
| 832 | uidState = new UidState(uid); |
| 833 | uidState.opModes = new SparseIntArray(); |
| 834 | uidState.opModes.put(code, mode); |
| 835 | mUidStates.put(uid, uidState); |
| 836 | scheduleWriteLocked(); |
| 837 | } else if (uidState.opModes == null) { |
| 838 | if (mode != defaultMode) { |
| 839 | uidState.opModes = new SparseIntArray(); |
| 840 | uidState.opModes.put(code, mode); |
| 841 | scheduleWriteLocked(); |
| 842 | } |
| 843 | } else { |
| 844 | if (uidState.opModes.get(code) == mode) { |
| 845 | return; |
| 846 | } |
| 847 | if (mode == defaultMode) { |
| 848 | uidState.opModes.delete(code); |
| 849 | if (uidState.opModes.size() <= 0) { |
| 850 | uidState.opModes = null; |
| 851 | } |
| 852 | } else { |
| 853 | uidState.opModes.put(code, mode); |
| 854 | } |
| 855 | scheduleWriteLocked(); |
| 856 | } |
| 857 | } |
| 858 | |
Svetoslav | 215b44a | 2015-08-04 19:03:40 -0700 | [diff] [blame] | 859 | String[] uidPackageNames = getPackagesForUid(uid); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 860 | ArrayMap<ModeCallback, ArraySet<String>> callbackSpecs = null; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 861 | |
riddle_hsu | 40b300f | 2015-11-23 13:22:03 +0800 | [diff] [blame] | 862 | synchronized (this) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 863 | ArraySet<ModeCallback> callbacks = mOpModeWatchers.get(code); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 864 | if (callbacks != null) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 865 | final int callbackCount = callbacks.size(); |
| 866 | for (int i = 0; i < callbackCount; i++) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 867 | ModeCallback callback = callbacks.valueAt(i); |
riddle_hsu | 40b300f | 2015-11-23 13:22:03 +0800 | [diff] [blame] | 868 | ArraySet<String> changedPackages = new ArraySet<>(); |
| 869 | Collections.addAll(changedPackages, uidPackageNames); |
| 870 | callbackSpecs = new ArrayMap<>(); |
| 871 | callbackSpecs.put(callback, changedPackages); |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | for (String uidPackageName : uidPackageNames) { |
| 876 | callbacks = mPackageModeWatchers.get(uidPackageName); |
| 877 | if (callbacks != null) { |
| 878 | if (callbackSpecs == null) { |
| 879 | callbackSpecs = new ArrayMap<>(); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 880 | } |
riddle_hsu | 40b300f | 2015-11-23 13:22:03 +0800 | [diff] [blame] | 881 | final int callbackCount = callbacks.size(); |
| 882 | for (int i = 0; i < callbackCount; i++) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 883 | ModeCallback callback = callbacks.valueAt(i); |
riddle_hsu | 40b300f | 2015-11-23 13:22:03 +0800 | [diff] [blame] | 884 | ArraySet<String> changedPackages = callbackSpecs.get(callback); |
| 885 | if (changedPackages == null) { |
| 886 | changedPackages = new ArraySet<>(); |
| 887 | callbackSpecs.put(callback, changedPackages); |
| 888 | } |
| 889 | changedPackages.add(uidPackageName); |
| 890 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 891 | } |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | if (callbackSpecs == null) { |
| 896 | return; |
| 897 | } |
| 898 | |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 899 | for (int i = 0; i < callbackSpecs.size(); i++) { |
| 900 | final ModeCallback callback = callbackSpecs.keyAt(i); |
| 901 | final ArraySet<String> reportedPackageNames = callbackSpecs.valueAt(i); |
| 902 | if (reportedPackageNames == null) { |
| 903 | mHandler.sendMessage(PooledLambda.obtainMessage( |
| 904 | AppOpsService::notifyOpChanged, |
| 905 | this, callback, code, uid, (String) null)); |
| 906 | |
| 907 | } else { |
| 908 | final int reportedPackageCount = reportedPackageNames.size(); |
| 909 | for (int j = 0; j < reportedPackageCount; j++) { |
| 910 | final String reportedPackageName = reportedPackageNames.valueAt(j); |
| 911 | mHandler.sendMessage(PooledLambda.obtainMessage( |
| 912 | AppOpsService::notifyOpChanged, |
| 913 | this, callback, code, uid, reportedPackageName)); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 914 | } |
| 915 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 916 | } |
| 917 | } |
| 918 | |
| 919 | @Override |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 920 | public void setMode(int code, int uid, String packageName, int mode) { |
Dianne Hackborn | b64afe1 | 2014-07-22 16:29:04 -0700 | [diff] [blame] | 921 | if (Binder.getCallingPid() != Process.myPid()) { |
Dianne Hackborn | bf1b57d | 2018-03-07 12:42:47 -0800 | [diff] [blame] | 922 | mContext.enforcePermission(android.Manifest.permission.MANAGE_APP_OPS_MODES, |
Dianne Hackborn | b64afe1 | 2014-07-22 16:29:04 -0700 | [diff] [blame] | 923 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
Dianne Hackborn | 133b9df | 2014-07-01 13:06:10 -0700 | [diff] [blame] | 924 | } |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 925 | verifyIncomingOp(code); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 926 | ArraySet<ModeCallback> repCbs = null; |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 927 | code = AppOpsManager.opToSwitch(code); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 928 | synchronized (this) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 929 | UidState uidState = getUidStateLocked(uid, false); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 930 | Op op = getOpLocked(code, uid, packageName, true); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 931 | if (op != null) { |
| 932 | if (op.mode != mode) { |
| 933 | op.mode = mode; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 934 | if (uidState != null) { |
| 935 | uidState.evalForegroundOps(); |
| 936 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 937 | ArraySet<ModeCallback> cbs = mOpModeWatchers.get(code); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 938 | if (cbs != null) { |
| 939 | if (repCbs == null) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 940 | repCbs = new ArraySet<>(); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 941 | } |
| 942 | repCbs.addAll(cbs); |
| 943 | } |
| 944 | cbs = mPackageModeWatchers.get(packageName); |
| 945 | if (cbs != null) { |
| 946 | if (repCbs == null) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 947 | repCbs = new ArraySet<>(); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 948 | } |
| 949 | repCbs.addAll(cbs); |
| 950 | } |
David Braun | f5d8319 | 2013-09-16 13:43:51 -0700 | [diff] [blame] | 951 | if (mode == AppOpsManager.opToDefaultMode(op.op)) { |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 952 | // If going into the default mode, prune this op |
| 953 | // if there is nothing else interesting in it. |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 954 | pruneOp(op, uid, packageName); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 955 | } |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 956 | scheduleFastWriteLocked(); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 957 | } |
| 958 | } |
| 959 | } |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 960 | if (repCbs != null) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 961 | mHandler.sendMessage(PooledLambda.obtainMessage( |
| 962 | AppOpsService::notifyOpChanged, |
| 963 | this, repCbs, code, uid, packageName)); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 964 | } |
| 965 | } |
| 966 | |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 967 | private void notifyOpChanged(ArraySet<ModeCallback> callbacks, int code, |
| 968 | int uid, String packageName) { |
| 969 | for (int i = 0; i < callbacks.size(); i++) { |
| 970 | final ModeCallback callback = callbacks.valueAt(i); |
| 971 | notifyOpChanged(callback, code, uid, packageName); |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | private void notifyOpChanged(ModeCallback callback, int code, |
| 976 | int uid, String packageName) { |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 977 | if (uid != UID_ANY && callback.mWatchingUid >= 0 && callback.mWatchingUid != uid) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 978 | return; |
| 979 | } |
| 980 | // There are components watching for mode changes such as window manager |
| 981 | // and location manager which are in our process. The callbacks in these |
| 982 | // components may require permissions our remote caller does not have. |
| 983 | final long identity = Binder.clearCallingIdentity(); |
| 984 | try { |
| 985 | callback.mCallback.opChanged(code, uid, packageName); |
| 986 | } catch (RemoteException e) { |
| 987 | /* ignore */ |
| 988 | } finally { |
| 989 | Binder.restoreCallingIdentity(identity); |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | private static HashMap<ModeCallback, ArrayList<ChangeRec>> addCallbacks( |
| 994 | HashMap<ModeCallback, ArrayList<ChangeRec>> callbacks, |
| 995 | int op, int uid, String packageName, ArraySet<ModeCallback> cbs) { |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 996 | if (cbs == null) { |
| 997 | return callbacks; |
| 998 | } |
| 999 | if (callbacks == null) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1000 | callbacks = new HashMap<>(); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1001 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1002 | boolean duplicate = false; |
Dianne Hackborn | 68d7655 | 2017-02-27 15:32:03 -0800 | [diff] [blame] | 1003 | final int N = cbs.size(); |
| 1004 | for (int i=0; i<N; i++) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1005 | ModeCallback cb = cbs.valueAt(i); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1006 | ArrayList<ChangeRec> reports = callbacks.get(cb); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1007 | if (reports == null) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1008 | reports = new ArrayList<>(); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1009 | callbacks.put(cb, reports); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1010 | } else { |
| 1011 | final int reportCount = reports.size(); |
| 1012 | for (int j = 0; j < reportCount; j++) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1013 | ChangeRec report = reports.get(j); |
| 1014 | if (report.op == op && report.pkg.equals(packageName)) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1015 | duplicate = true; |
| 1016 | break; |
| 1017 | } |
| 1018 | } |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1019 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1020 | if (!duplicate) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1021 | reports.add(new ChangeRec(op, uid, packageName)); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1022 | } |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1023 | } |
| 1024 | return callbacks; |
| 1025 | } |
| 1026 | |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1027 | static final class ChangeRec { |
| 1028 | final int op; |
| 1029 | final int uid; |
| 1030 | final String pkg; |
| 1031 | |
| 1032 | ChangeRec(int _op, int _uid, String _pkg) { |
| 1033 | op = _op; |
| 1034 | uid = _uid; |
| 1035 | pkg = _pkg; |
| 1036 | } |
| 1037 | } |
| 1038 | |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1039 | @Override |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 1040 | public void resetAllModes(int reqUserId, String reqPackageName) { |
| 1041 | final int callingPid = Binder.getCallingPid(); |
| 1042 | final int callingUid = Binder.getCallingUid(); |
Dianne Hackborn | bf1b57d | 2018-03-07 12:42:47 -0800 | [diff] [blame] | 1043 | mContext.enforcePermission(android.Manifest.permission.MANAGE_APP_OPS_MODES, |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 1044 | callingPid, callingUid, null); |
| 1045 | reqUserId = ActivityManager.handleIncomingUser(callingPid, callingUid, reqUserId, |
| 1046 | true, true, "resetAllModes", null); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1047 | |
| 1048 | int reqUid = -1; |
| 1049 | if (reqPackageName != null) { |
| 1050 | try { |
| 1051 | reqUid = AppGlobals.getPackageManager().getPackageUid( |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 1052 | reqPackageName, PackageManager.MATCH_UNINSTALLED_PACKAGES, reqUserId); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1053 | } catch (RemoteException e) { |
| 1054 | /* ignore - local call */ |
| 1055 | } |
| 1056 | } |
| 1057 | |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1058 | HashMap<ModeCallback, ArrayList<ChangeRec>> callbacks = null; |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1059 | synchronized (this) { |
| 1060 | boolean changed = false; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1061 | for (int i = mUidStates.size() - 1; i >= 0; i--) { |
| 1062 | UidState uidState = mUidStates.valueAt(i); |
| 1063 | |
| 1064 | SparseIntArray opModes = uidState.opModes; |
| 1065 | if (opModes != null && (uidState.uid == reqUid || reqUid == -1)) { |
| 1066 | final int uidOpCount = opModes.size(); |
| 1067 | for (int j = uidOpCount - 1; j >= 0; j--) { |
| 1068 | final int code = opModes.keyAt(j); |
| 1069 | if (AppOpsManager.opAllowsReset(code)) { |
| 1070 | opModes.removeAt(j); |
| 1071 | if (opModes.size() <= 0) { |
| 1072 | uidState.opModes = null; |
| 1073 | } |
| 1074 | for (String packageName : getPackagesForUid(uidState.uid)) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1075 | callbacks = addCallbacks(callbacks, code, uidState.uid, packageName, |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1076 | mOpModeWatchers.get(code)); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1077 | callbacks = addCallbacks(callbacks, code, uidState.uid, packageName, |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1078 | mPackageModeWatchers.get(packageName)); |
| 1079 | } |
| 1080 | } |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | if (uidState.pkgOps == null) { |
| 1085 | continue; |
| 1086 | } |
| 1087 | |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 1088 | if (reqUserId != UserHandle.USER_ALL |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1089 | && reqUserId != UserHandle.getUserId(uidState.uid)) { |
Alexandra Gherghina | d6a9897 | 2014-08-04 17:05:34 +0100 | [diff] [blame] | 1090 | // Skip any ops for a different user |
| 1091 | continue; |
| 1092 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1093 | |
| 1094 | Map<String, Ops> packages = uidState.pkgOps; |
Dianne Hackborn | 7f09ec3 | 2013-08-07 15:36:08 -0700 | [diff] [blame] | 1095 | Iterator<Map.Entry<String, Ops>> it = packages.entrySet().iterator(); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 1096 | boolean uidChanged = false; |
Dianne Hackborn | 7f09ec3 | 2013-08-07 15:36:08 -0700 | [diff] [blame] | 1097 | while (it.hasNext()) { |
| 1098 | Map.Entry<String, Ops> ent = it.next(); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1099 | String packageName = ent.getKey(); |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 1100 | if (reqPackageName != null && !reqPackageName.equals(packageName)) { |
| 1101 | // Skip any ops for a different package |
| 1102 | continue; |
| 1103 | } |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1104 | Ops pkgOps = ent.getValue(); |
Dianne Hackborn | 7f09ec3 | 2013-08-07 15:36:08 -0700 | [diff] [blame] | 1105 | for (int j=pkgOps.size()-1; j>=0; j--) { |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1106 | Op curOp = pkgOps.valueAt(j); |
Dianne Hackborn | 8828d3a | 2013-09-25 16:47:10 -0700 | [diff] [blame] | 1107 | if (AppOpsManager.opAllowsReset(curOp.op) |
| 1108 | && curOp.mode != AppOpsManager.opToDefaultMode(curOp.op)) { |
David Braun | f5d8319 | 2013-09-16 13:43:51 -0700 | [diff] [blame] | 1109 | curOp.mode = AppOpsManager.opToDefaultMode(curOp.op); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1110 | changed = true; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 1111 | uidChanged = true; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1112 | callbacks = addCallbacks(callbacks, curOp.op, curOp.uid, packageName, |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1113 | mOpModeWatchers.get(curOp.op)); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1114 | callbacks = addCallbacks(callbacks, curOp.op, curOp.uid, packageName, |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1115 | mPackageModeWatchers.get(packageName)); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1116 | if (!curOp.hasAnyTime()) { |
Dianne Hackborn | 7f09ec3 | 2013-08-07 15:36:08 -0700 | [diff] [blame] | 1117 | pkgOps.removeAt(j); |
| 1118 | } |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1119 | } |
| 1120 | } |
Dianne Hackborn | 7f09ec3 | 2013-08-07 15:36:08 -0700 | [diff] [blame] | 1121 | if (pkgOps.size() == 0) { |
| 1122 | it.remove(); |
| 1123 | } |
| 1124 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1125 | if (uidState.isDefault()) { |
| 1126 | mUidStates.remove(uidState.uid); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1127 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 1128 | if (uidChanged) { |
| 1129 | uidState.evalForegroundOps(); |
| 1130 | } |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1131 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1132 | |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1133 | if (changed) { |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 1134 | scheduleFastWriteLocked(); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1135 | } |
| 1136 | } |
| 1137 | if (callbacks != null) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1138 | for (Map.Entry<ModeCallback, ArrayList<ChangeRec>> ent : callbacks.entrySet()) { |
| 1139 | ModeCallback cb = ent.getKey(); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1140 | ArrayList<ChangeRec> reports = ent.getValue(); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1141 | for (int i=0; i<reports.size(); i++) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1142 | ChangeRec rep = reports.get(i); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1143 | mHandler.sendMessage(PooledLambda.obtainMessage( |
| 1144 | AppOpsService::notifyOpChanged, |
| 1145 | this, cb, rep.op, rep.uid, rep.pkg)); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1146 | } |
| 1147 | } |
| 1148 | } |
| 1149 | } |
| 1150 | |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1151 | @Override |
| 1152 | public void startWatchingMode(int op, String packageName, IAppOpsCallback callback) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1153 | int watchedUid = -1; |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 1154 | final int callingUid = Binder.getCallingUid(); |
| 1155 | final int callingPid = Binder.getCallingPid(); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1156 | if (mContext.checkCallingOrSelfPermission(Manifest.permission.WATCH_APPOPS) |
| 1157 | != PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 1158 | watchedUid = callingUid; |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1159 | } |
| 1160 | Preconditions.checkArgumentInRange(op, AppOpsManager.OP_NONE, |
| 1161 | AppOpsManager._NUM_OP - 1, "Invalid op code: " + op); |
Svetoslav Ganov | 8de5971 | 2015-12-09 18:25:13 -0800 | [diff] [blame] | 1162 | if (callback == null) { |
| 1163 | return; |
| 1164 | } |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1165 | synchronized (this) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1166 | op = (op != AppOpsManager.OP_NONE) ? AppOpsManager.opToSwitch(op) : op; |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1167 | ModeCallback cb = mModeWatchers.get(callback.asBinder()); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1168 | if (cb == null) { |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 1169 | cb = new ModeCallback(callback, watchedUid, callingUid, callingPid); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1170 | mModeWatchers.put(callback.asBinder(), cb); |
| 1171 | } |
| 1172 | if (op != AppOpsManager.OP_NONE) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1173 | ArraySet<ModeCallback> cbs = mOpModeWatchers.get(op); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1174 | if (cbs == null) { |
Dianne Hackborn | 68d7655 | 2017-02-27 15:32:03 -0800 | [diff] [blame] | 1175 | cbs = new ArraySet<>(); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1176 | mOpModeWatchers.put(op, cbs); |
| 1177 | } |
| 1178 | cbs.add(cb); |
| 1179 | } |
| 1180 | if (packageName != null) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1181 | ArraySet<ModeCallback> cbs = mPackageModeWatchers.get(packageName); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1182 | if (cbs == null) { |
Dianne Hackborn | 68d7655 | 2017-02-27 15:32:03 -0800 | [diff] [blame] | 1183 | cbs = new ArraySet<>(); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1184 | mPackageModeWatchers.put(packageName, cbs); |
| 1185 | } |
| 1186 | cbs.add(cb); |
| 1187 | } |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | @Override |
| 1192 | public void stopWatchingMode(IAppOpsCallback callback) { |
Svetoslav Ganov | 8de5971 | 2015-12-09 18:25:13 -0800 | [diff] [blame] | 1193 | if (callback == null) { |
| 1194 | return; |
| 1195 | } |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1196 | synchronized (this) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1197 | ModeCallback cb = mModeWatchers.remove(callback.asBinder()); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1198 | if (cb != null) { |
| 1199 | cb.unlinkToDeath(); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1200 | for (int i=mOpModeWatchers.size()-1; i>=0; i--) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1201 | ArraySet<ModeCallback> cbs = mOpModeWatchers.valueAt(i); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1202 | cbs.remove(cb); |
| 1203 | if (cbs.size() <= 0) { |
| 1204 | mOpModeWatchers.removeAt(i); |
| 1205 | } |
| 1206 | } |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1207 | for (int i=mPackageModeWatchers.size()-1; i>=0; i--) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1208 | ArraySet<ModeCallback> cbs = mPackageModeWatchers.valueAt(i); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1209 | cbs.remove(cb); |
| 1210 | if (cbs.size() <= 0) { |
| 1211 | mPackageModeWatchers.removeAt(i); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1212 | } |
| 1213 | } |
| 1214 | } |
| 1215 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | @Override |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1219 | public IBinder getToken(IBinder clientToken) { |
| 1220 | synchronized (this) { |
| 1221 | ClientState cs = mClients.get(clientToken); |
| 1222 | if (cs == null) { |
| 1223 | cs = new ClientState(clientToken); |
| 1224 | mClients.put(clientToken, cs); |
| 1225 | } |
| 1226 | return cs; |
| 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | @Override |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1231 | public int checkOperation(int code, int uid, String packageName) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1232 | verifyIncomingUid(uid); |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 1233 | verifyIncomingOp(code); |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 1234 | String resolvedPackageName = resolvePackageName(uid, packageName); |
| 1235 | if (resolvedPackageName == null) { |
| 1236 | return AppOpsManager.MODE_IGNORED; |
| 1237 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1238 | synchronized (this) { |
Svet Ganov | 442ed57 | 2016-08-17 17:29:43 -0700 | [diff] [blame] | 1239 | if (isOpRestrictedLocked(uid, code, resolvedPackageName)) { |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 1240 | return AppOpsManager.MODE_IGNORED; |
| 1241 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1242 | code = AppOpsManager.opToSwitch(code); |
| 1243 | UidState uidState = getUidStateLocked(uid, false); |
Svet Ganov | ee438d4 | 2017-01-19 18:04:38 -0800 | [diff] [blame] | 1244 | if (uidState != null && uidState.opModes != null |
| 1245 | && uidState.opModes.indexOfKey(code) >= 0) { |
| 1246 | return uidState.opModes.get(code); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1247 | } |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 1248 | Op op = getOpLocked(code, uid, resolvedPackageName, false); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1249 | if (op == null) { |
David Braun | f5d8319 | 2013-09-16 13:43:51 -0700 | [diff] [blame] | 1250 | return AppOpsManager.opToDefaultMode(code); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1251 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 1252 | return op.mode == AppOpsManager.MODE_FOREGROUND ? AppOpsManager.MODE_ALLOWED : op.mode; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1253 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | @Override |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 1257 | public int checkAudioOperation(int code, int usage, int uid, String packageName) { |
Andrei Stingaceanu | efc4a34 | 2016-03-22 14:43:01 +0000 | [diff] [blame] | 1258 | boolean suspended; |
| 1259 | try { |
| 1260 | suspended = isPackageSuspendedForUser(packageName, uid); |
| 1261 | } catch (IllegalArgumentException ex) { |
| 1262 | // Package not found. |
| 1263 | suspended = false; |
| 1264 | } |
| 1265 | |
| 1266 | if (suspended) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1267 | Slog.i(TAG, "Audio disabled for suspended package=" + packageName + " for uid=" + uid); |
Andrei Stingaceanu | 2bc2feb | 2016-02-11 16:23:49 +0000 | [diff] [blame] | 1268 | return AppOpsManager.MODE_IGNORED; |
| 1269 | } |
| 1270 | |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 1271 | synchronized (this) { |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 1272 | final int mode = checkRestrictionLocked(code, usage, uid, packageName); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 1273 | if (mode != AppOpsManager.MODE_ALLOWED) { |
| 1274 | return mode; |
| 1275 | } |
| 1276 | } |
| 1277 | return checkOperation(code, uid, packageName); |
| 1278 | } |
| 1279 | |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 1280 | private boolean isPackageSuspendedForUser(String pkg, int uid) { |
Andrei Stingaceanu | 2bc2feb | 2016-02-11 16:23:49 +0000 | [diff] [blame] | 1281 | try { |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 1282 | return AppGlobals.getPackageManager().isPackageSuspendedForUser( |
| 1283 | pkg, UserHandle.getUserId(uid)); |
Andrei Stingaceanu | 2bc2feb | 2016-02-11 16:23:49 +0000 | [diff] [blame] | 1284 | } catch (RemoteException re) { |
| 1285 | throw new SecurityException("Could not talk to package manager service"); |
| 1286 | } |
Andrei Stingaceanu | 2bc2feb | 2016-02-11 16:23:49 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 1289 | private int checkRestrictionLocked(int code, int usage, int uid, String packageName) { |
| 1290 | final SparseArray<Restriction> usageRestrictions = mAudioRestrictions.get(code); |
| 1291 | if (usageRestrictions != null) { |
| 1292 | final Restriction r = usageRestrictions.get(usage); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 1293 | if (r != null && !r.exceptionPackages.contains(packageName)) { |
| 1294 | return r.mode; |
| 1295 | } |
| 1296 | } |
| 1297 | return AppOpsManager.MODE_ALLOWED; |
| 1298 | } |
| 1299 | |
| 1300 | @Override |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 1301 | public void setAudioRestriction(int code, int usage, int uid, int mode, |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 1302 | String[] exceptionPackages) { |
| 1303 | verifyIncomingUid(uid); |
| 1304 | verifyIncomingOp(code); |
Dianne Hackborn | bf1b57d | 2018-03-07 12:42:47 -0800 | [diff] [blame] | 1305 | mContext.enforcePermission(android.Manifest.permission.MANAGE_APP_OPS_MODES, |
| 1306 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 1307 | synchronized (this) { |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 1308 | SparseArray<Restriction> usageRestrictions = mAudioRestrictions.get(code); |
| 1309 | if (usageRestrictions == null) { |
| 1310 | usageRestrictions = new SparseArray<Restriction>(); |
| 1311 | mAudioRestrictions.put(code, usageRestrictions); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 1312 | } |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 1313 | usageRestrictions.remove(usage); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 1314 | if (mode != AppOpsManager.MODE_ALLOWED) { |
| 1315 | final Restriction r = new Restriction(); |
| 1316 | r.mode = mode; |
| 1317 | if (exceptionPackages != null) { |
| 1318 | final int N = exceptionPackages.length; |
| 1319 | r.exceptionPackages = new ArraySet<String>(N); |
| 1320 | for (int i = 0; i < N; i++) { |
| 1321 | final String pkg = exceptionPackages[i]; |
| 1322 | if (pkg != null) { |
| 1323 | r.exceptionPackages.add(pkg.trim()); |
| 1324 | } |
| 1325 | } |
| 1326 | } |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 1327 | usageRestrictions.put(usage, r); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 1328 | } |
| 1329 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1330 | |
| 1331 | mHandler.sendMessage(PooledLambda.obtainMessage( |
Svet Ganov | 3a95f83 | 2018-03-23 17:44:30 -0700 | [diff] [blame] | 1332 | AppOpsService::notifyWatchersOfChange, this, code, UID_ANY)); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | @Override |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 1336 | public int checkPackage(int uid, String packageName) { |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 1337 | Preconditions.checkNotNull(packageName); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 1338 | synchronized (this) { |
Yohei Yukawa | a965d65 | 2017-10-12 15:02:26 -0700 | [diff] [blame] | 1339 | Ops ops = getOpsRawLocked(uid, packageName, true /* edit */, |
| 1340 | true /* uidMismatchExpected */); |
| 1341 | if (ops != null) { |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 1342 | return AppOpsManager.MODE_ALLOWED; |
| 1343 | } else { |
| 1344 | return AppOpsManager.MODE_ERRORED; |
| 1345 | } |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | @Override |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 1350 | public int noteProxyOperation(int code, String proxyPackageName, |
| 1351 | int proxiedUid, String proxiedPackageName) { |
| 1352 | verifyIncomingOp(code); |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 1353 | final int proxyUid = Binder.getCallingUid(); |
| 1354 | String resolveProxyPackageName = resolvePackageName(proxyUid, proxyPackageName); |
| 1355 | if (resolveProxyPackageName == null) { |
| 1356 | return AppOpsManager.MODE_IGNORED; |
| 1357 | } |
| 1358 | final int proxyMode = noteOperationUnchecked(code, proxyUid, |
| 1359 | resolveProxyPackageName, -1, null); |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 1360 | if (proxyMode != AppOpsManager.MODE_ALLOWED || Binder.getCallingUid() == proxiedUid) { |
| 1361 | return proxyMode; |
| 1362 | } |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 1363 | String resolveProxiedPackageName = resolvePackageName(proxiedUid, proxiedPackageName); |
| 1364 | if (resolveProxiedPackageName == null) { |
| 1365 | return AppOpsManager.MODE_IGNORED; |
| 1366 | } |
| 1367 | return noteOperationUnchecked(code, proxiedUid, resolveProxiedPackageName, |
| 1368 | proxyMode, resolveProxyPackageName); |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | @Override |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1372 | public int noteOperation(int code, int uid, String packageName) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1373 | verifyIncomingUid(uid); |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 1374 | verifyIncomingOp(code); |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 1375 | String resolvedPackageName = resolvePackageName(uid, packageName); |
| 1376 | if (resolvedPackageName == null) { |
| 1377 | return AppOpsManager.MODE_IGNORED; |
| 1378 | } |
| 1379 | return noteOperationUnchecked(code, uid, resolvedPackageName, 0, null); |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | private int noteOperationUnchecked(int code, int uid, String packageName, |
| 1383 | int proxyUid, String proxyPackageName) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1384 | synchronized (this) { |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1385 | final Ops ops = getOpsRawLocked(uid, packageName, true /* edit */, |
Yohei Yukawa | a965d65 | 2017-10-12 15:02:26 -0700 | [diff] [blame] | 1386 | false /* uidMismatchExpected */); |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1387 | if (ops == null) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1388 | if (DEBUG) Slog.d(TAG, "noteOperation: no op for code " + code + " uid " + uid |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1389 | + " package " + packageName); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 1390 | return AppOpsManager.MODE_ERRORED; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1391 | } |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1392 | final Op op = getOpLocked(ops, code, true); |
Svet Ganov | 442ed57 | 2016-08-17 17:29:43 -0700 | [diff] [blame] | 1393 | if (isOpRestrictedLocked(uid, code, packageName)) { |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 1394 | return AppOpsManager.MODE_IGNORED; |
| 1395 | } |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1396 | final UidState uidState = ops.uidState; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1397 | if (op.duration == -1) { |
| 1398 | Slog.w(TAG, "Noting op not finished: uid " + uid + " pkg " + packageName |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1399 | + " code " + code + " time=" + op.time[uidState.state] |
| 1400 | + " duration=" + op.duration); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1401 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1402 | op.duration = 0; |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1403 | final int switchCode = AppOpsManager.opToSwitch(code); |
Svetoslav Ganov | 1984bba | 2016-04-05 13:39:25 -0700 | [diff] [blame] | 1404 | // If there is a non-default per UID policy (we set UID op mode only if |
| 1405 | // non-default) it takes over, otherwise use the per package policy. |
| 1406 | if (uidState.opModes != null && uidState.opModes.indexOfKey(switchCode) >= 0) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 1407 | final int uidMode = uidState.evalMode(uidState.opModes.get(switchCode)); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1408 | if (uidMode != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 1409 | if (DEBUG) Slog.d(TAG, "noteOperation: uid reject #" + uidMode + " for code " |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1410 | + switchCode + " (" + code + ") uid " + uid + " package " |
| 1411 | + packageName); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1412 | op.rejectTime[uidState.state] = System.currentTimeMillis(); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1413 | return uidMode; |
| 1414 | } |
Svetoslav Ganov | 1984bba | 2016-04-05 13:39:25 -0700 | [diff] [blame] | 1415 | } else { |
| 1416 | final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, true) : op; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 1417 | final int mode = switchOp.getMode(); |
| 1418 | if (mode != AppOpsManager.MODE_ALLOWED) { |
| 1419 | if (DEBUG) Slog.d(TAG, "noteOperation: reject #" + mode + " for code " |
Svetoslav Ganov | 1984bba | 2016-04-05 13:39:25 -0700 | [diff] [blame] | 1420 | + switchCode + " (" + code + ") uid " + uid + " package " |
| 1421 | + packageName); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1422 | op.rejectTime[uidState.state] = System.currentTimeMillis(); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 1423 | return mode; |
Svetoslav Ganov | 1984bba | 2016-04-05 13:39:25 -0700 | [diff] [blame] | 1424 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1425 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1426 | if (DEBUG) Slog.d(TAG, "noteOperation: allowing code " + code + " uid " + uid |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1427 | + " package " + packageName); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1428 | op.time[uidState.state] = System.currentTimeMillis(); |
| 1429 | op.rejectTime[uidState.state] = 0; |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 1430 | op.proxyUid = proxyUid; |
| 1431 | op.proxyPackageName = proxyPackageName; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1432 | return AppOpsManager.MODE_ALLOWED; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1433 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | @Override |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1437 | public void startWatchingActive(int[] ops, IAppOpsActiveCallback callback) { |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 1438 | int watchedUid = -1; |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 1439 | final int callingUid = Binder.getCallingUid(); |
| 1440 | final int callingPid = Binder.getCallingPid(); |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 1441 | if (mContext.checkCallingOrSelfPermission(Manifest.permission.WATCH_APPOPS) |
| 1442 | != PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 1443 | watchedUid = callingUid; |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 1444 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1445 | if (ops != null) { |
| 1446 | Preconditions.checkArrayElementsInRange(ops, 0, |
| 1447 | AppOpsManager._NUM_OP - 1, "Invalid op code in: " + Arrays.toString(ops)); |
| 1448 | } |
| 1449 | if (callback == null) { |
| 1450 | return; |
| 1451 | } |
| 1452 | synchronized (this) { |
| 1453 | SparseArray<ActiveCallback> callbacks = mActiveWatchers.get(callback.asBinder()); |
| 1454 | if (callbacks == null) { |
| 1455 | callbacks = new SparseArray<>(); |
| 1456 | mActiveWatchers.put(callback.asBinder(), callbacks); |
| 1457 | } |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 1458 | final ActiveCallback activeCallback = new ActiveCallback(callback, watchedUid, |
| 1459 | callingUid, callingPid); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1460 | for (int op : ops) { |
| 1461 | callbacks.put(op, activeCallback); |
| 1462 | } |
| 1463 | } |
| 1464 | } |
| 1465 | |
| 1466 | @Override |
| 1467 | public void stopWatchingActive(IAppOpsActiveCallback callback) { |
| 1468 | if (callback == null) { |
| 1469 | return; |
| 1470 | } |
| 1471 | synchronized (this) { |
| 1472 | final SparseArray<ActiveCallback> activeCallbacks = |
| 1473 | mActiveWatchers.remove(callback.asBinder()); |
| 1474 | if (activeCallbacks == null) { |
| 1475 | return; |
| 1476 | } |
| 1477 | final int callbackCount = activeCallbacks.size(); |
| 1478 | for (int i = 0; i < callbackCount; i++) { |
| 1479 | // Apps ops are mapped to a singleton |
| 1480 | if (i == 0) { |
| 1481 | activeCallbacks.valueAt(i).destroy(); |
| 1482 | } |
| 1483 | } |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | @Override |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 1488 | public int startOperation(IBinder token, int code, int uid, String packageName, |
| 1489 | boolean startIfModeDefault) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1490 | verifyIncomingUid(uid); |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 1491 | verifyIncomingOp(code); |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 1492 | String resolvedPackageName = resolvePackageName(uid, packageName); |
| 1493 | if (resolvedPackageName == null) { |
| 1494 | return AppOpsManager.MODE_IGNORED; |
| 1495 | } |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1496 | ClientState client = (ClientState)token; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1497 | synchronized (this) { |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1498 | final Ops ops = getOpsRawLocked(uid, resolvedPackageName, true /* edit */, |
Yohei Yukawa | a965d65 | 2017-10-12 15:02:26 -0700 | [diff] [blame] | 1499 | false /* uidMismatchExpected */); |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1500 | if (ops == null) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1501 | if (DEBUG) Slog.d(TAG, "startOperation: no op for code " + code + " uid " + uid |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 1502 | + " package " + resolvedPackageName); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 1503 | return AppOpsManager.MODE_ERRORED; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1504 | } |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1505 | final Op op = getOpLocked(ops, code, true); |
Svet Ganov | 442ed57 | 2016-08-17 17:29:43 -0700 | [diff] [blame] | 1506 | if (isOpRestrictedLocked(uid, code, resolvedPackageName)) { |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 1507 | return AppOpsManager.MODE_IGNORED; |
| 1508 | } |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1509 | final int switchCode = AppOpsManager.opToSwitch(code); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1510 | final UidState uidState = ops.uidState; |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1511 | // If there is a non-default per UID policy (we set UID op mode only if |
| 1512 | // non-default) it takes over, otherwise use the per package policy. |
| 1513 | if (uidState.opModes != null && uidState.opModes.indexOfKey(switchCode) >= 0) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 1514 | final int uidMode = uidState.evalMode(uidState.opModes.get(switchCode)); |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 1515 | if (uidMode != AppOpsManager.MODE_ALLOWED |
| 1516 | && (!startIfModeDefault || uidMode != AppOpsManager.MODE_DEFAULT)) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 1517 | if (DEBUG) Slog.d(TAG, "noteOperation: uid reject #" + uidMode + " for code " |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1518 | + switchCode + " (" + code + ") uid " + uid + " package " |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 1519 | + resolvedPackageName); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1520 | op.rejectTime[uidState.state] = System.currentTimeMillis(); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1521 | return uidMode; |
| 1522 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1523 | } else { |
| 1524 | final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, true) : op; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 1525 | final int mode = switchOp.getMode(); |
| 1526 | if (mode != AppOpsManager.MODE_ALLOWED |
| 1527 | && (!startIfModeDefault || mode != AppOpsManager.MODE_DEFAULT)) { |
| 1528 | if (DEBUG) Slog.d(TAG, "startOperation: reject #" + mode + " for code " |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1529 | + switchCode + " (" + code + ") uid " + uid + " package " |
| 1530 | + resolvedPackageName); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1531 | op.rejectTime[uidState.state] = System.currentTimeMillis(); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 1532 | return mode; |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1533 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1534 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1535 | if (DEBUG) Slog.d(TAG, "startOperation: allowing code " + code + " uid " + uid |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 1536 | + " package " + resolvedPackageName); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1537 | if (op.startNesting == 0) { |
| 1538 | op.startRealtime = SystemClock.elapsedRealtime(); |
| 1539 | op.time[uidState.state] = System.currentTimeMillis(); |
| 1540 | op.rejectTime[uidState.state] = 0; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1541 | op.duration = -1; |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1542 | scheduleOpActiveChangedIfNeededLocked(code, uid, packageName, true); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1543 | } |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1544 | op.startNesting++; |
| 1545 | uidState.startNesting++; |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1546 | if (client.mStartedOps != null) { |
| 1547 | client.mStartedOps.add(op); |
| 1548 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1549 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1550 | |
| 1551 | return AppOpsManager.MODE_ALLOWED; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | @Override |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1555 | public void finishOperation(IBinder token, int code, int uid, String packageName) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1556 | verifyIncomingUid(uid); |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 1557 | verifyIncomingOp(code); |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 1558 | String resolvedPackageName = resolvePackageName(uid, packageName); |
| 1559 | if (resolvedPackageName == null) { |
| 1560 | return; |
| 1561 | } |
| 1562 | if (!(token instanceof ClientState)) { |
| 1563 | return; |
| 1564 | } |
| 1565 | ClientState client = (ClientState) token; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1566 | synchronized (this) { |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 1567 | Op op = getOpLocked(code, uid, resolvedPackageName, true); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1568 | if (op == null) { |
| 1569 | return; |
| 1570 | } |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 1571 | if (!client.mStartedOps.remove(op)) { |
Svet Ganov | f5d5af1 | 2018-03-18 11:51:17 -0700 | [diff] [blame] | 1572 | // We finish ops when packages get removed to guarantee no dangling |
| 1573 | // started ops. However, some part of the system may asynchronously |
| 1574 | // finish ops for an already gone package. Hence, finishing an op |
| 1575 | // for a non existing package is fine and we don't log as a wtf. |
| 1576 | final long identity = Binder.clearCallingIdentity(); |
| 1577 | try { |
| 1578 | if (LocalServices.getService(PackageManagerInternal.class).getPackageUid( |
| 1579 | resolvedPackageName, 0, UserHandle.getUserId(uid)) < 0) { |
| 1580 | Slog.i(TAG, "Finishing op=" + AppOpsManager.opToName(code) |
| 1581 | + " for non-existing package=" + resolvedPackageName |
| 1582 | + " in uid=" + uid); |
| 1583 | return; |
| 1584 | } |
| 1585 | } finally { |
| 1586 | Binder.restoreCallingIdentity(identity); |
| 1587 | } |
| 1588 | Slog.wtf(TAG, "Operation not started: uid=" + op.uid + " pkg=" |
| 1589 | + op.packageName + " op=" + AppOpsManager.opToName(op.op)); |
Svet Ganov | 31d83ae | 2018-03-15 10:45:56 -0700 | [diff] [blame] | 1590 | return; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1591 | } |
Svet Ganov | a7a0db6 | 2018-02-27 20:08:01 -0800 | [diff] [blame] | 1592 | finishOperationLocked(op, /*finishNested*/ false); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1593 | if (op.startNesting <= 0) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1594 | scheduleOpActiveChangedIfNeededLocked(code, uid, packageName, false); |
| 1595 | } |
| 1596 | } |
| 1597 | } |
| 1598 | |
| 1599 | private void scheduleOpActiveChangedIfNeededLocked(int code, int uid, String packageName, |
| 1600 | boolean active) { |
| 1601 | ArraySet<ActiveCallback> dispatchedCallbacks = null; |
| 1602 | final int callbackListCount = mActiveWatchers.size(); |
| 1603 | for (int i = 0; i < callbackListCount; i++) { |
| 1604 | final SparseArray<ActiveCallback> callbacks = mActiveWatchers.valueAt(i); |
| 1605 | ActiveCallback callback = callbacks.get(code); |
| 1606 | if (callback != null) { |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 1607 | if (callback.mWatchingUid >= 0 && callback.mWatchingUid != uid) { |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 1608 | continue; |
| 1609 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1610 | if (dispatchedCallbacks == null) { |
| 1611 | dispatchedCallbacks = new ArraySet<>(); |
| 1612 | } |
| 1613 | dispatchedCallbacks.add(callback); |
| 1614 | } |
| 1615 | } |
| 1616 | if (dispatchedCallbacks == null) { |
| 1617 | return; |
| 1618 | } |
| 1619 | mHandler.sendMessage(PooledLambda.obtainMessage( |
| 1620 | AppOpsService::notifyOpActiveChanged, |
| 1621 | this, dispatchedCallbacks, code, uid, packageName, active)); |
| 1622 | } |
| 1623 | |
| 1624 | private void notifyOpActiveChanged(ArraySet<ActiveCallback> callbacks, |
| 1625 | int code, int uid, String packageName, boolean active) { |
| 1626 | // There are components watching for mode changes such as window manager |
| 1627 | // and location manager which are in our process. The callbacks in these |
| 1628 | // components may require permissions our remote caller does not have. |
| 1629 | final long identity = Binder.clearCallingIdentity(); |
| 1630 | try { |
| 1631 | final int callbackCount = callbacks.size(); |
| 1632 | for (int i = 0; i < callbackCount; i++) { |
| 1633 | final ActiveCallback callback = callbacks.valueAt(i); |
| 1634 | try { |
| 1635 | callback.mCallback.opActiveChanged(code, uid, packageName, active); |
| 1636 | } catch (RemoteException e) { |
| 1637 | /* do nothing */ |
| 1638 | } |
| 1639 | } |
| 1640 | } finally { |
| 1641 | Binder.restoreCallingIdentity(identity); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1642 | } |
| 1643 | } |
| 1644 | |
Svet Ganov | b9d71a6 | 2015-04-30 10:38:13 -0700 | [diff] [blame] | 1645 | @Override |
| 1646 | public int permissionToOpCode(String permission) { |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 1647 | if (permission == null) { |
| 1648 | return AppOpsManager.OP_NONE; |
| 1649 | } |
Svet Ganov | b9d71a6 | 2015-04-30 10:38:13 -0700 | [diff] [blame] | 1650 | return AppOpsManager.permissionToOpCode(permission); |
| 1651 | } |
| 1652 | |
Svet Ganov | a7a0db6 | 2018-02-27 20:08:01 -0800 | [diff] [blame] | 1653 | void finishOperationLocked(Op op, boolean finishNested) { |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1654 | if (op.startNesting <= 1 || finishNested) { |
| 1655 | if (op.startNesting == 1 || finishNested) { |
| 1656 | op.duration = (int)(SystemClock.elapsedRealtime() - op.startRealtime); |
| 1657 | op.time[op.uidState.state] = System.currentTimeMillis(); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1658 | } else { |
| 1659 | Slog.w(TAG, "Finishing op nesting under-run: uid " + op.uid + " pkg " |
| 1660 | + op.packageName + " code " + op.op + " time=" + op.time |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1661 | + " duration=" + op.duration + " nesting=" + op.startNesting); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1662 | } |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1663 | if (op.startNesting >= 1) { |
| 1664 | op.uidState.startNesting -= op.startNesting; |
| 1665 | } |
| 1666 | op.startNesting = 0; |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1667 | } else { |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1668 | op.startNesting--; |
| 1669 | op.uidState.startNesting--; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1670 | } |
| 1671 | } |
| 1672 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1673 | private void verifyIncomingUid(int uid) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1674 | if (uid == Binder.getCallingUid()) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1675 | return; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1676 | } |
| 1677 | if (Binder.getCallingPid() == Process.myPid()) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1678 | return; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1679 | } |
| 1680 | mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS, |
| 1681 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1682 | } |
| 1683 | |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 1684 | private void verifyIncomingOp(int op) { |
| 1685 | if (op >= 0 && op < AppOpsManager._NUM_OP) { |
| 1686 | return; |
| 1687 | } |
| 1688 | throw new IllegalArgumentException("Bad operation #" + op); |
| 1689 | } |
| 1690 | |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1691 | private UidState getUidStateLocked(int uid, boolean edit) { |
| 1692 | UidState uidState = mUidStates.get(uid); |
| 1693 | if (uidState == null) { |
| 1694 | if (!edit) { |
| 1695 | return null; |
| 1696 | } |
| 1697 | uidState = new UidState(uid); |
| 1698 | mUidStates.put(uid, uidState); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 1699 | } else { |
| 1700 | if (uidState.pendingStateCommitTime != 0) { |
| 1701 | if (uidState.pendingStateCommitTime < mLastUptime) { |
| 1702 | uidState.state = uidState.pendingState; |
| 1703 | uidState.pendingStateCommitTime = 0; |
| 1704 | } else { |
| 1705 | mLastUptime = SystemClock.uptimeMillis(); |
| 1706 | if (uidState.pendingStateCommitTime < mLastUptime) { |
| 1707 | uidState.state = uidState.pendingState; |
| 1708 | uidState.pendingStateCommitTime = 0; |
| 1709 | } |
| 1710 | } |
| 1711 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1712 | } |
| 1713 | return uidState; |
| 1714 | } |
| 1715 | |
Yohei Yukawa | a965d65 | 2017-10-12 15:02:26 -0700 | [diff] [blame] | 1716 | private Ops getOpsRawLocked(int uid, String packageName, boolean edit, |
| 1717 | boolean uidMismatchExpected) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1718 | UidState uidState = getUidStateLocked(uid, edit); |
| 1719 | if (uidState == null) { |
| 1720 | return null; |
| 1721 | } |
| 1722 | |
| 1723 | if (uidState.pkgOps == null) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1724 | if (!edit) { |
| 1725 | return null; |
| 1726 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1727 | uidState.pkgOps = new ArrayMap<>(); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1728 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1729 | |
| 1730 | Ops ops = uidState.pkgOps.get(packageName); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1731 | if (ops == null) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1732 | if (!edit) { |
| 1733 | return null; |
| 1734 | } |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 1735 | boolean isPrivileged = false; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1736 | // This is the first time we have seen this package name under this uid, |
| 1737 | // so let's make sure it is valid. |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 1738 | if (uid != 0) { |
| 1739 | final long ident = Binder.clearCallingIdentity(); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1740 | try { |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 1741 | int pkgUid = -1; |
| 1742 | try { |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 1743 | ApplicationInfo appInfo = ActivityThread.getPackageManager() |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 1744 | .getApplicationInfo(packageName, |
| 1745 | PackageManager.MATCH_DEBUG_TRIAGED_MISSING, |
| 1746 | UserHandle.getUserId(uid)); |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 1747 | if (appInfo != null) { |
| 1748 | pkgUid = appInfo.uid; |
Alex Klyubin | b9f8a52 | 2015-02-03 11:12:59 -0800 | [diff] [blame] | 1749 | isPrivileged = (appInfo.privateFlags |
| 1750 | & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0; |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 1751 | } else { |
Svet Ganov | 82f09bc | 2018-01-12 22:08:40 -0800 | [diff] [blame] | 1752 | pkgUid = resolveUid(packageName); |
| 1753 | if (pkgUid >= 0) { |
Chien-Yu Chen | 75cade0 | 2016-01-11 10:56:21 -0800 | [diff] [blame] | 1754 | isPrivileged = false; |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 1755 | } |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 1756 | } |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 1757 | } catch (RemoteException e) { |
| 1758 | Slog.w(TAG, "Could not contact PackageManager", e); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 1759 | } |
| 1760 | if (pkgUid != uid) { |
| 1761 | // Oops! The package name is not valid for the uid they are calling |
| 1762 | // under. Abort. |
Yohei Yukawa | a965d65 | 2017-10-12 15:02:26 -0700 | [diff] [blame] | 1763 | if (!uidMismatchExpected) { |
| 1764 | RuntimeException ex = new RuntimeException("here"); |
| 1765 | ex.fillInStackTrace(); |
| 1766 | Slog.w(TAG, "Bad call: specified package " + packageName |
| 1767 | + " under uid " + uid + " but it is really " + pkgUid, ex); |
| 1768 | } |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 1769 | return null; |
| 1770 | } |
| 1771 | } finally { |
| 1772 | Binder.restoreCallingIdentity(ident); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1773 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1774 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1775 | ops = new Ops(packageName, uidState, isPrivileged); |
| 1776 | uidState.pkgOps.put(packageName, ops); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1777 | } |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 1778 | return ops; |
| 1779 | } |
| 1780 | |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1781 | private void scheduleWriteLocked() { |
| 1782 | if (!mWriteScheduled) { |
| 1783 | mWriteScheduled = true; |
| 1784 | mHandler.postDelayed(mWriteRunner, WRITE_DELAY); |
| 1785 | } |
| 1786 | } |
| 1787 | |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 1788 | private void scheduleFastWriteLocked() { |
| 1789 | if (!mFastWriteScheduled) { |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1790 | mWriteScheduled = true; |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 1791 | mFastWriteScheduled = true; |
| 1792 | mHandler.removeCallbacks(mWriteRunner); |
| 1793 | mHandler.postDelayed(mWriteRunner, 10*1000); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1794 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1795 | } |
| 1796 | |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 1797 | private Op getOpLocked(int code, int uid, String packageName, boolean edit) { |
Yohei Yukawa | a965d65 | 2017-10-12 15:02:26 -0700 | [diff] [blame] | 1798 | Ops ops = getOpsRawLocked(uid, packageName, edit, |
| 1799 | false /* uidMismatchExpected */); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 1800 | if (ops == null) { |
| 1801 | return null; |
| 1802 | } |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1803 | return getOpLocked(ops, code, edit); |
| 1804 | } |
| 1805 | |
| 1806 | private Op getOpLocked(Ops ops, int code, boolean edit) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1807 | Op op = ops.get(code); |
| 1808 | if (op == null) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1809 | if (!edit) { |
| 1810 | return null; |
| 1811 | } |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1812 | op = new Op(ops.uidState, ops.packageName, code); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1813 | ops.put(code, op); |
| 1814 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1815 | if (edit) { |
| 1816 | scheduleWriteLocked(); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1817 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1818 | return op; |
| 1819 | } |
| 1820 | |
Svet Ganov | 442ed57 | 2016-08-17 17:29:43 -0700 | [diff] [blame] | 1821 | private boolean isOpRestrictedLocked(int uid, int code, String packageName) { |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 1822 | int userHandle = UserHandle.getUserId(uid); |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 1823 | final int restrictionSetCount = mOpUserRestrictions.size(); |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 1824 | |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 1825 | for (int i = 0; i < restrictionSetCount; i++) { |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 1826 | // For each client, check that the given op is not restricted, or that the given |
| 1827 | // package is exempt from the restriction. |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 1828 | ClientRestrictionState restrictionState = mOpUserRestrictions.valueAt(i); |
Suprabh Shukla | ffddadb | 2016-05-20 16:37:26 -0700 | [diff] [blame] | 1829 | if (restrictionState.hasRestriction(code, packageName, userHandle)) { |
| 1830 | if (AppOpsManager.opAllowSystemBypassRestriction(code)) { |
| 1831 | // If we are the system, bypass user restrictions for certain codes |
| 1832 | synchronized (this) { |
Yohei Yukawa | a965d65 | 2017-10-12 15:02:26 -0700 | [diff] [blame] | 1833 | Ops ops = getOpsRawLocked(uid, packageName, true /* edit */, |
| 1834 | false /* uidMismatchExpected */); |
Suprabh Shukla | ffddadb | 2016-05-20 16:37:26 -0700 | [diff] [blame] | 1835 | if ((ops != null) && ops.isPrivileged) { |
| 1836 | return false; |
| 1837 | } |
Ruben Brunk | 32f0fa4 | 2016-03-11 19:07:07 -0800 | [diff] [blame] | 1838 | } |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 1839 | } |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 1840 | return true; |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 1841 | } |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 1842 | } |
| 1843 | return false; |
| 1844 | } |
| 1845 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1846 | void readState() { |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 1847 | int oldVersion = NO_VERSION; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1848 | synchronized (mFile) { |
| 1849 | synchronized (this) { |
| 1850 | FileInputStream stream; |
| 1851 | try { |
| 1852 | stream = mFile.openRead(); |
| 1853 | } catch (FileNotFoundException e) { |
| 1854 | Slog.i(TAG, "No existing app ops " + mFile.getBaseFile() + "; starting empty"); |
| 1855 | return; |
| 1856 | } |
| 1857 | boolean success = false; |
Dianne Hackborn | 4d34bb8 | 2015-08-07 18:26:38 -0700 | [diff] [blame] | 1858 | mUidStates.clear(); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1859 | try { |
| 1860 | XmlPullParser parser = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1861 | parser.setInput(stream, StandardCharsets.UTF_8.name()); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1862 | int type; |
| 1863 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 1864 | && type != XmlPullParser.END_DOCUMENT) { |
| 1865 | ; |
| 1866 | } |
| 1867 | |
| 1868 | if (type != XmlPullParser.START_TAG) { |
| 1869 | throw new IllegalStateException("no start tag found"); |
| 1870 | } |
| 1871 | |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 1872 | final String versionString = parser.getAttributeValue(null, "v"); |
| 1873 | if (versionString != null) { |
| 1874 | oldVersion = Integer.parseInt(versionString); |
| 1875 | } |
| 1876 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1877 | int outerDepth = parser.getDepth(); |
| 1878 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 1879 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 1880 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 1881 | continue; |
| 1882 | } |
| 1883 | |
| 1884 | String tagName = parser.getName(); |
| 1885 | if (tagName.equals("pkg")) { |
Dave Burke | 0997c5bd | 2013-08-02 20:25:02 +0000 | [diff] [blame] | 1886 | readPackage(parser); |
Svetoslav | 215b44a | 2015-08-04 19:03:40 -0700 | [diff] [blame] | 1887 | } else if (tagName.equals("uid")) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1888 | readUidOps(parser); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1889 | } else { |
| 1890 | Slog.w(TAG, "Unknown element under <app-ops>: " |
| 1891 | + parser.getName()); |
| 1892 | XmlUtils.skipCurrentTag(parser); |
| 1893 | } |
| 1894 | } |
| 1895 | success = true; |
| 1896 | } catch (IllegalStateException e) { |
| 1897 | Slog.w(TAG, "Failed parsing " + e); |
| 1898 | } catch (NullPointerException e) { |
| 1899 | Slog.w(TAG, "Failed parsing " + e); |
| 1900 | } catch (NumberFormatException e) { |
| 1901 | Slog.w(TAG, "Failed parsing " + e); |
| 1902 | } catch (XmlPullParserException e) { |
| 1903 | Slog.w(TAG, "Failed parsing " + e); |
| 1904 | } catch (IOException e) { |
| 1905 | Slog.w(TAG, "Failed parsing " + e); |
| 1906 | } catch (IndexOutOfBoundsException e) { |
| 1907 | Slog.w(TAG, "Failed parsing " + e); |
| 1908 | } finally { |
| 1909 | if (!success) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1910 | mUidStates.clear(); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1911 | } |
| 1912 | try { |
| 1913 | stream.close(); |
| 1914 | } catch (IOException e) { |
| 1915 | } |
| 1916 | } |
| 1917 | } |
| 1918 | } |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 1919 | synchronized (this) { |
| 1920 | upgradeLocked(oldVersion); |
| 1921 | } |
| 1922 | } |
| 1923 | |
| 1924 | private void upgradeRunAnyInBackgroundLocked() { |
| 1925 | for (int i = 0; i < mUidStates.size(); i++) { |
| 1926 | final UidState uidState = mUidStates.valueAt(i); |
| 1927 | if (uidState == null) { |
| 1928 | continue; |
| 1929 | } |
| 1930 | if (uidState.opModes != null) { |
| 1931 | final int idx = uidState.opModes.indexOfKey(AppOpsManager.OP_RUN_IN_BACKGROUND); |
| 1932 | if (idx >= 0) { |
| 1933 | uidState.opModes.put(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, |
| 1934 | uidState.opModes.valueAt(idx)); |
| 1935 | } |
| 1936 | } |
| 1937 | if (uidState.pkgOps == null) { |
| 1938 | continue; |
| 1939 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 1940 | boolean changed = false; |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 1941 | for (int j = 0; j < uidState.pkgOps.size(); j++) { |
| 1942 | Ops ops = uidState.pkgOps.valueAt(j); |
| 1943 | if (ops != null) { |
| 1944 | final Op op = ops.get(AppOpsManager.OP_RUN_IN_BACKGROUND); |
| 1945 | if (op != null && op.mode != AppOpsManager.opToDefaultMode(op.op)) { |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1946 | final Op copy = new Op(op.uidState, op.packageName, |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 1947 | AppOpsManager.OP_RUN_ANY_IN_BACKGROUND); |
| 1948 | copy.mode = op.mode; |
| 1949 | ops.put(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, copy); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 1950 | changed = true; |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 1951 | } |
| 1952 | } |
| 1953 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 1954 | if (changed) { |
| 1955 | uidState.evalForegroundOps(); |
| 1956 | } |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 1957 | } |
| 1958 | } |
| 1959 | |
| 1960 | private void upgradeLocked(int oldVersion) { |
| 1961 | if (oldVersion >= CURRENT_VERSION) { |
| 1962 | return; |
| 1963 | } |
| 1964 | Slog.d(TAG, "Upgrading app-ops xml from version " + oldVersion + " to " + CURRENT_VERSION); |
| 1965 | switch (oldVersion) { |
| 1966 | case NO_VERSION: |
| 1967 | upgradeRunAnyInBackgroundLocked(); |
| 1968 | // fall through |
| 1969 | case 1: |
| 1970 | // for future upgrades |
| 1971 | } |
| 1972 | scheduleFastWriteLocked(); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1973 | } |
| 1974 | |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1975 | void readUidOps(XmlPullParser parser) throws NumberFormatException, |
| 1976 | XmlPullParserException, IOException { |
| 1977 | final int uid = Integer.parseInt(parser.getAttributeValue(null, "n")); |
| 1978 | int outerDepth = parser.getDepth(); |
| 1979 | int type; |
| 1980 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 1981 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 1982 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 1983 | continue; |
| 1984 | } |
| 1985 | |
| 1986 | String tagName = parser.getName(); |
| 1987 | if (tagName.equals("op")) { |
| 1988 | final int code = Integer.parseInt(parser.getAttributeValue(null, "n")); |
| 1989 | final int mode = Integer.parseInt(parser.getAttributeValue(null, "m")); |
| 1990 | UidState uidState = getUidStateLocked(uid, true); |
| 1991 | if (uidState.opModes == null) { |
| 1992 | uidState.opModes = new SparseIntArray(); |
| 1993 | } |
| 1994 | uidState.opModes.put(code, mode); |
| 1995 | } else { |
| 1996 | Slog.w(TAG, "Unknown element under <uid-ops>: " |
| 1997 | + parser.getName()); |
| 1998 | XmlUtils.skipCurrentTag(parser); |
| 1999 | } |
| 2000 | } |
| 2001 | } |
| 2002 | |
Dave Burke | 0997c5bd | 2013-08-02 20:25:02 +0000 | [diff] [blame] | 2003 | void readPackage(XmlPullParser parser) throws NumberFormatException, |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2004 | XmlPullParserException, IOException { |
| 2005 | String pkgName = parser.getAttributeValue(null, "n"); |
| 2006 | int outerDepth = parser.getDepth(); |
| 2007 | int type; |
| 2008 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 2009 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 2010 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 2011 | continue; |
| 2012 | } |
| 2013 | |
| 2014 | String tagName = parser.getName(); |
| 2015 | if (tagName.equals("uid")) { |
Dave Burke | 0997c5bd | 2013-08-02 20:25:02 +0000 | [diff] [blame] | 2016 | readUid(parser, pkgName); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2017 | } else { |
| 2018 | Slog.w(TAG, "Unknown element under <pkg>: " |
| 2019 | + parser.getName()); |
| 2020 | XmlUtils.skipCurrentTag(parser); |
| 2021 | } |
| 2022 | } |
| 2023 | } |
| 2024 | |
Dave Burke | 0997c5bd | 2013-08-02 20:25:02 +0000 | [diff] [blame] | 2025 | void readUid(XmlPullParser parser, String pkgName) throws NumberFormatException, |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2026 | XmlPullParserException, IOException { |
| 2027 | int uid = Integer.parseInt(parser.getAttributeValue(null, "n")); |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 2028 | String isPrivilegedString = parser.getAttributeValue(null, "p"); |
| 2029 | boolean isPrivileged = false; |
| 2030 | if (isPrivilegedString == null) { |
| 2031 | try { |
| 2032 | IPackageManager packageManager = ActivityThread.getPackageManager(); |
| 2033 | if (packageManager != null) { |
| 2034 | ApplicationInfo appInfo = ActivityThread.getPackageManager() |
| 2035 | .getApplicationInfo(pkgName, 0, UserHandle.getUserId(uid)); |
| 2036 | if (appInfo != null) { |
Alex Klyubin | b9f8a52 | 2015-02-03 11:12:59 -0800 | [diff] [blame] | 2037 | isPrivileged = (appInfo.privateFlags |
| 2038 | & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0; |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 2039 | } |
| 2040 | } else { |
| 2041 | // Could not load data, don't add to cache so it will be loaded later. |
| 2042 | return; |
| 2043 | } |
| 2044 | } catch (RemoteException e) { |
| 2045 | Slog.w(TAG, "Could not contact PackageManager", e); |
| 2046 | } |
| 2047 | } else { |
| 2048 | isPrivileged = Boolean.parseBoolean(isPrivilegedString); |
| 2049 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2050 | int outerDepth = parser.getDepth(); |
| 2051 | int type; |
| 2052 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 2053 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 2054 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 2055 | continue; |
| 2056 | } |
| 2057 | |
| 2058 | String tagName = parser.getName(); |
| 2059 | if (tagName.equals("op")) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2060 | UidState uidState = getUidStateLocked(uid, true); |
| 2061 | if (uidState.pkgOps == null) { |
| 2062 | uidState.pkgOps = new ArrayMap<>(); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2063 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2064 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 2065 | Op op = new Op(uidState, pkgName, |
| 2066 | Integer.parseInt(parser.getAttributeValue(null, "n"))); |
| 2067 | |
| 2068 | for (int i = parser.getAttributeCount()-1; i >= 0; i--) { |
| 2069 | final String name = parser.getAttributeName(i); |
| 2070 | final String value = parser.getAttributeValue(i); |
| 2071 | switch (name) { |
| 2072 | case "m": |
| 2073 | op.mode = Integer.parseInt(value); |
| 2074 | break; |
| 2075 | case "d": |
| 2076 | op.duration = Integer.parseInt(value); |
| 2077 | break; |
| 2078 | case "pu": |
| 2079 | op.proxyUid = Integer.parseInt(value); |
| 2080 | break; |
| 2081 | case "pp": |
| 2082 | op.proxyPackageName = value; |
| 2083 | break; |
| 2084 | case "tp": |
| 2085 | op.time[AppOpsManager.UID_STATE_PERSISTENT] = Long.parseLong(value); |
| 2086 | break; |
| 2087 | case "tt": |
| 2088 | op.time[AppOpsManager.UID_STATE_TOP] = Long.parseLong(value); |
| 2089 | break; |
| 2090 | case "tfs": |
| 2091 | op.time[AppOpsManager.UID_STATE_FOREGROUND_SERVICE] |
| 2092 | = Long.parseLong(value); |
| 2093 | break; |
| 2094 | case "tf": |
| 2095 | op.time[AppOpsManager.UID_STATE_FOREGROUND] = Long.parseLong(value); |
| 2096 | break; |
| 2097 | case "tb": |
| 2098 | op.time[AppOpsManager.UID_STATE_BACKGROUND] = Long.parseLong(value); |
| 2099 | break; |
| 2100 | case "tc": |
| 2101 | op.time[AppOpsManager.UID_STATE_CACHED] = Long.parseLong(value); |
| 2102 | break; |
| 2103 | case "rp": |
| 2104 | op.rejectTime[AppOpsManager.UID_STATE_PERSISTENT] |
| 2105 | = Long.parseLong(value); |
| 2106 | break; |
| 2107 | case "rt": |
| 2108 | op.rejectTime[AppOpsManager.UID_STATE_TOP] = Long.parseLong(value); |
| 2109 | break; |
| 2110 | case "rfs": |
| 2111 | op.rejectTime[AppOpsManager.UID_STATE_FOREGROUND_SERVICE] |
| 2112 | = Long.parseLong(value); |
| 2113 | break; |
| 2114 | case "rf": |
| 2115 | op.rejectTime[AppOpsManager.UID_STATE_FOREGROUND] |
| 2116 | = Long.parseLong(value); |
| 2117 | break; |
| 2118 | case "rb": |
| 2119 | op.rejectTime[AppOpsManager.UID_STATE_BACKGROUND] |
| 2120 | = Long.parseLong(value); |
| 2121 | break; |
| 2122 | case "rc": |
| 2123 | op.rejectTime[AppOpsManager.UID_STATE_CACHED] |
| 2124 | = Long.parseLong(value); |
| 2125 | break; |
| 2126 | case "t": |
| 2127 | // Backwards compat. |
| 2128 | op.time[AppOpsManager.UID_STATE_TOP] = Long.parseLong(value); |
| 2129 | break; |
| 2130 | case "r": |
| 2131 | // Backwards compat. |
| 2132 | op.rejectTime[AppOpsManager.UID_STATE_TOP] = Long.parseLong(value); |
| 2133 | break; |
| 2134 | default: |
| 2135 | Slog.w(TAG, "Unknown attribute in 'op' tag: " + name); |
| 2136 | break; |
| 2137 | } |
| 2138 | } |
| 2139 | |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2140 | Ops ops = uidState.pkgOps.get(pkgName); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2141 | if (ops == null) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2142 | ops = new Ops(pkgName, uidState, isPrivileged); |
| 2143 | uidState.pkgOps.put(pkgName, ops); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2144 | } |
| 2145 | ops.put(op.op, op); |
| 2146 | } else { |
| 2147 | Slog.w(TAG, "Unknown element under <pkg>: " |
| 2148 | + parser.getName()); |
| 2149 | XmlUtils.skipCurrentTag(parser); |
| 2150 | } |
| 2151 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2152 | UidState uidState = getUidStateLocked(uid, false); |
| 2153 | if (uidState != null) { |
| 2154 | uidState.evalForegroundOps(); |
| 2155 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2156 | } |
| 2157 | |
| 2158 | void writeState() { |
| 2159 | synchronized (mFile) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2160 | FileOutputStream stream; |
| 2161 | try { |
| 2162 | stream = mFile.startWrite(); |
| 2163 | } catch (IOException e) { |
| 2164 | Slog.w(TAG, "Failed to write state: " + e); |
| 2165 | return; |
| 2166 | } |
| 2167 | |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 2168 | List<AppOpsManager.PackageOps> allOps = getPackagesForOps(null); |
| 2169 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2170 | try { |
| 2171 | XmlSerializer out = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 2172 | out.setOutput(stream, StandardCharsets.UTF_8.name()); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2173 | out.startDocument(null, true); |
Dianne Hackborn | 4d34bb8 | 2015-08-07 18:26:38 -0700 | [diff] [blame] | 2174 | out.startTag(null, "app-ops"); |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 2175 | out.attribute(null, "v", String.valueOf(CURRENT_VERSION)); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2176 | |
| 2177 | final int uidStateCount = mUidStates.size(); |
| 2178 | for (int i = 0; i < uidStateCount; i++) { |
| 2179 | UidState uidState = mUidStates.valueAt(i); |
| 2180 | if (uidState.opModes != null && uidState.opModes.size() > 0) { |
| 2181 | out.startTag(null, "uid"); |
| 2182 | out.attribute(null, "n", Integer.toString(uidState.uid)); |
| 2183 | SparseIntArray uidOpModes = uidState.opModes; |
| 2184 | final int opCount = uidOpModes.size(); |
| 2185 | for (int j = 0; j < opCount; j++) { |
| 2186 | final int op = uidOpModes.keyAt(j); |
| 2187 | final int mode = uidOpModes.valueAt(j); |
| 2188 | out.startTag(null, "op"); |
| 2189 | out.attribute(null, "n", Integer.toString(op)); |
| 2190 | out.attribute(null, "m", Integer.toString(mode)); |
| 2191 | out.endTag(null, "op"); |
| 2192 | } |
| 2193 | out.endTag(null, "uid"); |
| 2194 | } |
| 2195 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2196 | |
| 2197 | if (allOps != null) { |
| 2198 | String lastPkg = null; |
| 2199 | for (int i=0; i<allOps.size(); i++) { |
| 2200 | AppOpsManager.PackageOps pkg = allOps.get(i); |
| 2201 | if (!pkg.getPackageName().equals(lastPkg)) { |
| 2202 | if (lastPkg != null) { |
| 2203 | out.endTag(null, "pkg"); |
| 2204 | } |
| 2205 | lastPkg = pkg.getPackageName(); |
| 2206 | out.startTag(null, "pkg"); |
| 2207 | out.attribute(null, "n", lastPkg); |
| 2208 | } |
| 2209 | out.startTag(null, "uid"); |
| 2210 | out.attribute(null, "n", Integer.toString(pkg.getUid())); |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 2211 | synchronized (this) { |
Yohei Yukawa | a965d65 | 2017-10-12 15:02:26 -0700 | [diff] [blame] | 2212 | Ops ops = getOpsRawLocked(pkg.getUid(), pkg.getPackageName(), |
| 2213 | false /* edit */, false /* uidMismatchExpected */); |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 2214 | // Should always be present as the list of PackageOps is generated |
| 2215 | // from Ops. |
| 2216 | if (ops != null) { |
| 2217 | out.attribute(null, "p", Boolean.toString(ops.isPrivileged)); |
| 2218 | } else { |
| 2219 | out.attribute(null, "p", Boolean.toString(false)); |
| 2220 | } |
| 2221 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2222 | List<AppOpsManager.OpEntry> ops = pkg.getOps(); |
| 2223 | for (int j=0; j<ops.size(); j++) { |
| 2224 | AppOpsManager.OpEntry op = ops.get(j); |
| 2225 | out.startTag(null, "op"); |
| 2226 | out.attribute(null, "n", Integer.toString(op.getOp())); |
David Braun | f5d8319 | 2013-09-16 13:43:51 -0700 | [diff] [blame] | 2227 | if (op.getMode() != AppOpsManager.opToDefaultMode(op.getOp())) { |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 2228 | out.attribute(null, "m", Integer.toString(op.getMode())); |
| 2229 | } |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 2230 | for (int k = 0; k < _NUM_UID_STATE; k++) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2231 | final long time = op.getLastTimeFor(k); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 2232 | if (time != 0) { |
| 2233 | out.attribute(null, UID_STATE_TIME_ATTRS[k], |
| 2234 | Long.toString(time)); |
| 2235 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2236 | final long rejectTime = op.getLastRejectTimeFor(k); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 2237 | if (rejectTime != 0) { |
| 2238 | out.attribute(null, UID_STATE_REJECT_ATTRS[k], |
| 2239 | Long.toString(rejectTime)); |
| 2240 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 2241 | } |
| 2242 | int dur = op.getDuration(); |
| 2243 | if (dur != 0) { |
| 2244 | out.attribute(null, "d", Integer.toString(dur)); |
| 2245 | } |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 2246 | int proxyUid = op.getProxyUid(); |
| 2247 | if (proxyUid != -1) { |
| 2248 | out.attribute(null, "pu", Integer.toString(proxyUid)); |
| 2249 | } |
| 2250 | String proxyPackageName = op.getProxyPackageName(); |
| 2251 | if (proxyPackageName != null) { |
| 2252 | out.attribute(null, "pp", proxyPackageName); |
| 2253 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2254 | out.endTag(null, "op"); |
| 2255 | } |
| 2256 | out.endTag(null, "uid"); |
| 2257 | } |
| 2258 | if (lastPkg != null) { |
| 2259 | out.endTag(null, "pkg"); |
| 2260 | } |
| 2261 | } |
| 2262 | |
| 2263 | out.endTag(null, "app-ops"); |
| 2264 | out.endDocument(); |
| 2265 | mFile.finishWrite(stream); |
| 2266 | } catch (IOException e) { |
| 2267 | Slog.w(TAG, "Failed to write state, restoring backup.", e); |
| 2268 | mFile.failWrite(stream); |
| 2269 | } |
| 2270 | } |
| 2271 | } |
| 2272 | |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2273 | static class Shell extends ShellCommand { |
| 2274 | final IAppOpsService mInterface; |
| 2275 | final AppOpsService mInternal; |
| 2276 | |
| 2277 | int userId = UserHandle.USER_SYSTEM; |
| 2278 | String packageName; |
| 2279 | String opStr; |
Dianne Hackborn | e91f3e7 | 2016-03-25 18:48:15 -0700 | [diff] [blame] | 2280 | String modeStr; |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2281 | int op; |
Dianne Hackborn | e91f3e7 | 2016-03-25 18:48:15 -0700 | [diff] [blame] | 2282 | int mode; |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2283 | int packageUid; |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 2284 | int nonpackageUid; |
Julia Reynolds | 6cb5fcc | 2018-02-27 17:33:52 -0500 | [diff] [blame] | 2285 | final static Binder sBinder = new Binder(); |
| 2286 | IBinder mToken; |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2287 | |
| 2288 | Shell(IAppOpsService iface, AppOpsService internal) { |
| 2289 | mInterface = iface; |
| 2290 | mInternal = internal; |
Julia Reynolds | 6cb5fcc | 2018-02-27 17:33:52 -0500 | [diff] [blame] | 2291 | try { |
| 2292 | mToken = mInterface.getToken(sBinder); |
| 2293 | } catch (RemoteException e) { |
| 2294 | } |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2295 | } |
| 2296 | |
| 2297 | @Override |
| 2298 | public int onCommand(String cmd) { |
| 2299 | return onShellCommand(this, cmd); |
| 2300 | } |
| 2301 | |
| 2302 | @Override |
| 2303 | public void onHelp() { |
| 2304 | PrintWriter pw = getOutPrintWriter(); |
| 2305 | dumpCommandHelp(pw); |
| 2306 | } |
| 2307 | |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2308 | static private int strOpToOp(String op, PrintWriter err) { |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2309 | try { |
| 2310 | return AppOpsManager.strOpToOp(op); |
| 2311 | } catch (IllegalArgumentException e) { |
| 2312 | } |
| 2313 | try { |
| 2314 | return Integer.parseInt(op); |
| 2315 | } catch (NumberFormatException e) { |
| 2316 | } |
| 2317 | try { |
| 2318 | return AppOpsManager.strDebugOpToOp(op); |
| 2319 | } catch (IllegalArgumentException e) { |
| 2320 | err.println("Error: " + e.getMessage()); |
| 2321 | return -1; |
| 2322 | } |
| 2323 | } |
| 2324 | |
Dianne Hackborn | e91f3e7 | 2016-03-25 18:48:15 -0700 | [diff] [blame] | 2325 | int strModeToMode(String modeStr, PrintWriter err) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2326 | for (int i = AppOpsManager.MODE_NAMES.length - 1; i >= 0; i--) { |
| 2327 | if (AppOpsManager.MODE_NAMES[i].equals(modeStr)) { |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 2328 | return i; |
| 2329 | } |
Dianne Hackborn | e91f3e7 | 2016-03-25 18:48:15 -0700 | [diff] [blame] | 2330 | } |
| 2331 | try { |
| 2332 | return Integer.parseInt(modeStr); |
| 2333 | } catch (NumberFormatException e) { |
| 2334 | } |
| 2335 | err.println("Error: Mode " + modeStr + " is not valid"); |
| 2336 | return -1; |
| 2337 | } |
| 2338 | |
| 2339 | int parseUserOpMode(int defMode, PrintWriter err) throws RemoteException { |
| 2340 | userId = UserHandle.USER_CURRENT; |
| 2341 | opStr = null; |
| 2342 | modeStr = null; |
| 2343 | for (String argument; (argument = getNextArg()) != null;) { |
| 2344 | if ("--user".equals(argument)) { |
| 2345 | userId = UserHandle.parseUserArg(getNextArgRequired()); |
| 2346 | } else { |
| 2347 | if (opStr == null) { |
| 2348 | opStr = argument; |
| 2349 | } else if (modeStr == null) { |
| 2350 | modeStr = argument; |
| 2351 | break; |
| 2352 | } |
| 2353 | } |
| 2354 | } |
| 2355 | if (opStr == null) { |
| 2356 | err.println("Error: Operation not specified."); |
| 2357 | return -1; |
| 2358 | } |
| 2359 | op = strOpToOp(opStr, err); |
| 2360 | if (op < 0) { |
| 2361 | return -1; |
| 2362 | } |
| 2363 | if (modeStr != null) { |
| 2364 | if ((mode=strModeToMode(modeStr, err)) < 0) { |
| 2365 | return -1; |
| 2366 | } |
| 2367 | } else { |
| 2368 | mode = defMode; |
| 2369 | } |
| 2370 | return 0; |
| 2371 | } |
| 2372 | |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2373 | int parseUserPackageOp(boolean reqOp, PrintWriter err) throws RemoteException { |
| 2374 | userId = UserHandle.USER_CURRENT; |
| 2375 | packageName = null; |
| 2376 | opStr = null; |
| 2377 | for (String argument; (argument = getNextArg()) != null;) { |
| 2378 | if ("--user".equals(argument)) { |
| 2379 | userId = UserHandle.parseUserArg(getNextArgRequired()); |
| 2380 | } else { |
| 2381 | if (packageName == null) { |
| 2382 | packageName = argument; |
| 2383 | } else if (opStr == null) { |
| 2384 | opStr = argument; |
| 2385 | break; |
| 2386 | } |
| 2387 | } |
| 2388 | } |
| 2389 | if (packageName == null) { |
| 2390 | err.println("Error: Package name not specified."); |
| 2391 | return -1; |
| 2392 | } else if (opStr == null && reqOp) { |
| 2393 | err.println("Error: Operation not specified."); |
| 2394 | return -1; |
| 2395 | } |
| 2396 | if (opStr != null) { |
| 2397 | op = strOpToOp(opStr, err); |
| 2398 | if (op < 0) { |
| 2399 | return -1; |
| 2400 | } |
| 2401 | } else { |
| 2402 | op = AppOpsManager.OP_NONE; |
| 2403 | } |
| 2404 | if (userId == UserHandle.USER_CURRENT) { |
| 2405 | userId = ActivityManager.getCurrentUser(); |
| 2406 | } |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 2407 | nonpackageUid = -1; |
| 2408 | try { |
| 2409 | nonpackageUid = Integer.parseInt(packageName); |
| 2410 | } catch (NumberFormatException e) { |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2411 | } |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 2412 | if (nonpackageUid == -1 && packageName.length() > 1 && packageName.charAt(0) == 'u' |
| 2413 | && packageName.indexOf('.') < 0) { |
| 2414 | int i = 1; |
| 2415 | while (i < packageName.length() && packageName.charAt(i) >= '0' |
| 2416 | && packageName.charAt(i) <= '9') { |
| 2417 | i++; |
| 2418 | } |
| 2419 | if (i > 1 && i < packageName.length()) { |
| 2420 | String userStr = packageName.substring(1, i); |
| 2421 | try { |
| 2422 | int user = Integer.parseInt(userStr); |
| 2423 | char type = packageName.charAt(i); |
| 2424 | i++; |
| 2425 | int startTypeVal = i; |
| 2426 | while (i < packageName.length() && packageName.charAt(i) >= '0' |
| 2427 | && packageName.charAt(i) <= '9') { |
| 2428 | i++; |
| 2429 | } |
| 2430 | if (i > startTypeVal) { |
| 2431 | String typeValStr = packageName.substring(startTypeVal, i); |
| 2432 | try { |
| 2433 | int typeVal = Integer.parseInt(typeValStr); |
| 2434 | if (type == 'a') { |
| 2435 | nonpackageUid = UserHandle.getUid(user, |
| 2436 | typeVal + Process.FIRST_APPLICATION_UID); |
| 2437 | } else if (type == 's') { |
| 2438 | nonpackageUid = UserHandle.getUid(user, typeVal); |
| 2439 | } |
| 2440 | } catch (NumberFormatException e) { |
| 2441 | } |
| 2442 | } |
| 2443 | } catch (NumberFormatException e) { |
| 2444 | } |
| 2445 | } |
| 2446 | } |
| 2447 | if (nonpackageUid != -1) { |
| 2448 | packageName = null; |
| 2449 | } else { |
Svet Ganov | 82f09bc | 2018-01-12 22:08:40 -0800 | [diff] [blame] | 2450 | packageUid = resolveUid(packageName); |
| 2451 | if (packageUid < 0) { |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 2452 | packageUid = AppGlobals.getPackageManager().getPackageUid(packageName, |
| 2453 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
| 2454 | } |
| 2455 | if (packageUid < 0) { |
| 2456 | err.println("Error: No UID for " + packageName + " in user " + userId); |
| 2457 | return -1; |
| 2458 | } |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2459 | } |
| 2460 | return 0; |
| 2461 | } |
| 2462 | } |
| 2463 | |
| 2464 | @Override public void onShellCommand(FileDescriptor in, FileDescriptor out, |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 2465 | FileDescriptor err, String[] args, ShellCallback callback, |
| 2466 | ResultReceiver resultReceiver) { |
| 2467 | (new Shell(this, this)).exec(this, in, out, err, args, callback, resultReceiver); |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2468 | } |
| 2469 | |
| 2470 | static void dumpCommandHelp(PrintWriter pw) { |
| 2471 | pw.println("AppOps service (appops) commands:"); |
| 2472 | pw.println(" help"); |
| 2473 | pw.println(" Print this help text."); |
Julia Reynolds | 6cb5fcc | 2018-02-27 17:33:52 -0500 | [diff] [blame] | 2474 | pw.println(" start [--user <USER_ID>] <PACKAGE | UID> <OP> "); |
| 2475 | pw.println(" Starts a given operation for a particular application."); |
| 2476 | pw.println(" stop [--user <USER_ID>] <PACKAGE | UID> <OP> "); |
| 2477 | pw.println(" Stops a given operation for a particular application."); |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 2478 | pw.println(" set [--user <USER_ID>] <PACKAGE | UID> <OP> <MODE>"); |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2479 | pw.println(" Set the mode for a particular application and operation."); |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 2480 | pw.println(" get [--user <USER_ID>] <PACKAGE | UID> [<OP>]"); |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2481 | pw.println(" Return the mode for a particular application and optional operation."); |
Dianne Hackborn | e91f3e7 | 2016-03-25 18:48:15 -0700 | [diff] [blame] | 2482 | pw.println(" query-op [--user <USER_ID>] <OP> [<MODE>]"); |
| 2483 | pw.println(" Print all packages that currently have the given op in the given mode."); |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2484 | pw.println(" reset [--user <USER_ID>] [<PACKAGE>]"); |
| 2485 | pw.println(" Reset the given application or all applications to default modes."); |
Dianne Hackborn | 4d34bb8 | 2015-08-07 18:26:38 -0700 | [diff] [blame] | 2486 | pw.println(" write-settings"); |
| 2487 | pw.println(" Immediately write pending changes to storage."); |
| 2488 | pw.println(" read-settings"); |
| 2489 | pw.println(" Read the last written settings, replacing current state in RAM."); |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2490 | pw.println(" options:"); |
| 2491 | pw.println(" <PACKAGE> an Android package name."); |
| 2492 | pw.println(" <OP> an AppOps operation."); |
| 2493 | pw.println(" <MODE> one of allow, ignore, deny, or default"); |
| 2494 | pw.println(" <USER_ID> the user id under which the package is installed. If --user is not"); |
| 2495 | pw.println(" specified, the current user is assumed."); |
| 2496 | } |
| 2497 | |
| 2498 | static int onShellCommand(Shell shell, String cmd) { |
| 2499 | if (cmd == null) { |
| 2500 | return shell.handleDefaultCommands(cmd); |
| 2501 | } |
| 2502 | PrintWriter pw = shell.getOutPrintWriter(); |
| 2503 | PrintWriter err = shell.getErrPrintWriter(); |
| 2504 | try { |
| 2505 | switch (cmd) { |
| 2506 | case "set": { |
| 2507 | int res = shell.parseUserPackageOp(true, err); |
| 2508 | if (res < 0) { |
| 2509 | return res; |
| 2510 | } |
| 2511 | String modeStr = shell.getNextArg(); |
| 2512 | if (modeStr == null) { |
| 2513 | err.println("Error: Mode not specified."); |
| 2514 | return -1; |
| 2515 | } |
| 2516 | |
Dianne Hackborn | e91f3e7 | 2016-03-25 18:48:15 -0700 | [diff] [blame] | 2517 | final int mode = shell.strModeToMode(modeStr, err); |
| 2518 | if (mode < 0) { |
| 2519 | return -1; |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2520 | } |
| 2521 | |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 2522 | if (shell.packageName != null) { |
| 2523 | shell.mInterface.setMode(shell.op, shell.packageUid, shell.packageName, |
| 2524 | mode); |
| 2525 | } else { |
| 2526 | shell.mInterface.setUidMode(shell.op, shell.nonpackageUid, mode); |
| 2527 | } |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2528 | return 0; |
| 2529 | } |
| 2530 | case "get": { |
| 2531 | int res = shell.parseUserPackageOp(false, err); |
| 2532 | if (res < 0) { |
| 2533 | return res; |
| 2534 | } |
| 2535 | |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 2536 | List<AppOpsManager.PackageOps> ops; |
| 2537 | if (shell.packageName != null) { |
| 2538 | ops = shell.mInterface.getOpsForPackage( |
| 2539 | shell.packageUid, shell.packageName, |
| 2540 | shell.op != AppOpsManager.OP_NONE ? new int[]{shell.op} : null); |
| 2541 | } else { |
| 2542 | ops = shell.mInterface.getUidOps( |
| 2543 | shell.nonpackageUid, |
| 2544 | shell.op != AppOpsManager.OP_NONE ? new int[]{shell.op} : null); |
| 2545 | } |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2546 | if (ops == null || ops.size() <= 0) { |
| 2547 | pw.println("No operations."); |
Svet Ganov | 82f09bc | 2018-01-12 22:08:40 -0800 | [diff] [blame] | 2548 | if (shell.op > AppOpsManager.OP_NONE && shell.op < AppOpsManager._NUM_OP) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2549 | pw.println("Default mode: " + AppOpsManager.modeToName( |
Svet Ganov | 82f09bc | 2018-01-12 22:08:40 -0800 | [diff] [blame] | 2550 | AppOpsManager.opToDefaultMode(shell.op))); |
| 2551 | } |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2552 | return 0; |
| 2553 | } |
| 2554 | final long now = System.currentTimeMillis(); |
| 2555 | for (int i=0; i<ops.size(); i++) { |
| 2556 | List<AppOpsManager.OpEntry> entries = ops.get(i).getOps(); |
| 2557 | for (int j=0; j<entries.size(); j++) { |
| 2558 | AppOpsManager.OpEntry ent = entries.get(j); |
| 2559 | pw.print(AppOpsManager.opToName(ent.getOp())); |
| 2560 | pw.print(": "); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2561 | pw.print(AppOpsManager.modeToName(ent.getMode())); |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2562 | if (ent.getTime() != 0) { |
| 2563 | pw.print("; time="); |
| 2564 | TimeUtils.formatDuration(now - ent.getTime(), pw); |
| 2565 | pw.print(" ago"); |
| 2566 | } |
| 2567 | if (ent.getRejectTime() != 0) { |
| 2568 | pw.print("; rejectTime="); |
| 2569 | TimeUtils.formatDuration(now - ent.getRejectTime(), pw); |
| 2570 | pw.print(" ago"); |
| 2571 | } |
| 2572 | if (ent.getDuration() == -1) { |
| 2573 | pw.print(" (running)"); |
| 2574 | } else if (ent.getDuration() != 0) { |
| 2575 | pw.print("; duration="); |
| 2576 | TimeUtils.formatDuration(ent.getDuration(), pw); |
| 2577 | } |
| 2578 | pw.println(); |
| 2579 | } |
| 2580 | } |
| 2581 | return 0; |
| 2582 | } |
Dianne Hackborn | e91f3e7 | 2016-03-25 18:48:15 -0700 | [diff] [blame] | 2583 | case "query-op": { |
| 2584 | int res = shell.parseUserOpMode(AppOpsManager.MODE_IGNORED, err); |
| 2585 | if (res < 0) { |
| 2586 | return res; |
| 2587 | } |
| 2588 | List<AppOpsManager.PackageOps> ops = shell.mInterface.getPackagesForOps( |
| 2589 | new int[] {shell.op}); |
| 2590 | if (ops == null || ops.size() <= 0) { |
| 2591 | pw.println("No operations."); |
| 2592 | return 0; |
| 2593 | } |
| 2594 | for (int i=0; i<ops.size(); i++) { |
| 2595 | final AppOpsManager.PackageOps pkg = ops.get(i); |
| 2596 | boolean hasMatch = false; |
| 2597 | final List<AppOpsManager.OpEntry> entries = ops.get(i).getOps(); |
| 2598 | for (int j=0; j<entries.size(); j++) { |
| 2599 | AppOpsManager.OpEntry ent = entries.get(j); |
| 2600 | if (ent.getOp() == shell.op && ent.getMode() == shell.mode) { |
| 2601 | hasMatch = true; |
| 2602 | break; |
| 2603 | } |
| 2604 | } |
| 2605 | if (hasMatch) { |
| 2606 | pw.println(pkg.getPackageName()); |
| 2607 | } |
| 2608 | } |
| 2609 | return 0; |
| 2610 | } |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2611 | case "reset": { |
| 2612 | String packageName = null; |
| 2613 | int userId = UserHandle.USER_CURRENT; |
| 2614 | for (String argument; (argument = shell.getNextArg()) != null;) { |
| 2615 | if ("--user".equals(argument)) { |
| 2616 | String userStr = shell.getNextArgRequired(); |
| 2617 | userId = UserHandle.parseUserArg(userStr); |
| 2618 | } else { |
| 2619 | if (packageName == null) { |
| 2620 | packageName = argument; |
| 2621 | } else { |
| 2622 | err.println("Error: Unsupported argument: " + argument); |
| 2623 | return -1; |
| 2624 | } |
| 2625 | } |
| 2626 | } |
| 2627 | |
| 2628 | if (userId == UserHandle.USER_CURRENT) { |
| 2629 | userId = ActivityManager.getCurrentUser(); |
| 2630 | } |
| 2631 | |
| 2632 | shell.mInterface.resetAllModes(userId, packageName); |
| 2633 | pw.print("Reset all modes for: "); |
| 2634 | if (userId == UserHandle.USER_ALL) { |
| 2635 | pw.print("all users"); |
| 2636 | } else { |
| 2637 | pw.print("user "); pw.print(userId); |
| 2638 | } |
| 2639 | pw.print(", "); |
| 2640 | if (packageName == null) { |
| 2641 | pw.println("all packages"); |
| 2642 | } else { |
| 2643 | pw.print("package "); pw.println(packageName); |
| 2644 | } |
| 2645 | return 0; |
| 2646 | } |
| 2647 | case "write-settings": { |
| 2648 | shell.mInternal.mContext.enforcePermission( |
Dianne Hackborn | bf1b57d | 2018-03-07 12:42:47 -0800 | [diff] [blame] | 2649 | android.Manifest.permission.MANAGE_APP_OPS_MODES, |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2650 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 2651 | long token = Binder.clearCallingIdentity(); |
| 2652 | try { |
| 2653 | synchronized (shell.mInternal) { |
| 2654 | shell.mInternal.mHandler.removeCallbacks(shell.mInternal.mWriteRunner); |
| 2655 | } |
| 2656 | shell.mInternal.writeState(); |
| 2657 | pw.println("Current settings written."); |
| 2658 | } finally { |
| 2659 | Binder.restoreCallingIdentity(token); |
| 2660 | } |
| 2661 | return 0; |
| 2662 | } |
| 2663 | case "read-settings": { |
| 2664 | shell.mInternal.mContext.enforcePermission( |
Dianne Hackborn | bf1b57d | 2018-03-07 12:42:47 -0800 | [diff] [blame] | 2665 | android.Manifest.permission.MANAGE_APP_OPS_MODES, |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2666 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 2667 | long token = Binder.clearCallingIdentity(); |
| 2668 | try { |
| 2669 | shell.mInternal.readState(); |
| 2670 | pw.println("Last settings read."); |
| 2671 | } finally { |
| 2672 | Binder.restoreCallingIdentity(token); |
| 2673 | } |
| 2674 | return 0; |
| 2675 | } |
Julia Reynolds | 6cb5fcc | 2018-02-27 17:33:52 -0500 | [diff] [blame] | 2676 | case "start": { |
| 2677 | int res = shell.parseUserPackageOp(true, err); |
| 2678 | if (res < 0) { |
| 2679 | return res; |
| 2680 | } |
| 2681 | |
| 2682 | if (shell.packageName != null) { |
| 2683 | shell.mInterface.startOperation(shell.mToken, |
| 2684 | shell.op, shell.packageUid, shell.packageName, true); |
| 2685 | } else { |
| 2686 | return -1; |
| 2687 | } |
| 2688 | return 0; |
| 2689 | } |
| 2690 | case "stop": { |
| 2691 | int res = shell.parseUserPackageOp(true, err); |
| 2692 | if (res < 0) { |
| 2693 | return res; |
| 2694 | } |
| 2695 | |
| 2696 | if (shell.packageName != null) { |
| 2697 | shell.mInterface.finishOperation(shell.mToken, |
| 2698 | shell.op, shell.packageUid, shell.packageName); |
| 2699 | } else { |
| 2700 | return -1; |
| 2701 | } |
| 2702 | return 0; |
| 2703 | } |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2704 | default: |
| 2705 | return shell.handleDefaultCommands(cmd); |
| 2706 | } |
| 2707 | } catch (RemoteException e) { |
| 2708 | pw.println("Remote exception: " + e); |
| 2709 | } |
| 2710 | return -1; |
| 2711 | } |
| 2712 | |
| 2713 | private void dumpHelp(PrintWriter pw) { |
| 2714 | pw.println("AppOps service (appops) dump options:"); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2715 | pw.println(" -h"); |
| 2716 | pw.println(" Print this help text."); |
| 2717 | pw.println(" --op [OP]"); |
| 2718 | pw.println(" Limit output to data associated with the given app op code."); |
Dianne Hackborn | 4d34bb8 | 2015-08-07 18:26:38 -0700 | [diff] [blame] | 2719 | } |
| 2720 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 2721 | private void dumpTimesLocked(PrintWriter pw, String firstPrefix, String prefix, long[] times, |
| 2722 | long now, SimpleDateFormat sdf, Date date) { |
| 2723 | boolean hasTime = false; |
| 2724 | for (int i = 0; i < _NUM_UID_STATE; i++) { |
| 2725 | if (times[i] != 0) { |
| 2726 | hasTime = true; |
| 2727 | break; |
| 2728 | } |
| 2729 | } |
| 2730 | if (!hasTime) { |
| 2731 | return; |
| 2732 | } |
| 2733 | boolean first = true; |
| 2734 | for (int i = 0; i < _NUM_UID_STATE; i++) { |
| 2735 | if (times[i] != 0) { |
| 2736 | pw.print(first ? firstPrefix : prefix); |
| 2737 | first = false; |
| 2738 | pw.print(UID_STATE_NAMES[i]); |
| 2739 | pw.print(" = "); |
| 2740 | date.setTime(times[i]); |
| 2741 | pw.print(sdf.format(date)); |
| 2742 | pw.print(" ("); |
| 2743 | TimeUtils.formatDuration(times[i]-now, pw); |
| 2744 | pw.println(")"); |
| 2745 | } |
| 2746 | } |
| 2747 | } |
| 2748 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2749 | @Override |
| 2750 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | 6df866a | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 2751 | if (!DumpUtils.checkDumpAndUsageStatsPermission(mContext, TAG, pw)) return; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2752 | |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2753 | int dumpOp = -1; |
| 2754 | |
Dianne Hackborn | 4d34bb8 | 2015-08-07 18:26:38 -0700 | [diff] [blame] | 2755 | if (args != null) { |
| 2756 | for (int i=0; i<args.length; i++) { |
| 2757 | String arg = args[i]; |
| 2758 | if ("-h".equals(arg)) { |
| 2759 | dumpHelp(pw); |
| 2760 | return; |
Tim Kilbourn | 8f1ea83 | 2015-08-26 15:07:37 -0700 | [diff] [blame] | 2761 | } else if ("-a".equals(arg)) { |
| 2762 | // dump all data |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2763 | } else if ("--op".equals(arg)) { |
| 2764 | i++; |
| 2765 | if (i >= args.length) { |
| 2766 | pw.println("No argument for --op option"); |
| 2767 | return; |
| 2768 | } |
| 2769 | dumpOp = Shell.strOpToOp(args[i], pw); |
| 2770 | if (dumpOp < 0) { |
| 2771 | return; |
| 2772 | } |
Dianne Hackborn | 4d34bb8 | 2015-08-07 18:26:38 -0700 | [diff] [blame] | 2773 | } else if (arg.length() > 0 && arg.charAt(0) == '-'){ |
| 2774 | pw.println("Unknown option: " + arg); |
| 2775 | return; |
| 2776 | } else { |
| 2777 | pw.println("Unknown command: " + arg); |
| 2778 | return; |
| 2779 | } |
| 2780 | } |
| 2781 | } |
| 2782 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2783 | synchronized (this) { |
| 2784 | pw.println("Current AppOps Service state:"); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 2785 | final long now = System.currentTimeMillis(); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 2786 | final long nowElapsed = SystemClock.elapsedRealtime(); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2787 | final long nowUptime = SystemClock.uptimeMillis(); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 2788 | final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); |
| 2789 | final Date date = new Date(); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 2790 | boolean needSep = false; |
| 2791 | if (mOpModeWatchers.size() > 0) { |
| 2792 | needSep = true; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2793 | boolean printedHeader = false; |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 2794 | for (int i=0; i<mOpModeWatchers.size(); i++) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2795 | if (dumpOp >= 0 && dumpOp != mOpModeWatchers.keyAt(i)) { |
| 2796 | continue; |
| 2797 | } |
| 2798 | if (!printedHeader) { |
| 2799 | pw.println(" Op mode watchers:"); |
| 2800 | printedHeader = true; |
| 2801 | } |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 2802 | pw.print(" Op "); pw.print(AppOpsManager.opToName(mOpModeWatchers.keyAt(i))); |
| 2803 | pw.println(":"); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2804 | ArraySet<ModeCallback> callbacks = mOpModeWatchers.valueAt(i); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 2805 | for (int j=0; j<callbacks.size(); j++) { |
| 2806 | pw.print(" #"); pw.print(j); pw.print(": "); |
Dianne Hackborn | 68d7655 | 2017-02-27 15:32:03 -0800 | [diff] [blame] | 2807 | pw.println(callbacks.valueAt(j)); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 2808 | } |
| 2809 | } |
| 2810 | } |
| 2811 | if (mPackageModeWatchers.size() > 0) { |
| 2812 | needSep = true; |
| 2813 | pw.println(" Package mode watchers:"); |
| 2814 | for (int i=0; i<mPackageModeWatchers.size(); i++) { |
| 2815 | pw.print(" Pkg "); pw.print(mPackageModeWatchers.keyAt(i)); |
| 2816 | pw.println(":"); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2817 | ArraySet<ModeCallback> callbacks = mPackageModeWatchers.valueAt(i); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 2818 | for (int j=0; j<callbacks.size(); j++) { |
| 2819 | pw.print(" #"); pw.print(j); pw.print(": "); |
Dianne Hackborn | 68d7655 | 2017-02-27 15:32:03 -0800 | [diff] [blame] | 2820 | pw.println(callbacks.valueAt(j)); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 2821 | } |
| 2822 | } |
| 2823 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2824 | if (mModeWatchers.size() > 0 && dumpOp < 0) { |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 2825 | needSep = true; |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2826 | pw.println(" All op mode watchers:"); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 2827 | for (int i=0; i<mModeWatchers.size(); i++) { |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 2828 | pw.print(" "); |
| 2829 | pw.print(Integer.toHexString(System.identityHashCode(mModeWatchers.keyAt(i)))); |
| 2830 | pw.print(": "); pw.println(mModeWatchers.valueAt(i)); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 2831 | } |
| 2832 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2833 | if (mActiveWatchers.size() > 0) { |
| 2834 | needSep = true; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2835 | boolean printedHeader = false; |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2836 | for (int i = 0; i < mActiveWatchers.size(); i++) { |
| 2837 | final SparseArray<ActiveCallback> activeWatchers = mActiveWatchers.valueAt(i); |
| 2838 | if (activeWatchers.size() <= 0) { |
| 2839 | continue; |
| 2840 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2841 | if (dumpOp >= 0 && activeWatchers.indexOfKey(dumpOp) < 0) { |
| 2842 | continue; |
| 2843 | } |
| 2844 | if (!printedHeader) { |
| 2845 | pw.println(" All op active watchers:"); |
| 2846 | printedHeader = true; |
| 2847 | } |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 2848 | pw.print(" "); |
| 2849 | pw.print(Integer.toHexString(System.identityHashCode( |
| 2850 | mActiveWatchers.keyAt(i)))); |
| 2851 | pw.println(" ->"); |
| 2852 | pw.print(" ["); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2853 | final int opCount = activeWatchers.size(); |
| 2854 | for (i = 0; i < opCount; i++) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2855 | if (i > 0) { |
| 2856 | pw.print(' '); |
| 2857 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2858 | pw.print(AppOpsManager.opToName(activeWatchers.keyAt(i))); |
| 2859 | if (i < opCount - 1) { |
| 2860 | pw.print(','); |
| 2861 | } |
| 2862 | } |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 2863 | pw.println("]"); |
| 2864 | pw.print(" "); |
| 2865 | pw.println(activeWatchers.valueAt(0)); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2866 | } |
| 2867 | } |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 2868 | if (mClients.size() > 0) { |
| 2869 | needSep = true; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2870 | boolean printedHeader = false; |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 2871 | for (int i=0; i<mClients.size(); i++) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2872 | boolean printedClient = false; |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 2873 | ClientState cs = mClients.valueAt(i); |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 2874 | if (cs.mStartedOps.size() > 0) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2875 | boolean printedStarted = false; |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 2876 | for (int j=0; j<cs.mStartedOps.size(); j++) { |
| 2877 | Op op = cs.mStartedOps.get(j); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2878 | if (dumpOp >= 0 && op.op != dumpOp) { |
| 2879 | continue; |
| 2880 | } |
| 2881 | if (!printedHeader) { |
| 2882 | pw.println(" Clients:"); |
| 2883 | printedHeader = true; |
| 2884 | } |
| 2885 | if (!printedClient) { |
| 2886 | pw.print(" "); pw.print(mClients.keyAt(i)); pw.println(":"); |
| 2887 | pw.print(" "); pw.println(cs); |
| 2888 | printedClient = true; |
| 2889 | } |
| 2890 | if (!printedStarted) { |
| 2891 | pw.println(" Started ops:"); |
| 2892 | printedStarted = true; |
| 2893 | } |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 2894 | pw.print(" "); pw.print("uid="); pw.print(op.uid); |
| 2895 | pw.print(" pkg="); pw.print(op.packageName); |
| 2896 | pw.print(" op="); pw.println(AppOpsManager.opToName(op.op)); |
| 2897 | } |
| 2898 | } |
| 2899 | } |
| 2900 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2901 | if (mAudioRestrictions.size() > 0 && dumpOp < 0) { |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 2902 | boolean printedHeader = false; |
| 2903 | for (int o=0; o<mAudioRestrictions.size(); o++) { |
| 2904 | final String op = AppOpsManager.opToName(mAudioRestrictions.keyAt(o)); |
| 2905 | final SparseArray<Restriction> restrictions = mAudioRestrictions.valueAt(o); |
| 2906 | for (int i=0; i<restrictions.size(); i++) { |
| 2907 | if (!printedHeader){ |
| 2908 | pw.println(" Audio Restrictions:"); |
| 2909 | printedHeader = true; |
| 2910 | needSep = true; |
| 2911 | } |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 2912 | final int usage = restrictions.keyAt(i); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 2913 | pw.print(" "); pw.print(op); |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 2914 | pw.print(" usage="); pw.print(AudioAttributes.usageToString(usage)); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 2915 | Restriction r = restrictions.valueAt(i); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2916 | pw.print(": mode="); pw.println(AppOpsManager.modeToName(r.mode)); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 2917 | if (!r.exceptionPackages.isEmpty()) { |
| 2918 | pw.println(" Exceptions:"); |
| 2919 | for (int j=0; j<r.exceptionPackages.size(); j++) { |
| 2920 | pw.print(" "); pw.println(r.exceptionPackages.valueAt(j)); |
| 2921 | } |
| 2922 | } |
| 2923 | } |
| 2924 | } |
| 2925 | } |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 2926 | if (needSep) { |
| 2927 | pw.println(); |
| 2928 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2929 | for (int i=0; i<mUidStates.size(); i++) { |
| 2930 | UidState uidState = mUidStates.valueAt(i); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2931 | final SparseIntArray opModes = uidState.opModes; |
| 2932 | final ArrayMap<String, Ops> pkgOps = uidState.pkgOps; |
| 2933 | |
| 2934 | if (dumpOp >= 0) { |
| 2935 | boolean hasOp = uidState.opModes != null |
| 2936 | && uidState.opModes.indexOfKey(dumpOp) >= 0; |
| 2937 | if (pkgOps != null) { |
| 2938 | for (int pkgi = 0; !hasOp && pkgi < pkgOps.size(); pkgi++) { |
| 2939 | Ops ops = pkgOps.valueAt(pkgi); |
| 2940 | if (ops != null && ops.indexOfKey(dumpOp) >= 0) { |
| 2941 | hasOp = true; |
| 2942 | continue; |
| 2943 | } |
| 2944 | } |
| 2945 | } |
| 2946 | if (!hasOp) { |
| 2947 | continue; |
| 2948 | } |
| 2949 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2950 | |
| 2951 | pw.print(" Uid "); UserHandle.formatUid(pw, uidState.uid); pw.println(":"); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 2952 | pw.print(" state="); |
| 2953 | pw.println(UID_STATE_NAMES[uidState.state]); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2954 | if (uidState.state != uidState.pendingState) { |
| 2955 | pw.print(" pendingState="); |
| 2956 | pw.println(UID_STATE_NAMES[uidState.pendingState]); |
| 2957 | } |
| 2958 | if (uidState.pendingStateCommitTime != 0) { |
| 2959 | pw.print(" pendingStateCommitTime="); |
| 2960 | TimeUtils.formatDuration(uidState.pendingStateCommitTime, nowUptime, pw); |
| 2961 | pw.println(); |
| 2962 | } |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 2963 | if (uidState.startNesting != 0) { |
| 2964 | pw.print(" startNesting="); |
| 2965 | pw.println(uidState.startNesting); |
| 2966 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2967 | if (uidState.foregroundOps != null) { |
| 2968 | pw.println(" foregroundOps:"); |
| 2969 | for (int j = 0; j < uidState.foregroundOps.size(); j++) { |
| 2970 | pw.print(" "); |
| 2971 | pw.println(AppOpsManager.opToName(uidState.foregroundOps.keyAt(j))); |
| 2972 | } |
| 2973 | } |
Svet Ganov | ee438d4 | 2017-01-19 18:04:38 -0800 | [diff] [blame] | 2974 | needSep = true; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2975 | |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2976 | if (opModes != null) { |
| 2977 | final int opModeCount = opModes.size(); |
| 2978 | for (int j = 0; j < opModeCount; j++) { |
| 2979 | final int code = opModes.keyAt(j); |
| 2980 | final int mode = opModes.valueAt(j); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2981 | if (dumpOp >= 0 && dumpOp != code) { |
| 2982 | continue; |
| 2983 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2984 | pw.print(" "); pw.print(AppOpsManager.opToName(code)); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2985 | pw.print(": mode="); pw.println(AppOpsManager.modeToName(mode)); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2986 | } |
| 2987 | } |
| 2988 | |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2989 | if (pkgOps == null) { |
| 2990 | continue; |
| 2991 | } |
| 2992 | |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2993 | for (int pkgi = 0; pkgi < pkgOps.size(); pkgi++) { |
| 2994 | Ops ops = pkgOps.valueAt(pkgi); |
| 2995 | boolean printedPackage = false; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2996 | for (int j=0; j<ops.size(); j++) { |
| 2997 | Op op = ops.valueAt(j); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 2998 | if (dumpOp >= 0 && dumpOp != op.op) { |
| 2999 | continue; |
| 3000 | } |
| 3001 | if (!printedPackage) { |
| 3002 | pw.print(" Package "); pw.print(ops.packageName); pw.println(":"); |
| 3003 | printedPackage = true; |
| 3004 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 3005 | pw.print(" "); pw.print(AppOpsManager.opToName(op.op)); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame^] | 3006 | pw.print(" ("); pw.print(AppOpsManager.modeToName(op.mode)); |
| 3007 | pw.println("): "); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 3008 | dumpTimesLocked(pw, |
| 3009 | " Access: ", |
| 3010 | " ", op.time, now, sdf, date); |
| 3011 | dumpTimesLocked(pw, |
| 3012 | " Reject: ", |
| 3013 | " ", op.rejectTime, now, sdf, date); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3014 | if (op.duration == -1) { |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 3015 | pw.print(" Running start at: "); |
| 3016 | TimeUtils.formatDuration(nowElapsed-op.startRealtime, pw); |
| 3017 | pw.println(); |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 3018 | } else if (op.duration != 0) { |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 3019 | pw.print(" duration="); |
| 3020 | TimeUtils.formatDuration(op.duration, pw); |
| 3021 | pw.println(); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3022 | } |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 3023 | if (op.startNesting != 0) { |
| 3024 | pw.print(" startNesting="); |
| 3025 | pw.println(op.startNesting); |
| 3026 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3027 | } |
| 3028 | } |
| 3029 | } |
Svet Ganov | ee438d4 | 2017-01-19 18:04:38 -0800 | [diff] [blame] | 3030 | if (needSep) { |
| 3031 | pw.println(); |
| 3032 | } |
| 3033 | |
| 3034 | final int userRestrictionCount = mOpUserRestrictions.size(); |
| 3035 | for (int i = 0; i < userRestrictionCount; i++) { |
| 3036 | IBinder token = mOpUserRestrictions.keyAt(i); |
| 3037 | ClientRestrictionState restrictionState = mOpUserRestrictions.valueAt(i); |
| 3038 | pw.println(" User restrictions for token " + token + ":"); |
| 3039 | |
| 3040 | final int restrictionCount = restrictionState.perUserRestrictions != null |
| 3041 | ? restrictionState.perUserRestrictions.size() : 0; |
| 3042 | if (restrictionCount > 0) { |
| 3043 | pw.println(" Restricted ops:"); |
| 3044 | for (int j = 0; j < restrictionCount; j++) { |
| 3045 | int userId = restrictionState.perUserRestrictions.keyAt(j); |
| 3046 | boolean[] restrictedOps = restrictionState.perUserRestrictions.valueAt(j); |
| 3047 | if (restrictedOps == null) { |
| 3048 | continue; |
| 3049 | } |
| 3050 | StringBuilder restrictedOpsValue = new StringBuilder(); |
| 3051 | restrictedOpsValue.append("["); |
| 3052 | final int restrictedOpCount = restrictedOps.length; |
| 3053 | for (int k = 0; k < restrictedOpCount; k++) { |
| 3054 | if (restrictedOps[k]) { |
| 3055 | if (restrictedOpsValue.length() > 1) { |
| 3056 | restrictedOpsValue.append(", "); |
| 3057 | } |
| 3058 | restrictedOpsValue.append(AppOpsManager.opToName(k)); |
| 3059 | } |
| 3060 | } |
| 3061 | restrictedOpsValue.append("]"); |
| 3062 | pw.print(" "); pw.print("user: "); pw.print(userId); |
| 3063 | pw.print(" restricted ops: "); pw.println(restrictedOpsValue); |
| 3064 | } |
| 3065 | } |
| 3066 | |
| 3067 | final int excludedPackageCount = restrictionState.perUserExcludedPackages != null |
| 3068 | ? restrictionState.perUserExcludedPackages.size() : 0; |
| 3069 | if (excludedPackageCount > 0) { |
| 3070 | pw.println(" Excluded packages:"); |
| 3071 | for (int j = 0; j < excludedPackageCount; j++) { |
| 3072 | int userId = restrictionState.perUserExcludedPackages.keyAt(j); |
| 3073 | String[] packageNames = restrictionState.perUserExcludedPackages.valueAt(j); |
| 3074 | pw.print(" "); pw.print("user: "); pw.print(userId); |
| 3075 | pw.print(" packages: "); pw.println(Arrays.toString(packageNames)); |
| 3076 | } |
| 3077 | } |
| 3078 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3079 | } |
| 3080 | } |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 3081 | |
| 3082 | private static final class Restriction { |
| 3083 | private static final ArraySet<String> NO_EXCEPTIONS = new ArraySet<String>(); |
| 3084 | int mode; |
| 3085 | ArraySet<String> exceptionPackages = NO_EXCEPTIONS; |
| 3086 | } |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 3087 | |
| 3088 | @Override |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 3089 | public void setUserRestrictions(Bundle restrictions, IBinder token, int userHandle) { |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 3090 | checkSystemUid("setUserRestrictions"); |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 3091 | Preconditions.checkNotNull(restrictions); |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 3092 | Preconditions.checkNotNull(token); |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 3093 | for (int i = 0; i < AppOpsManager._NUM_OP; i++) { |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 3094 | String restriction = AppOpsManager.opToRestriction(i); |
Suprabh Shukla | 64e0dcb | 2016-05-24 16:23:11 -0700 | [diff] [blame] | 3095 | if (restriction != null) { |
| 3096 | setUserRestrictionNoCheck(i, restrictions.getBoolean(restriction, false), token, |
| 3097 | userHandle, null); |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 3098 | } |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 3099 | } |
| 3100 | } |
| 3101 | |
| 3102 | @Override |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 3103 | public void setUserRestriction(int code, boolean restricted, IBinder token, int userHandle, |
| 3104 | String[] exceptionPackages) { |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 3105 | if (Binder.getCallingPid() != Process.myPid()) { |
| 3106 | mContext.enforcePermission(Manifest.permission.MANAGE_APP_OPS_RESTRICTIONS, |
| 3107 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 3108 | } |
| 3109 | if (userHandle != UserHandle.getCallingUserId()) { |
| 3110 | if (mContext.checkCallingOrSelfPermission(Manifest.permission |
| 3111 | .INTERACT_ACROSS_USERS_FULL) != PackageManager.PERMISSION_GRANTED |
| 3112 | && mContext.checkCallingOrSelfPermission(Manifest.permission |
| 3113 | .INTERACT_ACROSS_USERS) != PackageManager.PERMISSION_GRANTED) { |
| 3114 | throw new SecurityException("Need INTERACT_ACROSS_USERS_FULL or" |
| 3115 | + " INTERACT_ACROSS_USERS to interact cross user "); |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 3116 | } |
| 3117 | } |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 3118 | verifyIncomingOp(code); |
| 3119 | Preconditions.checkNotNull(token); |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 3120 | setUserRestrictionNoCheck(code, restricted, token, userHandle, exceptionPackages); |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 3121 | } |
| 3122 | |
| 3123 | private void setUserRestrictionNoCheck(int code, boolean restricted, IBinder token, |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 3124 | int userHandle, String[] exceptionPackages) { |
Svet Ganov | 442ed57 | 2016-08-17 17:29:43 -0700 | [diff] [blame] | 3125 | synchronized (AppOpsService.this) { |
| 3126 | ClientRestrictionState restrictionState = mOpUserRestrictions.get(token); |
| 3127 | |
| 3128 | if (restrictionState == null) { |
| 3129 | try { |
| 3130 | restrictionState = new ClientRestrictionState(token); |
| 3131 | } catch (RemoteException e) { |
| 3132 | return; |
| 3133 | } |
| 3134 | mOpUserRestrictions.put(token, restrictionState); |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 3135 | } |
Svet Ganov | 442ed57 | 2016-08-17 17:29:43 -0700 | [diff] [blame] | 3136 | |
| 3137 | if (restrictionState.setRestriction(code, restricted, exceptionPackages, userHandle)) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3138 | mHandler.sendMessage(PooledLambda.obtainMessage( |
Svet Ganov | 3a95f83 | 2018-03-23 17:44:30 -0700 | [diff] [blame] | 3139 | AppOpsService::notifyWatchersOfChange, this, code, UID_ANY)); |
Svet Ganov | 442ed57 | 2016-08-17 17:29:43 -0700 | [diff] [blame] | 3140 | } |
| 3141 | |
| 3142 | if (restrictionState.isDefault()) { |
| 3143 | mOpUserRestrictions.remove(token); |
| 3144 | restrictionState.destroy(); |
| 3145 | } |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 3146 | } |
Julia Reynolds | bb21c25 | 2016-04-05 16:01:49 -0400 | [diff] [blame] | 3147 | } |
| 3148 | |
Svet Ganov | 3a95f83 | 2018-03-23 17:44:30 -0700 | [diff] [blame] | 3149 | private void notifyWatchersOfChange(int code, int uid) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3150 | final ArraySet<ModeCallback> clonedCallbacks; |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 3151 | synchronized (this) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3152 | ArraySet<ModeCallback> callbacks = mOpModeWatchers.get(code); |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 3153 | if (callbacks == null) { |
| 3154 | return; |
| 3155 | } |
Dianne Hackborn | 68d7655 | 2017-02-27 15:32:03 -0800 | [diff] [blame] | 3156 | clonedCallbacks = new ArraySet<>(callbacks); |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 3157 | } |
| 3158 | |
Svet Ganov | 3a95f83 | 2018-03-23 17:44:30 -0700 | [diff] [blame] | 3159 | notifyOpChanged(clonedCallbacks, code, uid, null); |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 3160 | } |
| 3161 | |
| 3162 | @Override |
| 3163 | public void removeUser(int userHandle) throws RemoteException { |
| 3164 | checkSystemUid("removeUser"); |
Svet Ganov | 442ed57 | 2016-08-17 17:29:43 -0700 | [diff] [blame] | 3165 | synchronized (AppOpsService.this) { |
| 3166 | final int tokenCount = mOpUserRestrictions.size(); |
| 3167 | for (int i = tokenCount - 1; i >= 0; i--) { |
| 3168 | ClientRestrictionState opRestrictions = mOpUserRestrictions.valueAt(i); |
| 3169 | opRestrictions.removeUser(userHandle); |
| 3170 | } |
Sudheer Shanka | bc2fadd | 2016-09-27 17:36:39 -0700 | [diff] [blame] | 3171 | removeUidsForUserLocked(userHandle); |
| 3172 | } |
| 3173 | } |
| 3174 | |
Jeff Sharkey | 35e46d2 | 2017-06-09 10:01:20 -0600 | [diff] [blame] | 3175 | @Override |
| 3176 | public boolean isOperationActive(int code, int uid, String packageName) { |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 3177 | if (Binder.getCallingUid() != uid) { |
| 3178 | if (mContext.checkCallingOrSelfPermission(Manifest.permission.WATCH_APPOPS) |
| 3179 | != PackageManager.PERMISSION_GRANTED) { |
| 3180 | return false; |
| 3181 | } |
| 3182 | } |
Jeff Sharkey | 35e46d2 | 2017-06-09 10:01:20 -0600 | [diff] [blame] | 3183 | verifyIncomingOp(code); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3184 | final String resolvedPackageName = resolvePackageName(uid, packageName); |
Jeff Sharkey | 35e46d2 | 2017-06-09 10:01:20 -0600 | [diff] [blame] | 3185 | if (resolvedPackageName == null) { |
| 3186 | return false; |
| 3187 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3188 | synchronized (AppOpsService.this) { |
Jeff Sharkey | 35e46d2 | 2017-06-09 10:01:20 -0600 | [diff] [blame] | 3189 | for (int i = mClients.size() - 1; i >= 0; i--) { |
| 3190 | final ClientState client = mClients.valueAt(i); |
Jeff Sharkey | 35e46d2 | 2017-06-09 10:01:20 -0600 | [diff] [blame] | 3191 | for (int j = client.mStartedOps.size() - 1; j >= 0; j--) { |
| 3192 | final Op op = client.mStartedOps.get(j); |
| 3193 | if (op.op == code && op.uid == uid) return true; |
| 3194 | } |
| 3195 | } |
| 3196 | } |
| 3197 | return false; |
| 3198 | } |
| 3199 | |
Sudheer Shanka | bc2fadd | 2016-09-27 17:36:39 -0700 | [diff] [blame] | 3200 | private void removeUidsForUserLocked(int userHandle) { |
| 3201 | for (int i = mUidStates.size() - 1; i >= 0; --i) { |
| 3202 | final int uid = mUidStates.keyAt(i); |
| 3203 | if (UserHandle.getUserId(uid) == userHandle) { |
| 3204 | mUidStates.removeAt(i); |
| 3205 | } |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 3206 | } |
| 3207 | } |
| 3208 | |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 3209 | private void checkSystemUid(String function) { |
| 3210 | int uid = Binder.getCallingUid(); |
| 3211 | if (uid != Process.SYSTEM_UID) { |
| 3212 | throw new SecurityException(function + " must by called by the system"); |
| 3213 | } |
| 3214 | } |
| 3215 | |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 3216 | private static String resolvePackageName(int uid, String packageName) { |
Svet Ganov | 82f09bc | 2018-01-12 22:08:40 -0800 | [diff] [blame] | 3217 | if (uid == Process.ROOT_UID) { |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 3218 | return "root"; |
| 3219 | } else if (uid == Process.SHELL_UID) { |
| 3220 | return "com.android.shell"; |
Svet Ganov | 82f09bc | 2018-01-12 22:08:40 -0800 | [diff] [blame] | 3221 | } else if (uid == Process.MEDIA_UID) { |
| 3222 | return "media"; |
| 3223 | } else if (uid == Process.AUDIOSERVER_UID) { |
| 3224 | return "audioserver"; |
| 3225 | } else if (uid == Process.CAMERASERVER_UID) { |
| 3226 | return "cameraserver"; |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 3227 | } else if (uid == Process.SYSTEM_UID && packageName == null) { |
| 3228 | return "android"; |
| 3229 | } |
| 3230 | return packageName; |
| 3231 | } |
| 3232 | |
Svet Ganov | 82f09bc | 2018-01-12 22:08:40 -0800 | [diff] [blame] | 3233 | private static int resolveUid(String packageName) { |
| 3234 | if (packageName == null) { |
| 3235 | return -1; |
| 3236 | } |
| 3237 | switch (packageName) { |
| 3238 | case "root": |
| 3239 | return Process.ROOT_UID; |
| 3240 | case "shell": |
| 3241 | return Process.SHELL_UID; |
| 3242 | case "media": |
| 3243 | return Process.MEDIA_UID; |
| 3244 | case "audioserver": |
| 3245 | return Process.AUDIOSERVER_UID; |
| 3246 | case "cameraserver": |
| 3247 | return Process.CAMERASERVER_UID; |
| 3248 | } |
| 3249 | return -1; |
| 3250 | } |
| 3251 | |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 3252 | private static String[] getPackagesForUid(int uid) { |
Svet Ganov | f3807aa | 2015-08-02 10:09:56 -0700 | [diff] [blame] | 3253 | String[] packageNames = null; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 3254 | try { |
riddle_hsu | 40b300f | 2015-11-23 13:22:03 +0800 | [diff] [blame] | 3255 | packageNames = AppGlobals.getPackageManager().getPackagesForUid(uid); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 3256 | } catch (RemoteException e) { |
| 3257 | /* ignore - local call */ |
| 3258 | } |
Svet Ganov | f3807aa | 2015-08-02 10:09:56 -0700 | [diff] [blame] | 3259 | if (packageNames == null) { |
| 3260 | return EmptyArray.STRING; |
| 3261 | } |
| 3262 | return packageNames; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 3263 | } |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 3264 | |
| 3265 | private final class ClientRestrictionState implements DeathRecipient { |
| 3266 | private final IBinder token; |
| 3267 | SparseArray<boolean[]> perUserRestrictions; |
| 3268 | SparseArray<String[]> perUserExcludedPackages; |
| 3269 | |
| 3270 | public ClientRestrictionState(IBinder token) |
| 3271 | throws RemoteException { |
| 3272 | token.linkToDeath(this, 0); |
| 3273 | this.token = token; |
| 3274 | } |
| 3275 | |
| 3276 | public boolean setRestriction(int code, boolean restricted, |
| 3277 | String[] excludedPackages, int userId) { |
| 3278 | boolean changed = false; |
| 3279 | |
| 3280 | if (perUserRestrictions == null && restricted) { |
| 3281 | perUserRestrictions = new SparseArray<>(); |
| 3282 | } |
| 3283 | |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 3284 | int[] users; |
| 3285 | if (userId == UserHandle.USER_ALL) { |
| 3286 | List<UserInfo> liveUsers = UserManager.get(mContext).getUsers(false); |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 3287 | |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 3288 | users = new int[liveUsers.size()]; |
| 3289 | for (int i = 0; i < liveUsers.size(); i++) { |
| 3290 | users[i] = liveUsers.get(i).id; |
| 3291 | } |
| 3292 | } else { |
| 3293 | users = new int[]{userId}; |
| 3294 | } |
| 3295 | |
| 3296 | if (perUserRestrictions != null) { |
| 3297 | int numUsers = users.length; |
| 3298 | |
| 3299 | for (int i = 0; i < numUsers; i++) { |
| 3300 | int thisUserId = users[i]; |
| 3301 | |
| 3302 | boolean[] userRestrictions = perUserRestrictions.get(thisUserId); |
| 3303 | if (userRestrictions == null && restricted) { |
| 3304 | userRestrictions = new boolean[AppOpsManager._NUM_OP]; |
| 3305 | perUserRestrictions.put(thisUserId, userRestrictions); |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 3306 | } |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 3307 | if (userRestrictions != null && userRestrictions[code] != restricted) { |
| 3308 | userRestrictions[code] = restricted; |
| 3309 | if (!restricted && isDefault(userRestrictions)) { |
| 3310 | perUserRestrictions.remove(thisUserId); |
| 3311 | userRestrictions = null; |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 3312 | } |
| 3313 | changed = true; |
| 3314 | } |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 3315 | |
| 3316 | if (userRestrictions != null) { |
| 3317 | final boolean noExcludedPackages = ArrayUtils.isEmpty(excludedPackages); |
| 3318 | if (perUserExcludedPackages == null && !noExcludedPackages) { |
| 3319 | perUserExcludedPackages = new SparseArray<>(); |
| 3320 | } |
| 3321 | if (perUserExcludedPackages != null && !Arrays.equals(excludedPackages, |
| 3322 | perUserExcludedPackages.get(thisUserId))) { |
| 3323 | if (noExcludedPackages) { |
| 3324 | perUserExcludedPackages.remove(thisUserId); |
| 3325 | if (perUserExcludedPackages.size() <= 0) { |
| 3326 | perUserExcludedPackages = null; |
| 3327 | } |
| 3328 | } else { |
| 3329 | perUserExcludedPackages.put(thisUserId, excludedPackages); |
| 3330 | } |
| 3331 | changed = true; |
| 3332 | } |
| 3333 | } |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 3334 | } |
| 3335 | } |
| 3336 | |
| 3337 | return changed; |
| 3338 | } |
| 3339 | |
| 3340 | public boolean hasRestriction(int restriction, String packageName, int userId) { |
| 3341 | if (perUserRestrictions == null) { |
| 3342 | return false; |
| 3343 | } |
| 3344 | boolean[] restrictions = perUserRestrictions.get(userId); |
| 3345 | if (restrictions == null) { |
| 3346 | return false; |
| 3347 | } |
| 3348 | if (!restrictions[restriction]) { |
| 3349 | return false; |
| 3350 | } |
| 3351 | if (perUserExcludedPackages == null) { |
| 3352 | return true; |
| 3353 | } |
| 3354 | String[] perUserExclusions = perUserExcludedPackages.get(userId); |
| 3355 | if (perUserExclusions == null) { |
| 3356 | return true; |
| 3357 | } |
| 3358 | return !ArrayUtils.contains(perUserExclusions, packageName); |
| 3359 | } |
| 3360 | |
| 3361 | public void removeUser(int userId) { |
| 3362 | if (perUserExcludedPackages != null) { |
| 3363 | perUserExcludedPackages.remove(userId); |
| 3364 | if (perUserExcludedPackages.size() <= 0) { |
| 3365 | perUserExcludedPackages = null; |
| 3366 | } |
| 3367 | } |
Sudheer Shanka | bc2fadd | 2016-09-27 17:36:39 -0700 | [diff] [blame] | 3368 | if (perUserRestrictions != null) { |
| 3369 | perUserRestrictions.remove(userId); |
| 3370 | if (perUserRestrictions.size() <= 0) { |
| 3371 | perUserRestrictions = null; |
| 3372 | } |
| 3373 | } |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 3374 | } |
| 3375 | |
| 3376 | public boolean isDefault() { |
| 3377 | return perUserRestrictions == null || perUserRestrictions.size() <= 0; |
| 3378 | } |
| 3379 | |
| 3380 | @Override |
| 3381 | public void binderDied() { |
| 3382 | synchronized (AppOpsService.this) { |
| 3383 | mOpUserRestrictions.remove(token); |
| 3384 | if (perUserRestrictions == null) { |
| 3385 | return; |
| 3386 | } |
| 3387 | final int userCount = perUserRestrictions.size(); |
| 3388 | for (int i = 0; i < userCount; i++) { |
| 3389 | final boolean[] restrictions = perUserRestrictions.valueAt(i); |
| 3390 | final int restrictionCount = restrictions.length; |
| 3391 | for (int j = 0; j < restrictionCount; j++) { |
| 3392 | if (restrictions[j]) { |
| 3393 | final int changedCode = j; |
Svet Ganov | 3a95f83 | 2018-03-23 17:44:30 -0700 | [diff] [blame] | 3394 | mHandler.post(() -> notifyWatchersOfChange(changedCode, UID_ANY)); |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 3395 | } |
| 3396 | } |
| 3397 | } |
| 3398 | destroy(); |
| 3399 | } |
| 3400 | } |
| 3401 | |
| 3402 | public void destroy() { |
| 3403 | token.unlinkToDeath(this, 0); |
| 3404 | } |
| 3405 | |
| 3406 | private boolean isDefault(boolean[] array) { |
| 3407 | if (ArrayUtils.isEmpty(array)) { |
| 3408 | return true; |
| 3409 | } |
| 3410 | for (boolean value : array) { |
| 3411 | if (value) { |
| 3412 | return false; |
| 3413 | } |
| 3414 | } |
| 3415 | return true; |
| 3416 | } |
| 3417 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3418 | } |