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