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