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