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