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