Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.om; |
| 18 | |
| 19 | import static android.app.AppGlobals.getPackageManager; |
| 20 | import static android.content.Intent.ACTION_PACKAGE_ADDED; |
| 21 | import static android.content.Intent.ACTION_PACKAGE_CHANGED; |
| 22 | import static android.content.Intent.ACTION_PACKAGE_REMOVED; |
Adam Lesinski | ada8deb | 2017-05-12 13:50:42 -0700 | [diff] [blame] | 23 | import static android.content.Intent.ACTION_USER_ADDED; |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 24 | import static android.content.Intent.ACTION_USER_REMOVED; |
| 25 | import static android.content.pm.PackageManager.SIGNATURE_MATCH; |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 26 | import static android.os.Trace.TRACE_TAG_RRO; |
| 27 | import static android.os.Trace.traceBegin; |
| 28 | import static android.os.Trace.traceEnd; |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 29 | |
| 30 | import android.annotation.NonNull; |
| 31 | import android.annotation.Nullable; |
| 32 | import android.app.ActivityManager; |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 33 | import android.app.IActivityManager; |
| 34 | import android.content.BroadcastReceiver; |
| 35 | import android.content.Context; |
| 36 | import android.content.Intent; |
| 37 | import android.content.IntentFilter; |
| 38 | import android.content.om.IOverlayManager; |
| 39 | import android.content.om.OverlayInfo; |
| 40 | import android.content.pm.IPackageManager; |
| 41 | import android.content.pm.PackageInfo; |
| 42 | import android.content.pm.PackageManagerInternal; |
| 43 | import android.content.pm.UserInfo; |
| 44 | import android.net.Uri; |
| 45 | import android.os.Binder; |
| 46 | import android.os.Environment; |
| 47 | import android.os.IBinder; |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 48 | import android.os.RemoteException; |
| 49 | import android.os.ResultReceiver; |
| 50 | import android.os.ShellCallback; |
Adam Lesinski | a5ca624 | 2017-03-01 15:45:12 -0800 | [diff] [blame] | 51 | import android.os.SystemProperties; |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 52 | import android.os.UserHandle; |
Adam Lesinski | ada8deb | 2017-05-12 13:50:42 -0700 | [diff] [blame] | 53 | import android.os.UserManager; |
Adam Lesinski | a5ca624 | 2017-03-01 15:45:12 -0800 | [diff] [blame] | 54 | import android.text.TextUtils; |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 55 | import android.util.ArrayMap; |
Adam Lesinski | a5ca624 | 2017-03-01 15:45:12 -0800 | [diff] [blame] | 56 | import android.util.ArraySet; |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 57 | import android.util.AtomicFile; |
| 58 | import android.util.Slog; |
| 59 | import android.util.SparseArray; |
| 60 | |
| 61 | import com.android.server.FgThread; |
| 62 | import com.android.server.IoThread; |
| 63 | import com.android.server.LocalServices; |
| 64 | import com.android.server.SystemService; |
| 65 | import com.android.server.pm.Installer; |
| 66 | import com.android.server.pm.UserManagerService; |
| 67 | |
Adam Lesinski | 7b03181 | 2018-02-22 13:32:53 -0800 | [diff] [blame] | 68 | import libcore.util.EmptyArray; |
| 69 | |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 70 | import org.xmlpull.v1.XmlPullParserException; |
| 71 | |
| 72 | import java.io.File; |
| 73 | import java.io.FileDescriptor; |
| 74 | import java.io.FileInputStream; |
| 75 | import java.io.FileOutputStream; |
| 76 | import java.io.IOException; |
| 77 | import java.io.PrintWriter; |
| 78 | import java.util.ArrayList; |
Adam Lesinski | d11c551 | 2017-04-11 12:01:10 -0700 | [diff] [blame] | 79 | import java.util.Arrays; |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 80 | import java.util.Collections; |
| 81 | import java.util.HashMap; |
| 82 | import java.util.List; |
| 83 | import java.util.Map; |
| 84 | import java.util.concurrent.atomic.AtomicBoolean; |
| 85 | |
| 86 | /** |
| 87 | * Service to manage asset overlays. |
| 88 | * |
| 89 | * <p>Asset overlays are additional resources that come from apks loaded |
| 90 | * alongside the system and app apks. This service, the OverlayManagerService |
| 91 | * (OMS), tracks which installed overlays to use and provides methods to change |
| 92 | * this. Changes propagate to running applications as part of the Activity |
| 93 | * lifecycle. This allows Activities to reread their resources at a well |
| 94 | * defined point.</p> |
| 95 | * |
| 96 | * <p>By itself, the OMS will not change what overlays should be active. |
| 97 | * Instead, it is only responsible for making sure that overlays *can* be used |
| 98 | * from a technical and security point of view and to activate overlays in |
| 99 | * response to external requests. The responsibility to toggle overlays on and |
| 100 | * off lies within components that implement different use-cases such as themes |
| 101 | * or dynamic customization.</p> |
| 102 | * |
| 103 | * <p>The OMS receives input from three sources:</p> |
| 104 | * |
| 105 | * <ul> |
| 106 | * <li>Callbacks from the SystemService class, specifically when the |
| 107 | * Android framework is booting and when the end user switches Android |
| 108 | * users.</li> |
| 109 | * |
| 110 | * <li>Intents from the PackageManagerService (PMS). Overlays are regular |
| 111 | * apks, and whenever a package is installed (or removed, or has a |
| 112 | * component enabled or disabled), the PMS broadcasts this as an intent. |
| 113 | * When the OMS receives one of these intents, it updates its internal |
| 114 | * representation of the available overlays and, if there was a visible |
| 115 | * change, triggers an asset refresh in the affected apps.</li> |
| 116 | * |
| 117 | * <li>External requests via the {@link IOverlayManager AIDL interface}. |
| 118 | * The interface allows clients to read information about the currently |
| 119 | * available overlays, change whether an overlay should be used or not, and |
| 120 | * change the relative order in which overlay packages are loaded. |
| 121 | * Read-access is granted if the request targets the same Android user as |
| 122 | * the caller runs as, or if the caller holds the |
| 123 | * INTERACT_ACROSS_USERS_FULL permission. Write-access is granted if the |
| 124 | * caller is granted read-access and additionaly holds the |
| 125 | * CHANGE_OVERLAY_PACKAGES permission.</li> |
| 126 | * </ul> |
| 127 | * |
| 128 | * <p>The AIDL interface works with String package names, int user IDs, and |
| 129 | * {@link OverlayInfo} objects. OverlayInfo instances are used to track a |
| 130 | * specific pair of target and overlay packages and include information such as |
| 131 | * the current state of the overlay. OverlayInfo objects are immutable.</p> |
| 132 | * |
| 133 | * <p>Internally, OverlayInfo objects are maintained by the |
| 134 | * OverlayManagerSettings class. The OMS and its helper classes are notified of |
| 135 | * changes to the settings by the OverlayManagerSettings.ChangeListener |
| 136 | * callback interface. The file /data/system/overlays.xml is used to persist |
| 137 | * the settings.</p> |
| 138 | * |
| 139 | * <p>Creation and deletion of idmap files are handled by the IdmapManager |
| 140 | * class.</p> |
| 141 | * |
| 142 | * <p>The following is an overview of OMS and its related classes. Note how box |
| 143 | * (2) does the heavy lifting, box (1) interacts with the Android framework, |
| 144 | * and box (3) replaces box (1) during unit testing.</p> |
| 145 | * |
| 146 | * <pre> |
| 147 | * Android framework |
| 148 | * | ^ |
| 149 | * . . . | . . . . | . . . . |
| 150 | * . | | . |
| 151 | * . AIDL, broadcasts . |
| 152 | * . intents | . |
| 153 | * . | | . . . . . . . . . . . . |
| 154 | * . v | . . |
| 155 | * . OverlayManagerService . OverlayManagerTests . |
| 156 | * . \ . / . |
| 157 | * . (1) \ . / (3) . |
| 158 | * . . . . . . . . . . \ . . . / . . . . . . . . . |
| 159 | * . \ / . |
| 160 | * . (2) \ / . |
| 161 | * . OverlayManagerServiceImpl . |
| 162 | * . | | . |
| 163 | * . | | . |
| 164 | * . OverlayManagerSettings IdmapManager . |
| 165 | * . . |
| 166 | * . . . . . . . . . . . . . . . . . . . . . . |
| 167 | * </pre> |
| 168 | * |
Mårten Kongstad | 1370c05 | 2018-05-03 10:48:35 +0200 | [diff] [blame] | 169 | * <p>To test the OMS, execute: |
| 170 | * <code> |
| 171 | * atest FrameworksServicesTests:com.android.server.om # internal tests |
| 172 | * atest OverlayDeviceTests OverlayHostTests # public API tests |
| 173 | * </code> |
| 174 | * </p> |
| 175 | * |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 176 | * <p>Finally, here is a list of keywords used in the OMS context.</p> |
| 177 | * |
| 178 | * <ul> |
| 179 | * <li><b>target [package]</b> -- A regular apk that may have its resource |
| 180 | * pool extended by zero or more overlay packages.</li> |
| 181 | * |
| 182 | * <li><b>overlay [package]</b> -- An apk that provides additional |
| 183 | * resources to another apk.</li> |
| 184 | * |
| 185 | * <li><b>OMS</b> -- The OverlayManagerService, i.e. this class.</li> |
| 186 | * |
| 187 | * <li><b>approved</b> -- An overlay is approved if the OMS has verified |
| 188 | * that it can be used technically speaking (its target package is |
| 189 | * installed, at least one resource name in both packages match, the |
| 190 | * idmap was created, etc) and that it is secure to do so. External |
| 191 | * clients can not change this state.</li> |
| 192 | * |
| 193 | * <li><b>not approved</b> -- The opposite of approved.</li> |
| 194 | * |
| 195 | * <li><b>enabled</b> -- An overlay currently in active use and thus part |
| 196 | * of resource lookups. This requires the overlay to be approved. Only |
| 197 | * external clients can change this state.</li> |
| 198 | * |
| 199 | * <li><b>disabled</b> -- The opposite of enabled.</li> |
| 200 | * |
| 201 | * <li><b>idmap</b> -- A mapping of resource IDs between target and overlay |
| 202 | * used during resource lookup. Also the name of the binary that creates |
| 203 | * the mapping.</li> |
| 204 | * </ul> |
| 205 | */ |
| 206 | public final class OverlayManagerService extends SystemService { |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 207 | static final String TAG = "OverlayManager"; |
| 208 | |
| 209 | static final boolean DEBUG = false; |
| 210 | |
Adam Lesinski | a5ca624 | 2017-03-01 15:45:12 -0800 | [diff] [blame] | 211 | /** |
| 212 | * The system property that specifies the default overlays to apply. |
| 213 | * This is a semicolon separated list of package names. |
| 214 | * |
| 215 | * Ex: com.android.vendor.overlay_one;com.android.vendor.overlay_two |
| 216 | */ |
| 217 | private static final String DEFAULT_OVERLAYS_PROP = "ro.boot.vendor.overlay.theme"; |
| 218 | |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 219 | private final Object mLock = new Object(); |
| 220 | |
| 221 | private final AtomicFile mSettingsFile; |
| 222 | |
| 223 | private final PackageManagerHelper mPackageManager; |
| 224 | |
| 225 | private final UserManagerService mUserManager; |
| 226 | |
| 227 | private final OverlayManagerSettings mSettings; |
| 228 | |
| 229 | private final OverlayManagerServiceImpl mImpl; |
| 230 | |
| 231 | private final AtomicBoolean mPersistSettingsScheduled = new AtomicBoolean(false); |
| 232 | |
| 233 | public OverlayManagerService(@NonNull final Context context, |
| 234 | @NonNull final Installer installer) { |
| 235 | super(context); |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 236 | try { |
| 237 | traceBegin(TRACE_TAG_RRO, "OMS#OverlayManagerService"); |
| 238 | mSettingsFile = new AtomicFile( |
| 239 | new File(Environment.getDataSystemDirectory(), "overlays.xml"), "overlays"); |
| 240 | mPackageManager = new PackageManagerHelper(); |
| 241 | mUserManager = UserManagerService.getInstance(); |
Winson | e0818bf | 2019-03-01 12:10:09 -0800 | [diff] [blame] | 242 | IdmapManager im = new IdmapManager(installer, mPackageManager); |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 243 | mSettings = new OverlayManagerSettings(); |
| 244 | mImpl = new OverlayManagerServiceImpl(mPackageManager, im, mSettings, |
| 245 | getDefaultOverlayPackages(), new OverlayChangeListener()); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 246 | |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 247 | final IntentFilter packageFilter = new IntentFilter(); |
| 248 | packageFilter.addAction(ACTION_PACKAGE_ADDED); |
| 249 | packageFilter.addAction(ACTION_PACKAGE_CHANGED); |
| 250 | packageFilter.addAction(ACTION_PACKAGE_REMOVED); |
| 251 | packageFilter.addDataScheme("package"); |
| 252 | getContext().registerReceiverAsUser(new PackageReceiver(), UserHandle.ALL, |
| 253 | packageFilter, null, null); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 254 | |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 255 | final IntentFilter userFilter = new IntentFilter(); |
| 256 | userFilter.addAction(ACTION_USER_ADDED); |
| 257 | userFilter.addAction(ACTION_USER_REMOVED); |
| 258 | getContext().registerReceiverAsUser(new UserReceiver(), UserHandle.ALL, |
| 259 | userFilter, null, null); |
Adam Lesinski | ada8deb | 2017-05-12 13:50:42 -0700 | [diff] [blame] | 260 | |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 261 | restoreSettings(); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 262 | |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 263 | initIfNeeded(); |
| 264 | onSwitchUser(UserHandle.USER_SYSTEM); |
Mårten Kongstad | cd78e0f | 2018-05-23 10:41:35 +0200 | [diff] [blame] | 265 | |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 266 | publishBinderService(Context.OVERLAY_SERVICE, mService); |
| 267 | publishLocalService(OverlayManagerService.class, this); |
| 268 | } finally { |
| 269 | traceEnd(TRACE_TAG_RRO); |
| 270 | } |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | @Override |
| 274 | public void onStart() { |
| 275 | // Intentionally left empty. |
| 276 | } |
| 277 | |
Adam Lesinski | ada8deb | 2017-05-12 13:50:42 -0700 | [diff] [blame] | 278 | private void initIfNeeded() { |
| 279 | final UserManager um = getContext().getSystemService(UserManager.class); |
| 280 | final List<UserInfo> users = um.getUsers(true /*excludeDying*/); |
| 281 | synchronized (mLock) { |
| 282 | final int userCount = users.size(); |
| 283 | for (int i = 0; i < userCount; i++) { |
| 284 | final UserInfo userInfo = users.get(i); |
| 285 | if (!userInfo.supportsSwitchTo() && userInfo.id != UserHandle.USER_SYSTEM) { |
| 286 | // Initialize any users that can't be switched to, as there state would |
| 287 | // never be setup in onSwitchUser(). We will switch to the system user right |
| 288 | // after this, and its state will be setup there. |
| 289 | final List<String> targets = mImpl.updateOverlaysForUser(users.get(i).id); |
| 290 | updateOverlayPaths(users.get(i).id, targets); |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
Fyodor Kupolov | 94ab1a6 | 2017-03-06 14:28:20 -0800 | [diff] [blame] | 296 | @Override |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 297 | public void onSwitchUser(final int newUserId) { |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 298 | try { |
| 299 | traceBegin(TRACE_TAG_RRO, "OMS#onSwitchUser " + newUserId); |
| 300 | // ensure overlays in the settings are up-to-date, and propagate |
| 301 | // any asset changes to the rest of the system |
| 302 | synchronized (mLock) { |
| 303 | final List<String> targets = mImpl.updateOverlaysForUser(newUserId); |
| 304 | updateAssets(newUserId, targets); |
| 305 | } |
| 306 | schedulePersistSettings(); |
| 307 | } finally { |
| 308 | traceEnd(TRACE_TAG_RRO); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 309 | } |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 310 | } |
| 311 | |
Adam Lesinski | 7b03181 | 2018-02-22 13:32:53 -0800 | [diff] [blame] | 312 | private static String[] getDefaultOverlayPackages() { |
Adam Lesinski | a5ca624 | 2017-03-01 15:45:12 -0800 | [diff] [blame] | 313 | final String str = SystemProperties.get(DEFAULT_OVERLAYS_PROP); |
| 314 | if (TextUtils.isEmpty(str)) { |
Adam Lesinski | 7b03181 | 2018-02-22 13:32:53 -0800 | [diff] [blame] | 315 | return EmptyArray.STRING; |
Adam Lesinski | a5ca624 | 2017-03-01 15:45:12 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | final ArraySet<String> defaultPackages = new ArraySet<>(); |
| 319 | for (String packageName : str.split(";")) { |
| 320 | if (!TextUtils.isEmpty(packageName)) { |
| 321 | defaultPackages.add(packageName); |
| 322 | } |
| 323 | } |
Adam Lesinski | 7b03181 | 2018-02-22 13:32:53 -0800 | [diff] [blame] | 324 | return defaultPackages.toArray(new String[defaultPackages.size()]); |
Adam Lesinski | a5ca624 | 2017-03-01 15:45:12 -0800 | [diff] [blame] | 325 | } |
| 326 | |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 327 | private final class PackageReceiver extends BroadcastReceiver { |
| 328 | @Override |
| 329 | public void onReceive(@NonNull final Context context, @NonNull final Intent intent) { |
Youngha Park | 14d8d68 | 2018-08-02 15:43:38 +0900 | [diff] [blame] | 330 | final String action = intent.getAction(); |
| 331 | if (action == null) { |
| 332 | Slog.e(TAG, "Cannot handle package broadcast with null action"); |
| 333 | return; |
| 334 | } |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 335 | final Uri data = intent.getData(); |
| 336 | if (data == null) { |
| 337 | Slog.e(TAG, "Cannot handle package broadcast with null data"); |
| 338 | return; |
| 339 | } |
| 340 | final String packageName = data.getSchemeSpecificPart(); |
| 341 | |
| 342 | final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); |
| 343 | |
| 344 | final int[] userIds; |
| 345 | final int extraUid = intent.getIntExtra(Intent.EXTRA_UID, UserHandle.USER_NULL); |
| 346 | if (extraUid == UserHandle.USER_NULL) { |
| 347 | userIds = mUserManager.getUserIds(); |
| 348 | } else { |
| 349 | userIds = new int[] { UserHandle.getUserId(extraUid) }; |
| 350 | } |
| 351 | |
Youngha Park | 14d8d68 | 2018-08-02 15:43:38 +0900 | [diff] [blame] | 352 | switch (action) { |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 353 | case ACTION_PACKAGE_ADDED: |
| 354 | if (replacing) { |
Mårten Kongstad | 15a0ac0 | 2019-03-26 10:30:08 +0100 | [diff] [blame] | 355 | onPackageReplaced(packageName, userIds); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 356 | } else { |
| 357 | onPackageAdded(packageName, userIds); |
| 358 | } |
| 359 | break; |
| 360 | case ACTION_PACKAGE_CHANGED: |
| 361 | onPackageChanged(packageName, userIds); |
| 362 | break; |
| 363 | case ACTION_PACKAGE_REMOVED: |
| 364 | if (replacing) { |
Mårten Kongstad | 15a0ac0 | 2019-03-26 10:30:08 +0100 | [diff] [blame] | 365 | onPackageReplacing(packageName, userIds); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 366 | } else { |
| 367 | onPackageRemoved(packageName, userIds); |
| 368 | } |
| 369 | break; |
| 370 | default: |
| 371 | // do nothing |
| 372 | break; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | private void onPackageAdded(@NonNull final String packageName, |
| 377 | @NonNull final int[] userIds) { |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 378 | try { |
| 379 | traceBegin(TRACE_TAG_RRO, "OMS#onPackageAdded " + packageName); |
| 380 | for (final int userId : userIds) { |
| 381 | synchronized (mLock) { |
| 382 | final PackageInfo pi = mPackageManager.getPackageInfo(packageName, userId, |
| 383 | false); |
Mårten Kongstad | ad32ba9 | 2019-03-26 16:05:23 +0100 | [diff] [blame] | 384 | if (pi != null && !pi.applicationInfo.isInstantApp()) { |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 385 | mPackageManager.cachePackageInfo(packageName, userId, pi); |
| 386 | if (pi.isOverlayPackage()) { |
| 387 | mImpl.onOverlayPackageAdded(packageName, userId); |
| 388 | } else { |
| 389 | mImpl.onTargetPackageAdded(packageName, userId); |
| 390 | } |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | } |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 394 | } finally { |
| 395 | traceEnd(TRACE_TAG_RRO); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
| 399 | private void onPackageChanged(@NonNull final String packageName, |
| 400 | @NonNull final int[] userIds) { |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 401 | try { |
| 402 | traceBegin(TRACE_TAG_RRO, "OMS#onPackageChanged " + packageName); |
| 403 | for (int userId : userIds) { |
| 404 | synchronized (mLock) { |
| 405 | final PackageInfo pi = mPackageManager.getPackageInfo(packageName, userId, |
| 406 | false); |
Mårten Kongstad | ad32ba9 | 2019-03-26 16:05:23 +0100 | [diff] [blame] | 407 | if (pi != null && pi.applicationInfo.isInstantApp()) { |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 408 | mPackageManager.cachePackageInfo(packageName, userId, pi); |
| 409 | if (pi.isOverlayPackage()) { |
| 410 | mImpl.onOverlayPackageChanged(packageName, userId); |
| 411 | } else { |
| 412 | mImpl.onTargetPackageChanged(packageName, userId); |
| 413 | } |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | } |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 417 | } finally { |
| 418 | traceEnd(TRACE_TAG_RRO); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
Mårten Kongstad | 15a0ac0 | 2019-03-26 10:30:08 +0100 | [diff] [blame] | 422 | private void onPackageReplacing(@NonNull final String packageName, |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 423 | @NonNull final int[] userIds) { |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 424 | try { |
Mårten Kongstad | 15a0ac0 | 2019-03-26 10:30:08 +0100 | [diff] [blame] | 425 | traceBegin(TRACE_TAG_RRO, "OMS#onPackageReplacing " + packageName); |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 426 | for (int userId : userIds) { |
| 427 | synchronized (mLock) { |
| 428 | mPackageManager.forgetPackageInfo(packageName, userId); |
| 429 | final OverlayInfo oi = mImpl.getOverlayInfo(packageName, userId); |
| 430 | if (oi != null) { |
Mårten Kongstad | 15a0ac0 | 2019-03-26 10:30:08 +0100 | [diff] [blame] | 431 | mImpl.onOverlayPackageReplacing(packageName, userId); |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 432 | } |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 433 | } |
| 434 | } |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 435 | } finally { |
| 436 | traceEnd(TRACE_TAG_RRO); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
Mårten Kongstad | 15a0ac0 | 2019-03-26 10:30:08 +0100 | [diff] [blame] | 440 | private void onPackageReplaced(@NonNull final String packageName, |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 441 | @NonNull final int[] userIds) { |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 442 | try { |
Mårten Kongstad | 15a0ac0 | 2019-03-26 10:30:08 +0100 | [diff] [blame] | 443 | traceBegin(TRACE_TAG_RRO, "OMS#onPackageReplaced " + packageName); |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 444 | for (int userId : userIds) { |
| 445 | synchronized (mLock) { |
| 446 | final PackageInfo pi = mPackageManager.getPackageInfo(packageName, userId, |
| 447 | false); |
Mårten Kongstad | ad32ba9 | 2019-03-26 16:05:23 +0100 | [diff] [blame] | 448 | if (pi != null && !pi.applicationInfo.isInstantApp()) { |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 449 | mPackageManager.cachePackageInfo(packageName, userId, pi); |
| 450 | if (pi.isOverlayPackage()) { |
Mårten Kongstad | 15a0ac0 | 2019-03-26 10:30:08 +0100 | [diff] [blame] | 451 | mImpl.onOverlayPackageReplaced(packageName, userId); |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 452 | } else { |
Mårten Kongstad | 15a0ac0 | 2019-03-26 10:30:08 +0100 | [diff] [blame] | 453 | mImpl.onTargetPackageReplaced(packageName, userId); |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 454 | } |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | } |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 458 | } finally { |
| 459 | traceEnd(TRACE_TAG_RRO); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
| 463 | private void onPackageRemoved(@NonNull final String packageName, |
| 464 | @NonNull final int[] userIds) { |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 465 | try { |
| 466 | traceBegin(TRACE_TAG_RRO, "OMS#onPackageRemoved " + packageName); |
| 467 | for (int userId : userIds) { |
| 468 | synchronized (mLock) { |
| 469 | mPackageManager.forgetPackageInfo(packageName, userId); |
| 470 | final OverlayInfo oi = mImpl.getOverlayInfo(packageName, userId); |
| 471 | if (oi != null) { |
| 472 | mImpl.onOverlayPackageRemoved(packageName, userId); |
| 473 | } else { |
| 474 | mImpl.onTargetPackageRemoved(packageName, userId); |
| 475 | } |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 476 | } |
| 477 | } |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 478 | } finally { |
| 479 | traceEnd(TRACE_TAG_RRO); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | private final class UserReceiver extends BroadcastReceiver { |
| 485 | @Override |
| 486 | public void onReceive(@NonNull final Context context, @NonNull final Intent intent) { |
Adam Lesinski | ada8deb | 2017-05-12 13:50:42 -0700 | [diff] [blame] | 487 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 488 | switch (intent.getAction()) { |
Adam Lesinski | ada8deb | 2017-05-12 13:50:42 -0700 | [diff] [blame] | 489 | case ACTION_USER_ADDED: |
| 490 | if (userId != UserHandle.USER_NULL) { |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 491 | try { |
| 492 | traceBegin(TRACE_TAG_RRO, "OMS ACTION_USER_ADDED"); |
| 493 | final ArrayList<String> targets; |
| 494 | synchronized (mLock) { |
| 495 | targets = mImpl.updateOverlaysForUser(userId); |
| 496 | } |
| 497 | updateOverlayPaths(userId, targets); |
| 498 | } finally { |
| 499 | traceEnd(TRACE_TAG_RRO); |
Adam Lesinski | ada8deb | 2017-05-12 13:50:42 -0700 | [diff] [blame] | 500 | } |
Adam Lesinski | ada8deb | 2017-05-12 13:50:42 -0700 | [diff] [blame] | 501 | } |
| 502 | break; |
| 503 | |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 504 | case ACTION_USER_REMOVED: |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 505 | if (userId != UserHandle.USER_NULL) { |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 506 | try { |
| 507 | traceBegin(TRACE_TAG_RRO, "OMS ACTION_USER_REMOVED"); |
| 508 | synchronized (mLock) { |
| 509 | mImpl.onUserRemoved(userId); |
| 510 | mPackageManager.forgetAllPackageInfos(userId); |
| 511 | } |
| 512 | } finally { |
| 513 | traceEnd(TRACE_TAG_RRO); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | break; |
| 517 | default: |
| 518 | // do nothing |
| 519 | break; |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | private final IBinder mService = new IOverlayManager.Stub() { |
| 525 | @Override |
Adam Lesinski | c745f42 | 2017-04-05 16:31:30 -0700 | [diff] [blame] | 526 | public Map<String, List<OverlayInfo>> getAllOverlays(int userId) throws RemoteException { |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 527 | try { |
| 528 | traceBegin(TRACE_TAG_RRO, "OMS#getAllOverlays " + userId); |
| 529 | userId = handleIncomingUser(userId, "getAllOverlays"); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 530 | |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 531 | synchronized (mLock) { |
| 532 | return mImpl.getOverlaysForUser(userId); |
| 533 | } |
| 534 | } finally { |
| 535 | traceEnd(TRACE_TAG_RRO); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
| 539 | @Override |
| 540 | public List<OverlayInfo> getOverlayInfosForTarget(@Nullable final String targetPackageName, |
| 541 | int userId) throws RemoteException { |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 542 | try { |
| 543 | traceBegin(TRACE_TAG_RRO, "OMS#getOverlayInfosForTarget " + targetPackageName); |
| 544 | userId = handleIncomingUser(userId, "getOverlayInfosForTarget"); |
| 545 | if (targetPackageName == null) { |
| 546 | return Collections.emptyList(); |
| 547 | } |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 548 | |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 549 | synchronized (mLock) { |
| 550 | return mImpl.getOverlayInfosForTarget(targetPackageName, userId); |
| 551 | } |
| 552 | } finally { |
| 553 | traceEnd(TRACE_TAG_RRO); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | |
| 557 | @Override |
| 558 | public OverlayInfo getOverlayInfo(@Nullable final String packageName, |
| 559 | int userId) throws RemoteException { |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 560 | try { |
| 561 | traceBegin(TRACE_TAG_RRO, "OMS#getOverlayInfo " + packageName); |
| 562 | userId = handleIncomingUser(userId, "getOverlayInfo"); |
| 563 | if (packageName == null) { |
| 564 | return null; |
| 565 | } |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 566 | |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 567 | synchronized (mLock) { |
| 568 | return mImpl.getOverlayInfo(packageName, userId); |
| 569 | } |
| 570 | } finally { |
| 571 | traceEnd(TRACE_TAG_RRO); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | |
| 575 | @Override |
| 576 | public boolean setEnabled(@Nullable final String packageName, final boolean enable, |
| 577 | int userId) throws RemoteException { |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 578 | try { |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 579 | traceBegin(TRACE_TAG_RRO, "OMS#setEnabled " + packageName + " " + enable); |
| 580 | enforceChangeOverlayPackagesPermission("setEnabled"); |
| 581 | userId = handleIncomingUser(userId, "setEnabled"); |
| 582 | if (packageName == null) { |
| 583 | return false; |
| 584 | } |
| 585 | |
| 586 | final long ident = Binder.clearCallingIdentity(); |
| 587 | try { |
| 588 | synchronized (mLock) { |
| 589 | return mImpl.setEnabled(packageName, enable, userId); |
| 590 | } |
| 591 | } finally { |
| 592 | Binder.restoreCallingIdentity(ident); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 593 | } |
| 594 | } finally { |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 595 | traceEnd(TRACE_TAG_RRO); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | |
| 599 | @Override |
Jason Monk | 929ed8d | 2017-03-07 16:01:20 -0500 | [diff] [blame] | 600 | public boolean setEnabledExclusive(@Nullable final String packageName, final boolean enable, |
| 601 | int userId) throws RemoteException { |
Jason Monk | 929ed8d | 2017-03-07 16:01:20 -0500 | [diff] [blame] | 602 | try { |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 603 | traceBegin(TRACE_TAG_RRO, "OMS#setEnabledExclusive " + packageName + " " + enable); |
| 604 | enforceChangeOverlayPackagesPermission("setEnabledExclusive"); |
| 605 | userId = handleIncomingUser(userId, "setEnabledExclusive"); |
| 606 | if (packageName == null || !enable) { |
| 607 | return false; |
| 608 | } |
| 609 | |
| 610 | final long ident = Binder.clearCallingIdentity(); |
| 611 | try { |
| 612 | synchronized (mLock) { |
| 613 | return mImpl.setEnabledExclusive(packageName, false /* withinCategory */, |
| 614 | userId); |
| 615 | } |
| 616 | } finally { |
| 617 | Binder.restoreCallingIdentity(ident); |
Adrian Roos | c84df77 | 2018-01-19 21:20:22 +0100 | [diff] [blame] | 618 | } |
| 619 | } finally { |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 620 | traceEnd(TRACE_TAG_RRO); |
Adrian Roos | c84df77 | 2018-01-19 21:20:22 +0100 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | |
| 624 | @Override |
| 625 | public boolean setEnabledExclusiveInCategory(@Nullable String packageName, int userId) |
| 626 | throws RemoteException { |
Adrian Roos | c84df77 | 2018-01-19 21:20:22 +0100 | [diff] [blame] | 627 | try { |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 628 | traceBegin(TRACE_TAG_RRO, "OMS#setEnabledExclusiveInCategory " + packageName); |
| 629 | enforceChangeOverlayPackagesPermission("setEnabledExclusiveInCategory"); |
| 630 | userId = handleIncomingUser(userId, "setEnabledExclusiveInCategory"); |
| 631 | if (packageName == null) { |
| 632 | return false; |
| 633 | } |
| 634 | |
| 635 | final long ident = Binder.clearCallingIdentity(); |
| 636 | try { |
| 637 | synchronized (mLock) { |
| 638 | return mImpl.setEnabledExclusive(packageName, true /* withinCategory */, |
| 639 | userId); |
| 640 | } |
| 641 | } finally { |
| 642 | Binder.restoreCallingIdentity(ident); |
Jason Monk | 929ed8d | 2017-03-07 16:01:20 -0500 | [diff] [blame] | 643 | } |
| 644 | } finally { |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 645 | traceEnd(TRACE_TAG_RRO); |
Jason Monk | 929ed8d | 2017-03-07 16:01:20 -0500 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | |
| 649 | @Override |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 650 | public boolean setPriority(@Nullable final String packageName, |
| 651 | @Nullable final String parentPackageName, int userId) throws RemoteException { |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 652 | try { |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 653 | traceBegin(TRACE_TAG_RRO, "OMS#setPriority " + packageName + " " |
| 654 | + parentPackageName); |
| 655 | enforceChangeOverlayPackagesPermission("setPriority"); |
| 656 | userId = handleIncomingUser(userId, "setPriority"); |
| 657 | if (packageName == null || parentPackageName == null) { |
| 658 | return false; |
| 659 | } |
| 660 | |
| 661 | final long ident = Binder.clearCallingIdentity(); |
| 662 | try { |
| 663 | synchronized (mLock) { |
| 664 | return mImpl.setPriority(packageName, parentPackageName, userId); |
| 665 | } |
| 666 | } finally { |
| 667 | Binder.restoreCallingIdentity(ident); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 668 | } |
| 669 | } finally { |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 670 | traceEnd(TRACE_TAG_RRO); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | |
| 674 | @Override |
| 675 | public boolean setHighestPriority(@Nullable final String packageName, int userId) |
| 676 | throws RemoteException { |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 677 | try { |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 678 | traceBegin(TRACE_TAG_RRO, "OMS#setHighestPriority " + packageName); |
| 679 | enforceChangeOverlayPackagesPermission("setHighestPriority"); |
| 680 | userId = handleIncomingUser(userId, "setHighestPriority"); |
| 681 | if (packageName == null) { |
| 682 | return false; |
| 683 | } |
| 684 | |
| 685 | final long ident = Binder.clearCallingIdentity(); |
| 686 | try { |
| 687 | synchronized (mLock) { |
| 688 | return mImpl.setHighestPriority(packageName, userId); |
| 689 | } |
| 690 | } finally { |
| 691 | Binder.restoreCallingIdentity(ident); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 692 | } |
| 693 | } finally { |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 694 | traceEnd(TRACE_TAG_RRO); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 695 | } |
| 696 | } |
| 697 | |
| 698 | @Override |
| 699 | public boolean setLowestPriority(@Nullable final String packageName, int userId) |
| 700 | throws RemoteException { |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 701 | try { |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 702 | traceBegin(TRACE_TAG_RRO, "OMS#setLowestPriority " + packageName); |
| 703 | enforceChangeOverlayPackagesPermission("setLowestPriority"); |
| 704 | userId = handleIncomingUser(userId, "setLowestPriority"); |
| 705 | if (packageName == null) { |
| 706 | return false; |
| 707 | } |
| 708 | |
| 709 | final long ident = Binder.clearCallingIdentity(); |
| 710 | try { |
| 711 | synchronized (mLock) { |
| 712 | return mImpl.setLowestPriority(packageName, userId); |
| 713 | } |
| 714 | } finally { |
| 715 | Binder.restoreCallingIdentity(ident); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 716 | } |
| 717 | } finally { |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 718 | traceEnd(TRACE_TAG_RRO); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | |
| 722 | @Override |
Winson Chung | 43f311e | 2019-04-29 12:57:21 -0700 | [diff] [blame] | 723 | public String[] getDefaultOverlayPackages() throws RemoteException { |
| 724 | try { |
| 725 | traceBegin(TRACE_TAG_RRO, "OMS#getDefaultOverlayPackages"); |
| 726 | getContext().enforceCallingOrSelfPermission( |
| 727 | android.Manifest.permission.MODIFY_THEME_OVERLAY, null); |
| 728 | |
| 729 | final long ident = Binder.clearCallingIdentity(); |
| 730 | try { |
| 731 | synchronized (mLock) { |
| 732 | return mImpl.getDefaultOverlayPackages(); |
| 733 | } |
| 734 | } finally { |
| 735 | Binder.restoreCallingIdentity(ident); |
| 736 | } |
| 737 | } finally { |
| 738 | traceEnd(TRACE_TAG_RRO); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | @Override |
Ryan Mitchell | 4043ca7 | 2019-06-03 16:11:24 -0700 | [diff] [blame^] | 743 | public void invalidateCachesForOverlay(@Nullable String packageName, int userId) |
| 744 | throws RemoteException { |
| 745 | if (packageName == null) { |
| 746 | return; |
| 747 | } |
| 748 | |
| 749 | enforceChangeOverlayPackagesPermission("invalidateCachesForOverlay"); |
| 750 | userId = handleIncomingUser(userId, "invalidateCachesForOverlay"); |
| 751 | final long ident = Binder.clearCallingIdentity(); |
| 752 | try { |
| 753 | synchronized (mLock) { |
| 754 | mImpl.removeIdmapForOverlay(packageName, userId); |
| 755 | } |
| 756 | } finally { |
| 757 | Binder.restoreCallingIdentity(ident); |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | @Override |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 762 | public void onShellCommand(@NonNull final FileDescriptor in, |
| 763 | @NonNull final FileDescriptor out, @NonNull final FileDescriptor err, |
| 764 | @NonNull final String[] args, @NonNull final ShellCallback callback, |
| 765 | @NonNull final ResultReceiver resultReceiver) { |
Mårten Kongstad | 4ea02d1 | 2019-06-14 14:08:18 +0200 | [diff] [blame] | 766 | (new OverlayManagerShellCommand(getContext(), this)).exec( |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 767 | this, in, out, err, args, callback, resultReceiver); |
| 768 | } |
| 769 | |
| 770 | @Override |
Mårten Kongstad | a465aec | 2019-04-12 12:32:41 +0200 | [diff] [blame] | 771 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 772 | final DumpState dumpState = new DumpState(); |
Ryan Mitchell | b7ef06a | 2019-07-17 17:16:23 -0700 | [diff] [blame] | 773 | dumpState.setUserId(UserHandle.USER_ALL); |
Mårten Kongstad | a465aec | 2019-04-12 12:32:41 +0200 | [diff] [blame] | 774 | |
| 775 | int opti = 0; |
| 776 | while (opti < args.length) { |
| 777 | final String opt = args[opti]; |
| 778 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 779 | break; |
| 780 | } |
| 781 | opti++; |
| 782 | |
| 783 | if ("-h".equals(opt)) { |
| 784 | pw.println("dump [-h] [--verbose] [--user USER_ID] [[FIELD] PACKAGE]"); |
| 785 | pw.println(" Print debugging information about the overlay manager."); |
| 786 | pw.println(" With optional parameter PACKAGE, limit output to the specified"); |
| 787 | pw.println(" package. With optional parameter FIELD, limit output to"); |
| 788 | pw.println(" the value of that SettingsItem field. Field names are"); |
| 789 | pw.println(" case insensitive and out.println the m prefix can be omitted,"); |
| 790 | pw.println(" so the following are equivalent: mState, mstate, State, state."); |
| 791 | return; |
| 792 | } else if ("--user".equals(opt)) { |
Mårten Kongstad | a465aec | 2019-04-12 12:32:41 +0200 | [diff] [blame] | 793 | if (opti >= args.length) { |
| 794 | pw.println("Error: user missing argument"); |
| 795 | return; |
| 796 | } |
| 797 | try { |
| 798 | dumpState.setUserId(Integer.parseInt(args[opti])); |
Ryan Mitchell | b7ef06a | 2019-07-17 17:16:23 -0700 | [diff] [blame] | 799 | opti++; |
Mårten Kongstad | a465aec | 2019-04-12 12:32:41 +0200 | [diff] [blame] | 800 | } catch (NumberFormatException e) { |
| 801 | pw.println("Error: user argument is not a number: " + args[opti]); |
| 802 | return; |
| 803 | } |
| 804 | } else if ("--verbose".equals(opt)) { |
| 805 | dumpState.setVerbose(true); |
| 806 | } else { |
| 807 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
| 808 | } |
| 809 | } |
| 810 | if (opti < args.length) { |
| 811 | final String arg = args[opti]; |
| 812 | opti++; |
| 813 | switch (arg) { |
| 814 | case "packagename": |
| 815 | case "userid": |
| 816 | case "targetpackagename": |
| 817 | case "targetoverlayablename": |
| 818 | case "basecodepath": |
| 819 | case "state": |
| 820 | case "isenabled": |
| 821 | case "isstatic": |
| 822 | case "priority": |
| 823 | case "category": |
| 824 | dumpState.setField(arg); |
| 825 | break; |
| 826 | default: |
| 827 | dumpState.setPackageName(arg); |
| 828 | break; |
| 829 | } |
| 830 | } |
| 831 | if (dumpState.getPackageName() == null && opti < args.length) { |
| 832 | dumpState.setPackageName(args[opti]); |
| 833 | opti++; |
| 834 | } |
| 835 | |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 836 | enforceDumpPermission("dump"); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 837 | synchronized (mLock) { |
Mårten Kongstad | a465aec | 2019-04-12 12:32:41 +0200 | [diff] [blame] | 838 | mImpl.dump(pw, dumpState); |
| 839 | if (dumpState.getPackageName() == null) { |
| 840 | mPackageManager.dump(pw, dumpState); |
| 841 | } |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 842 | } |
| 843 | } |
| 844 | |
| 845 | /** |
| 846 | * Ensure that the caller has permission to interact with the given userId. |
| 847 | * If the calling user is not the same as the provided user, the caller needs |
| 848 | * to hold the INTERACT_ACROSS_USERS_FULL permission (or be system uid or |
| 849 | * root). |
| 850 | * |
| 851 | * @param userId the user to interact with |
| 852 | * @param message message for any SecurityException |
| 853 | */ |
| 854 | private int handleIncomingUser(final int userId, @NonNull final String message) { |
| 855 | return ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 856 | Binder.getCallingUid(), userId, false, true, message, null); |
| 857 | } |
| 858 | |
| 859 | /** |
| 860 | * Enforce that the caller holds the CHANGE_OVERLAY_PACKAGES permission (or is |
| 861 | * system or root). |
| 862 | * |
| 863 | * @param message used as message if SecurityException is thrown |
| 864 | * @throws SecurityException if the permission check fails |
| 865 | */ |
| 866 | private void enforceChangeOverlayPackagesPermission(@NonNull final String message) { |
Winson Chung | ddaf351 | 2019-03-18 13:02:44 -0700 | [diff] [blame] | 867 | getContext().enforceCallingOrSelfPermission( |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 868 | android.Manifest.permission.CHANGE_OVERLAY_PACKAGES, message); |
| 869 | } |
| 870 | |
| 871 | /** |
| 872 | * Enforce that the caller holds the DUMP permission (or is system or root). |
| 873 | * |
| 874 | * @param message used as message if SecurityException is thrown |
| 875 | * @throws SecurityException if the permission check fails |
| 876 | */ |
| 877 | private void enforceDumpPermission(@NonNull final String message) { |
Winson Chung | ddaf351 | 2019-03-18 13:02:44 -0700 | [diff] [blame] | 878 | getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, message); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 879 | } |
| 880 | }; |
| 881 | |
Adam Lesinski | c745f42 | 2017-04-05 16:31:30 -0700 | [diff] [blame] | 882 | private final class OverlayChangeListener |
| 883 | implements OverlayManagerServiceImpl.OverlayChangeListener { |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 884 | @Override |
Mårten Kongstad | 497945c | 2018-04-27 09:56:13 +0200 | [diff] [blame] | 885 | public void onOverlaysChanged(@NonNull final String targetPackageName, final int userId) { |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 886 | schedulePersistSettings(); |
Adam Lesinski | c745f42 | 2017-04-05 16:31:30 -0700 | [diff] [blame] | 887 | FgThread.getHandler().post(() -> { |
Mårten Kongstad | 497945c | 2018-04-27 09:56:13 +0200 | [diff] [blame] | 888 | updateAssets(userId, targetPackageName); |
| 889 | |
| 890 | final Intent intent = new Intent(Intent.ACTION_OVERLAY_CHANGED, |
| 891 | Uri.fromParts("package", targetPackageName, null)); |
| 892 | intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 893 | |
| 894 | if (DEBUG) { |
| 895 | Slog.d(TAG, "send broadcast " + intent); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 896 | } |
Adam Lesinski | c745f42 | 2017-04-05 16:31:30 -0700 | [diff] [blame] | 897 | |
Mårten Kongstad | 497945c | 2018-04-27 09:56:13 +0200 | [diff] [blame] | 898 | try { |
| 899 | ActivityManager.getService().broadcastIntent(null, intent, null, null, 0, |
| 900 | null, null, null, android.app.AppOpsManager.OP_NONE, null, false, false, |
| 901 | userId); |
| 902 | } catch (RemoteException e) { |
| 903 | // Intentionally left empty. |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 904 | } |
Adam Lesinski | c745f42 | 2017-04-05 16:31:30 -0700 | [diff] [blame] | 905 | }); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 906 | } |
| 907 | } |
| 908 | |
Adam Lesinski | ada8deb | 2017-05-12 13:50:42 -0700 | [diff] [blame] | 909 | /** |
| 910 | * Updates the target packages' set of enabled overlays in PackageManager. |
| 911 | */ |
| 912 | private void updateOverlayPaths(int userId, List<String> targetPackageNames) { |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 913 | try { |
| 914 | traceBegin(TRACE_TAG_RRO, "OMS#updateOverlayPaths " + targetPackageNames); |
| 915 | if (DEBUG) { |
| 916 | Slog.d(TAG, "Updating overlay assets"); |
| 917 | } |
| 918 | final PackageManagerInternal pm = |
| 919 | LocalServices.getService(PackageManagerInternal.class); |
| 920 | final boolean updateFrameworkRes = targetPackageNames.contains("android"); |
| 921 | if (updateFrameworkRes) { |
| 922 | targetPackageNames = pm.getTargetPackageNames(userId); |
| 923 | } |
Mårten Kongstad | 2e0d0f3 | 2016-06-02 09:35:31 +0200 | [diff] [blame] | 924 | |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 925 | final Map<String, List<String>> pendingChanges = |
| 926 | new ArrayMap<>(targetPackageNames.size()); |
| 927 | synchronized (mLock) { |
| 928 | final List<String> frameworkOverlays = |
| 929 | mImpl.getEnabledOverlayPackageNames("android", userId); |
| 930 | final int n = targetPackageNames.size(); |
| 931 | for (int i = 0; i < n; i++) { |
| 932 | final String targetPackageName = targetPackageNames.get(i); |
| 933 | List<String> list = new ArrayList<>(); |
| 934 | if (!"android".equals(targetPackageName)) { |
| 935 | list.addAll(frameworkOverlays); |
| 936 | } |
| 937 | list.addAll(mImpl.getEnabledOverlayPackageNames(targetPackageName, userId)); |
| 938 | pendingChanges.put(targetPackageName, list); |
| 939 | } |
| 940 | } |
| 941 | |
Mårten Kongstad | 3898834 | 2018-12-04 10:28:01 +0100 | [diff] [blame] | 942 | final int n = targetPackageNames.size(); |
| 943 | for (int i = 0; i < n; i++) { |
Mårten Kongstad | 2e0d0f3 | 2016-06-02 09:35:31 +0200 | [diff] [blame] | 944 | final String targetPackageName = targetPackageNames.get(i); |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 945 | if (DEBUG) { |
| 946 | Slog.d(TAG, "-> Updating overlay: target=" + targetPackageName + " overlays=[" |
| 947 | + TextUtils.join(",", pendingChanges.get(targetPackageName)) |
| 948 | + "] userId=" + userId); |
Todd Kennedy | 560830c | 2017-06-16 13:55:13 -0700 | [diff] [blame] | 949 | } |
Mårten Kongstad | 2e0d0f3 | 2016-06-02 09:35:31 +0200 | [diff] [blame] | 950 | |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 951 | if (!pm.setEnabledOverlayPackages( |
| 952 | userId, targetPackageName, pendingChanges.get(targetPackageName))) { |
| 953 | Slog.e(TAG, String.format("Failed to change enabled overlays for %s user %d", |
| 954 | targetPackageName, userId)); |
| 955 | } |
Adam Lesinski | c745f42 | 2017-04-05 16:31:30 -0700 | [diff] [blame] | 956 | } |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 957 | } finally { |
| 958 | traceEnd(TRACE_TAG_RRO); |
Mårten Kongstad | 2e0d0f3 | 2016-06-02 09:35:31 +0200 | [diff] [blame] | 959 | } |
Adam Lesinski | ada8deb | 2017-05-12 13:50:42 -0700 | [diff] [blame] | 960 | } |
Mårten Kongstad | 2e0d0f3 | 2016-06-02 09:35:31 +0200 | [diff] [blame] | 961 | |
Adam Lesinski | ada8deb | 2017-05-12 13:50:42 -0700 | [diff] [blame] | 962 | private void updateAssets(final int userId, final String targetPackageName) { |
| 963 | updateAssets(userId, Collections.singletonList(targetPackageName)); |
| 964 | } |
| 965 | |
| 966 | private void updateAssets(final int userId, List<String> targetPackageNames) { |
| 967 | updateOverlayPaths(userId, targetPackageNames); |
Mårten Kongstad | 2e0d0f3 | 2016-06-02 09:35:31 +0200 | [diff] [blame] | 968 | final IActivityManager am = ActivityManager.getService(); |
| 969 | try { |
| 970 | am.scheduleApplicationInfoChanged(targetPackageNames, userId); |
| 971 | } catch (RemoteException e) { |
| 972 | // Intentionally left empty. |
| 973 | } |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | private void schedulePersistSettings() { |
| 977 | if (mPersistSettingsScheduled.getAndSet(true)) { |
| 978 | return; |
| 979 | } |
Adam Lesinski | c745f42 | 2017-04-05 16:31:30 -0700 | [diff] [blame] | 980 | IoThread.getHandler().post(() -> { |
| 981 | mPersistSettingsScheduled.set(false); |
| 982 | if (DEBUG) { |
| 983 | Slog.d(TAG, "Writing overlay settings"); |
| 984 | } |
| 985 | synchronized (mLock) { |
| 986 | FileOutputStream stream = null; |
| 987 | try { |
| 988 | stream = mSettingsFile.startWrite(); |
| 989 | mSettings.persist(stream); |
| 990 | mSettingsFile.finishWrite(stream); |
| 991 | } catch (IOException | XmlPullParserException e) { |
| 992 | mSettingsFile.failWrite(stream); |
| 993 | Slog.e(TAG, "failed to persist overlay state", e); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 994 | } |
| 995 | } |
| 996 | }); |
| 997 | } |
| 998 | |
| 999 | private void restoreSettings() { |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 1000 | try { |
| 1001 | traceBegin(TRACE_TAG_RRO, "OMS#restoreSettings"); |
| 1002 | synchronized (mLock) { |
| 1003 | if (!mSettingsFile.getBaseFile().exists()) { |
| 1004 | return; |
Adam Lesinski | d11c551 | 2017-04-11 12:01:10 -0700 | [diff] [blame] | 1005 | } |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 1006 | try (FileInputStream stream = mSettingsFile.openRead()) { |
| 1007 | mSettings.restore(stream); |
Adam Lesinski | d11c551 | 2017-04-11 12:01:10 -0700 | [diff] [blame] | 1008 | |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 1009 | // We might have data for dying users if the device was |
| 1010 | // restarted before we received USER_REMOVED. Remove data for |
| 1011 | // users that will not exist after the system is ready. |
| 1012 | |
| 1013 | final List<UserInfo> liveUsers = mUserManager.getUsers(true /*excludeDying*/); |
| 1014 | final int[] liveUserIds = new int[liveUsers.size()]; |
| 1015 | for (int i = 0; i < liveUsers.size(); i++) { |
| 1016 | liveUserIds[i] = liveUsers.get(i).getUserHandle().getIdentifier(); |
Adam Lesinski | d11c551 | 2017-04-11 12:01:10 -0700 | [diff] [blame] | 1017 | } |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 1018 | Arrays.sort(liveUserIds); |
| 1019 | |
| 1020 | for (int userId : mSettings.getUsers()) { |
| 1021 | if (Arrays.binarySearch(liveUserIds, userId) < 0) { |
| 1022 | mSettings.removeUser(userId); |
| 1023 | } |
| 1024 | } |
| 1025 | } catch (IOException | XmlPullParserException e) { |
| 1026 | Slog.e(TAG, "failed to restore overlay state", e); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 1027 | } |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 1028 | } |
Mårten Kongstad | acfcdc7 | 2018-12-03 14:59:51 +0100 | [diff] [blame] | 1029 | } finally { |
| 1030 | traceEnd(TRACE_TAG_RRO); |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 1031 | } |
| 1032 | } |
| 1033 | |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 1034 | private static final class PackageManagerHelper implements |
| 1035 | OverlayManagerServiceImpl.PackageManagerHelper { |
| 1036 | |
| 1037 | private final IPackageManager mPackageManager; |
| 1038 | private final PackageManagerInternal mPackageManagerInternal; |
| 1039 | |
| 1040 | // Use a cache for performance and for consistency within OMS: because |
| 1041 | // additional PACKAGE_* intents may be delivered while we process an |
| 1042 | // intent, querying the PackageManagerService for the actual current |
| 1043 | // state may lead to contradictions within OMS. Better then to lag |
| 1044 | // behind until all pending intents have been processed. |
| 1045 | private final SparseArray<HashMap<String, PackageInfo>> mCache = new SparseArray<>(); |
| 1046 | |
| 1047 | PackageManagerHelper() { |
| 1048 | mPackageManager = getPackageManager(); |
| 1049 | mPackageManagerInternal = LocalServices.getService(PackageManagerInternal.class); |
| 1050 | } |
| 1051 | |
| 1052 | public PackageInfo getPackageInfo(@NonNull final String packageName, final int userId, |
| 1053 | final boolean useCache) { |
| 1054 | if (useCache) { |
| 1055 | final PackageInfo cachedPi = getCachedPackageInfo(packageName, userId); |
| 1056 | if (cachedPi != null) { |
| 1057 | return cachedPi; |
| 1058 | } |
| 1059 | } |
| 1060 | try { |
| 1061 | final PackageInfo pi = mPackageManager.getPackageInfo(packageName, 0, userId); |
| 1062 | if (useCache && pi != null) { |
| 1063 | cachePackageInfo(packageName, userId, pi); |
| 1064 | } |
| 1065 | return pi; |
| 1066 | } catch (RemoteException e) { |
| 1067 | // Intentionally left empty. |
| 1068 | } |
| 1069 | return null; |
| 1070 | } |
| 1071 | |
| 1072 | @Override |
| 1073 | public PackageInfo getPackageInfo(@NonNull final String packageName, final int userId) { |
| 1074 | return getPackageInfo(packageName, userId, true); |
| 1075 | } |
| 1076 | |
| 1077 | @Override |
| 1078 | public boolean signaturesMatching(@NonNull final String packageName1, |
| 1079 | @NonNull final String packageName2, final int userId) { |
| 1080 | // The package manager does not support different versions of packages |
| 1081 | // to be installed for different users: ignore userId for now. |
| 1082 | try { |
Adam Lesinski | c745f42 | 2017-04-05 16:31:30 -0700 | [diff] [blame] | 1083 | return mPackageManager.checkSignatures( |
| 1084 | packageName1, packageName2) == SIGNATURE_MATCH; |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 1085 | } catch (RemoteException e) { |
| 1086 | // Intentionally left blank |
| 1087 | } |
| 1088 | return false; |
| 1089 | } |
| 1090 | |
| 1091 | @Override |
| 1092 | public List<PackageInfo> getOverlayPackages(final int userId) { |
| 1093 | return mPackageManagerInternal.getOverlayPackages(userId); |
| 1094 | } |
| 1095 | |
| 1096 | public PackageInfo getCachedPackageInfo(@NonNull final String packageName, |
| 1097 | final int userId) { |
| 1098 | final HashMap<String, PackageInfo> map = mCache.get(userId); |
| 1099 | return map == null ? null : map.get(packageName); |
| 1100 | } |
| 1101 | |
| 1102 | public void cachePackageInfo(@NonNull final String packageName, final int userId, |
| 1103 | @NonNull final PackageInfo pi) { |
| 1104 | HashMap<String, PackageInfo> map = mCache.get(userId); |
| 1105 | if (map == null) { |
| 1106 | map = new HashMap<>(); |
| 1107 | mCache.put(userId, map); |
| 1108 | } |
| 1109 | map.put(packageName, pi); |
| 1110 | } |
| 1111 | |
| 1112 | public void forgetPackageInfo(@NonNull final String packageName, final int userId) { |
| 1113 | final HashMap<String, PackageInfo> map = mCache.get(userId); |
| 1114 | if (map == null) { |
| 1115 | return; |
| 1116 | } |
| 1117 | map.remove(packageName); |
| 1118 | if (map.isEmpty()) { |
| 1119 | mCache.delete(userId); |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | public void forgetAllPackageInfos(final int userId) { |
| 1124 | mCache.delete(userId); |
| 1125 | } |
| 1126 | |
| 1127 | private static final String TAB1 = " "; |
| 1128 | private static final String TAB2 = TAB1 + TAB1; |
| 1129 | |
Mårten Kongstad | a465aec | 2019-04-12 12:32:41 +0200 | [diff] [blame] | 1130 | public void dump(@NonNull final PrintWriter pw, @NonNull DumpState dumpState) { |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 1131 | pw.println("PackageInfo cache"); |
| 1132 | |
Mårten Kongstad | a465aec | 2019-04-12 12:32:41 +0200 | [diff] [blame] | 1133 | if (!dumpState.isVerbose()) { |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 1134 | int count = 0; |
Mårten Kongstad | 3898834 | 2018-12-04 10:28:01 +0100 | [diff] [blame] | 1135 | final int n = mCache.size(); |
| 1136 | for (int i = 0; i < n; i++) { |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 1137 | final int userId = mCache.keyAt(i); |
| 1138 | count += mCache.get(userId).size(); |
| 1139 | } |
| 1140 | pw.println(TAB1 + count + " package(s)"); |
| 1141 | return; |
| 1142 | } |
| 1143 | |
| 1144 | if (mCache.size() == 0) { |
| 1145 | pw.println(TAB1 + "<empty>"); |
| 1146 | return; |
| 1147 | } |
| 1148 | |
Mårten Kongstad | 3898834 | 2018-12-04 10:28:01 +0100 | [diff] [blame] | 1149 | final int n = mCache.size(); |
| 1150 | for (int i = 0; i < n; i++) { |
Mårten Kongstad | eabc9e9 | 2015-12-15 16:40:23 +0100 | [diff] [blame] | 1151 | final int userId = mCache.keyAt(i); |
| 1152 | pw.println(TAB1 + "User " + userId); |
| 1153 | final HashMap<String, PackageInfo> map = mCache.get(userId); |
| 1154 | for (Map.Entry<String, PackageInfo> entry : map.entrySet()) { |
| 1155 | pw.println(TAB2 + entry.getKey() + ": " + entry.getValue()); |
| 1156 | } |
| 1157 | } |
| 1158 | } |
| 1159 | } |
| 1160 | } |