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