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