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