Merge "Add argument checks in setScale and setCrop" into tm-dev
diff --git a/apct-tests/perftests/windowmanager/src/android/wm/RelayoutPerfTest.java b/apct-tests/perftests/windowmanager/src/android/wm/RelayoutPerfTest.java
index 4ad015d..c92c634 100644
--- a/apct-tests/perftests/windowmanager/src/android/wm/RelayoutPerfTest.java
+++ b/apct-tests/perftests/windowmanager/src/android/wm/RelayoutPerfTest.java
@@ -20,6 +20,7 @@
 
 import android.app.Activity;
 import android.content.Context;
+import android.os.Bundle;
 import android.os.RemoteException;
 import android.perftests.utils.BenchmarkState;
 import android.perftests.utils.PerfStatusReporter;
@@ -153,7 +154,8 @@
             while (state.keepRunning()) {
                 session.relayout(mWindow, mParams, mWidth, mHeight,
                         mViewVisibility.getAsInt(), mFlags, mOutFrames,
-                        mOutMergedConfiguration, mOutSurfaceControl, mOutInsetsState, mOutControls);
+                        mOutMergedConfiguration, mOutSurfaceControl, mOutInsetsState, mOutControls,
+                        new Bundle());
             }
         }
     }
diff --git a/apex/jobscheduler/framework/java/android/app/AlarmManager.java b/apex/jobscheduler/framework/java/android/app/AlarmManager.java
index 66767e2..da429af 100644
--- a/apex/jobscheduler/framework/java/android/app/AlarmManager.java
+++ b/apex/jobscheduler/framework/java/android/app/AlarmManager.java
@@ -210,6 +210,8 @@
      * on how frequently it can be scheduled.  Only available (and automatically applied) to
      * system alarms.
      *
+     * <p>Note that alarms set with a {@link WorkSource} <b>do not</b> get this flag.
+     *
      * @hide
      */
     @UnsupportedAppUsage
diff --git a/apex/jobscheduler/service/java/com/android/server/alarm/AlarmManagerService.java b/apex/jobscheduler/service/java/com/android/server/alarm/AlarmManagerService.java
index 90ec700..528be3c 100644
--- a/apex/jobscheduler/service/java/com/android/server/alarm/AlarmManagerService.java
+++ b/apex/jobscheduler/service/java/com/android/server/alarm/AlarmManagerService.java
@@ -4891,13 +4891,15 @@
             filter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
             filter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
             filter.addDataScheme(IntentFilter.SCHEME_PACKAGE);
-            getContext().registerReceiver(this, filter);
+            getContext().registerReceiverForAllUsers(this, filter,
+                    /* broadcastPermission */ null, /* scheduler */ null);
             // Register for events related to sdcard installation.
             IntentFilter sdFilter = new IntentFilter();
             sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
             sdFilter.addAction(Intent.ACTION_USER_STOPPED);
             sdFilter.addAction(Intent.ACTION_UID_REMOVED);
-            getContext().registerReceiver(this, sdFilter);
+            getContext().registerReceiverForAllUsers(this, sdFilter,
+                    /* broadcastPermission */ null, /* scheduler */ null);
         }
 
         @Override
@@ -4915,9 +4917,6 @@
                             }
                         }
                         return;
-                    case Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE:
-                        pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
-                        break;
                     case Intent.ACTION_USER_STOPPED:
                         final int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
                         if (userHandle >= 0) {
@@ -4932,6 +4931,18 @@
                         mRemovalHistory.delete(uid);
                         mLastOpScheduleExactAlarm.delete(uid);
                         return;
+                    case Intent.ACTION_PACKAGE_ADDED:
+                        if (intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
+                            final String packageUpdated = intent.getData().getSchemeSpecificPart();
+                            mHandler.obtainMessage(
+                                    AlarmHandler.CHECK_EXACT_ALARM_PERMISSION_ON_UPDATE, uid, -1,
+                                    packageUpdated).sendToTarget();
+                        }
+                        mHandler.sendEmptyMessage(AlarmHandler.REFRESH_EXACT_ALARM_CANDIDATES);
+                        return;
+                    case Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE:
+                        pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
+                        break;
                     case Intent.ACTION_PACKAGE_REMOVED:
                         if (intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
                             // This package is being updated; don't kill its alarms.
@@ -4950,15 +4961,6 @@
                             }
                         }
                         break;
-                    case Intent.ACTION_PACKAGE_ADDED:
-                        if (intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
-                            final String packageUpdated = intent.getData().getSchemeSpecificPart();
-                            mHandler.obtainMessage(
-                                    AlarmHandler.CHECK_EXACT_ALARM_PERMISSION_ON_UPDATE, uid, -1,
-                                    packageUpdated).sendToTarget();
-                        }
-                        mHandler.sendEmptyMessage(AlarmHandler.REFRESH_EXACT_ALARM_CANDIDATES);
-                        return;
                 }
                 if (pkgList != null && (pkgList.length > 0)) {
                     for (String pkg : pkgList) {
diff --git a/apex/jobscheduler/service/java/com/android/server/job/JobStore.java b/apex/jobscheduler/service/java/com/android/server/job/JobStore.java
index a8dd752..dfa1442 100644
--- a/apex/jobscheduler/service/java/com/android/server/job/JobStore.java
+++ b/apex/jobscheduler/service/java/com/android/server/job/JobStore.java
@@ -548,7 +548,7 @@
             out.attribute(null, "sourceUserId", String.valueOf(jobStatus.getSourceUserId()));
             out.attribute(null, "uid", Integer.toString(jobStatus.getUid()));
             out.attribute(null, "bias", String.valueOf(jobStatus.getBias()));
-            out.attribute(null, "priority", String.valueOf(jobStatus.getEffectivePriority()));
+            out.attribute(null, "priority", String.valueOf(jobStatus.getJob().getPriority()));
             out.attribute(null, "flags", String.valueOf(jobStatus.getFlags()));
             if (jobStatus.getInternalFlags() != 0) {
                 out.attribute(null, "internalFlags", String.valueOf(jobStatus.getInternalFlags()));
diff --git a/apex/jobscheduler/service/java/com/android/server/usage/AppIdleHistory.java b/apex/jobscheduler/service/java/com/android/server/usage/AppIdleHistory.java
index dd102bd..e986b1a 100644
--- a/apex/jobscheduler/service/java/com/android/server/usage/AppIdleHistory.java
+++ b/apex/jobscheduler/service/java/com/android/server/usage/AppIdleHistory.java
@@ -667,8 +667,8 @@
     long getBucketExpiryTimeMs(String packageName, int userId, int bucket, long elapsedRealtimeMs) {
         ArrayMap<String, AppUsageHistory> userHistory = getUserHistory(userId);
         AppUsageHistory appUsageHistory = getPackageHistory(userHistory, packageName,
-                elapsedRealtimeMs, true);
-        if (appUsageHistory.bucketExpiryTimesMs == null) {
+                elapsedRealtimeMs, false /* create */);
+        if (appUsageHistory == null || appUsageHistory.bucketExpiryTimesMs == null) {
             return 0;
         }
         return appUsageHistory.bucketExpiryTimesMs.get(bucket, 0);
diff --git a/apex/jobscheduler/service/java/com/android/server/usage/AppStandbyController.java b/apex/jobscheduler/service/java/com/android/server/usage/AppStandbyController.java
index 849354b..4952894 100644
--- a/apex/jobscheduler/service/java/com/android/server/usage/AppStandbyController.java
+++ b/apex/jobscheduler/service/java/com/android/server/usage/AppStandbyController.java
@@ -78,6 +78,7 @@
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.PackageManagerInternal;
+import android.content.pm.ResolveInfo;
 import android.database.ContentObserver;
 import android.hardware.display.DisplayManager;
 import android.net.NetworkScoreManager;
@@ -219,7 +220,8 @@
 
     private static final int HEADLESS_APP_CHECK_FLAGS =
             PackageManager.MATCH_DIRECT_BOOT_AWARE | PackageManager.MATCH_DIRECT_BOOT_UNAWARE
-                    | PackageManager.GET_ACTIVITIES | PackageManager.MATCH_DISABLED_COMPONENTS;
+                    | PackageManager.MATCH_DISABLED_COMPONENTS
+                    | PackageManager.MATCH_SYSTEM_ONLY;
 
     // To name the lock for stack traces
     static class Lock {}
@@ -253,7 +255,7 @@
     private final SparseArray<Set<String>> mActiveAdminApps = new SparseArray<>();
 
     /**
-     * Set of system apps that are headless (don't have any declared activities, enabled or
+     * Set of system apps that are headless (don't have any "front door" activities, enabled or
      * disabled). Presence in this map indicates that the app is a headless system app.
      */
     @GuardedBy("mHeadlessSystemApps")
@@ -1573,8 +1575,10 @@
                     (reason & REASON_MAIN_MASK) == REASON_MAIN_FORCED_BY_SYSTEM;
 
             if (app.currentBucket == newBucket && wasForcedBySystem && isForcedBySystem) {
-                mAppIdleHistory
-                        .noteRestrictionAttempt(packageName, userId, elapsedRealtime, reason);
+                if (newBucket == STANDBY_BUCKET_RESTRICTED) {
+                    mAppIdleHistory
+                            .noteRestrictionAttempt(packageName, userId, elapsedRealtime, reason);
+                }
                 // Keep track of all restricting reasons
                 reason = REASON_MAIN_FORCED_BY_SYSTEM
                         | (app.bucketingReason & REASON_SUB_MASK)
@@ -1942,7 +1946,7 @@
         try {
             PackageInfo pi = mPackageManager.getPackageInfoAsUser(
                     packageName, HEADLESS_APP_CHECK_FLAGS, userId);
-            evaluateSystemAppException(pi);
+            maybeUpdateHeadlessSystemAppCache(pi);
         } catch (PackageManager.NameNotFoundException e) {
             synchronized (mHeadlessSystemApps) {
                 mHeadlessSystemApps.remove(packageName);
@@ -1950,19 +1954,31 @@
         }
     }
 
-    /** Returns true if the exception status changed. */
-    private boolean evaluateSystemAppException(@Nullable PackageInfo pkgInfo) {
+    /**
+     * Update the "headless system app" cache.
+     *
+     * @return true if the cache is updated.
+     */
+    private boolean maybeUpdateHeadlessSystemAppCache(@Nullable PackageInfo pkgInfo) {
         if (pkgInfo == null || pkgInfo.applicationInfo == null
                 || (!pkgInfo.applicationInfo.isSystemApp()
                         && !pkgInfo.applicationInfo.isUpdatedSystemApp())) {
             return false;
         }
+        final Intent frontDoorActivityIntent = new Intent(Intent.ACTION_MAIN)
+                .addCategory(Intent.CATEGORY_LAUNCHER)
+                .setPackage(pkgInfo.packageName);
+        List<ResolveInfo> res = mPackageManager.queryIntentActivitiesAsUser(frontDoorActivityIntent,
+                HEADLESS_APP_CHECK_FLAGS, UserHandle.USER_SYSTEM);
+        return updateHeadlessSystemAppCache(pkgInfo.packageName, ArrayUtils.isEmpty(res));
+    }
+
+    private boolean updateHeadlessSystemAppCache(String packageName, boolean add) {
         synchronized (mHeadlessSystemApps) {
-            if (pkgInfo.activities == null || pkgInfo.activities.length == 0) {
-                // Headless system app.
-                return mHeadlessSystemApps.add(pkgInfo.packageName);
+            if (add) {
+                return mHeadlessSystemApps.add(packageName);
             } else {
-                return mHeadlessSystemApps.remove(pkgInfo.packageName);
+                return mHeadlessSystemApps.remove(packageName);
             }
         }
     }
@@ -1999,20 +2015,45 @@
         }
     }
 
+    /** Returns the packages that have launcher icons. */
+    private Set<String> getSystemPackagesWithLauncherActivities() {
+        final Intent intent = new Intent(Intent.ACTION_MAIN)
+                .addCategory(Intent.CATEGORY_LAUNCHER);
+        List<ResolveInfo> activities = mPackageManager.queryIntentActivitiesAsUser(intent,
+                HEADLESS_APP_CHECK_FLAGS, UserHandle.USER_SYSTEM);
+        final ArraySet<String> ret = new ArraySet<>();
+        for (ResolveInfo ri : activities) {
+            ret.add(ri.activityInfo.packageName);
+        }
+        return ret;
+    }
+
     /** Call on system boot to get the initial set of headless system apps. */
     private void loadHeadlessSystemAppCache() {
-        Slog.d(TAG, "Loading headless system app cache. appIdleEnabled=" + mAppIdleEnabled);
+        final long start = SystemClock.uptimeMillis();
         final List<PackageInfo> packages = mPackageManager.getInstalledPackagesAsUser(
                 HEADLESS_APP_CHECK_FLAGS, UserHandle.USER_SYSTEM);
+
+        final Set<String> systemLauncherActivities = getSystemPackagesWithLauncherActivities();
+
         final int packageCount = packages.size();
         for (int i = 0; i < packageCount; i++) {
-            PackageInfo pkgInfo = packages.get(i);
-            if (pkgInfo != null && evaluateSystemAppException(pkgInfo)) {
+            final PackageInfo pkgInfo = packages.get(i);
+            if (pkgInfo == null) {
+                continue;
+            }
+            final String pkg = pkgInfo.packageName;
+            final boolean isHeadLess = !systemLauncherActivities.contains(pkg);
+
+            if (updateHeadlessSystemAppCache(pkg, isHeadLess)) {
                 mHandler.obtainMessage(MSG_CHECK_PACKAGE_IDLE_STATE,
-                        UserHandle.USER_SYSTEM, -1, pkgInfo.packageName)
+                        UserHandle.USER_SYSTEM, -1, pkg)
                     .sendToTarget();
             }
         }
+        final long end = SystemClock.uptimeMillis();
+        Slog.d(TAG, "Loaded headless system app cache in " + (end - start) + " ms:"
+                + " appIdleEnabled=" + mAppIdleEnabled);
     }
 
     @Override
diff --git a/boot/hiddenapi/hiddenapi-max-target-o.txt b/boot/hiddenapi/hiddenapi-max-target-o.txt
index d3b5be9..3c16915 100644
--- a/boot/hiddenapi/hiddenapi-max-target-o.txt
+++ b/boot/hiddenapi/hiddenapi-max-target-o.txt
@@ -32472,14 +32472,6 @@
 Landroid/net/DhcpResults;->setServerAddress(Ljava/lang/String;)Z
 Landroid/net/DhcpResults;->setVendorInfo(Ljava/lang/String;)V
 Landroid/net/DhcpResults;->TAG:Ljava/lang/String;
-Landroid/net/EthernetManager;-><init>(Landroid/content/Context;Landroid/net/IEthernetManager;)V
-Landroid/net/EthernetManager;->mContext:Landroid/content/Context;
-Landroid/net/EthernetManager;->mHandler:Landroid/os/Handler;
-Landroid/net/EthernetManager;->mListeners:Ljava/util/ArrayList;
-Landroid/net/EthernetManager;->mService:Landroid/net/IEthernetManager;
-Landroid/net/EthernetManager;->mServiceListener:Landroid/net/IEthernetServiceListener$Stub;
-Landroid/net/EthernetManager;->MSG_AVAILABILITY_CHANGED:I
-Landroid/net/EthernetManager;->TAG:Ljava/lang/String;
 Landroid/net/EventLogTags;-><init>()V
 Landroid/net/EventLogTags;->NTP_FAILURE:I
 Landroid/net/EventLogTags;->NTP_SUCCESS:I
@@ -32513,39 +32505,6 @@
 Landroid/net/http/X509TrustManagerExtensions;->mDelegate:Lcom/android/org/conscrypt/TrustManagerImpl;
 Landroid/net/http/X509TrustManagerExtensions;->mIsSameTrustConfiguration:Ljava/lang/reflect/Method;
 Landroid/net/http/X509TrustManagerExtensions;->mTrustManager:Ljavax/net/ssl/X509TrustManager;
-Landroid/net/IEthernetManager$Stub$Proxy;-><init>(Landroid/os/IBinder;)V
-Landroid/net/IEthernetManager$Stub$Proxy;->addListener(Landroid/net/IEthernetServiceListener;)V
-Landroid/net/IEthernetManager$Stub$Proxy;->getAvailableInterfaces()[Ljava/lang/String;
-Landroid/net/IEthernetManager$Stub$Proxy;->getConfiguration(Ljava/lang/String;)Landroid/net/IpConfiguration;
-Landroid/net/IEthernetManager$Stub$Proxy;->getInterfaceDescriptor()Ljava/lang/String;
-Landroid/net/IEthernetManager$Stub$Proxy;->isAvailable(Ljava/lang/String;)Z
-Landroid/net/IEthernetManager$Stub$Proxy;->mRemote:Landroid/os/IBinder;
-Landroid/net/IEthernetManager$Stub$Proxy;->removeListener(Landroid/net/IEthernetServiceListener;)V
-Landroid/net/IEthernetManager$Stub$Proxy;->setConfiguration(Ljava/lang/String;Landroid/net/IpConfiguration;)V
-Landroid/net/IEthernetManager$Stub;-><init>()V
-Landroid/net/IEthernetManager$Stub;->asInterface(Landroid/os/IBinder;)Landroid/net/IEthernetManager;
-Landroid/net/IEthernetManager$Stub;->DESCRIPTOR:Ljava/lang/String;
-Landroid/net/IEthernetManager$Stub;->TRANSACTION_addListener:I
-Landroid/net/IEthernetManager$Stub;->TRANSACTION_getAvailableInterfaces:I
-Landroid/net/IEthernetManager$Stub;->TRANSACTION_getConfiguration:I
-Landroid/net/IEthernetManager$Stub;->TRANSACTION_isAvailable:I
-Landroid/net/IEthernetManager$Stub;->TRANSACTION_removeListener:I
-Landroid/net/IEthernetManager$Stub;->TRANSACTION_setConfiguration:I
-Landroid/net/IEthernetManager;->addListener(Landroid/net/IEthernetServiceListener;)V
-Landroid/net/IEthernetManager;->getAvailableInterfaces()[Ljava/lang/String;
-Landroid/net/IEthernetManager;->getConfiguration(Ljava/lang/String;)Landroid/net/IpConfiguration;
-Landroid/net/IEthernetManager;->isAvailable(Ljava/lang/String;)Z
-Landroid/net/IEthernetManager;->removeListener(Landroid/net/IEthernetServiceListener;)V
-Landroid/net/IEthernetManager;->setConfiguration(Ljava/lang/String;Landroid/net/IpConfiguration;)V
-Landroid/net/IEthernetServiceListener$Stub$Proxy;-><init>(Landroid/os/IBinder;)V
-Landroid/net/IEthernetServiceListener$Stub$Proxy;->getInterfaceDescriptor()Ljava/lang/String;
-Landroid/net/IEthernetServiceListener$Stub$Proxy;->mRemote:Landroid/os/IBinder;
-Landroid/net/IEthernetServiceListener$Stub$Proxy;->onAvailabilityChanged(Ljava/lang/String;Z)V
-Landroid/net/IEthernetServiceListener$Stub;-><init>()V
-Landroid/net/IEthernetServiceListener$Stub;->asInterface(Landroid/os/IBinder;)Landroid/net/IEthernetServiceListener;
-Landroid/net/IEthernetServiceListener$Stub;->DESCRIPTOR:Ljava/lang/String;
-Landroid/net/IEthernetServiceListener$Stub;->TRANSACTION_onAvailabilityChanged:I
-Landroid/net/IEthernetServiceListener;->onAvailabilityChanged(Ljava/lang/String;Z)V
 Landroid/net/IIpConnectivityMetrics$Stub$Proxy;-><init>(Landroid/os/IBinder;)V
 Landroid/net/IIpConnectivityMetrics$Stub$Proxy;->addNetdEventCallback(ILandroid/net/INetdEventCallback;)Z
 Landroid/net/IIpConnectivityMetrics$Stub$Proxy;->getInterfaceDescriptor()Ljava/lang/String;
diff --git a/cmds/app_process/Android.bp b/cmds/app_process/Android.bp
index 6a685a79..a157517 100644
--- a/cmds/app_process/Android.bp
+++ b/cmds/app_process/Android.bp
@@ -64,8 +64,6 @@
         "libwilhelm",
     ],
 
-    header_libs: ["bionic_libc_platform_headers"],
-
     compile_multilib: "both",
 
     cflags: [
diff --git a/cmds/app_process/app_main.cpp b/cmds/app_process/app_main.cpp
index 815f945..12083b6 100644
--- a/cmds/app_process/app_main.cpp
+++ b/cmds/app_process/app_main.cpp
@@ -15,7 +15,6 @@
 
 #include <android-base/macros.h>
 #include <binder/IPCThreadState.h>
-#include <bionic/pac.h>
 #include <hwbinder/IPCThreadState.h>
 #include <utils/Log.h>
 #include <cutils/memory.h>
@@ -183,10 +182,6 @@
       ALOGV("app_process main with argv: %s", argv_String.string());
     }
 
-    // Because of applications that are using PAC instructions incorrectly, PAC
-    // is disabled in application processes for now.
-    ScopedDisablePAC x;
-
     AppRuntime runtime(argv[0], computeArgBlockSize(argc, argv));
     // Process command line arguments
     // ignore argv[0]
diff --git a/cmds/idmap2/Android.bp b/cmds/idmap2/Android.bp
index c202f6f..6ef6845 100644
--- a/cmds/idmap2/Android.bp
+++ b/cmds/idmap2/Android.bp
@@ -52,6 +52,7 @@
         "-readability-braces-around-statements",
         "-readability-const-return-type",
         "-readability-convert-member-functions-to-static",
+        "-readability-duplicate-include",
         "-readability-else-after-return",
         "-readability-identifier-length",
         "-readability-named-parameter",
diff --git a/core/api/current.txt b/core/api/current.txt
index 542221c..b5493a3 100644
--- a/core/api/current.txt
+++ b/core/api/current.txt
@@ -113,8 +113,9 @@
     field public static final String MANAGE_MEDIA = "android.permission.MANAGE_MEDIA";
     field public static final String MANAGE_ONGOING_CALLS = "android.permission.MANAGE_ONGOING_CALLS";
     field public static final String MANAGE_OWN_CALLS = "android.permission.MANAGE_OWN_CALLS";
-    field public static final String MANAGE_WIFI_AUTO_JOIN = "android.permission.MANAGE_WIFI_AUTO_JOIN";
+    field @Deprecated public static final String MANAGE_WIFI_AUTO_JOIN = "android.permission.MANAGE_WIFI_AUTO_JOIN";
     field public static final String MANAGE_WIFI_INTERFACES = "android.permission.MANAGE_WIFI_INTERFACES";
+    field public static final String MANAGE_WIFI_NETWORK_SELECTION = "android.permission.MANAGE_WIFI_NETWORK_SELECTION";
     field public static final String MASTER_CLEAR = "android.permission.MASTER_CLEAR";
     field public static final String MEDIA_CONTENT_CONTROL = "android.permission.MEDIA_CONTENT_CONTROL";
     field public static final String MODIFY_AUDIO_SETTINGS = "android.permission.MODIFY_AUDIO_SETTINGS";
@@ -141,7 +142,7 @@
     field @Deprecated public static final String READ_INPUT_STATE = "android.permission.READ_INPUT_STATE";
     field public static final String READ_LOGS = "android.permission.READ_LOGS";
     field public static final String READ_MEDIA_AUDIO = "android.permission.READ_MEDIA_AUDIO";
-    field public static final String READ_MEDIA_IMAGE = "android.permission.READ_MEDIA_IMAGE";
+    field public static final String READ_MEDIA_IMAGES = "android.permission.READ_MEDIA_IMAGES";
     field public static final String READ_MEDIA_VIDEO = "android.permission.READ_MEDIA_VIDEO";
     field public static final String READ_NEARBY_STREAMING_POLICY = "android.permission.READ_NEARBY_STREAMING_POLICY";
     field public static final String READ_PHONE_NUMBERS = "android.permission.READ_PHONE_NUMBERS";
@@ -3335,7 +3336,7 @@
   }
 
   public class InputMethod {
-    ctor protected InputMethod(@NonNull android.accessibilityservice.AccessibilityService);
+    ctor public InputMethod(@NonNull android.accessibilityservice.AccessibilityService);
     method @Nullable public final android.accessibilityservice.InputMethod.AccessibilityInputConnection getCurrentInputConnection();
     method @Nullable public final android.view.inputmethod.EditorInfo getCurrentInputEditorInfo();
     method public final boolean getCurrentInputStarted();
@@ -3900,6 +3901,7 @@
     method public Object getAnimatedValue(String);
     method public long getCurrentPlayTime();
     method public long getDuration();
+    method @FloatRange(from=0) public static float getDurationScale();
     method public static long getFrameDelay();
     method public int getRepeatCount();
     method public int getRepeatMode();
@@ -3911,6 +3913,7 @@
     method public static android.animation.ValueAnimator ofInt(int...);
     method public static android.animation.ValueAnimator ofObject(android.animation.TypeEvaluator, java.lang.Object...);
     method public static android.animation.ValueAnimator ofPropertyValuesHolder(android.animation.PropertyValuesHolder...);
+    method public static boolean registerDurationScaleChangeListener(@NonNull android.animation.ValueAnimator.DurationScaleChangeListener);
     method public void removeAllUpdateListeners();
     method public void removeUpdateListener(android.animation.ValueAnimator.AnimatorUpdateListener);
     method public void reverse();
@@ -3927,6 +3930,7 @@
     method public void setRepeatMode(int);
     method public void setStartDelay(long);
     method public void setValues(android.animation.PropertyValuesHolder...);
+    method public static boolean unregisterDurationScaleChangeListener(@NonNull android.animation.ValueAnimator.DurationScaleChangeListener);
     field public static final int INFINITE = -1; // 0xffffffff
     field public static final int RESTART = 1; // 0x1
     field public static final int REVERSE = 2; // 0x2
@@ -3936,6 +3940,10 @@
     method public void onAnimationUpdate(@NonNull android.animation.ValueAnimator);
   }
 
+  public static interface ValueAnimator.DurationScaleChangeListener {
+    method public void onChanged(@FloatRange(from=0) float);
+  }
+
 }
 
 package android.annotation {
@@ -4275,7 +4283,6 @@
     method public void setLocusContext(@Nullable android.content.LocusId, @Nullable android.os.Bundle);
     method public final void setMediaController(android.media.session.MediaController);
     method public void setPictureInPictureParams(@NonNull android.app.PictureInPictureParams);
-    method public void setPreferDockBigOverlays(boolean);
     method @Deprecated public final void setProgress(int);
     method @Deprecated public final void setProgressBarIndeterminate(boolean);
     method @Deprecated public final void setProgressBarIndeterminateVisibility(boolean);
@@ -4285,6 +4292,7 @@
     method public final void setResult(int);
     method public final void setResult(int, android.content.Intent);
     method @Deprecated public final void setSecondaryProgress(int);
+    method public void setShouldDockBigOverlays(boolean);
     method public void setShowWhenLocked(boolean);
     method public void setTaskDescription(android.app.ActivityManager.TaskDescription);
     method public void setTitle(CharSequence);
@@ -4295,6 +4303,7 @@
     method public void setVisible(boolean);
     method public final void setVolumeControlStream(int);
     method public void setVrModeEnabled(boolean, @NonNull android.content.ComponentName) throws android.content.pm.PackageManager.NameNotFoundException;
+    method public boolean shouldDockBigOverlays();
     method public boolean shouldShowRequestPermissionRationale(@NonNull String);
     method public boolean shouldUpRecreateTask(android.content.Intent);
     method public boolean showAssist(android.os.Bundle);
@@ -7437,7 +7446,7 @@
     method @Nullable public java.util.List<java.lang.String> getPermittedCrossProfileNotificationListeners(@NonNull android.content.ComponentName);
     method @Nullable public java.util.List<java.lang.String> getPermittedInputMethods(@NonNull android.content.ComponentName);
     method public int getPersonalAppsSuspendedReasons(@NonNull android.content.ComponentName);
-    method @NonNull public android.app.admin.PreferentialNetworkServiceConfig getPreferentialNetworkServiceConfig();
+    method @NonNull public java.util.List<android.app.admin.PreferentialNetworkServiceConfig> getPreferentialNetworkServiceConfigs();
     method public int getRequiredPasswordComplexity();
     method public long getRequiredStrongAuthTimeout(@Nullable android.content.ComponentName);
     method public boolean getScreenCaptureDisabled(@Nullable android.content.ComponentName);
@@ -7582,7 +7591,7 @@
     method public boolean setPermittedCrossProfileNotificationListeners(@NonNull android.content.ComponentName, @Nullable java.util.List<java.lang.String>);
     method public boolean setPermittedInputMethods(@NonNull android.content.ComponentName, java.util.List<java.lang.String>);
     method public void setPersonalAppsSuspended(@NonNull android.content.ComponentName, boolean);
-    method public void setPreferentialNetworkServiceConfig(@NonNull android.app.admin.PreferentialNetworkServiceConfig);
+    method public void setPreferentialNetworkServiceConfigs(@NonNull java.util.List<android.app.admin.PreferentialNetworkServiceConfig>);
     method public void setPreferentialNetworkServiceEnabled(boolean);
     method public void setProfileEnabled(@NonNull android.content.ComponentName);
     method public void setProfileName(@NonNull android.content.ComponentName, String);
@@ -11291,6 +11300,7 @@
     method @RequiresPermission(anyOf={android.Manifest.permission.INTERACT_ACROSS_PROFILES, "android.permission.INTERACT_ACROSS_USERS"}) public void startActivity(@NonNull android.content.Intent, @NonNull android.os.UserHandle, @Nullable android.app.Activity);
     method @RequiresPermission(anyOf={android.Manifest.permission.INTERACT_ACROSS_PROFILES, "android.permission.INTERACT_ACROSS_USERS"}) public void startActivity(@NonNull android.content.Intent, @NonNull android.os.UserHandle, @Nullable android.app.Activity, @Nullable android.os.Bundle);
     method public void startMainActivity(@NonNull android.content.ComponentName, @NonNull android.os.UserHandle);
+    method public void startMainActivity(@NonNull android.content.ComponentName, @NonNull android.os.UserHandle, @Nullable android.app.Activity, @Nullable android.os.Bundle);
     field public static final String ACTION_CAN_INTERACT_ACROSS_PROFILES_CHANGED = "android.content.pm.action.CAN_INTERACT_ACROSS_PROFILES_CHANGED";
   }
 
@@ -16398,12 +16408,8 @@
 package android.graphics.text {
 
   public final class LineBreakConfig {
-    ctor public LineBreakConfig();
     method public int getLineBreakStyle();
     method public int getLineBreakWordStyle();
-    method public void set(@NonNull android.graphics.text.LineBreakConfig);
-    method public void setLineBreakStyle(int);
-    method public void setLineBreakWordStyle(int);
     field public static final int LINE_BREAK_STYLE_LOOSE = 1; // 0x1
     field public static final int LINE_BREAK_STYLE_NONE = 0; // 0x0
     field public static final int LINE_BREAK_STYLE_NORMAL = 2; // 0x2
@@ -16412,6 +16418,13 @@
     field public static final int LINE_BREAK_WORD_STYLE_PHRASE = 1; // 0x1
   }
 
+  public static final class LineBreakConfig.Builder {
+    ctor public LineBreakConfig.Builder();
+    method @NonNull public android.graphics.text.LineBreakConfig build();
+    method @NonNull public android.graphics.text.LineBreakConfig.Builder setLineBreakStyle(int);
+    method @NonNull public android.graphics.text.LineBreakConfig.Builder setLineBreakWordStyle(int);
+  }
+
   public class LineBreaker {
     method @NonNull public android.graphics.text.LineBreaker.Result computeLineBreaks(@NonNull android.graphics.text.MeasuredText, @NonNull android.graphics.text.LineBreaker.ParagraphConstraints, @IntRange(from=0) int);
     field public static final int BREAK_STRATEGY_BALANCED = 2; // 0x2
@@ -17399,7 +17412,7 @@
     field @NonNull public static final android.hardware.camera2.CameraCharacteristics.Key<java.lang.Long> REQUEST_RECOMMENDED_TEN_BIT_DYNAMIC_RANGE_PROFILE;
     field @NonNull public static final android.hardware.camera2.CameraCharacteristics.Key<java.lang.Float> SCALER_AVAILABLE_MAX_DIGITAL_ZOOM;
     field @NonNull public static final android.hardware.camera2.CameraCharacteristics.Key<int[]> SCALER_AVAILABLE_ROTATE_AND_CROP_MODES;
-    field @NonNull public static final android.hardware.camera2.CameraCharacteristics.Key<int[]> SCALER_AVAILABLE_STREAM_USE_CASES;
+    field @NonNull public static final android.hardware.camera2.CameraCharacteristics.Key<long[]> SCALER_AVAILABLE_STREAM_USE_CASES;
     field @NonNull public static final android.hardware.camera2.CameraCharacteristics.Key<java.lang.Integer> SCALER_CROPPING_TYPE;
     field @NonNull public static final android.hardware.camera2.CameraCharacteristics.Key<android.util.Size> SCALER_DEFAULT_SECURE_IMAGE_SIZE;
     field @NonNull public static final android.hardware.camera2.CameraCharacteristics.Key<android.hardware.camera2.params.MandatoryStreamCombination[]> SCALER_MANDATORY_CONCURRENT_STREAM_COMBINATIONS;
@@ -18184,7 +18197,7 @@
     method public int get10BitFormat();
     method @NonNull public java.util.List<android.util.Size> getAvailableSizes();
     method public int getFormat();
-    method public int getStreamUseCase();
+    method public long getStreamUseCase();
     method public boolean is10BitCapable();
     method public boolean isInput();
     method public boolean isMaximumSize();
@@ -18242,7 +18255,7 @@
     method public long getDynamicRangeProfile();
     method public int getMaxSharedSurfaceCount();
     method public int getMirrorMode();
-    method public int getStreamUseCase();
+    method public long getStreamUseCase();
     method @Nullable public android.view.Surface getSurface();
     method public int getSurfaceGroupId();
     method @NonNull public java.util.List<android.view.Surface> getSurfaces();
@@ -18252,7 +18265,7 @@
     method public void setDynamicRangeProfile(long);
     method public void setMirrorMode(int);
     method public void setPhysicalCameraId(@Nullable String);
-    method public void setStreamUseCase(int);
+    method public void setStreamUseCase(long);
     method public void setTimestampBase(int);
     method public void writeToParcel(android.os.Parcel, int);
     field @NonNull public static final android.os.Parcelable.Creator<android.hardware.camera2.params.OutputConfiguration> CREATOR;
@@ -18818,6 +18831,7 @@
     method public void onStartInput(android.view.inputmethod.EditorInfo, boolean);
     method public void onStartInputView(android.view.inputmethod.EditorInfo, boolean);
     method public boolean onStartStylusHandwriting();
+    method public void onStylusHandwritingMotionEvent(@NonNull android.view.MotionEvent);
     method public void onUnbindInput();
     method @Deprecated public void onUpdateCursor(android.graphics.Rect);
     method public void onUpdateCursorAnchorInfo(android.view.inputmethod.CursorAnchorInfo);
@@ -26063,11 +26077,9 @@
   public static final class AppLinkInfo.Builder {
     ctor public AppLinkInfo.Builder(@NonNull String, @NonNull String);
     method @NonNull public android.media.tv.interactive.AppLinkInfo build();
-    method @NonNull public android.media.tv.interactive.AppLinkInfo.Builder setClassName(@NonNull String);
-    method @NonNull public android.media.tv.interactive.AppLinkInfo.Builder setPackageName(@NonNull String);
-    method @NonNull public android.media.tv.interactive.AppLinkInfo.Builder setUriHost(@Nullable String);
-    method @NonNull public android.media.tv.interactive.AppLinkInfo.Builder setUriPrefix(@Nullable String);
-    method @NonNull public android.media.tv.interactive.AppLinkInfo.Builder setUriScheme(@Nullable String);
+    method @NonNull public android.media.tv.interactive.AppLinkInfo.Builder setUriHost(@NonNull String);
+    method @NonNull public android.media.tv.interactive.AppLinkInfo.Builder setUriPrefix(@NonNull String);
+    method @NonNull public android.media.tv.interactive.AppLinkInfo.Builder setUriScheme(@NonNull String);
   }
 
   public final class TvInteractiveAppInfo implements android.os.Parcelable {
@@ -26087,7 +26099,7 @@
     method @NonNull public java.util.List<android.media.tv.interactive.TvInteractiveAppInfo> getTvInteractiveAppServiceList();
     method public void prepare(@NonNull String, int);
     method public void registerAppLinkInfo(@NonNull String, @NonNull android.media.tv.interactive.AppLinkInfo);
-    method public void registerCallback(@NonNull android.media.tv.interactive.TvInteractiveAppManager.TvInteractiveAppCallback, @NonNull java.util.concurrent.Executor);
+    method public void registerCallback(@NonNull java.util.concurrent.Executor, @NonNull android.media.tv.interactive.TvInteractiveAppManager.TvInteractiveAppCallback);
     method public void sendAppLinkCommand(@NonNull String, @NonNull android.os.Bundle);
     method public void unregisterAppLinkInfo(@NonNull String, @NonNull android.media.tv.interactive.AppLinkInfo);
     method public void unregisterCallback(@NonNull android.media.tv.interactive.TvInteractiveAppManager.TvInteractiveAppCallback);
@@ -26157,10 +26169,10 @@
 
   public abstract static class TvInteractiveAppService.Session implements android.view.KeyEvent.Callback {
     ctor public TvInteractiveAppService.Session(@NonNull android.content.Context);
-    method public void layoutSurface(int, int, int, int);
-    method public final void notifyBiInteractiveAppCreated(@NonNull android.net.Uri, @Nullable String);
-    method public void notifySessionStateChanged(int, int);
-    method public final void notifyTeletextAppStateChanged(int);
+    method @CallSuper public void layoutSurface(int, int, int, int);
+    method @CallSuper public final void notifyBiInteractiveAppCreated(@NonNull android.net.Uri, @Nullable String);
+    method @CallSuper public void notifySessionStateChanged(int, int);
+    method @CallSuper public final void notifyTeletextAppStateChanged(int);
     method public void onAdResponse(@NonNull android.media.tv.AdResponse);
     method public void onBroadcastInfoResponse(@NonNull android.media.tv.BroadcastInfoResponse);
     method public void onContentAllowed();
@@ -26194,17 +26206,17 @@
     method public void onTuned(@NonNull android.net.Uri);
     method public void onVideoAvailable();
     method public void onVideoUnavailable(int);
-    method public void removeBroadcastInfo(int);
-    method public void requestAd(@NonNull android.media.tv.AdRequest);
-    method public void requestBroadcastInfo(@NonNull android.media.tv.BroadcastInfoRequest);
-    method public void requestCurrentChannelLcn();
-    method public void requestCurrentChannelUri();
-    method public void requestCurrentTvInputId();
-    method public void requestStreamVolume();
-    method public void requestTrackInfoList();
-    method public void sendPlaybackCommandRequest(@NonNull String, @Nullable android.os.Bundle);
-    method public void setMediaViewEnabled(boolean);
-    method public void setVideoBounds(@NonNull android.graphics.Rect);
+    method @CallSuper public void removeBroadcastInfo(int);
+    method @CallSuper public void requestAd(@NonNull android.media.tv.AdRequest);
+    method @CallSuper public void requestBroadcastInfo(@NonNull android.media.tv.BroadcastInfoRequest);
+    method @CallSuper public void requestCurrentChannelLcn();
+    method @CallSuper public void requestCurrentChannelUri();
+    method @CallSuper public void requestCurrentTvInputId();
+    method @CallSuper public void requestStreamVolume();
+    method @CallSuper public void requestTrackInfoList();
+    method @CallSuper public void sendPlaybackCommandRequest(@NonNull String, @Nullable android.os.Bundle);
+    method @CallSuper public void setMediaViewEnabled(boolean);
+    method @CallSuper public void setVideoBounds(@NonNull android.graphics.Rect);
   }
 
   public class TvInteractiveAppView extends android.view.ViewGroup {
@@ -26507,14 +26519,6 @@
     method public int getUid();
   }
 
-  public final class EthernetNetworkSpecifier extends android.net.NetworkSpecifier implements android.os.Parcelable {
-    ctor public EthernetNetworkSpecifier(@NonNull String);
-    method public int describeContents();
-    method @Nullable public String getInterfaceName();
-    method public void writeToParcel(@NonNull android.os.Parcel, int);
-    field @NonNull public static final android.os.Parcelable.Creator<android.net.EthernetNetworkSpecifier> CREATOR;
-  }
-
   public final class Ikev2VpnProfile extends android.net.PlatformVpnProfile {
     method @NonNull public java.util.List<java.lang.String> getAllowedAlgorithms();
     method public int getMaxMtu();
@@ -45062,7 +45066,7 @@
   public static final class PrecomputedText.Params {
     method public int getBreakStrategy();
     method public int getHyphenationFrequency();
-    method @Nullable public android.graphics.text.LineBreakConfig getLineBreakConfig();
+    method @NonNull public android.graphics.text.LineBreakConfig getLineBreakConfig();
     method @NonNull public android.text.TextDirectionHeuristic getTextDirection();
     method @NonNull public android.text.TextPaint getTextPaint();
   }
@@ -57352,7 +57356,8 @@
     method public final android.text.Layout getLayout();
     method public float getLetterSpacing();
     method public int getLineBounds(int, android.graphics.Rect);
-    method @NonNull public android.graphics.text.LineBreakConfig getLineBreakConfig();
+    method public int getLineBreakStyle();
+    method public int getLineBreakWordStyle();
     method public int getLineCount();
     method public int getLineHeight();
     method public float getLineSpacingExtra();
@@ -57480,7 +57485,8 @@
     method public void setKeyListener(android.text.method.KeyListener);
     method public void setLastBaselineToBottomHeight(@IntRange(from=0) @Px int);
     method public void setLetterSpacing(float);
-    method public void setLineBreakConfig(@NonNull android.graphics.text.LineBreakConfig);
+    method public void setLineBreakStyle(int);
+    method public void setLineBreakWordStyle(int);
     method public void setLineHeight(@IntRange(from=0) @Px int);
     method public void setLineSpacing(float, float);
     method public void setLines(int);
diff --git a/core/api/module-lib-current.txt b/core/api/module-lib-current.txt
index 241e5c8..3d5232b 100644
--- a/core/api/module-lib-current.txt
+++ b/core/api/module-lib-current.txt
@@ -102,6 +102,8 @@
   public abstract class PackageManager {
     method @NonNull public String getPermissionControllerPackageName();
     method @NonNull public String getSdkSandboxPackageName();
+    method @RequiresPermission("android.permission.MAKE_UID_VISIBLE") public void makeUidVisible(int, int);
+    field public static final String EXTRA_VERIFICATION_ROOT_HASH = "android.content.pm.extra.VERIFICATION_ROOT_HASH";
     field public static final int MATCH_STATIC_SHARED_AND_SDK_LIBRARIES = 67108864; // 0x4000000
   }
 
@@ -231,22 +233,6 @@
 
 package android.net {
 
-  public class EthernetManager {
-    method @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) public void addInterfaceStateListener(@NonNull java.util.concurrent.Executor, @NonNull android.net.EthernetManager.InterfaceStateListener);
-    method public void removeInterfaceStateListener(@NonNull android.net.EthernetManager.InterfaceStateListener);
-    method public void setIncludeTestInterfaces(boolean);
-    field public static final int ROLE_CLIENT = 1; // 0x1
-    field public static final int ROLE_NONE = 0; // 0x0
-    field public static final int ROLE_SERVER = 2; // 0x2
-    field public static final int STATE_ABSENT = 0; // 0x0
-    field public static final int STATE_LINK_DOWN = 1; // 0x1
-    field public static final int STATE_LINK_UP = 2; // 0x2
-  }
-
-  public static interface EthernetManager.InterfaceStateListener {
-    method public void onInterfaceStateChanged(@NonNull String, int, int, @Nullable android.net.IpConfiguration);
-  }
-
   public class LocalSocket implements java.io.Closeable {
     ctor public LocalSocket(@NonNull java.io.FileDescriptor);
   }
@@ -454,6 +440,14 @@
 
 }
 
+package android.telecom {
+
+  public abstract class ConnectionService extends android.app.Service {
+    method @Nullable @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE) public android.telecom.Connection onCreateUnknownConnection(@NonNull android.telecom.PhoneAccountHandle, @NonNull android.telecom.ConnectionRequest);
+  }
+
+}
+
 package android.telephony {
 
   public abstract class CellSignalStrength {
diff --git a/core/api/system-current.txt b/core/api/system-current.txt
index ac7bfab..5da1f39 100644
--- a/core/api/system-current.txt
+++ b/core/api/system-current.txt
@@ -34,6 +34,7 @@
     field public static final String ALLOCATE_AGGRESSIVE = "android.permission.ALLOCATE_AGGRESSIVE";
     field public static final String ALLOW_ANY_CODEC_FOR_PLAYBACK = "android.permission.ALLOW_ANY_CODEC_FOR_PLAYBACK";
     field public static final String ALLOW_PLACE_IN_MULTI_PANE_SETTINGS = "android.permission.ALLOW_PLACE_IN_MULTI_PANE_SETTINGS";
+    field public static final String ALLOW_SLIPPERY_TOUCHES = "android.permission.ALLOW_SLIPPERY_TOUCHES";
     field public static final String AMBIENT_WALLPAPER = "android.permission.AMBIENT_WALLPAPER";
     field public static final String APPROVE_INCIDENT_REPORTS = "android.permission.APPROVE_INCIDENT_REPORTS";
     field public static final String ASSOCIATE_COMPANION_DEVICES = "android.permission.ASSOCIATE_COMPANION_DEVICES";
@@ -243,6 +244,7 @@
     field public static final String READ_APP_SPECIFIC_LOCALES = "android.permission.READ_APP_SPECIFIC_LOCALES";
     field public static final String READ_CARRIER_APP_INFO = "android.permission.READ_CARRIER_APP_INFO";
     field public static final String READ_CELL_BROADCASTS = "android.permission.READ_CELL_BROADCASTS";
+    field public static final String READ_CLIPBOARD_IN_BACKGROUND = "android.permission.READ_CLIPBOARD_IN_BACKGROUND";
     field public static final String READ_CONTENT_RATING_SYSTEMS = "android.permission.READ_CONTENT_RATING_SYSTEMS";
     field public static final String READ_DEVICE_CONFIG = "android.permission.READ_DEVICE_CONFIG";
     field public static final String READ_DREAM_STATE = "android.permission.READ_DREAM_STATE";
@@ -469,7 +471,6 @@
     method public boolean convertToTranslucent(android.app.Activity.TranslucentConversionListener, android.app.ActivityOptions);
     method @Deprecated public boolean isBackgroundVisibleBehind();
     method @Deprecated public void onBackgroundVisibleBehindChanged(boolean);
-    method @RequiresPermission(anyOf={android.Manifest.permission.INTERACT_ACROSS_USERS, android.Manifest.permission.INTERACT_ACROSS_USERS_FULL}) public void startActivityAsUser(@NonNull android.content.Intent, @Nullable android.os.Bundle, @NonNull android.os.UserHandle);
     method @RequiresPermission(anyOf={android.Manifest.permission.INTERACT_ACROSS_USERS, android.Manifest.permission.INTERACT_ACROSS_USERS_FULL}) public void startActivityForResultAsUser(@NonNull android.content.Intent, int, @NonNull android.os.UserHandle);
     method @RequiresPermission(anyOf={android.Manifest.permission.INTERACT_ACROSS_USERS, android.Manifest.permission.INTERACT_ACROSS_USERS_FULL}) public void startActivityForResultAsUser(@NonNull android.content.Intent, int, @Nullable android.os.Bundle, @NonNull android.os.UserHandle);
     method @RequiresPermission(anyOf={android.Manifest.permission.INTERACT_ACROSS_USERS, android.Manifest.permission.INTERACT_ACROSS_USERS_FULL}) public void startActivityForResultAsUser(@NonNull android.content.Intent, @NonNull String, int, @Nullable android.os.Bundle, @NonNull android.os.UserHandle);
@@ -1128,7 +1129,7 @@
     field @RequiresPermission(android.Manifest.permission.LAUNCH_DEVICE_MANAGER_SETUP) public static final String ACTION_ROLE_HOLDER_PROVISION_MANAGED_PROFILE = "android.app.action.ROLE_HOLDER_PROVISION_MANAGED_PROFILE";
     field public static final String ACTION_SET_PROFILE_OWNER = "android.app.action.SET_PROFILE_OWNER";
     field @Deprecated public static final String ACTION_STATE_USER_SETUP_COMPLETE = "android.app.action.STATE_USER_SETUP_COMPLETE";
-    field @RequiresPermission(android.Manifest.permission.LAUNCH_DEVICE_MANAGER_SETUP) public static final String ACTION_UPDATE_DEVICE_MANAGEMENT_ROLE_HOLDER = "android.app.action.UPDATE_DEVICE_MANAGEMENT_ROLE_HOLDER";
+    field @RequiresPermission(android.Manifest.permission.LAUNCH_DEVICE_MANAGER_SETUP) public static final String ACTION_UPDATE_DEVICE_POLICY_MANAGEMENT_ROLE_HOLDER = "android.app.action.UPDATE_DEVICE_POLICY_MANAGEMENT_ROLE_HOLDER";
     field public static final String EXTRA_FORCE_UPDATE_ROLE_HOLDER = "android.app.extra.FORCE_UPDATE_ROLE_HOLDER";
     field public static final String EXTRA_LOST_MODE_LOCATION = "android.app.extra.LOST_MODE_LOCATION";
     field public static final String EXTRA_PROFILE_OWNER_NAME = "android.app.extra.PROFILE_OWNER_NAME";
@@ -1161,8 +1162,8 @@
     field public static final String REQUIRED_APP_MANAGED_PROFILE = "android.app.REQUIRED_APP_MANAGED_PROFILE";
     field public static final String REQUIRED_APP_MANAGED_USER = "android.app.REQUIRED_APP_MANAGED_USER";
     field public static final int RESULT_DEVICE_OWNER_SET = 123; // 0x7b
-    field public static final int RESULT_UPDATE_DEVICE_MANAGEMENT_ROLE_HOLDER_RECOVERABLE_ERROR = 1; // 0x1
-    field public static final int RESULT_UPDATE_DEVICE_MANAGEMENT_ROLE_HOLDER_UNRECOVERABLE_ERROR = 2; // 0x2
+    field public static final int RESULT_UPDATE_DEVICE_POLICY_MANAGEMENT_ROLE_HOLDER_RECOVERABLE_ERROR = 1; // 0x1
+    field public static final int RESULT_UPDATE_DEVICE_POLICY_MANAGEMENT_ROLE_HOLDER_UNRECOVERABLE_ERROR = 2; // 0x2
     field public static final int RESULT_UPDATE_ROLE_HOLDER = 2; // 0x2
     field public static final int RESULT_WORK_PROFILE_CREATED = 122; // 0x7a
     field public static final int STATE_USER_PROFILE_COMPLETE = 4; // 0x4
@@ -1650,13 +1651,13 @@
 
   public final class SearchRequest implements android.os.Parcelable {
     method public int describeContents();
+    method @NonNull public String getCallerPackageName();
     method public float getMaxLatencyMillis();
     method @NonNull public String getQuery();
     method @NonNull public String getRequestId();
     method public int getResultNumber();
     method public int getResultOffset();
     method @NonNull public android.os.Bundle getSearchConstraints();
-    method @NonNull public String getSource();
     method public void writeToParcel(@NonNull android.os.Parcel, int);
     field public static final String CONSTRAINT_IS_PRESUBMIT_SUGGESTION = "android.app.cloudsearch.IS_PRESUBMIT_SUGGESTION";
     field public static final String CONSTRAINT_SEARCH_PROVIDER_FILTER = "android.app.cloudsearch.SEARCH_PROVIDER_FILTER";
@@ -1701,8 +1702,10 @@
     method @NonNull public String getTitle();
     method public void writeToParcel(@NonNull android.os.Parcel, int);
     field @NonNull public static final android.os.Parcelable.Creator<android.app.cloudsearch.SearchResult> CREATOR;
+    field public static final String EXTRAINFO_ACTION_APP_CARD = "android.app.cloudsearch.ACTION_APP_CARD";
     field public static final String EXTRAINFO_ACTION_BUTTON_IMAGE_PREREGISTERING = "android.app.cloudsearch.ACTION_BUTTON_IMAGE";
     field public static final String EXTRAINFO_ACTION_BUTTON_TEXT_PREREGISTERING = "android.app.cloudsearch.ACTION_BUTTON_TEXT";
+    field public static final String EXTRAINFO_ACTION_INSTALL_BUTTON = "android.app.cloudsearch.ACTION_INSTALL_BUTTON";
     field public static final String EXTRAINFO_APP_BADGES = "android.app.cloudsearch.APP_BADGES";
     field public static final String EXTRAINFO_APP_CONTAINS_ADS_DISCLAIMER = "android.app.cloudsearch.APP_CONTAINS_ADS_DISCLAIMER";
     field public static final String EXTRAINFO_APP_CONTAINS_IAP_DISCLAIMER = "android.app.cloudsearch.APP_CONTAINS_IAP_DISCLAIMER";
@@ -1710,6 +1713,8 @@
     field public static final String EXTRAINFO_APP_DOMAIN_URL = "android.app.cloudsearch.APP_DOMAIN_URL";
     field public static final String EXTRAINFO_APP_IARC = "android.app.cloudsearch.APP_IARC";
     field public static final String EXTRAINFO_APP_ICON = "android.app.cloudsearch.APP_ICON";
+    field public static final String EXTRAINFO_APP_INSTALL_COUNT = "android.app.cloudsearch.APP_INSTALL_COUNT";
+    field public static final String EXTRAINFO_APP_PACKAGE_NAME = "android.app.cloudsearch.APP_PACKAGE_NAME";
     field public static final String EXTRAINFO_APP_REVIEW_COUNT = "android.app.cloudsearch.APP_REVIEW_COUNT";
     field public static final String EXTRAINFO_APP_SIZE_BYTES = "android.app.cloudsearch.APP_SIZE_BYTES";
     field public static final String EXTRAINFO_APP_STAR_RATING = "android.app.cloudsearch.APP_STAR_RATING";
@@ -2948,6 +2953,7 @@
     method public void sendBroadcastMultiplePermissions(@NonNull android.content.Intent, @NonNull String[], @Nullable android.app.BroadcastOptions);
     method public abstract void sendOrderedBroadcast(@NonNull android.content.Intent, @Nullable String, @Nullable android.os.Bundle, @Nullable android.content.BroadcastReceiver, @Nullable android.os.Handler, int, @Nullable String, @Nullable android.os.Bundle);
     method @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) public void startActivityAsUser(@NonNull @RequiresPermission android.content.Intent, @NonNull android.os.UserHandle);
+    method @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) public void startActivityAsUser(@NonNull @RequiresPermission android.content.Intent, @Nullable android.os.Bundle, @NonNull android.os.UserHandle);
     field public static final String AMBIENT_CONTEXT_SERVICE = "ambient_context";
     field public static final String APP_HIBERNATION_SERVICE = "app_hibernation";
     field public static final String APP_INTEGRITY_SERVICE = "app_integrity";
@@ -3187,6 +3193,7 @@
   }
 
   public class CrossProfileApps {
+    method @RequiresPermission(anyOf={android.Manifest.permission.INTERACT_ACROSS_PROFILES, android.Manifest.permission.START_CROSS_PROFILE_ACTIVITIES}) public void startActivity(@NonNull android.content.ComponentName, @NonNull android.os.UserHandle, @Nullable android.app.Activity, @Nullable android.os.Bundle);
     method @RequiresPermission(anyOf={android.Manifest.permission.INTERACT_ACROSS_PROFILES, android.Manifest.permission.START_CROSS_PROFILE_ACTIVITIES}) public void startActivity(@NonNull android.content.ComponentName, @NonNull android.os.UserHandle);
   }
 
@@ -6946,7 +6953,7 @@
   }
 
   public class Lnb implements java.lang.AutoCloseable {
-    method public void addCallback(@NonNull android.media.tv.tuner.LnbCallback, @NonNull java.util.concurrent.Executor);
+    method public void addCallback(@NonNull java.util.concurrent.Executor, @NonNull android.media.tv.tuner.LnbCallback);
     method public void close();
     method public boolean removeCallback(@NonNull android.media.tv.tuner.LnbCallback);
     method public int sendDiseqcMessage(@NonNull byte[]);
@@ -8521,45 +8528,6 @@
 
 package android.net {
 
-  public class EthernetManager {
-    method @RequiresPermission(anyOf={android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, android.Manifest.permission.NETWORK_STACK, android.Manifest.permission.MANAGE_ETHERNET_NETWORKS}) public void connectNetwork(@NonNull String, @Nullable java.util.concurrent.Executor, @Nullable java.util.function.BiConsumer<android.net.Network,android.net.EthernetNetworkManagementException>);
-    method @RequiresPermission(anyOf={android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, android.Manifest.permission.NETWORK_STACK, android.Manifest.permission.MANAGE_ETHERNET_NETWORKS}) public void disconnectNetwork(@NonNull String, @Nullable java.util.concurrent.Executor, @Nullable java.util.function.BiConsumer<android.net.Network,android.net.EthernetNetworkManagementException>);
-    method @NonNull @RequiresPermission(anyOf={android.Manifest.permission.NETWORK_STACK, android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK}) public android.net.EthernetManager.TetheredInterfaceRequest requestTetheredInterface(@NonNull java.util.concurrent.Executor, @NonNull android.net.EthernetManager.TetheredInterfaceCallback);
-    method @RequiresPermission(anyOf={android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, android.Manifest.permission.NETWORK_STACK, android.Manifest.permission.MANAGE_ETHERNET_NETWORKS}) public void updateConfiguration(@NonNull String, @NonNull android.net.EthernetNetworkUpdateRequest, @Nullable java.util.concurrent.Executor, @Nullable java.util.function.BiConsumer<android.net.Network,android.net.EthernetNetworkManagementException>);
-  }
-
-  public static interface EthernetManager.TetheredInterfaceCallback {
-    method public void onAvailable(@NonNull String);
-    method public void onUnavailable();
-  }
-
-  public static class EthernetManager.TetheredInterfaceRequest {
-    method public void release();
-  }
-
-  public final class EthernetNetworkManagementException extends java.lang.RuntimeException implements android.os.Parcelable {
-    ctor public EthernetNetworkManagementException(@NonNull String);
-    method public int describeContents();
-    method public void writeToParcel(@NonNull android.os.Parcel, int);
-    field @NonNull public static final android.os.Parcelable.Creator<android.net.EthernetNetworkManagementException> CREATOR;
-  }
-
-  public final class EthernetNetworkUpdateRequest implements android.os.Parcelable {
-    method public int describeContents();
-    method @NonNull public android.net.IpConfiguration getIpConfiguration();
-    method @NonNull public android.net.NetworkCapabilities getNetworkCapabilities();
-    method public void writeToParcel(@NonNull android.os.Parcel, int);
-    field @NonNull public static final android.os.Parcelable.Creator<android.net.EthernetNetworkUpdateRequest> CREATOR;
-  }
-
-  public static final class EthernetNetworkUpdateRequest.Builder {
-    ctor public EthernetNetworkUpdateRequest.Builder();
-    ctor public EthernetNetworkUpdateRequest.Builder(@NonNull android.net.EthernetNetworkUpdateRequest);
-    method @NonNull public android.net.EthernetNetworkUpdateRequest build();
-    method @NonNull public android.net.EthernetNetworkUpdateRequest.Builder setIpConfiguration(@NonNull android.net.IpConfiguration);
-    method @NonNull public android.net.EthernetNetworkUpdateRequest.Builder setNetworkCapabilities(@NonNull android.net.NetworkCapabilities);
-  }
-
   public final class MatchAllNetworkSpecifier extends android.net.NetworkSpecifier implements android.os.Parcelable {
     ctor public MatchAllNetworkSpecifier();
     method public int describeContents();
@@ -9030,6 +8998,7 @@
     method public void enableVerboseLogging(boolean);
     method @NonNull public int[] getChannelsMhzForBand(int);
     method @Nullable public android.net.wifi.nl80211.DeviceWiphyCapabilities getDeviceWiphyCapabilities(@NonNull String);
+    method public int getMaxSsidsPerScan(@NonNull String);
     method @NonNull public java.util.List<android.net.wifi.nl80211.NativeScanResult> getScanResults(@NonNull String, int);
     method @Nullable public android.net.wifi.nl80211.WifiNl80211Manager.TxPacketCounters getTxPacketCounters(@NonNull String);
     method public void notifyCountryCodeChanged(@Nullable String);
@@ -9819,11 +9788,12 @@
     method @RequiresPermission(anyOf={android.Manifest.permission.MANAGE_USERS, android.Manifest.permission.INTERACT_ACROSS_USERS}, conditional=true) public boolean hasUserRestrictionForUser(@NonNull String, @NonNull android.os.UserHandle);
     method @RequiresPermission(anyOf={android.Manifest.permission.MANAGE_USERS, android.Manifest.permission.CREATE_USERS, android.Manifest.permission.QUERY_USERS}) public boolean isAdminUser();
     method public boolean isCloneProfile();
-    method public boolean isCredentialSharedWithParent();
+    method public boolean isCredentialSharableWithParent();
     method @RequiresPermission(anyOf={android.Manifest.permission.MANAGE_USERS, android.Manifest.permission.CREATE_USERS, android.Manifest.permission.QUERY_USERS}) public boolean isGuestUser();
     method @RequiresPermission(anyOf={android.Manifest.permission.MANAGE_USERS, android.Manifest.permission.QUERY_USERS, android.Manifest.permission.INTERACT_ACROSS_USERS}, conditional=true) public boolean isManagedProfile(int);
     method @RequiresPermission(anyOf={android.Manifest.permission.MANAGE_USERS, android.Manifest.permission.INTERACT_ACROSS_USERS}, conditional=true) public boolean isMediaSharedWithParent();
     method @RequiresPermission(anyOf={android.Manifest.permission.MANAGE_USERS, android.Manifest.permission.CREATE_USERS, android.Manifest.permission.QUERY_USERS}) public boolean isPrimaryUser();
+    method public static boolean isRemoveResultSuccessful(int);
     method public boolean isRestrictedProfile();
     method @RequiresPermission(anyOf={android.Manifest.permission.MANAGE_USERS, android.Manifest.permission.CREATE_USERS}, conditional=true) public boolean isRestrictedProfile(@NonNull android.os.UserHandle);
     method @RequiresPermission(anyOf={android.Manifest.permission.MANAGE_USERS, android.Manifest.permission.QUERY_USERS}) public boolean isSameProfileGroup(@NonNull android.os.UserHandle, @NonNull android.os.UserHandle);
@@ -9841,7 +9811,10 @@
     field public static final String DISALLOW_RUN_IN_BACKGROUND = "no_run_in_background";
     field public static final int REMOVE_RESULT_ALREADY_BEING_REMOVED = 2; // 0x2
     field public static final int REMOVE_RESULT_DEFERRED = 1; // 0x1
-    field public static final int REMOVE_RESULT_ERROR = 3; // 0x3
+    field public static final int REMOVE_RESULT_ERROR_SYSTEM_USER = -4; // 0xfffffffc
+    field public static final int REMOVE_RESULT_ERROR_UNKNOWN = -1; // 0xffffffff
+    field public static final int REMOVE_RESULT_ERROR_USER_NOT_FOUND = -3; // 0xfffffffd
+    field public static final int REMOVE_RESULT_ERROR_USER_RESTRICTION = -2; // 0xfffffffe
     field public static final int REMOVE_RESULT_REMOVED = 0; // 0x0
     field public static final int RESTRICTION_NOT_SET = 0; // 0x0
     field public static final int RESTRICTION_SOURCE_DEVICE_OWNER = 2; // 0x2
@@ -10331,6 +10304,7 @@
     field public static final String NAMESPACE_MEDIA_NATIVE = "media_native";
     field public static final String NAMESPACE_NETD_NATIVE = "netd_native";
     field public static final String NAMESPACE_NNAPI_NATIVE = "nnapi_native";
+    field public static final String NAMESPACE_ON_DEVICE_PERSONALIZATION = "on_device_personalization";
     field public static final String NAMESPACE_OTA = "ota";
     field public static final String NAMESPACE_PACKAGE_MANAGER_SERVICE = "package_manager_service";
     field public static final String NAMESPACE_PERMISSIONS = "permissions";
@@ -10580,7 +10554,6 @@
     field public static final String AUTO_REVOKE_DISABLED = "auto_revoke_disabled";
     field public static final String COMPLETED_CATEGORY_PREFIX = "suggested.completed_category.";
     field public static final String DOZE_ALWAYS_ON = "doze_always_on";
-    field public static final String FAST_PAIR_SCAN_ENABLED = "fast_pair_scan_enabled";
     field public static final String HUSH_GESTURE_USED = "hush_gesture_used";
     field public static final String INSTANT_APPS_ENABLED = "instant_apps_enabled";
     field public static final String LAST_SETUP_SHOWN = "last_setup_shown";
@@ -12217,7 +12190,6 @@
 
   public abstract class ConnectionService extends android.app.Service {
     method public final void addExistingConnection(@NonNull android.telecom.PhoneAccountHandle, @NonNull android.telecom.Connection, @NonNull android.telecom.Conference);
-    method @Nullable @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE) public android.telecom.Connection onCreateUnknownConnection(@NonNull android.telecom.PhoneAccountHandle, @NonNull android.telecom.ConnectionRequest);
   }
 
   public abstract class InCallService extends android.app.Service {
@@ -13375,7 +13347,7 @@
   }
 
   public class TelephonyManager {
-    method @RequiresPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE) public void addCarrierPrivilegesListener(int, @NonNull java.util.concurrent.Executor, @NonNull android.telephony.TelephonyManager.CarrierPrivilegesListener);
+    method @Deprecated @RequiresPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE) public void addCarrierPrivilegesListener(int, @NonNull java.util.concurrent.Executor, @NonNull android.telephony.TelephonyManager.CarrierPrivilegesListener);
     method @RequiresPermission(anyOf={android.Manifest.permission.MODIFY_PHONE_STATE, android.Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION}) @WorkerThread public void bootstrapAuthenticationRequest(int, @NonNull android.net.Uri, @NonNull android.telephony.gba.UaSecurityProtocolIdentifier, boolean, @NonNull java.util.concurrent.Executor, @NonNull android.telephony.TelephonyManager.BootstrapAuthenticationCallback);
     method @Deprecated @RequiresPermission(android.Manifest.permission.CALL_PHONE) public void call(String, String);
     method @NonNull @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE) public android.telephony.PinResult changeIccLockPin(@NonNull String, @NonNull String);
@@ -13475,7 +13447,8 @@
     method @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE) public void notifyOtaEmergencyNumberDbInstalled();
     method @RequiresPermission(android.Manifest.permission.REBOOT) public int prepareForUnattendedReboot();
     method @Deprecated @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE) public boolean rebootRadio();
-    method @RequiresPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE) public void removeCarrierPrivilegesListener(@NonNull android.telephony.TelephonyManager.CarrierPrivilegesListener);
+    method @RequiresPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE) public void registerCarrierPrivilegesCallback(int, @NonNull java.util.concurrent.Executor, @NonNull android.telephony.TelephonyManager.CarrierPrivilegesCallback);
+    method @Deprecated @RequiresPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE) public void removeCarrierPrivilegesListener(@NonNull android.telephony.TelephonyManager.CarrierPrivilegesListener);
     method @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE) public void reportDefaultNetworkStatus(boolean);
     method @RequiresPermission(allOf={android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.MODIFY_PHONE_STATE}) public void requestCellInfoUpdate(@NonNull android.os.WorkSource, @NonNull java.util.concurrent.Executor, @NonNull android.telephony.TelephonyManager.CellInfoCallback);
     method @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE) public void requestModemActivityInfo(@NonNull java.util.concurrent.Executor, @NonNull android.os.OutcomeReceiver<android.telephony.ModemActivityInfo,android.telephony.TelephonyManager.ModemActivityInfoException>);
@@ -13525,6 +13498,7 @@
     method @Deprecated @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE) public int[] supplyPukReportResult(String, String);
     method @Deprecated @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE) public boolean switchSlots(int[]);
     method @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE) public void toggleRadioOnOff();
+    method @RequiresPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE) public void unregisterCarrierPrivilegesCallback(@NonNull android.telephony.TelephonyManager.CarrierPrivilegesCallback);
     method @RequiresPermission(android.Manifest.permission.READ_ACTIVE_EMERGENCY_SESSION) public void updateOtaEmergencyNumberDbFilePath(@NonNull android.os.ParcelFileDescriptor);
     method public void updateServiceLocation();
     field @RequiresPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE) public static final String ACTION_ANOMALY_REPORTED = "android.telephony.action.ANOMALY_REPORTED";
@@ -13630,8 +13604,13 @@
     field public static final int RESULT_SUCCESS = 0; // 0x0
   }
 
-  public static interface TelephonyManager.CarrierPrivilegesListener {
-    method public void onCarrierPrivilegesChanged(@NonNull java.util.List<java.lang.String>, @NonNull int[]);
+  public static interface TelephonyManager.CarrierPrivilegesCallback {
+    method public void onCarrierPrivilegesChanged(@NonNull java.util.Set<java.lang.String>, @NonNull java.util.Set<java.lang.Integer>);
+    method public default void onCarrierServiceChanged(@Nullable String, int);
+  }
+
+  @Deprecated public static interface TelephonyManager.CarrierPrivilegesListener {
+    method @Deprecated public void onCarrierPrivilegesChanged(@NonNull java.util.List<java.lang.String>, @NonNull int[]);
   }
 
   public static class TelephonyManager.ModemActivityInfoException extends java.lang.Exception {
diff --git a/core/api/test-current.txt b/core/api/test-current.txt
index a22c4bc..db95a1f 100644
--- a/core/api/test-current.txt
+++ b/core/api/test-current.txt
@@ -19,6 +19,7 @@
     field public static final String FORCE_STOP_PACKAGES = "android.permission.FORCE_STOP_PACKAGES";
     field public static final String INSTALL_TEST_ONLY_PACKAGE = "android.permission.INSTALL_TEST_ONLY_PACKAGE";
     field public static final String KEEP_UNINSTALLED_PACKAGES = "android.permission.KEEP_UNINSTALLED_PACKAGES";
+    field public static final String MAKE_UID_VISIBLE = "android.permission.MAKE_UID_VISIBLE";
     field @Deprecated public static final String MANAGE_ACTIVITY_STACKS = "android.permission.MANAGE_ACTIVITY_STACKS";
     field public static final String MANAGE_ACTIVITY_TASKS = "android.permission.MANAGE_ACTIVITY_TASKS";
     field public static final String MANAGE_CRATES = "android.permission.MANAGE_CRATES";
@@ -95,8 +96,7 @@
 package android.animation {
 
   public class ValueAnimator extends android.animation.Animator {
-    method public static float getDurationScale();
-    method public static void setDurationScale(float);
+    method @MainThread public static void setDurationScale(@FloatRange(from=0) float);
   }
 
 }
@@ -422,9 +422,9 @@
     method @NonNull public android.content.res.Configuration getConfiguration();
     method public int getParentTaskId();
     method @Nullable public android.app.PictureInPictureParams getPictureInPictureParams();
-    method public boolean getPreferDockBigOverlays();
     method @NonNull public android.window.WindowContainerToken getToken();
     method public boolean hasParentTask();
+    method public boolean shouldDockBigOverlays();
   }
 
   public class TimePickerDialog extends android.app.AlertDialog implements android.content.DialogInterface.OnClickListener android.widget.TimePicker.OnTimeChangedListener {
@@ -622,7 +622,7 @@
 package android.app.cloudsearch {
 
   public static final class SearchRequest.Builder {
-    method @NonNull public android.app.cloudsearch.SearchRequest.Builder setSource(@NonNull String);
+    method @NonNull public android.app.cloudsearch.SearchRequest.Builder setCallerPackageName(@NonNull String);
   }
 
 }
@@ -836,6 +836,7 @@
     method @Nullable public String getSystemTextClassifierPackageName();
     method @Nullable public String getWellbeingPackageName();
     method public void holdLock(android.os.IBinder, int);
+    method @RequiresPermission(android.Manifest.permission.MAKE_UID_VISIBLE) public void makeUidVisible(int, int);
     method @RequiresPermission(android.Manifest.permission.KEEP_UNINSTALLED_PACKAGES) public void setKeepUninstalledPackages(@NonNull java.util.List<java.lang.String>);
     field public static final String FEATURE_ADOPTABLE_STORAGE = "android.software.adoptable_storage";
     field public static final String FEATURE_COMMUNAL_MODE = "android.software.communal_mode";
diff --git a/core/java/android/accessibilityservice/AccessibilityService.java b/core/java/android/accessibilityservice/AccessibilityService.java
index c82f5f6..3cb04e7 100644
--- a/core/java/android/accessibilityservice/AccessibilityService.java
+++ b/core/java/android/accessibilityservice/AccessibilityService.java
@@ -825,17 +825,7 @@
             for (int i = 0; i < mMagnificationControllers.size(); i++) {
                 mMagnificationControllers.valueAt(i).onServiceConnectedLocked();
             }
-            AccessibilityServiceInfo info = getServiceInfo();
-            if (info != null) {
-                boolean requestIme = (info.flags
-                        & AccessibilityServiceInfo.FLAG_INPUT_METHOD_EDITOR) != 0;
-                if (requestIme && !mInputMethodInitialized) {
-                    mInputMethod = onCreateInputMethod();
-                    mInputMethodInitialized = true;
-                }
-            } else {
-                Log.e(LOG_TAG, "AccessibilityServiceInfo is null in dispatchServiceConnected");
-            }
+            updateInputMethod(getServiceInfo());
         }
         if (mSoftKeyboardController != null) {
             mSoftKeyboardController.onServiceConnected();
@@ -846,6 +836,20 @@
         onServiceConnected();
     }
 
+    private void updateInputMethod(AccessibilityServiceInfo info) {
+        if (info != null) {
+            boolean requestIme = (info.flags
+                    & AccessibilityServiceInfo.FLAG_INPUT_METHOD_EDITOR) != 0;
+            if (requestIme && !mInputMethodInitialized) {
+                mInputMethod = onCreateInputMethod();
+                mInputMethodInitialized = true;
+            } else if (!requestIme & mInputMethodInitialized) {
+                mInputMethod = null;
+                mInputMethodInitialized = false;
+            }
+        }
+    }
+
     /**
      * This method is a part of the {@link AccessibilityService} lifecycle and is
      * called after the system has successfully bound to the service. If is
@@ -2521,6 +2525,7 @@
      */
     public final void setServiceInfo(AccessibilityServiceInfo info) {
         mInfo = info;
+        updateInputMethod(info);
         sendServiceInfo();
     }
 
diff --git a/core/java/android/accessibilityservice/InputMethod.java b/core/java/android/accessibilityservice/InputMethod.java
index 001d804..36cfd0e 100644
--- a/core/java/android/accessibilityservice/InputMethod.java
+++ b/core/java/android/accessibilityservice/InputMethod.java
@@ -67,7 +67,7 @@
     private InputConnection mStartedInputConnection;
     private EditorInfo mInputEditorInfo;
 
-    protected InputMethod(@NonNull AccessibilityService service) {
+    public InputMethod(@NonNull AccessibilityService service) {
         mService = service;
     }
 
diff --git a/core/java/android/animation/ValueAnimator.java b/core/java/android/animation/ValueAnimator.java
index 06b424b..6ab7ae6 100644
--- a/core/java/android/animation/ValueAnimator.java
+++ b/core/java/android/animation/ValueAnimator.java
@@ -17,7 +17,9 @@
 package android.animation;
 
 import android.annotation.CallSuper;
+import android.annotation.FloatRange;
 import android.annotation.IntDef;
+import android.annotation.MainThread;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.annotation.TestApi;
@@ -35,8 +37,10 @@
 
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
+import java.lang.ref.WeakReference;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 
 /**
  * This class provides a simple timing engine for running animations
@@ -91,6 +95,9 @@
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
     private static float sDurationScale = 1.0f;
 
+    private static final ArrayList<WeakReference<DurationScaleChangeListener>>
+            sDurationScaleChangeListeners = new ArrayList<>();
+
     /**
      * Internal variables
      * NOTE: This object implements the clone() method, making a deep copy of any referenced
@@ -308,20 +315,92 @@
      */
     @UnsupportedAppUsage
     @TestApi
-    public static void setDurationScale(float durationScale) {
+    @MainThread
+    public static void setDurationScale(@FloatRange(from = 0) float durationScale) {
         sDurationScale = durationScale;
+        List<WeakReference<DurationScaleChangeListener>> listenerCopy;
+
+        synchronized (sDurationScaleChangeListeners) {
+            listenerCopy = new ArrayList<>(sDurationScaleChangeListeners);
+        }
+
+        for (WeakReference<DurationScaleChangeListener> listenerRef : listenerCopy) {
+            final DurationScaleChangeListener listener = listenerRef.get();
+            if (listener != null) {
+                listener.onChanged(durationScale);
+            }
+        }
     }
 
     /**
-     * @hide
+     * Returns the system-wide scaling factor for Animator-based animations.
+     *
+     * This affects both the start delay and duration of all such animations. Setting to 0 will
+     * cause animations to end immediately. The default value is 1.0f.
+     *
+     * @return the duration scale.
      */
-    @UnsupportedAppUsage
-    @TestApi
+    @FloatRange(from = 0)
     public static float getDurationScale() {
         return sDurationScale;
     }
 
     /**
+     * Registers a {@link DurationScaleChangeListener}
+     *
+     * This listens for changes to the system-wide scaling factor for Animator-based animations.
+     * Listeners will be called on the main thread.
+     *
+     * @param listener the listener to register.
+     * @return true if the listener was registered.
+     */
+    public static boolean registerDurationScaleChangeListener(
+            @NonNull DurationScaleChangeListener listener) {
+        int posToReplace = -1;
+        synchronized (sDurationScaleChangeListeners) {
+            for (int i = 0; i < sDurationScaleChangeListeners.size(); i++) {
+                final WeakReference<DurationScaleChangeListener> ref =
+                        sDurationScaleChangeListeners.get(i);
+                if (ref.get() == null) {
+                    if (posToReplace == -1) {
+                        posToReplace = i;
+                    }
+                } else if (ref.get() == listener) {
+                    return false;
+                }
+            }
+            if (posToReplace != -1) {
+                sDurationScaleChangeListeners.set(posToReplace, new WeakReference<>(listener));
+                return true;
+            } else {
+                return sDurationScaleChangeListeners.add(new WeakReference<>(listener));
+            }
+        }
+    }
+
+    /**
+     * Unregisters a DurationScaleChangeListener.
+     *
+     * @see #registerDurationScaleChangeListener(DurationScaleChangeListener)
+     * @param listener the listener to unregister.
+     * @return true if the listener was unregistered.
+     */
+    public static boolean unregisterDurationScaleChangeListener(
+            @NonNull DurationScaleChangeListener listener) {
+        synchronized (sDurationScaleChangeListeners) {
+            WeakReference<DurationScaleChangeListener> listenerRefToRemove = null;
+            for (WeakReference<DurationScaleChangeListener> listenerRef :
+                    sDurationScaleChangeListeners) {
+                if (listenerRef.get() == listener) {
+                    listenerRefToRemove = listenerRef;
+                    break;
+                }
+            }
+            return sDurationScaleChangeListeners.remove(listenerRefToRemove);
+        }
+    }
+
+    /**
      * Returns whether animators are currently enabled, system-wide. By default, all
      * animators are enabled. This can change if either the user sets a Developer Option
      * to set the animator duration scale to 0 or by Battery Savery mode being enabled
@@ -1709,4 +1788,18 @@
     public void setAnimationHandler(@Nullable AnimationHandler animationHandler) {
         mAnimationHandler = animationHandler;
     }
+
+    /**
+     * Listener interface for the system-wide scaling factor for Animator-based animations.
+     *
+     * @see #registerDurationScaleChangeListener(DurationScaleChangeListener)
+     * @see #unregisterDurationScaleChangeListener(DurationScaleChangeListener)
+     */
+    public interface DurationScaleChangeListener {
+        /**
+         * Called when the duration scale changes.
+         * @param scale the duration scale
+         */
+        void onChanged(@FloatRange(from = 0) float scale);
+    }
 }
diff --git a/core/java/android/app/Activity.java b/core/java/android/app/Activity.java
index 8f348a4..ec9bb26 100644
--- a/core/java/android/app/Activity.java
+++ b/core/java/android/app/Activity.java
@@ -984,6 +984,8 @@
     private boolean mIsInMultiWindowMode;
     private boolean mIsInPictureInPictureMode;
 
+    private boolean mShouldDockBigOverlays;
+
     private UiTranslationController mUiTranslationController;
 
     private SplashScreen mSplashScreen;
@@ -2977,13 +2979,28 @@
      * <p> If specified, the system will try to respect the preference, but it may be
      * overridden by a user preference.
      *
-     * @param preferDockBigOverlays indicates that the activity prefers big overlays to be
-     *                              docked next to it instead of overlaying its content
+     * @param shouldDockBigOverlays indicates that big overlays should be docked next to the
+     *                              activity instead of overlay its content
      *
      * @see PictureInPictureParams.Builder#setExpandedAspectRatio
+     * @see #shouldDockBigOverlays
      */
-    public void setPreferDockBigOverlays(boolean preferDockBigOverlays) {
-        ActivityClient.getInstance().setPreferDockBigOverlays(mToken, preferDockBigOverlays);
+    public void setShouldDockBigOverlays(boolean shouldDockBigOverlays) {
+        ActivityClient.getInstance().setShouldDockBigOverlays(mToken, shouldDockBigOverlays);
+        mShouldDockBigOverlays = shouldDockBigOverlays;
+    }
+
+    /**
+     * Returns whether big overlays should be docked next to the activity as set by
+     * {@link #setShouldDockBigOverlays}.
+     *
+     * @return {@code true} if big overlays should be docked next to the activity instead
+     *         of overlay its content
+     *
+     * @see #setShouldDockBigOverlays
+     */
+    public boolean shouldDockBigOverlays() {
+        return mShouldDockBigOverlays;
     }
 
     void dispatchMovedToDisplay(int displayId, Configuration config) {
@@ -5663,7 +5680,6 @@
      * @throws ActivityNotFoundException &nbsp;
      * @hide
      */
-    @SystemApi
     @RequiresPermission(anyOf = {INTERACT_ACROSS_USERS, INTERACT_ACROSS_USERS_FULL})
     public void startActivityAsUser(@NonNull Intent intent,
             @Nullable Bundle options, @NonNull UserHandle user) {
@@ -8249,6 +8265,7 @@
                 .getWindowingMode();
         mIsInMultiWindowMode = inMultiWindowMode(windowingMode);
         mIsInPictureInPictureMode = windowingMode == WINDOWING_MODE_PINNED;
+        mShouldDockBigOverlays = getResources().getBoolean(R.bool.config_dockBigOverlayWindows);
         restoreHasCurrentPermissionRequest(icicle);
         if (persistentState != null) {
             onCreate(icicle, persistentState);
diff --git a/core/java/android/app/ActivityClient.java b/core/java/android/app/ActivityClient.java
index cf8480c..7b7b1ef 100644
--- a/core/java/android/app/ActivityClient.java
+++ b/core/java/android/app/ActivityClient.java
@@ -324,9 +324,9 @@
         }
     }
 
-    void setPreferDockBigOverlays(IBinder token, boolean preferDockBigOverlays) {
+    void setShouldDockBigOverlays(IBinder token, boolean shouldDockBigOverlays) {
         try {
-            getActivityClientController().setPreferDockBigOverlays(token, preferDockBigOverlays);
+            getActivityClientController().setShouldDockBigOverlays(token, shouldDockBigOverlays);
         } catch (RemoteException e) {
             e.rethrowFromSystemServer();
         }
diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java
index 64f0301..3d0ed20 100644
--- a/core/java/android/app/ActivityThread.java
+++ b/core/java/android/app/ActivityThread.java
@@ -868,6 +868,7 @@
         String processName;
         @UnsupportedAppUsage
         ApplicationInfo appInfo;
+        String sdkSandboxClientAppPackage;
         @UnsupportedAppUsage
         List<ProviderInfo> providers;
         ComponentName instrumentationName;
@@ -1113,9 +1114,9 @@
 
         @Override
         public final void bindApplication(String processName, ApplicationInfo appInfo,
-                ProviderInfoList providerList, ComponentName instrumentationName,
-                ProfilerInfo profilerInfo, Bundle instrumentationArgs,
-                IInstrumentationWatcher instrumentationWatcher,
+                String sdkSandboxClientAppPackage, ProviderInfoList providerList,
+                ComponentName instrumentationName, ProfilerInfo profilerInfo,
+                Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
                 IUiAutomationConnection instrumentationUiConnection, int debugMode,
                 boolean enableBinderTracking, boolean trackAllocation,
                 boolean isRestrictedBackupMode, boolean persistent, Configuration config,
@@ -1155,6 +1156,7 @@
             AppBindData data = new AppBindData();
             data.processName = processName;
             data.appInfo = appInfo;
+            data.sdkSandboxClientAppPackage = sdkSandboxClientAppPackage;
             data.providers = providerList.getList();
             data.instrumentationName = instrumentationName;
             data.instrumentationArgs = instrumentationArgs;
@@ -3587,7 +3589,7 @@
         }
 
         try {
-            Application app = r.packageInfo.makeApplication(false, mInstrumentation);
+            Application app = r.packageInfo.makeApplicationInner(false, mInstrumentation);
 
             if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
             if (localLOGV) Slog.v(
@@ -4286,7 +4288,7 @@
         BroadcastReceiver receiver;
         ContextImpl context;
         try {
-            app = packageInfo.makeApplication(false, mInstrumentation);
+            app = packageInfo.makeApplicationInner(false, mInstrumentation);
             context = (ContextImpl) app.getBaseContext();
             if (data.info.splitName != null) {
                 context = (ContextImpl) context.createContextForSplit(data.info.splitName);
@@ -4475,7 +4477,7 @@
         try {
             if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
 
-            Application app = packageInfo.makeApplication(false, mInstrumentation);
+            Application app = packageInfo.makeApplicationInner(false, mInstrumentation);
 
             final java.lang.ClassLoader cl;
             if (data.info.splitName != null) {
@@ -6536,6 +6538,9 @@
         }
 
         data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
+        if (data.sdkSandboxClientAppPackage != null) {
+            data.info.setSdkSandboxStorage(data.sdkSandboxClientAppPackage);
+        }
 
         if (agent != null) {
             handleAttachAgent(agent, data.info);
@@ -6695,7 +6700,7 @@
         try {
             // If the app is being launched for full backup or restore, bring it up in
             // a restricted environment with the base application class.
-            app = data.info.makeApplication(data.restrictedBackupMode, null);
+            app = data.info.makeApplicationInner(data.restrictedBackupMode, null);
 
             // Propagate autofill compat state
             app.setAutofillOptions(data.autofillOptions);
@@ -7565,7 +7570,7 @@
                 mInstrumentation.basicInit(this);
                 ContextImpl context = ContextImpl.createAppContext(
                         this, getSystemContext().mPackageInfo);
-                mInitialApplication = context.mPackageInfo.makeApplication(true, null);
+                mInitialApplication = context.mPackageInfo.makeApplicationInner(true, null);
                 mInitialApplication.onCreate();
             } catch (Exception e) {
                 throw new RuntimeException(
diff --git a/core/java/android/app/AppOpsManager.java b/core/java/android/app/AppOpsManager.java
index 7c7c7ef..4829dc0 100644
--- a/core/java/android/app/AppOpsManager.java
+++ b/core/java/android/app/AppOpsManager.java
@@ -2367,7 +2367,7 @@
             null, // no permission for OP_WRITE_MEDIA_AUDIO
             Manifest.permission.READ_MEDIA_VIDEO,
             null, // no permission for OP_WRITE_MEDIA_VIDEO
-            Manifest.permission.READ_MEDIA_IMAGE,
+            Manifest.permission.READ_MEDIA_IMAGES,
             null, // no permission for OP_WRITE_MEDIA_IMAGES
             null, // no permission for OP_LEGACY_STORAGE
             null, // no permission for OP_ACCESS_ACCESSIBILITY
diff --git a/core/java/android/app/ApplicationExitInfo.java b/core/java/android/app/ApplicationExitInfo.java
index 60e22f4..9bdddd0 100644
--- a/core/java/android/app/ApplicationExitInfo.java
+++ b/core/java/android/app/ApplicationExitInfo.java
@@ -360,6 +360,53 @@
      */
     public static final int SUBREASON_FREEZER_BINDER_TRANSACTION = 20;
 
+    /**
+     * The process was killed because of force-stop, it could be due to that
+     * the user clicked the "Force stop" button of the application in the Settings;
+     * this would be set only when the reason is {@link #REASON_USER_REQUESTED}.
+     *
+     * For internal use only.
+     * @hide
+     */
+    public static final int SUBREASON_FORCE_STOP = 21;
+
+    /**
+     * The process was killed because the user removed the application away from Recents;
+     * this would be set only when the reason is {@link #REASON_USER_REQUESTED}.
+     *
+     * For internal use only.
+     * @hide
+     */
+    public static final int SUBREASON_REMOVE_TASK = 22;
+
+    /**
+     * The process was killed because the user stopped the application from the task manager;
+     * this would be set only when the reason is {@link #REASON_USER_REQUESTED}.
+     *
+     * For internal use only.
+     * @hide
+     */
+    public static final int SUBREASON_STOP_APP = 23;
+
+    /**
+     * The process was killed because the user stopped the application from developer options,
+     * or via the adb shell commmand interface; this would be set only when the reason is
+     * {@link #REASON_USER_REQUESTED}.
+     *
+     * For internal use only.
+     * @hide
+     */
+    public static final int SUBREASON_KILL_BACKGROUND = 24;
+
+    /**
+     * The process was killed because of package update; this would be set only when the reason is
+     * {@link #REASON_USER_REQUESTED}.
+     *
+     * For internal use only.
+     * @hide
+     */
+    public static final int SUBREASON_PACKAGE_UPDATE = 25;
+
     // If there is any OEM code which involves additional app kill reasons, it should
     // be categorized in {@link #REASON_OTHER}, with subreason code starting from 1000.
 
@@ -520,6 +567,11 @@
         SUBREASON_ISOLATED_NOT_NEEDED,
         SUBREASON_FREEZER_BINDER_IOCTL,
         SUBREASON_FREEZER_BINDER_TRANSACTION,
+        SUBREASON_FORCE_STOP,
+        SUBREASON_REMOVE_TASK,
+        SUBREASON_STOP_APP,
+        SUBREASON_KILL_BACKGROUND,
+        SUBREASON_PACKAGE_UPDATE,
     })
     @Retention(RetentionPolicy.SOURCE)
     public @interface SubReason {}
@@ -1193,6 +1245,16 @@
                 return "FREEZER BINDER IOCTL";
             case SUBREASON_FREEZER_BINDER_TRANSACTION:
                 return "FREEZER BINDER TRANSACTION";
+            case SUBREASON_FORCE_STOP:
+                return "FORCE STOP";
+            case SUBREASON_REMOVE_TASK:
+                return "REMOVE TASK";
+            case SUBREASON_STOP_APP:
+                return "STOP APP";
+            case SUBREASON_KILL_BACKGROUND:
+                return "KILL BACKGROUND";
+            case SUBREASON_PACKAGE_UPDATE:
+                return "PACKAGE UPDATE";
             default:
                 return "UNKNOWN";
         }
diff --git a/core/java/android/app/ApplicationPackageManager.java b/core/java/android/app/ApplicationPackageManager.java
index dca5c54..7ffa61b 100644
--- a/core/java/android/app/ApplicationPackageManager.java
+++ b/core/java/android/app/ApplicationPackageManager.java
@@ -3832,4 +3832,13 @@
             throw re.rethrowAsRuntimeException();
         }
     }
+
+    @Override
+    public void makeUidVisible(int recipientUid, int visibleUid) {
+        try {
+            mPM.makeUidVisible(recipientUid, visibleUid);
+        } catch (RemoteException e) {
+            throw e.rethrowAsRuntimeException();
+        }
+    }
 }
diff --git a/core/java/android/app/ContextImpl.java b/core/java/android/app/ContextImpl.java
index a3dd705a..f5eb1f6 100644
--- a/core/java/android/app/ContextImpl.java
+++ b/core/java/android/app/ContextImpl.java
@@ -1998,7 +1998,7 @@
     private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags,
             String instanceName, Handler handler, Executor executor, UserHandle user) {
         // Keep this in sync with DevicePolicyManager.bindDeviceAdminServiceAsUser and
-        // ActivityManagerLocal.bindSupplementalProcessService
+        // ActivityManagerLocal.bindSdkSandboxService
         IServiceConnection sd;
         if (conn == null) {
             throw new IllegalArgumentException("connection is null");
diff --git a/core/java/android/app/IActivityClientController.aidl b/core/java/android/app/IActivityClientController.aidl
index caf1c41b7..1307161 100644
--- a/core/java/android/app/IActivityClientController.aidl
+++ b/core/java/android/app/IActivityClientController.aidl
@@ -88,7 +88,7 @@
 
     boolean enterPictureInPictureMode(in IBinder token, in PictureInPictureParams params);
     void setPictureInPictureParams(in IBinder token, in PictureInPictureParams params);
-    oneway void setPreferDockBigOverlays(in IBinder token, in boolean preferDockBigOverlays);
+    oneway void setShouldDockBigOverlays(in IBinder token, in boolean shouldDockBigOverlays);
     void toggleFreeformWindowingMode(in IBinder token);
 
     oneway void startLockTaskModeByToken(in IBinder token);
diff --git a/core/java/android/app/IApplicationThread.aidl b/core/java/android/app/IApplicationThread.aidl
index 77657d5..f4fbcce 100644
--- a/core/java/android/app/IApplicationThread.aidl
+++ b/core/java/android/app/IApplicationThread.aidl
@@ -72,6 +72,7 @@
     @UnsupportedAppUsage
     void scheduleStopService(IBinder token);
     void bindApplication(in String packageName, in ApplicationInfo info,
+            in String sdkSandboxClientAppPackage,
             in ProviderInfoList providerList, in ComponentName testName,
             in ProfilerInfo profilerInfo, in Bundle testArguments,
             IInstrumentationWatcher testWatcher, IUiAutomationConnection uiAutomationConnection,
diff --git a/core/java/android/app/LoadedApk.java b/core/java/android/app/LoadedApk.java
index cf259e57..deefea8 100644
--- a/core/java/android/app/LoadedApk.java
+++ b/core/java/android/app/LoadedApk.java
@@ -37,6 +37,7 @@
 import android.content.res.Resources;
 import android.os.Build;
 import android.os.Bundle;
+import android.os.Environment;
 import android.os.FileUtils;
 import android.os.GraphicsEnvironment;
 import android.os.Handler;
@@ -411,6 +412,26 @@
         }
     }
 
+    /** @hide */
+    void setSdkSandboxStorage(String sdkSandboxClientAppPackage) {
+        int userId = UserHandle.myUserId();
+        mDeviceProtectedDataDirFile = Environment
+                .getDataMiscDeSharedSdkSandboxDirectory(userId, sdkSandboxClientAppPackage)
+                .getAbsoluteFile();
+        mCredentialProtectedDataDirFile = Environment
+                .getDataMiscCeSharedSdkSandboxDirectory(userId, sdkSandboxClientAppPackage)
+                .getAbsoluteFile();
+
+        if ((mApplicationInfo.privateFlags
+                & ApplicationInfo.PRIVATE_FLAG_DEFAULT_TO_DEVICE_PROTECTED_STORAGE) != 0
+                && PackageManager.APPLY_DEFAULT_TO_DEVICE_PROTECTED_STORAGE) {
+            mDataDirFile = mDeviceProtectedDataDirFile;
+        } else {
+            mDataDirFile = mCredentialProtectedDataDirFile;
+        }
+        mDataDir = mDataDirFile.getAbsolutePath();
+    }
+
     public static void makePaths(ActivityThread activityThread,
                                  ApplicationInfo aInfo,
                                  List<String> outZipPaths) {
@@ -1352,9 +1373,28 @@
         return mResources;
     }
 
+    /**
+     * This is for 3p apps accessing this hidden API directly... in which case, we don't return
+     * the cached Application instance.
+     */
     @UnsupportedAppUsage
     public Application makeApplication(boolean forceDefaultAppClass,
             Instrumentation instrumentation) {
+        return makeApplicationInner(forceDefaultAppClass, instrumentation,
+                /* allowDuplicateInstances= */ true);
+    }
+
+    /**
+     * This is for all the (internal) callers, for which we do return the cached instance.
+     */
+    public Application makeApplicationInner(boolean forceDefaultAppClass,
+            Instrumentation instrumentation) {
+        return makeApplicationInner(forceDefaultAppClass, instrumentation,
+                /* allowDuplicateInstances= */ false);
+    }
+
+    private Application makeApplicationInner(boolean forceDefaultAppClass,
+            Instrumentation instrumentation, boolean allowDuplicateInstances) {
         if (mApplication != null) {
             return mApplication;
         }
@@ -1366,11 +1406,15 @@
                 // Looks like this is always happening for the system server, because
                 // the LoadedApk created in systemMain() -> attach() isn't cached properly?
                 if (!"android".equals(mPackageName)) {
-                    Slog.wtf(TAG, "App instance already created for package=" + mPackageName
+                    Slog.wtfStack(TAG, "App instance already created for package=" + mPackageName
                             + " instance=" + cached);
                 }
-                mApplication = cached;
-                return cached;
+                if (!allowDuplicateInstances) {
+                    mApplication = cached;
+                    return cached;
+                }
+                // Some apps intentionally call makeApplication() to create a new Application
+                // instance... Sigh...
             }
         }
 
@@ -1421,8 +1465,10 @@
         }
         mActivityThread.mAllApplications.add(app);
         mApplication = app;
-        synchronized (sApplications) {
-            sApplications.put(mPackageName, app);
+        if (!allowDuplicateInstances) {
+            synchronized (sApplications) {
+                sApplications.put(mPackageName, app);
+            }
         }
 
         if (instrumentation != null) {
diff --git a/core/java/android/app/StatusBarManager.java b/core/java/android/app/StatusBarManager.java
index c6e36a3..150888c 100644
--- a/core/java/android/app/StatusBarManager.java
+++ b/core/java/android/app/StatusBarManager.java
@@ -24,6 +24,9 @@
 import android.annotation.SystemApi;
 import android.annotation.SystemService;
 import android.annotation.TestApi;
+import android.app.compat.CompatChanges;
+import android.compat.annotation.ChangeId;
+import android.compat.annotation.EnabledSince;
 import android.compat.annotation.UnsupportedAppUsage;
 import android.content.ComponentName;
 import android.content.Context;
@@ -39,6 +42,7 @@
 import android.os.IBinder;
 import android.os.RemoteException;
 import android.os.ServiceManager;
+import android.os.UserHandle;
 import android.util.Pair;
 import android.util.Slog;
 import android.view.View;
@@ -520,6 +524,27 @@
     private final Map<NearbyMediaDevicesProvider, NearbyMediaDevicesProviderWrapper>
             nearbyMediaDevicesProviderMap = new HashMap<>();
 
+    /**
+     * Media controls based on {@link android.app.Notification.MediaStyle} notifications will have
+     * actions based on the media session's {@link android.media.session.PlaybackState}, rather than
+     * the notification's actions.
+     *
+     * These actions will be:
+     * - Play/Pause (depending on whether the current state is a playing state)
+     * - Previous (if declared), or a custom action if the slot is not reserved with
+     *   {@code SESSION_EXTRAS_KEY_SLOT_RESERVATION_SKIP_TO_PREV}
+     * - Next (if declared), or a custom action if the slot is not reserved with
+     *   {@code SESSION_EXTRAS_KEY_SLOT_RESERVATION_SKIP_TO_NEXT}
+     * - Custom action
+     * - Custom action
+     *
+     * @see androidx.media.utils.MediaConstants#SESSION_EXTRAS_KEY_SLOT_RESERVATION_SKIP_TO_PREV
+     * @see androidx.media.utils.MediaConstants#SESSION_EXTRAS_KEY_SLOT_RESERVATION_SKIP_TO_NEXT
+     */
+    @ChangeId
+    @EnabledSince(targetSdkVersion = Build.VERSION_CODES.TIRAMISU)
+    private static final long MEDIA_CONTROL_SESSION_ACTIONS = 203800354L;
+
     @UnsupportedAppUsage
     private Context mContext;
     private IStatusBarService mService;
@@ -844,6 +869,24 @@
     }
 
     /**
+     * Sets an active {@link android.service.quicksettings.TileService} to listening state
+     *
+     * The {@code componentName}'s package must match the calling package.
+     *
+     * @param componentName the tile to set into listening state
+     * @see android.service.quicksettings.TileService#requestListeningState
+     * @hide
+     */
+    public void requestTileServiceListeningState(@NonNull ComponentName componentName) {
+        Objects.requireNonNull(componentName);
+        try {
+            getService().requestTileServiceListeningState(componentName, mContext.getUserId());
+        } catch (RemoteException ex) {
+            throw ex.rethrowFromSystemServer();
+        }
+    }
+
+    /**
      * Request to the user to add a {@link android.service.quicksettings.TileService}
      * to the set of current QS tiles.
      * <p>
@@ -1109,6 +1152,20 @@
         }
     }
 
+    /**
+     * Checks whether the given package should use session-based actions for its media controls.
+     *
+     * @param packageName App posting media controls
+     * @param userId Current user ID
+     * @return true if the app supports session actions
+     *
+     * @hide
+     */
+    public static boolean useMediaSessionActionsForApp(String packageName, int userId) {
+        UserHandle handle = UserHandle.getUserHandleForUid(userId);
+        return CompatChanges.isChangeEnabled(MEDIA_CONTROL_SESSION_ACTIONS, packageName, handle);
+    }
+
     /** @hide */
     public static String windowStateToString(int state) {
         if (state == WINDOW_STATE_HIDING) return "WINDOW_STATE_HIDING";
diff --git a/core/java/android/app/SystemServiceRegistry.java b/core/java/android/app/SystemServiceRegistry.java
index 58db93c..6615374 100644
--- a/core/java/android/app/SystemServiceRegistry.java
+++ b/core/java/android/app/SystemServiceRegistry.java
@@ -138,8 +138,6 @@
 import android.nearby.NearbyFrameworkInitializer;
 import android.net.ConnectivityFrameworkInitializer;
 import android.net.ConnectivityFrameworkInitializerTiramisu;
-import android.net.EthernetManager;
-import android.net.IEthernetManager;
 import android.net.INetworkPolicyManager;
 import android.net.IPacProxyManager;
 import android.net.IVpnManager;
@@ -156,6 +154,7 @@
 import android.net.wifi.WifiFrameworkInitializer;
 import android.net.wifi.nl80211.WifiNl80211Manager;
 import android.nfc.NfcManager;
+import android.ondevicepersonalization.OnDevicePersonalizationFrameworkInitializer;
 import android.os.BatteryManager;
 import android.os.BatteryStats;
 import android.os.BatteryStatsManager;
@@ -789,15 +788,6 @@
                 return new LowpanManager(ctx.getOuterContext(), service);
             }});
 
-        registerService(Context.ETHERNET_SERVICE, EthernetManager.class,
-                new CachedServiceFetcher<EthernetManager>() {
-            @Override
-            public EthernetManager createService(ContextImpl ctx) throws ServiceNotFoundException {
-                IBinder b = ServiceManager.getServiceOrThrow(Context.ETHERNET_SERVICE);
-                IEthernetManager service = IEthernetManager.Stub.asInterface(b);
-                return new EthernetManager(ctx.getOuterContext(), service);
-            }});
-
         registerService(Context.WIFI_NL80211_SERVICE, WifiNl80211Manager.class,
                 new CachedServiceFetcher<WifiNl80211Manager>() {
                     @Override
@@ -1571,6 +1561,7 @@
             SafetyCenterFrameworkInitializer.registerServiceWrappers();
             ConnectivityFrameworkInitializerTiramisu.registerServiceWrappers();
             NearbyFrameworkInitializer.registerServiceWrappers();
+            OnDevicePersonalizationFrameworkInitializer.registerServiceWrappers();
         } finally {
             // If any of the above code throws, we're in a pretty bad shape and the process
             // will likely crash, but we'll reset it just in case there's an exception handler...
diff --git a/core/java/android/app/TaskInfo.java b/core/java/android/app/TaskInfo.java
index 5c7c73c..1a38fcf 100644
--- a/core/java/android/app/TaskInfo.java
+++ b/core/java/android/app/TaskInfo.java
@@ -188,7 +188,7 @@
     /**
      * @hide
      */
-    public boolean preferDockBigOverlays;
+    public boolean shouldDockBigOverlays;
 
     /**
      * The task id of the host Task of the launch-into-pip Activity, i.e., it points to the Task
@@ -392,8 +392,8 @@
 
     /** @hide */
     @TestApi
-    public boolean getPreferDockBigOverlays() {
-        return preferDockBigOverlays;
+    public boolean shouldDockBigOverlays() {
+        return shouldDockBigOverlays;
     }
 
     /** @hide */
@@ -465,7 +465,7 @@
                 && displayAreaFeatureId == that.displayAreaFeatureId
                 && Objects.equals(positionInParent, that.positionInParent)
                 && Objects.equals(pictureInPictureParams, that.pictureInPictureParams)
-                && Objects.equals(preferDockBigOverlays, that.preferDockBigOverlays)
+                && Objects.equals(shouldDockBigOverlays, that.shouldDockBigOverlays)
                 && Objects.equals(displayCutoutInsets, that.displayCutoutInsets)
                 && getWindowingMode() == that.getWindowingMode()
                 && Objects.equals(taskDescription, that.taskDescription)
@@ -522,7 +522,7 @@
         token = WindowContainerToken.CREATOR.createFromParcel(source);
         topActivityType = source.readInt();
         pictureInPictureParams = source.readTypedObject(PictureInPictureParams.CREATOR);
-        preferDockBigOverlays = source.readBoolean();
+        shouldDockBigOverlays = source.readBoolean();
         launchIntoPipHostTaskId = source.readInt();
         displayCutoutInsets = source.readTypedObject(Rect.CREATOR);
         topActivityInfo = source.readTypedObject(ActivityInfo.CREATOR);
@@ -569,7 +569,7 @@
         token.writeToParcel(dest, flags);
         dest.writeInt(topActivityType);
         dest.writeTypedObject(pictureInPictureParams, flags);
-        dest.writeBoolean(preferDockBigOverlays);
+        dest.writeBoolean(shouldDockBigOverlays);
         dest.writeInt(launchIntoPipHostTaskId);
         dest.writeTypedObject(displayCutoutInsets, flags);
         dest.writeTypedObject(topActivityInfo, flags);
@@ -610,7 +610,7 @@
                 + " token=" + token
                 + " topActivityType=" + topActivityType
                 + " pictureInPictureParams=" + pictureInPictureParams
-                + " preferDockBigOverlays=" + preferDockBigOverlays
+                + " shouldDockBigOverlays=" + shouldDockBigOverlays
                 + " launchIntoPipHostTaskId=" + launchIntoPipHostTaskId
                 + " displayCutoutSafeInsets=" + displayCutoutInsets
                 + " topActivityInfo=" + topActivityInfo
diff --git a/core/java/android/app/admin/DevicePolicyManager.java b/core/java/android/app/admin/DevicePolicyManager.java
index 8d4e0d6..27fe312 100644
--- a/core/java/android/app/admin/DevicePolicyManager.java
+++ b/core/java/android/app/admin/DevicePolicyManager.java
@@ -16,6 +16,8 @@
 
 package android.app.admin;
 
+import static android.net.NetworkCapabilities.NET_ENTERPRISE_ID_1;
+
 import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage;
 
 import android.Manifest.permission;
@@ -717,10 +719,11 @@
 
     /**
      * A {@code boolean} extra which determines whether to force a role holder update, regardless
-     * of any internal conditions {@link #ACTION_UPDATE_DEVICE_MANAGEMENT_ROLE_HOLDER} might have.
+     * of any internal conditions {@link #ACTION_UPDATE_DEVICE_POLICY_MANAGEMENT_ROLE_HOLDER} might
+     * have.
      *
      * <p>This extra can be provided to intents with action {@link
-     * #ACTION_UPDATE_DEVICE_MANAGEMENT_ROLE_HOLDER}.
+     * #ACTION_UPDATE_DEVICE_POLICY_MANAGEMENT_ROLE_HOLDER}.
      *
      * @hide
      */
@@ -3279,39 +3282,43 @@
      *
      * <p>The activity must handle the device policy management role holder update and set the
      * intent result to either {@link Activity#RESULT_OK} if the update was successful, {@link
-     * #RESULT_UPDATE_DEVICE_MANAGEMENT_ROLE_HOLDER_RECOVERABLE_ERROR} if it encounters a problem
-     * that may be solved by relaunching it again, or {@link
-     * #RESULT_UPDATE_DEVICE_MANAGEMENT_ROLE_HOLDER_UNRECOVERABLE_ERROR} if it encounters a problem
-     * that will not be solved by relaunching it again.
+     * #RESULT_UPDATE_DEVICE_POLICY_MANAGEMENT_ROLE_HOLDER_RECOVERABLE_ERROR} if it encounters a
+     * problem that may be solved by relaunching it again, or {@link
+     * #RESULT_UPDATE_DEVICE_POLICY_MANAGEMENT_ROLE_HOLDER_UNRECOVERABLE_ERROR} if it encounters a
+     * problem that will not be solved by relaunching it again.
      *
      * <p>If this activity has additional internal conditions which are not met, it should return
-     * {@link #RESULT_UPDATE_DEVICE_MANAGEMENT_ROLE_HOLDER_UNRECOVERABLE_ERROR}.
+     * {@link #RESULT_UPDATE_DEVICE_POLICY_MANAGEMENT_ROLE_HOLDER_UNRECOVERABLE_ERROR}.
      *
      * @hide
      */
     @RequiresPermission(android.Manifest.permission.LAUNCH_DEVICE_MANAGER_SETUP)
     @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
     @SystemApi
-    public static final String ACTION_UPDATE_DEVICE_MANAGEMENT_ROLE_HOLDER =
-            "android.app.action.UPDATE_DEVICE_MANAGEMENT_ROLE_HOLDER";
+    public static final String ACTION_UPDATE_DEVICE_POLICY_MANAGEMENT_ROLE_HOLDER =
+            "android.app.action.UPDATE_DEVICE_POLICY_MANAGEMENT_ROLE_HOLDER";
 
     /**
-     * Result code that can be returned by the {@link #ACTION_UPDATE_DEVICE_MANAGEMENT_ROLE_HOLDER}
-     * handler if it encounters a problem that may be solved by relaunching it again.
+     * Result code that can be returned by the {@link
+     * #ACTION_UPDATE_DEVICE_POLICY_MANAGEMENT_ROLE_HOLDER} handler if it encounters a problem
+     * that may be solved by relaunching it again.
      *
      * @hide
      */
     @SystemApi
-    public static final int RESULT_UPDATE_DEVICE_MANAGEMENT_ROLE_HOLDER_RECOVERABLE_ERROR = 1;
+    public static final int RESULT_UPDATE_DEVICE_POLICY_MANAGEMENT_ROLE_HOLDER_RECOVERABLE_ERROR =
+            1;
 
     /**
-     * Result code that can be returned by the {@link #ACTION_UPDATE_DEVICE_MANAGEMENT_ROLE_HOLDER}
-     * handler if it encounters a problem that will not be solved by relaunching it again.
+     * Result code that can be returned by the {@link
+     * #ACTION_UPDATE_DEVICE_POLICY_MANAGEMENT_ROLE_HOLDER} handler if it encounters a problem that
+     * will not be solved by relaunching it again.
      *
      * @hide
      */
     @SystemApi
-    public static final int RESULT_UPDATE_DEVICE_MANAGEMENT_ROLE_HOLDER_UNRECOVERABLE_ERROR = 2;
+    public static final int RESULT_UPDATE_DEVICE_POLICY_MANAGEMENT_ROLE_HOLDER_UNRECOVERABLE_ERROR =
+            2;
 
     /**
      * An {@link Intent} extra which resolves to a custom user consent screen.
@@ -11031,7 +11038,7 @@
     }
 
     /**
-     * Sets whether preferential network service is enabled on the work profile.
+     * Sets whether preferential network service is enabled.
      * For example, an organization can have a deal/agreement with a carrier that all of
      * the work data from its employees’ devices will be sent via a network service dedicated
      * for enterprise use.
@@ -11039,75 +11046,72 @@
      * An example of a supported preferential network service is the Enterprise
      * slice on 5G networks.
      *
-     * By default, preferential network service is disabled on the work profile on supported
-     * carriers and devices. Admins can explicitly enable it with this API.
-     * On fully-managed devices this method is unsupported because all traffic is considered
-     * work traffic.
+     * By default, preferential network service is disabled on the work profile and
+     * fully managed devices, on supported carriers and devices.
+     * Admins can explicitly enable it with this API.
      *
      * <p> This method enables preferential network service with a default configuration.
-     * To fine-tune the configuration, use {@link #setPreferentialNetworkServiceConfig) instead.
+     * To fine-tune the configuration, use {@link #setPreferentialNetworkServiceConfigs) instead.
+     * <p> Before Android version {@link android.os.Build.VERSION_CODES#TIRAMISU}:
+     * this method can be called by the profile owner of a managed profile.
+     * <p> Starting from Android version {@link android.os.Build.VERSION_CODES#TIRAMISU}:
+     * This method can be called by the profile owner of a managed profile
+     * or device owner.
      *
-     * <p>This method can only be called by the profile owner of a managed profile.
      * @param enabled whether preferential network service should be enabled.
-     * @throws SecurityException if the caller is not the profile owner.
+     * @throws SecurityException if the caller is not the profile owner or device owner.
      **/
     public void setPreferentialNetworkServiceEnabled(boolean enabled) {
         throwIfParentInstance("setPreferentialNetworkServiceEnabled");
-        if (mService == null) {
-            return;
+        PreferentialNetworkServiceConfig.Builder configBuilder =
+                new PreferentialNetworkServiceConfig.Builder();
+        configBuilder.setEnabled(enabled);
+        if (enabled) {
+            configBuilder.setNetworkId(NET_ENTERPRISE_ID_1);
         }
-
-        try {
-            mService.setPreferentialNetworkServiceEnabled(enabled);
-        } catch (RemoteException e) {
-            throw e.rethrowFromSystemServer();
-        }
+        setPreferentialNetworkServiceConfigs(List.of(configBuilder.build()));
     }
 
     /**
      * Indicates whether preferential network service is enabled.
      *
-     * <p>This method can be called by the profile owner of a managed profile.
+     * <p> Before Android version {@link android.os.Build.VERSION_CODES#TIRAMISU}:
+     * This method can be called by the profile owner of a managed profile.
+     * <p> Starting from Android version {@link android.os.Build.VERSION_CODES#TIRAMISU}:
+     * This method can be called by the profile owner of a managed profile
+     * or device owner.
      *
      * @return whether preferential network service is enabled.
-     * @throws SecurityException if the caller is not the profile owner.
+     * @throws SecurityException if the caller is not the profile owner or device owner.
      */
     public boolean isPreferentialNetworkServiceEnabled() {
         throwIfParentInstance("isPreferentialNetworkServiceEnabled");
-        if (mService == null) {
-            return false;
-        }
-        try {
-            return mService.isPreferentialNetworkServiceEnabled(myUserId());
-        } catch (RemoteException e) {
-            throw e.rethrowFromSystemServer();
-        }
+        return getPreferentialNetworkServiceConfigs().stream().anyMatch(c -> c.isEnabled());
     }
 
     /**
-     * Sets preferential network configuration on the work profile.
+     * Sets preferential network configurations.
      * {@see PreferentialNetworkServiceConfig}
      *
      * An example of a supported preferential network service is the Enterprise
      * slice on 5G networks.
      *
-     * By default, preferential network service is disabled on the work profile on supported
-     * carriers and devices. Admins can explicitly enable it with this API.
-     * On fully-managed devices this method is unsupported because all traffic is considered
-     * work traffic.
+     * By default, preferential network service is disabled on the work profile and fully managed
+     * devices, on supported carriers and devices. Admins can explicitly enable it with this API.
+     * If admin wants to have multiple enterprise slices,
+     * it can be configured by passing list of {@link PreferentialNetworkServiceConfig} objects.
      *
-     * <p>This method can only be called by the profile owner of a managed profile.
-     * @param preferentialNetworkServiceConfig preferential network configuration.
-     * @throws SecurityException if the caller is not the profile owner.
+     * @param preferentialNetworkServiceConfigs list of preferential network configurations.
+     * @throws SecurityException if the caller is not the profile owner or device owner.
      **/
-    public void setPreferentialNetworkServiceConfig(
-            @NonNull PreferentialNetworkServiceConfig preferentialNetworkServiceConfig) {
-        throwIfParentInstance("setPreferentialNetworkServiceConfig");
+    public void setPreferentialNetworkServiceConfigs(
+            @NonNull List<PreferentialNetworkServiceConfig> preferentialNetworkServiceConfigs) {
+        throwIfParentInstance("setPreferentialNetworkServiceConfigs");
         if (mService == null) {
             return;
         }
         try {
-            mService.setPreferentialNetworkServiceConfig(preferentialNetworkServiceConfig);
+            mService.setPreferentialNetworkServiceConfigs(preferentialNetworkServiceConfigs);
         } catch (RemoteException e) {
             throw e.rethrowFromSystemServer();
         }
@@ -11117,18 +11121,16 @@
      * Get preferential network configuration
      * {@see PreferentialNetworkServiceConfig}
      *
-     * <p>This method can be called by the profile owner of a managed profile.
-     *
      * @return preferential network configuration.
-     * @throws SecurityException if the caller is not the profile owner.
+     * @throws SecurityException if the caller is not the profile owner or device owner.
      */
-    public @NonNull PreferentialNetworkServiceConfig getPreferentialNetworkServiceConfig() {
-        throwIfParentInstance("getPreferentialNetworkServiceConfig");
+    public @NonNull List<PreferentialNetworkServiceConfig> getPreferentialNetworkServiceConfigs() {
+        throwIfParentInstance("getPreferentialNetworkServiceConfigs");
         if (mService == null) {
-            return PreferentialNetworkServiceConfig.DEFAULT;
+            return List.of(PreferentialNetworkServiceConfig.DEFAULT);
         }
         try {
-            return mService.getPreferentialNetworkServiceConfig();
+            return mService.getPreferentialNetworkServiceConfigs();
         } catch (RemoteException e) {
             throw e.rethrowFromSystemServer();
         }
@@ -13612,13 +13614,18 @@
     }
 
     /**
-     * Called by device owner to add an override APN.
+     * Called by device owner or profile owner to add an override APN.
      *
      * <p>This method may returns {@code -1} if {@code apnSetting} conflicts with an existing
      * override APN. Update the existing conflicted APN with
      * {@link #updateOverrideApn(ComponentName, int, ApnSetting)} instead of adding a new entry.
      * <p>Two override APNs are considered to conflict when all the following APIs return
      * the same values on both override APNs:
+     * <p> Before Android version {@link android.os.Build.VERSION_CODES#TIRAMISU}:
+     * Only device owners can add APNs.
+     * <p> Starting from Android version {@link android.os.Build.VERSION_CODES#TIRAMISU}:
+     * Device and profile owners can add enterprise APNs
+     * ({@link ApnSetting#TYPE_ENTERPRISE}), while only device owners can add other type of APNs.
      * <ul>
      *   <li>{@link ApnSetting#getOperatorNumeric()}</li>
      *   <li>{@link ApnSetting#getApnName()}</li>
@@ -13637,7 +13644,8 @@
      * @param apnSetting the override APN to insert
      * @return The {@code id} of inserted override APN. Or {@code -1} when failed to insert into
      *         the database.
-     * @throws SecurityException if {@code admin} is not a device owner.
+     * @throws SecurityException If request is for enterprise APN {@code admin} is either device
+     * owner or profile owner and in all other types of APN if {@code admin} is not a device owner.
      *
      * @see #setOverrideApnsEnabled(ComponentName, boolean)
      */
@@ -13654,20 +13662,26 @@
     }
 
     /**
-     * Called by device owner to update an override APN.
+     * Called by device owner or profile owner to update an override APN.
      *
      * <p>This method may returns {@code false} if there is no override APN with the given
      * {@code apnId}.
      * <p>This method may also returns {@code false} if {@code apnSetting} conflicts with an
      * existing override APN. Update the existing conflicted APN instead.
      * <p>See {@link #addOverrideApn} for the definition of conflict.
+     * <p> Before Android version {@link android.os.Build.VERSION_CODES#TIRAMISU}:
+     * Only device owners can update APNs.
+     * <p> Starting from Android version {@link android.os.Build.VERSION_CODES#TIRAMISU}:
+     * Device and profile owners can update enterprise APNs
+     * ({@link ApnSetting#TYPE_ENTERPRISE}), while only device owners can update other type of APNs.
      *
      * @param admin which {@link DeviceAdminReceiver} this request is associated with
      * @param apnId the {@code id} of the override APN to update
      * @param apnSetting the override APN to update
      * @return {@code true} if the required override APN is successfully updated,
      *         {@code false} otherwise.
-     * @throws SecurityException if {@code admin} is not a device owner.
+     * @throws SecurityException If request is for enterprise APN {@code admin} is either device
+     * owner or profile owner and in all other types of APN if {@code admin} is not a device owner.
      *
      * @see #setOverrideApnsEnabled(ComponentName, boolean)
      */
@@ -13685,16 +13699,22 @@
     }
 
     /**
-     * Called by device owner to remove an override APN.
+     * Called by device owner or profile owner to remove an override APN.
      *
      * <p>This method may returns {@code false} if there is no override APN with the given
      * {@code apnId}.
+     * <p> Before Android version {@link android.os.Build.VERSION_CODES#TIRAMISU}:
+     * Only device owners can remove APNs.
+     * <p> Starting from Android version {@link android.os.Build.VERSION_CODES#TIRAMISU}:
+     * Device and profile owners can remove enterprise APNs
+     * ({@link ApnSetting#TYPE_ENTERPRISE}), while only device owners can remove other type of APNs.
      *
      * @param admin which {@link DeviceAdminReceiver} this request is associated with
      * @param apnId the {@code id} of the override APN to remove
      * @return {@code true} if the required override APN is successfully removed, {@code false}
      *         otherwise.
-     * @throws SecurityException if {@code admin} is not a device owner.
+     * @throws SecurityException If request is for enterprise APN {@code admin} is either device
+     * owner or profile owner and in all other types of APN if {@code admin} is not a device owner.
      *
      * @see #setOverrideApnsEnabled(ComponentName, boolean)
      */
diff --git a/core/java/android/app/admin/IDevicePolicyManager.aidl b/core/java/android/app/admin/IDevicePolicyManager.aidl
index 9d28dde..77db146 100644
--- a/core/java/android/app/admin/IDevicePolicyManager.aidl
+++ b/core/java/android/app/admin/IDevicePolicyManager.aidl
@@ -285,12 +285,9 @@
     void setSecondaryLockscreenEnabled(in ComponentName who, boolean enabled);
     boolean isSecondaryLockscreenEnabled(in UserHandle userHandle);
 
-    void setPreferentialNetworkServiceEnabled(in boolean enabled);
-    boolean isPreferentialNetworkServiceEnabled(int userHandle);
-
-    void setPreferentialNetworkServiceConfig(
-            in PreferentialNetworkServiceConfig preferentialNetworkServiceConfig);
-    PreferentialNetworkServiceConfig getPreferentialNetworkServiceConfig();
+    void setPreferentialNetworkServiceConfigs(
+            in List<PreferentialNetworkServiceConfig> preferentialNetworkServiceConfigs);
+    List<PreferentialNetworkServiceConfig> getPreferentialNetworkServiceConfigs();
 
     void setLockTaskPackages(in ComponentName who, in String[] packages);
     String[] getLockTaskPackages(in ComponentName who);
diff --git a/core/java/android/app/admin/PreferentialNetworkServiceConfig.java b/core/java/android/app/admin/PreferentialNetworkServiceConfig.java
index 2849139..54170a2 100644
--- a/core/java/android/app/admin/PreferentialNetworkServiceConfig.java
+++ b/core/java/android/app/admin/PreferentialNetworkServiceConfig.java
@@ -28,7 +28,7 @@
 
 /**
  * Network configuration to be set for the user profile
- * {@see DevicePolicyManager#setPreferentialNetworkServiceConfig}.
+ * {@see DevicePolicyManager#setPreferentialNetworkServiceConfigs}.
  */
 public final class PreferentialNetworkServiceConfig implements Parcelable {
     final boolean mIsEnabled;
@@ -147,8 +147,6 @@
 
     /**
      * @return preference enterprise identifier.
-     * valid values starts from
-     * {@link #PREFERENTIAL_NETWORK_ID_1} to {@link #PREFERENTIAL_NETWORK_ID_5}.
      * preference identifier is applicable only if preference network service is enabled
      *
      */
@@ -286,8 +284,6 @@
 
         /**
          * Set the preferential network identifier.
-         * Valid values starts from {@link #PREFERENTIAL_NETWORK_ID_1} to
-         * {@link #PREFERENTIAL_NETWORK_ID_5}.
          * preference identifier is applicable only if preferential network service is enabled.
          * @param preferenceId  preference Id
          * @return The builder to facilitate chaining.
diff --git a/core/java/android/app/cloudsearch/SearchRequest.java b/core/java/android/app/cloudsearch/SearchRequest.java
index 4d6507a..bf78325 100644
--- a/core/java/android/app/cloudsearch/SearchRequest.java
+++ b/core/java/android/app/cloudsearch/SearchRequest.java
@@ -100,7 +100,7 @@
      *
      */
     @NonNull
-    private String mSource;
+    private String mCallerPackageName;
 
     private SearchRequest(Parcel in) {
         this.mQuery = in.readString();
@@ -109,17 +109,17 @@
         this.mMaxLatencyMillis = in.readFloat();
         this.mSearchConstraints = in.readBundle();
         this.mId = in.readString();
-        this.mSource = in.readString();
+        this.mCallerPackageName = in.readString();
     }
 
     private SearchRequest(String query, int resultOffset, int resultNumber, float maxLatencyMillis,
-            Bundle searchConstraints, String source) {
+            Bundle searchConstraints, String callerPackageName) {
         mQuery = query;
         mResultOffset = resultOffset;
         mResultNumber = resultNumber;
         mMaxLatencyMillis = maxLatencyMillis;
         mSearchConstraints = searchConstraints;
-        mSource = source;
+        mCallerPackageName = callerPackageName;
     }
 
     /** Returns the original query. */
@@ -151,8 +151,8 @@
 
     /** Gets the caller's package name. */
     @NonNull
-    public String getSource() {
-        return mSource;
+    public String getCallerPackageName() {
+        return mCallerPackageName;
     }
 
     /** Returns the search request id, which is used to identify the request. */
@@ -169,8 +169,8 @@
      *
      * @hide
      */
-    public void setSource(@NonNull String source) {
-        this.mSource = source;
+    public void setCallerPackageName(@NonNull String callerPackageName) {
+        this.mCallerPackageName = callerPackageName;
     }
 
     private SearchRequest(Builder b) {
@@ -179,7 +179,7 @@
         mResultNumber = b.mResultNumber;
         mMaxLatencyMillis = b.mMaxLatencyMillis;
         mSearchConstraints = requireNonNull(b.mSearchConstraints);
-        mSource = requireNonNull(b.mSource);
+        mCallerPackageName = requireNonNull(b.mCallerPackageName);
     }
 
     /**
@@ -207,7 +207,7 @@
         dest.writeFloat(this.mMaxLatencyMillis);
         dest.writeBundle(this.mSearchConstraints);
         dest.writeString(getRequestId());
-        dest.writeString(this.mSource);
+        dest.writeString(this.mCallerPackageName);
     }
 
     @Override
@@ -231,7 +231,7 @@
                 && mResultNumber == that.mResultNumber
                 && mMaxLatencyMillis == that.mMaxLatencyMillis
                 && Objects.equals(mSearchConstraints, that.mSearchConstraints)
-                && Objects.equals(mSource, that.mSource);
+                && Objects.equals(mCallerPackageName, that.mCallerPackageName);
     }
 
     @Override
@@ -246,14 +246,15 @@
         }
 
         return String.format("SearchRequest: {query:%s,offset:%d;number:%d;max_latency:%f;"
-                        + "is_presubmit:%b;search_provider:%s;source:%s}", mQuery, mResultOffset,
-                mResultNumber, mMaxLatencyMillis, isPresubmit, searchProvider, mSource);
+                        + "is_presubmit:%b;search_provider:%s;callerPackageName:%s}", mQuery,
+                mResultOffset, mResultNumber, mMaxLatencyMillis, isPresubmit, searchProvider,
+                mCallerPackageName);
     }
 
     @Override
     public int hashCode() {
         return Objects.hash(mQuery, mResultOffset, mResultNumber, mMaxLatencyMillis,
-                mSearchConstraints, mSource);
+                mSearchConstraints, mCallerPackageName);
     }
 
     /**
@@ -268,7 +269,7 @@
         private int mResultNumber;
         private float mMaxLatencyMillis;
         private Bundle mSearchConstraints;
-        private String mSource;
+        private String mCallerPackageName;
 
         /**
          *
@@ -284,7 +285,7 @@
             mResultNumber = 10;
             mMaxLatencyMillis = 200;
             mSearchConstraints = Bundle.EMPTY;
-            mSource = "DEFAULT_CALLER";
+            mCallerPackageName = "DEFAULT_CALLER";
         }
 
         /** Sets the input query. */
@@ -329,8 +330,8 @@
          */
         @NonNull
         @TestApi
-        public Builder setSource(@NonNull String source) {
-            this.mSource = source;
+        public Builder setCallerPackageName(@NonNull String callerPackageName) {
+            this.mCallerPackageName = callerPackageName;
             return this;
         }
 
@@ -343,7 +344,7 @@
             }
 
             return new SearchRequest(mQuery, mResultOffset, mResultNumber, mMaxLatencyMillis,
-                               mSearchConstraints, mSource);
+                               mSearchConstraints, mCallerPackageName);
         }
     }
 }
diff --git a/core/java/android/app/cloudsearch/SearchResult.java b/core/java/android/app/cloudsearch/SearchResult.java
index af8adac..c6583b6 100644
--- a/core/java/android/app/cloudsearch/SearchResult.java
+++ b/core/java/android/app/cloudsearch/SearchResult.java
@@ -71,6 +71,10 @@
             EXTRAINFO_APP_BADGES,
             EXTRAINFO_ACTION_BUTTON_TEXT_PREREGISTERING,
             EXTRAINFO_ACTION_BUTTON_IMAGE_PREREGISTERING,
+            EXTRAINFO_ACTION_APP_CARD,
+            EXTRAINFO_ACTION_INSTALL_BUTTON,
+            EXTRAINFO_APP_PACKAGE_NAME,
+            EXTRAINFO_APP_INSTALL_COUNT,
             EXTRAINFO_WEB_URL,
             EXTRAINFO_WEB_ICON})
     public @interface SearchResultExtraInfoKey {}
@@ -119,6 +123,20 @@
     @SuppressLint("IntentName")
     public static final String EXTRAINFO_ACTION_BUTTON_IMAGE_PREREGISTERING =
             "android.app.cloudsearch.ACTION_BUTTON_IMAGE";
+    /** Intent for tapping the app card, PendingIntent expected. */
+    @SuppressLint("IntentName")
+    public static final String EXTRAINFO_ACTION_APP_CARD =
+            "android.app.cloudsearch.ACTION_APP_CARD";
+    /** Intent for tapping the install button, PendingIntent expected. */
+    @SuppressLint("IntentName")
+    public static final String EXTRAINFO_ACTION_INSTALL_BUTTON =
+            "android.app.cloudsearch.ACTION_INSTALL_BUTTON";
+    /** App's package name, String value expected. */
+    public static final String EXTRAINFO_APP_PACKAGE_NAME =
+            "android.app.cloudsearch.APP_PACKAGE_NAME";
+    /** App's install count, double value expected. */
+    public static final String EXTRAINFO_APP_INSTALL_COUNT =
+            "android.app.cloudsearch.APP_INSTALL_COUNT";
     /** Web content's URL, String value expected. */
     public static final String EXTRAINFO_WEB_URL = "android.app.cloudsearch.WEB_URL";
     /** Web content's domain icon, android.graphics.drawable.Icon expected. */
diff --git a/core/java/android/app/trust/TEST_MAPPING b/core/java/android/app/trust/TEST_MAPPING
new file mode 100644
index 0000000..b9c46bf
--- /dev/null
+++ b/core/java/android/app/trust/TEST_MAPPING
@@ -0,0 +1,15 @@
+{
+  "presubmit": [
+    {
+      "name": "TrustTests",
+      "options": [
+        {
+          "include-filter": "android.trust.test"
+        },
+        {
+          "exclude-annotation": "androidx.test.filters.FlakyTest"
+        }
+      ]
+    }
+  ]
+}
\ No newline at end of file
diff --git a/core/java/android/companion/virtual/VirtualDeviceParams.java b/core/java/android/companion/virtual/VirtualDeviceParams.java
index 41b1a1f..cbb5183 100644
--- a/core/java/android/companion/virtual/VirtualDeviceParams.java
+++ b/core/java/android/companion/virtual/VirtualDeviceParams.java
@@ -28,6 +28,8 @@
 import android.os.UserHandle;
 import android.util.ArraySet;
 
+import com.android.internal.util.Preconditions;
+
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
@@ -82,7 +84,7 @@
     public static final int ACTIVITY_POLICY_DEFAULT_BLOCKED = 1;
 
     private final int mLockState;
-    private final ArraySet<UserHandle> mUsersWithMatchingAccounts;
+    @NonNull private final ArraySet<UserHandle> mUsersWithMatchingAccounts;
     @NonNull private final ArraySet<ComponentName> mAllowedActivities;
     @NonNull private final ArraySet<ComponentName> mBlockedActivities;
     @ActivityPolicy
@@ -94,10 +96,14 @@
             @NonNull Set<ComponentName> allowedActivities,
             @NonNull Set<ComponentName> blockedActivities,
             @ActivityPolicy int defaultActivityPolicy) {
+        Preconditions.checkNotNull(usersWithMatchingAccounts);
+        Preconditions.checkNotNull(allowedActivities);
+        Preconditions.checkNotNull(blockedActivities);
+
         mLockState = lockState;
         mUsersWithMatchingAccounts = new ArraySet<>(usersWithMatchingAccounts);
-        mAllowedActivities = allowedActivities == null ? null : new ArraySet<>(allowedActivities);
-        mBlockedActivities = blockedActivities == null ? null : new ArraySet<>(blockedActivities);
+        mAllowedActivities = new ArraySet<>(allowedActivities);
+        mBlockedActivities = new ArraySet<>(blockedActivities);
         mDefaultActivityPolicy = defaultActivityPolicy;
     }
 
@@ -130,30 +136,24 @@
     }
 
     /**
-     * Returns the set of activities allowed to be streamed, or {@code null} if all activities are
+     * Returns the set of activities allowed to be streamed, or empty set if all activities are
      * allowed, except the ones explicitly blocked.
      *
      * @see Builder#setAllowedActivities(Set)
      */
     @NonNull
     public Set<ComponentName> getAllowedActivities() {
-        if (mAllowedActivities == null) {
-            return Collections.emptySet();
-        }
         return Collections.unmodifiableSet(mAllowedActivities);
     }
 
     /**
-     * Returns the set of activities that are blocked from streaming, or {@code null} to indicate
+     * Returns the set of activities that are blocked from streaming, or empty set to indicate
      * that all activities in {@link #getAllowedActivities} are allowed.
      *
      * @see Builder#setBlockedActivities(Set)
      */
     @NonNull
     public Set<ComponentName> getBlockedActivities() {
-        if (mBlockedActivities == null) {
-            return Collections.emptySet();
-        }
         return Collections.unmodifiableSet(mBlockedActivities);
     }
 
@@ -237,7 +237,7 @@
     public static final class Builder {
 
         private @LockState int mLockState = LOCK_STATE_DEFAULT;
-        private Set<UserHandle> mUsersWithMatchingAccounts;
+        @NonNull private Set<UserHandle> mUsersWithMatchingAccounts = Collections.emptySet();;
         @NonNull private Set<ComponentName> mBlockedActivities = Collections.emptySet();
         @NonNull private Set<ComponentName> mAllowedActivities = Collections.emptySet();
         @ActivityPolicy
@@ -282,6 +282,7 @@
         @NonNull
         public Builder setUsersWithMatchingAccounts(
                 @NonNull Set<UserHandle> usersWithMatchingAccounts) {
+            Preconditions.checkNotNull(usersWithMatchingAccounts);
             mUsersWithMatchingAccounts = usersWithMatchingAccounts;
             return this;
         }
@@ -301,6 +302,7 @@
          */
         @NonNull
         public Builder setAllowedActivities(@NonNull Set<ComponentName> allowedActivities) {
+            Preconditions.checkNotNull(allowedActivities);
             if (mDefaultActivityPolicyConfigured
                     && mDefaultActivityPolicy != ACTIVITY_POLICY_DEFAULT_BLOCKED) {
                 throw new IllegalArgumentException(
@@ -327,6 +329,7 @@
          */
         @NonNull
         public Builder setBlockedActivities(@NonNull Set<ComponentName> blockedActivities) {
+            Preconditions.checkNotNull(blockedActivities);
             if (mDefaultActivityPolicyConfigured
                     && mDefaultActivityPolicy != ACTIVITY_POLICY_DEFAULT_ALLOWED) {
                 throw new IllegalArgumentException(
@@ -343,9 +346,6 @@
          */
         @NonNull
         public VirtualDeviceParams build() {
-            if (mUsersWithMatchingAccounts == null) {
-                mUsersWithMatchingAccounts = Collections.emptySet();
-            }
             return new VirtualDeviceParams(
                     mLockState,
                     mUsersWithMatchingAccounts,
diff --git a/core/java/android/content/Context.java b/core/java/android/content/Context.java
index 2bda020..60efb4d 100644
--- a/core/java/android/content/Context.java
+++ b/core/java/android/content/Context.java
@@ -2011,9 +2011,9 @@
      * @hide
      */
     @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS)
-    @UnsupportedAppUsage
-    public void startActivityAsUser(@RequiresPermission Intent intent, @Nullable Bundle options,
-            UserHandle userId) {
+    @SystemApi
+    public void startActivityAsUser(@RequiresPermission @NonNull Intent intent,
+            @Nullable Bundle options, @NonNull UserHandle userId) {
         throw new RuntimeException("Not implemented. Must override in a subclass.");
     }
 
diff --git a/core/java/android/content/pm/CrossProfileApps.java b/core/java/android/content/pm/CrossProfileApps.java
index 94f0561..b6917e26 100644
--- a/core/java/android/content/pm/CrossProfileApps.java
+++ b/core/java/android/content/pm/CrossProfileApps.java
@@ -104,7 +104,44 @@
                     mContext.getAttributionTag(),
                     component,
                     targetUser.getIdentifier(),
-                    true);
+                    true,
+                    null,
+                    null);
+        } catch (RemoteException ex) {
+            throw ex.rethrowFromSystemServer();
+        }
+    }
+
+    /**
+     * Starts the specified main activity of the caller package in the specified profile, launching
+     * in the specified activity.
+     *
+     * @param component The ComponentName of the activity to launch, it must be exported and has
+     *        action {@link android.content.Intent#ACTION_MAIN}, category
+     *        {@link android.content.Intent#CATEGORY_LAUNCHER}. Otherwise, SecurityException will
+     *        be thrown.
+     * @param targetUser The UserHandle of the profile, must be one of the users returned by
+     *        {@link #getTargetUserProfiles()}, otherwise a {@link SecurityException} will
+     *        be thrown.
+     * @param callingActivity The activity to start the new activity from for the purposes of
+     *        deciding which task the new activity should belong to. If {@code null}, the activity
+     *        will always be started in a new task.
+     * @param options The activity options or {@code null}. See {@link android.app.ActivityOptions}.
+     */
+    public void startMainActivity(@NonNull ComponentName component,
+            @NonNull UserHandle targetUser,
+            @Nullable Activity callingActivity,
+            @Nullable Bundle options) {
+        try {
+            mService.startActivityAsUser(
+                    mContext.getIApplicationThread(),
+                    mContext.getPackageName(),
+                    mContext.getAttributionTag(),
+                    component,
+                    targetUser.getIdentifier(),
+                    true,
+                    callingActivity != null ? callingActivity.getActivityToken() : null,
+                    options);
         } catch (RemoteException ex) {
             throw ex.rethrowFromSystemServer();
         }
@@ -191,6 +228,48 @@
      * @param targetUser The UserHandle of the profile, must be one of the users returned by
      *        {@link #getTargetUserProfiles()}, otherwise a {@link SecurityException} will
      *        be thrown.
+     * @param callingActivity The activity to start the new activity from for the purposes of
+     *        deciding which task the new activity should belong to. If {@code null}, the activity
+     *        will always be started in a new task.
+     * @param options The activity options or {@code null}. See {@link android.app.ActivityOptions}.
+     * @hide
+     */
+    @SystemApi
+    @RequiresPermission(anyOf = {
+            android.Manifest.permission.INTERACT_ACROSS_PROFILES,
+            android.Manifest.permission.START_CROSS_PROFILE_ACTIVITIES})
+    public void startActivity(
+            @NonNull ComponentName component,
+            @NonNull UserHandle targetUser,
+            @Nullable Activity callingActivity,
+            @Nullable Bundle options) {
+        try {
+            mService.startActivityAsUser(
+                    mContext.getIApplicationThread(),
+                    mContext.getPackageName(),
+                    mContext.getAttributionTag(),
+                    component,
+                    targetUser.getIdentifier(),
+                    false,
+                    callingActivity != null ? callingActivity.getActivityToken() : null,
+                    options);
+        } catch (RemoteException ex) {
+            throw ex.rethrowFromSystemServer();
+        }
+    }
+
+    /**
+     * Starts the specified activity of the caller package in the specified profile. Unlike
+     * {@link #startMainActivity}, this can start any activity of the caller package, not just
+     * the main activity.
+     * The caller must have the {@link android.Manifest.permission#INTERACT_ACROSS_PROFILES}
+     * or {@link android.Manifest.permission#START_CROSS_PROFILE_ACTIVITIES}
+     * permission and both the caller and target user profiles must be in the same profile group.
+     *
+     * @param component The ComponentName of the activity to launch. It must be exported.
+     * @param targetUser The UserHandle of the profile, must be one of the users returned by
+     *        {@link #getTargetUserProfiles()}, otherwise a {@link SecurityException} will
+     *        be thrown.
      * @hide
      */
     @SystemApi
@@ -201,7 +280,7 @@
         try {
             mService.startActivityAsUser(mContext.getIApplicationThread(),
                     mContext.getPackageName(), mContext.getAttributionTag(), component,
-                    targetUser.getIdentifier(), false);
+                    targetUser.getIdentifier(), false, null, null);
         } catch (RemoteException ex) {
             throw ex.rethrowFromSystemServer();
         }
diff --git a/core/java/android/content/pm/ICrossProfileApps.aidl b/core/java/android/content/pm/ICrossProfileApps.aidl
index e2850f1..4f2c106 100644
--- a/core/java/android/content/pm/ICrossProfileApps.aidl
+++ b/core/java/android/content/pm/ICrossProfileApps.aidl
@@ -29,7 +29,7 @@
 interface ICrossProfileApps {
     void startActivityAsUser(in IApplicationThread caller, in String callingPackage,
             in String callingFeatureId, in ComponentName component, int userId,
-            boolean launchMainActivity);
+            boolean launchMainActivity, in IBinder task, in Bundle options);
     void startActivityAsUserByIntent(in IApplicationThread caller, in String callingPackage,
             in String callingFeatureId, in Intent intent, int userId, in IBinder callingActivity,
             in Bundle options);
diff --git a/core/java/android/content/pm/IPackageManager.aidl b/core/java/android/content/pm/IPackageManager.aidl
index 0f236df..ca7d77b 100644
--- a/core/java/android/content/pm/IPackageManager.aidl
+++ b/core/java/android/content/pm/IPackageManager.aidl
@@ -781,7 +781,11 @@
 
     boolean isAutoRevokeWhitelisted(String packageName);
 
-    void grantImplicitAccess(int queryingUid, String visibleAuthority);
+    void makeProviderVisible(int recipientAppId, String visibleAuthority);
+
+    @JavaPassthrough(annotation = "@android.annotation.RequiresPermission(android.Manifest"
+            + ".permission.MAKE_UID_VISIBLE)")
+    void makeUidVisible(int recipientAppId, int visibleUid);
 
     IBinder getHoldLockToken();
 
diff --git a/core/java/android/content/pm/PackageInfoLite.java b/core/java/android/content/pm/PackageInfoLite.java
index 410e106..148eacc 100644
--- a/core/java/android/content/pm/PackageInfoLite.java
+++ b/core/java/android/content/pm/PackageInfoLite.java
@@ -79,6 +79,11 @@
     public boolean debuggable;
 
     /**
+     * Indicates if this apk is a sdk.
+     */
+    public boolean isSdkLibrary;
+
+    /**
      * Specifies the recommended install location. Can be one of
      * {@link InstallLocationUtils#RECOMMEND_INSTALL_INTERNAL} to install on internal storage,
      * {@link InstallLocationUtils#RECOMMEND_INSTALL_EXTERNAL} to install on external media,
diff --git a/core/java/android/content/pm/PackageInstaller.java b/core/java/android/content/pm/PackageInstaller.java
index 3bdef50..450e09a 100644
--- a/core/java/android/content/pm/PackageInstaller.java
+++ b/core/java/android/content/pm/PackageInstaller.java
@@ -2433,15 +2433,6 @@
         /** {@hide} */
         private static final int[] NO_SESSIONS = {};
 
-        /** @hide */
-        @IntDef(prefix = { "SESSION_" }, value = {
-                SESSION_NO_ERROR,
-                SESSION_VERIFICATION_FAILED,
-                SESSION_ACTIVATION_FAILED,
-                SESSION_UNKNOWN_ERROR,
-                SESSION_CONFLICT})
-        @Retention(RetentionPolicy.SOURCE)
-        public @interface SessionErrorCode {}
         /**
          * @deprecated use {@link #SESSION_NO_ERROR}.
          */
@@ -3125,7 +3116,7 @@
          * If something went wrong with a staged session, clients can check this error code to
          * understand which kind of failure happened. Only meaningful if {@code isStaged} is true.
          */
-        public @SessionErrorCode int getStagedSessionErrorCode() {
+        public int getStagedSessionErrorCode() {
             checkSessionIsStaged();
             return mSessionErrorCode;
         }
@@ -3140,7 +3131,7 @@
         }
 
         /** {@hide} */
-        public void setSessionErrorCode(@SessionErrorCode int errorCode, String errorMessage) {
+        public void setSessionErrorCode(int errorCode, String errorMessage) {
             mSessionErrorCode = errorCode;
             mSessionErrorMessage = errorMessage;
         }
diff --git a/core/java/android/content/pm/PackageManager.java b/core/java/android/content/pm/PackageManager.java
index f4bc161..81c941e 100644
--- a/core/java/android/content/pm/PackageManager.java
+++ b/core/java/android/content/pm/PackageManager.java
@@ -121,6 +121,9 @@
     /** {@hide} */
     public static final boolean APPLY_DEFAULT_TO_DEVICE_PROTECTED_STORAGE = true;
 
+    /** {@hide} */
+    public static final boolean ENABLE_SHARED_UID_MIGRATION = true;
+
     /**
      * This exception is thrown when a given package, application, or component
      * name cannot be found.
@@ -2202,6 +2205,14 @@
      */
     public static final int INSTALL_FAILED_BAD_PERMISSION_GROUP = -127;
 
+    /**
+     * Installation failed return code: an error occurred during the activation phase of this
+     * session.
+     *
+     * @hide
+     */
+    public static final int INSTALL_ACTIVATION_FAILED = -128;
+
     /** @hide */
     @IntDef(flag = true, prefix = { "DELETE_" }, value = {
             DELETE_KEEP_DATA,
@@ -4251,8 +4262,9 @@
      * for more details.
      * @hide
      */
+    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
     public static final String EXTRA_VERIFICATION_ROOT_HASH =
-            "android.content.pm.extra.EXTRA_VERIFICATION_ROOT_HASH";
+            "android.content.pm.extra.VERIFICATION_ROOT_HASH";
 
     /**
      * Extra field name for the ID of a intent filter pending verification.
@@ -10269,19 +10281,38 @@
     }
 
     /**
-     * Grants implicit visibility of the package that provides an authority to a querying UID.
+     * Makes a package that provides an authority {@code visibleAuthority} become visible to the
+     * application {@code recipientUid}.
      *
      * @throws SecurityException when called by a package other than the contacts provider
      * @hide
      */
-    public void grantImplicitAccess(int queryingUid, String visibleAuthority) {
+    public void makeProviderVisible(int recipientUid, String visibleAuthority) {
         try {
-            ActivityThread.getPackageManager().grantImplicitAccess(queryingUid, visibleAuthority);
+            ActivityThread.getPackageManager().makeProviderVisible(recipientUid, visibleAuthority);
         } catch (RemoteException e) {
             throw e.rethrowFromSystemServer();
         }
     }
 
+    /**
+     * Makes the package associated with the uid {@code visibleUid} become visible to the
+     * recipient uid application.
+     *
+     * @param recipientUid The uid of the application that is being given access to {@code
+     *                     visibleUid}
+     * @param visibleUid The uid of the application that is becoming accessible to {@code
+     *                   recipientAppId}
+     * @hide
+     */
+    @RequiresPermission(android.Manifest.permission.MAKE_UID_VISIBLE)
+    @TestApi
+    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
+    public void makeUidVisible(int recipientUid, int visibleUid) {
+        throw new UnsupportedOperationException(
+                "makeUidVisible not implemented in subclass");
+    }
+
     // Some of the flags don't affect the query result, but let's be conservative and cache
     // each combination of flags separately.
 
diff --git a/core/java/android/content/pm/PackageParser.java b/core/java/android/content/pm/PackageParser.java
index e914432..4d4a57d 100644
--- a/core/java/android/content/pm/PackageParser.java
+++ b/core/java/android/content/pm/PackageParser.java
@@ -1944,19 +1944,26 @@
         TypedArray sa = res.obtainAttributes(parser,
                 com.android.internal.R.styleable.AndroidManifest);
 
-        String str = sa.getNonConfigurationString(
-                com.android.internal.R.styleable.AndroidManifest_sharedUserId, 0);
-        if (str != null && str.length() > 0) {
-            String nameError = validateName(str, true, true);
-            if (nameError != null && !"android".equals(pkg.packageName)) {
-                outError[0] = "<manifest> specifies bad sharedUserId name \""
-                    + str + "\": " + nameError;
-                mParseError = PackageManager.INSTALL_PARSE_FAILED_BAD_SHARED_USER_ID;
-                return null;
+        int maxSdkVersion = 0;
+        if (PackageManager.ENABLE_SHARED_UID_MIGRATION) {
+            maxSdkVersion = sa.getInteger(
+                    com.android.internal.R.styleable.AndroidManifest_sharedUserMaxSdkVersion, 0);
+        }
+        if (maxSdkVersion == 0 || maxSdkVersion >= Build.VERSION.RESOURCES_SDK_INT) {
+            String str = sa.getNonConfigurationString(
+                    com.android.internal.R.styleable.AndroidManifest_sharedUserId, 0);
+            if (str != null && str.length() > 0) {
+                String nameError = validateName(str, true, true);
+                if (nameError != null && !"android".equals(pkg.packageName)) {
+                    outError[0] = "<manifest> specifies bad sharedUserId name \""
+                            + str + "\": " + nameError;
+                    mParseError = PackageManager.INSTALL_PARSE_FAILED_BAD_SHARED_USER_ID;
+                    return null;
+                }
+                pkg.mSharedUserId = str.intern();
+                pkg.mSharedUserLabel = sa.getResourceId(
+                        com.android.internal.R.styleable.AndroidManifest_sharedUserLabel, 0);
             }
-            pkg.mSharedUserId = str.intern();
-            pkg.mSharedUserLabel = sa.getResourceId(
-                    com.android.internal.R.styleable.AndroidManifest_sharedUserLabel, 0);
         }
 
         pkg.installLocation = sa.getInteger(
diff --git a/core/java/android/content/pm/Signature.java b/core/java/android/content/pm/Signature.java
index 3f5c5d2..d94b0d8 100644
--- a/core/java/android/content/pm/Signature.java
+++ b/core/java/android/content/pm/Signature.java
@@ -312,7 +312,7 @@
      * @hide
      */
     public static boolean areExactMatch(Signature[] a, Signature[] b) {
-        return (a.length == b.length) && ArrayUtils.containsAll(a, b)
+        return (ArrayUtils.size(a) == ArrayUtils.size(b)) && ArrayUtils.containsAll(a, b)
                 && ArrayUtils.containsAll(b, a);
     }
 
@@ -387,4 +387,4 @@
 
         return sPrime;
     }
-}
\ No newline at end of file
+}
diff --git a/core/java/android/content/pm/parsing/ApkLite.java b/core/java/android/content/pm/parsing/ApkLite.java
index 5ffb958..269bec2 100644
--- a/core/java/android/content/pm/parsing/ApkLite.java
+++ b/core/java/android/content/pm/parsing/ApkLite.java
@@ -133,6 +133,11 @@
      */
     private final boolean mHasDeviceAdminReceiver;
 
+    /**
+     * Indicates if this apk is a sdk.
+     */
+    private final boolean mIsSdkLibrary;
+
     public ApkLite(String path, String packageName, String splitName, boolean isFeatureSplit,
             String configForSplit, String usesSplitName, boolean isSplitRequired, int versionCode,
             int versionCodeMajor, int revisionCode, int installLocation,
@@ -143,7 +148,7 @@
             String requiredSystemPropertyName, String requiredSystemPropertyValue,
             int minSdkVersion, int targetSdkVersion, int rollbackDataPolicy,
             Set<String> requiredSplitTypes, Set<String> splitTypes,
-            boolean hasDeviceAdminReceiver) {
+            boolean hasDeviceAdminReceiver, boolean isSdkLibrary) {
         mPath = path;
         mPackageName = packageName;
         mSplitName = splitName;
@@ -176,6 +181,7 @@
         mTargetSdkVersion = targetSdkVersion;
         mRollbackDataPolicy = rollbackDataPolicy;
         mHasDeviceAdminReceiver = hasDeviceAdminReceiver;
+        mIsSdkLibrary = isSdkLibrary;
     }
 
     /**
@@ -473,11 +479,19 @@
         return mHasDeviceAdminReceiver;
     }
 
+    /**
+     * Indicates if this apk is a sdk.
+     */
+    @DataClass.Generated.Member
+    public boolean isIsSdkLibrary() {
+        return mIsSdkLibrary;
+    }
+
     @DataClass.Generated(
-            time = 1635266936769L,
+            time = 1643063342990L,
             codegenVersion = "1.0.23",
             sourceFile = "frameworks/base/core/java/android/content/pm/parsing/ApkLite.java",
-            inputSignatures = "private final @android.annotation.NonNull java.lang.String mPackageName\nprivate final @android.annotation.NonNull java.lang.String mPath\nprivate final @android.annotation.Nullable java.lang.String mSplitName\nprivate final @android.annotation.Nullable java.lang.String mUsesSplitName\nprivate final @android.annotation.Nullable java.lang.String mConfigForSplit\nprivate final @android.annotation.Nullable java.util.Set<java.lang.String> mRequiredSplitTypes\nprivate final @android.annotation.Nullable java.util.Set<java.lang.String> mSplitTypes\nprivate final  int mVersionCodeMajor\nprivate final  int mVersionCode\nprivate final  int mRevisionCode\nprivate final  int mInstallLocation\nprivate final  int mMinSdkVersion\nprivate final  int mTargetSdkVersion\nprivate final @android.annotation.NonNull android.content.pm.VerifierInfo[] mVerifiers\nprivate final @android.annotation.NonNull android.content.pm.SigningDetails mSigningDetails\nprivate final  boolean mFeatureSplit\nprivate final  boolean mIsolatedSplits\nprivate final  boolean mSplitRequired\nprivate final  boolean mCoreApp\nprivate final  boolean mDebuggable\nprivate final  boolean mProfileableByShell\nprivate final  boolean mMultiArch\nprivate final  boolean mUse32bitAbi\nprivate final  boolean mExtractNativeLibs\nprivate final  boolean mUseEmbeddedDex\nprivate final @android.annotation.Nullable java.lang.String mTargetPackageName\nprivate final  boolean mOverlayIsStatic\nprivate final  int mOverlayPriority\nprivate final @android.annotation.Nullable java.lang.String mRequiredSystemPropertyName\nprivate final @android.annotation.Nullable java.lang.String mRequiredSystemPropertyValue\nprivate final  int mRollbackDataPolicy\nprivate final  boolean mHasDeviceAdminReceiver\npublic  long getLongVersionCode()\nprivate  boolean hasAnyRequiredSplitTypes()\nclass ApkLite extends java.lang.Object implements []\n@com.android.internal.util.DataClass(genConstructor=false, genConstDefs=false)")
+            inputSignatures = "private final @android.annotation.NonNull java.lang.String mPackageName\nprivate final @android.annotation.NonNull java.lang.String mPath\nprivate final @android.annotation.Nullable java.lang.String mSplitName\nprivate final @android.annotation.Nullable java.lang.String mUsesSplitName\nprivate final @android.annotation.Nullable java.lang.String mConfigForSplit\nprivate final @android.annotation.Nullable java.util.Set<java.lang.String> mRequiredSplitTypes\nprivate final @android.annotation.Nullable java.util.Set<java.lang.String> mSplitTypes\nprivate final  int mVersionCodeMajor\nprivate final  int mVersionCode\nprivate final  int mRevisionCode\nprivate final  int mInstallLocation\nprivate final  int mMinSdkVersion\nprivate final  int mTargetSdkVersion\nprivate final @android.annotation.NonNull android.content.pm.VerifierInfo[] mVerifiers\nprivate final @android.annotation.NonNull android.content.pm.SigningDetails mSigningDetails\nprivate final  boolean mFeatureSplit\nprivate final  boolean mIsolatedSplits\nprivate final  boolean mSplitRequired\nprivate final  boolean mCoreApp\nprivate final  boolean mDebuggable\nprivate final  boolean mProfileableByShell\nprivate final  boolean mMultiArch\nprivate final  boolean mUse32bitAbi\nprivate final  boolean mExtractNativeLibs\nprivate final  boolean mUseEmbeddedDex\nprivate final @android.annotation.Nullable java.lang.String mTargetPackageName\nprivate final  boolean mOverlayIsStatic\nprivate final  int mOverlayPriority\nprivate final @android.annotation.Nullable java.lang.String mRequiredSystemPropertyName\nprivate final @android.annotation.Nullable java.lang.String mRequiredSystemPropertyValue\nprivate final  int mRollbackDataPolicy\nprivate final  boolean mHasDeviceAdminReceiver\nprivate final  boolean mIsSdkLibrary\npublic  long getLongVersionCode()\nprivate  boolean hasAnyRequiredSplitTypes()\nclass ApkLite extends java.lang.Object implements []\n@com.android.internal.util.DataClass(genConstructor=false, genConstDefs=false)")
     @Deprecated
     private void __metadata() {}
 
diff --git a/core/java/android/content/pm/parsing/ApkLiteParseUtils.java b/core/java/android/content/pm/parsing/ApkLiteParseUtils.java
index 165cae8..5680bcd 100644
--- a/core/java/android/content/pm/parsing/ApkLiteParseUtils.java
+++ b/core/java/android/content/pm/parsing/ApkLiteParseUtils.java
@@ -87,6 +87,7 @@
     private static final String TAG_USES_SDK = "uses-sdk";
     private static final String TAG_USES_SPLIT = "uses-split";
     private static final String TAG_MANIFEST = "manifest";
+    private static final String TAG_SDK_LIBRARY = "sdk-library";
     private static final int SDK_VERSION = Build.VERSION.SDK_INT;
     private static final String[] SDK_CODENAMES = Build.VERSION.ACTIVE_CODENAMES;
 
@@ -449,6 +450,8 @@
 
         boolean hasDeviceAdminReceiver = false;
 
+        boolean isSdkLibrary = false;
+
         // Only search the tree when the tag is the direct child of <manifest> tag
         int type;
         final int searchDepth = parser.getDepth() + 1;
@@ -506,6 +509,8 @@
                     } else if (TAG_RECEIVER.equals(parser.getName())) {
                         hasDeviceAdminReceiver |= isDeviceAdminReceiver(
                                 parser, hasBindDeviceAdminPermission);
+                    } else if (TAG_SDK_LIBRARY.equals(parser.getName())) {
+                        isSdkLibrary = true;
                     }
                 }
             } else if (TAG_OVERLAY.equals(parser.getName())) {
@@ -598,7 +603,7 @@
                         overlayIsStatic, overlayPriority, requiredSystemPropertyName,
                         requiredSystemPropertyValue, minSdkVersion, targetSdkVersion,
                         rollbackDataPolicy, requiredSplitTypes.first, requiredSplitTypes.second,
-                        hasDeviceAdminReceiver));
+                        hasDeviceAdminReceiver, isSdkLibrary));
     }
 
     private static boolean isDeviceAdminReceiver(
diff --git a/core/java/android/content/pm/parsing/FrameworkParsingPackageUtils.java b/core/java/android/content/pm/parsing/FrameworkParsingPackageUtils.java
index 8b86a16..a65b681 100644
--- a/core/java/android/content/pm/parsing/FrameworkParsingPackageUtils.java
+++ b/core/java/android/content/pm/parsing/FrameworkParsingPackageUtils.java
@@ -379,6 +379,30 @@
     }
 
     /**
+     * Computes the maxSdkVersion. If the package is not compatible with this platform, populates
+     * {@code outError[0]} with an error message.
+     * <p>
+     * {@code maxVers} is compared against {@code platformSdkVersion}. If {@code maxVers} is less
+     * than the {@code platformSdkVersion} then populates {@code outError[0]} with an error message.
+     * Otherwise, it returns {@code maxVers} unmodified.
+     *
+     * @param maxVers maxSdkVersion number, if specified in the application manifest, or {@code
+     *                Integer.MAX_VALUE} otherwise
+     * @param platformSdkVersion   platform SDK version number, typically Build.VERSION.SDK_INT
+     * @return the maxSdkVersion that was recognised or an error if the condition is not satisfied
+     */
+    public static ParseResult<Integer> computeMaxSdkVersion(@IntRange(from = 0) int maxVers,
+            @IntRange(from = 1) int platformSdkVersion, @NonNull ParseInput input) {
+        if (platformSdkVersion > maxVers) {
+            return input.error(PackageManager.INSTALL_FAILED_NEWER_SDK,
+                    "Requires max SDK version " + maxVers + " but is "
+                            + platformSdkVersion);
+        } else {
+            return input.success(maxVers);
+        }
+    }
+
+    /**
      * Matches a given {@code targetCode} against a set of release codeNames. Target codes can
      * either be of the form {@code [codename]}" (e.g {@code "Q"}) or of the form {@code
      * [codename].[fingerprint]} (e.g {@code "Q.cafebc561"}).
diff --git a/core/java/android/content/pm/parsing/PackageLite.java b/core/java/android/content/pm/parsing/PackageLite.java
index 5f5e812..e2789c9 100644
--- a/core/java/android/content/pm/parsing/PackageLite.java
+++ b/core/java/android/content/pm/parsing/PackageLite.java
@@ -105,6 +105,10 @@
      * or locally compiled variants.
      */
     private final boolean mUseEmbeddedDex;
+    /**
+     * Indicates if this package is a sdk.
+     */
+    private final boolean mIsSdkLibrary;
 
     public PackageLite(String path, String baseApkPath, ApkLite baseApk,
             String[] splitNames, boolean[] isFeatureSplits, String[] usesSplitNames,
@@ -131,6 +135,7 @@
         mRequiredSplitTypes = requiredSplitTypes;
         mSplitRequired = (baseApk.isSplitRequired() || hasAnyRequiredSplitTypes());
         mProfileableByShell = baseApk.isProfileableByShell();
+        mIsSdkLibrary = baseApk.isIsSdkLibrary();
         mSplitNames = splitNames;
         mSplitTypes = splitTypes;
         mIsFeatureSplits = isFeatureSplits;
@@ -401,11 +406,20 @@
         return mUseEmbeddedDex;
     }
 
+    /**
+     * Indicates if this package is a sdk.
+     */
+    @DataClass.Generated.Member
+    public boolean isIsSdkLibrary() {
+        return mIsSdkLibrary;
+    }
+
     @DataClass.Generated(
-            time = 1628562559343L,
+            time = 1643132127068L,
             codegenVersion = "1.0.23",
             sourceFile = "frameworks/base/core/java/android/content/pm/parsing/PackageLite.java",
-            inputSignatures = "private final @android.annotation.NonNull java.lang.String mPackageName\nprivate final @android.annotation.NonNull java.lang.String mPath\nprivate final @android.annotation.NonNull java.lang.String mBaseApkPath\nprivate final @android.annotation.Nullable java.lang.String[] mSplitApkPaths\nprivate final @android.annotation.Nullable java.lang.String[] mSplitNames\nprivate final @android.annotation.Nullable java.lang.String[] mUsesSplitNames\nprivate final @android.annotation.Nullable java.lang.String[] mConfigForSplit\nprivate final @android.annotation.Nullable java.util.Set<java.lang.String> mBaseRequiredSplitTypes\nprivate final @android.annotation.Nullable java.util.Set<java.lang.String>[] mRequiredSplitTypes\nprivate final @android.annotation.Nullable java.util.Set<java.lang.String>[] mSplitTypes\nprivate final  int mVersionCodeMajor\nprivate final  int mVersionCode\nprivate final  int mTargetSdk\nprivate final  int mBaseRevisionCode\nprivate final @android.annotation.Nullable int[] mSplitRevisionCodes\nprivate final  int mInstallLocation\nprivate final @android.annotation.NonNull android.content.pm.VerifierInfo[] mVerifiers\nprivate final @android.annotation.Nullable boolean[] mIsFeatureSplits\nprivate final  boolean mIsolatedSplits\nprivate final  boolean mSplitRequired\nprivate final  boolean mCoreApp\nprivate final  boolean mDebuggable\nprivate final  boolean mMultiArch\nprivate final  boolean mUse32bitAbi\nprivate final  boolean mExtractNativeLibs\nprivate final  boolean mProfileableByShell\nprivate final  boolean mUseEmbeddedDex\npublic  java.util.List<java.lang.String> getAllApkPaths()\npublic  long getLongVersionCode()\nprivate  boolean hasAnyRequiredSplitTypes()\nclass PackageLite extends java.lang.Object implements []\n@com.android.internal.util.DataClass(genConstructor=false, genConstDefs=false)")
+            inputSignatures =
+                    "private final @android.annotation.NonNull java.lang.String mPackageName\nprivate final @android.annotation.NonNull java.lang.String mPath\nprivate final @android.annotation.NonNull java.lang.String mBaseApkPath\nprivate final @android.annotation.Nullable java.lang.String[] mSplitApkPaths\nprivate final @android.annotation.Nullable java.lang.String[] mSplitNames\nprivate final @android.annotation.Nullable java.lang.String[] mUsesSplitNames\nprivate final @android.annotation.Nullable java.lang.String[] mConfigForSplit\nprivate final @android.annotation.Nullable java.util.Set<java.lang.String> mBaseRequiredSplitTypes\nprivate final @android.annotation.Nullable java.util.Set<java.lang.String>[] mRequiredSplitTypes\nprivate final @android.annotation.Nullable java.util.Set<java.lang.String>[] mSplitTypes\nprivate final  int mVersionCodeMajor\nprivate final  int mVersionCode\nprivate final  int mTargetSdk\nprivate final  int mBaseRevisionCode\nprivate final @android.annotation.Nullable int[] mSplitRevisionCodes\nprivate final  int mInstallLocation\nprivate final @android.annotation.NonNull android.content.pm.VerifierInfo[] mVerifiers\nprivate final @android.annotation.Nullable boolean[] mIsFeatureSplits\nprivate final  boolean mIsolatedSplits\nprivate final  boolean mSplitRequired\nprivate final  boolean mCoreApp\nprivate final  boolean mDebuggable\nprivate final  boolean mMultiArch\nprivate final  boolean mUse32bitAbi\nprivate final  boolean mExtractNativeLibs\nprivate final  boolean mProfileableByShell\nprivate final  boolean mUseEmbeddedDex\nprivate final  boolean mIsSdkLibrary\npublic  java.util.List<java.lang.String> getAllApkPaths()\npublic  long getLongVersionCode()\nprivate  boolean hasAnyRequiredSplitTypes()\nclass PackageLite extends java.lang.Object implements []\n@com.android.internal.util.DataClass(genConstructor=false, genConstDefs=false)")
     @Deprecated
     private void __metadata() {}
 
diff --git a/core/java/android/hardware/CameraSessionStats.java b/core/java/android/hardware/CameraSessionStats.java
index f34e2bf..698cc76 100644
--- a/core/java/android/hardware/CameraSessionStats.java
+++ b/core/java/android/hardware/CameraSessionStats.java
@@ -59,6 +59,7 @@
     private long mRequestCount;
     private long mResultErrorCount;
     private boolean mDeviceError;
+    private float mMaxPreviewFps;
     private ArrayList<CameraStreamStats> mStreamStats;
 
     public CameraSessionStats() {
@@ -67,6 +68,7 @@
         mApiLevel = -1;
         mIsNdk = false;
         mLatencyMs = -1;
+        mMaxPreviewFps = 0;
         mSessionType = -1;
         mInternalReconfigure = -1;
         mRequestCount = 0;
@@ -77,7 +79,7 @@
 
     public CameraSessionStats(String cameraId, int facing, int newCameraState,
             String clientName, int apiLevel, boolean isNdk, int creationDuration,
-            int sessionType, int internalReconfigure) {
+            float maxPreviewFps, int sessionType, int internalReconfigure) {
         mCameraId = cameraId;
         mFacing = facing;
         mNewCameraState = newCameraState;
@@ -85,6 +87,7 @@
         mApiLevel = apiLevel;
         mIsNdk = isNdk;
         mLatencyMs = creationDuration;
+        mMaxPreviewFps = maxPreviewFps;
         mSessionType = sessionType;
         mInternalReconfigure = internalReconfigure;
         mStreamStats = new ArrayList<CameraStreamStats>();
@@ -121,6 +124,7 @@
         dest.writeInt(mApiLevel);
         dest.writeBoolean(mIsNdk);
         dest.writeInt(mLatencyMs);
+        dest.writeFloat(mMaxPreviewFps);
         dest.writeInt(mSessionType);
         dest.writeInt(mInternalReconfigure);
         dest.writeLong(mRequestCount);
@@ -137,6 +141,7 @@
         mApiLevel = in.readInt();
         mIsNdk = in.readBoolean();
         mLatencyMs = in.readInt();
+        mMaxPreviewFps = in.readFloat();
         mSessionType = in.readInt();
         mInternalReconfigure = in.readInt();
         mRequestCount = in.readLong();
@@ -176,6 +181,10 @@
         return mLatencyMs;
     }
 
+    public float getMaxPreviewFps() {
+        return mMaxPreviewFps;
+    }
+
     public int getSessionType() {
         return mSessionType;
     }
diff --git a/core/java/android/hardware/CameraStreamStats.java b/core/java/android/hardware/CameraStreamStats.java
index 823d454..3952467 100644
--- a/core/java/android/hardware/CameraStreamStats.java
+++ b/core/java/android/hardware/CameraStreamStats.java
@@ -15,8 +15,8 @@
  */
 package android.hardware;
 
-import android.hardware.camera2.params.DynamicRangeProfiles;
 import android.hardware.camera2.CameraMetadata;
+import android.hardware.camera2.params.DynamicRangeProfiles;
 import android.os.Parcel;
 import android.os.Parcelable;
 import android.util.Log;
@@ -37,6 +37,7 @@
     private int mWidth;
     private int mHeight;
     private int mFormat;
+    private float mMaxPreviewFps;
     private int mDataSpace;
     private long mUsage;
     private long mRequestCount;
@@ -48,7 +49,7 @@
     private float[] mHistogramBins;
     private long[] mHistogramCounts;
     private long mDynamicRangeProfile;
-    private int mStreamUseCase;
+    private long mStreamUseCase;
 
     private static final String TAG = "CameraStreamStats";
 
@@ -56,6 +57,7 @@
         mWidth = 0;
         mHeight = 0;
         mFormat = 0;
+        mMaxPreviewFps = 0;
         mDataSpace = 0;
         mUsage = 0;
         mRequestCount = 0;
@@ -68,13 +70,14 @@
         mStreamUseCase = CameraMetadata.SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT;
     }
 
-    public CameraStreamStats(int width, int height, int format,
+    public CameraStreamStats(int width, int height, int format, float maxPreviewFps,
             int dataSpace, long usage, long requestCount, long errorCount,
             int startLatencyMs, int maxHalBuffers, int maxAppBuffers, long dynamicRangeProfile,
-            int streamUseCase) {
+            long streamUseCase) {
         mWidth = width;
         mHeight = height;
         mFormat = format;
+        mMaxPreviewFps = maxPreviewFps;
         mDataSpace = dataSpace;
         mUsage = usage;
         mRequestCount = requestCount;
@@ -120,6 +123,7 @@
         dest.writeInt(mWidth);
         dest.writeInt(mHeight);
         dest.writeInt(mFormat);
+        dest.writeFloat(mMaxPreviewFps);
         dest.writeInt(mDataSpace);
         dest.writeLong(mUsage);
         dest.writeLong(mRequestCount);
@@ -131,13 +135,14 @@
         dest.writeFloatArray(mHistogramBins);
         dest.writeLongArray(mHistogramCounts);
         dest.writeLong(mDynamicRangeProfile);
-        dest.writeInt(mStreamUseCase);
+        dest.writeLong(mStreamUseCase);
     }
 
     public void readFromParcel(Parcel in) {
         mWidth = in.readInt();
         mHeight = in.readInt();
         mFormat = in.readInt();
+        mMaxPreviewFps = in.readFloat();
         mDataSpace = in.readInt();
         mUsage = in.readLong();
         mRequestCount = in.readLong();
@@ -149,7 +154,7 @@
         mHistogramBins = in.createFloatArray();
         mHistogramCounts = in.createLongArray();
         mDynamicRangeProfile = in.readLong();
-        mStreamUseCase = in.readInt();
+        mStreamUseCase = in.readLong();
     }
 
     public int getWidth() {
@@ -164,6 +169,10 @@
         return mFormat;
     }
 
+    public float getMaxPreviewFps() {
+        return mMaxPreviewFps;
+    }
+
     public int getDataSpace() {
         return mDataSpace;
     }
@@ -208,7 +217,7 @@
         return mDynamicRangeProfile;
     }
 
-    public int getStreamUseCase() {
+    public long getStreamUseCase() {
         return mStreamUseCase;
     }
 }
diff --git a/core/java/android/hardware/camera2/CameraCharacteristics.java b/core/java/android/hardware/camera2/CameraCharacteristics.java
index 7bebe1f..b05e6d1 100644
--- a/core/java/android/hardware/camera2/CameraCharacteristics.java
+++ b/core/java/android/hardware/camera2/CameraCharacteristics.java
@@ -3563,8 +3563,8 @@
      */
     @PublicKey
     @NonNull
-    public static final Key<int[]> SCALER_AVAILABLE_STREAM_USE_CASES =
-            new Key<int[]>("android.scaler.availableStreamUseCases", int[].class);
+    public static final Key<long[]> SCALER_AVAILABLE_STREAM_USE_CASES =
+            new Key<long[]>("android.scaler.availableStreamUseCases", long[].class);
 
     /**
      * <p>An array of mandatory stream combinations with stream use cases.
diff --git a/core/java/android/hardware/camera2/params/MandatoryStreamCombination.java b/core/java/android/hardware/camera2/params/MandatoryStreamCombination.java
index 465abfb..a3bc665 100644
--- a/core/java/android/hardware/camera2/params/MandatoryStreamCombination.java
+++ b/core/java/android/hardware/camera2/params/MandatoryStreamCombination.java
@@ -66,7 +66,7 @@
         private final boolean mIsUltraHighResolution;
         private final boolean mIsMaximumSize;
         private final boolean mIs10BitCapable;
-        private final int mStreamUseCase;
+        private final long mStreamUseCase;
 
         /**
          * Create a new {@link MandatoryStreamInformation}.
@@ -168,7 +168,7 @@
          */
         public MandatoryStreamInformation(@NonNull List<Size> availableSizes, @Format int format,
                 boolean isMaximumSize, boolean isInput, boolean isUltraHighResolution,
-                boolean is10BitCapable, @StreamUseCase int streamUseCase) {
+                boolean is10BitCapable, @StreamUseCase long streamUseCase) {
             if (availableSizes.isEmpty()) {
                 throw new IllegalArgumentException("No available sizes");
             }
@@ -308,9 +308,9 @@
          * For {@link MandatoryStreamInformation} belonging to other mandatory stream
          * combinations, the return value will be DEFAULT. </p>
          *
-         * @return the integer stream use case.
+         * @return the long integer stream use case.
          */
-        public @StreamUseCase int getStreamUseCase() {
+        public @StreamUseCase long getStreamUseCase() {
             return mStreamUseCase;
         }
 
@@ -365,15 +365,15 @@
     /**
      * Short hand for stream use cases
      */
-    private static final int STREAM_USE_CASE_PREVIEW =
+    private static final long STREAM_USE_CASE_PREVIEW =
             CameraMetadata.SCALER_AVAILABLE_STREAM_USE_CASES_PREVIEW;
-    private static final int STREAM_USE_CASE_STILL_CAPTURE =
+    private static final long STREAM_USE_CASE_STILL_CAPTURE =
             CameraMetadata.SCALER_AVAILABLE_STREAM_USE_CASES_STILL_CAPTURE;
-    private static final int STREAM_USE_CASE_RECORD =
+    private static final long STREAM_USE_CASE_RECORD =
             CameraMetadata.SCALER_AVAILABLE_STREAM_USE_CASES_VIDEO_RECORD;
-    private static final int STREAM_USE_CASE_PREVIEW_VIDEO_STILL =
+    private static final long STREAM_USE_CASE_PREVIEW_VIDEO_STILL =
             CameraMetadata.SCALER_AVAILABLE_STREAM_USE_CASES_PREVIEW_VIDEO_STILL;
-    private static final int STREAM_USE_CASE_VIDEO_CALL =
+    private static final long STREAM_USE_CASE_VIDEO_CALL =
             CameraMetadata.SCALER_AVAILABLE_STREAM_USE_CASES_VIDEO_CALL;
 
     /**
@@ -471,12 +471,12 @@
     private static final class StreamTemplate {
         public int mFormat;
         public SizeThreshold mSizeThreshold;
-        public int mStreamUseCase;
+        public long mStreamUseCase;
         public StreamTemplate(int format, SizeThreshold sizeThreshold) {
             this(format, sizeThreshold, CameraMetadata.SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT);
         }
         public StreamTemplate(@Format int format, @NonNull SizeThreshold sizeThreshold,
-                @StreamUseCase int streamUseCase) {
+                @StreamUseCase long streamUseCase) {
             mFormat = format;
             mSizeThreshold = sizeThreshold;
             mStreamUseCase = streamUseCase;
diff --git a/core/java/android/hardware/camera2/params/OutputConfiguration.java b/core/java/android/hardware/camera2/params/OutputConfiguration.java
index 2350b7c..39cb7f3 100644
--- a/core/java/android/hardware/camera2/params/OutputConfiguration.java
+++ b/core/java/android/hardware/camera2/params/OutputConfiguration.java
@@ -918,9 +918,9 @@
      * @throws IllegalArgumentException If the streamUseCase isn't within the range of valid
      *                                  values.
      */
-    public void setStreamUseCase(@StreamUseCase int streamUseCase) {
+    public void setStreamUseCase(@StreamUseCase long streamUseCase) {
         // Verify that the value is in range
-        int maxUseCaseValue = CameraMetadata.SCALER_AVAILABLE_STREAM_USE_CASES_VIDEO_CALL;
+        long maxUseCaseValue = CameraMetadata.SCALER_AVAILABLE_STREAM_USE_CASES_VIDEO_CALL;
         if (streamUseCase > maxUseCaseValue &&
                 streamUseCase < CameraMetadata.SCALER_AVAILABLE_STREAM_USE_CASES_VENDOR_START) {
             throw new IllegalArgumentException("Not a valid stream use case value " +
@@ -938,7 +938,7 @@
      *
      * @return the currently set stream use case
      */
-    public int getStreamUseCase() {
+    public long getStreamUseCase() {
         return mStreamUseCase;
     }
 
@@ -1067,7 +1067,7 @@
         String physicalCameraId = source.readString();
         boolean isMultiResolutionOutput = source.readInt() == 1;
         int[] sensorPixelModesUsed = source.createIntArray();
-        int streamUseCase = source.readInt();
+        long streamUseCase = source.readLong();
 
         checkArgumentInRange(rotation, ROTATION_0, ROTATION_270, "Rotation constant");
         long dynamicRangeProfile = source.readLong();
@@ -1218,7 +1218,7 @@
         // writeList doesn't seem to work well with Integer list.
         dest.writeIntArray(convertIntegerToIntList(mSensorPixelModesUsed));
         dest.writeLong(mDynamicRangeProfile);
-        dest.writeInt(mStreamUseCase);
+        dest.writeLong(mStreamUseCase);
         dest.writeInt(mTimestampBase);
         dest.writeInt(mMirrorMode);
     }
@@ -1337,7 +1337,7 @@
     // Dynamic range profile
     private long mDynamicRangeProfile;
     // Stream use case
-    private int mStreamUseCase;
+    private long mStreamUseCase;
     // Timestamp base
     private int mTimestampBase;
     // Mirroring mode
diff --git a/core/java/android/hardware/camera2/params/StreamConfigurationMap.java b/core/java/android/hardware/camera2/params/StreamConfigurationMap.java
index d9734b4..5981d27 100644
--- a/core/java/android/hardware/camera2/params/StreamConfigurationMap.java
+++ b/core/java/android/hardware/camera2/params/StreamConfigurationMap.java
@@ -781,10 +781,11 @@
      * <li>The fpsMin and fpsMax will be a multiple 30fps.</li>
      * <li>The fpsMin will be no less than 30fps, the fpsMax will be no less than 120fps.</li>
      * <li>At least one range will be a fixed FPS range where fpsMin == fpsMax.</li>
-     * <li>For each fixed FPS range, there will be one corresponding variable FPS range [30,
-     * fps_max]. These kinds of FPS ranges are suitable for preview-only use cases where the
-     * application doesn't want the camera device always produce higher frame rate than the display
-     * refresh rate.</li>
+     * <li>For each fixed FPS range, there will be one corresponding variable FPS range
+     * [30, fps_max] or [60, fps_max]. These kinds of FPS ranges are suitable for preview-only
+     * use cases where the application doesn't want the camera device always produce higher frame
+     * rate than the display refresh rate. Both 30fps and 60fps preview rate will not be
+     * supported for the same recording rate.</li>
      * </p>
      *
      * @return an array of supported high speed video recording FPS ranges The upper bound of
diff --git a/core/java/android/inputmethodservice/ImsConfigurationTracker.java b/core/java/android/inputmethodservice/ImsConfigurationTracker.java
index 3c78888..30ef0a2 100644
--- a/core/java/android/inputmethodservice/ImsConfigurationTracker.java
+++ b/core/java/android/inputmethodservice/ImsConfigurationTracker.java
@@ -63,8 +63,9 @@
      */
     @MainThread
     public void onBindInput(@Nullable Resources resources) {
-        Preconditions.checkState(mInitialized,
-                "onBindInput can be called only after onInitialize().");
+        if (!mInitialized) {
+            return;
+        }
         if (mLastKnownConfig == null && resources != null) {
             mLastKnownConfig = new Configuration(resources.getConfiguration());
         }
diff --git a/core/java/android/inputmethodservice/InkWindow.java b/core/java/android/inputmethodservice/InkWindow.java
index 499634a..8289c26 100644
--- a/core/java/android/inputmethodservice/InkWindow.java
+++ b/core/java/android/inputmethodservice/InkWindow.java
@@ -27,6 +27,8 @@
 import android.os.IBinder;
 import android.util.Slog;
 import android.view.View;
+import android.view.ViewGroup;
+import android.view.ViewTreeObserver;
 import android.view.WindowManager;
 
 import com.android.internal.policy.PhoneWindow;
@@ -40,6 +42,9 @@
 
     private final WindowManager mWindowManager;
     private boolean mIsViewAdded;
+    private View mInkView;
+    private InkVisibilityListener mInkViewVisibilityListener;
+    private ViewTreeObserver.OnGlobalLayoutListener mGlobalLayoutListener;
 
     public InkWindow(@NonNull Context context) {
         super(context);
@@ -102,4 +107,77 @@
         lp.token = token;
         setAttributes(lp);
     }
+
+    @Override
+    public void addContentView(View view, ViewGroup.LayoutParams params) {
+        if (mInkView == null) {
+            mInkView = view;
+        } else if (mInkView != view) {
+            throw new IllegalStateException("Only one Child Inking view is permitted.");
+        }
+        super.addContentView(view, params);
+        initInkViewVisibilityListener();
+    }
+
+    @Override
+    public void setContentView(View view, ViewGroup.LayoutParams params) {
+        mInkView = view;
+        super.setContentView(view, params);
+        initInkViewVisibilityListener();
+    }
+
+    @Override
+    public void setContentView(View view) {
+        mInkView = view;
+        super.setContentView(view);
+        initInkViewVisibilityListener();
+    }
+
+    @Override
+    public void clearContentView() {
+        if (mGlobalLayoutListener != null && mInkView != null) {
+            mInkView.getViewTreeObserver().removeOnGlobalLayoutListener(mGlobalLayoutListener);
+        }
+        mGlobalLayoutListener = null;
+        mInkView = null;
+        super.clearContentView();
+    }
+
+    /**
+    * Listener used by InkWindow to time the dispatching of {@link MotionEvent}s to Ink view, once
+    * it is visible to user.
+    */
+    interface InkVisibilityListener {
+        void onInkViewVisible();
+    }
+
+    void setInkViewVisibilityListener(InkVisibilityListener listener) {
+        mInkViewVisibilityListener = listener;
+        initInkViewVisibilityListener();
+    }
+
+    void initInkViewVisibilityListener() {
+        if (mInkView == null || mInkViewVisibilityListener == null
+                || mGlobalLayoutListener != null) {
+            return;
+        }
+        mGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
+            @Override
+            public void onGlobalLayout() {
+                if (mInkView.isVisibleToUser()) {
+                    if (mInkViewVisibilityListener != null) {
+                        mInkViewVisibilityListener.onInkViewVisible();
+                    }
+                    mInkView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
+                    mGlobalLayoutListener = null;
+                }
+            }
+        };
+        mInkView.getViewTreeObserver().addOnGlobalLayoutListener(mGlobalLayoutListener);
+    }
+
+    boolean isInkViewVisible() {
+        return getDecorView().getVisibility() == View.VISIBLE
+                && mInkView != null && mInkView.isVisibleToUser();
+    }
 }
diff --git a/core/java/android/inputmethodservice/InputMethodService.java b/core/java/android/inputmethodservice/InputMethodService.java
index 656aea1..b46bb32 100644
--- a/core/java/android/inputmethodservice/InputMethodService.java
+++ b/core/java/android/inputmethodservice/InputMethodService.java
@@ -143,6 +143,7 @@
 import com.android.internal.inputmethod.InputMethodNavButtonFlags;
 import com.android.internal.inputmethod.InputMethodPrivilegedOperations;
 import com.android.internal.inputmethod.InputMethodPrivilegedOperationsRegistry;
+import com.android.internal.util.RingBuffer;
 import com.android.internal.view.IInlineSuggestionsRequestCallback;
 import com.android.internal.view.IInputContext;
 import com.android.internal.view.InlineSuggestionsRequestInfo;
@@ -334,6 +335,17 @@
             "persist.sys.ime.can_render_gestural_nav_buttons";
 
     /**
+     * Number of {@link MotionEvent} to buffer if IME is not ready with Ink view.
+     * This number may be configured eventually based on device's touch sampling frequency.
+     */
+    private static final int MAX_EVENTS_BUFFER = 500;
+
+    /**
+     * A circular buffer of size MAX_EVENTS_BUFFER in case IME is taking too long to add ink view.
+     **/
+    private RingBuffer<MotionEvent> mPendingEvents;
+
+    /**
      * Returns whether {@link InputMethodService} is responsible for rendering the back button and
      * the IME switcher button or not when the gestural navigation is enabled.
      *
@@ -954,7 +966,8 @@
             mInkWindow.show();
 
             // deliver previous @param stylusEvents
-            stylusEvents.forEach(mInkWindow.getDecorView()::dispatchTouchEvent);
+            stylusEvents.forEach(InputMethodService.this::onStylusHandwritingMotionEvent);
+
             // create receiver for channel
             mHandwritingEventReceiver = new SimpleBatchedInputEventReceiver(
                     channel,
@@ -963,11 +976,11 @@
                         if (!(event instanceof MotionEvent)) {
                             return false;
                         }
-                        return mInkWindow.getDecorView().dispatchTouchEvent((MotionEvent) event);
+                        onStylusHandwritingMotionEvent((MotionEvent) event);
+                        return true;
                     });
         }
 
-
         /**
          * {@inheritDoc}
          * @hide
@@ -2357,7 +2370,8 @@
      *
      * If the IME supports handwriting for the current input, it should return {@code true},
      * ensure its inking views are attached to the {@link #getStylusHandwritingWindow()}, and handle
-     * stylus input received on the ink window via {@link #getCurrentInputConnection()}.
+     * stylus input received from {@link #onStylusHandwritingMotionEvent(MotionEvent)} on the
+     * {@link #getStylusHandwritingWindow()} via {@link #getCurrentInputConnection()}.
      * @return {@code true} if IME can honor the request, {@code false} if IME cannot at this time.
      */
     public boolean onStartStylusHandwriting() {
@@ -2366,6 +2380,33 @@
     }
 
     /**
+     * Called after {@link #onStartStylusHandwriting()} returns {@code true} for every Stylus
+     * {@link MotionEvent}.
+     * By default, this method forwards all {@link MotionEvent}s to the
+     * {@link #getStylusHandwritingWindow()} once its visible, however IME can override it to
+     * receive them sooner.
+     * @param motionEvent {@link MotionEvent} from stylus.
+     */
+    public void onStylusHandwritingMotionEvent(@NonNull MotionEvent motionEvent) {
+        if (mInkWindow.isInkViewVisible()) {
+            mInkWindow.getDecorView().dispatchTouchEvent(motionEvent);
+        } else {
+            if (mPendingEvents == null) {
+                mPendingEvents = new RingBuffer(MotionEvent.class, MAX_EVENTS_BUFFER);
+            }
+            mPendingEvents.append(motionEvent);
+            mInkWindow.setInkViewVisibilityListener(() -> {
+                if (mPendingEvents != null && !mPendingEvents.isEmpty()) {
+                    for (MotionEvent event : mPendingEvents.toArray()) {
+                        mInkWindow.getDecorView().dispatchTouchEvent(event);
+                    }
+                    mPendingEvents.clear();
+                }
+            });
+        }
+    }
+
+    /**
      * Called when the current stylus handwriting session was finished (either by the system or
      * via {@link #finishStylusHandwriting()}.
      *
diff --git a/core/java/android/inputmethodservice/navigationbar/ButtonDispatcher.java b/core/java/android/inputmethodservice/navigationbar/ButtonDispatcher.java
index 3f26fa4..6b2db7d 100644
--- a/core/java/android/inputmethodservice/navigationbar/ButtonDispatcher.java
+++ b/core/java/android/inputmethodservice/navigationbar/ButtonDispatcher.java
@@ -67,7 +67,7 @@
         }
     };
 
-    public ButtonDispatcher(int id) {
+    ButtonDispatcher(int id) {
         mId = id;
     }
 
@@ -125,8 +125,8 @@
 
     public void setImageDrawable(KeyButtonDrawable drawable) {
         mImageDrawable = drawable;
-        final int N = mViews.size();
-        for (int i = 0; i < N; i++) {
+        final int numViews = mViews.size();
+        for (int i = 0; i < numViews; i++) {
             if (mViews.get(i) instanceof ButtonInterface) {
                 ((ButtonInterface) mViews.get(i)).setImageDrawable(mImageDrawable);
             }
@@ -143,8 +143,8 @@
         }
 
         mVisibility = visibility;
-        final int N = mViews.size();
-        for (int i = 0; i < N; i++) {
+        final int numViews = mViews.size();
+        for (int i = 0; i < numViews; i++) {
             mViews.get(i).setVisibility(mVisibility);
         }
     }
@@ -188,8 +188,8 @@
             int nextAlpha = (int) (alpha * 255);
             if (prevAlpha != nextAlpha) {
                 mAlpha = nextAlpha / 255f;
-                final int N = mViews.size();
-                for (int i = 0; i < N; i++) {
+                final int numViews = mViews.size();
+                for (int i = 0; i < numViews; i++) {
                     mViews.get(i).setAlpha(mAlpha);
                 }
             }
@@ -198,8 +198,8 @@
 
     public void setDarkIntensity(float darkIntensity) {
         mDarkIntensity = darkIntensity;
-        final int N = mViews.size();
-        for (int i = 0; i < N; i++) {
+        final int numViews = mViews.size();
+        for (int i = 0; i < numViews; i++) {
             if (mViews.get(i) instanceof ButtonInterface) {
                 ((ButtonInterface) mViews.get(i)).setDarkIntensity(darkIntensity);
             }
@@ -208,8 +208,8 @@
 
     public void setDelayTouchFeedback(boolean delay) {
         mDelayTouchFeedback = delay;
-        final int N = mViews.size();
-        for (int i = 0; i < N; i++) {
+        final int numViews = mViews.size();
+        for (int i = 0; i < numViews; i++) {
             if (mViews.get(i) instanceof ButtonInterface) {
                 ((ButtonInterface) mViews.get(i)).setDelayTouchFeedback(delay);
             }
@@ -218,55 +218,55 @@
 
     public void setOnClickListener(View.OnClickListener clickListener) {
         mClickListener = clickListener;
-        final int N = mViews.size();
-        for (int i = 0; i < N; i++) {
+        final int numViews = mViews.size();
+        for (int i = 0; i < numViews; i++) {
             mViews.get(i).setOnClickListener(mClickListener);
         }
     }
 
     public void setOnTouchListener(View.OnTouchListener touchListener) {
         mTouchListener = touchListener;
-        final int N = mViews.size();
-        for (int i = 0; i < N; i++) {
+        final int numViews = mViews.size();
+        for (int i = 0; i < numViews; i++) {
             mViews.get(i).setOnTouchListener(mTouchListener);
         }
     }
 
     public void setLongClickable(boolean isLongClickable) {
         mLongClickable = isLongClickable;
-        final int N = mViews.size();
-        for (int i = 0; i < N; i++) {
+        final int numViews = mViews.size();
+        for (int i = 0; i < numViews; i++) {
             mViews.get(i).setLongClickable(mLongClickable);
         }
     }
 
     public void setOnLongClickListener(View.OnLongClickListener longClickListener) {
         mLongClickListener = longClickListener;
-        final int N = mViews.size();
-        for (int i = 0; i < N; i++) {
+        final int numViews = mViews.size();
+        for (int i = 0; i < numViews; i++) {
             mViews.get(i).setOnLongClickListener(mLongClickListener);
         }
     }
 
     public void setOnHoverListener(View.OnHoverListener hoverListener) {
         mOnHoverListener = hoverListener;
-        final int N = mViews.size();
-        for (int i = 0; i < N; i++) {
+        final int numViews = mViews.size();
+        for (int i = 0; i < numViews; i++) {
             mViews.get(i).setOnHoverListener(mOnHoverListener);
         }
     }
 
     public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
         mAccessibilityDelegate = delegate;
-        final int N = mViews.size();
-        for (int i = 0; i < N; i++) {
+        final int numViews = mViews.size();
+        for (int i = 0; i < numViews; i++) {
             mViews.get(i).setAccessibilityDelegate(delegate);
         }
     }
 
     public void setTranslation(int x, int y, int z) {
-        final int N = mViews.size();
-        for (int i = 0; i < N; i++) {
+        final int numViews = mViews.size();
+        for (int i = 0; i < numViews; i++) {
             final View view = mViews.get(i);
             view.setTranslationX(x);
             view.setTranslationY(y);
diff --git a/core/java/android/inputmethodservice/navigationbar/DeadZone.java b/core/java/android/inputmethodservice/navigationbar/DeadZone.java
index 4adc84b..4cfd813 100644
--- a/core/java/android/inputmethodservice/navigationbar/DeadZone.java
+++ b/core/java/android/inputmethodservice/navigationbar/DeadZone.java
@@ -82,7 +82,7 @@
         }
     };
 
-    public DeadZone(NavigationBarView view) {
+    DeadZone(NavigationBarView view) {
         mNavigationBarView = view;
         onConfigurationChanged(Surface.ROTATION_0);
     }
@@ -92,13 +92,15 @@
     }
 
     private float getSize(long now) {
-        if (mSizeMax == 0)
+        if (mSizeMax == 0) {
             return 0;
+        }
         long dt = (now - mLastPokeTime);
-        if (dt > mHold + mDecay)
+        if (dt > mHold + mDecay) {
             return mSizeMin;
-        if (dt < mHold)
+        } else if (dt < mHold) {
             return mSizeMax;
+        }
         return (int) lerp(mSizeMax, mSizeMin, (float) (dt - mHold) / mDecay);
     }
 
@@ -177,8 +179,9 @@
 
     private void poke(MotionEvent event) {
         mLastPokeTime = event.getEventTime();
-        if (DEBUG)
+        if (DEBUG) {
             Log.v(TAG, "poked! size=" + getSize(mLastPokeTime));
+        }
         if (mShouldFlash) mNavigationBarView.postInvalidate();
     }
 
diff --git a/core/java/android/inputmethodservice/navigationbar/KeyButtonDrawable.java b/core/java/android/inputmethodservice/navigationbar/KeyButtonDrawable.java
index 25a443d..45c8a18 100644
--- a/core/java/android/inputmethodservice/navigationbar/KeyButtonDrawable.java
+++ b/core/java/android/inputmethodservice/navigationbar/KeyButtonDrawable.java
@@ -54,30 +54,30 @@
 final class KeyButtonDrawable extends Drawable {
 
     public static final FloatProperty<KeyButtonDrawable> KEY_DRAWABLE_ROTATE =
-        new FloatProperty<KeyButtonDrawable>("KeyButtonRotation") {
-            @Override
-            public void setValue(KeyButtonDrawable drawable, float degree) {
-                drawable.setRotation(degree);
-            }
+            new FloatProperty<KeyButtonDrawable>("KeyButtonRotation") {
+                @Override
+                public void setValue(KeyButtonDrawable drawable, float degree) {
+                    drawable.setRotation(degree);
+                }
 
-            @Override
-            public Float get(KeyButtonDrawable drawable) {
-                return drawable.getRotation();
-            }
-        };
+                @Override
+                public Float get(KeyButtonDrawable drawable) {
+                    return drawable.getRotation();
+                }
+            };
 
     public static final FloatProperty<KeyButtonDrawable> KEY_DRAWABLE_TRANSLATE_Y =
-        new FloatProperty<KeyButtonDrawable>("KeyButtonTranslateY") {
-            @Override
-            public void setValue(KeyButtonDrawable drawable, float y) {
-                drawable.setTranslationY(y);
-            }
+            new FloatProperty<KeyButtonDrawable>("KeyButtonTranslateY") {
+                @Override
+                public void setValue(KeyButtonDrawable drawable, float y) {
+                    drawable.setTranslationY(y);
+                }
 
-            @Override
-            public Float get(KeyButtonDrawable drawable) {
-                return drawable.getTranslationY();
-            }
-        };
+                @Override
+                public Float get(KeyButtonDrawable drawable) {
+                    return drawable.getTranslationY();
+                }
+            };
 
     private final Paint mIconPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
     private final Paint mShadowPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
@@ -100,7 +100,7 @@
         }
     };
 
-    public KeyButtonDrawable(Drawable d, @ColorInt int lightColor, @ColorInt int darkColor,
+    KeyButtonDrawable(Drawable d, @ColorInt int lightColor, @ColorInt int darkColor,
             boolean horizontalFlip, Color ovalBackgroundColor) {
         this(d, new ShadowDrawableState(lightColor, darkColor,
                 d instanceof AnimatedVectorDrawable, horizontalFlip, ovalBackgroundColor));
@@ -433,8 +433,8 @@
         final boolean mSupportsAnimation;
         final Color mOvalBackgroundColor;
 
-        public ShadowDrawableState(@ColorInt int lightColor, @ColorInt int darkColor,
-                boolean animated, boolean horizontalFlip, Color ovalBackgroundColor) {
+        ShadowDrawableState(@ColorInt int lightColor, @ColorInt int darkColor, boolean animated,
+                boolean horizontalFlip, Color ovalBackgroundColor) {
             mLightColor = lightColor;
             mDarkColor = darkColor;
             mSupportsAnimation = animated;
diff --git a/core/java/android/inputmethodservice/navigationbar/KeyButtonRipple.java b/core/java/android/inputmethodservice/navigationbar/KeyButtonRipple.java
index 38a63b6..cf77c898 100644
--- a/core/java/android/inputmethodservice/navigationbar/KeyButtonRipple.java
+++ b/core/java/android/inputmethodservice/navigationbar/KeyButtonRipple.java
@@ -90,7 +90,7 @@
 
     private Type mType = Type.ROUNDED_RECT;
 
-    public KeyButtonRipple(Context ctx, View targetView, @DimenRes int maxWidthResource) {
+    KeyButtonRipple(Context ctx, View targetView, @DimenRes int maxWidthResource) {
         mMaxWidthResource = maxWidthResource;
         mMaxWidth = ctx.getResources().getDimensionPixelSize(maxWidthResource);
         mTargetView = targetView;
@@ -126,7 +126,7 @@
     private void drawSoftware(Canvas canvas) {
         if (mGlowAlpha > 0f) {
             final Paint p = getRipplePaint();
-            p.setAlpha((int)(mGlowAlpha * 255f));
+            p.setAlpha((int) (mGlowAlpha * 255f));
 
             final float w = getBounds().width();
             final float h = getBounds().height();
@@ -412,7 +412,7 @@
         mDrawingHardwareGlow = true;
         setExtendStart(CanvasProperty.createFloat(getExtendSize() / 2));
         final RenderNodeAnimator startAnim = new RenderNodeAnimator(getExtendStart(),
-                getExtendSize()/2 - GLOW_MAX_SCALE_FACTOR * getRippleSize()/2);
+                getExtendSize() / 2 - GLOW_MAX_SCALE_FACTOR * getRippleSize() / 2);
         startAnim.setDuration(ANIMATION_DURATION_SCALE);
         startAnim.setInterpolator(mInterpolator);
         startAnim.addListener(mAnimatorListener);
@@ -420,7 +420,7 @@
 
         setExtendEnd(CanvasProperty.createFloat(getExtendSize() / 2));
         final RenderNodeAnimator endAnim = new RenderNodeAnimator(getExtendEnd(),
-                getExtendSize()/2 + GLOW_MAX_SCALE_FACTOR * getRippleSize()/2);
+                getExtendSize() / 2 + GLOW_MAX_SCALE_FACTOR * getRippleSize() / 2);
         endAnim.setDuration(ANIMATION_DURATION_SCALE);
         endAnim.setInterpolator(mInterpolator);
         endAnim.addListener(mAnimatorListener);
@@ -430,13 +430,13 @@
         if (isHorizontal()) {
             mTopProp = CanvasProperty.createFloat(0f);
             mBottomProp = CanvasProperty.createFloat(getBounds().height());
-            mRxProp = CanvasProperty.createFloat(getBounds().height()/2);
-            mRyProp = CanvasProperty.createFloat(getBounds().height()/2);
+            mRxProp = CanvasProperty.createFloat(getBounds().height() / 2);
+            mRyProp = CanvasProperty.createFloat(getBounds().height() / 2);
         } else {
             mLeftProp = CanvasProperty.createFloat(0f);
             mRightProp = CanvasProperty.createFloat(getBounds().width());
-            mRxProp = CanvasProperty.createFloat(getBounds().width()/2);
-            mRyProp = CanvasProperty.createFloat(getBounds().width()/2);
+            mRxProp = CanvasProperty.createFloat(getBounds().width() / 2);
+            mRyProp = CanvasProperty.createFloat(getBounds().width() / 2);
         }
 
         mGlowScale = GLOW_MAX_SCALE_FACTOR;
diff --git a/core/java/android/inputmethodservice/navigationbar/KeyButtonView.java b/core/java/android/inputmethodservice/navigationbar/KeyButtonView.java
index 74d30f8..cfdb6ca 100644
--- a/core/java/android/inputmethodservice/navigationbar/KeyButtonView.java
+++ b/core/java/android/inputmethodservice/navigationbar/KeyButtonView.java
@@ -200,8 +200,8 @@
                 postDelayed(mCheckLongPress, ViewConfiguration.getLongPressTimeout());
                 break;
             case MotionEvent.ACTION_MOVE:
-                x = (int)ev.getRawX();
-                y = (int)ev.getRawY();
+                x = (int) ev.getRawX();
+                y = (int) ev.getRawY();
 
                 float slop = getQuickStepTouchSlopPx(getContext());
                 if (Math.abs(x - mTouchDownX) > slop || Math.abs(y - mTouchDownY) > slop) {
@@ -272,12 +272,13 @@
                 : KeyButtonRipple.Type.ROUNDED_RECT);
     }
 
+    @Override
     public void playSoundEffect(int soundConstant) {
         if (!mPlaySounds) return;
         mAudioManager.playSoundEffect(soundConstant);
     }
 
-    public void sendEvent(int action, int flags) {
+    private void sendEvent(int action, int flags) {
         sendEvent(action, flags, SystemClock.uptimeMillis());
     }
 
@@ -309,8 +310,8 @@
             switch (action) {
                 case KeyEvent.ACTION_DOWN:
                     handled = ims.onKeyDown(ev.getKeyCode(), ev);
-                    mTracking = handled && ev.getRepeatCount() == 0 &&
-                            (ev.getFlags() & KeyEvent.FLAG_START_TRACKING) != 0;
+                    mTracking = handled && ev.getRepeatCount() == 0
+                            && (ev.getFlags() & KeyEvent.FLAG_START_TRACKING) != 0;
                     break;
                 case KeyEvent.ACTION_UP:
                     handled = ims.onKeyUp(ev.getKeyCode(), ev);
diff --git a/core/java/android/inputmethodservice/navigationbar/NavigationBarFrame.java b/core/java/android/inputmethodservice/navigationbar/NavigationBarFrame.java
index f01173e..a270675 100644
--- a/core/java/android/inputmethodservice/navigationbar/NavigationBarFrame.java
+++ b/core/java/android/inputmethodservice/navigationbar/NavigationBarFrame.java
@@ -59,4 +59,4 @@
         }
         return super.dispatchTouchEvent(event);
     }
-}
\ No newline at end of file
+}
diff --git a/core/java/android/inputmethodservice/navigationbar/NavigationBarInflaterView.java b/core/java/android/inputmethodservice/navigationbar/NavigationBarInflaterView.java
index d488890..e93bda2 100644
--- a/core/java/android/inputmethodservice/navigationbar/NavigationBarInflaterView.java
+++ b/core/java/android/inputmethodservice/navigationbar/NavigationBarInflaterView.java
@@ -121,7 +121,7 @@
         return CONFIG_NAV_BAR_LAYOUT_HANDLE;
     }
 
-    public void setButtonDispatchers(SparseArray<ButtonDispatcher> buttonDispatchers) {
+    void setButtonDispatchers(SparseArray<ButtonDispatcher> buttonDispatchers) {
         mButtonDispatchers = buttonDispatchers;
         for (int i = 0; i < buttonDispatchers.size(); i++) {
             initiallyFill(buttonDispatchers.valueAt(i));
@@ -376,7 +376,7 @@
     }
     */
 
-    public static String extractSize(String buttonSpec) {
+    private static String extractSize(String buttonSpec) {
         if (!buttonSpec.contains(SIZE_MOD_START)) {
             return null;
         }
@@ -384,7 +384,7 @@
         return buttonSpec.substring(sizeStart + 1, buttonSpec.indexOf(SIZE_MOD_END));
     }
 
-    public static String extractButton(String buttonSpec) {
+    private static String extractButton(String buttonSpec) {
         if (!buttonSpec.contains(SIZE_MOD_START)) {
             return buttonSpec;
         }
@@ -398,9 +398,9 @@
                 mButtonDispatchers.valueAt(indexOfKey).addView(v);
             }
             if (v instanceof ViewGroup) {
-                final ViewGroup viewGroup = (ViewGroup)v;
-                final int N = viewGroup.getChildCount();
-                for (int i = 0; i < N; i++) {
+                final ViewGroup viewGroup = (ViewGroup) v;
+                final int numChildViews = viewGroup.getChildCount();
+                for (int i = 0; i < numChildViews; i++) {
                     addToDispatchers(viewGroup.getChildAt(i));
                 }
             }
diff --git a/core/java/android/inputmethodservice/navigationbar/NavigationBarView.java b/core/java/android/inputmethodservice/navigationbar/NavigationBarView.java
index a2d7105..510b14e 100644
--- a/core/java/android/inputmethodservice/navigationbar/NavigationBarView.java
+++ b/core/java/android/inputmethodservice/navigationbar/NavigationBarView.java
@@ -48,8 +48,8 @@
  * @hide
  */
 public final class NavigationBarView extends FrameLayout {
-    final static boolean DEBUG = false;
-    final static String TAG = "NavBarView";
+    private static final boolean DEBUG = false;
+    private static final String TAG = "NavBarView";
 
     // Copied from com.android.systemui.animation.Interpolators#FAST_OUT_SLOW_IN
     private static final Interpolator FAST_OUT_SLOW_IN = new PathInterpolator(0.4f, 0f, 0.2f, 1f);
@@ -139,7 +139,7 @@
     }
 
     /**
-     * Applies {@param consumer} to each of the nav bar views.
+     * Applies {@code consumer} to each of the nav bar views.
      */
     public void forEachView(Consumer<View> consumer) {
         if (mHorizontal != null) {
@@ -181,7 +181,7 @@
         }
     }
 
-    public KeyButtonDrawable getBackDrawable() {
+    private KeyButtonDrawable getBackDrawable() {
         KeyButtonDrawable drawable = getDrawable(com.android.internal.R.drawable.ic_ime_nav_back);
         orientBackButton(drawable);
         return drawable;
@@ -211,7 +211,7 @@
         // Animate the back button's rotation to the new degrees and only in portrait move up the
         // back button to line up with the other buttons
         float targetY = useAltBack
-                ? - dpToPx(NAVBAR_BACK_BUTTON_IME_OFFSET, getResources())
+                ? -dpToPx(NAVBAR_BACK_BUTTON_IME_OFFSET, getResources())
                 : 0;
         ObjectAnimator navBarAnimator = ObjectAnimator.ofPropertyValuesHolder(drawable,
                 PropertyValuesHolder.ofFloat(KeyButtonDrawable.KEY_DRAWABLE_ROTATE, degrees),
@@ -233,6 +233,11 @@
         super.setLayoutDirection(layoutDirection);
     }
 
+    /**
+     * Updates the navigation icons based on {@code hints}.
+     *
+     * @param hints bit flags defined in {@link StatusBarManager}.
+     */
     public void setNavigationIconHints(int hints) {
         if (hints == mNavigationIconHints) return;
         final boolean newBackAlt = (hints & StatusBarManager.NAVIGATION_HINT_BACK_ALT) != 0;
@@ -250,15 +255,7 @@
         updateNavButtonIcons();
     }
 
-    public void setDisabledFlags(int disabledFlags) {
-        if (mDisabledFlags == disabledFlags) return;
-
-        mDisabledFlags = disabledFlags;
-
-        updateNavButtonIcons();
-    }
-
-    public void updateNavButtonIcons() {
+    private void updateNavButtonIcons() {
         // We have to replace or restore the back and home button icons when exiting or entering
         // carmode, respectively. Recents are not available in CarMode in nav bar so change
         // to recent icon is not required.
@@ -319,7 +316,7 @@
         mHorizontal.setVisibility(View.GONE);
     }
 
-    public void reorient() {
+    private void reorient() {
         updateCurrentView();
 
         final android.inputmethodservice.navigationbar.NavigationBarFrame frame =
@@ -372,6 +369,11 @@
         }
     }
 
+    /**
+     * Updates the dark intensity.
+     *
+     * @param intensity The intensity of darkness from {@code 0.0f} to {@code 1.0f}.
+     */
     public void setDarkIntensity(@FloatRange(from = 0.0f, to = 1.0f) float intensity) {
         for (int i = 0; i < mButtonDispatchers.size(); ++i) {
             mButtonDispatchers.valueAt(i).setDarkIntensity(intensity);
diff --git a/core/java/android/inputmethodservice/navigationbar/ReverseLinearLayout.java b/core/java/android/inputmethodservice/navigationbar/ReverseLinearLayout.java
index 68163c3..9b36cc5 100644
--- a/core/java/android/inputmethodservice/navigationbar/ReverseLinearLayout.java
+++ b/core/java/android/inputmethodservice/navigationbar/ReverseLinearLayout.java
@@ -30,9 +30,8 @@
 /**
  * Automatically reverses the order of children as they are added.
  * Also reverse the width and height values of layout params
- * @hide
  */
-public class ReverseLinearLayout extends LinearLayout {
+class ReverseLinearLayout extends LinearLayout {
 
     /** If true, the layout is reversed vs. a regular linear layout */
     private boolean mIsLayoutReverse;
@@ -40,7 +39,7 @@
     /** If true, the layout is opposite to it's natural reversity from the layout direction */
     private boolean mIsAlternativeOrder;
 
-    public ReverseLinearLayout(Context context, @Nullable AttributeSet attrs) {
+    ReverseLinearLayout(Context context, @Nullable AttributeSet attrs) {
         super(context, attrs);
     }
 
@@ -129,7 +128,7 @@
 
     public static class ReverseRelativeLayout extends RelativeLayout implements Reversible {
 
-        public ReverseRelativeLayout(Context context) {
+        ReverseRelativeLayout(Context context) {
             super(context);
         }
 
diff --git a/core/java/android/net/netstats/NetworkStatsDataMigrationUtils.java b/core/java/android/net/netstats/NetworkStatsDataMigrationUtils.java
index 2dd3aaa1..5c9989e 100644
--- a/core/java/android/net/netstats/NetworkStatsDataMigrationUtils.java
+++ b/core/java/android/net/netstats/NetworkStatsDataMigrationUtils.java
@@ -27,6 +27,7 @@
 import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID;
 
 import android.annotation.NonNull;
+import android.annotation.StringDef;
 import android.annotation.SystemApi;
 import android.net.NetworkIdentity;
 import android.net.NetworkStatsCollection;
@@ -47,6 +48,8 @@
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
 import java.net.ProtocolException;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -76,6 +79,15 @@
      */
     public static final String PREFIX_UID_TAG = "uid_tag";
 
+    /** @hide */
+    @StringDef(prefix = {"PREFIX_"}, value = {
+        PREFIX_XT,
+        PREFIX_UID,
+        PREFIX_UID_TAG,
+    })
+    @Retention(RetentionPolicy.SOURCE)
+    public @interface Prefix {}
+
     private static final HashMap<String, String> sPrefixLegacyFileNameMap =
             new HashMap<String, String>() {{
                 put(PREFIX_XT, "netstats_xt.bin");
@@ -141,13 +153,13 @@
 
     // Get /data/system/netstats_*.bin legacy files. Does not check for existence.
     @NonNull
-    private static File getLegacyBinFileForPrefix(@NonNull String prefix) {
+    private static File getLegacyBinFileForPrefix(@NonNull @Prefix String prefix) {
         return new File(getPlatformSystemDir(), sPrefixLegacyFileNameMap.get(prefix));
     }
 
     // List /data/system/netstats/[xt|uid|uid_tag].<start>-<end> legacy files.
     @NonNull
-    private static ArrayList<File> getPlatformFileListForPrefix(@NonNull String prefix) {
+    private static ArrayList<File> getPlatformFileListForPrefix(@NonNull @Prefix String prefix) {
         final ArrayList<File> list = new ArrayList<>();
         final File platformFiles = new File(getPlatformBaseDir(), "netstats");
         if (platformFiles.exists()) {
@@ -207,7 +219,7 @@
      */
     @NonNull
     public static NetworkStatsCollection readPlatformCollection(
-            @NonNull String prefix, long bucketDuration) throws IOException {
+            @NonNull @Prefix String prefix, long bucketDuration) throws IOException {
         final NetworkStatsCollection.Builder builder =
                 new NetworkStatsCollection.Builder(bucketDuration);
 
diff --git a/core/java/android/os/BatteryConsumer.java b/core/java/android/os/BatteryConsumer.java
index de76c8f..f4ca1b5 100644
--- a/core/java/android/os/BatteryConsumer.java
+++ b/core/java/android/os/BatteryConsumer.java
@@ -165,6 +165,7 @@
             PROCESS_STATE_FOREGROUND,
             PROCESS_STATE_BACKGROUND,
             PROCESS_STATE_FOREGROUND_SERVICE,
+            PROCESS_STATE_CACHED,
     })
     @Retention(RetentionPolicy.SOURCE)
     public @interface ProcessState {
@@ -175,8 +176,9 @@
     public static final int PROCESS_STATE_FOREGROUND = 1;
     public static final int PROCESS_STATE_BACKGROUND = 2;
     public static final int PROCESS_STATE_FOREGROUND_SERVICE = 3;
+    public static final int PROCESS_STATE_CACHED = 4;
 
-    public static final int PROCESS_STATE_COUNT = 4;
+    public static final int PROCESS_STATE_COUNT = 5;
 
     private static final String[] sProcessStateNames = new String[PROCESS_STATE_COUNT];
 
@@ -186,6 +188,7 @@
         sProcessStateNames[PROCESS_STATE_FOREGROUND] = "fg";
         sProcessStateNames[PROCESS_STATE_BACKGROUND] = "bg";
         sProcessStateNames[PROCESS_STATE_FOREGROUND_SERVICE] = "fgs";
+        sProcessStateNames[PROCESS_STATE_CACHED] = "cached";
     }
 
     private static final int[] SUPPORTED_POWER_COMPONENTS_PER_PROCESS_STATE = {
diff --git a/core/java/android/os/BatteryStats.java b/core/java/android/os/BatteryStats.java
index de1dc80..06c35b5 100644
--- a/core/java/android/os/BatteryStats.java
+++ b/core/java/android/os/BatteryStats.java
@@ -680,6 +680,8 @@
                 return BatteryConsumer.PROCESS_STATE_BACKGROUND;
             case BatteryStats.Uid.PROCESS_STATE_FOREGROUND_SERVICE:
                 return BatteryConsumer.PROCESS_STATE_FOREGROUND_SERVICE;
+            case BatteryStats.Uid.PROCESS_STATE_CACHED:
+                return BatteryConsumer.PROCESS_STATE_CACHED;
             default:
                 return BatteryConsumer.PROCESS_STATE_UNSPECIFIED;
         }
@@ -2681,7 +2683,7 @@
     public static final String[] RADIO_ACCESS_TECHNOLOGY_NAMES = {"Other", "LTE", "NR"};
 
     /**
-     * Returns the time in microseconds that the mobile radio has been active on a
+     * Returns the time in milliseconds that the mobile radio has been active on a
      * given Radio Access Technology (RAT), at a given frequency (NR RAT only), for a given
      * transmission power level.
      *
@@ -2700,6 +2702,46 @@
             @ServiceState.FrequencyRange int frequencyRange, int signalStrength,
             long elapsedRealtimeMs);
 
+    /**
+     * Returns the time in milliseconds that the mobile radio has been actively transmitting data on
+     * a given Radio Access Technology (RAT), at a given frequency (NR RAT only), for a given
+     * transmission power level.
+     *
+     * @param rat            Radio Access Technology {@see RadioAccessTechnology}
+     * @param frequencyRange frequency range {@see ServiceState.FrequencyRange}, only needed for
+     *                       RADIO_ACCESS_TECHNOLOGY_NR. Use
+     *                       {@link ServiceState.FREQUENCY_RANGE_UNKNOWN} for other Radio Access
+     *                       Technologies.
+     * @param signalStrength the cellular signal strength. {@see CellSignalStrength#getLevel()}
+     * @param elapsedRealtimeMs current elapsed realtime
+     * @return time (in milliseconds) the mobile radio spent actively transmitting data in the
+     *         specified state, while on battery. Returns {@link DURATION_UNAVAILABLE} if
+     *         data unavailable.
+     * @hide
+     */
+    public abstract long getActiveTxRadioDurationMs(@RadioAccessTechnology int rat,
+            @ServiceState.FrequencyRange int frequencyRange, int signalStrength,
+            long elapsedRealtimeMs);
+
+    /**
+     * Returns the time in milliseconds that the mobile radio has been actively receiving data on a
+     * given Radio Access Technology (RAT), at a given frequency (NR RAT only), for a given
+     * transmission power level.
+     *
+     * @param rat            Radio Access Technology {@see RadioAccessTechnology}
+     * @param frequencyRange frequency range {@see ServiceState.FrequencyRange}, only needed for
+     *                       RADIO_ACCESS_TECHNOLOGY_NR. Use
+     *                       {@link ServiceState.FREQUENCY_RANGE_UNKNOWN} for other Radio Access
+     *                       Technologies.
+     * @param elapsedRealtimeMs current elapsed realtime
+     * @return time (in milliseconds) the mobile radio spent actively receiving data in the
+     *         specified state, while on battery. Returns {@link DURATION_UNAVAILABLE} if
+     *         data unavailable.
+     * @hide
+     */
+    public abstract long getActiveRxRadioDurationMs(@RadioAccessTechnology int rat,
+            @ServiceState.FrequencyRange int frequencyRange, long elapsedRealtimeMs);
+
     static final String[] WIFI_SUPPL_STATE_NAMES = {
         "invalid", "disconn", "disabled", "inactive", "scanning",
         "authenticating", "associating", "associated", "4-way-handshake",
@@ -2720,6 +2762,13 @@
     public static final long POWER_DATA_UNAVAILABLE = -1L;
 
     /**
+     * Returned value if duration data is unavailable.
+     *
+     * {@hide}
+     */
+    public static final long DURATION_UNAVAILABLE = -1L;
+
+    /**
      * Returns the battery consumption (in microcoulombs) of bluetooth, derived from on
      * device power measurement data.
      * Will return {@link #POWER_DATA_UNAVAILABLE} if data is unavailable.
@@ -4093,6 +4142,10 @@
                 "    Mmwave frequency (greater than 6GHz):\n"};
         final String signalStrengthHeader =
                 "      Signal Strength Time:\n";
+        final String txHeader =
+                "      Tx Time:\n";
+        final String rxHeader =
+                "      Rx Time: ";
         final String[] signalStrengthDescription = new String[]{
                 "        unknown:  ",
                 "        poor:     ",
@@ -4144,6 +4197,29 @@
                     sb.append(")\n");
                 }
 
+                sb.append(prefix);
+                sb.append(txHeader);
+                for (int strength = 0; strength < numSignalStrength; strength++) {
+                    final long timeMs = getActiveTxRadioDurationMs(rat, freqLvl, strength,
+                            rawRealtimeMs);
+                    if (timeMs <= 0) continue;
+                    hasFreqData = true;
+                    sb.append(prefix);
+                    sb.append(signalStrengthDescription[strength]);
+                    formatTimeMs(sb, timeMs);
+                    sb.append("(");
+                    sb.append(formatRatioLocked(timeMs, totalActiveTimesMs));
+                    sb.append(")\n");
+                }
+
+                sb.append(prefix);
+                sb.append(rxHeader);
+                final long rxTimeMs = getActiveRxRadioDurationMs(rat, freqLvl, rawRealtimeMs);
+                formatTimeMs(sb, rxTimeMs);
+                sb.append("(");
+                sb.append(formatRatioLocked(rxTimeMs, totalActiveTimesMs));
+                sb.append(")\n");
+
                 if (hasFreqData) {
                     hasData = true;
                     pw.print(sb);
@@ -5655,6 +5731,7 @@
                         .setMaxStatsAgeMs(0)
                         .includePowerModels()
                         .includeProcessStateData()
+                        .includeVirtualUids()
                         .build());
         stats.dump(pw, prefix);
 
diff --git a/core/java/android/os/BatteryUsageStatsQuery.java b/core/java/android/os/BatteryUsageStatsQuery.java
index 37bd51b..b3f4d98 100644
--- a/core/java/android/os/BatteryUsageStatsQuery.java
+++ b/core/java/android/os/BatteryUsageStatsQuery.java
@@ -42,6 +42,7 @@
             FLAG_BATTERY_USAGE_STATS_POWER_PROFILE_MODEL,
             FLAG_BATTERY_USAGE_STATS_INCLUDE_HISTORY,
             FLAG_BATTERY_USAGE_STATS_INCLUDE_PROCESS_STATE_DATA,
+            FLAG_BATTERY_USAGE_STATS_INCLUDE_VIRTUAL_UIDS,
     })
     @Retention(RetentionPolicy.SOURCE)
     public @interface BatteryUsageStatsFlags {}
@@ -69,6 +70,8 @@
 
     public static final int FLAG_BATTERY_USAGE_STATS_INCLUDE_PROCESS_STATE_DATA = 0x0008;
 
+    public static final int FLAG_BATTERY_USAGE_STATS_INCLUDE_VIRTUAL_UIDS = 0x0010;
+
     private static final long DEFAULT_MAX_STATS_AGE_MS = 5 * 60 * 1000;
 
     private final int mFlags;
@@ -271,6 +274,15 @@
         }
 
         /**
+         * Requests to return attribution data for virtual UIDs such as
+         * {@link Process#SDK_SANDBOX_VIRTUAL_UID}.
+         */
+        public Builder includeVirtualUids() {
+            mFlags |= BatteryUsageStatsQuery.FLAG_BATTERY_USAGE_STATS_INCLUDE_VIRTUAL_UIDS;
+            return this;
+        }
+
+        /**
          * Requests to aggregate stored snapshots between the two supplied timestamps
          * @param fromTimestamp Exclusive starting timestamp, as per System.currentTimeMillis()
          * @param toTimestamp Inclusive ending timestamp, as per System.currentTimeMillis()
diff --git a/core/java/android/os/Bundle.java b/core/java/android/os/Bundle.java
index a19b51b..cf28c16 100644
--- a/core/java/android/os/Bundle.java
+++ b/core/java/android/os/Bundle.java
@@ -989,7 +989,7 @@
      * Otherwise, this method might throw an exception or return {@code null}.
      *
      * @param key a String, or {@code null}
-     * @param clazz The type of the items inside the array
+     * @param clazz The type of the items inside the array. This is only verified when unparceling.
      * @return a Parcelable[] value, or {@code null}
      */
     @SuppressLint({"ArrayReturn", "NullableCollection"})
@@ -1053,7 +1053,8 @@
      * Otherwise, this method might throw an exception or return {@code null}.
      *
      * @param key   a String, or {@code null}
-     * @param clazz The type of the items inside the array list
+     * @param clazz The type of the items inside the array list. This is only verified when
+     *     unparceling.
      * @return an ArrayList<T> value, or {@code null}
      */
     @SuppressLint("NullableCollection")
@@ -1103,6 +1104,8 @@
      * </ul>
      *
      * @param key a String, or null
+     * @param clazz The type of the items inside the sparse array. This is only verified when
+     *     unparceling.
      * @return a SparseArray of T values, or null
      */
     @SuppressWarnings("unchecked")
diff --git a/core/java/android/os/Environment.java b/core/java/android/os/Environment.java
index 0a7a407..ecdc803 100644
--- a/core/java/android/os/Environment.java
+++ b/core/java/android/os/Environment.java
@@ -478,10 +478,20 @@
     }
 
     /** {@hide} */
+    public static File getDataMiscCeSharedSdkSandboxDirectory(int userId, String packageName) {
+        return buildPath(getDataMiscCeDirectory(userId), "sdksandbox", packageName, "shared");
+    }
+
+    /** {@hide} */
     public static File getDataMiscDeDirectory(int userId) {
         return buildPath(getDataDirectory(), "misc_de", String.valueOf(userId));
     }
 
+    /** {@hide} */
+    public static File getDataMiscDeSharedSdkSandboxDirectory(int userId, String packageName) {
+        return buildPath(getDataMiscDeDirectory(userId), "sdksandbox", packageName, "shared");
+    }
+
     private static File getDataProfilesDeDirectory(int userId) {
         return buildPath(getDataDirectory(), "misc", "profiles", "cur", String.valueOf(userId));
     }
diff --git a/core/java/android/os/IBinder.java b/core/java/android/os/IBinder.java
index 010459d..9e47a70 100644
--- a/core/java/android/os/IBinder.java
+++ b/core/java/android/os/IBinder.java
@@ -293,7 +293,10 @@
      *
      * @return Returns the result from {@link Binder#onTransact}.  A successful call
      * generally returns true; false generally means the transaction code was not
-     * understood.
+     * understood.  For a oneway call to a different process false should never be
+     * returned.  If a oneway call is made to code in the same process (usually to
+     * a C++ or Rust implementation), then there are no oneway semantics, and
+     * false can still be returned.
      */
     public boolean transact(int code, @NonNull Parcel data, @Nullable Parcel reply, int flags)
         throws RemoteException;
diff --git a/core/java/android/os/IUserManager.aidl b/core/java/android/os/IUserManager.aidl
index 39ca596..3cde031 100644
--- a/core/java/android/os/IUserManager.aidl
+++ b/core/java/android/os/IUserManager.aidl
@@ -109,7 +109,7 @@
     boolean someUserHasAccount(in String accountName, in String accountType);
     String getProfileType(int userId);
     boolean isMediaSharedWithParent(int userId);
-    boolean isCredentialSharedWithParent(int userId);
+    boolean isCredentialSharableWithParent(int userId);
     boolean isDemoUser(int userId);
     boolean isPreCreated(int userId);
     UserInfo createProfileForUserEvenWhenDisallowedWithThrow(in String name, in String userType, int flags,
diff --git a/core/java/android/os/ParcelableHolder.java b/core/java/android/os/ParcelableHolder.java
index 368ee2d..a739ba3 100644
--- a/core/java/android/os/ParcelableHolder.java
+++ b/core/java/android/os/ParcelableHolder.java
@@ -179,7 +179,11 @@
      * Read ParcelableHolder from a parcel.
      */
     public void readFromParcel(@NonNull Parcel parcel) {
-        this.mStability = parcel.readInt();
+        int wireStability = parcel.readInt();
+        if (this.mStability != wireStability) {
+            throw new IllegalArgumentException("Expected stability " + this.mStability
+                                               + " but got " + wireStability);
+        }
 
         mParcelable = null;
 
diff --git a/core/java/android/os/PowerComponents.java b/core/java/android/os/PowerComponents.java
index 6051712..522807b 100644
--- a/core/java/android/os/PowerComponents.java
+++ b/core/java/android/os/PowerComponents.java
@@ -292,6 +292,10 @@
                 procState = BatteryUsageStatsAtomsProto.BatteryConsumerData.PowerComponentUsageSlice
                         .FOREGROUND_SERVICE;
                 break;
+            case BatteryConsumer.PROCESS_STATE_CACHED:
+                procState = BatteryUsageStatsAtomsProto.BatteryConsumerData.PowerComponentUsageSlice
+                        .CACHED;
+                break;
             default:
                 throw new IllegalArgumentException("Unknown process state: " + processState);
         }
diff --git a/core/java/android/os/UidBatteryConsumer.java b/core/java/android/os/UidBatteryConsumer.java
index a1ff923..91d231e 100644
--- a/core/java/android/os/UidBatteryConsumer.java
+++ b/core/java/android/os/UidBatteryConsumer.java
@@ -119,6 +119,8 @@
                     skipEmptyComponents);
             appendProcessStateData(sb, BatteryConsumer.PROCESS_STATE_FOREGROUND_SERVICE,
                     skipEmptyComponents);
+            appendProcessStateData(sb, BatteryConsumer.PROCESS_STATE_CACHED,
+                    skipEmptyComponents);
             pw.print(sb);
         }
 
@@ -202,20 +204,24 @@
         private static final String PACKAGE_NAME_UNINITIALIZED = "";
         private final BatteryStats.Uid mBatteryStatsUid;
         private final int mUid;
+        private final boolean mIsVirtualUid;
         private String mPackageWithHighestDrain = PACKAGE_NAME_UNINITIALIZED;
         private boolean mExcludeFromBatteryUsageStats;
 
         public Builder(BatteryConsumerData data, @NonNull BatteryStats.Uid batteryStatsUid) {
-            super(data, CONSUMER_TYPE_UID);
-            mBatteryStatsUid = batteryStatsUid;
-            mUid = batteryStatsUid.getUid();
-            data.putLong(COLUMN_INDEX_UID, mUid);
+            this(data, batteryStatsUid, batteryStatsUid.getUid());
         }
 
         public Builder(BatteryConsumerData data, int uid) {
+            this(data, null, uid);
+        }
+
+        private Builder(BatteryConsumerData data, @Nullable BatteryStats.Uid batteryStatsUid,
+                int uid) {
             super(data, CONSUMER_TYPE_UID);
-            mBatteryStatsUid = null;
+            mBatteryStatsUid = batteryStatsUid;
             mUid = uid;
+            mIsVirtualUid = mUid == Process.SDK_SANDBOX_VIRTUAL_UID;
             data.putLong(COLUMN_INDEX_UID, mUid);
         }
 
@@ -232,6 +238,10 @@
             return mUid;
         }
 
+        public boolean isVirtualUid() {
+            return mIsVirtualUid;
+        }
+
         /**
          * Sets the name of the package owned by this UID that consumed the highest amount
          * of power since BatteryStats reset.
diff --git a/core/java/android/os/UserManager.java b/core/java/android/os/UserManager.java
index a715c69..2448a05 100644
--- a/core/java/android/os/UserManager.java
+++ b/core/java/android/os/UserManager.java
@@ -1703,12 +1703,40 @@
 
     /**
      * A response code from {@link #removeUserWhenPossible(UserHandle, boolean)} indicating that
-     * an error occurred that prevented the user from being removed or set as ephemeral.
+     * an unknown error occurred that prevented the user from being removed or set as ephemeral.
      *
      * @hide
      */
     @SystemApi
-    public static final int REMOVE_RESULT_ERROR = 3;
+    public static final int REMOVE_RESULT_ERROR_UNKNOWN = -1;
+
+    /**
+     * A response code from {@link #removeUserWhenPossible(UserHandle, boolean)} indicating that
+     * the user could not be removed due to a {@link #DISALLOW_REMOVE_MANAGED_PROFILE} or
+     * {@link #DISALLOW_REMOVE_USER} user restriction.
+     *
+     * @hide
+     */
+    @SystemApi
+    public static final int REMOVE_RESULT_ERROR_USER_RESTRICTION = -2;
+
+    /**
+     * A response code from {@link #removeUserWhenPossible(UserHandle, boolean)} indicating that
+     * user being removed does not exist.
+     *
+     * @hide
+     */
+    @SystemApi
+    public static final int REMOVE_RESULT_ERROR_USER_NOT_FOUND = -3;
+
+    /**
+     * A response code from {@link #removeUserWhenPossible(UserHandle, boolean)} indicating that
+     * user being removed is a {@link UserHandle#SYSTEM} user which can't be removed.
+     *
+     * @hide
+     */
+    @SystemApi
+    public static final int REMOVE_RESULT_ERROR_SYSTEM_USER = -4;
 
     /**
      * Possible response codes from {@link #removeUserWhenPossible(UserHandle, boolean)}.
@@ -1719,7 +1747,10 @@
             REMOVE_RESULT_REMOVED,
             REMOVE_RESULT_DEFERRED,
             REMOVE_RESULT_ALREADY_BEING_REMOVED,
-            REMOVE_RESULT_ERROR,
+            REMOVE_RESULT_ERROR_USER_RESTRICTION,
+            REMOVE_RESULT_ERROR_USER_NOT_FOUND,
+            REMOVE_RESULT_ERROR_SYSTEM_USER,
+            REMOVE_RESULT_ERROR_UNKNOWN,
     })
     @Retention(RetentionPolicy.SOURCE)
     public @interface RemoveResult {}
@@ -4763,7 +4794,7 @@
     }
 
     /**
-     * Returns {@code true} if the user shares lock settings credential with its parent user
+     * Returns whether the user can have shared lockscreen credential with its parent user.
      *
      * This API only works for {@link UserManager#isProfile() profiles}
      * and will always return false for any other user type.
@@ -4776,9 +4807,9 @@
                     Manifest.permission.MANAGE_USERS,
                     Manifest.permission.INTERACT_ACROSS_USERS})
     @SuppressAutoDoc
-    public boolean isCredentialSharedWithParent() {
+    public boolean isCredentialSharableWithParent() {
         try {
-            return mService.isCredentialSharedWithParent(mUserId);
+            return mService.isCredentialSharableWithParent(mUserId);
         } catch (RemoteException re) {
             throw re.rethrowFromSystemServer();
         }
@@ -4846,8 +4877,10 @@
      * the {@link #DISALLOW_REMOVE_USER} or {@link #DISALLOW_REMOVE_MANAGED_PROFILE} restriction
      *
      * @return the {@link RemoveResult} code: {@link #REMOVE_RESULT_REMOVED},
-     * {@link #REMOVE_RESULT_DEFERRED}, {@link #REMOVE_RESULT_ALREADY_BEING_REMOVED}, or
-     * {@link #REMOVE_RESULT_ERROR}.
+     * {@link #REMOVE_RESULT_DEFERRED}, {@link #REMOVE_RESULT_ALREADY_BEING_REMOVED},
+     * {@link #REMOVE_RESULT_ERROR_USER_RESTRICTION}, {@link #REMOVE_RESULT_ERROR_USER_NOT_FOUND},
+     * {@link #REMOVE_RESULT_ERROR_SYSTEM_USER}, or {@link #REMOVE_RESULT_ERROR_UNKNOWN}. All error
+     * codes have negative values.
      *
      * @hide
      */
@@ -4864,6 +4897,19 @@
     }
 
     /**
+     * Check if {@link #removeUserWhenPossible} returned a success code which means that the user
+     * has been removed or is slated for removal.
+     *
+     * @param result is {@link #RemoveResult} code return by {@link #removeUserWhenPossible}.
+     * @return {@code true} if it is a success code.
+     * @hide
+     */
+    @SystemApi
+    public static boolean isRemoveResultSuccessful(@RemoveResult int result) {
+        return result >= 0;
+    }
+
+    /**
      * Updates the user's name.
      *
      * @param userId the user's integer id
diff --git a/core/java/android/os/storage/IStorageManager.aidl b/core/java/android/os/storage/IStorageManager.aidl
index 7832dcc..c021c07 100644
--- a/core/java/android/os/storage/IStorageManager.aidl
+++ b/core/java/android/os/storage/IStorageManager.aidl
@@ -78,37 +78,10 @@
      */
     String getMountedObbPath(in String rawPath) = 24;
     /**
-     * Decrypts any encrypted volumes.
-     */
-    int decryptStorage(in String password) = 26;
-    /**
-     * Encrypts storage.
-     */
-    int encryptStorage(int type, in String password) = 27;
-    /**
-     * Changes the encryption password.
-     */
-    int changeEncryptionPassword(int type, in String password) = 28;
-    /**
      * Returns list of all mountable volumes for the specified userId
      */
     StorageVolume[] getVolumeList(int userId, in String callingPackage, int flags) = 29;
     /**
-     * Determines the encryption state of the volume.
-     * @return a numerical value. See {@code ENCRYPTION_STATE_*} for possible
-     * values.
-     * Note that this has been replaced in most cases by the APIs in
-     * StorageManager (see isEncryptable and below)
-     * This is still useful to get the error state when encryption has failed
-     * and CryptKeeper needs to throw up a screen advising the user what to do
-     */
-    int getEncryptionState() = 31;
-    /**
-     * Verify the encryption password against the stored volume.  This method
-     * may only be called by the system process.
-     */
-    int verifyEncryptionPassword(in String password) = 32;
-    /**
      * Ensure that all directories along given path exist, creating parent
      * directories as needed. Validates that given path is absolute and that it
      * contains no relative "." or ".." paths or symlinks. Also ensures that
@@ -117,32 +90,6 @@
      */
     void mkdirs(in String callingPkg, in String path) = 34;
     /**
-     * Determines the type of the encryption password
-     * @return PasswordType
-     */
-    int getPasswordType() = 35;
-    /**
-     * Get password from vold
-     * @return password or empty string
-     */
-    String getPassword() = 36;
-    /**
-     * Securely clear password from vold
-     */
-    oneway void clearPassword() = 37;
-    /**
-     * Set a field in the crypto header.
-     * @param field field to set
-     * @param contents contents to set in field
-     */
-    oneway void setField(in String field, in String contents) = 38;
-    /**
-     * Gets a field from the crypto header.
-     * @param field field to get
-     * @return contents of field
-     */
-    String getField(in String field) = 39;
-    /**
      * Report the time of the last maintenance operation such as fstrim.
      * @return Timestamp of the last maintenance operation, in the
      *     System.currentTimeMillis() time base
diff --git a/core/java/android/os/storage/StorageManager.java b/core/java/android/os/storage/StorageManager.java
index b501730..9971cbc 100644
--- a/core/java/android/os/storage/StorageManager.java
+++ b/core/java/android/os/storage/StorageManager.java
@@ -83,7 +83,6 @@
 import android.provider.DeviceConfig;
 import android.provider.MediaStore;
 import android.provider.Settings;
-import android.sysprop.VoldProperties;
 import android.system.ErrnoException;
 import android.system.Os;
 import android.system.OsConstants;
@@ -1739,10 +1738,7 @@
      *         false not encrypted or file encrypted
      */
     public static boolean isBlockEncrypted() {
-        if (!isEncrypted()) {
-            return false;
-        }
-        return RoSystemProperties.CRYPTO_BLOCK_ENCRYPTED;
+        return false;
     }
 
     /** {@hide}
@@ -1752,18 +1748,7 @@
      *         false not encrypted, file encrypted or default block encrypted
      */
     public static boolean isNonDefaultBlockEncrypted() {
-        if (!isBlockEncrypted()) {
-            return false;
-        }
-
-        try {
-            IStorageManager storageManager = IStorageManager.Stub.asInterface(
-                    ServiceManager.getService("mount"));
-            return storageManager.getPasswordType() != CRYPT_TYPE_DEFAULT;
-        } catch (RemoteException e) {
-            Log.e(TAG, "Error getting encryption type");
-            return false;
-        }
+        return false;
     }
 
     /** {@hide}
@@ -1777,8 +1762,7 @@
      * framework, so no service needs to check for changes during their lifespan
      */
     public static boolean isBlockEncrypting() {
-        final String state = VoldProperties.encrypt_progress().orElse("");
-        return !"".equalsIgnoreCase(state);
+        return false;
     }
 
     /** {@hide}
@@ -1793,8 +1777,7 @@
      * framework, so no service needs to check for changes during their lifespan
      */
     public static boolean inCryptKeeperBounce() {
-        final String status = VoldProperties.decrypt().orElse("");
-        return "trigger_restart_min_framework".equals(status);
+        return false;
     }
 
     /** {@hide} */
@@ -3061,14 +3044,4 @@
     public static final int CRYPT_TYPE_PATTERN = IVold.PASSWORD_TYPE_PATTERN;
     /** @hide */
     public static final int CRYPT_TYPE_PIN = IVold.PASSWORD_TYPE_PIN;
-
-    // Constants for the data available via StorageManagerService.getField.
-    /** @hide */
-    public static final String SYSTEM_LOCALE_KEY = "SystemLocale";
-    /** @hide */
-    public static final String OWNER_INFO_KEY = "OwnerInfo";
-    /** @hide */
-    public static final String PATTERN_VISIBLE_KEY = "PatternVisible";
-    /** @hide */
-    public static final String PASSWORD_VISIBLE_KEY = "PasswordVisible";
 }
diff --git a/core/java/android/provider/DeviceConfig.java b/core/java/android/provider/DeviceConfig.java
index 052e4d0..4731504 100644
--- a/core/java/android/provider/DeviceConfig.java
+++ b/core/java/android/provider/DeviceConfig.java
@@ -74,6 +74,13 @@
     public static final String NAMESPACE_ACTIVITY_MANAGER = "activity_manager";
 
     /**
+     * Namespace for activity manager, specific to the "component alias" feature. We needed a
+     * different namespace in order to avoid phonetype from resetting it.
+     * @hide
+     */
+    public static final String NAMESPACE_ACTIVITY_MANAGER_COMPONENT_ALIAS = "activity_manager_ca";
+
+    /**
      * Namespace for all activity manager related features that are used at the native level.
      * These features are applied at reboot.
      *
@@ -322,6 +329,13 @@
     public static final String NAMESPACE_NNAPI_NATIVE = "nnapi_native";
 
     /**
+     * Namespace for all OnDevicePersonalization related feature.
+     * @hide
+     */
+    @SystemApi
+    public static final String NAMESPACE_ON_DEVICE_PERSONALIZATION = "on_device_personalization";
+
+    /**
      * Namespace for features related to the Package Manager Service.
      *
      * @hide
@@ -586,6 +600,13 @@
     public static final String NAMESPACE_SELECTION_TOOLBAR = "selection_toolbar";
 
     /**
+     * Definitions for voice interaction related functions.
+     *
+     * @hide
+     */
+    public static final String NAMESPACE_VOICE_INTERACTION = "voice_interaction";
+
+    /**
      * List of namespaces which can be read without READ_DEVICE_CONFIG permission
      *
      * @hide
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java
index cfef7ad..a6ad5e5 100644
--- a/core/java/android/provider/Settings.java
+++ b/core/java/android/provider/Settings.java
@@ -10729,14 +10729,6 @@
                 "communal_mode_trusted_networks";
 
         /**
-         * Setting to allow Fast Pair scans to be enabled.
-         * @hide
-         */
-        @SystemApi
-        @Readable
-        public static final String FAST_PAIR_SCAN_ENABLED = "fast_pair_scan_enabled";
-
-        /**
          * Setting to store denylisted system languages by the CEC {@code <Set Menu Language>}
          * confirmation dialog.
          *
diff --git a/core/java/android/service/dreams/DreamService.java b/core/java/android/service/dreams/DreamService.java
index db622d3..001707d 100644
--- a/core/java/android/service/dreams/DreamService.java
+++ b/core/java/android/service/dreams/DreamService.java
@@ -231,7 +231,7 @@
      * The default value for whether to show complications on the overlay.
      * @hide
      */
-    public static final boolean DEFAULT_SHOW_COMPLICATIONS = true;
+    public static final boolean DEFAULT_SHOW_COMPLICATIONS = false;
 
     private final IDreamManager mDreamManager;
     private final Handler mHandler = new Handler(Looper.getMainLooper());
@@ -1341,7 +1341,9 @@
                     public void onViewDetachedFromWindow(View v) {
                         if (mActivity == null || !mActivity.isChangingConfigurations()) {
                             // Only stop the dream if the view is not detached by relaunching
-                            // activity for configuration changes.
+                            // activity for configuration changes. It is important to also clear
+                            // the window reference in order to fully release the DreamActivity.
+                            mWindow = null;
                             mActivity = null;
                             finish();
                         }
diff --git a/core/java/android/service/quicksettings/TileService.java b/core/java/android/service/quicksettings/TileService.java
index b507328..0829d28 100644
--- a/core/java/android/service/quicksettings/TileService.java
+++ b/core/java/android/service/quicksettings/TileService.java
@@ -15,18 +15,19 @@
  */
 package android.service.quicksettings;
 
-import android.Manifest;
 import android.annotation.SdkConstant;
 import android.annotation.SdkConstant.SdkConstantType;
 import android.annotation.SystemApi;
 import android.annotation.TestApi;
 import android.app.Dialog;
 import android.app.Service;
+import android.app.StatusBarManager;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.res.Resources;
 import android.graphics.drawable.Icon;
+import android.os.Build;
 import android.os.Handler;
 import android.os.IBinder;
 import android.os.Looper;
@@ -147,13 +148,6 @@
             "android.service.quicksettings.TOGGLEABLE_TILE";
 
     /**
-     * Used to notify SysUI that Listening has be requested.
-     * @hide
-     */
-    public static final String ACTION_REQUEST_LISTENING =
-            "android.service.quicksettings.action.REQUEST_LISTENING";
-
-    /**
      * @hide
      */
     public static final String EXTRA_SERVICE = "service";
@@ -482,14 +476,24 @@
      *
      * This method is only applicable to tiles that have {@link #META_DATA_ACTIVE_TILE} defined
      * as true on their TileService Manifest declaration, and will do nothing otherwise.
+     *
+     * For apps targeting {@link Build.VERSION_CODES#TIRAMISU} or later, this call may throw
+     * the following exceptions if the request is not valid:
+     * <ul>
+     *     <li> {@link NullPointerException} if {@code component} is {@code null}.</li>
+     *     <li> {@link SecurityException} if the package of {@code component} does not match
+     *     the calling package or if the calling user cannot act on behalf of the user from the
+     *     {@code context}.</li>
+     *     <li> {@link IllegalArgumentException} if the user of the {@code context} is not the
+     *     current user.</li>
+     * </ul>
      */
     public static final void requestListeningState(Context context, ComponentName component) {
-        final ComponentName sysuiComponent = ComponentName.unflattenFromString(
-                context.getResources().getString(
-                        com.android.internal.R.string.config_systemUIServiceComponent));
-        Intent intent = new Intent(ACTION_REQUEST_LISTENING);
-        intent.putExtra(Intent.EXTRA_COMPONENT_NAME, component);
-        intent.setPackage(sysuiComponent.getPackageName());
-        context.sendBroadcast(intent, Manifest.permission.BIND_QUICK_SETTINGS_TILE);
+        StatusBarManager sbm = context.getSystemService(StatusBarManager.class);
+        if (sbm == null) {
+            Log.e(TAG, "No StatusBarManager service found");
+            return;
+        }
+        sbm.requestTileServiceListeningState(component);
     }
 }
diff --git a/core/java/android/service/wallpaper/WallpaperService.java b/core/java/android/service/wallpaper/WallpaperService.java
index a2938a8..ef792d4 100644
--- a/core/java/android/service/wallpaper/WallpaperService.java
+++ b/core/java/android/service/wallpaper/WallpaperService.java
@@ -252,6 +252,7 @@
         final InsetsVisibilities mRequestedVisibilities = new InsetsVisibilities();
         final InsetsSourceControl[] mTempControls = new InsetsSourceControl[0];
         final MergedConfiguration mMergedConfiguration = new MergedConfiguration();
+        final Bundle mSyncSeqIdBundle = new Bundle();
         private final Point mSurfaceSize = new Point();
         private final Point mLastSurfaceSize = new Point();
         private final Matrix mTmpMatrix = new Matrix();
@@ -391,7 +392,7 @@
             @Override
             public void resized(ClientWindowFrames frames, boolean reportDraw,
                     MergedConfiguration mergedConfiguration, boolean forceLayout,
-                    boolean alwaysConsumeSystemBars, int displayId) {
+                    boolean alwaysConsumeSystemBars, int displayId, int syncSeqId) {
                 Message msg = mCaller.obtainMessageIO(MSG_WINDOW_RESIZED,
                         reportDraw ? 1 : 0,
                         mergedConfiguration);
@@ -1151,7 +1152,7 @@
                     final int relayoutResult = mSession.relayout(
                             mWindow, mLayout, mWidth, mHeight,
                             View.VISIBLE, 0, mWinFrames, mMergedConfiguration, mSurfaceControl,
-                            mInsetsState, mTempControls);
+                            mInsetsState, mTempControls, mSyncSeqIdBundle);
 
                     final int transformHint = SurfaceControl.rotationToBufferTransform(
                             (mDisplayInstallOrientation + mDisplay.getRotation()) % 4);
@@ -1338,7 +1339,8 @@
                         mSurfaceCreated = true;
                         if (redrawNeeded) {
                             resetWindowPages();
-                            mSession.finishDrawing(mWindow, null /* postDrawTransaction */);
+                            mSession.finishDrawing(mWindow, null /* postDrawTransaction */,
+                                                   Integer.MAX_VALUE);
                             processLocalColors(mPendingXOffset, mPendingXOffsetStep);
                             notifyColorsChanged();
                         }
diff --git a/core/java/android/telephony/TelephonyRegistryManager.java b/core/java/android/telephony/TelephonyRegistryManager.java
index c1fcd66..f844592 100644
--- a/core/java/android/telephony/TelephonyRegistryManager.java
+++ b/core/java/android/telephony/TelephonyRegistryManager.java
@@ -27,6 +27,7 @@
 import android.os.Build;
 import android.os.RemoteException;
 import android.os.ServiceManager;
+import android.service.carrier.CarrierService;
 import android.telephony.Annotation.CallState;
 import android.telephony.Annotation.DataActivityType;
 import android.telephony.Annotation.DisconnectCauses;
@@ -36,6 +37,7 @@
 import android.telephony.Annotation.RadioPowerState;
 import android.telephony.Annotation.SimActivationState;
 import android.telephony.Annotation.SrvccState;
+import android.telephony.TelephonyManager.CarrierPrivilegesCallback;
 import android.telephony.TelephonyManager.CarrierPrivilegesListener;
 import android.telephony.emergency.EmergencyNumber;
 import android.telephony.ims.ImsReasonInfo;
@@ -44,17 +46,19 @@
 
 import com.android.internal.annotations.GuardedBy;
 import com.android.internal.listeners.ListenerExecutor;
-import com.android.internal.telephony.ICarrierPrivilegesListener;
+import com.android.internal.telephony.ICarrierPrivilegesCallback;
 import com.android.internal.telephony.IOnSubscriptionsChangedListener;
 import com.android.internal.telephony.ITelephonyRegistry;
 
 import java.lang.ref.WeakReference;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.WeakHashMap;
 import java.util.concurrent.Executor;
+import java.util.stream.Collectors;
 
 /**
  * A centralized place to notify telephony related status changes, e.g, {@link ServiceState} update
@@ -1260,34 +1264,78 @@
                 pkgName, attributionTag, callback, new int[0], notifyNow);
     }
 
-    private static class CarrierPrivilegesListenerWrapper extends ICarrierPrivilegesListener.Stub
+    // TODO(b/216549778): Remove listener logic once all clients switch to CarrierPrivilegesCallback
+    private static class CarrierPrivilegesCallbackWrapper extends ICarrierPrivilegesCallback.Stub
             implements ListenerExecutor {
-        private final WeakReference<CarrierPrivilegesListener> mListener;
-        private final Executor mExecutor;
+        // Either mListener or mCallback may be null, never both
+        @Nullable private final WeakReference<CarrierPrivilegesListener> mListener;
+        @Nullable private final WeakReference<CarrierPrivilegesCallback> mCallback;
+        @NonNull private final Executor mExecutor;
 
-        CarrierPrivilegesListenerWrapper(CarrierPrivilegesListener listener, Executor executor) {
+        CarrierPrivilegesCallbackWrapper(
+                @NonNull CarrierPrivilegesCallback callback, @NonNull Executor executor) {
+            mListener = null;
+            mCallback = new WeakReference<>(callback);
+            mExecutor = executor;
+        }
+
+        CarrierPrivilegesCallbackWrapper(
+                @NonNull CarrierPrivilegesListener listener, @NonNull Executor executor) {
             mListener = new WeakReference<>(listener);
+            mCallback = null;
             mExecutor = executor;
         }
 
         @Override
         public void onCarrierPrivilegesChanged(
-                List<String> privilegedPackageNames, int[] privilegedUids) {
-            Binder.withCleanCallingIdentity(
-                    () ->
-                            executeSafely(
-                                    mExecutor,
-                                    mListener::get,
-                                    cpl ->
-                                            cpl.onCarrierPrivilegesChanged(
-                                                    privilegedPackageNames, privilegedUids)));
+                @NonNull List<String> privilegedPackageNames, @NonNull int[] privilegedUids) {
+            if (mListener != null) {
+                Binder.withCleanCallingIdentity(
+                        () ->
+                                executeSafely(
+                                        mExecutor,
+                                        mListener::get,
+                                        cpl ->
+                                                cpl.onCarrierPrivilegesChanged(
+                                                        privilegedPackageNames, privilegedUids)));
+            }
+
+            if (mCallback != null) {
+                // AIDL interface does not support Set, keep the List/Array and translate them here
+                Set<String> privilegedPkgNamesSet = Set.copyOf(privilegedPackageNames);
+                Set<Integer> privilegedUidsSet = Arrays.stream(privilegedUids).boxed().collect(
+                        Collectors.toSet());
+                Binder.withCleanCallingIdentity(
+                        () ->
+                                executeSafely(
+                                        mExecutor,
+                                        mCallback::get,
+                                        cpc ->
+                                                cpc.onCarrierPrivilegesChanged(
+                                                        privilegedPkgNamesSet, privilegedUidsSet)));
+            }
+        }
+
+        @Override
+        public void onCarrierServiceChanged(@Nullable String packageName, int uid) {
+            if (mCallback != null) {
+                Binder.withCleanCallingIdentity(
+                        () ->
+                                executeSafely(
+                                        mExecutor,
+                                        mCallback::get,
+                                        cpc -> cpc.onCarrierServiceChanged(packageName, uid)));
+            }
         }
     }
 
-    @GuardedBy("sCarrierPrivilegeListeners")
-    private static final WeakHashMap<
-                    CarrierPrivilegesListener, WeakReference<CarrierPrivilegesListenerWrapper>>
-            sCarrierPrivilegeListeners = new WeakHashMap<>();
+    // TODO(b/216549778): Change the map key to CarrierPrivilegesCallback once all clients switch to
+    // CarrierPrivilegesCallback. Before that, the key is either CarrierPrivilegesCallback or
+    // CarrierPrivilegesListener, no logic actually depends on the type.
+    @NonNull
+    @GuardedBy("sCarrierPrivilegeCallbacks")
+    private static final WeakHashMap<Object,  WeakReference<CarrierPrivilegesCallbackWrapper>>
+            sCarrierPrivilegeCallbacks = new WeakHashMap<>();
 
     /**
      * Registers a {@link CarrierPrivilegesListener} on the given {@code logicalSlotIndex} to
@@ -1297,7 +1345,11 @@
      * @param logicalSlotIndex The SIM slot to listen on
      * @param executor The executor where {@code listener} will be invoked
      * @param listener The callback to register
+     *
+     * @deprecated Use {@link #addCarrierPrivilegesCallback} instead. This API will be removed
+     * prior to API finalization.
      */
+    @Deprecated
     public void addCarrierPrivilegesListener(
             int logicalSlotIndex,
             @NonNull @CallbackExecutor Executor executor,
@@ -1305,18 +1357,18 @@
         if (listener == null || executor == null) {
             throw new IllegalArgumentException("listener and executor must be non-null");
         }
-        synchronized (sCarrierPrivilegeListeners) {
-            WeakReference<CarrierPrivilegesListenerWrapper> existing =
-                    sCarrierPrivilegeListeners.get(listener);
+        synchronized (sCarrierPrivilegeCallbacks) {
+            WeakReference<CarrierPrivilegesCallbackWrapper> existing =
+                    sCarrierPrivilegeCallbacks.get(listener);
             if (existing != null && existing.get() != null) {
                 Log.d(TAG, "addCarrierPrivilegesListener: listener already registered");
                 return;
             }
-            CarrierPrivilegesListenerWrapper wrapper =
-                    new CarrierPrivilegesListenerWrapper(listener, executor);
-            sCarrierPrivilegeListeners.put(listener, new WeakReference<>(wrapper));
+            CarrierPrivilegesCallbackWrapper wrapper =
+                    new CarrierPrivilegesCallbackWrapper(listener, executor);
+            sCarrierPrivilegeCallbacks.put(listener, new WeakReference<>(wrapper));
             try {
-                sRegistry.addCarrierPrivilegesListener(
+                sRegistry.addCarrierPrivilegesCallback(
                         logicalSlotIndex,
                         wrapper,
                         mContext.getOpPackageName(),
@@ -1331,19 +1383,84 @@
      * Unregisters a {@link CarrierPrivilegesListener}.
      *
      * @param listener The callback to unregister
+     *
+     * @deprecated Use {@link #removeCarrierPrivilegesCallback} instead. The callback will prior
+     * to API finalization.
      */
+    @Deprecated
     public void removeCarrierPrivilegesListener(@NonNull CarrierPrivilegesListener listener) {
         if (listener == null) {
             throw new IllegalArgumentException("listener must be non-null");
         }
-        synchronized (sCarrierPrivilegeListeners) {
-            WeakReference<CarrierPrivilegesListenerWrapper> ref =
-                    sCarrierPrivilegeListeners.remove(listener);
+        synchronized (sCarrierPrivilegeCallbacks) {
+            WeakReference<CarrierPrivilegesCallbackWrapper> ref =
+                    sCarrierPrivilegeCallbacks.remove(listener);
             if (ref == null) return;
-            CarrierPrivilegesListenerWrapper wrapper = ref.get();
+            CarrierPrivilegesCallbackWrapper wrapper = ref.get();
             if (wrapper == null) return;
             try {
-                sRegistry.removeCarrierPrivilegesListener(wrapper, mContext.getOpPackageName());
+                sRegistry.removeCarrierPrivilegesCallback(wrapper, mContext.getOpPackageName());
+            } catch (RemoteException e) {
+                throw e.rethrowFromSystemServer();
+            }
+        }
+    }
+
+    /**
+     * Registers a {@link CarrierPrivilegesCallback} on the given {@code logicalSlotIndex} to
+     * receive callbacks when the set of packages with carrier privileges changes. The callback will
+     * immediately be called with the latest state.
+     *
+     * @param logicalSlotIndex The SIM slot to listen on
+     * @param executor The executor where {@code listener} will be invoked
+     * @param callback The callback to register
+     */
+    public void addCarrierPrivilegesCallback(
+            int logicalSlotIndex,
+            @NonNull @CallbackExecutor Executor executor,
+            @NonNull CarrierPrivilegesCallback callback) {
+        if (callback == null || executor == null) {
+            throw new IllegalArgumentException("callback and executor must be non-null");
+        }
+        synchronized (sCarrierPrivilegeCallbacks) {
+            WeakReference<CarrierPrivilegesCallbackWrapper> existing =
+                    sCarrierPrivilegeCallbacks.get(callback);
+            if (existing != null && existing.get() != null) {
+                Log.d(TAG, "addCarrierPrivilegesCallback: callback already registered");
+                return;
+            }
+            CarrierPrivilegesCallbackWrapper wrapper =
+                    new CarrierPrivilegesCallbackWrapper(callback, executor);
+            sCarrierPrivilegeCallbacks.put(callback, new WeakReference<>(wrapper));
+            try {
+                sRegistry.addCarrierPrivilegesCallback(
+                        logicalSlotIndex,
+                        wrapper,
+                        mContext.getOpPackageName(),
+                        mContext.getAttributionTag());
+            } catch (RemoteException e) {
+                throw e.rethrowFromSystemServer();
+            }
+        }
+    }
+
+    /**
+     * Unregisters a {@link CarrierPrivilegesCallback}.
+     *
+     * @param callback The callback to unregister
+     */
+    public void removeCarrierPrivilegesCallback(@NonNull CarrierPrivilegesCallback callback) {
+        if (callback == null) {
+            throw new IllegalArgumentException("listener must be non-null");
+        }
+        synchronized (sCarrierPrivilegeCallbacks) {
+            WeakReference<CarrierPrivilegesCallbackWrapper> ref =
+                    sCarrierPrivilegeCallbacks.remove(callback);
+            if (ref == null) return;
+            CarrierPrivilegesCallbackWrapper wrapper = ref.get();
+            if (wrapper == null) return;
+            try {
+                sRegistry.removeCarrierPrivilegesCallback(wrapper, mContext.getOpPackageName());
             } catch (RemoteException e) {
                 throw e.rethrowFromSystemServer();
             }
@@ -1359,15 +1476,33 @@
      */
     public void notifyCarrierPrivilegesChanged(
             int logicalSlotIndex,
-            @NonNull List<String> privilegedPackageNames,
-            @NonNull int[] privilegedUids) {
+            @NonNull Set<String> privilegedPackageNames,
+            @NonNull Set<Integer> privilegedUids) {
         if (privilegedPackageNames == null || privilegedUids == null) {
             throw new IllegalArgumentException(
                     "privilegedPackageNames and privilegedUids must be non-null");
         }
         try {
-            sRegistry.notifyCarrierPrivilegesChanged(
-                    logicalSlotIndex, privilegedPackageNames, privilegedUids);
+            // AIDL doesn't support Set yet. Convert Set to List/Array
+            List<String> pkgList = List.copyOf(privilegedPackageNames);
+            int[] uids = privilegedUids.stream().mapToInt(Number::intValue).toArray();
+            sRegistry.notifyCarrierPrivilegesChanged(logicalSlotIndex, pkgList, uids);
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+
+    /**
+     * Notify listeners that the {@link CarrierService} for current user has changed.
+     *
+     * @param logicalSlotIndex the SIM slot the change occurred on
+     * @param packageName the package name of the changed {@link CarrierService}
+     * @param uid the UID of the changed {@link CarrierService}
+     */
+    public void notifyCarrierServiceChanged(int logicalSlotIndex, @Nullable String packageName,
+            int uid) {
+        try {
+            sRegistry.notifyCarrierServiceChanged(logicalSlotIndex, packageName, uid);
         } catch (RemoteException e) {
             throw e.rethrowFromSystemServer();
         }
diff --git a/core/java/android/text/PrecomputedText.java b/core/java/android/text/PrecomputedText.java
index 9307e56..f31a690 100644
--- a/core/java/android/text/PrecomputedText.java
+++ b/core/java/android/text/PrecomputedText.java
@@ -99,7 +99,7 @@
         private final @Layout.HyphenationFrequency int mHyphenationFrequency;
 
         // The line break configuration for calculating text wrapping.
-        private final @Nullable LineBreakConfig mLineBreakConfig;
+        private final @NonNull LineBreakConfig mLineBreakConfig;
 
         /**
          * A builder for creating {@link Params}.
@@ -119,7 +119,7 @@
                     Layout.HYPHENATION_FREQUENCY_NORMAL;
 
             // The line break configuration for calculating text wrapping.
-            private @Nullable LineBreakConfig mLineBreakConfig;
+            private @NonNull LineBreakConfig mLineBreakConfig = LineBreakConfig.NONE;
 
             /**
              * Builder constructor.
@@ -212,7 +212,7 @@
         // For the external developers, use Builder instead.
         /** @hide */
         public Params(@NonNull TextPaint paint,
-                @Nullable LineBreakConfig lineBreakConfig,
+                @NonNull LineBreakConfig lineBreakConfig,
                 @NonNull TextDirectionHeuristic textDir,
                 @Layout.BreakStrategy int strategy,
                 @Layout.HyphenationFrequency int frequency) {
@@ -260,11 +260,12 @@
         }
 
         /**
-         * Return the line break configuration for this text.
+         * Returns the {@link LineBreakConfig} for this text.
          *
-         * @return the current line break configuration, null if no line break configuration is set.
+         * @return the current line break configuration. The {@link LineBreakConfig} with default
+         * values will be returned if no line break configuration is set.
          */
-        public @Nullable LineBreakConfig getLineBreakConfig() {
+        public @NonNull LineBreakConfig getLineBreakConfig() {
             return mLineBreakConfig;
         }
 
@@ -297,9 +298,9 @@
         /** @hide */
         public @CheckResultUsableResult int checkResultUsable(@NonNull TextPaint paint,
                 @NonNull TextDirectionHeuristic textDir, @Layout.BreakStrategy int strategy,
-                @Layout.HyphenationFrequency int frequency, @Nullable LineBreakConfig lbConfig) {
+                @Layout.HyphenationFrequency int frequency, @NonNull LineBreakConfig lbConfig) {
             if (mBreakStrategy == strategy && mHyphenationFrequency == frequency
-                    && isLineBreakEquals(mLineBreakConfig, lbConfig)
+                    && mLineBreakConfig.equals(lbConfig)
                     && mPaint.equalsForTextMeasurement(paint)) {
                 return mTextDir == textDir ? USABLE : NEED_RECOMPUTE;
             } else {
@@ -308,29 +309,6 @@
         }
 
         /**
-         * Check the two LineBreakConfig instances are equal.
-         * This method assumes they are equal if one parameter is null and the other parameter has
-         * a LineBreakStyle value of LineBreakConfig.LINE_BREAK_STYLE_NONE.
-         *
-         * @param o1 the first LineBreakConfig instance.
-         * @param o2 the second LineBreakConfig instance.
-         * @return true if the two LineBreakConfig instances are equal.
-         */
-        private boolean isLineBreakEquals(LineBreakConfig o1, LineBreakConfig o2) {
-            if (Objects.equals(o1, o2)) {
-                return true;
-            }
-            if (o1 == null && (o2 != null
-                    && o2.getLineBreakStyle() == LineBreakConfig.LINE_BREAK_STYLE_NONE)) {
-                return true;
-            } else if (o2 == null && (o1 != null
-                    && o1.getLineBreakStyle() == LineBreakConfig.LINE_BREAK_STYLE_NONE)) {
-                return true;
-            }
-            return false;
-        }
-
-        /**
          * Check if the same text layout.
          *
          * @return true if this and the given param result in the same text layout
diff --git a/core/java/android/text/StaticLayout.java b/core/java/android/text/StaticLayout.java
index b10fc37..2f85d2b6 100644
--- a/core/java/android/text/StaticLayout.java
+++ b/core/java/android/text/StaticLayout.java
@@ -112,6 +112,7 @@
             b.mBreakStrategy = Layout.BREAK_STRATEGY_SIMPLE;
             b.mHyphenationFrequency = Layout.HYPHENATION_FREQUENCY_NONE;
             b.mJustificationMode = Layout.JUSTIFICATION_MODE_NONE;
+            b.mLineBreakConfig = LineBreakConfig.NONE;
             return b;
         }
 
@@ -410,7 +411,8 @@
          *
          * @param lineBreakConfig the line break configuration for text wrapping.
          * @return this builder, useful for chaining.
-         * @see android.widget.TextView#setLineBreakConfig
+         * @see android.widget.TextView#setLineBreakStyle
+         * @see android.widget.TextView#setLineBreakWordStyle
          */
         @NonNull
         public Builder setLineBreakConfig(@NonNull LineBreakConfig lineBreakConfig) {
@@ -454,7 +456,7 @@
         @Nullable private int[] mRightIndents;
         private int mJustificationMode;
         private boolean mAddLastLineLineSpacing;
-        private LineBreakConfig mLineBreakConfig;
+        private LineBreakConfig mLineBreakConfig = LineBreakConfig.NONE;
 
         private final Paint.FontMetricsInt mFontMetricsInt = new Paint.FontMetricsInt();
 
diff --git a/core/java/android/util/FeatureFlagUtils.java b/core/java/android/util/FeatureFlagUtils.java
index 4541f3a..34e7ea7 100644
--- a/core/java/android/util/FeatureFlagUtils.java
+++ b/core/java/android/util/FeatureFlagUtils.java
@@ -71,8 +71,8 @@
      * Hide back key in the Settings two pane design.
      * @hide
      */
-    public static final String SETTINGS_HIDE_SECONDARY_PAGE_BACK_BUTTON_IN_TWO_PANE =
-            "settings_hide_secondary_page_back_button_in_two_pane";
+    public static final String SETTINGS_HIDE_SECOND_LAYER_PAGE_NAVIGATE_UP_BUTTON_IN_TWO_PANE =
+            "settings_hide_second_layer_page_navigate_up_button_in_two_pane";
 
     private static final Map<String, String> DEFAULT_FLAGS;
 
@@ -96,10 +96,10 @@
         DEFAULT_FLAGS.put(SETTINGS_ENABLE_SECURITY_HUB, "true");
         DEFAULT_FLAGS.put(SETTINGS_SUPPORT_LARGE_SCREEN, "true");
         DEFAULT_FLAGS.put("settings_search_always_expand", "true");
-        DEFAULT_FLAGS.put(SETTINGS_APP_LANGUAGE_SELECTION, "true");
+        DEFAULT_FLAGS.put(SETTINGS_APP_LANGUAGE_SELECTION, "false");
         DEFAULT_FLAGS.put(SETTINGS_ENABLE_MONITOR_PHANTOM_PROCS, "true");
         DEFAULT_FLAGS.put(SETTINGS_APP_ALLOW_DARK_THEME_ACTIVATION_AT_BEDTIME, "false");
-        DEFAULT_FLAGS.put(SETTINGS_HIDE_SECONDARY_PAGE_BACK_BUTTON_IN_TWO_PANE, "true");
+        DEFAULT_FLAGS.put(SETTINGS_HIDE_SECOND_LAYER_PAGE_NAVIGATE_UP_BUTTON_IN_TWO_PANE, "true");
     }
 
     private static final Set<String> PERSISTENT_FLAGS;
@@ -109,7 +109,7 @@
         PERSISTENT_FLAGS.add(SETTINGS_SUPPORT_LARGE_SCREEN);
         PERSISTENT_FLAGS.add(SETTINGS_ENABLE_MONITOR_PHANTOM_PROCS);
         PERSISTENT_FLAGS.add(SETTINGS_APP_ALLOW_DARK_THEME_ACTIVATION_AT_BEDTIME);
-        PERSISTENT_FLAGS.add(SETTINGS_HIDE_SECONDARY_PAGE_BACK_BUTTON_IN_TWO_PANE);
+        PERSISTENT_FLAGS.add(SETTINGS_HIDE_SECOND_LAYER_PAGE_NAVIGATE_UP_BUTTON_IN_TWO_PANE);
     }
 
     /**
diff --git a/core/java/android/view/HandwritingInitiator.java b/core/java/android/view/HandwritingInitiator.java
index c1413be..190adbd 100644
--- a/core/java/android/view/HandwritingInitiator.java
+++ b/core/java/android/view/HandwritingInitiator.java
@@ -249,19 +249,19 @@
             return;
         }
 
-        Rect handwritingArea = getViewHandwritingArea(connectedView);
-        if (handwritingArea != null) {
-            if (contains(handwritingArea, mState.mStylusDownX, mState.mStylusDownY)) {
-                startHandwriting(connectedView);
-            }
+        final Rect handwritingArea = getViewHandwritingArea(connectedView);
+        if (contains(handwritingArea, mState.mStylusDownX, mState.mStylusDownY)) {
+            startHandwriting(connectedView);
+        } else {
+            reset();
         }
-        reset();
     }
 
     /** For test only. */
     @VisibleForTesting
     public void startHandwriting(@NonNull View view) {
         mImm.startStylusHandwriting(view);
+        reset();
     }
 
     /**
@@ -287,7 +287,7 @@
         final View connectedView = getConnectedView();
         if (connectedView != null && connectedView.isAutoHandwritingEnabled()) {
             final Rect handwritingArea = getViewHandwritingArea(connectedView);
-            if (handwritingArea != null && contains(handwritingArea, x, y)) {
+            if (contains(handwritingArea, x, y)) {
                 return connectedView;
             }
         }
@@ -298,8 +298,7 @@
         for (HandwritableViewInfo viewInfo : handwritableViewInfos) {
             final View view = viewInfo.getView();
             if (!view.isAutoHandwritingEnabled()) continue;
-            final Rect rect = viewInfo.getHandwritingArea();
-            if (rect != null && contains(rect, x, y)) {
+            if (contains(viewInfo.getHandwritingArea(), x, y)) {
                 return viewInfo.getView();
             }
         }
@@ -315,12 +314,15 @@
     private static Rect getViewHandwritingArea(@NonNull View view) {
         final ViewParent viewParent = view.getParent();
         if (viewParent != null && view.isAttachedToWindow() && view.isAggregatedVisible()) {
-            Rect handwritingArea = view.getHandwritingArea();
-            if (handwritingArea == null) {
-                handwritingArea = new Rect(0, 0, view.getWidth(), view.getHeight());
+            final Rect localHandwritingArea = view.getHandwritingArea();
+            final Rect globalHandwritingArea = new Rect();
+            if (localHandwritingArea != null) {
+                globalHandwritingArea.set(localHandwritingArea);
+            } else {
+                globalHandwritingArea.set(0, 0, view.getWidth(), view.getHeight());
             }
-            if (viewParent.getChildVisibleRect(view, handwritingArea, null)) {
-                return handwritingArea;
+            if (viewParent.getChildVisibleRect(view, globalHandwritingArea, null)) {
+                return globalHandwritingArea;
             }
         }
         return null;
@@ -329,7 +331,8 @@
     /**
      * Return true if the (x, y) is inside by the given {@link Rect}.
      */
-    private boolean contains(@NonNull Rect rect, float x, float y) {
+    private boolean contains(@Nullable Rect rect, float x, float y) {
+        if (rect == null) return false;
         return x >= rect.left && x < rect.right && y >= rect.top && y < rect.bottom;
     }
 
@@ -481,17 +484,18 @@
             if (!mIsDirty) {
                 return true;
             }
-            final Rect localRect = view.getHandwritingArea();
-            if (localRect == null) {
+            final Rect handwritingArea = view.getHandwritingArea();
+            if (handwritingArea == null) {
                 return false;
             }
 
             ViewParent parent = view.getParent();
             if (parent != null) {
-                final Rect newRect = new Rect(localRect);
-                if (parent.getChildVisibleRect(view, newRect, null /* offset */)) {
-                    mHandwritingArea = newRect;
-                } else {
+                if (mHandwritingArea == null) {
+                    mHandwritingArea = new Rect();
+                }
+                mHandwritingArea.set(handwritingArea);
+                if (!parent.getChildVisibleRect(view, mHandwritingArea, null /* offset */)) {
                     mHandwritingArea = null;
                 }
             }
diff --git a/core/java/android/view/IWindow.aidl b/core/java/android/view/IWindow.aidl
index 12421ed..1f6cee6 100644
--- a/core/java/android/view/IWindow.aidl
+++ b/core/java/android/view/IWindow.aidl
@@ -55,7 +55,8 @@
 
     void resized(in ClientWindowFrames frames, boolean reportDraw,
             in MergedConfiguration newMergedConfiguration,
-            boolean forceLayout, boolean alwaysConsumeSystemBars, int displayId);
+            boolean forceLayout, boolean alwaysConsumeSystemBars, int displayId,
+            int syncSeqId);
 
     /**
      * Called when the window insets configuration has changed.
diff --git a/core/java/android/view/IWindowSession.aidl b/core/java/android/view/IWindowSession.aidl
index 5c7c844..fd86900 100644
--- a/core/java/android/view/IWindowSession.aidl
+++ b/core/java/android/view/IWindowSession.aidl
@@ -104,7 +104,8 @@
             int requestedWidth, int requestedHeight, int viewVisibility,
             int flags, out ClientWindowFrames outFrames,
             out MergedConfiguration outMergedConfiguration, out SurfaceControl outSurfaceControl,
-            out InsetsState insetsState, out InsetsSourceControl[] activeControls);
+            out InsetsState insetsState, out InsetsSourceControl[] activeControls,
+            out Bundle bundle);
 
     /*
      * Notify the window manager that an application is relaunching and
@@ -142,7 +143,8 @@
      * is null if there is no sync required.
      */
     @UnsupportedAppUsage
-    oneway void finishDrawing(IWindow window, in SurfaceControl.Transaction postDrawTransaction);
+    oneway void finishDrawing(IWindow window, in SurfaceControl.Transaction postDrawTransaction,
+            int seqId);
 
     @UnsupportedAppUsage
     oneway void setInTouchMode(boolean showFocus);
diff --git a/core/java/android/view/InsetsController.java b/core/java/android/view/InsetsController.java
index 71c1b7c..a4841f6 100644
--- a/core/java/android/view/InsetsController.java
+++ b/core/java/android/view/InsetsController.java
@@ -23,6 +23,7 @@
 import static android.view.InsetsState.ITYPE_IME;
 import static android.view.InsetsState.toInternalType;
 import static android.view.InsetsState.toPublicType;
+import static android.view.ViewRootImpl.CAPTION_ON_SHELL;
 import static android.view.WindowInsets.Type.all;
 import static android.view.WindowInsets.Type.ime;
 
@@ -682,9 +683,15 @@
 
     @VisibleForTesting
     public boolean onStateChanged(InsetsState state) {
-        boolean stateChanged = !mState.equals(state, true /* excludingCaptionInsets */,
-                        false /* excludeInvisibleIme */)
-                || !captionInsetsUnchanged();
+        boolean stateChanged = false;
+        if (!CAPTION_ON_SHELL) {
+            stateChanged = !mState.equals(state, true /* excludingCaptionInsets */,
+                    false /* excludeInvisibleIme */)
+                    || captionInsetsUnchanged();
+        } else {
+            stateChanged = !mState.equals(state, false /* excludingCaptionInsets */,
+                    false /* excludeInvisibleIme */);
+        }
         if (!stateChanged && mLastDispatchedState.equals(state)) {
             return false;
         }
@@ -758,16 +765,20 @@
     }
 
     private boolean captionInsetsUnchanged() {
+        if (CAPTION_ON_SHELL) {
+            return false;
+        }
         if (mState.peekSource(ITYPE_CAPTION_BAR) == null
                 && mCaptionInsetsHeight == 0) {
-            return true;
+            return false;
         }
         if (mState.peekSource(ITYPE_CAPTION_BAR) != null
                 && mCaptionInsetsHeight
                 == mState.peekSource(ITYPE_CAPTION_BAR).getFrame().height()) {
-            return true;
+            return false;
         }
-        return false;
+
+        return true;
     }
 
     private void startResizingAnimationIfNeeded(InsetsState fromState) {
@@ -1582,11 +1593,15 @@
 
     @Override
     public void setCaptionInsetsHeight(int height) {
+        // This method is to be removed once the caption is moved to the shell.
+        if (CAPTION_ON_SHELL) {
+            return;
+        }
         if (mCaptionInsetsHeight != height) {
             mCaptionInsetsHeight = height;
             if (mCaptionInsetsHeight != 0) {
-                mState.getSource(ITYPE_CAPTION_BAR).setFrame(new Rect(mFrame.left, mFrame.top,
-                        mFrame.right, mFrame.top + mCaptionInsetsHeight));
+                mState.getSource(ITYPE_CAPTION_BAR).setFrame(mFrame.left, mFrame.top,
+                        mFrame.right, mFrame.top + mCaptionInsetsHeight);
             } else {
                 mState.removeSource(ITYPE_CAPTION_BAR);
             }
diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java
index c222991..1b8dc70 100644
--- a/core/java/android/view/View.java
+++ b/core/java/android/view/View.java
@@ -12016,7 +12016,6 @@
 
     /**
      * Return the handwriting areas set on this view, in its local coordinates.
-     * Notice: the caller of this method should not modify the Rect returned.
      * @see #setHandwritingArea(Rect)
      *
      * @hide
@@ -12025,7 +12024,7 @@
     public Rect getHandwritingArea() {
         final ListenerInfo info = mListenerInfo;
         if (info != null) {
-            return info.mHandwritingArea;
+            return new Rect(info.mHandwritingArea);
         }
         return null;
     }
@@ -15617,7 +15616,7 @@
      * @param event the KeyEvent object that defines the button action
      */
     public boolean onKeyDown(int keyCode, KeyEvent event) {
-        if (event.hasNoModifiers() && KeyEvent.isConfirmKey(keyCode)) {
+        if (KeyEvent.isConfirmKey(keyCode) && event.hasNoModifiers()) {
             if ((mViewFlags & ENABLED_MASK) == DISABLED) {
                 return true;
             }
@@ -15674,7 +15673,7 @@
      * @param event   The KeyEvent object that defines the button action.
      */
     public boolean onKeyUp(int keyCode, KeyEvent event) {
-        if (event.hasNoModifiers() && KeyEvent.isConfirmKey(keyCode)) {
+        if (KeyEvent.isConfirmKey(keyCode) && event.hasNoModifiers()) {
             if ((mViewFlags & ENABLED_MASK) == DISABLED) {
                 return true;
             }
diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java
index bde761e..cdbf8b4 100644
--- a/core/java/android/view/ViewRootImpl.java
+++ b/core/java/android/view/ViewRootImpl.java
@@ -277,6 +277,12 @@
     private static final boolean ENABLE_INPUT_LATENCY_TRACKING = true;
 
     /**
+     * Whether the caption is drawn by the shell.
+     * @hide
+     */
+    public static final boolean CAPTION_ON_SHELL = false;
+
+    /**
      * Set this system property to true to force the view hierarchy to render
      * at 60 Hz. This can be used to measure the potential framerate.
      */
@@ -546,7 +552,6 @@
 
     private final Rect mVisRect = new Rect(); // used to retrieve visible rect of focused view.
     private final Rect mTempRect = new Rect();
-    private final Rect mTempRect2 = new Rect();
 
     private final WindowLayout mWindowLayout = new WindowLayout();
 
@@ -824,6 +829,24 @@
 
     private int mLastTransformHint = Integer.MIN_VALUE;
 
+    /**
+     * A temporary object used so relayoutWindow can return the latest SyncSeqId
+     * system. The SyncSeqId system was designed to work without synchronous relayout
+     * window, and actually synchronous relayout window presents a problem.  We could have
+     * a sequence like this:
+     *    1. We send MSG_RESIZED to the client with a new syncSeqId to begin a new sync
+     *    2. Due to scheduling the client executes performTraversals before calling MSG_RESIZED
+     *    3. Coincidentally for some random reason it also calls relayout
+     *    4. It observes the new state from relayout, and so the next frame will contain the state
+     * However it hasn't received the seqId yet, and so under the designed operation of
+     * seqId flowing through MSG_RESIZED, the next frame wouldn't be synced. Since it
+     * contains our target sync state, we need to sync it! This problem won't come up once
+     * we get rid of synchronous relayout, until then, we use this bundle to channel the
+     * integer back over relayout.
+     */
+    private Bundle mRelayoutBundle = new Bundle();
+    private int mSyncSeqId;
+
     private String mTag = TAG;
 
     public ViewRootImpl(Context context, Display display) {
@@ -867,6 +890,7 @@
         mDensity = context.getResources().getDisplayMetrics().densityDpi;
         mNoncompatDensity = context.getResources().getDisplayMetrics().noncompatDensityDpi;
         mFallbackEventHandler = new PhoneFallbackEventHandler(context);
+        // TODO(b/222696368): remove getSfInstance usage and use vsyncId for transactions
         mChoreographer = useSfChoreographer
                 ? Choreographer.getSfInstance() : Choreographer.getInstance();
         mDisplayManager = (DisplayManager)context.getSystemService(Context.DISPLAY_SERVICE);
@@ -1198,8 +1222,7 @@
                         displayCutoutSafe, winConfig.getBounds(), winConfig.getWindowingMode(),
                         UNSPECIFIED_LENGTH, UNSPECIFIED_LENGTH,
                         mInsetsController.getRequestedVisibilities(),
-                        getAttachedWindowFrame(), 1f /* compactScale */,
-                        mTmpFrames.displayFrame, mTempRect2, mTmpFrames.frame);
+                        getAttachedWindowFrame(), 1f /* compactScale */, mTmpFrames);
                 setFrame(mTmpFrames.frame);
                 registerBackCallbackOnWindow();
                 if (!WindowOnBackInvokedDispatcher.isOnBackInvokedCallbackEnabled(mContext)) {
@@ -1704,6 +1727,7 @@
         mPendingBackDropFrame.set(backdropFrame);
         mForceNextWindowRelayout = forceNextWindowRelayout;
         mPendingAlwaysConsumeSystemBars = args.argi2 != 0;
+        mSyncSeqId = args.argi4;
 
         if (msg == MSG_RESIZED_REPORT) {
             reportNextDraw();
@@ -1771,6 +1795,7 @@
         updateInternalDisplay(displayId, mView.getResources());
         mImeFocusController.onMovedToDisplay();
         mAttachInfo.mDisplayState = mDisplay.getState();
+        mDisplayInstallOrientation = mDisplay.getInstallOrientation();
         // Internal state updated, now notify the view hierarchy.
         mView.dispatchMovedToDisplay(mDisplay, config);
     }
@@ -2561,6 +2586,9 @@
     }
 
     private boolean updateCaptionInsets() {
+        if (CAPTION_ON_SHELL) {
+            return false;
+        }
         if (!(mView instanceof DecorView)) return false;
         final int captionInsetsHeight = ((DecorView) mView).getCaptionInsetsHeight();
         final Rect captionFrame = new Rect();
@@ -3439,6 +3467,12 @@
                     mReportNextDraw = false;
                     pendingDrawFinished();
                 }
+
+                // Make sure the consumer is not waiting if the view root was just made invisible.
+                if (mBLASTDrawConsumer != null) {
+                    mBLASTDrawConsumer.accept(null);
+                    mBLASTDrawConsumer = null;
+                }
             }
         }
 
@@ -4111,7 +4145,7 @@
         mDrawsNeededToReport = 0;
 
         try {
-            mWindowSession.finishDrawing(mWindow, mSurfaceChangedTransaction);
+            mWindowSession.finishDrawing(mWindow, mSurfaceChangedTransaction, Integer.MAX_VALUE);
         } catch (RemoteException e) {
             Log.e(mTag, "Unable to report draw finished", e);
             mSurfaceChangedTransaction.apply();
@@ -8054,7 +8088,7 @@
                 requestedWidth, requestedHeight, viewVisibility,
                 insetsPending ? WindowManagerGlobal.RELAYOUT_INSETS_PENDING : 0,
                 mTmpFrames, mPendingMergedConfiguration, mSurfaceControl, mTempInsets,
-                mTempControls);
+                mTempControls, mRelayoutBundle);
 
         final int transformHint = SurfaceControl.rotationToBufferTransform(
                 (mDisplayInstallOrientation + mDisplay.getRotation()) % 4);
@@ -8480,7 +8514,7 @@
                             if ((relayoutWindow(mWindowAttributes, viewVisibility, false)
                                     & WindowManagerGlobal.RELAYOUT_RES_FIRST_TIME) != 0) {
                                 mWindowSession.finishDrawing(
-                                        mWindow, null /* postDrawTransaction */);
+                                    mWindow, null /* postDrawTransaction */, Integer.MAX_VALUE);
                             }
                         } catch (RemoteException e) {
                         }
@@ -8554,7 +8588,7 @@
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     private void dispatchResized(ClientWindowFrames frames, boolean reportDraw,
             MergedConfiguration mergedConfiguration, boolean forceLayout,
-            boolean alwaysConsumeSystemBars, int displayId) {
+                                 boolean alwaysConsumeSystemBars, int displayId, int seqId) {
         final Rect frame = frames.frame;
         final Rect backDropFrame = frames.backdropFrame;
         if (DEBUG_LAYOUT) Log.v(mTag, "Resizing " + this + ": frame=" + frame.toShortString()
@@ -8585,6 +8619,7 @@
         args.argi1 = forceLayout ? 1 : 0;
         args.argi2 = alwaysConsumeSystemBars ? 1 : 0;
         args.argi3 = displayId;
+        args.argi4 = seqId;
         msg.obj = args;
         mHandler.sendMessage(msg);
     }
@@ -9969,11 +10004,11 @@
         @Override
         public void resized(ClientWindowFrames frames, boolean reportDraw,
                 MergedConfiguration mergedConfiguration, boolean forceLayout,
-                boolean alwaysConsumeSystemBars, int displayId) {
+                boolean alwaysConsumeSystemBars, int displayId, int seqId) {
             final ViewRootImpl viewAncestor = mViewAncestor.get();
             if (viewAncestor != null) {
                 viewAncestor.dispatchResized(frames, reportDraw, mergedConfiguration, forceLayout,
-                        alwaysConsumeSystemBars, displayId);
+                        alwaysConsumeSystemBars, displayId, seqId);
             }
         }
 
diff --git a/core/java/android/view/WindowLayout.java b/core/java/android/view/WindowLayout.java
index ad9f21b..a3b1313 100644
--- a/core/java/android/view/WindowLayout.java
+++ b/core/java/android/view/WindowLayout.java
@@ -42,6 +42,7 @@
 import android.graphics.Point;
 import android.graphics.Rect;
 import android.util.Log;
+import android.window.ClientWindowFrames;
 
 /**
  * Computes window frames.
@@ -56,15 +57,17 @@
     private final Rect mTempDisplayCutoutSafeExceptMaybeBarsRect = new Rect();
     private final Rect mTempRect = new Rect();
 
-    public boolean computeFrames(WindowManager.LayoutParams attrs, InsetsState state,
+    public void computeFrames(WindowManager.LayoutParams attrs, InsetsState state,
             Rect displayCutoutSafe, Rect windowBounds, @WindowingMode int windowingMode,
             int requestedWidth, int requestedHeight, InsetsVisibilities requestedVisibilities,
-            Rect attachedWindowFrame, float compatScale, Rect outDisplayFrame, Rect outParentFrame,
-            Rect outFrame) {
+            Rect attachedWindowFrame, float compatScale, ClientWindowFrames outFrames) {
         final int type = attrs.type;
         final int fl = attrs.flags;
         final int pfl = attrs.privateFlags;
         final boolean layoutInScreen = (fl & FLAG_LAYOUT_IN_SCREEN) == FLAG_LAYOUT_IN_SCREEN;
+        final Rect outDisplayFrame = outFrames.displayFrame;
+        final Rect outParentFrame = outFrames.parentFrame;
+        final Rect outFrame = outFrames.frame;
 
         // Compute bounds restricted by insets
         final Insets insets = state.calculateInsets(windowBounds, attrs.getFitInsetsTypes(),
@@ -95,7 +98,7 @@
         final DisplayCutout cutout = state.getDisplayCutout();
         final Rect displayCutoutSafeExceptMaybeBars = mTempDisplayCutoutSafeExceptMaybeBarsRect;
         displayCutoutSafeExceptMaybeBars.set(displayCutoutSafe);
-        boolean clippedByDisplayCutout = false;
+        outFrames.isParentFrameClippedByDisplayCutout = false;
         if (cutoutMode != LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS && !cutout.isEmpty()) {
             // Ensure that windows with a non-ALWAYS display cutout mode are laid out in
             // the cutout safe zone.
@@ -158,7 +161,7 @@
             if (!attachedInParent && !floatingInScreenWindow) {
                 mTempRect.set(outParentFrame);
                 outParentFrame.intersectUnchecked(displayCutoutSafeExceptMaybeBars);
-                clippedByDisplayCutout = !mTempRect.equals(outParentFrame);
+                outFrames.isParentFrameClippedByDisplayCutout = !mTempRect.equals(outParentFrame);
             }
             outDisplayFrame.intersectUnchecked(displayCutoutSafeExceptMaybeBars);
         }
@@ -287,9 +290,7 @@
         }
 
         if (DEBUG) Log.d(TAG, "computeWindowFrames " + attrs.getTitle()
-                + " outFrame=" + outFrame.toShortString()
-                + " outParentFrame=" + outParentFrame.toShortString()
-                + " outDisplayFrame=" + outDisplayFrame.toShortString()
+                + " outFrames=" + outFrames
                 + " windowBounds=" + windowBounds.toShortString()
                 + " attachedWindowFrame=" + (attachedWindowFrame != null
                         ? attachedWindowFrame.toShortString()
@@ -302,8 +303,6 @@
                 + " attrs=" + attrs
                 + " state=" + state
                 + " requestedVisibilities=" + requestedVisibilities);
-
-        return clippedByDisplayCutout;
     }
 
     public static void computeSurfaceSize(WindowManager.LayoutParams attrs, Rect maxBounds,
diff --git a/core/java/android/view/WindowlessWindowManager.java b/core/java/android/view/WindowlessWindowManager.java
index a270c92..ad151df 100644
--- a/core/java/android/view/WindowlessWindowManager.java
+++ b/core/java/android/view/WindowlessWindowManager.java
@@ -22,6 +22,7 @@
 import android.graphics.Rect;
 import android.graphics.Region;
 import android.os.Binder;
+import android.os.Bundle;
 import android.os.IBinder;
 import android.os.RemoteCallback;
 import android.os.RemoteException;
@@ -217,9 +218,13 @@
             throw new IllegalArgumentException(
                     "Invalid window token (never added or removed already)");
         }
+        removeSurface(state.mSurfaceControl);
+    }
 
+    /** Separate from {@link #remove} so that subclasses can put removal on a sync transaction. */
+    protected void removeSurface(SurfaceControl sc) {
         try (SurfaceControl.Transaction t = new SurfaceControl.Transaction()) {
-            t.remove(state.mSurfaceControl).apply();
+            t.remove(sc).apply();
         }
     }
 
@@ -277,7 +282,7 @@
             int requestedWidth, int requestedHeight, int viewFlags, int flags,
             ClientWindowFrames outFrames, MergedConfiguration mergedConfiguration,
             SurfaceControl outSurfaceControl, InsetsState outInsetsState,
-            InsetsSourceControl[] outActiveControls) {
+            InsetsSourceControl[] outActiveControls, Bundle outSyncSeqIdBundle) {
         final State state;
         synchronized (this) {
             state = mStateForWindow.get(window.asBinder());
@@ -354,7 +359,7 @@
 
     @Override
     public void finishDrawing(android.view.IWindow window,
-            android.view.SurfaceControl.Transaction postDrawTransaction) {
+            android.view.SurfaceControl.Transaction postDrawTransaction, int seqId) {
         synchronized (this) {
             final ResizeCompleteCallback c =
                 mResizeCompletionForWindow.get(window.asBinder());
diff --git a/core/java/android/view/autofill/AutofillManager.java b/core/java/android/view/autofill/AutofillManager.java
index a8cc114..3c5007b 100644
--- a/core/java/android/view/autofill/AutofillManager.java
+++ b/core/java/android/view/autofill/AutofillManager.java
@@ -3121,17 +3121,19 @@
     }
 
     /**
-     * If autofill suggestions for a dialog-style UI are available for {@code view}, shows a dialog
-     * allowing the user to select a suggestion and returns {@code true}.
+     * If autofill suggestions for a
+     * <a href="{@docRoot}reference/android/service/autofill/Dataset.html#FillDialogUI">
+     * dialog-style UI</a> are available for {@code view}, shows a dialog allowing the user to
+     * select a suggestion and returns {@code true}.
      * <p>
      * The dialog may not be available if the autofill service does not support it, or if the
      * autofill request has not returned a response yet.
      * <p>
-     * It is recommended to call this method the first time a user focuses on an autofill-able form,
-     * and to avoid showing the input method if the dialog is shown. If this method returns
-     * {@code false}, you should then instead show the input method (assuming that is how the
-     * view normally handles the focus event). If the user re-focuses on the view, you should not
-     * call this method again so as to not disrupt usage of the input method.
+     * It is recommended apps to call this method the first time a user focuses on
+     * an autofill-able form, and to avoid showing the input method if the dialog is shown. If
+     * this method returns {@code false}, you should then instead show the input method (assuming
+     * that is how the view normally handles the focus event). If the user re-focuses on the view,
+     * you should not call this method again so as to not disrupt usage of the input method.
      *
      * @param view the view for which to show autofill suggestions. This is typically a view
      *             receiving a focus event. The autofill suggestions shown will include content for
diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java
index 3a7a544..7f8a68d 100644
--- a/core/java/android/widget/Editor.java
+++ b/core/java/android/widget/Editor.java
@@ -4595,7 +4595,7 @@
 
             if (includeEditorBounds) {
                 final RectF bounds = new RectF();
-                mTextView.getBoundsOnScreen(bounds, false /* clipToParent */);
+                bounds.set(0 /* left */, 0 /* top */, mTextView.getWidth(), mTextView.getHeight());
                 EditorBoundsInfo.Builder boundsBuilder = new EditorBoundsInfo.Builder();
                 //TODO(b/210039666): add Handwriting bounds once they're available.
                 builder.setEditorBoundsInfo(
diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java
index b076d39..3c8fcb9 100644
--- a/core/java/android/widget/TextView.java
+++ b/core/java/android/widget/TextView.java
@@ -788,7 +788,8 @@
     private Layout mLayout;
     private boolean mLocalesChanged = false;
     private int mTextSizeUnit = -1;
-    private LineBreakConfig mLineBreakConfig = new LineBreakConfig();
+    private int mLineBreakStyle = DEFAULT_LINE_BREAK_STYLE;
+    private int mLineBreakWordStyle = DEFAULT_LINE_BREAK_WORD_STYLE;
 
     // This is used to reflect the current user preference for changing font weight and making text
     // more bold.
@@ -1457,13 +1458,12 @@
                     break;
 
                 case com.android.internal.R.styleable.TextView_lineBreakStyle:
-                    mLineBreakConfig.setLineBreakStyle(
-                            a.getInt(attr, LineBreakConfig.LINE_BREAK_STYLE_NONE));
+                    mLineBreakStyle = a.getInt(attr, LineBreakConfig.LINE_BREAK_STYLE_NONE);
                     break;
 
                 case com.android.internal.R.styleable.TextView_lineBreakWordStyle:
-                    mLineBreakConfig.setLineBreakWordStyle(
-                            a.getInt(attr, LineBreakConfig.LINE_BREAK_WORD_STYLE_NONE));
+                    mLineBreakWordStyle = a.getInt(attr,
+                            LineBreakConfig.LINE_BREAK_WORD_STYLE_NONE);
                     break;
 
                 case com.android.internal.R.styleable.TextView_autoSizeTextType:
@@ -4301,13 +4301,12 @@
             @LineBreakConfig.LineBreakStyle int lineBreakStyle,
             @LineBreakConfig.LineBreakWordStyle int lineBreakWordStyle) {
         boolean updated = false;
-        if (isLineBreakStyleSpecified && mLineBreakConfig.getLineBreakStyle() != lineBreakStyle) {
-            mLineBreakConfig.setLineBreakStyle(lineBreakStyle);
+        if (isLineBreakStyleSpecified && mLineBreakStyle != lineBreakStyle) {
+            mLineBreakStyle = lineBreakStyle;
             updated = true;
         }
-        if (isLineBreakWordStyleSpecified
-                && mLineBreakConfig.getLineBreakWordStyle() != lineBreakWordStyle) {
-            mLineBreakConfig.setLineBreakWordStyle(lineBreakWordStyle);
+        if (isLineBreakWordStyleSpecified && mLineBreakWordStyle != lineBreakWordStyle) {
+            mLineBreakWordStyle = lineBreakWordStyle;
             updated = true;
         }
         if (updated && mLayout != null) {
@@ -4871,50 +4870,72 @@
     }
 
     /**
-     * Sets line break configuration indicates which strategy needs to be used when calculating the
-     * text wrapping.
-     * <P>
-     * There are two types of line break rules that can be configured at the same time. One is
-     * line break style(lb) and the other is line break word style(lw). The line break style
-     * affects rule-based breaking. The line break word style affects dictionary-based breaking
-     * and provide phrase-based breaking opportunities. There are several types for the
-     * line break style:
+     * Set the line break style for text wrapping.
+     *
+     * The line break style to indicates the line break strategies can be used when
+     * calculating the text wrapping. The line break style affects rule-based breaking. It
+     * specifies the strictness of line-breaking rules.
+     * There are several types for the line break style:
      * {@link LineBreakConfig#LINE_BREAK_STYLE_LOOSE},
      * {@link LineBreakConfig#LINE_BREAK_STYLE_NORMAL} and
-     * {@link LineBreakConfig#LINE_BREAK_STYLE_STRICT}.
-     * The type for the line break word style is
-     * {@link LineBreakConfig#LINE_BREAK_WORD_STYLE_PHRASE}.
-     * The default values of the line break style and the line break word style are
-     * {@link LineBreakConfig#LINE_BREAK_STYLE_NONE} and
-     * {@link LineBreakConfig#LINE_BREAK_WORD_STYLE_NONE} respectively, indicating that no line
-     * breaking rules are specified.
-     * See <a href="https://drafts.csswg.org/css-text/#line-break-property">
+     * {@link LineBreakConfig#LINE_BREAK_STYLE_STRICT}. The default values of the line break style
+     * is {@link LineBreakConfig#LINE_BREAK_STYLE_NONE}, indicating no breaking rule is specified.
+     * See <a href="https://www.w3.org/TR/css-text-3/#line-break-property">
      *         the line-break property</a>
      *
-     * @param lineBreakConfig the line break config for text wrapping.
+     * @param lineBreakStyle the line break style for the text.
      */
-    public void setLineBreakConfig(@NonNull LineBreakConfig lineBreakConfig) {
-        Objects.requireNonNull(lineBreakConfig);
-        if (mLineBreakConfig.equals(lineBreakConfig)) {
-            return;
-        }
-        mLineBreakConfig.set(lineBreakConfig);
-        if (mLayout != null) {
-            nullLayouts();
-            requestLayout();
-            invalidate();
+    public void setLineBreakStyle(@LineBreakConfig.LineBreakStyle int lineBreakStyle) {
+        if (mLineBreakStyle != lineBreakStyle) {
+            mLineBreakStyle = lineBreakStyle;
+            if (mLayout != null) {
+                nullLayouts();
+                requestLayout();
+                invalidate();
+            }
         }
     }
 
     /**
-     * Get the current line break configuration for text wrapping.
+     * Set the line break word style for text wrapping.
      *
-     * @return the current line break configuration to be used for text wrapping.
+     * The line break word style affects dictionary-based breaking and provide phrase-based
+     * breaking opportunities. The type for the line break word style is
+     * {@link LineBreakConfig#LINE_BREAK_WORD_STYLE_PHRASE}. The default values of the line break
+     * word style is {@link LineBreakConfig#LINE_BREAK_WORD_STYLE_NONE}, indicating no breaking rule
+     * is specified.
+     * See <a href="https://www.w3.org/TR/css-text-3/#word-break-property">
+     *         the word-break property</a>
+     *
+     * @param lineBreakWordStyle the line break word style for the tet
      */
-    public @NonNull LineBreakConfig getLineBreakConfig() {
-        LineBreakConfig lbConfig = new LineBreakConfig();
-        lbConfig.set(mLineBreakConfig);
-        return lbConfig;
+    public void setLineBreakWordStyle(@LineBreakConfig.LineBreakWordStyle int lineBreakWordStyle) {
+        if (mLineBreakWordStyle != lineBreakWordStyle) {
+            mLineBreakWordStyle = lineBreakWordStyle;
+            if (mLayout != null) {
+                nullLayouts();
+                requestLayout();
+                invalidate();
+            }
+        }
+    }
+
+    /**
+     * Get the current line break style for text wrapping.
+     *
+     * @return the current line break style to be used for text wrapping.
+     */
+    public @LineBreakConfig.LineBreakStyle int getLineBreakStyle() {
+        return mLineBreakStyle;
+    }
+
+    /**
+     * Get the current line word break style for text wrapping.
+     *
+     * @return the current line break word style to be used for text wrapping.
+     */
+    public @LineBreakConfig.LineBreakWordStyle int getLineBreakWordStyle() {
+        return mLineBreakWordStyle;
     }
 
     /**
@@ -4924,7 +4945,8 @@
      * @see PrecomputedText
      */
     public @NonNull PrecomputedText.Params getTextMetricsParams() {
-        return new PrecomputedText.Params(new TextPaint(mTextPaint), mLineBreakConfig,
+        return new PrecomputedText.Params(new TextPaint(mTextPaint),
+                LineBreakConfig.getLineBreakConfig(mLineBreakStyle, mLineBreakWordStyle),
                 getTextDirectionHeuristic(),
                 mBreakStrategy, mHyphenationFrequency);
     }
@@ -4941,13 +4963,9 @@
         mTextDir = params.getTextDirection();
         mBreakStrategy = params.getBreakStrategy();
         mHyphenationFrequency = params.getHyphenationFrequency();
-        if (params.getLineBreakConfig() != null) {
-            mLineBreakConfig.set(params.getLineBreakConfig());
-        } else {
-            // Set default value if the line break config in the PrecomputedText.Params is null.
-            mLineBreakConfig.setLineBreakStyle(DEFAULT_LINE_BREAK_STYLE);
-            mLineBreakConfig.setLineBreakWordStyle(DEFAULT_LINE_BREAK_WORD_STYLE);
-        }
+        LineBreakConfig lineBreakConfig = params.getLineBreakConfig();
+        mLineBreakStyle = lineBreakConfig.getLineBreakStyle();
+        mLineBreakWordStyle = lineBreakConfig.getLineBreakWordStyle();
         if (mLayout != null) {
             nullLayouts();
             requestLayout();
@@ -6486,7 +6504,8 @@
             }
             final @PrecomputedText.Params.CheckResultUsableResult int checkResult =
                     precomputed.getParams().checkResultUsable(getPaint(), mTextDir, mBreakStrategy,
-                            mHyphenationFrequency, mLineBreakConfig);
+                            mHyphenationFrequency, LineBreakConfig.getLineBreakConfig(
+                                    mLineBreakStyle, mLineBreakWordStyle));
             switch (checkResult) {
                 case PrecomputedText.Params.UNUSABLE:
                     throw new IllegalArgumentException(
@@ -9383,7 +9402,8 @@
                         .setHyphenationFrequency(mHyphenationFrequency)
                         .setJustificationMode(mJustificationMode)
                         .setMaxLines(mMaxMode == LINES ? mMaximum : Integer.MAX_VALUE)
-                        .setLineBreakConfig(mLineBreakConfig);
+                        .setLineBreakConfig(LineBreakConfig.getLineBreakConfig(
+                                mLineBreakStyle, mLineBreakWordStyle));
                 if (shouldEllipsize) {
                     builder.setEllipsize(mEllipsize)
                             .setEllipsizedWidth(ellipsisWidth);
@@ -9498,7 +9518,8 @@
                     .setHyphenationFrequency(mHyphenationFrequency)
                     .setJustificationMode(mJustificationMode)
                     .setMaxLines(mMaxMode == LINES ? mMaximum : Integer.MAX_VALUE)
-                    .setLineBreakConfig(mLineBreakConfig);
+                    .setLineBreakConfig(LineBreakConfig.getLineBreakConfig(
+                            mLineBreakStyle, mLineBreakWordStyle));
             if (shouldEllipsize) {
                 builder.setEllipsize(effectiveEllipsize)
                         .setEllipsizedWidth(ellipsisWidth);
@@ -9866,7 +9887,8 @@
                 .setJustificationMode(getJustificationMode())
                 .setMaxLines(mMaxMode == LINES ? mMaximum : Integer.MAX_VALUE)
                 .setTextDirection(getTextDirectionHeuristic())
-                .setLineBreakConfig(mLineBreakConfig);
+                .setLineBreakConfig(LineBreakConfig.getLineBreakConfig(
+                        mLineBreakStyle, mLineBreakWordStyle));
 
         final StaticLayout layout = layoutBuilder.build();
 
diff --git a/core/java/android/window/ClientWindowFrames.java b/core/java/android/window/ClientWindowFrames.java
index acf9882..5b915cc 100644
--- a/core/java/android/window/ClientWindowFrames.java
+++ b/core/java/android/window/ClientWindowFrames.java
@@ -27,47 +27,55 @@
  */
 public class ClientWindowFrames implements Parcelable {
     /** The actual window bounds. */
-    public final @NonNull Rect frame;
+    public final @NonNull Rect frame = new Rect();
 
     /**
      * The container frame that is usually the same as display size. It may exclude the area of
      * insets if the window layout parameter has specified fit-insets-sides.
      */
-    public final @NonNull Rect displayFrame;
+    public final @NonNull Rect displayFrame = new Rect();
+
+    /**
+     * The frame to be referenced while applying gravity and MATCH_PARENT.
+     */
+    public final @NonNull Rect parentFrame = new Rect();
 
     /** The background area while the window is resizing. */
-    public final @NonNull Rect backdropFrame;
+    public final @NonNull Rect backdropFrame = new Rect();
+
+    public boolean isParentFrameClippedByDisplayCutout;
 
     public ClientWindowFrames() {
-        frame = new Rect();
-        displayFrame = new Rect();
-        backdropFrame = new Rect();
     }
 
     public ClientWindowFrames(ClientWindowFrames other) {
-        frame = new Rect(other.frame);
-        displayFrame = new Rect(other.displayFrame);
-        backdropFrame = new Rect(other.backdropFrame);
+        frame.set(other.frame);
+        displayFrame.set(other.displayFrame);
+        parentFrame.set(other.parentFrame);
+        backdropFrame.set(other.backdropFrame);
+        isParentFrameClippedByDisplayCutout = other.isParentFrameClippedByDisplayCutout;
     }
 
     private ClientWindowFrames(Parcel in) {
-        frame = Rect.CREATOR.createFromParcel(in);
-        displayFrame = Rect.CREATOR.createFromParcel(in);
-        backdropFrame = Rect.CREATOR.createFromParcel(in);
+        readFromParcel(in);
     }
 
     /** Needed for AIDL out parameters. */
     public void readFromParcel(Parcel in) {
         frame.readFromParcel(in);
         displayFrame.readFromParcel(in);
+        parentFrame.readFromParcel(in);
         backdropFrame.readFromParcel(in);
+        isParentFrameClippedByDisplayCutout = in.readBoolean();
     }
 
     @Override
     public void writeToParcel(Parcel dest, int flags) {
         frame.writeToParcel(dest, flags);
         displayFrame.writeToParcel(dest, flags);
+        parentFrame.writeToParcel(dest, flags);
         backdropFrame.writeToParcel(dest, flags);
+        dest.writeBoolean(isParentFrameClippedByDisplayCutout);
     }
 
     @Override
@@ -75,7 +83,9 @@
         final StringBuilder sb = new StringBuilder(32);
         return "ClientWindowFrames{frame=" + frame.toShortString(sb)
                 + " display=" + displayFrame.toShortString(sb)
-                + " backdrop=" + backdropFrame.toShortString(sb) + "}";
+                + " parentFrame=" + parentFrame.toShortString(sb)
+                + " backdrop=" + backdropFrame.toShortString(sb)
+                + " parentClippedByDisplayCutout=" + isParentFrameClippedByDisplayCutout + "}";
     }
 
     @Override
diff --git a/core/java/android/window/DisplayWindowPolicyController.java b/core/java/android/window/DisplayWindowPolicyController.java
index 3359a41..1270d87 100644
--- a/core/java/android/window/DisplayWindowPolicyController.java
+++ b/core/java/android/window/DisplayWindowPolicyController.java
@@ -17,12 +17,14 @@
 package android.window;
 
 import android.annotation.NonNull;
+import android.app.WindowConfiguration;
 import android.content.ComponentName;
 import android.content.pm.ActivityInfo;
 import android.util.ArraySet;
 
 import java.io.PrintWriter;
 import java.util.List;
+import java.util.Set;
 
 /**
  * Abstract class to control the policies of the windows that can be displayed on the virtual
@@ -46,6 +48,22 @@
     private int mSystemWindowFlags;
 
     /**
+     * The set of windowing mode that are supported in this display.
+     * @see android.app.WindowConfiguration.WindowingMode
+     */
+    private final Set<Integer> mSupportedWindowingModes = new ArraySet<>();
+
+    /**
+     * A controller to control the policies of the windows that can be displayed on the virtual
+     * display.
+     */
+    public DisplayWindowPolicyController() {
+        synchronized (mSupportedWindowingModes) {
+            mSupportedWindowingModes.add(WindowConfiguration.WINDOWING_MODE_FULLSCREEN);
+        }
+    }
+
+    /**
      * Returns {@code true} if the given window flags contain the flags that we're interested in.
      */
     public final boolean isInterestedWindowFlags(int windowFlags, int systemWindowFlags) {
@@ -62,9 +80,34 @@
     }
 
     /**
-     * Returns {@code true} if the given activities can be displayed on this virtual display.
+     * Returns {@code true} if the given windowing mode is supported in this display.
      */
-    public abstract boolean canContainActivities(@NonNull List<ActivityInfo> activities);
+    public final boolean isWindowingModeSupported(
+            @WindowConfiguration.WindowingMode int windowingMode) {
+        synchronized (mSupportedWindowingModes) {
+            return mSupportedWindowingModes.contains(windowingMode);
+        }
+    }
+
+    /**
+     * Sets the windowing modes are supported in this display.
+     *
+     * @param supportedWindowingModes The set of
+     * {@link android.app.WindowConfiguration.WindowingMode}.
+     */
+    public final void setSupportedWindowingModes(Set<Integer> supportedWindowingModes) {
+        synchronized (mSupportedWindowingModes) {
+            mSupportedWindowingModes.clear();
+            mSupportedWindowingModes.addAll(supportedWindowingModes);
+        }
+    }
+
+    /**
+     * Returns {@code true} if the given activities can be displayed on this virtual display and
+     * the windowing mode is supported.
+     */
+    public abstract boolean canContainActivities(@NonNull List<ActivityInfo> activities,
+            @WindowConfiguration.WindowingMode int windowingMode);
 
     /**
      * Called when an Activity window is layouted with the new changes where contains the
diff --git a/core/java/android/window/ITaskOrganizerController.aidl b/core/java/android/window/ITaskOrganizerController.aidl
index 022d05d..172456e4 100644
--- a/core/java/android/window/ITaskOrganizerController.aidl
+++ b/core/java/android/window/ITaskOrganizerController.aidl
@@ -52,7 +52,7 @@
     /** Gets all root tasks on a display (ordered from top-to-bottom) */
     List<ActivityManager.RunningTaskInfo> getRootTasks(int displayId, in int[] activityTypes);
 
-    /** Get the root task which contains the current ime target */
+    /** Get the {@link WindowContainerToken} of the task which contains the current ime target */
     WindowContainerToken getImeTarget(int display);
 
     /**
diff --git a/core/java/android/window/TaskOrganizer.java b/core/java/android/window/TaskOrganizer.java
index 3ec18db..8d88f80 100644
--- a/core/java/android/window/TaskOrganizer.java
+++ b/core/java/android/window/TaskOrganizer.java
@@ -199,7 +199,7 @@
         }
     }
 
-    /** Get the root task which contains the current ime target */
+    /** Get the {@link WindowContainerToken} of the task which contains the current ime target */
     @RequiresPermission(android.Manifest.permission.MANAGE_ACTIVITY_TASKS)
     @Nullable
     public WindowContainerToken getImeTarget(int display) {
diff --git a/core/java/android/window/WindowOnBackInvokedDispatcher.java b/core/java/android/window/WindowOnBackInvokedDispatcher.java
index 5f82fb0..d046cef 100644
--- a/core/java/android/window/WindowOnBackInvokedDispatcher.java
+++ b/core/java/android/window/WindowOnBackInvokedDispatcher.java
@@ -28,6 +28,7 @@
 import android.view.IWindow;
 import android.view.IWindowSession;
 
+import java.lang.ref.WeakReference;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.TreeMap;
@@ -52,7 +53,7 @@
     private static final String TAG = "WindowOnBackDispatcher";
     private static final String BACK_PREDICTABILITY_PROP = "persist.debug.back_predictability";
     private static final boolean IS_BACK_PREDICTABILITY_ENABLED = SystemProperties
-            .getInt(BACK_PREDICTABILITY_PROP, 0) > 0;
+            .getInt(BACK_PREDICTABILITY_PROP, 1) > 0;
 
     /** Convenience hashmap to quickly decide if a callback has been added. */
     private final HashMap<OnBackInvokedCallback, Integer> mAllCallbacks = new HashMap<>();
@@ -185,35 +186,58 @@
     }
 
     private static class OnBackInvokedCallbackWrapper extends IOnBackInvokedCallback.Stub {
-        private final OnBackInvokedCallback mCallback;
+        private final WeakReference<OnBackInvokedCallback> mCallback;
 
         OnBackInvokedCallbackWrapper(@NonNull OnBackInvokedCallback callback) {
-            mCallback = callback;
-        }
-
-        @NonNull
-        public OnBackInvokedCallback getCallback() {
-            return mCallback;
+            mCallback = new WeakReference<>(callback);
         }
 
         @Override
         public void onBackStarted() {
-            Handler.getMain().post(() -> mCallback.onBackStarted());
+            Handler.getMain().post(() -> {
+                final OnBackInvokedCallback callback = mCallback.get();
+                if (callback == null) {
+                    return;
+                }
+
+                callback.onBackStarted();
+            });
         }
 
         @Override
         public void onBackProgressed(BackEvent backEvent) {
-            Handler.getMain().post(() -> mCallback.onBackProgressed(backEvent));
+            Handler.getMain().post(() -> {
+                final OnBackInvokedCallback callback = mCallback.get();
+                if (callback == null) {
+                    return;
+                }
+
+                callback.onBackProgressed(backEvent);
+            });
         }
 
         @Override
         public void onBackCancelled() {
-            Handler.getMain().post(() -> mCallback.onBackCancelled());
+            Handler.getMain().post(() -> {
+                final OnBackInvokedCallback callback = mCallback.get();
+                if (callback == null) {
+                    return;
+                }
+
+                callback.onBackCancelled();
+            });
         }
 
         @Override
         public void onBackInvoked() throws RemoteException {
-            Handler.getMain().post(() -> mCallback.onBackInvoked());
+            Handler.getMain().post(() -> {
+                final OnBackInvokedCallback callback = mCallback.get();
+                if (callback == null) {
+                    return;
+                }
+
+                callback.onBackInvoked();
+            });
         }
     }
 
diff --git a/core/java/com/android/internal/graphics/SfVsyncFrameCallbackProvider.java b/core/java/com/android/internal/graphics/SfVsyncFrameCallbackProvider.java
index 45555bf..dbbe4b9 100644
--- a/core/java/com/android/internal/graphics/SfVsyncFrameCallbackProvider.java
+++ b/core/java/com/android/internal/graphics/SfVsyncFrameCallbackProvider.java
@@ -24,6 +24,7 @@
  *
  * @hide
  */
+// TODO(b/222698397): remove getSfInstance/this class usage and use vsyncId for transactions
 public final class SfVsyncFrameCallbackProvider implements AnimationFrameCallbackProvider {
 
     private final Choreographer mChoreographer;
diff --git a/core/java/com/android/internal/infra/ServiceConnector.java b/core/java/com/android/internal/infra/ServiceConnector.java
index 9e07f97..cb16267 100644
--- a/core/java/com/android/internal/infra/ServiceConnector.java
+++ b/core/java/com/android/internal/infra/ServiceConnector.java
@@ -31,6 +31,7 @@
 import android.os.RemoteException;
 import android.os.UserHandle;
 import android.util.Log;
+import android.util.Slog;
 
 import java.io.PrintWriter;
 import java.util.ArrayDeque;
@@ -562,10 +563,21 @@
         void unbindJobThread() {
             cancelTimeout();
             I service = mService;
+            // TODO(b/224695239): This is actually checking wasConnected. Rename and/or fix
+            // implementation based on what this should actually be checking. At least the first
+            // check for calling unbind is the correct behavior, though.
             boolean wasBound = service != null;
+            if (wasBound || mBinding) {
+                try {
+                    mContext.unbindService(mServiceConnection);
+                } catch (IllegalArgumentException e) {  // TODO(b/224697137): Fix the race condition
+                                                        // that requires catching this (crashes if
+                                                        // service isn't currently bound).
+                    Slog.e(LOG_TAG, "Failed to unbind: " + e);
+                }
+            }
             if (wasBound) {
                 dispatchOnServiceConnectionStatusChanged(service, false);
-                mContext.unbindService(mServiceConnection);
                 service.asBinder().unlinkToDeath(this, 0);
                 mService = null;
             }
diff --git a/core/java/com/android/internal/jank/InteractionJankMonitor.java b/core/java/com/android/internal/jank/InteractionJankMonitor.java
index 3746bfd..5947e66 100644
--- a/core/java/com/android/internal/jank/InteractionJankMonitor.java
+++ b/core/java/com/android/internal/jank/InteractionJankMonitor.java
@@ -62,6 +62,10 @@
 import static com.android.internal.util.FrameworkStatsLog.UIINTERACTION_FRAME_INFO_REPORTED__INTERACTION_TYPE__SPLASHSCREEN_AVD;
 import static com.android.internal.util.FrameworkStatsLog.UIINTERACTION_FRAME_INFO_REPORTED__INTERACTION_TYPE__SPLASHSCREEN_EXIT_ANIM;
 import static com.android.internal.util.FrameworkStatsLog.UIINTERACTION_FRAME_INFO_REPORTED__INTERACTION_TYPE__STATUS_BAR_APP_LAUNCH_FROM_CALL_CHIP;
+import static com.android.internal.util.FrameworkStatsLog.UIINTERACTION_FRAME_INFO_REPORTED__INTERACTION_TYPE__SUW_LOADING_SCREEN_FOR_STATUS;
+import static com.android.internal.util.FrameworkStatsLog.UIINTERACTION_FRAME_INFO_REPORTED__INTERACTION_TYPE__SUW_LOADING_TO_NEXT_FLOW;
+import static com.android.internal.util.FrameworkStatsLog.UIINTERACTION_FRAME_INFO_REPORTED__INTERACTION_TYPE__SUW_LOADING_TO_SHOW_INFO_WITH_ACTIONS;
+import static com.android.internal.util.FrameworkStatsLog.UIINTERACTION_FRAME_INFO_REPORTED__INTERACTION_TYPE__SUW_SHOW_FUNCTION_SCREEN_WITH_ACTIONS;
 import static com.android.internal.util.FrameworkStatsLog.UIINTERACTION_FRAME_INFO_REPORTED__INTERACTION_TYPE__UNFOLD_ANIM;
 import static com.android.internal.util.FrameworkStatsLog.UIINTERACTION_FRAME_INFO_REPORTED__INTERACTION_TYPE__USER_SWITCH;
 import static com.android.internal.util.FrameworkStatsLog.UIINTERACTION_FRAME_INFO_REPORTED__INTERACTION_TYPE__WALLPAPER_TRANSITION;
@@ -176,6 +180,10 @@
     public static final int CUJ_ONE_HANDED_ENTER_TRANSITION = 42;
     public static final int CUJ_ONE_HANDED_EXIT_TRANSITION = 43;
     public static final int CUJ_UNFOLD_ANIM = 44;
+    public static final int CUJ_SUW_LOADING_TO_SHOW_INFO_WITH_ACTIONS = 45;
+    public static final int CUJ_SUW_SHOW_FUNCTION_SCREEN_WITH_ACTIONS = 46;
+    public static final int CUJ_SUW_LOADING_TO_NEXT_FLOW = 47;
+    public static final int CUJ_SUW_LOADING_SCREEN_FOR_STATUS = 48;
 
     private static final int NO_STATSD_LOGGING = -1;
 
@@ -229,6 +237,10 @@
             UIINTERACTION_FRAME_INFO_REPORTED__INTERACTION_TYPE__ONE_HANDED_ENTER_TRANSITION,
             UIINTERACTION_FRAME_INFO_REPORTED__INTERACTION_TYPE__ONE_HANDED_EXIT_TRANSITION,
             UIINTERACTION_FRAME_INFO_REPORTED__INTERACTION_TYPE__UNFOLD_ANIM,
+            UIINTERACTION_FRAME_INFO_REPORTED__INTERACTION_TYPE__SUW_LOADING_TO_SHOW_INFO_WITH_ACTIONS,
+            UIINTERACTION_FRAME_INFO_REPORTED__INTERACTION_TYPE__SUW_SHOW_FUNCTION_SCREEN_WITH_ACTIONS,
+            UIINTERACTION_FRAME_INFO_REPORTED__INTERACTION_TYPE__SUW_LOADING_TO_NEXT_FLOW,
+            UIINTERACTION_FRAME_INFO_REPORTED__INTERACTION_TYPE__SUW_LOADING_SCREEN_FOR_STATUS,
     };
 
     private static volatile InteractionJankMonitor sInstance;
@@ -294,6 +306,10 @@
             CUJ_ONE_HANDED_ENTER_TRANSITION,
             CUJ_ONE_HANDED_EXIT_TRANSITION,
             CUJ_UNFOLD_ANIM,
+            CUJ_SUW_LOADING_TO_SHOW_INFO_WITH_ACTIONS,
+            CUJ_SUW_SHOW_FUNCTION_SCREEN_WITH_ACTIONS,
+            CUJ_SUW_LOADING_TO_NEXT_FLOW,
+            CUJ_SUW_LOADING_SCREEN_FOR_STATUS
     })
     @Retention(RetentionPolicy.SOURCE)
     public @interface CujType {
@@ -701,6 +717,14 @@
                 return "ONE_HANDED_EXIT_TRANSITION";
             case CUJ_UNFOLD_ANIM:
                 return "UNFOLD_ANIM";
+            case CUJ_SUW_LOADING_TO_SHOW_INFO_WITH_ACTIONS:
+                return "SUW_LOADING_TO_SHOW_INFO_WITH_ACTIONS";
+            case CUJ_SUW_SHOW_FUNCTION_SCREEN_WITH_ACTIONS:
+                return "SUW_SHOW_FUNCTION_SCREEN_WITH_ACTIONS";
+            case CUJ_SUW_LOADING_TO_NEXT_FLOW:
+                return "SUW_LOADING_TO_NEXT_FLOW";
+            case CUJ_SUW_LOADING_SCREEN_FOR_STATUS:
+                return "SUW_LOADING_SCREEN_FOR_STATUS";
         }
         return "UNKNOWN";
     }
diff --git a/core/java/com/android/internal/os/AudioPowerCalculator.java b/core/java/com/android/internal/os/AudioPowerCalculator.java
index f9310b0..ebf0ca2 100644
--- a/core/java/com/android/internal/os/AudioPowerCalculator.java
+++ b/core/java/com/android/internal/os/AudioPowerCalculator.java
@@ -78,7 +78,9 @@
         final double powerMah = mPowerEstimator.calculatePower(durationMs);
         app.setUsageDurationMillis(BatteryConsumer.POWER_COMPONENT_AUDIO, durationMs)
                 .setConsumedPower(BatteryConsumer.POWER_COMPONENT_AUDIO, powerMah);
-        total.durationMs += durationMs;
-        total.powerMah += powerMah;
+        if (!app.isVirtualUid()) {
+            total.durationMs += durationMs;
+            total.powerMah += powerMah;
+        }
     }
 }
diff --git a/core/java/com/android/internal/os/BatteryStatsImpl.java b/core/java/com/android/internal/os/BatteryStatsImpl.java
index 70b9639..3b46f62 100644
--- a/core/java/com/android/internal/os/BatteryStatsImpl.java
+++ b/core/java/com/android/internal/os/BatteryStatsImpl.java
@@ -166,7 +166,7 @@
     private static final int MAGIC = 0xBA757475; // 'BATSTATS'
 
     // Current on-disk Parcel version
-    static final int VERSION = 206;
+    static final int VERSION = 207;
 
     // The maximum number of names wakelocks we will keep track of
     // per uid; once the limit is reached, we batch the remaining wakelocks
@@ -546,9 +546,9 @@
                 final LongArrayMultiStateCounter onBatteryScreenOffCounter =
                         u.getProcStateScreenOffTimeCounter().getCounter();
 
-                if (uid == parentUid) {
-                    mKernelSingleUidTimeReader.addDelta(uid, onBatteryCounter, timestampMs);
-                    mKernelSingleUidTimeReader.addDelta(uid, onBatteryScreenOffCounter,
+                if (uid == parentUid || Process.isSdkSandboxUid(uid)) {
+                    mKernelSingleUidTimeReader.addDelta(parentUid, onBatteryCounter, timestampMs);
+                    mKernelSingleUidTimeReader.addDelta(parentUid, onBatteryScreenOffCounter,
                             timestampMs);
                 } else {
                     Uid.ChildUid childUid = u.getChildUid(uid);
@@ -964,6 +964,16 @@
          * Timers for each combination of frequency range and signal strength.
          */
         public final StopwatchTimer[][] perStateTimers;
+        /**
+         * Counters tracking the time (in milliseconds) spent transmitting data in a given state.
+         */
+        @Nullable
+        private LongSamplingCounter[][] mPerStateTxDurationMs = null;
+        /**
+         * Counters tracking the time (in milliseconds) spent receiving data in at given frequency.
+         */
+        @Nullable
+        private LongSamplingCounter[] mPerFrequencyRxDurationMs = null;
 
         RadioAccessTechnologyBatteryStats(int freqCount, Clock clock, TimeBase timeBase) {
             perStateTimers =
@@ -1024,15 +1034,198 @@
         }
 
         /**
-         * Reset display timers.
+         * Returns the duration in milliseconds spent in a given state since the last mark.
+         */
+        public long getTimeSinceMark(@ServiceState.FrequencyRange int frequencyRange,
+                int signalStrength, long elapsedRealtimeMs) {
+            return perStateTimers[frequencyRange][signalStrength].getTimeSinceMarkLocked(
+                    elapsedRealtimeMs * 1000) / 1000;
+        }
+
+        /**
+         * Set mark for all timers.
+         */
+        public void setMark(long elapsedRealtimeMs) {
+            final int size = perStateTimers.length;
+            for (int i = 0; i < size; i++) {
+                for (int j = 0; j < CellSignalStrength.NUM_SIGNAL_STRENGTH_BINS; j++) {
+                    perStateTimers[i][j].setMark(elapsedRealtimeMs);
+                }
+            }
+        }
+
+        /**
+         * Returns numbers of frequencies tracked for this RAT.
+         */
+        public int getFrequencyRangeCount() {
+            return perStateTimers.length;
+        }
+
+        /**
+         * Add TX time for a given state.
+         */
+        public void incrementTxDuration(@ServiceState.FrequencyRange int frequencyRange,
+                int signalStrength, long durationMs) {
+            getTxDurationCounter(frequencyRange, signalStrength, true).addCountLocked(durationMs);
+        }
+
+        /**
+         * Add TX time for a given frequency.
+         */
+        public void incrementRxDuration(@ServiceState.FrequencyRange int frequencyRange,
+                long durationMs) {
+            getRxDurationCounter(frequencyRange, true).addCountLocked(durationMs);
+        }
+
+        /**
+         * Reset radio access technology timers and counts.
          */
         public void reset(long elapsedRealtimeUs) {
             final int size = perStateTimers.length;
             for (int i = 0; i < size; i++) {
                 for (int j = 0; j < CellSignalStrength.NUM_SIGNAL_STRENGTH_BINS; j++) {
                     perStateTimers[i][j].reset(false, elapsedRealtimeUs);
+                    if (mPerStateTxDurationMs == null) continue;
+                    mPerStateTxDurationMs[i][j].reset(false, elapsedRealtimeUs);
+                }
+                if (mPerFrequencyRxDurationMs == null) continue;
+                mPerFrequencyRxDurationMs[i].reset(false, elapsedRealtimeUs);
+            }
+        }
+
+        /**
+         * Write data to summary parcel
+         */
+        public void writeSummaryToParcel(Parcel out, long elapsedRealtimeUs) {
+            final int freqCount = perStateTimers.length;
+            out.writeInt(freqCount);
+            out.writeInt(CellSignalStrength.NUM_SIGNAL_STRENGTH_BINS);
+            for (int i = 0; i < freqCount; i++) {
+                for (int j = 0; j < CellSignalStrength.NUM_SIGNAL_STRENGTH_BINS; j++) {
+                    perStateTimers[i][j].writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
                 }
             }
+
+            if (mPerStateTxDurationMs == null) {
+                out.writeInt(0);
+            } else {
+                out.writeInt(1);
+                for (int i = 0; i < freqCount; i++) {
+                    for (int j = 0; j < CellSignalStrength.NUM_SIGNAL_STRENGTH_BINS; j++) {
+                        mPerStateTxDurationMs[i][j].writeSummaryFromParcelLocked(out);
+                    }
+                }
+            }
+
+            if (mPerFrequencyRxDurationMs == null) {
+                out.writeInt(0);
+            } else {
+                out.writeInt(1);
+                for (int i = 0; i < freqCount; i++) {
+                    mPerFrequencyRxDurationMs[i].writeSummaryFromParcelLocked(out);
+                }
+            }
+        }
+
+        /**
+         * Read data from summary parcel
+         */
+        public void readSummaryFromParcel(Parcel in) {
+            final int oldFreqCount = in.readInt();
+            final int oldSignalStrengthCount = in.readInt();
+            final int currFreqCount = perStateTimers.length;
+            final int currSignalStrengthCount = CellSignalStrength.NUM_SIGNAL_STRENGTH_BINS;
+
+            for (int freq = 0; freq < oldFreqCount; freq++) {
+                for (int strength = 0; strength < oldSignalStrengthCount; strength++) {
+                    if (freq >= currFreqCount || strength >= currSignalStrengthCount) {
+                        // Mismatch with the summary parcel. Consume the data but don't use it.
+                        final StopwatchTimer temp = new StopwatchTimer(null, null, -1, null,
+                                new TimeBase());
+                        // Consume perStateTimers data.
+                        temp.readSummaryFromParcelLocked(in);
+                    } else {
+                        perStateTimers[freq][strength].readSummaryFromParcelLocked(in);
+                    }
+                }
+            }
+
+            if (in.readInt() == 1) {
+                for (int freq = 0; freq < oldFreqCount; freq++) {
+                    for (int strength = 0; strength < oldSignalStrengthCount; strength++) {
+                        if (freq >= currFreqCount || strength >= currSignalStrengthCount) {
+                            // Mismatch with the summary parcel. Consume the data but don't use it.
+                            final StopwatchTimer temp = new StopwatchTimer(null, null, -1, null,
+                                    new TimeBase());
+                            // Consume mPerStateTxDurationMs data.
+                            temp.readSummaryFromParcelLocked(in);
+                        }
+                        getTxDurationCounter(freq, strength, true).readSummaryFromParcelLocked(in);
+                    }
+                }
+            }
+
+            if (in.readInt() == 1) {
+                for (int freq = 0; freq < oldFreqCount; freq++) {
+                    if (freq >= currFreqCount) {
+                        // Mismatch with the summary parcel. Consume the data but don't use it.
+                        final StopwatchTimer
+                                temp = new StopwatchTimer(null, null, -1, null, new TimeBase());
+                        // Consume mPerFrequencyRxDurationMs data.
+                        temp.readSummaryFromParcelLocked(in);
+                        continue;
+                    }
+                    getRxDurationCounter(freq, true).readSummaryFromParcelLocked(in);
+                }
+            }
+        }
+
+        private LongSamplingCounter getTxDurationCounter(
+                @ServiceState.FrequencyRange int frequencyRange, int signalStrength, boolean make) {
+            if (mPerStateTxDurationMs == null) {
+                if (!make) return null;
+
+                final int freqCount = getFrequencyRangeCount();
+                final int signalStrengthCount = perStateTimers[0].length;
+                final TimeBase timeBase = perStateTimers[0][0].mTimeBase;
+                mPerStateTxDurationMs = new LongSamplingCounter[freqCount][signalStrengthCount];
+                for (int freq = 0; freq < freqCount; freq++) {
+                    for (int strength = 0; strength < signalStrengthCount; strength++) {
+                        mPerStateTxDurationMs[freq][strength] = new LongSamplingCounter(timeBase);
+                    }
+                }
+            }
+            if (frequencyRange < 0 || frequencyRange >= getFrequencyRangeCount()) {
+                Slog.w(TAG, "Unexpected frequency range (" + frequencyRange
+                        + ") requested in getTxDurationCounter");
+                return null;
+            }
+            if (signalStrength < 0 || signalStrength >= perStateTimers[0].length) {
+                Slog.w(TAG, "Unexpected signal strength (" + signalStrength
+                        + ") requested in getTxDurationCounter");
+                return null;
+            }
+            return mPerStateTxDurationMs[frequencyRange][signalStrength];
+        }
+
+        private LongSamplingCounter getRxDurationCounter(
+                @ServiceState.FrequencyRange int frequencyRange, boolean make) {
+            if (mPerFrequencyRxDurationMs == null) {
+                if (!make) return null;
+
+                final int freqCount = getFrequencyRangeCount();
+                final TimeBase timeBase = perStateTimers[0][0].mTimeBase;
+                mPerFrequencyRxDurationMs = new LongSamplingCounter[freqCount];
+                for (int freq = 0; freq < freqCount; freq++) {
+                    mPerFrequencyRxDurationMs[freq] = new LongSamplingCounter(timeBase);
+                }
+            }
+            if (frequencyRange < 0 || frequencyRange >= getFrequencyRangeCount()) {
+                Slog.w(TAG, "Unexpected frequency range (" + frequencyRange
+                        + ") requested in getRxDurationCounter");
+                return null;
+            }
+            return mPerFrequencyRxDurationMs[frequencyRange];
         }
     }
 
@@ -1929,18 +2122,26 @@
         private final TimeBase mTimeBase;
         private final LongMultiStateCounter mCounter;
 
-        private TimeMultiStateCounter(TimeBase timeBase, Parcel in, long timestampMs) {
+        private TimeMultiStateCounter(TimeBase timeBase, int stateCount, long timestampMs) {
+            this(timeBase, new LongMultiStateCounter(stateCount), timestampMs);
+        }
+
+        private TimeMultiStateCounter(TimeBase timeBase, LongMultiStateCounter counter,
+                long timestampMs) {
             mTimeBase = timeBase;
-            mCounter = LongMultiStateCounter.CREATOR.createFromParcel(in);
+            mCounter = counter;
             mCounter.setEnabled(mTimeBase.isRunning(), timestampMs);
             timeBase.add(this);
         }
 
-        private TimeMultiStateCounter(TimeBase timeBase, int stateCount, long timestampMs) {
-            mTimeBase = timeBase;
-            mCounter = new LongMultiStateCounter(stateCount);
-            mCounter.setEnabled(mTimeBase.isRunning(), timestampMs);
-            timeBase.add(this);
+        @Nullable
+        private static TimeMultiStateCounter readFromParcel(Parcel in, TimeBase timeBase,
+                int stateCount, long timestampMs) {
+            LongMultiStateCounter counter = LongMultiStateCounter.CREATOR.createFromParcel(in);
+            if (counter.getStateCount() != stateCount) {
+                return null;
+            }
+            return new TimeMultiStateCounter(timeBase, counter, timestampMs);
         }
 
         private void writeToParcel(Parcel out) {
@@ -3510,11 +3711,8 @@
 
         private TimeMultiStateCounter readTimeMultiStateCounter(Parcel in, TimeBase timeBase) {
             if (in.readBoolean()) {
-                final TimeMultiStateCounter counter =
-                        new TimeMultiStateCounter(timeBase, in, mClock.elapsedRealtime());
-                if (counter.getStateCount() == BatteryConsumer.PROCESS_STATE_COUNT) {
-                    return counter;
-                }
+                return TimeMultiStateCounter.readFromParcel(in, timeBase,
+                        BatteryConsumer.PROCESS_STATE_COUNT, mClock.elapsedRealtime());
             }
             return null;
         }
@@ -3537,9 +3735,10 @@
                 // invalid.
                 TimeMultiStateCounter[] counters = new TimeMultiStateCounter[numCounters];
                 for (int i = 0; i < numCounters; i++) {
-                    final TimeMultiStateCounter counter =
-                            new TimeMultiStateCounter(timeBase, in, mClock.elapsedRealtime());
-                    if (counter.getStateCount() == BatteryConsumer.PROCESS_STATE_COUNT) {
+                    final TimeMultiStateCounter counter = TimeMultiStateCounter.readFromParcel(in,
+                            timeBase, BatteryConsumer.PROCESS_STATE_COUNT,
+                            mClock.elapsedRealtime());
+                    if (counter != null) {
                         counters[i] = counter;
                     } else {
                         valid = false;
@@ -4560,7 +4759,10 @@
         mIsolatedUidRefCounts.put(uid, refCount + 1);
     }
 
-    public int mapUid(int uid) {
+    private int mapUid(int uid) {
+        if (Process.isSdkSandboxUid(uid)) {
+            return Process.getAppUidForSdkSandboxUid(uid);
+        }
         int isolated = mIsolatedUids.get(uid, -1);
         return isolated > 0 ? isolated : uid;
     }
@@ -4656,16 +4858,18 @@
             long elapsedRealtimeMs, long uptimeMs) {
         int parentUid = mapUid(uid);
         if (uid != parentUid) {
-            // Isolated UIDs process state is already rolled up into parent, so no need to track
-            // Otherwise the parent's process state will get downgraded incorrectly
-            return;
+            if (Process.isIsolated(uid)) {
+                // Isolated UIDs process state is already rolled up into parent, so no need to track
+                // Otherwise the parent's process state will get downgraded incorrectly
+                return;
+            }
         }
         // TODO(b/155216561): It is possible for isolated uids to be in a higher
         // state than its parent uid. We should track the highest state within the union of host
         // and isolated uids rather than only the parent uid.
         FrameworkStatsLog.write(FrameworkStatsLog.UID_PROCESS_STATE_CHANGED, uid,
                 ActivityManager.processStateAmToProto(state));
-        getUidStatsLocked(uid, elapsedRealtimeMs, uptimeMs)
+        getUidStatsLocked(parentUid, elapsedRealtimeMs, uptimeMs)
                 .updateUidProcessStateLocked(state, elapsedRealtimeMs, uptimeMs);
     }
 
@@ -7995,6 +8199,32 @@
                 elapsedRealtimeMs * 1000, STATS_SINCE_CHARGED) / 1000;
     }
 
+    @Override
+    public long getActiveTxRadioDurationMs(@RadioAccessTechnology int rat,
+            @ServiceState.FrequencyRange int frequencyRange, int signalStrength,
+            long elapsedRealtimeMs) {
+        final RadioAccessTechnologyBatteryStats stats = mPerRatBatteryStats[rat];
+        if (stats == null) return DURATION_UNAVAILABLE;
+
+        final LongSamplingCounter counter = stats.getTxDurationCounter(frequencyRange,
+                signalStrength, false);
+        if (counter == null) return DURATION_UNAVAILABLE;
+
+        return counter.getCountLocked(STATS_SINCE_CHARGED);
+    }
+
+    @Override
+    public long getActiveRxRadioDurationMs(@RadioAccessTechnology int rat,
+            @ServiceState.FrequencyRange int frequencyRange, long elapsedRealtimeMs) {
+        final RadioAccessTechnologyBatteryStats stats = mPerRatBatteryStats[rat];
+        if (stats == null) return DURATION_UNAVAILABLE;
+
+        final LongSamplingCounter counter = stats.getRxDurationCounter(frequencyRange, false);
+        if (counter == null) return DURATION_UNAVAILABLE;
+
+        return counter.getCountLocked(STATS_SINCE_CHARGED);
+    }
+
     @UnsupportedAppUsage
     @Override public long getMobileRadioActiveTime(long elapsedRealtimeUs, int which) {
         return mMobileRadioActiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
@@ -10739,11 +10969,9 @@
                             = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
                 }
                 if (in.readBoolean()) {
-                    final TimeMultiStateCounter counter =
-                            new TimeMultiStateCounter(mBsi.mOnBatteryTimeBase, in, timestampMs);
-                    if (counter.getStateCount() == BatteryConsumer.PROCESS_STATE_COUNT) {
-                        mMobileRadioActiveTime = counter;
-                    }
+                    mMobileRadioActiveTime = TimeMultiStateCounter.readFromParcel(in,
+                            mBsi.mOnBatteryTimeBase, BatteryConsumer.PROCESS_STATE_COUNT,
+                            timestampMs);
                 }
 
                 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
@@ -10789,11 +11017,9 @@
 
             int stateCount = in.readInt();
             if (stateCount != 0) {
-                final TimeMultiStateCounter counter = new TimeMultiStateCounter(
-                        mBsi.mOnBatteryTimeBase, in, timestampMs);
-                if (stateCount == BatteryConsumer.PROCESS_STATE_COUNT) {
-                    mCpuActiveTimeMs = counter;
-                }
+                mCpuActiveTimeMs = TimeMultiStateCounter.readFromParcel(in,
+                        mBsi.mOnBatteryTimeBase, BatteryConsumer.PROCESS_STATE_COUNT,
+                        timestampMs);
             }
             mCpuClusterTimesMs = new LongSamplingCounterArray(mBsi.mOnBatteryTimeBase, in);
 
@@ -13477,6 +13703,67 @@
                     addHistoryRecordLocked(elapsedRealtimeMs, uptimeMs);
                     mTmpRailStats.resetCellularTotalEnergyUsed();
                 }
+
+                // Proportionally smear Rx and Tx times across each RAt
+                final int levelCount = CellSignalStrength.getNumSignalStrengthLevels();
+                long[] perSignalStrengthActiveTimeMs = new long[levelCount];
+                long totalActiveTimeMs = 0;
+
+                for (int rat = 0; rat < RADIO_ACCESS_TECHNOLOGY_COUNT; rat++) {
+                    final RadioAccessTechnologyBatteryStats ratStats = mPerRatBatteryStats[rat];
+                    if (ratStats == null) continue;
+
+                    final int freqCount = ratStats.getFrequencyRangeCount();
+                    for (int freq = 0; freq < freqCount; freq++) {
+                        for (int level = 0; level < levelCount; level++) {
+                            final long durationMs = ratStats.getTimeSinceMark(freq, level,
+                                    elapsedRealtimeMs);
+                            perSignalStrengthActiveTimeMs[level] += durationMs;
+                            totalActiveTimeMs += durationMs;
+                        }
+                    }
+                }
+
+                if (totalActiveTimeMs != 0) {
+                    // Smear the provided Tx/Rx durations across each RAT, frequency, and signal
+                    // strength.
+                    for (int rat = 0; rat < RADIO_ACCESS_TECHNOLOGY_COUNT; rat++) {
+                        final RadioAccessTechnologyBatteryStats ratStats = mPerRatBatteryStats[rat];
+                        if (ratStats == null) continue;
+
+                        final int freqCount = ratStats.getFrequencyRangeCount();
+                        for (int freq = 0; freq < freqCount; freq++) {
+                            long frequencyDurationMs = 0;
+                            for (int level = 0; level < levelCount; level++) {
+                                final long durationMs = ratStats.getTimeSinceMark(freq, level,
+                                        elapsedRealtimeMs);
+                                final long totalLvlDurationMs =
+                                        perSignalStrengthActiveTimeMs[level];
+                                if (totalLvlDurationMs == 0) continue;
+                                final long totalTxLvlDurations =
+                                        deltaInfo.getTransmitDurationMillisAtPowerLevel(level);
+                                // Smear HAL provided Tx power level duration based on active modem
+                                // duration in a given state. (Add totalLvlDurationMs / 2 before
+                                // the integer division with totalLvlDurationMs for rounding.)
+                                final long proportionalTxDurationMs =
+                                        (durationMs * totalTxLvlDurations
+                                                + (totalLvlDurationMs / 2)) / totalLvlDurationMs;
+                                ratStats.incrementTxDuration(freq, level, proportionalTxDurationMs);
+                                frequencyDurationMs += durationMs;
+                            }
+                            final long totalRxDuration = deltaInfo.getReceiveTimeMillis();
+                            // Smear HAL provided Rx power duration based on active modem
+                            // duration in a given state.  (Add totalActiveTimeMs / 2 before the
+                            // integer division with totalActiveTimeMs for rounding.)
+                            final long proportionalRxDurationMs =
+                                    (frequencyDurationMs * totalRxDuration + (totalActiveTimeMs
+                                            / 2)) / totalActiveTimeMs;
+                            ratStats.incrementRxDuration(freq, proportionalRxDurationMs);
+                        }
+
+                        ratStats.setMark(elapsedRealtimeMs);
+                    }
+                }
             }
             long totalAppRadioTimeUs = mMobileRadioActivePerAppTimer.getTimeSinceMarkLocked(
                     elapsedRealtimeMs * 1000);
@@ -15970,6 +16257,9 @@
     public Uid getUidStatsLocked(int uid, long elapsedRealtimeMs, long uptimeMs) {
         Uid u = mUidStats.get(uid);
         if (u == null) {
+            if (Process.isSdkSandboxUid(uid)) {
+                Log.wtf(TAG, "Tracking an SDK Sandbox UID");
+            }
             u = new Uid(this, uid, elapsedRealtimeMs, uptimeMs);
             mUidStats.put(uid, u);
         }
@@ -16879,14 +17169,18 @@
         mNextMaxDailyDeadlineMs = in.readLong();
         mBatteryTimeToFullSeconds = in.readLong();
 
-        mMeasuredEnergyStatsConfig = MeasuredEnergyStats.Config.createFromParcel(in);
-
-        /**
-         * WARNING: Supported buckets may have changed across boots. Bucket mismatch is handled
-         *          later when {@link #initMeasuredEnergyStatsLocked} is called.
-         */
-        mGlobalMeasuredEnergyStats = MeasuredEnergyStats.createAndReadSummaryFromParcel(
-                mMeasuredEnergyStatsConfig, in);
+        final MeasuredEnergyStats.Config config = MeasuredEnergyStats.Config.createFromParcel(in);
+        final MeasuredEnergyStats measuredEnergyStats =
+                MeasuredEnergyStats.createAndReadSummaryFromParcel(mMeasuredEnergyStatsConfig, in);
+        if (config != null && Arrays.equals(config.getStateNames(),
+                getBatteryConsumerProcessStateNames())) {
+            /**
+             * WARNING: Supported buckets may have changed across boots. Bucket mismatch is handled
+             *          later when {@link #initMeasuredEnergyStatsLocked} is called.
+             */
+            mMeasuredEnergyStatsConfig = config;
+            mGlobalMeasuredEnergyStats = measuredEnergyStats;
+        }
 
         mStartCount++;
 
@@ -16918,6 +17212,13 @@
             mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
             mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
         }
+
+        final int numRat = in.readInt();
+        for (int i = 0; i < numRat; i++) {
+            if (in.readInt() == 0) continue;
+            getRatBatteryStatsLocked(i).readSummaryFromParcel(in);
+        }
+
         mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
         mMobileRadioActiveTimer.readSummaryFromParcelLocked(in);
         mMobileRadioActivePerAppTimer.readSummaryFromParcelLocked(in);
@@ -16978,7 +17279,6 @@
                 getScreenOffRpmTimerLocked(rpmName).readSummaryFromParcelLocked(in);
             }
         }
-
         int NKW = in.readInt();
         if (NKW > 10000) {
             throw new ParcelFormatException("File corrupt: too many kernel wake locks " + NKW);
@@ -17106,11 +17406,9 @@
                     u.mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
                 }
                 if (in.readBoolean()) {
-                    TimeMultiStateCounter counter = new TimeMultiStateCounter(
-                            mOnBatteryTimeBase, in, elapsedRealtimeMs);
-                    if (counter.getStateCount() == BatteryConsumer.PROCESS_STATE_COUNT) {
-                        u.mMobileRadioActiveTime = counter;
-                    }
+                    u.mMobileRadioActiveTime = TimeMultiStateCounter.readFromParcel(in,
+                            mOnBatteryTimeBase, BatteryConsumer.PROCESS_STATE_COUNT,
+                            elapsedRealtimeMs);
                 }
                 u.mMobileRadioActiveCount.readSummaryFromParcelLocked(in);
             }
@@ -17160,11 +17458,9 @@
 
             int stateCount = in.readInt();
             if (stateCount != 0) {
-                final TimeMultiStateCounter counter = new TimeMultiStateCounter(
-                        mOnBatteryTimeBase, in, mClock.elapsedRealtime());
-                if (stateCount == BatteryConsumer.PROCESS_STATE_COUNT) {
-                    u.mCpuActiveTimeMs = counter;
-                }
+                u.mCpuActiveTimeMs = TimeMultiStateCounter.readFromParcel(in,
+                        mOnBatteryTimeBase, BatteryConsumer.PROCESS_STATE_COUNT,
+                        mClock.elapsedRealtime());
             }
             u.mCpuClusterTimesMs.readSummaryFromParcelLocked(in);
 
@@ -17423,6 +17719,17 @@
             mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
             mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
         }
+        final int numRat = mPerRatBatteryStats.length;
+        out.writeInt(numRat);
+        for (int i = 0; i < numRat; i++) {
+            final RadioAccessTechnologyBatteryStats ratStat = mPerRatBatteryStats[i];
+            if (ratStat == null) {
+                out.writeInt(0);
+                continue;
+            }
+            out.writeInt(1);
+            ratStat.writeSummaryToParcel(out, nowRealtime);
+        }
         mMobileRadioActiveTimer.writeSummaryFromParcelLocked(out, nowRealtime);
         mMobileRadioActivePerAppTimer.writeSummaryFromParcelLocked(out, nowRealtime);
         mMobileRadioActiveAdjustedTime.writeSummaryFromParcelLocked(out);
@@ -17999,9 +18306,15 @@
         mLastWriteTimeMs = in.readLong();
         mBatteryTimeToFullSeconds = in.readLong();
 
-        mMeasuredEnergyStatsConfig = MeasuredEnergyStats.Config.createFromParcel(in);
-        mGlobalMeasuredEnergyStats =
+
+        final MeasuredEnergyStats.Config config = MeasuredEnergyStats.Config.createFromParcel(in);
+        final MeasuredEnergyStats measuredEnergyStats =
                 MeasuredEnergyStats.createFromParcel(mMeasuredEnergyStatsConfig, in);
+        if (config != null && Arrays.equals(config.getStateNames(),
+                getBatteryConsumerProcessStateNames())) {
+            mMeasuredEnergyStatsConfig = config;
+            mGlobalMeasuredEnergyStats = measuredEnergyStats;
+        }
 
         mRpmStats.clear();
         int NRPMS = in.readInt();
diff --git a/core/java/com/android/internal/os/BatteryUsageStatsProvider.java b/core/java/com/android/internal/os/BatteryUsageStatsProvider.java
index a1c1917..81c6ee7 100644
--- a/core/java/com/android/internal/os/BatteryUsageStatsProvider.java
+++ b/core/java/com/android/internal/os/BatteryUsageStatsProvider.java
@@ -22,6 +22,7 @@
 import android.os.BatteryUsageStats;
 import android.os.BatteryUsageStatsQuery;
 import android.os.Parcel;
+import android.os.Process;
 import android.os.SystemClock;
 import android.os.UidBatteryConsumer;
 import android.util.Log;
@@ -162,6 +163,8 @@
         final boolean includeProcessStateData = ((query.getFlags()
                 & BatteryUsageStatsQuery.FLAG_BATTERY_USAGE_STATS_INCLUDE_PROCESS_STATE_DATA) != 0)
                 && mStats.isProcessStateDataAvailable();
+        final boolean includeVirtualUids =  ((query.getFlags()
+                & BatteryUsageStatsQuery.FLAG_BATTERY_USAGE_STATS_INCLUDE_VIRTUAL_UIDS) != 0);
 
         final BatteryUsageStats.Builder batteryUsageStatsBuilder = new BatteryUsageStats.Builder(
                 mStats.getCustomEnergyConsumerNames(), includePowerModels,
@@ -174,6 +177,10 @@
         SparseArray<? extends BatteryStats.Uid> uidStats = mStats.getUidStats();
         for (int i = uidStats.size() - 1; i >= 0; i--) {
             final BatteryStats.Uid uid = uidStats.valueAt(i);
+            if (!includeVirtualUids && uid.getUid() == Process.SDK_SANDBOX_VIRTUAL_UID) {
+                continue;
+            }
+
             batteryUsageStatsBuilder.getOrCreateUidBatteryConsumerBuilder(uid)
                     .setTimeInStateMs(UidBatteryConsumer.STATE_BACKGROUND,
                             getProcessBackgroundTimeMs(uid, realtimeUs))
diff --git a/core/java/com/android/internal/os/BluetoothPowerCalculator.java b/core/java/com/android/internal/os/BluetoothPowerCalculator.java
index 2ebf689..e52c8a3 100644
--- a/core/java/com/android/internal/os/BluetoothPowerCalculator.java
+++ b/core/java/com/android/internal/os/BluetoothPowerCalculator.java
@@ -139,8 +139,10 @@
                         BatteryConsumer.POWER_COMPONENT_BLUETOOTH, powerAndDuration.powerMah,
                         powerModel);
 
-        powerAndDuration.totalDurationMs += powerAndDuration.durationMs;
-        powerAndDuration.totalPowerMah += powerAndDuration.powerMah;
+        if (!app.isVirtualUid()) {
+            powerAndDuration.totalDurationMs += powerAndDuration.durationMs;
+            powerAndDuration.totalPowerMah += powerAndDuration.powerMah;
+        }
 
         if (query.isProcessStateDataNeeded() && powerAndDuration.keys != null) {
             for (int j = 0; j < powerAndDuration.keys.length; j++) {
diff --git a/core/java/com/android/internal/os/CpuPowerCalculator.java b/core/java/com/android/internal/os/CpuPowerCalculator.java
index 1fc2baf..8704e93 100644
--- a/core/java/com/android/internal/os/CpuPowerCalculator.java
+++ b/core/java/com/android/internal/os/CpuPowerCalculator.java
@@ -117,7 +117,9 @@
                 }
             }
             calculateApp(app, app.getBatteryStatsUid(), query, result, keys);
-            totalPowerMah += result.powerMah;
+            if (!app.isVirtualUid()) {
+                totalPowerMah += result.powerMah;
+            }
         }
 
         final long consumptionUC = batteryStats.getCpuMeasuredBatteryConsumptionUC();
diff --git a/core/java/com/android/internal/os/CustomMeasuredPowerCalculator.java b/core/java/com/android/internal/os/CustomMeasuredPowerCalculator.java
index cbbb526..0853bd8 100644
--- a/core/java/com/android/internal/os/CustomMeasuredPowerCalculator.java
+++ b/core/java/com/android/internal/os/CustomMeasuredPowerCalculator.java
@@ -96,7 +96,9 @@
                 app.setConsumedPowerForCustomComponent(
                         BatteryConsumer.FIRST_CUSTOM_POWER_COMPONENT_ID + i,
                         customMeasuredPowerMah[i]);
-                newTotalPowerMah[i] += customMeasuredPowerMah[i];
+                if (!app.isVirtualUid()) {
+                    newTotalPowerMah[i] += customMeasuredPowerMah[i];
+                }
             }
         }
         return newTotalPowerMah;
diff --git a/core/java/com/android/internal/os/GnssPowerCalculator.java b/core/java/com/android/internal/os/GnssPowerCalculator.java
index 0f78306..070783a 100644
--- a/core/java/com/android/internal/os/GnssPowerCalculator.java
+++ b/core/java/com/android/internal/os/GnssPowerCalculator.java
@@ -58,8 +58,11 @@
             final long consumptionUC =
                     app.getBatteryStatsUid().getGnssMeasuredBatteryConsumptionUC();
             final int powerModel = getPowerModel(consumptionUC, query);
-            appsPowerMah += calculateApp(app, app.getBatteryStatsUid(), powerModel,
+            final double powerMah = calculateApp(app, app.getBatteryStatsUid(), powerModel,
                     rawRealtimeUs, averageGnssPowerMa, consumptionUC);
+            if (!app.isVirtualUid()) {
+                appsPowerMah += powerMah;
+            }
         }
 
         final long consumptionUC = batteryStats.getGnssMeasuredBatteryConsumptionUC();
diff --git a/core/java/com/android/internal/os/MobileRadioPowerCalculator.java b/core/java/com/android/internal/os/MobileRadioPowerCalculator.java
index f4624de..d0df45c 100644
--- a/core/java/com/android/internal/os/MobileRadioPowerCalculator.java
+++ b/core/java/com/android/internal/os/MobileRadioPowerCalculator.java
@@ -136,12 +136,14 @@
             PowerAndDuration total,
             BatteryUsageStatsQuery query, BatteryConsumer.Key[] keys) {
         final long radioActiveDurationMs = calculateDuration(u, BatteryStats.STATS_SINCE_CHARGED);
-        total.totalAppDurationMs += radioActiveDurationMs;
-
         final long consumptionUC = u.getMobileRadioMeasuredBatteryConsumptionUC();
         final int powerModel = getPowerModel(consumptionUC, query);
         final double powerMah = calculatePower(u, powerModel, radioActiveDurationMs, consumptionUC);
-        total.totalAppPowerMah += powerMah;
+
+        if (!app.isVirtualUid()) {
+            total.totalAppDurationMs += radioActiveDurationMs;
+            total.totalAppPowerMah += powerMah;
+        }
 
         app.setUsageDurationMillis(BatteryConsumer.POWER_COMPONENT_MOBILE_RADIO,
                         radioActiveDurationMs)
diff --git a/core/java/com/android/internal/os/ScreenPowerCalculator.java b/core/java/com/android/internal/os/ScreenPowerCalculator.java
index 67d3d6e..5ca1a85 100644
--- a/core/java/com/android/internal/os/ScreenPowerCalculator.java
+++ b/core/java/com/android/internal/os/ScreenPowerCalculator.java
@@ -96,8 +96,10 @@
                                     appPowerAndDuration.durationMs)
                             .setConsumedPower(BatteryConsumer.POWER_COMPONENT_SCREEN,
                                     appPowerAndDuration.powerMah, powerModel);
-                    totalAppPower += appPowerAndDuration.powerMah;
-                    totalAppDuration += appPowerAndDuration.durationMs;
+                    if (!app.isVirtualUid()) {
+                        totalAppPower += appPowerAndDuration.powerMah;
+                        totalAppDuration += appPowerAndDuration.durationMs;
+                    }
                 }
                 break;
             case BatteryConsumer.POWER_MODEL_POWER_PROFILE:
@@ -192,10 +194,13 @@
         long totalActivityTimeMs = 0;
         final SparseLongArray activityTimeArray = new SparseLongArray();
         for (int i = uidBatteryConsumerBuilders.size() - 1; i >= 0; i--) {
-            final BatteryStats.Uid uid = uidBatteryConsumerBuilders.valueAt(i).getBatteryStatsUid();
+            final UidBatteryConsumer.Builder app = uidBatteryConsumerBuilders.valueAt(i);
+            final BatteryStats.Uid uid = app.getBatteryStatsUid();
             final long timeMs = getProcessForegroundTimeMs(uid, rawRealtimeUs);
             activityTimeArray.put(uid.getUid(), timeMs);
-            totalActivityTimeMs += timeMs;
+            if (!app.isVirtualUid()) {
+                totalActivityTimeMs += timeMs;
+            }
         }
 
         if (totalActivityTimeMs >= MIN_ACTIVE_TIME_FOR_SMEARING) {
diff --git a/core/java/com/android/internal/os/SensorPowerCalculator.java b/core/java/com/android/internal/os/SensorPowerCalculator.java
index 4a9c91d..573692e 100644
--- a/core/java/com/android/internal/os/SensorPowerCalculator.java
+++ b/core/java/com/android/internal/os/SensorPowerCalculator.java
@@ -51,7 +51,9 @@
                 builder.getUidBatteryConsumerBuilders();
         for (int i = uidBatteryConsumerBuilders.size() - 1; i >= 0; i--) {
             final UidBatteryConsumer.Builder app = uidBatteryConsumerBuilders.valueAt(i);
-            appsPowerMah += calculateApp(app, app.getBatteryStatsUid(), rawRealtimeUs);
+            if (!app.isVirtualUid()) {
+                appsPowerMah += calculateApp(app, app.getBatteryStatsUid(), rawRealtimeUs);
+            }
         }
 
         builder.getAggregateBatteryConsumerBuilder(
diff --git a/core/java/com/android/internal/os/UserPowerCalculator.java b/core/java/com/android/internal/os/UserPowerCalculator.java
index 22cff6e..79e3a19 100644
--- a/core/java/com/android/internal/os/UserPowerCalculator.java
+++ b/core/java/com/android/internal/os/UserPowerCalculator.java
@@ -49,7 +49,11 @@
                 builder.getUidBatteryConsumerBuilders();
 
         for (int i = uidBatteryConsumerBuilders.size() - 1; i >= 0; i--) {
-            UidBatteryConsumer.Builder uidBuilder = uidBatteryConsumerBuilders.valueAt(i);
+            final UidBatteryConsumer.Builder uidBuilder = uidBatteryConsumerBuilders.valueAt(i);
+            if (uidBuilder.isVirtualUid()) {
+                continue;
+            }
+
             final int uid = uidBuilder.getUid();
             if (UserHandle.getAppId(uid) < Process.FIRST_APPLICATION_UID) {
                 continue;
diff --git a/core/java/com/android/internal/os/VideoPowerCalculator.java b/core/java/com/android/internal/os/VideoPowerCalculator.java
index a222bcb..2daf15e 100644
--- a/core/java/com/android/internal/os/VideoPowerCalculator.java
+++ b/core/java/com/android/internal/os/VideoPowerCalculator.java
@@ -75,7 +75,9 @@
         final double powerMah = mPowerEstimator.calculatePower(durationMs);
         app.setUsageDurationMillis(BatteryConsumer.POWER_COMPONENT_VIDEO, durationMs)
                 .setConsumedPower(BatteryConsumer.POWER_COMPONENT_VIDEO, powerMah);
-        total.durationMs += durationMs;
-        total.powerMah += powerMah;
+        if (!app.isVirtualUid()) {
+            total.durationMs += durationMs;
+            total.powerMah += powerMah;
+        }
     }
 }
diff --git a/core/java/com/android/internal/os/WakelockPowerCalculator.java b/core/java/com/android/internal/os/WakelockPowerCalculator.java
index 0251e1c..3ae7113 100644
--- a/core/java/com/android/internal/os/WakelockPowerCalculator.java
+++ b/core/java/com/android/internal/os/WakelockPowerCalculator.java
@@ -62,8 +62,10 @@
                     BatteryStats.STATS_SINCE_CHARGED);
             app.setUsageDurationMillis(BatteryConsumer.POWER_COMPONENT_WAKELOCK, result.durationMs)
                     .setConsumedPower(BatteryConsumer.POWER_COMPONENT_WAKELOCK, result.powerMah);
-            totalAppDurationMs += result.durationMs;
-            appPowerMah += result.powerMah;
+            if (!app.isVirtualUid()) {
+                totalAppDurationMs += result.durationMs;
+                appPowerMah += result.powerMah;
+            }
 
             if (app.getUid() == Process.ROOT_UID) {
                 osBatteryConsumer = app;
diff --git a/core/java/com/android/internal/os/WifiPowerCalculator.java b/core/java/com/android/internal/os/WifiPowerCalculator.java
index 8c3fb86..2181821 100644
--- a/core/java/com/android/internal/os/WifiPowerCalculator.java
+++ b/core/java/com/android/internal/os/WifiPowerCalculator.java
@@ -111,9 +111,10 @@
             calculateApp(powerDurationAndTraffic, app.getBatteryStatsUid(), powerModel,
                     rawRealtimeUs, BatteryStats.STATS_SINCE_CHARGED,
                     batteryStats.hasWifiActivityReporting(), consumptionUC);
-
-            totalAppDurationMs += powerDurationAndTraffic.durationMs;
-            totalAppPowerMah += powerDurationAndTraffic.powerMah;
+            if (!app.isVirtualUid()) {
+                totalAppDurationMs += powerDurationAndTraffic.durationMs;
+                totalAppPowerMah += powerDurationAndTraffic.powerMah;
+            }
 
             app.setUsageDurationMillis(BatteryConsumer.POWER_COMPONENT_WIFI,
                     powerDurationAndTraffic.durationMs);
diff --git a/core/java/com/android/internal/policy/DecorView.java b/core/java/com/android/internal/policy/DecorView.java
index 40e4085..89ac722 100644
--- a/core/java/com/android/internal/policy/DecorView.java
+++ b/core/java/com/android/internal/policy/DecorView.java
@@ -30,6 +30,7 @@
 import static android.view.View.MeasureSpec.getMode;
 import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
 import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
+import static android.view.ViewRootImpl.CAPTION_ON_SHELL;
 import static android.view.Window.DECOR_CAPTION_SHADE_DARK;
 import static android.view.Window.DECOR_CAPTION_SHADE_LIGHT;
 import static android.view.WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS;
@@ -2120,7 +2121,9 @@
      * corresponding insets change to the InsetsController.
      */
     public void notifyCaptionHeightChanged() {
-        getWindowInsetsController().setCaptionInsetsHeight(getCaptionInsetsHeight());
+        if (!CAPTION_ON_SHELL) {
+            getWindowInsetsController().setCaptionInsetsHeight(getCaptionInsetsHeight());
+        }
     }
 
     void setWindow(PhoneWindow phoneWindow) {
diff --git a/core/java/com/android/internal/power/MeasuredEnergyStats.java b/core/java/com/android/internal/power/MeasuredEnergyStats.java
index 7262e84..7fb8696 100644
--- a/core/java/com/android/internal/power/MeasuredEnergyStats.java
+++ b/core/java/com/android/internal/power/MeasuredEnergyStats.java
@@ -194,6 +194,7 @@
             return mSupportedMultiStateBuckets[index];
         }
 
+        @NonNull
         public String[] getStateNames() {
             return mStateNames;
         }
@@ -321,6 +322,10 @@
             LongMultiStateCounter multiStateCounter = null;
             if (in.readBoolean()) {
                 multiStateCounter = LongMultiStateCounter.CREATOR.createFromParcel(in);
+                if (mConfig == null
+                        || multiStateCounter.getStateCount() != mConfig.getStateNames().length) {
+                    multiStateCounter = null;
+                }
             }
 
             if (index < mAccumulatedChargeMicroCoulomb.length) {
diff --git a/core/java/com/android/internal/statusbar/IStatusBar.aidl b/core/java/com/android/internal/statusbar/IStatusBar.aidl
index d629d66..089179d 100644
--- a/core/java/com/android/internal/statusbar/IStatusBar.aidl
+++ b/core/java/com/android/internal/statusbar/IStatusBar.aidl
@@ -297,6 +297,11 @@
      */
     void runGcForTest();
 
+    /**
+     * Send a request to SystemUI to put a given active tile in listening state
+     */
+    void requestTileServiceListeningState(in ComponentName componentName);
+
     void requestAddTile(in ComponentName componentName, in CharSequence appName, in CharSequence label, in Icon icon, in IAddTileResultCallback callback);
     void cancelRequestAddTile(in String packageName);
 
diff --git a/core/java/com/android/internal/statusbar/IStatusBarService.aidl b/core/java/com/android/internal/statusbar/IStatusBarService.aidl
index 1d60c50..2ee5e79 100644
--- a/core/java/com/android/internal/statusbar/IStatusBarService.aidl
+++ b/core/java/com/android/internal/statusbar/IStatusBarService.aidl
@@ -91,7 +91,7 @@
     void onBubbleNotificationSuppressionChanged(String key, boolean isNotifSuppressed, boolean isBubbleSuppressed);
     void hideCurrentInputMethodForBubbles();
     void grantInlineReplyUriPermission(String key, in Uri uri, in UserHandle user, String packageName);
-    void clearInlineReplyUriPermissions(String key);
+    oneway void clearInlineReplyUriPermissions(String key);
     void onNotificationFeedbackReceived(String key, in Bundle feedback);
 
     void onGlobalActionsShown();
@@ -171,6 +171,11 @@
      */
     void suppressAmbientDisplay(boolean suppress);
 
+    /**
+     * Send a request to SystemUI to put a given active tile in listening state
+     */
+    void requestTileServiceListeningState(in ComponentName componentName, int userId);
+
     void requestAddTile(in ComponentName componentName, in CharSequence label, in Icon icon, int userId, in IAddTileResultCallback callback);
     void cancelRequestAddTile(in String packageName);
 
diff --git a/core/java/com/android/internal/telephony/ICarrierPrivilegesListener.aidl b/core/java/com/android/internal/telephony/ICarrierPrivilegesCallback.aidl
similarity index 84%
rename from core/java/com/android/internal/telephony/ICarrierPrivilegesListener.aidl
rename to core/java/com/android/internal/telephony/ICarrierPrivilegesCallback.aidl
index 6ca8cec..0c8e73f 100644
--- a/core/java/com/android/internal/telephony/ICarrierPrivilegesListener.aidl
+++ b/core/java/com/android/internal/telephony/ICarrierPrivilegesCallback.aidl
@@ -16,7 +16,8 @@
 
 package com.android.internal.telephony;
 
-oneway interface ICarrierPrivilegesListener {
+oneway interface ICarrierPrivilegesCallback {
     void onCarrierPrivilegesChanged(
             in List<String> privilegedPackageNames, in int[] privilegedUids);
+    void onCarrierServiceChanged(in String carrierServicePackageName, in int carrierServiceUid);
 }
diff --git a/core/java/com/android/internal/telephony/ITelephonyRegistry.aidl b/core/java/com/android/internal/telephony/ITelephonyRegistry.aidl
index 9712d7e..c7fa757 100644
--- a/core/java/com/android/internal/telephony/ITelephonyRegistry.aidl
+++ b/core/java/com/android/internal/telephony/ITelephonyRegistry.aidl
@@ -32,7 +32,7 @@
 import android.telephony.ServiceState;
 import android.telephony.SignalStrength;
 import android.telephony.emergency.EmergencyNumber;
-import com.android.internal.telephony.ICarrierPrivilegesListener;
+import com.android.internal.telephony.ICarrierPrivilegesCallback;
 import com.android.internal.telephony.IPhoneStateListener;
 import com.android.internal.telephony.IOnSubscriptionsChangedListener;
 
@@ -102,9 +102,11 @@
     void notifyLinkCapacityEstimateChanged(in int phoneId, in int subId,
             in List<LinkCapacityEstimate> linkCapacityEstimateList);
 
-    void addCarrierPrivilegesListener(
-            int phoneId, ICarrierPrivilegesListener callback, String pkg, String featureId);
-    void removeCarrierPrivilegesListener(ICarrierPrivilegesListener callback, String pkg);
+    void addCarrierPrivilegesCallback(
+            int phoneId, ICarrierPrivilegesCallback callback, String pkg, String featureId);
+    void removeCarrierPrivilegesCallback(ICarrierPrivilegesCallback callback, String pkg);
     void notifyCarrierPrivilegesChanged(
             int phoneId, in List<String> privilegedPackageNames, in int[] privilegedUids);
+    void notifyCarrierServiceChanged(int phoneId, in String packageName, int uid);
+
 }
diff --git a/core/java/com/android/internal/view/BaseIWindow.java b/core/java/com/android/internal/view/BaseIWindow.java
index 6673f67..842d72a 100644
--- a/core/java/com/android/internal/view/BaseIWindow.java
+++ b/core/java/com/android/internal/view/BaseIWindow.java
@@ -51,10 +51,10 @@
     @Override
     public void resized(ClientWindowFrames frames, boolean reportDraw,
             MergedConfiguration mergedConfiguration, boolean forceLayout,
-            boolean alwaysConsumeSystemBars, int displayId) {
+            boolean alwaysConsumeSystemBars, int displayId, int seqId) {
         if (reportDraw) {
             try {
-                mSession.finishDrawing(this, null /* postDrawTransaction */);
+                mSession.finishDrawing(this, null /* postDrawTransaction */, seqId);
             } catch (RemoteException e) {
             }
         }
diff --git a/core/java/com/android/internal/widget/CachingIconView.java b/core/java/com/android/internal/widget/CachingIconView.java
index 299cbe1..bd27e60 100644
--- a/core/java/com/android/internal/widget/CachingIconView.java
+++ b/core/java/com/android/internal/widget/CachingIconView.java
@@ -23,6 +23,7 @@
 import android.compat.annotation.UnsupportedAppUsage;
 import android.content.Context;
 import android.content.res.Configuration;
+import android.content.res.TypedArray;
 import android.graphics.Bitmap;
 import android.graphics.PorterDuff;
 import android.graphics.drawable.Drawable;
@@ -35,6 +36,9 @@
 import android.widget.ImageView;
 import android.widget.RemoteViews;
 
+import com.android.internal.R;
+
+import java.io.IOException;
 import java.util.Objects;
 import java.util.function.Consumer;
 
@@ -55,9 +59,42 @@
     private int mBackgroundColor;
     private boolean mWillBeForceHidden;
 
+    private int mMaxDrawableWidth = -1;
+    private int mMaxDrawableHeight = -1;
+
+    public CachingIconView(Context context) {
+        this(context, null, 0, 0);
+    }
+
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     public CachingIconView(Context context, @Nullable AttributeSet attrs) {
-        super(context, attrs);
+        this(context, attrs, 0, 0);
+    }
+
+    public CachingIconView(Context context, @Nullable AttributeSet attrs,
+            int defStyleAttr) {
+        this(context, attrs, defStyleAttr, 0);
+    }
+
+    public CachingIconView(Context context, @Nullable AttributeSet attrs,
+            int defStyleAttr, int defStyleRes) {
+        super(context, attrs, defStyleAttr, defStyleRes);
+        init(context, attrs, defStyleAttr, defStyleRes);
+    }
+
+    private void init(Context context, @Nullable AttributeSet attrs, int defStyleAttr,
+            int defStyleRes) {
+        if (attrs == null) {
+            return;
+        }
+
+        TypedArray ta = context.obtainStyledAttributes(attrs,
+                R.styleable.CachingIconView, defStyleAttr, defStyleRes);
+        mMaxDrawableWidth = ta.getDimensionPixelSize(R.styleable
+                .CachingIconView_maxDrawableWidth, -1);
+        mMaxDrawableHeight = ta.getDimensionPixelSize(R.styleable
+                .CachingIconView_maxDrawableHeight, -1);
+        ta.recycle();
     }
 
     @Override
@@ -66,15 +103,31 @@
         if (!testAndSetCache(icon)) {
             mInternalSetDrawable = true;
             // This calls back to setImageDrawable, make sure we don't clear the cache there.
-            super.setImageIcon(icon);
+            Drawable drawable = loadSizeRestrictedIcon(icon);
+            if (drawable == null) {
+                super.setImageIcon(icon);
+            } else {
+                super.setImageDrawable(drawable);
+            }
             mInternalSetDrawable = false;
         }
     }
 
+    @Nullable
+    private Drawable loadSizeRestrictedIcon(@Nullable Icon icon) {
+        try {
+            return LocalImageResolver.resolveImage(icon, getContext(), mMaxDrawableWidth,
+                    mMaxDrawableHeight);
+        } catch (IOException e) {
+            return null;
+        }
+    }
+
     @Override
-    public Runnable setImageIconAsync(@Nullable Icon icon) {
+    public Runnable setImageIconAsync(@Nullable final Icon icon) {
         resetCache();
-        return super.setImageIconAsync(icon);
+        Drawable drawable = loadSizeRestrictedIcon(icon);
+        return () -> setImageDrawable(drawable);
     }
 
     @Override
@@ -83,14 +136,34 @@
         if (!testAndSetCache(resId)) {
             mInternalSetDrawable = true;
             // This calls back to setImageDrawable, make sure we don't clear the cache there.
-            super.setImageResource(resId);
+            Drawable drawable = loadSizeRestrictedDrawable(resId);
+            if (drawable == null) {
+                super.setImageResource(resId);
+            } else {
+                super.setImageDrawable(drawable);
+            }
             mInternalSetDrawable = false;
         }
     }
 
+    @Nullable
+    private Drawable loadSizeRestrictedDrawable(@DrawableRes int resId) {
+        try {
+            return LocalImageResolver.resolveImage(resId, getContext(), mMaxDrawableWidth,
+                    mMaxDrawableHeight);
+        } catch (IOException e) {
+            return null;
+        }
+    }
+
     @Override
     public Runnable setImageResourceAsync(@DrawableRes int resId) {
         resetCache();
+        Drawable drawable = loadSizeRestrictedDrawable(resId);
+        if (drawable != null) {
+            return () -> setImageDrawable(drawable);
+        }
+
         return super.setImageResourceAsync(resId);
     }
 
@@ -98,13 +171,35 @@
     @RemotableViewMethod(asyncImpl="setImageURIAsync")
     public void setImageURI(@Nullable Uri uri) {
         resetCache();
-        super.setImageURI(uri);
+        Drawable drawable = loadSizeRestrictedUri(uri);
+        if (drawable == null) {
+            super.setImageURI(uri);
+        } else {
+            mInternalSetDrawable = true;
+            super.setImageDrawable(drawable);
+            mInternalSetDrawable = false;
+        }
+    }
+
+    @Nullable
+    private Drawable loadSizeRestrictedUri(@Nullable Uri uri) {
+        try {
+            return LocalImageResolver.resolveImage(uri, getContext(), mMaxDrawableWidth,
+                    mMaxDrawableHeight);
+        } catch (IOException e) {
+            return null;
+        }
     }
 
     @Override
     public Runnable setImageURIAsync(@Nullable Uri uri) {
         resetCache();
-        return super.setImageURIAsync(uri);
+        Drawable drawable = loadSizeRestrictedUri(uri);
+        if (drawable == null) {
+            return super.setImageURIAsync(uri);
+        } else {
+            return () -> setImageDrawable(drawable);
+        }
     }
 
     @Override
@@ -307,4 +402,18 @@
     public void setWillBeForceHidden(boolean forceHidden) {
         mWillBeForceHidden = forceHidden;
     }
+
+    /**
+     * Returns the set maximum width of drawable in pixels. -1 if not set.
+     */
+    public int getMaxDrawableWidth() {
+        return mMaxDrawableWidth;
+    }
+
+    /**
+     * Returns the set maximum height of drawable in pixels. -1 if not set.
+     */
+    public int getMaxDrawableHeight() {
+        return mMaxDrawableHeight;
+    }
 }
diff --git a/core/java/com/android/internal/widget/ConversationLayout.java b/core/java/com/android/internal/widget/ConversationLayout.java
index 5fa4a65..4706aff 100644
--- a/core/java/com/android/internal/widget/ConversationLayout.java
+++ b/core/java/com/android/internal/widget/ConversationLayout.java
@@ -66,7 +66,6 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
-import java.util.function.Consumer;
 
 /**
  * A custom-built layout for the Notification.MessagingStyle allows dynamic addition and removal
@@ -76,8 +75,6 @@
 public class ConversationLayout extends FrameLayout
         implements ImageMessageConsumer, IMessagingLayout {
 
-    private static final Consumer<MessagingMessage> REMOVE_MESSAGE
-            = MessagingMessage::removeMessage;
     public static final Interpolator LINEAR_OUT_SLOW_IN = new PathInterpolator(0f, 0f, 0.2f, 1f);
     public static final Interpolator FAST_OUT_LINEAR_IN = new PathInterpolator(0.4f, 0f, 1f, 1f);
     public static final Interpolator FAST_OUT_SLOW_IN = new PathInterpolator(0.4f, 0f, 0.2f, 1f);
@@ -150,7 +147,7 @@
     private Icon mShortcutIcon;
     private View mAppNameDivider;
     private TouchDelegateComposite mTouchDelegate = new TouchDelegateComposite(this);
-    private ArrayList<MessagingGroup> mToRecycle = new ArrayList<>();
+    private ArrayList<MessagingLinearLayout.MessagingChild> mToRecycle = new ArrayList<>();
 
     public ConversationLayout(@NonNull Context context) {
         super(context);
@@ -463,8 +460,12 @@
         removeGroups(oldGroups);
 
         // Let's remove the remaining messages
-        mMessages.forEach(REMOVE_MESSAGE);
-        mHistoricMessages.forEach(REMOVE_MESSAGE);
+        for (MessagingMessage message : mMessages) {
+            message.removeMessage(mToRecycle);
+        }
+        for (MessagingMessage historicMessage : mHistoricMessages) {
+            historicMessage.removeMessage(mToRecycle);
+        }
 
         mMessages = messages;
         mHistoricMessages = historicMessages;
@@ -475,8 +476,8 @@
         updateConversationLayout();
 
         // Recycle everything at the end of the update, now that we know it's no longer needed.
-        for (MessagingGroup group : mToRecycle) {
-            group.recycle();
+        for (MessagingLinearLayout.MessagingChild child : mToRecycle) {
+            child.recycle();
         }
         mToRecycle.clear();
     }
diff --git a/core/java/com/android/internal/widget/ILockSettings.aidl b/core/java/com/android/internal/widget/ILockSettings.aidl
index db4bc2c..3494c9e 100644
--- a/core/java/com/android/internal/widget/ILockSettings.aidl
+++ b/core/java/com/android/internal/widget/ILockSettings.aidl
@@ -53,7 +53,6 @@
     VerifyCredentialResponse verifyTiedProfileChallenge(in LockscreenCredential credential, int userId, int flags);
     VerifyCredentialResponse verifyGatekeeperPasswordHandle(long gatekeeperPasswordHandle, long challenge, int userId);
     void removeGatekeeperPasswordHandle(long gatekeeperPasswordHandle);
-    boolean checkVoldPassword(int userId);
     int getCredentialType(int userId);
     byte[] getHashFactor(in LockscreenCredential currentCredential, int userId);
     void setSeparateProfileChallengeEnabled(int userId, boolean enabled, in LockscreenCredential managedUserPassword);
@@ -97,7 +96,6 @@
     boolean hasSecureLockScreen();
     boolean tryUnlockWithCachedUnifiedChallenge(int userId);
     void removeCachedUnifiedChallenge(int userId);
-    void updateEncryptionPassword(int type, in byte[] password);
     boolean registerWeakEscrowTokenRemovedListener(in IWeakEscrowTokenRemovedListener listener);
     boolean unregisterWeakEscrowTokenRemovedListener(in IWeakEscrowTokenRemovedListener listener);
     long addWeakEscrowToken(in byte[] token, int userId, in IWeakEscrowTokenActivatedListener callback);
diff --git a/core/java/com/android/internal/widget/LocalImageResolver.java b/core/java/com/android/internal/widget/LocalImageResolver.java
index 616b699..66a3ff9 100644
--- a/core/java/com/android/internal/widget/LocalImageResolver.java
+++ b/core/java/com/android/internal/widget/LocalImageResolver.java
@@ -16,21 +16,25 @@
 
 package com.android.internal.widget;
 
+import android.annotation.DrawableRes;
 import android.annotation.Nullable;
 import android.content.Context;
+import android.graphics.Bitmap;
 import android.graphics.ImageDecoder;
 import android.graphics.drawable.Drawable;
 import android.graphics.drawable.Icon;
 import android.net.Uri;
 import android.util.Size;
 
+import com.android.internal.annotations.VisibleForTesting;
+
 import java.io.IOException;
 
 /** A class to extract Drawables from a MessagingStyle/ConversationStyle message. */
 public class LocalImageResolver {
-    private static final String TAG = LocalImageResolver.class.getSimpleName();
 
-    private static final int MAX_SAFE_ICON_SIZE_PX = 480;
+    @VisibleForTesting
+    static final int DEFAULT_MAX_SAFE_ICON_SIZE_PX = 480;
 
     /**
      * Resolve an image from the given Uri using {@link ImageDecoder}
@@ -38,9 +42,9 @@
     public static Drawable resolveImage(Uri uri, Context context) throws IOException {
         final ImageDecoder.Source source =
                 ImageDecoder.createSource(context.getContentResolver(), uri);
-        final Drawable drawable =
-                ImageDecoder.decodeDrawable(source, LocalImageResolver::onHeaderDecoded);
-        return drawable;
+        return ImageDecoder.decodeDrawable(source,
+                (decoder, info, s) -> LocalImageResolver.onHeaderDecoded(decoder, info,
+                        DEFAULT_MAX_SAFE_ICON_SIZE_PX, DEFAULT_MAX_SAFE_ICON_SIZE_PX));
     }
 
     /**
@@ -48,17 +52,49 @@
      * using {@link Icon#loadDrawable(Context)} otherwise.  This will correctly apply the Icon's,
      * tint, if present, to the drawable.
      */
-    public static Drawable resolveImage(Icon icon, Context context) throws IOException {
-        Uri uri = getResolvableUri(icon);
-        if (uri != null) {
-            Drawable result = resolveImage(uri, context);
-            if (icon.hasTint()) {
-                result.mutate();
-                result.setTintList(icon.getTintList());
-                result.setTintBlendMode(icon.getTintBlendMode());
-            }
-            return result;
+    public static Drawable resolveImage(@Nullable Icon icon, Context context)
+            throws IOException {
+        return resolveImage(icon, context, DEFAULT_MAX_SAFE_ICON_SIZE_PX,
+                DEFAULT_MAX_SAFE_ICON_SIZE_PX);
+    }
+
+    /**
+     * Get the drawable from Icon using {@link ImageDecoder} if it contains a Uri, or
+     * using {@link Icon#loadDrawable(Context)} otherwise.  This will correctly apply the Icon's,
+     * tint, if present, to the drawable.
+     */
+    @Nullable
+    public static Drawable resolveImage(@Nullable Icon icon, Context context, int maxWidth,
+            int maxHeight)
+            throws IOException {
+        if (icon == null) {
+            return null;
         }
+
+        switch (icon.getType()) {
+            case Icon.TYPE_URI:
+            case Icon.TYPE_URI_ADAPTIVE_BITMAP:
+                Uri uri = getResolvableUri(icon);
+                if (uri != null) {
+                    Drawable result = resolveImage(uri, context, maxWidth, maxHeight);
+                    return tintDrawable(icon, result);
+                }
+                break;
+            case Icon.TYPE_RESOURCE:
+                Drawable result = resolveImage(icon.getResId(), context, maxWidth, maxHeight);
+                if (result != null) {
+                    return tintDrawable(icon, result);
+                }
+                break;
+            case Icon.TYPE_BITMAP:
+            case Icon.TYPE_ADAPTIVE_BITMAP:
+                return resolveBitmapImage(icon, context, maxWidth, maxHeight);
+            case Icon.TYPE_DATA:    // We can't really improve on raw data images.
+            default:
+                break;
+        }
+
+        // Fallback to straight drawable load if we fail with more efficient approach.
         return icon.loadDrawable(context);
     }
 
@@ -66,7 +102,71 @@
             throws IOException {
         final ImageDecoder.Source source =
                 ImageDecoder.createSource(context.getContentResolver(), uri);
+        return resolveImage(source, maxWidth, maxHeight);
+    }
+
+    /**
+     * Attempts to resolve the resource as a bitmap drawable constrained within max sizes.
+     *
+     * @return decoded drawable or null if the passed resource is not a straight bitmap
+     */
+    @Nullable
+    public static Drawable resolveImage(@DrawableRes int resId, Context context, int maxWidth,
+            int maxHeight)
+            throws IOException {
+        final ImageDecoder.Source source = ImageDecoder.createSource(context.getResources(), resId);
+        // It's possible that the resource isn't an actual bitmap drawable so this decode can fail.
+        // Return null in that case.
+        try {
+            return resolveImage(source, maxWidth, maxHeight);
+        } catch (ImageDecoder.DecodeException e) {
+            return null;
+        }
+    }
+
+    @Nullable
+    private static Drawable resolveBitmapImage(Icon icon, Context context, int maxWidth,
+            int maxHeight) {
+        Bitmap bitmap = icon.getBitmap();
+        if (bitmap == null) {
+            return null;
+        }
+
+        if (bitmap.getWidth() > maxWidth || bitmap.getHeight() > maxHeight) {
+            Icon smallerIcon = icon.getType() == Icon.TYPE_ADAPTIVE_BITMAP
+                    ? Icon.createWithAdaptiveBitmap(bitmap) : Icon.createWithBitmap(bitmap);
+            // We don't want to modify the source icon, create a copy.
+            smallerIcon.setTintList(icon.getTintList())
+                    .setTintBlendMode(icon.getTintBlendMode())
+                    .scaleDownIfNecessary(maxWidth, maxHeight);
+            return smallerIcon.loadDrawable(context);
+        }
+
+        return icon.loadDrawable(context);
+    }
+
+    @Nullable
+    private static Drawable tintDrawable(Icon icon, @Nullable Drawable drawable) {
+        if (drawable == null) {
+            return null;
+        }
+
+        if (icon.hasTint()) {
+            drawable.mutate();
+            drawable.setTintList(icon.getTintList());
+            drawable.setTintBlendMode(icon.getTintBlendMode());
+        }
+
+        return drawable;
+    }
+
+    private static Drawable resolveImage(ImageDecoder.Source source, int maxWidth, int maxHeight)
+            throws IOException {
         return ImageDecoder.decodeDrawable(source, (decoder, info, unused) -> {
+            if (maxWidth <= 0 || maxHeight <= 0) {
+                return;
+            }
+
             final Size size = info.getSize();
             if (size.getWidth() > size.getHeight()) {
                 if (size.getWidth() > maxWidth) {
@@ -88,11 +188,12 @@
     }
 
     private static void onHeaderDecoded(ImageDecoder decoder, ImageDecoder.ImageInfo info,
-            ImageDecoder.Source source) {
+            int maxWidth, int maxHeight) {
         final Size size = info.getSize();
         final int originalSize = Math.max(size.getHeight(), size.getWidth());
-        final double ratio = (originalSize > MAX_SAFE_ICON_SIZE_PX)
-                ? originalSize * 1f / MAX_SAFE_ICON_SIZE_PX
+        final int maxSize = Math.max(maxWidth, maxHeight);
+        final double ratio = (originalSize > maxSize)
+                ? originalSize * 1f / maxSize
                 : 1.0;
         decoder.setTargetSampleSize(getPowerOfTwoForSampleRatio(ratio));
     }
diff --git a/core/java/com/android/internal/widget/LockPatternUtils.java b/core/java/com/android/internal/widget/LockPatternUtils.java
index 1e11c6d..a94b307 100644
--- a/core/java/com/android/internal/widget/LockPatternUtils.java
+++ b/core/java/com/android/internal/widget/LockPatternUtils.java
@@ -526,20 +526,6 @@
     }
 
     /**
-     * Check to see if vold already has the password.
-     * Note that this also clears vold's copy of the password.
-     * @return Whether the vold password matches or not.
-     */
-    public boolean checkVoldPassword(int userId) {
-        try {
-            return getLockSettings().checkVoldPassword(userId);
-        } catch (RemoteException re) {
-            Log.e(TAG, "failed to check vold password", re);
-            return false;
-        }
-    }
-
-    /**
      * Returns the password history hash factor, needed to check new password against password
      * history with {@link #checkPasswordHistory(byte[], byte[], int)}
      */
@@ -722,38 +708,14 @@
         return true;
     }
 
-    private void updateCryptoUserInfo(int userId) {
-        if (userId != UserHandle.USER_SYSTEM) {
-            return;
-        }
-
-        final String ownerInfo = isOwnerInfoEnabled(userId) ? getOwnerInfo(userId) : "";
-
-        IBinder service = ServiceManager.getService("mount");
-        if (service == null) {
-            Log.e(TAG, "Could not find the mount service to update the user info");
-            return;
-        }
-
-        IStorageManager storageManager = IStorageManager.Stub.asInterface(service);
-        try {
-            Log.d(TAG, "Setting owner info");
-            storageManager.setField(StorageManager.OWNER_INFO_KEY, ownerInfo);
-        } catch (RemoteException e) {
-            Log.e(TAG, "Error changing user info", e);
-        }
-    }
-
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     public void setOwnerInfo(String info, int userId) {
         setString(LOCK_SCREEN_OWNER_INFO, info, userId);
-        updateCryptoUserInfo(userId);
     }
 
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
     public void setOwnerInfoEnabled(boolean enabled, int userId) {
         setBoolean(LOCK_SCREEN_OWNER_INFO_ENABLED, enabled, userId);
-        updateCryptoUserInfo(userId);
     }
 
     @UnsupportedAppUsage
@@ -808,17 +770,6 @@
     }
 
     /**
-     * Clears the encryption password.
-     */
-    public void clearEncryptionPassword() {
-        try {
-            getLockSettings().updateEncryptionPassword(StorageManager.CRYPT_TYPE_DEFAULT, null);
-        } catch (RemoteException e) {
-            Log.e(TAG, "Couldn't clear encryption password");
-        }
-    }
-
-    /**
      * Retrieves the quality mode for {@code userHandle}.
      * @see DevicePolicyManager#getPasswordQuality(android.content.ComponentName)
      *
@@ -843,7 +794,7 @@
      */
     public void setSeparateProfileChallengeEnabled(int userHandle, boolean enabled,
             LockscreenCredential profilePassword) {
-        if (!isCredentialSharedWithParent(userHandle)) {
+        if (!isCredentialSharableWithParent(userHandle)) {
             return;
         }
         try {
@@ -859,7 +810,7 @@
      * Returns true if {@code userHandle} is a managed profile with separate challenge.
      */
     public boolean isSeparateProfileChallengeEnabled(int userHandle) {
-        return isCredentialSharedWithParent(userHandle) && hasSeparateChallenge(userHandle);
+        return isCredentialSharableWithParent(userHandle) && hasSeparateChallenge(userHandle);
     }
 
     /**
@@ -884,8 +835,8 @@
         return info != null && info.isManagedProfile();
     }
 
-    private boolean isCredentialSharedWithParent(int userHandle) {
-        return getUserManager(userHandle).isCredentialSharedWithParent();
+    private boolean isCredentialSharableWithParent(int userHandle) {
+        return getUserManager(userHandle).isCredentialSharableWithParent();
     }
 
     /**
@@ -1042,24 +993,6 @@
      */
     public void setVisiblePatternEnabled(boolean enabled, int userId) {
         setBoolean(Settings.Secure.LOCK_PATTERN_VISIBLE, enabled, userId);
-
-        // Update for crypto if owner
-        if (userId != UserHandle.USER_SYSTEM) {
-            return;
-        }
-
-        IBinder service = ServiceManager.getService("mount");
-        if (service == null) {
-            Log.e(TAG, "Could not find the mount service to update the user info");
-            return;
-        }
-
-        IStorageManager storageManager = IStorageManager.Stub.asInterface(service);
-        try {
-            storageManager.setField(StorageManager.PATTERN_VISIBLE_KEY, enabled ? "1" : "0");
-        } catch (RemoteException e) {
-            Log.e(TAG, "Error changing pattern visible state", e);
-        }
     }
 
     public boolean isVisiblePatternEverChosen(int userId) {
@@ -1070,23 +1003,7 @@
      * Set whether the visible password is enabled for cryptkeeper screen.
      */
     public void setVisiblePasswordEnabled(boolean enabled, int userId) {
-        // Update for crypto if owner
-        if (userId != UserHandle.USER_SYSTEM) {
-            return;
-        }
-
-        IBinder service = ServiceManager.getService("mount");
-        if (service == null) {
-            Log.e(TAG, "Could not find the mount service to update the user info");
-            return;
-        }
-
-        IStorageManager storageManager = IStorageManager.Stub.asInterface(service);
-        try {
-            storageManager.setField(StorageManager.PASSWORD_VISIBLE_KEY, enabled ? "1" : "0");
-        } catch (RemoteException e) {
-            Log.e(TAG, "Error changing password visible state", e);
-        }
+        // No longer does anything.
     }
 
     /**
@@ -1204,7 +1121,7 @@
     public List<ComponentName> getEnabledTrustAgents(int userId) {
         String serialized = getString(ENABLED_TRUST_AGENTS, userId);
         if (TextUtils.isEmpty(serialized)) {
-            return null;
+            return new ArrayList<ComponentName>();
         }
         String[] split = serialized.split(",");
         ArrayList<ComponentName> activeTrustAgents = new ArrayList<ComponentName>(split.length);
diff --git a/core/java/com/android/internal/widget/LockPatternView.java b/core/java/com/android/internal/widget/LockPatternView.java
index 2b6b933..01cec77 100644
--- a/core/java/com/android/internal/widget/LockPatternView.java
+++ b/core/java/com/android/internal/widget/LockPatternView.java
@@ -45,6 +45,7 @@
 import android.util.IntArray;
 import android.util.Log;
 import android.util.SparseArray;
+import android.util.TypedValue;
 import android.view.HapticFeedbackConstants;
 import android.view.MotionEvent;
 import android.view.RenderNodeAnimator;
@@ -82,10 +83,12 @@
     private static final int DOT_ACTIVATION_DURATION_MILLIS = 50;
     private static final int DOT_RADIUS_INCREASE_DURATION_MILLIS = 96;
     private static final int DOT_RADIUS_DECREASE_DURATION_MILLIS = 192;
+    private static final float MIN_DOT_HIT_FACTOR = 0.2f;
     private final CellState[][] mCellStates;
 
     private final int mDotSize;
     private final int mDotSizeActivated;
+    private final float mDotHitFactor;
     private final int mPathWidth;
 
     private boolean mDrawingProfilingStarted = false;
@@ -143,12 +146,11 @@
     private boolean mPatternInProgress = false;
     private boolean mFadePattern = true;
 
-    private float mHitFactor = 0.6f;
-
     @UnsupportedAppUsage
     private float mSquareWidth;
     @UnsupportedAppUsage
     private float mSquareHeight;
+    private float mDotHitRadius;
     private final LinearGradient mFadeOutGradientShader;
 
     private final Path mCurrentPath = new Path();
@@ -164,8 +166,7 @@
 
     private final Interpolator mFastOutSlowInInterpolator;
     private final Interpolator mLinearOutSlowInInterpolator;
-    private PatternExploreByTouchHelper mExploreByTouchHelper;
-    private AudioManager mAudioManager;
+    private final PatternExploreByTouchHelper mExploreByTouchHelper;
 
     private Drawable mSelectedDrawable;
     private Drawable mNotSelectedDrawable;
@@ -349,6 +350,9 @@
         mDotSize = getResources().getDimensionPixelSize(R.dimen.lock_pattern_dot_size);
         mDotSizeActivated = getResources().getDimensionPixelSize(
                 R.dimen.lock_pattern_dot_size_activated);
+        TypedValue outValue = new TypedValue();
+        getResources().getValue(R.dimen.lock_pattern_dot_hit_factor, outValue, true);
+        mDotHitFactor = Math.max(Math.min(outValue.getFloat(), 1f), MIN_DOT_HIT_FACTOR);
 
         mUseLockPatternDrawable = getResources().getBoolean(R.bool.use_lock_pattern_drawable);
         if (mUseLockPatternDrawable) {
@@ -375,7 +379,6 @@
                 AnimationUtils.loadInterpolator(context, android.R.interpolator.linear_out_slow_in);
         mExploreByTouchHelper = new PatternExploreByTouchHelper(this);
         setAccessibilityDelegate(mExploreByTouchHelper);
-        mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
 
         int fadeAwayGradientWidth = getResources().getDimensionPixelSize(
                 R.dimen.lock_pattern_fade_away_gradient_width);
@@ -679,6 +682,7 @@
         final int height = h - mPaddingTop - mPaddingBottom;
         mSquareHeight = height / 3.0f;
         mExploreByTouchHelper.invalidateRoot();
+        mDotHitRadius = Math.min(mSquareHeight / 2, mSquareWidth / 2) * mDotHitFactor;
 
         if (mUseLockPatternDrawable) {
             mNotSelectedDrawable.setBounds(mPaddingLeft, mPaddingTop, width, height);
@@ -890,63 +894,30 @@
         return set;
     }
 
-    // helper method to find which cell a point maps to
+    @Nullable
     private Cell checkForNewHit(float x, float y) {
-
-        final int rowHit = getRowHit(y);
-        if (rowHit < 0) {
-            return null;
+        Cell cellHit = detectCellHit(x, y);
+        if (cellHit != null && !mPatternDrawLookup[cellHit.row][cellHit.column]) {
+            return cellHit;
         }
-        final int columnHit = getColumnHit(x);
-        if (columnHit < 0) {
-            return null;
-        }
-
-        if (mPatternDrawLookup[rowHit][columnHit]) {
-            return null;
-        }
-        return Cell.of(rowHit, columnHit);
+        return null;
     }
 
-    /**
-     * Helper method to find the row that y falls into.
-     * @param y The y coordinate
-     * @return The row that y falls in, or -1 if it falls in no row.
-     */
-    private int getRowHit(float y) {
-
-        final float squareHeight = mSquareHeight;
-        float hitSize = squareHeight * mHitFactor;
-
-        float offset = mPaddingTop + (squareHeight - hitSize) / 2f;
-        for (int i = 0; i < 3; i++) {
-
-            final float hitTop = offset + squareHeight * i;
-            if (y >= hitTop && y <= hitTop + hitSize) {
-                return i;
+    /** Helper method to find which cell a point maps to. */
+    @Nullable
+    private Cell detectCellHit(float x, float y) {
+        final float hitRadiusSquared = mDotHitRadius * mDotHitRadius;
+        for (int row = 0; row < 3; row++) {
+            for (int column = 0; column < 3; column++) {
+                float centerY = getCenterYForRow(row);
+                float centerX = getCenterXForColumn(column);
+                if ((x - centerX) * (x - centerX) + (y - centerY) * (y - centerY)
+                        < hitRadiusSquared) {
+                    return Cell.of(row, column);
+                }
             }
         }
-        return -1;
-    }
-
-    /**
-     * Helper method to find the column x fallis into.
-     * @param x The x coordinate.
-     * @return The column that x falls in, or -1 if it falls in no column.
-     */
-    private int getColumnHit(float x) {
-        final float squareWidth = mSquareWidth;
-        float hitSize = squareWidth * mHitFactor;
-
-        float offset = mPaddingLeft + (squareWidth - hitSize) / 2f;
-        for (int i = 0; i < 3; i++) {
-
-            final float hitLeft = offset + squareWidth * i;
-            if (x >= hitLeft && x <= hitLeft + hitSize) {
-                return i;
-            }
-        }
-        return -1;
+        return null;
     }
 
     @Override
@@ -1553,8 +1524,7 @@
         protected int getVirtualViewAt(float x, float y) {
             // This must use the same hit logic for the screen to ensure consistency whether
             // accessibility is on or off.
-            int id = getVirtualViewIdForHit(x, y);
-            return id;
+            return getVirtualViewIdForHit(x, y);
         }
 
         @Override
@@ -1670,12 +1640,11 @@
             final int col = ordinal % 3;
             float centerX = getCenterXForColumn(col);
             float centerY = getCenterYForRow(row);
-            float cellheight = mSquareHeight * mHitFactor * 0.5f;
-            float cellwidth = mSquareWidth * mHitFactor * 0.5f;
-            bounds.left = (int) (centerX - cellwidth);
-            bounds.right = (int) (centerX + cellwidth);
-            bounds.top = (int) (centerY - cellheight);
-            bounds.bottom = (int) (centerY + cellheight);
+            float cellHitRadius = mDotHitRadius;
+            bounds.left = (int) (centerX - cellHitRadius);
+            bounds.right = (int) (centerX + cellHitRadius);
+            bounds.top = (int) (centerY - cellHitRadius);
+            bounds.bottom = (int) (centerY + cellHitRadius);
             return bounds;
         }
 
@@ -1694,16 +1663,12 @@
          * @return VIRTUAL_BASE_VIEW_ID+id or 0 if no view was hit
          */
         private int getVirtualViewIdForHit(float x, float y) {
-            final int rowHit = getRowHit(y);
-            if (rowHit < 0) {
+            Cell cellHit = detectCellHit(x, y);
+            if (cellHit == null) {
                 return ExploreByTouchHelper.INVALID_ID;
             }
-            final int columnHit = getColumnHit(x);
-            if (columnHit < 0) {
-                return ExploreByTouchHelper.INVALID_ID;
-            }
-            boolean dotAvailable = mPatternDrawLookup[rowHit][columnHit];
-            int dotId = (rowHit * 3 + columnHit) + VIRTUAL_BASE_VIEW_ID;
+            boolean dotAvailable = mPatternDrawLookup[cellHit.row][cellHit.column];
+            int dotId = (cellHit.row * 3 + cellHit.column) + VIRTUAL_BASE_VIEW_ID;
             int view = dotAvailable ? dotId : ExploreByTouchHelper.INVALID_ID;
             if (DEBUG_A11Y) Log.v(TAG, "getVirtualViewIdForHit(" + x + "," + y + ") => "
                     + view + "avail =" + dotAvailable);
diff --git a/core/java/com/android/internal/widget/MessagingGroup.java b/core/java/com/android/internal/widget/MessagingGroup.java
index 9e06e33..146cb3f 100644
--- a/core/java/com/android/internal/widget/MessagingGroup.java
+++ b/core/java/com/android/internal/widget/MessagingGroup.java
@@ -262,7 +262,8 @@
         return createdGroup;
     }
 
-    public void removeMessage(MessagingMessage messagingMessage) {
+    public void removeMessage(MessagingMessage messagingMessage,
+            ArrayList<MessagingLinearLayout.MessagingChild> toRecycle) {
         View view = messagingMessage.getView();
         boolean wasShown = view.isShown();
         ViewGroup messageParent = (ViewGroup) view.getParent();
@@ -270,15 +271,14 @@
             return;
         }
         messageParent.removeView(view);
-        Runnable recycleRunnable = () -> {
-            messageParent.removeTransientView(view);
-            messagingMessage.recycle();
-        };
         if (wasShown && !MessagingLinearLayout.isGone(view)) {
             messageParent.addTransientView(view, 0);
-            performRemoveAnimation(view, recycleRunnable);
+            performRemoveAnimation(view, () -> {
+                messageParent.removeTransientView(view);
+                messagingMessage.recycle();
+            });
         } else {
-            recycleRunnable.run();
+            toRecycle.add(messagingMessage);
         }
     }
 
diff --git a/core/java/com/android/internal/widget/MessagingLayout.java b/core/java/com/android/internal/widget/MessagingLayout.java
index 21ca196..9ac6ef7 100644
--- a/core/java/com/android/internal/widget/MessagingLayout.java
+++ b/core/java/com/android/internal/widget/MessagingLayout.java
@@ -51,7 +51,6 @@
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
-import java.util.function.Consumer;
 
 /**
  * A custom-built layout for the Notification.MessagingStyle allows dynamic addition and removal
@@ -62,8 +61,6 @@
         implements ImageMessageConsumer, IMessagingLayout {
 
     private static final float COLOR_SHIFT_AMOUNT = 60;
-    private static final Consumer<MessagingMessage> REMOVE_MESSAGE
-            = MessagingMessage::removeMessage;
     public static final Interpolator LINEAR_OUT_SLOW_IN = new PathInterpolator(0f, 0f, 0.2f, 1f);
     public static final Interpolator FAST_OUT_LINEAR_IN = new PathInterpolator(0.4f, 0f, 1f, 1f);
     public static final Interpolator FAST_OUT_SLOW_IN = new PathInterpolator(0.4f, 0f, 0.2f, 1f);
@@ -89,6 +86,7 @@
     private boolean mIsCollapsed;
     private ImageResolver mImageResolver;
     private CharSequence mConversationTitle;
+    private ArrayList<MessagingLinearLayout.MessagingChild> mToRecycle = new ArrayList<>();
 
     public MessagingLayout(@NonNull Context context) {
         super(context);
@@ -212,8 +210,12 @@
         removeGroups(oldGroups);
 
         // Let's remove the remaining messages
-        mMessages.forEach(REMOVE_MESSAGE);
-        mHistoricMessages.forEach(REMOVE_MESSAGE);
+        for (MessagingMessage message : mMessages) {
+            message.removeMessage(mToRecycle);
+        }
+        for (MessagingMessage historicMessage : mHistoricMessages) {
+            historicMessage.removeMessage(mToRecycle);
+        }
 
         mMessages = messages;
         mHistoricMessages = historicMessages;
@@ -223,6 +225,12 @@
         // after groups are finalized, hide the first sender name if it's showing as the title
         mPeopleHelper.maybeHideFirstSenderName(mGroups, mIsOneToOne, mConversationTitle);
         updateImageMessages();
+
+        // Recycle everything at the end of the update, now that we know it's no longer needed.
+        for (MessagingLinearLayout.MessagingChild child : mToRecycle) {
+            child.recycle();
+        }
+        mToRecycle.clear();
     }
 
     private void updateImageMessages() {
@@ -263,18 +271,17 @@
             MessagingGroup group = oldGroups.get(i);
             if (!mGroups.contains(group)) {
                 List<MessagingMessage> messages = group.getMessages();
-                Runnable endRunnable = () -> {
-                    mMessagingLinearLayout.removeTransientView(group);
-                    group.recycle();
-                };
 
                 boolean wasShown = group.isShown();
                 mMessagingLinearLayout.removeView(group);
                 if (wasShown && !MessagingLinearLayout.isGone(group)) {
                     mMessagingLinearLayout.addTransientView(group, 0);
-                    group.removeGroupAnimated(endRunnable);
+                    group.removeGroupAnimated(() -> {
+                        mMessagingLinearLayout.removeTransientView(group);
+                        group.recycle();
+                    });
                 } else {
-                    endRunnable.run();
+                    mToRecycle.add(group);
                 }
                 mMessages.removeAll(messages);
                 mHistoricMessages.removeAll(messages);
diff --git a/core/java/com/android/internal/widget/MessagingLinearLayout.java b/core/java/com/android/internal/widget/MessagingLinearLayout.java
index cb1d387..c06f5f7 100644
--- a/core/java/com/android/internal/widget/MessagingLinearLayout.java
+++ b/core/java/com/android/internal/widget/MessagingLinearLayout.java
@@ -365,6 +365,7 @@
         default int getExtraSpacing() {
             return 0;
         }
+        void recycle();
     }
 
     public static class LayoutParams extends MarginLayoutParams {
diff --git a/core/java/com/android/internal/widget/MessagingMessage.java b/core/java/com/android/internal/widget/MessagingMessage.java
index 8c84379..2cc0d23 100644
--- a/core/java/com/android/internal/widget/MessagingMessage.java
+++ b/core/java/com/android/internal/widget/MessagingMessage.java
@@ -20,6 +20,7 @@
 import android.app.Notification;
 import android.view.View;
 
+import java.util.ArrayList;
 import java.util.Objects;
 
 /**
@@ -96,8 +97,8 @@
         return sameAs(message.getMessage());
     }
 
-    default void removeMessage() {
-        getGroup().removeMessage(this);
+    default void removeMessage(ArrayList<MessagingLinearLayout.MessagingChild> toRecycle) {
+        getGroup().removeMessage(this, toRecycle);
     }
 
     default void setMessagingGroup(MessagingGroup group) {
diff --git a/core/java/com/android/server/SystemConfig.java b/core/java/com/android/server/SystemConfig.java
index b1846d2..cc7e9d9 100644
--- a/core/java/com/android/server/SystemConfig.java
+++ b/core/java/com/android/server/SystemConfig.java
@@ -435,15 +435,15 @@
     }
 
     /** Get privapp permission allowlist for an apk-in-apex. */
-    public ArraySet<String> getApexPrivAppPermissions(String module, String packageName) {
-        return mApexPrivAppPermissions.getOrDefault(module, EMPTY_PERMISSIONS)
-                .get(packageName);
+    public ArraySet<String> getApexPrivAppPermissions(String apexName, String apkPackageName) {
+        return mApexPrivAppPermissions.getOrDefault(apexName, EMPTY_PERMISSIONS)
+                .get(apkPackageName);
     }
 
     /** Get privapp permissions denylist for an apk-in-apex. */
-    public ArraySet<String> getApexPrivAppDenyPermissions(String module, String packageName) {
-        return mApexPrivAppDenyPermissions.getOrDefault(module, EMPTY_PERMISSIONS)
-                .get(packageName);
+    public ArraySet<String> getApexPrivAppDenyPermissions(String apexName, String apkPackageName) {
+        return mApexPrivAppDenyPermissions.getOrDefault(apexName, EMPTY_PERMISSIONS)
+                .get(apkPackageName);
     }
 
     public ArraySet<String> getVendorPrivAppPermissions(String packageName) {
diff --git a/core/jni/Android.bp b/core/jni/Android.bp
index 4aa00f6..3a76745 100644
--- a/core/jni/Android.bp
+++ b/core/jni/Android.bp
@@ -361,7 +361,7 @@
                 "libwuffs_mirror_release_c",
             ],
         },
-        linux_glibc: {
+        host_linux: {
             srcs: [
                 "android_content_res_ApkAssets.cpp",
                 "android_database_CursorWindow.cpp",
diff --git a/core/jni/AndroidRuntime.cpp b/core/jni/AndroidRuntime.cpp
index eedf7fa..eba6cca 100644
--- a/core/jni/AndroidRuntime.cpp
+++ b/core/jni/AndroidRuntime.cpp
@@ -1567,6 +1567,7 @@
         REG_JNI(register_android_graphics_classes),
         REG_JNI(register_android_graphics_BLASTBufferQueue),
         REG_JNI(register_android_graphics_GraphicBuffer),
+        REG_JNI(register_android_graphics_GraphicsStatsService),
         REG_JNI(register_android_graphics_SurfaceTexture),
         REG_JNI(register_android_database_CursorWindow),
         REG_JNI(register_android_database_SQLiteConnection),
diff --git a/core/jni/com_android_internal_os_Zygote.cpp b/core/jni/com_android_internal_os_Zygote.cpp
index 5b7092c..7bc6905 100644
--- a/core/jni/com_android_internal_os_Zygote.cpp
+++ b/core/jni/com_android_internal_os_Zygote.cpp
@@ -895,7 +895,7 @@
 
   // pthread_setname_np fails rather than truncating long strings.
   char buf[16];       // MAX_TASK_COMM_LEN=16 is hard-coded into bionic
-  strlcpy(buf, name_start_ptr, sizeof(buf) - 1);
+  strlcpy(buf, name_start_ptr, sizeof(buf));
   errno = pthread_setname_np(pthread_self(), buf);
   if (errno != 0) {
     ALOGW("Unable to set the name of current thread to '%s': %s", buf, strerror(errno));
diff --git a/core/jni/com_android_internal_os_ZygoteCommandBuffer.cpp b/core/jni/com_android_internal_os_ZygoteCommandBuffer.cpp
index 0c05da5..679a4f0 100644
--- a/core/jni/com_android_internal_os_ZygoteCommandBuffer.cpp
+++ b/core/jni/com_android_internal_os_ZygoteCommandBuffer.cpp
@@ -34,6 +34,7 @@
 #include <sys/mman.h>
 #include <sys/types.h>
 #include <sys/socket.h>
+#include <sys/system_properties.h>
 #include <vector>
 
 namespace android {
@@ -43,10 +44,10 @@
 using android::zygote::ZygoteFailure;
 
 // WARNING: Knows a little about the wire protocol used to communicate with Zygote.
-// TODO: Fix error handling.
 
+// Commands and nice names have large arbitrary size limits to avoid dynamic memory allocation.
 constexpr size_t MAX_COMMAND_BYTES = 32768;
-constexpr size_t NICE_NAME_BYTES = 50;
+constexpr size_t NICE_NAME_BYTES = 128;
 
 // A buffer optionally bundled with a file descriptor from which we can fill it.
 // Does not own the file descriptor; destroying a NativeCommandBuffer does not
@@ -190,6 +191,9 @@
         size_t copy_len = std::min(name_len, NICE_NAME_BYTES - 1);
         memcpy(mNiceName, arg_start + NN_LENGTH, copy_len);
         mNiceName[copy_len] = '\0';
+        if (haveWrapProperty()) {
+          return false;
+        }
         continue;
       }
       if (arg_end - arg_start == IW_LENGTH
@@ -222,6 +226,8 @@
         }
         saw_setgid = true;
       }
+      // ro.debuggable can be handled entirely in the child unless --invoke-with is also specified.
+      // Thus we do not need to check it here.
     }
     return saw_runtime_args && saw_setuid && saw_setgid;
   }
@@ -249,6 +255,14 @@
   }
 
  private:
+  bool haveWrapProperty() {
+    static const char* WRAP = "wrap.";
+    static const size_t WRAP_LENGTH = strlen(WRAP);
+    char propNameBuf[WRAP_LENGTH + NICE_NAME_BYTES];
+    strcpy(propNameBuf, WRAP);
+    strlcpy(propNameBuf + WRAP_LENGTH, mNiceName, NICE_NAME_BYTES);
+    return __system_property_find(propNameBuf) != nullptr;
+  }
   // Picky version of atoi(). No sign or unexpected characters allowed. Return -1 on failure.
   static int digitsVal(char* start, char* end) {
     int result = 0;
@@ -269,7 +283,7 @@
   uint32_t mNext;  // Index of first character past last line returned by readLine.
   int32_t mLinesLeft;  // Lines in current command that haven't yet been read.
   int mFd;  // Open file descriptor from which we can read more. -1 if none.
-  char mNiceName[NICE_NAME_BYTES];
+  char mNiceName[NICE_NAME_BYTES];  // Always null terminated.
   char mBuffer[MAX_COMMAND_BYTES];
 };
 
@@ -372,6 +386,7 @@
             jint minUid,
             jstring managed_nice_name) {
 
+  ALOGI("Entering forkRepeatedly native zygote loop");
   NativeCommandBuffer* n_buffer = reinterpret_cast<NativeCommandBuffer*>(j_buffer);
   int session_socket = n_buffer->getFd();
   std::vector<int> session_socket_fds {session_socket};
@@ -400,7 +415,8 @@
   socklen_t cred_size = sizeof credentials;
   if (getsockopt(n_buffer->getFd(), SOL_SOCKET, SO_PEERCRED, &credentials, &cred_size) == -1
       || cred_size != sizeof credentials) {
-    fail_fn_1(CREATE_ERROR("ForkMany failed to get initial credentials, %s", strerror(errno)));
+    fail_fn_1(CREATE_ERROR("ForkRepeatedly failed to get initial credentials, %s",
+                           strerror(errno)));
   }
 
   bool first_time = true;
diff --git a/core/proto/android/os/batteryusagestats.proto b/core/proto/android/os/batteryusagestats.proto
index cc90e05..856bc83 100644
--- a/core/proto/android/os/batteryusagestats.proto
+++ b/core/proto/android/os/batteryusagestats.proto
@@ -76,6 +76,7 @@
                 FOREGROUND = 1;
                 BACKGROUND = 2;
                 FOREGROUND_SERVICE = 3;
+                CACHED = 4;
             }
 
             optional ProcessState process_state = 2;
diff --git a/core/res/AndroidManifest.xml b/core/res/AndroidManifest.xml
index 21baa0b..729bc82 100644
--- a/core/res/AndroidManifest.xml
+++ b/core/res/AndroidManifest.xml
@@ -1030,10 +1030,10 @@
       targetSdkVersion}</a> of {@link android.os.Build.VERSION_CODES#S} or lower, this permission
       must not be used and the READ_EXTERNAL_STORAGE permission must be used instead.
      <p>Protection level: dangerous -->
-    <permission android:name="android.permission.READ_MEDIA_IMAGE"
+    <permission android:name="android.permission.READ_MEDIA_IMAGES"
                 android:permissionGroup="android.permission-group.UNDEFINED"
-                android:label="@string/permlab_readMediaImage"
-                android:description="@string/permdesc_readMediaImage"
+                android:label="@string/permlab_readMediaImages"
+                android:description="@string/permdesc_readMediaImages"
                 android:protectionLevel="dangerous" />
 
     <!-- Allows an application to write to external storage.
@@ -1901,11 +1901,21 @@
     <!-- Allows applications to enable/disable wifi auto join. This permission
          is used to let OEMs grant their trusted app access to a subset of privileged wifi APIs
          to improve wifi performance.
-         <p>Not for use by third-party applications. -->
+         <p>Not for use by third-party applications.
+         @deprecated will be replaced with MANAGE_WIFI_NETWORK_SELECTION -->
     <permission android:name="android.permission.MANAGE_WIFI_AUTO_JOIN"
                 android:protectionLevel="signature|privileged|knownSigner"
                 android:knownCerts="@array/wifi_known_signers" />
 
+    <!-- This permission is used to let OEMs grant their trusted app access to a subset of
+         privileged wifi APIs to improve wifi performance. Allows applications to manage
+         Wi-Fi network selection related features such as enable or disable global auto-join,
+         modify connectivity scan intervals, and approve Wi-Fi Direct connections.
+         <p>Not for use by third-party applications. -->
+    <permission android:name="android.permission.MANAGE_WIFI_NETWORK_SELECTION"
+                android:protectionLevel="signature|privileged|knownSigner"
+                android:knownCerts="@array/wifi_known_signers" />
+
     <!-- Allows applications to get notified when a Wi-Fi interface request cannot
          be satisfied without tearing down one or more other interfaces, and provide a decision
          whether to approve the request or reject it.
@@ -3089,7 +3099,7 @@
 
          <p>Not for use by third-party applications. -->
     <permission android:name="android.permission.SYSTEM_APPLICATION_OVERLAY"
-                android:protectionLevel="signature|recents|role"/>
+                android:protectionLevel="signature|recents|role|installer"/>
 
     <!-- @deprecated Use {@link android.Manifest.permission#REQUEST_COMPANION_RUN_IN_BACKGROUND}
          @hide
@@ -6041,10 +6051,10 @@
     <permission android:name="android.permission.MANAGE_APPOPS"
                 android:protectionLevel="signature" />
 
-    <!-- @hide Permission that allows background clipboard access.
-         <p>Not for use by third-party applications. -->
+    <!-- @SystemApi Permission that allows background clipboard access.
+         @hide Not for use by third-party applications. -->
     <permission android:name="android.permission.READ_CLIPBOARD_IN_BACKGROUND"
-        android:protectionLevel="signature" />
+        android:protectionLevel="internal|role" />
     <!-- @hide Permission that suppresses the notification when the clipboard is accessed.
          <p>Not for use by third-party applications. -->
     <permission android:name="android.permission.SUPPRESS_CLIPBOARD_ACCESS_NOTIFICATION"
@@ -6150,10 +6160,10 @@
     <!-- Allows input events to be monitored. Very dangerous!  @hide -->
     <permission android:name="android.permission.MONITOR_INPUT"
                 android:protectionLevel="signature|recents" />
-    <!-- Allows the use of FLAG_SLIPPERY, which permits touch events to slip from the current
-         window to the window where the touch currently is on top of.  @hide -->
+    <!-- @SystemApi Allows the use of FLAG_SLIPPERY, which permits touch events to slip from the
+         current window to the window where the touch currently is on top of.  @hide -->
     <permission android:name="android.permission.ALLOW_SLIPPERY_TOUCHES"
-                android:protectionLevel="signature|recents" />
+                android:protectionLevel="signature|privileged|recents|role" />
     <!--  Allows the caller to change the associations between input devices and displays.
         Very dangerous! @hide -->
     <permission android:name="android.permission.ASSOCIATE_INPUT_DEVICE_TO_DISPLAY"
@@ -6410,6 +6420,12 @@
     <permission android:name="android.permission.WRITE_SECURITY_LOG"
         android:protectionLevel="signature|privileged" />
 
+    <!-- Allows an UID to be visible to the application based on an interaction between the
+         two apps. This permission is not intended to be held by apps.
+         @hide @TestApi  -->
+    <permission android:name="android.permission.MAKE_UID_VISIBLE"
+                android:protectionLevel="signature" />
+
     <!-- Attribution for Geofencing service. -->
     <attribution android:tag="GeofencingService" android:label="@string/geofencing_service"/>
     <!-- Attribution for Country Detector. -->
@@ -6829,6 +6845,13 @@
             </intent-filter>
         </receiver>
 
+        <receiver android:name="com.android.server.sdksandbox.SdkSandboxVerifierReceiver"
+                 android:exported="false">
+            <intent-filter>
+                <action android:name="android.intent.action.PACKAGE_NEEDS_VERIFICATION"/>
+            </intent-filter>
+        </receiver>
+
         <service android:name="android.hardware.location.GeofenceHardwareService"
             android:permission="android.permission.LOCATION_HARDWARE"
             android:exported="false" />
diff --git a/core/res/res/drawable-nodpi/stat_sys_adb.xml b/core/res/res/drawable-nodpi/stat_sys_adb.xml
index f8c0451..cb4462c 100644
--- a/core/res/res/drawable-nodpi/stat_sys_adb.xml
+++ b/core/res/res/drawable-nodpi/stat_sys_adb.xml
@@ -1,5 +1,5 @@
 <!--
-Copyright (C) 2021 The Android Open Source Project
+Copyright (C) 2022 The Android Open Source Project
 
    Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
@@ -13,11 +13,78 @@
     See the License for the specific language governing permissions and
     limitations under the License.
 -->
-<vector xmlns:android="http://schemas.android.com/apk/res/android"
-    android:width="24dp" android:height="24dp"
-    android:viewportWidth="24"
-    android:viewportHeight="24">
-  <path android:pathData="M18 9c0-.69268-.1174-1.35795-.3333-1.97699C16.8495 4.68061 14.621 3 12 3 8.68629 3 6 5.68629 6 9v3h6M6 15c0 .6927.11738 1.3579.33333 1.977C7.15047 19.3194 9.37897 21 12 21c3.3137 0 6-2.6863 6-6v-3h-6" android:strokeColor="#000000" android:strokeWidth="2" android:fillColor="#00000000"/>
-  <path android:fillColor="#000000" android:pathData="M10 7a1 1 0 1 0 0 2 1 1 0 1 0 0-2zM14 7a1 1 0 1 0 0 2 1 1 0 1 0 0-2z"/>
-  <path android:pathData="M6 3l1.5 1.5M18 3l-1.5 1.5" android:strokeColor="#000000" android:strokeWidth="2" android:fillColor="#000000"/>
+<vector android:width="24dp" android:height="24dp"
+        android:viewportWidth="24" android:viewportHeight="24"
+        xmlns:android="http://schemas.android.com/apk/res/android">
+  <path android:pathData="
+        M22.45 11.94
+        l-.58-.21
+        a1.19 1.19 0 0 1-.26-2.12
+        l.51-.34
+        a1.2 1.2 0 0 0-.83-2.19
+        l-.61.08
+        a1.2 1.2 0 0 1-1.21-1.76
+        l.29-.54
+        A1.2 1.2 0 0 0 18 3.31
+        l-.5.36
+        a1.21 1.21 0 0 1-1.9-1
+        v-.61
+        a1.2 1.2 0 0 0-2.27-.56
+        l-.26.5
+        a1.2 1.2 0 0 1-2.14 0
+        l-.28-.54
+        a1.2 1.2 0 0 0-2.27.56
+        v.61
+        a1.21 1.21 0 0 1-1.9 1
+        L6 3.31
+        a1.2 1.2 0 0 0-1.76 1.55
+        l.29.54
+        a1.2 1.2 0 0 1-1.21 1.76
+        l-.61-.08
+        a1.2 1.2 0 0 0-.83 2.19
+        l.51.34
+        a1.19 1.19 0 0 1-.26 2.12
+        l-.58.21
+        a1.21 1.21 0 0 0 .29 2.33
+        l.61.06
+        a1.2 1.2 0 0 1 .76 2
+        l-.42.46
+        a1.2 1.2 0 0 0 1.33 1.92
+        l.57-.22
+        a1.21 1.21 0 0 1 1.61 1.42
+        l-.16.59
+        a1.2 1.2 0 0 0 2.07 1.09
+        l.4-.47
+        a1.2 1.2 0 0 1 2.08.51
+        l.14.6
+        a1.2 1.2 0 0 0 2.34 0
+        l.14-.6
+        a1.2 1.2 0 0 1 2.08-.51
+        l.4.47
+        a1.2 1.2 0 0 0 2.07-1.09
+        l-.16-.59
+        a1.21 1.21 0 0 1 1.61-1.42
+        l.57.22
+        a1.2 1.2 0 0 0 1.33-1.92
+        l-.42-.46
+        a1.2 1.2 0 0 1 .76-2
+        l.61-.06
+        a1.21 1.21 0 0 0 .29-2.33
+        z
+        M12 19
+        a7 7 0 1 1 7-7 7 7 0 0 1-7 7
+        z
+        "
+        android:fillColor="#000000" />
+  <path android:pathData="
+        M9 7.75
+        a.75.75 0 1 0 0 1.5.75.75 0 1 0 0-1.5
+        z
+        M15 7.75
+        a.75.75 0 1 0 0 1.5.75.75 0 1 0 0-1.5
+        z
+        "
+        android:fillColor="#000000" />
+  <path android:strokeColor="#000000" android:strokeMiterLimit="10" android:strokeWidth="2"
+        android:pathData="M4 12h16M12 12v8" />
 </vector>
diff --git a/core/res/res/layout/notification_template_header.xml b/core/res/res/layout/notification_template_header.xml
index 81a79c5..a7f2aa7 100644
--- a/core/res/res/layout/notification_template_header.xml
+++ b/core/res/res/layout/notification_template_header.xml
@@ -49,6 +49,8 @@
         android:layout_marginStart="@dimen/notification_icon_circle_start"
         android:background="@drawable/notification_icon_circle"
         android:padding="@dimen/notification_icon_circle_padding"
+        android:maxDrawableWidth="@dimen/notification_icon_circle_size"
+        android:maxDrawableHeight="@dimen/notification_icon_circle_size"
         />
 
     <!-- extends ViewGroup -->
diff --git a/core/res/res/layout/notification_template_material_base.xml b/core/res/res/layout/notification_template_material_base.xml
index c6983ae..fd787f6 100644
--- a/core/res/res/layout/notification_template_material_base.xml
+++ b/core/res/res/layout/notification_template_material_base.xml
@@ -45,6 +45,8 @@
         android:layout_marginStart="@dimen/notification_icon_circle_start"
         android:background="@drawable/notification_icon_circle"
         android:padding="@dimen/notification_icon_circle_padding"
+        android:maxDrawableWidth="@dimen/notification_icon_circle_size"
+        android:maxDrawableHeight="@dimen/notification_icon_circle_size"
         />
 
     <FrameLayout
@@ -136,7 +138,7 @@
 
         </LinearLayout>
 
-        <ImageView
+        <com.android.internal.widget.CachingIconView
             android:id="@+id/right_icon"
             android:layout_width="@dimen/notification_right_icon_size"
             android:layout_height="@dimen/notification_right_icon_size"
@@ -148,6 +150,8 @@
             android:clipToOutline="true"
             android:importantForAccessibility="no"
             android:scaleType="centerCrop"
+            android:maxDrawableWidth="@dimen/notification_right_icon_size"
+            android:maxDrawableHeight="@dimen/notification_right_icon_size"
             />
 
         <FrameLayout
diff --git a/core/res/res/layout/notification_template_right_icon.xml b/core/res/res/layout/notification_template_right_icon.xml
index f163ed5..8b3b795 100644
--- a/core/res/res/layout/notification_template_right_icon.xml
+++ b/core/res/res/layout/notification_template_right_icon.xml
@@ -13,7 +13,7 @@
   ~ See the License for the specific language governing permissions and
   ~ limitations under the License
   -->
-<ImageView
+<com.android.internal.widget.CachingIconView
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/right_icon"
     android:layout_width="@dimen/notification_right_icon_size"
@@ -25,4 +25,6 @@
     android:clipToOutline="true"
     android:importantForAccessibility="no"
     android:scaleType="centerCrop"
+    android:maxDrawableWidth="@dimen/notification_right_icon_size"
+    android:maxDrawableHeight="@dimen/notification_right_icon_size"
     />
diff --git a/core/res/res/values/attrs.xml b/core/res/res/values/attrs.xml
index da5f899..b3203ae 100644
--- a/core/res/res/values/attrs.xml
+++ b/core/res/res/values/attrs.xml
@@ -5479,9 +5479,9 @@
             <enum name="none" value="0" />
             <!-- Use the least restrictive rule for line-breaking. -->
             <enum name="loose" value="1" />
-            <!-- Indicate breaking text with the most comment set of line-breaking rules. -->
+            <!-- Indicates breaking text with the most comment set of line-breaking rules. -->
             <enum name="normal" value="2" />
-            <!-- ndicates breaking text with the most strictest line-breaking rules. -->
+            <!-- Indicates breaking text with the most strictest line-breaking rules. -->
             <enum name="strict" value="3" />
         </attr>
         <!-- Specify the phrase-based line break can be used when calculating the text wrapping.-->
@@ -9807,4 +9807,12 @@
         of the supported locale. {@link android.app.LocaleConfig} -->
         <attr name="name" />
     </declare-styleable>
+
+    <!-- @hide -->
+    <declare-styleable name="CachingIconView">
+        <!-- Maximum width of displayed drawable. Drawables exceeding this size will be downsampled. -->
+        <attr name="maxDrawableWidth" format="dimension"/>
+        <!-- Maximum width of height drawable. Drawables exceeding this size will be downsampled. -->
+        <attr name="maxDrawableHeight" format="dimension"/>
+    </declare-styleable>
     </resources>
diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml
index 0bdf716..269aa1b 100644
--- a/core/res/res/values/config.xml
+++ b/core/res/res/values/config.xml
@@ -2335,7 +2335,7 @@
     <!-- Remote server that can provide NTP responses. -->
     <string translatable="false" name="config_ntpServer">time.android.com</string>
     <!-- Normal polling frequency in milliseconds -->
-    <integer name="config_ntpPollingInterval">86400000</integer>
+    <integer name="config_ntpPollingInterval">64800000</integer>
     <!-- Try-again polling interval in milliseconds, in case the network request failed -->
     <integer name="config_ntpPollingIntervalShorter">60000</integer>
     <!-- Number of times to try again with the shorter interval, before backing
@@ -2723,7 +2723,7 @@
     <string name="config_bandwidthEstimateSource">bandwidth_estimator</string>
 
     <!-- Whether force to enable telephony new data stack or not -->
-    <bool name="config_force_enable_telephony_new_data_stack">true</bool>
+    <bool name="config_force_enable_telephony_new_data_stack">false</bool>
 
     <!-- Whether WiFi display is supported by this device.
          There are many prerequisites for this feature to work correctly.
@@ -2988,6 +2988,12 @@
 
     </string-array>
 
+    <!-- When migrating notification settings into the permission framework, whether all existing
+         apps should be marked as 'user-set' (true) or whether only the apps that have explicitly
+         modified notification settings should be marked as 'user-set' (false). Users will not see
+         system generated permission prompts for 'user-set' apps. -->
+    <bool name="config_notificationForceUserSetOnUpgrade">true</bool>
+
     <!-- Default Gravity setting for the system Toast view. Equivalent to: Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM -->
     <integer name="config_toastDefaultGravity">0x00000051</integer>
 
diff --git a/core/res/res/values/dimens.xml b/core/res/res/values/dimens.xml
index 1b9f7fe..44c5512 100644
--- a/core/res/res/values/dimens.xml
+++ b/core/res/res/values/dimens.xml
@@ -668,6 +668,9 @@
     <dimen name="lock_pattern_dot_line_width">22dp</dimen>
     <dimen name="lock_pattern_dot_size">14dp</dimen>
     <dimen name="lock_pattern_dot_size_activated">30dp</dimen>
+    <!-- How much of the cell space is classified as hit areas [0..1] where 1 means that hit area is
+         a circle with diameter equals to cell minimum side min(width, height). -->
+    <item type="dimen" format="float" name="lock_pattern_dot_hit_factor">0.6</item>
     <!-- Width of a gradient applied to a lock pattern line while its disappearing animation. -->
     <dimen name="lock_pattern_fade_away_gradient_width">8dp</dimen>
 
diff --git a/core/res/res/values/public-staging.xml b/core/res/res/values/public-staging.xml
index 2dc17b8..0a4c4c0 100644
--- a/core/res/res/values/public-staging.xml
+++ b/core/res/res/values/public-staging.xml
@@ -148,6 +148,10 @@
     <public name="supportsInlineSuggestionsWithTouchExploration" />
     <public name="lineBreakStyle" />
     <public name="lineBreakWordStyle" />
+    <!-- @hide -->
+    <public name="maxDrawableWidth" />
+    <!-- @hide -->
+    <public name="maxDrawableHeight" />
   </staging-public-group>
 
   <staging-public-group type="id" first-id="0x01de0000">
diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml
index 602e42d..4c71b3a 100644
--- a/core/res/res/values/strings.xml
+++ b/core/res/res/values/strings.xml
@@ -1248,14 +1248,13 @@
         Malicious apps may use this to erase or modify your call log.</string>
 
     <!-- Title of the body sensors permission, listed so the user can decide whether to allow the application to access body sensor data. [CHAR LIMIT=80] -->
-    <string name="permlab_bodySensors">access body sensors (like heart rate monitors)
-    </string>
+    <string name="permlab_bodySensors">Access body sensor data, like heart rate, while in use</string>
     <!-- Description of the body sensors permission, listed so the user can decide whether to allow the application to access data from body sensors. [CHAR LIMIT=NONE] -->
-    <string name="permdesc_bodySensors" product="default">Access to data from body sensors such as heart rate, temperature, blood oxygen percentage, etc.</string>
+    <string name="permdesc_bodySensors" product="default">Allows the app to access body sensor data, such as heart rate, temperature, and blood oxygen percentage, while the app is in use.</string>
     <!-- Title of the background body sensors permission, listed so the user can decide whether to allow the application to access body sensor data in the background. [CHAR LIMIT=80] -->
-    <string name="permlab_bodySensors_background">access body sensors (like heart rate monitors) while in the background</string>
+    <string name="permlab_bodySensors_background">Access body sensor data, like heart rate, while in the background</string>
     <!-- Description of the background body sensors permission, listed so the user can decide whether to allow the application to access data from body sensors in the background. [CHAR LIMIT=NONE] -->
-    <string name="permdesc_bodySensors_background" product="default">Access to data from body sensors such as heart rate, temperature, blood oxygen percentage, etc. while in the background.</string>
+    <string name="permdesc_bodySensors_background" product="default">Allows the app to access body sensor data, such as heart rate, temperature, and blood oxygen percentage, while the app is in the background.</string>
 
     <!-- Title of an application permission, listed so the user can choose whether they want to allow the application to do this. -->
     <string name="permlab_readCalendar">Read calendar events and details</string>
@@ -1919,9 +1918,9 @@
     <string name="permdesc_readMediaVideo">Allows the app to read video files from your shared storage.</string>
 
     <!-- Title of an application permission, listed so the user can choose whether they want to allow the application to do this. "shared storage" refers to a storage space on the device that all apps with this permission can read from. [CHAR LIMIT=none] -->
-    <string name="permlab_readMediaImage">read image files from shared storage</string>
+    <string name="permlab_readMediaImages">read image files from shared storage</string>
     <!-- Description of an application permission, listed so the user can choose whether they want to allow the application to do this. "shared storage" refers to a storage space on the device that all apps with this permission can read from. [CHAR LIMIT=none] -->
-    <string name="permdesc_readMediaImage">Allows the app to read image files from your shared storage.</string>
+    <string name="permdesc_readMediaImages">Allows the app to read image files from your shared storage.</string>
 
     <!-- Title of an application permission, listed so the user can choose whether they want to allow the application to do this. "shared storage" refers to a storage space on the device that all apps with this permission can write to. [CHAR LIMIT=none] -->
     <string name="permlab_sdcardWrite">modify or delete the contents of your shared storage</string>
@@ -2415,7 +2414,7 @@
     <!-- On the unlock pattern screen, shown at the top of the unlock screen to tell the user what to do. Below this text is the place for theu ser to draw the pattern. -->
     <string name="lockscreen_pattern_instructions">Draw pattern to unlock</string>
     <!-- Button at the bottom of the unlock screen to make an emergency call or access other emergency assistance functions. -->
-    <string name="lockscreen_emergency_call">Emergency call</string>
+    <string name="lockscreen_emergency_call">Emergency</string>
     <!-- Button at the bottom of the unlock screen that lets the user return to a call -->
     <string name="lockscreen_return_to_call">Return to call</string>
     <!-- Shown to confirm that the user entered their lock pattern correctly. -->
@@ -5740,7 +5739,7 @@
 
     <!-- Content for the log access confirmation dialog. [CHAR LIMIT=NONE]-->
     <string name="log_access_confirmation_body">Device logs record what happens on your device. Apps can use these logs to find and fix issues.\n\nSome logs may contain sensitive info, so only allow apps you trust to access all device logs.
-        \n\nIf you don’t allow this app to access all device logs, it can still access its own logs, and your device manufacturer may still be able to access some logs or info on your device. Learn more
+        \n\nIf you don’t allow this app to access all device logs, it can still access its own logs and your device manufacturer may still be able to access some logs or info on your device. Learn more
     </string>
 
     <!-- Privacy notice do not show [CHAR LIMIT=20] -->
diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml
index 8f3abd6..1f0b22b 100644
--- a/core/res/res/values/symbols.xml
+++ b/core/res/res/values/symbols.xml
@@ -1325,6 +1325,7 @@
   <java-symbol type="dimen" name="lock_pattern_dot_line_width" />
   <java-symbol type="dimen" name="lock_pattern_dot_size" />
   <java-symbol type="dimen" name="lock_pattern_dot_size_activated" />
+  <java-symbol type="dimen" name="lock_pattern_dot_hit_factor" />
   <java-symbol type="dimen" name="lock_pattern_fade_away_gradient_width" />
   <java-symbol type="drawable" name="clock_dial" />
   <java-symbol type="drawable" name="clock_hand_hour" />
@@ -4769,5 +4770,6 @@
   <java-symbol type="integer" name="config_bg_current_drain_exempted_types" />
   <java-symbol type="bool" name="config_bg_current_drain_high_threshold_by_bg_location" />
   <java-symbol type="drawable" name="ic_swap_horiz" />
+  <java-symbol type="bool" name="config_notificationForceUserSetOnUpgrade" />
 
 </resources>
diff --git a/core/tests/batterystatstests/BatteryStatsViewer/src/com/android/frameworks/core/batterystatsviewer/BatteryConsumerData.java b/core/tests/batterystatstests/BatteryStatsViewer/src/com/android/frameworks/core/batterystatsviewer/BatteryConsumerData.java
index 6a53f68..19bb718 100644
--- a/core/tests/batterystatstests/BatteryStatsViewer/src/com/android/frameworks/core/batterystatsviewer/BatteryConsumerData.java
+++ b/core/tests/batterystatstests/BatteryStatsViewer/src/com/android/frameworks/core/batterystatsviewer/BatteryConsumerData.java
@@ -188,6 +188,9 @@
                 case BatteryConsumer.PROCESS_STATE_FOREGROUND_SERVICE:
                     label = "FGS";
                     break;
+                case BatteryConsumer.PROCESS_STATE_CACHED:
+                    label = "cached";
+                    break;
                 default:
                     continue;
             }
diff --git a/core/tests/batterystatstests/BatteryUsageStatsProtoTests/src/com/android/internal/os/BatteryUsageStatsPulledTest.java b/core/tests/batterystatstests/BatteryUsageStatsProtoTests/src/com/android/internal/os/BatteryUsageStatsPulledTest.java
index 23b12cf..fd08e3c 100644
--- a/core/tests/batterystatstests/BatteryUsageStatsProtoTests/src/com/android/internal/os/BatteryUsageStatsPulledTest.java
+++ b/core/tests/batterystatstests/BatteryUsageStatsProtoTests/src/com/android/internal/os/BatteryUsageStatsPulledTest.java
@@ -242,13 +242,17 @@
                 BatteryConsumer.PROCESS_STATE_BACKGROUND);
         final BatteryConsumer.Key keyFgs = uidBuilder.getKey(BatteryConsumer.POWER_COMPONENT_CPU,
                 BatteryConsumer.PROCESS_STATE_FOREGROUND_SERVICE);
+        final BatteryConsumer.Key keyCached = uidBuilder.getKey(BatteryConsumer.POWER_COMPONENT_CPU,
+                BatteryConsumer.PROCESS_STATE_CACHED);
 
         uidBuilder.setConsumedPower(keyFg, 9100, BatteryConsumer.POWER_MODEL_POWER_PROFILE)
                 .setUsageDurationMillis(keyFg, 8100)
                 .setConsumedPower(keyBg, 9200, BatteryConsumer.POWER_MODEL_MEASURED_ENERGY)
                 .setUsageDurationMillis(keyBg, 8200)
                 .setConsumedPower(keyFgs, 9300, BatteryConsumer.POWER_MODEL_MEASURED_ENERGY)
-                .setUsageDurationMillis(keyFgs, 8300);
+                .setUsageDurationMillis(keyFgs, 8300)
+                .setConsumedPower(keyCached, 9400, BatteryConsumer.POWER_MODEL_MEASURED_ENERGY)
+                .setUsageDurationMillis(keyFgs, 8400);
 
         builder.getOrCreateUidBatteryConsumerBuilder(batteryStatsUid1)
                 .setPackageWithHighestDrain("myPackage1")
diff --git a/core/tests/coretests/res/drawable/big_a.png b/core/tests/coretests/res/drawable/big_a.png
new file mode 100644
index 0000000..dc059a3
--- /dev/null
+++ b/core/tests/coretests/res/drawable/big_a.png
Binary files differ
diff --git a/core/tests/coretests/res/layout/caching_icon_view_test_max_size.xml b/core/tests/coretests/res/layout/caching_icon_view_test_max_size.xml
new file mode 100644
index 0000000..9a03446
--- /dev/null
+++ b/core/tests/coretests/res/layout/caching_icon_view_test_max_size.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2022 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+
+<com.android.internal.widget.CachingIconView
+          xmlns:android="http://schemas.android.com/apk/res/android"
+          android:id="@+id/caching_icon_view"
+          android:layout_width="120dp"
+          android:layout_height="120dp"
+          android:maxDrawableWidth="80dp"
+          android:maxDrawableHeight="80dp" />
diff --git a/core/tests/coretests/res/layout/caching_icon_view_test_no_max_size.xml b/core/tests/coretests/res/layout/caching_icon_view_test_no_max_size.xml
new file mode 100644
index 0000000..a213a97
--- /dev/null
+++ b/core/tests/coretests/res/layout/caching_icon_view_test_no_max_size.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2022 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+
+<com.android.internal.widget.CachingIconView
+          xmlns:android="http://schemas.android.com/apk/res/android"
+          android:id="@+id/caching_icon_view"
+          android:layout_width="120dp"
+          android:layout_height="120dp" />
diff --git a/core/tests/coretests/src/android/app/servertransaction/TransactionParcelTests.java b/core/tests/coretests/src/android/app/servertransaction/TransactionParcelTests.java
index 5c9044c..beadc446 100644
--- a/core/tests/coretests/src/android/app/servertransaction/TransactionParcelTests.java
+++ b/core/tests/coretests/src/android/app/servertransaction/TransactionParcelTests.java
@@ -424,6 +424,7 @@
 
         @Override
         public void bindApplication(String s, ApplicationInfo applicationInfo,
+                String sdkSandboxClientAppPackage,
                 ProviderInfoList list, ComponentName componentName, ProfilerInfo profilerInfo,
                 Bundle bundle, IInstrumentationWatcher iInstrumentationWatcher,
                 IUiAutomationConnection iUiAutomationConnection, int i, boolean b, boolean b1,
diff --git a/core/tests/coretests/src/android/graphics/drawable/IconTest.java b/core/tests/coretests/src/android/graphics/drawable/IconTest.java
index 2bdcc28..75390a2 100644
--- a/core/tests/coretests/src/android/graphics/drawable/IconTest.java
+++ b/core/tests/coretests/src/android/graphics/drawable/IconTest.java
@@ -180,6 +180,61 @@
         }
     }
 
+    /**
+     * Icon resource test that ensures we can load and draw non-bitmaps. (In this case,
+     * stat_sys_adb is assumed, and asserted, to be a vector drawable.)
+     */
+    @SmallTest
+    public void testWithStatSysAdbResource() throws Exception {
+        // establish reference bitmap
+        final float dp = getContext().getResources().getDisplayMetrics().density;
+        final int stat_sys_adb_width = (int) (24 * dp);
+        final int stat_sys_adb_height = (int) (24 * dp);
+
+        final Drawable stat_sys_adb = getContext()
+                .getDrawable(com.android.internal.R.drawable.stat_sys_adb);
+        if (!(stat_sys_adb instanceof VectorDrawable)) {
+            fail("stat_sys_adb is a " + stat_sys_adb.toString()
+                    + ", not a VectorDrawable; stat_sys_adb malformed");
+        }
+
+        if (stat_sys_adb.getIntrinsicWidth() != stat_sys_adb_width) {
+            fail("intrinsic width of stat_sys_adb is not 24dp; stat_sys_adb malformed");
+        }
+        if (stat_sys_adb.getIntrinsicHeight() != stat_sys_adb_height) {
+            fail("intrinsic height of stat_sys_adb is not 24dp; stat_sys_adb malformed");
+        }
+        final Bitmap referenceBitmap = Bitmap.createBitmap(
+                stat_sys_adb_width,
+                stat_sys_adb_height,
+                Bitmap.Config.ARGB_8888);
+        stat_sys_adb.setBounds(0, 0, stat_sys_adb_width, stat_sys_adb_height);
+        stat_sys_adb.draw(new Canvas(referenceBitmap));
+
+        final Icon im1 = Icon.createWithResource(getContext(),
+                com.android.internal.R.drawable.stat_sys_adb);
+        final Drawable draw1 = im1.loadDrawable(getContext());
+
+        assertEquals(stat_sys_adb.getIntrinsicWidth(), draw1.getIntrinsicWidth());
+        assertEquals(stat_sys_adb.getIntrinsicHeight(), draw1.getIntrinsicHeight());
+        assertEquals(im1.getResId(), com.android.internal.R.drawable.stat_sys_adb);
+
+        final Bitmap test1 = Bitmap.createBitmap(
+                draw1.getIntrinsicWidth(),
+                draw1.getIntrinsicHeight(),
+                Bitmap.Config.ARGB_8888);
+        draw1.setBounds(0, 0, test1.getWidth(), test1.getHeight());
+        draw1.draw(new Canvas(test1));
+
+        final File dir = getContext().getExternalFilesDir(null);
+        test1.compress(Bitmap.CompressFormat.PNG, 100,
+                new FileOutputStream(new File(dir, "testWithVectorDrawableResource-test.png")));
+        if (!equalBitmaps(referenceBitmap, test1)) {
+            findBitmapDifferences(referenceBitmap, test1);
+            fail("testWithFile: file1 differs, check " + dir);
+        }
+    }
+
     @SmallTest
     public void testWithFile() throws Exception {
         final Bitmap bit1 = ((BitmapDrawable) getContext().getDrawable(R.drawable.landscape))
diff --git a/core/tests/coretests/src/android/view/InsetsControllerTest.java b/core/tests/coretests/src/android/view/InsetsControllerTest.java
index 227a8657..c504f0c 100644
--- a/core/tests/coretests/src/android/view/InsetsControllerTest.java
+++ b/core/tests/coretests/src/android/view/InsetsControllerTest.java
@@ -30,6 +30,7 @@
 import static android.view.InsetsState.ITYPE_NAVIGATION_BAR;
 import static android.view.InsetsState.ITYPE_STATUS_BAR;
 import static android.view.InsetsState.LAST_TYPE;
+import static android.view.ViewRootImpl.CAPTION_ON_SHELL;
 import static android.view.WindowInsets.Type.ime;
 import static android.view.WindowInsets.Type.navigationBars;
 import static android.view.WindowInsets.Type.statusBars;
@@ -758,6 +759,11 @@
 
     @Test
     public void testCaptionInsetsStateAssemble() {
+        if (CAPTION_ON_SHELL) {
+            // For this case, the test is covered by WindowContainerInsetsSourceProviderTest, This
+            // test can be removed after the caption is moved to shell completely.
+            return;
+        }
         InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> {
             mController.onFrameChanged(new Rect(0, 0, 100, 300));
             final InsetsState state = new InsetsState(mController.getState(), true);
@@ -769,6 +775,7 @@
             assertEquals(captionFrame, currentState.peekSource(ITYPE_CAPTION_BAR).getFrame());
             assertTrue(currentState.equals(state, true /* excludingCaptionInsets*/,
                     true /* excludeInvisibleIme */));
+            // Test update to remove the caption bar
             mController.setCaptionInsetsHeight(0);
             mController.onStateChanged(state);
             // The caption bar source should not be there at all, because we don't add empty
@@ -779,6 +786,11 @@
 
     @Test
     public void testNotifyCaptionInsetsOnlyChange() {
+        if (CAPTION_ON_SHELL) {
+            // For this case, the test is covered by WindowContainerInsetsSourceProviderTest, This
+            // test can be removed after the caption is moved to shell completely.
+            return;
+        }
         InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> {
             final InsetsState state = new InsetsState(mController.getState(), true);
             reset(mTestHost);
diff --git a/core/tests/coretests/src/com/android/internal/os/BatteryStatsNoteTest.java b/core/tests/coretests/src/com/android/internal/os/BatteryStatsNoteTest.java
index f5cbffb..7ccb9d9 100644
--- a/core/tests/coretests/src/com/android/internal/os/BatteryStatsNoteTest.java
+++ b/core/tests/coretests/src/com/android/internal/os/BatteryStatsNoteTest.java
@@ -17,6 +17,8 @@
 package com.android.internal.os;
 
 import static android.os.BatteryStats.NUM_SCREEN_BRIGHTNESS_BINS;
+import static android.os.BatteryStats.POWER_DATA_UNAVAILABLE;
+import static android.os.BatteryStats.RADIO_ACCESS_TECHNOLOGY_COUNT;
 import static android.os.BatteryStats.RADIO_ACCESS_TECHNOLOGY_NR;
 import static android.os.BatteryStats.STATS_SINCE_CHARGED;
 import static android.os.BatteryStats.WAKE_TYPE_PARTIAL;
@@ -24,7 +26,10 @@
 import static com.android.internal.os.BatteryStatsImpl.ExternalStatsSync.UPDATE_CPU;
 import static com.android.internal.os.BatteryStatsImpl.ExternalStatsSync.UPDATE_DISPLAY;
 
+import static org.mockito.Mockito.mock;
+
 import android.app.ActivityManager;
+import android.app.usage.NetworkStatsManager;
 import android.os.BatteryStats;
 import android.os.BatteryStats.HistoryItem;
 import android.os.BatteryStats.Uid.Sensor;
@@ -34,6 +39,7 @@
 import android.telephony.Annotation;
 import android.telephony.CellSignalStrength;
 import android.telephony.DataConnectionRealTimeInfo;
+import android.telephony.ModemActivityInfo;
 import android.telephony.ServiceState;
 import android.telephony.TelephonyManager;
 import android.util.SparseIntArray;
@@ -48,6 +54,8 @@
 
 import junit.framework.TestCase;
 
+import org.mockito.Mock;
+
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
@@ -72,6 +80,13 @@
     private static final int ISOLATED_UID = UserHandle.getUid(0, ISOLATED_APP_ID);
     private static final WorkSource WS = new WorkSource(UID);
 
+    enum ModemState {
+        SLEEP, IDLE, RECEIVING, TRANSMITTING
+    }
+
+    @Mock
+    NetworkStatsManager mNetworkStatsManager;
+
     /**
      * Test BatteryStatsImpl.Uid.noteBluetoothScanResultLocked.
      */
@@ -1173,69 +1188,29 @@
     }
 
     @SmallTest
-    public void testGetPerStateActiveRadioDurationMs() {
+    public void testGetPerStateActiveRadioDurationMs_noModemActivity() {
         final MockClock clock = new MockClock(); // holds realtime and uptime in ms
         final MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clock);
-        final int ratCount = BatteryStats.RADIO_ACCESS_TECHNOLOGY_COUNT;
+        final int ratCount = RADIO_ACCESS_TECHNOLOGY_COUNT;
         final int frequencyCount = ServiceState.FREQUENCY_RANGE_MMWAVE + 1;
         final int txLevelCount = CellSignalStrength.getNumSignalStrengthLevels();
 
         final long[][][] expectedDurationsMs = new long[ratCount][frequencyCount][txLevelCount];
+        final long[][] expectedRxDurationsMs = new long[ratCount][frequencyCount];
+        final long[][][] expectedTxDurationsMs = new long[ratCount][frequencyCount][txLevelCount];
         for (int rat = 0; rat < ratCount; rat++) {
             for (int freq = 0; freq < frequencyCount; freq++) {
+                // Should have no RX data without Modem Activity Info
+                expectedRxDurationsMs[rat][freq] = POWER_DATA_UNAVAILABLE;
                 for (int txLvl = 0; txLvl < txLevelCount; txLvl++) {
                     expectedDurationsMs[rat][freq][txLvl] = 0;
+                    // Should have no TX data without Modem Activity Info
+                    expectedTxDurationsMs[rat][freq][txLvl] = POWER_DATA_UNAVAILABLE;
                 }
             }
         }
 
-        class ModemAndBatteryState {
-            public long currentTimeMs = 100;
-            public boolean onBattery = false;
-            public boolean modemActive = false;
-            @Annotation.NetworkType
-            public int currentNetworkDataType = TelephonyManager.NETWORK_TYPE_UNKNOWN;
-            @BatteryStats.RadioAccessTechnology
-            public int currentRat = BatteryStats.RADIO_ACCESS_TECHNOLOGY_OTHER;
-            @ServiceState.FrequencyRange
-            public int currentFrequencyRange = ServiceState.FREQUENCY_RANGE_UNKNOWN;
-            public SparseIntArray currentSignalStrengths = new SparseIntArray();
-
-            void setOnBattery(boolean onBattery) {
-                this.onBattery = onBattery;
-                bi.updateTimeBasesLocked(onBattery, Display.STATE_OFF, currentTimeMs * 1000,
-                        currentTimeMs * 1000);
-            }
-
-            void setModemActive(boolean active) {
-                modemActive = active;
-                final int state = active ? DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH
-                        : DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
-                bi.noteMobileRadioPowerStateLocked(state, currentTimeMs * 1000_000L, UID);
-            }
-
-            void setRatType(@Annotation.NetworkType int dataType,
-                    @BatteryStats.RadioAccessTechnology int rat) {
-                currentNetworkDataType = dataType;
-                currentRat = rat;
-                bi.notePhoneDataConnectionStateLocked(dataType, true, ServiceState.STATE_IN_SERVICE,
-                        currentFrequencyRange);
-            }
-
-            void setFrequencyRange(@ServiceState.FrequencyRange int frequency) {
-                currentFrequencyRange = frequency;
-                bi.notePhoneDataConnectionStateLocked(currentNetworkDataType, true,
-                        ServiceState.STATE_IN_SERVICE, frequency);
-            }
-
-            void setSignalStrength(@BatteryStats.RadioAccessTechnology int rat, int strength) {
-                currentSignalStrengths.put(rat, strength);
-                final int size = currentSignalStrengths.size();
-                final int newestGenSignalStrength = currentSignalStrengths.valueAt(size - 1);
-                bi.notePhoneSignalStrengthLocked(newestGenSignalStrength, currentSignalStrengths);
-            }
-        }
-        final ModemAndBatteryState state = new ModemAndBatteryState();
+        final ModemAndBatteryState state = new ModemAndBatteryState(bi, null);
 
         IntConsumer incrementTime = inc -> {
             state.currentTimeMs += inc;
@@ -1253,6 +1228,7 @@
             expectedDurationsMs[currentRat][currentFrequencyRange][currentSignalStrength] += inc;
         };
 
+
         state.setOnBattery(false);
         state.setModemActive(false);
         state.setRatType(TelephonyManager.NETWORK_TYPE_UNKNOWN,
@@ -1260,95 +1236,367 @@
         state.setFrequencyRange(ServiceState.FREQUENCY_RANGE_UNKNOWN);
         state.setSignalStrength(BatteryStats.RADIO_ACCESS_TECHNOLOGY_OTHER,
                 CellSignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN);
-        checkPerStateActiveRadioDurations(expectedDurationsMs, bi, state.currentTimeMs);
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
 
         // While not on battery, the timers should not increase.
         state.setModemActive(true);
         incrementTime.accept(100);
-        checkPerStateActiveRadioDurations(expectedDurationsMs, bi, state.currentTimeMs);
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
 
         state.setRatType(TelephonyManager.NETWORK_TYPE_NR, BatteryStats.RADIO_ACCESS_TECHNOLOGY_NR);
         incrementTime.accept(200);
-        checkPerStateActiveRadioDurations(expectedDurationsMs, bi, state.currentTimeMs);
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
 
         state.setSignalStrength(BatteryStats.RADIO_ACCESS_TECHNOLOGY_NR,
                 CellSignalStrength.SIGNAL_STRENGTH_GOOD);
         incrementTime.accept(500);
-        checkPerStateActiveRadioDurations(expectedDurationsMs, bi, state.currentTimeMs);
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
 
         state.setFrequencyRange(ServiceState.FREQUENCY_RANGE_MMWAVE);
         incrementTime.accept(300);
-        checkPerStateActiveRadioDurations(expectedDurationsMs, bi, state.currentTimeMs);
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
 
         state.setRatType(TelephonyManager.NETWORK_TYPE_LTE,
                 BatteryStats.RADIO_ACCESS_TECHNOLOGY_LTE);
         incrementTime.accept(400);
-        checkPerStateActiveRadioDurations(expectedDurationsMs, bi, state.currentTimeMs);
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
 
         state.setSignalStrength(BatteryStats.RADIO_ACCESS_TECHNOLOGY_LTE,
                 CellSignalStrength.SIGNAL_STRENGTH_MODERATE);
         incrementTime.accept(500);
-        checkPerStateActiveRadioDurations(expectedDurationsMs, bi, state.currentTimeMs);
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
 
         // When set on battery, currently active state (RAT:LTE, Signal Strength:Moderate) should
         // start counting up.
         state.setOnBattery(true);
         incrementTime.accept(600);
-        checkPerStateActiveRadioDurations(expectedDurationsMs, bi, state.currentTimeMs);
-
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
         // Changing LTE signal strength should be tracked.
         state.setSignalStrength(BatteryStats.RADIO_ACCESS_TECHNOLOGY_LTE,
                 CellSignalStrength.SIGNAL_STRENGTH_POOR);
         incrementTime.accept(700);
-        checkPerStateActiveRadioDurations(expectedDurationsMs, bi, state.currentTimeMs);
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
 
         state.setSignalStrength(BatteryStats.RADIO_ACCESS_TECHNOLOGY_LTE,
                 CellSignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN);
         incrementTime.accept(800);
-        checkPerStateActiveRadioDurations(expectedDurationsMs, bi, state.currentTimeMs);
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
 
         state.setSignalStrength(BatteryStats.RADIO_ACCESS_TECHNOLOGY_LTE,
                 CellSignalStrength.SIGNAL_STRENGTH_GOOD);
         incrementTime.accept(900);
-        checkPerStateActiveRadioDurations(expectedDurationsMs, bi, state.currentTimeMs);
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
 
         state.setSignalStrength(BatteryStats.RADIO_ACCESS_TECHNOLOGY_LTE,
                 CellSignalStrength.SIGNAL_STRENGTH_GREAT);
         incrementTime.accept(1000);
-        checkPerStateActiveRadioDurations(expectedDurationsMs, bi, state.currentTimeMs);
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
 
         // Change in the signal strength of nonactive RAT should not affect anything.
         state.setSignalStrength(BatteryStats.RADIO_ACCESS_TECHNOLOGY_OTHER,
                 CellSignalStrength.SIGNAL_STRENGTH_POOR);
         incrementTime.accept(1100);
-        checkPerStateActiveRadioDurations(expectedDurationsMs, bi, state.currentTimeMs);
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
 
         // Changing to OTHER Rat should start tracking the poor signal strength.
         state.setRatType(TelephonyManager.NETWORK_TYPE_CDMA,
                 BatteryStats.RADIO_ACCESS_TECHNOLOGY_OTHER);
         incrementTime.accept(1200);
-        checkPerStateActiveRadioDurations(expectedDurationsMs, bi, state.currentTimeMs);
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
 
         // Noting frequency change should not affect non NR Rat.
         state.setFrequencyRange(ServiceState.FREQUENCY_RANGE_HIGH);
         incrementTime.accept(1300);
-        checkPerStateActiveRadioDurations(expectedDurationsMs, bi, state.currentTimeMs);
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
 
         // Now the NR Rat, HIGH frequency range, good signal strength should start counting.
         state.setRatType(TelephonyManager.NETWORK_TYPE_NR, BatteryStats.RADIO_ACCESS_TECHNOLOGY_NR);
         incrementTime.accept(1400);
-        checkPerStateActiveRadioDurations(expectedDurationsMs, bi, state.currentTimeMs);
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
 
         // Noting frequency change should not affect non NR Rat.
         state.setFrequencyRange(ServiceState.FREQUENCY_RANGE_LOW);
         incrementTime.accept(1500);
-        checkPerStateActiveRadioDurations(expectedDurationsMs, bi, state.currentTimeMs);
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
 
         // Modem no longer active, should not be tracking any more.
         state.setModemActive(false);
         incrementTime.accept(1500);
-        checkPerStateActiveRadioDurations(expectedDurationsMs, bi, state.currentTimeMs);
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
+    }
 
+    @SmallTest
+    public void testGetPerStateActiveRadioDurationMs_withModemActivity() {
+        final MockClock clock = new MockClock(); // holds realtime and uptime in ms
+        final MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clock);
+        bi.setPowerProfile(mock(PowerProfile.class));
+        final int ratCount = RADIO_ACCESS_TECHNOLOGY_COUNT;
+        final int frequencyCount = ServiceState.FREQUENCY_RANGE_MMWAVE + 1;
+        final int txLevelCount = CellSignalStrength.getNumSignalStrengthLevels();
+
+        final long[][][] expectedDurationsMs = new long[ratCount][frequencyCount][txLevelCount];
+        final long[][] expectedRxDurationsMs = new long[ratCount][frequencyCount];
+        final long[][][] expectedTxDurationsMs = new long[ratCount][frequencyCount][txLevelCount];
+        for (int rat = 0; rat < ratCount; rat++) {
+            for (int freq = 0; freq < frequencyCount; freq++) {
+                if (rat != RADIO_ACCESS_TECHNOLOGY_NR
+                        && freq != ServiceState.FREQUENCY_RANGE_UNKNOWN) {
+                    // Only the NR RAT should have per frequency data.
+                    expectedRxDurationsMs[rat][freq] = POWER_DATA_UNAVAILABLE;
+                } else {
+                    expectedRxDurationsMs[rat][freq] = 0;
+                }
+                expectedRxDurationsMs[rat][freq] = POWER_DATA_UNAVAILABLE;
+
+                for (int txLvl = 0; txLvl < txLevelCount; txLvl++) {
+                    expectedDurationsMs[rat][freq][txLvl] = 0;
+
+                    if (rat != RADIO_ACCESS_TECHNOLOGY_NR
+                            && freq != ServiceState.FREQUENCY_RANGE_UNKNOWN) {
+                        // Only the NR RAT should have per frequency data.
+                        expectedTxDurationsMs[rat][freq][txLvl] = POWER_DATA_UNAVAILABLE;
+                    } else {
+                        expectedTxDurationsMs[rat][freq][txLvl] = 0;
+                    }
+                    expectedTxDurationsMs[rat][freq][txLvl] = POWER_DATA_UNAVAILABLE;
+                }
+            }
+        }
+
+        final ModemActivityInfo mai = new ModemActivityInfo(0L, 0L, 0L, new int[txLevelCount], 0L);
+        final ModemAndBatteryState state = new ModemAndBatteryState(bi, mai);
+
+        IntConsumer incrementTime = inc -> {
+            state.currentTimeMs += inc;
+            clock.realtime = clock.uptime = state.currentTimeMs;
+
+            // If the device is not on battery, no timers should increment.
+            if (!state.onBattery) return;
+            // If the modem is not active, no timers should increment.
+            if (!state.modemActive) return;
+
+            final int currRat = state.currentRat;
+            final int currFreqRange =
+                    currRat == RADIO_ACCESS_TECHNOLOGY_NR ? state.currentFrequencyRange : 0;
+            int currSignalStrength = state.currentSignalStrengths.get(currRat);
+
+            expectedDurationsMs[currRat][currFreqRange][currSignalStrength] += inc;
+
+            // Evaluate the HAL provided time in states.
+            switch (state.modemState) {
+                case SLEEP:
+                    long sleepMs = state.modemActivityInfo.getSleepTimeMillis();
+                    state.modemActivityInfo.setSleepTimeMillis(sleepMs + inc);
+                    break;
+                case IDLE:
+                    long idleMs = state.modemActivityInfo.getIdleTimeMillis();
+                    state.modemActivityInfo.setIdleTimeMillis(idleMs + inc);
+                    break;
+                case RECEIVING:
+                    long rxMs = state.modemActivityInfo.getReceiveTimeMillis();
+                    state.modemActivityInfo.setReceiveTimeMillis(rxMs + inc);
+                    expectedRxDurationsMs[currRat][currFreqRange] += inc;
+                    break;
+                case TRANSMITTING:
+                    int[] txMs = state.modemActivityInfo.getTransmitTimeMillis();
+                    txMs[currSignalStrength] += inc;
+                    state.modemActivityInfo.setTransmitTimeMillis(txMs);
+                    expectedTxDurationsMs[currRat][currFreqRange][currSignalStrength] += inc;
+                    break;
+            }
+        };
+
+        state.setOnBattery(false);
+        state.setModemActive(false);
+        state.setRatType(TelephonyManager.NETWORK_TYPE_UNKNOWN,
+                BatteryStats.RADIO_ACCESS_TECHNOLOGY_OTHER);
+        state.setFrequencyRange(ServiceState.FREQUENCY_RANGE_UNKNOWN);
+        state.setSignalStrength(BatteryStats.RADIO_ACCESS_TECHNOLOGY_OTHER,
+                CellSignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN);
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
+
+        // While not on battery, the timers should not increase.
+        state.setModemActive(true);
+        incrementTime.accept(100);
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
+
+        state.setRatType(TelephonyManager.NETWORK_TYPE_NR, BatteryStats.RADIO_ACCESS_TECHNOLOGY_NR);
+        incrementTime.accept(200);
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
+
+        state.setSignalStrength(BatteryStats.RADIO_ACCESS_TECHNOLOGY_NR,
+                CellSignalStrength.SIGNAL_STRENGTH_GOOD);
+        incrementTime.accept(500);
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
+
+        state.setFrequencyRange(ServiceState.FREQUENCY_RANGE_MMWAVE);
+        incrementTime.accept(300);
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
+
+        state.setRatType(TelephonyManager.NETWORK_TYPE_LTE,
+                BatteryStats.RADIO_ACCESS_TECHNOLOGY_LTE);
+        incrementTime.accept(400);
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
+
+        state.setSignalStrength(BatteryStats.RADIO_ACCESS_TECHNOLOGY_LTE,
+                CellSignalStrength.SIGNAL_STRENGTH_MODERATE);
+        incrementTime.accept(500);
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
+
+        // Data will now be available.
+        for (int rat = 0; rat < ratCount; rat++) {
+            for (int freq = 0; freq < frequencyCount; freq++) {
+                if (rat == RADIO_ACCESS_TECHNOLOGY_NR
+                        || freq == ServiceState.FREQUENCY_RANGE_UNKNOWN) {
+                    // Only the NR RAT should have per frequency data.
+                    expectedRxDurationsMs[rat][freq] = 0;
+                }
+                for (int txLvl = 0; txLvl < txLevelCount; txLvl++) {
+                    if (rat == RADIO_ACCESS_TECHNOLOGY_NR
+                            || freq == ServiceState.FREQUENCY_RANGE_UNKNOWN) {
+                        // Only the NR RAT should have per frequency data.
+                        expectedTxDurationsMs[rat][freq][txLvl] = 0;
+                    }
+                }
+            }
+        }
+
+        // When set on battery, currently active state (RAT:LTE, Signal Strength:Moderate) should
+        // start counting up.
+        state.setOnBattery(true);
+        incrementTime.accept(300);
+        state.setModemState(ModemState.RECEIVING);
+        incrementTime.accept(500);
+        state.setModemState(ModemState.TRANSMITTING);
+        incrementTime.accept(600);
+        state.noteModemControllerActivity();
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
+        // Changing LTE signal strength should be tracked.
+        state.setSignalStrength(BatteryStats.RADIO_ACCESS_TECHNOLOGY_LTE,
+                CellSignalStrength.SIGNAL_STRENGTH_POOR);
+        incrementTime.accept(300);
+        state.setModemState(ModemState.SLEEP);
+        incrementTime.accept(1000);
+        state.setModemState(ModemState.RECEIVING);
+        incrementTime.accept(700);
+        state.noteModemControllerActivity();
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
+
+        state.setSignalStrength(BatteryStats.RADIO_ACCESS_TECHNOLOGY_LTE,
+                CellSignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN);
+        incrementTime.accept(800);
+        state.setModemState(ModemState.TRANSMITTING);
+        incrementTime.accept(222);
+        state.setModemState(ModemState.IDLE);
+        incrementTime.accept(111);
+        state.setModemState(ModemState.RECEIVING);
+        incrementTime.accept(7777);
+        state.noteModemControllerActivity();
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
+
+        state.setSignalStrength(BatteryStats.RADIO_ACCESS_TECHNOLOGY_LTE,
+                CellSignalStrength.SIGNAL_STRENGTH_GOOD);
+        incrementTime.accept(88);
+        state.setModemState(ModemState.TRANSMITTING);
+        incrementTime.accept(900);
+        state.noteModemControllerActivity();
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
+
+        state.setSignalStrength(BatteryStats.RADIO_ACCESS_TECHNOLOGY_LTE,
+                CellSignalStrength.SIGNAL_STRENGTH_GREAT);
+        incrementTime.accept(123);
+        state.setModemState(ModemState.RECEIVING);
+        incrementTime.accept(333);
+        state.setModemState(ModemState.TRANSMITTING);
+        incrementTime.accept(1000);
+        state.setModemState(ModemState.RECEIVING);
+        incrementTime.accept(555);
+        state.noteModemControllerActivity();
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
+
+        // Change in the signal strength of nonactive RAT should not affect anything.
+        state.setSignalStrength(BatteryStats.RADIO_ACCESS_TECHNOLOGY_OTHER,
+                CellSignalStrength.SIGNAL_STRENGTH_POOR);
+        incrementTime.accept(631);
+        state.setModemState(ModemState.TRANSMITTING);
+        incrementTime.accept(321);
+        state.setModemState(ModemState.RECEIVING);
+        incrementTime.accept(99);
+        state.noteModemControllerActivity();
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
+
+        // Changing to OTHER Rat should start tracking the poor signal strength.
+        state.setRatType(TelephonyManager.NETWORK_TYPE_CDMA,
+                BatteryStats.RADIO_ACCESS_TECHNOLOGY_OTHER);
+        incrementTime.accept(1200);
+        state.noteModemControllerActivity();
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
+
+        // Noting frequency change should not affect non NR Rat.
+        state.setFrequencyRange(ServiceState.FREQUENCY_RANGE_HIGH);
+        incrementTime.accept(444);
+        state.setModemState(ModemState.TRANSMITTING);
+        incrementTime.accept(1300);
+        state.noteModemControllerActivity();
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
+
+        // Now the NR Rat, HIGH frequency range, good signal strength should start counting.
+        state.setRatType(TelephonyManager.NETWORK_TYPE_NR, BatteryStats.RADIO_ACCESS_TECHNOLOGY_NR);
+        incrementTime.accept(1400);
+        state.noteModemControllerActivity();
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
+
+        // Frequency changed to low.
+        state.setFrequencyRange(ServiceState.FREQUENCY_RANGE_LOW);
+        incrementTime.accept(852);
+        state.setModemState(ModemState.RECEIVING);
+        incrementTime.accept(157);
+        state.setModemState(ModemState.TRANSMITTING);
+        incrementTime.accept(1500);
+        state.noteModemControllerActivity();
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
+
+        // Modem no longer active, should not be tracking any more.
+        state.setModemActive(false);
+        incrementTime.accept(1500);
+        state.noteModemControllerActivity();
+        checkPerStateActiveRadioDurations(expectedDurationsMs, expectedRxDurationsMs,
+                expectedTxDurationsMs, bi, state.currentTimeMs);
     }
 
     private void setFgState(int uid, boolean fgOn, MockBatteryStatsImpl bi) {
@@ -1426,28 +1674,124 @@
     }
 
     private void checkPerStateActiveRadioDurations(long[][][] expectedDurationsMs,
+            long[][] expectedRxDurationsMs, long[][][] expectedTxDurationsMs,
             BatteryStatsImpl bi, long currentTimeMs) {
         for (int rat = 0; rat < expectedDurationsMs.length; rat++) {
             final long[][] expectedRatDurationsMs = expectedDurationsMs[rat];
             for (int freq = 0; freq < expectedRatDurationsMs.length; freq++) {
+                final long expectedRxDurationMs = expectedRxDurationsMs[rat][freq];
+
+                // Build a verbose fail message, just in case.
+                final StringBuilder rxFailSb = new StringBuilder();
+                rxFailSb.append("Wrong time in Rx state for RAT:");
+                rxFailSb.append(BatteryStats.RADIO_ACCESS_TECHNOLOGY_NAMES[rat]);
+                rxFailSb.append(", frequency:");
+                rxFailSb.append(ServiceState.frequencyRangeToString(freq));
+                assertEquals(rxFailSb.toString(), expectedRxDurationMs,
+                        bi.getActiveRxRadioDurationMs(rat, freq, currentTimeMs));
+
                 final long[] expectedFreqDurationsMs = expectedRatDurationsMs[freq];
                 for (int strength = 0; strength < expectedFreqDurationsMs.length; strength++) {
                     final long expectedSignalStrengthDurationMs = expectedFreqDurationsMs[strength];
+                    final long expectedTxDurationMs = expectedTxDurationsMs[rat][freq][strength];
                     final long actualDurationMs = bi.getActiveRadioDurationMs(rat, freq,
                             strength, currentTimeMs);
 
-                    // Build a verbose fail message, just in case.
-                    final StringBuilder sb = new StringBuilder();
-                    sb.append("Wrong time in state for RAT:");
-                    sb.append(BatteryStats.RADIO_ACCESS_TECHNOLOGY_NAMES[rat]);
-                    sb.append(", frequency:");
-                    sb.append(ServiceState.frequencyRangeToString(freq));
-                    sb.append(", strength:");
-                    sb.append(strength);
+                    final StringBuilder failSb = new StringBuilder();
+                    failSb.append("Wrong time in state for RAT:");
+                    failSb.append(BatteryStats.RADIO_ACCESS_TECHNOLOGY_NAMES[rat]);
+                    failSb.append(", frequency:");
+                    failSb.append(ServiceState.frequencyRangeToString(freq));
+                    failSb.append(", strength:");
+                    failSb.append(strength);
+                    assertEquals(failSb.toString(), expectedSignalStrengthDurationMs,
+                            actualDurationMs);
 
-                    assertEquals(sb.toString(), expectedSignalStrengthDurationMs, actualDurationMs);
+                    final StringBuilder txFailSb = new StringBuilder();
+                    txFailSb.append("Wrong time in Tx state for RAT:");
+                    txFailSb.append(BatteryStats.RADIO_ACCESS_TECHNOLOGY_NAMES[rat]);
+                    txFailSb.append(", frequency:");
+                    txFailSb.append(ServiceState.frequencyRangeToString(freq));
+                    txFailSb.append(", strength:");
+                    txFailSb.append(strength);
+                    assertEquals(txFailSb.toString(), expectedTxDurationMs,
+                            bi.getActiveTxRadioDurationMs(rat, freq, strength, currentTimeMs));
                 }
             }
         }
     }
+
+    private class ModemAndBatteryState {
+        public long currentTimeMs = 100;
+        public boolean onBattery = false;
+        public boolean modemActive = false;
+        @Annotation.NetworkType
+        public int currentNetworkDataType = TelephonyManager.NETWORK_TYPE_UNKNOWN;
+        @BatteryStats.RadioAccessTechnology
+        public int currentRat = BatteryStats.RADIO_ACCESS_TECHNOLOGY_OTHER;
+        @ServiceState.FrequencyRange
+        public int currentFrequencyRange = ServiceState.FREQUENCY_RANGE_UNKNOWN;
+        public SparseIntArray currentSignalStrengths = new SparseIntArray();
+        public ModemState modemState = ModemState.SLEEP;
+        public ModemActivityInfo modemActivityInfo;
+
+        private final MockBatteryStatsImpl mBsi;
+
+        ModemAndBatteryState(MockBatteryStatsImpl bsi, ModemActivityInfo mai) {
+            mBsi = bsi;
+            modemActivityInfo = mai;
+        }
+
+        void setOnBattery(boolean onBattery) {
+            this.onBattery = onBattery;
+            mBsi.updateTimeBasesLocked(onBattery, Display.STATE_OFF, currentTimeMs * 1000,
+                    currentTimeMs * 1000);
+            mBsi.setOnBatteryInternal(onBattery);
+            noteModemControllerActivity();
+        }
+
+        void setModemActive(boolean active) {
+            modemActive = active;
+            final int state = active ? DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH
+                    : DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
+            mBsi.noteMobileRadioPowerStateLocked(state, currentTimeMs * 1000_000L, UID);
+            noteModemControllerActivity();
+        }
+
+        void setRatType(@Annotation.NetworkType int dataType,
+                @BatteryStats.RadioAccessTechnology int rat) {
+            currentNetworkDataType = dataType;
+            currentRat = rat;
+            mBsi.notePhoneDataConnectionStateLocked(dataType, true, ServiceState.STATE_IN_SERVICE,
+                    currentFrequencyRange);
+        }
+
+        void setFrequencyRange(@ServiceState.FrequencyRange int frequency) {
+            currentFrequencyRange = frequency;
+            mBsi.notePhoneDataConnectionStateLocked(currentNetworkDataType, true,
+                    ServiceState.STATE_IN_SERVICE, frequency);
+        }
+
+        void setSignalStrength(@BatteryStats.RadioAccessTechnology int rat, int strength) {
+            currentSignalStrengths.put(rat, strength);
+            final int size = currentSignalStrengths.size();
+            final int newestGenSignalStrength = currentSignalStrengths.valueAt(size - 1);
+            mBsi.notePhoneSignalStrengthLocked(newestGenSignalStrength, currentSignalStrengths);
+        }
+
+        void setModemState(ModemState state) {
+            modemState = state;
+        }
+
+        void noteModemControllerActivity() {
+            if (modemActivityInfo == null) return;
+            modemActivityInfo.setTimestamp(currentTimeMs);
+            ModemActivityInfo copy = new ModemActivityInfo(modemActivityInfo.getTimestampMillis(),
+                    modemActivityInfo.getSleepTimeMillis(), modemActivityInfo.getIdleTimeMillis(),
+                    modemActivityInfo.getTransmitTimeMillis().clone(),
+                    modemActivityInfo.getReceiveTimeMillis());
+            mBsi.noteModemControllerActivity(copy, POWER_DATA_UNAVAILABLE,
+                    currentTimeMs, currentTimeMs, mNetworkStatsManager);
+        }
+    }
 }
diff --git a/core/tests/coretests/src/com/android/internal/os/BatteryStatsSensorTest.java b/core/tests/coretests/src/com/android/internal/os/BatteryStatsSensorTest.java
index 0e394c1..bfb3449 100644
--- a/core/tests/coretests/src/com/android/internal/os/BatteryStatsSensorTest.java
+++ b/core/tests/coretests/src/com/android/internal/os/BatteryStatsSensorTest.java
@@ -30,6 +30,7 @@
 public class BatteryStatsSensorTest extends TestCase {
 
     private static final int UID = 10500;
+    private static final int UID_2 = 10501; // second uid for testing pool usage
     private static final int SENSOR_ID = -10000;
 
     @SmallTest
@@ -239,7 +240,6 @@
 
     @SmallTest
     public void testPooledBackgroundUsage() throws Exception {
-        final int UID_2 = 20000; // second uid for testing pool usage
         final MockClock clocks = new MockClock();
         MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
         bi.mForceOnBattery = true;
diff --git a/core/tests/coretests/src/com/android/internal/os/BatteryUsageStatsTest.java b/core/tests/coretests/src/com/android/internal/os/BatteryUsageStatsTest.java
index 5adc9bd..483224c 100644
--- a/core/tests/coretests/src/com/android/internal/os/BatteryUsageStatsTest.java
+++ b/core/tests/coretests/src/com/android/internal/os/BatteryUsageStatsTest.java
@@ -20,6 +20,7 @@
 import static android.os.BatteryConsumer.POWER_MODEL_MEASURED_ENERGY;
 import static android.os.BatteryConsumer.POWER_MODEL_UNDEFINED;
 import static android.os.BatteryConsumer.PROCESS_STATE_BACKGROUND;
+import static android.os.BatteryConsumer.PROCESS_STATE_CACHED;
 import static android.os.BatteryConsumer.PROCESS_STATE_FOREGROUND;
 import static android.os.BatteryConsumer.PROCESS_STATE_FOREGROUND_SERVICE;
 
@@ -83,7 +84,7 @@
         final Parcel parcel = Parcel.obtain();
         parcel.writeParcelable(outBatteryUsageStats, 0);
 
-        assertThat(parcel.dataSize()).isLessThan(7000);
+        assertThat(parcel.dataSize()).isLessThan(8000);
 
         parcel.setDataPosition(0);
 
@@ -155,10 +156,11 @@
         assertThat(dump).contains("cpu(fg): 2333 apps: 1333 duration: 3s 332ms");
         assertThat(dump).contains("cpu(bg): 2444 apps: 1444 duration: 4s 442ms");
         assertThat(dump).contains("cpu(fgs): 2555 apps: 1555 duration: 5s 552ms");
+        assertThat(dump).contains("cpu(cached): 123 apps: 123 duration: 456ms");
         assertThat(dump).contains("FOO: 20200 apps: 10200 duration: 20s 400ms");
-        assertThat(dump).contains("UID 271: 1200 fg: 1777 bg: 1888 fgs: 1999 ( screen=300 "
-                + "cpu=400 (600ms) cpu:fg=1777 (7s 771ms) cpu:bg=1888 (8s 881ms) "
-                + "cpu:fgs=1999 (9s 991ms) FOO=500 )");
+        assertThat(dump).contains("UID 271: 1200 fg: 1777 bg: 1888 fgs: 1999 cached: 123 "
+                + "( screen=300 cpu=400 (600ms) cpu:fg=1777 (7s 771ms) cpu:bg=1888 (8s 881ms) "
+                + "cpu:fgs=1999 (9s 991ms) cpu:cached=123 (456ms) FOO=500 )");
         assertThat(dump).contains("User 42: 30.0 ( cpu=10.0 (30ms) FOO=20.0 )");
     }
 
@@ -193,13 +195,15 @@
                         5321, 7432, 423, BatteryConsumer.POWER_MODEL_POWER_PROFILE, 745,
                         POWER_MODEL_UNDEFINED,
                         956, 1167, 1478,
-                        true, 3554, 3776, 3998, 3554, 15542, 3776, 17762, 3998, 19982);
+                        true, 3554, 3776, 3998, 444, 3554, 15542, 3776, 17762, 3998, 19982,
+                        444, 1110);
             } else if (uidBatteryConsumer.getUid() == APP_UID2) {
                 assertUidBatteryConsumer(uidBatteryConsumer, 1332, "bar",
                         1111, 2222, 333, BatteryConsumer.POWER_MODEL_POWER_PROFILE, 444,
                         BatteryConsumer.POWER_MODEL_POWER_PROFILE,
                         555, 666, 777,
-                        true, 1777, 1888, 1999, 1777, 7771, 1888, 8881, 1999, 9991);
+                        true, 1777, 1888, 1999, 321, 1777, 7771, 1888, 8881, 1999, 9991,
+                        321, 654);
             } else {
                 fail("Unexpected UID " + uidBatteryConsumer.getUid());
             }
@@ -267,17 +271,17 @@
                 1000, 2000,
                 300, BatteryConsumer.POWER_MODEL_POWER_PROFILE, 400,
                 BatteryConsumer.POWER_MODEL_POWER_PROFILE, 500, 600, 800,
-                1777, 7771, 1888, 8881, 1999, 9991);
+                1777, 7771, 1888, 8881, 1999, 9991, 123, 456);
 
         addAggregateBatteryConsumer(builder,
                 BatteryUsageStats.AGGREGATE_BATTERY_CONSUMER_SCOPE_ALL_APPS, 0,
                 10100, 10200, 10300, 10400,
-                1333, 3331, 1444, 4441, 1555, 5551);
+                1333, 3331, 1444, 4441, 1555, 5551, 123, 456);
 
         addAggregateBatteryConsumer(builder,
                 BatteryUsageStats.AGGREGATE_BATTERY_CONSUMER_SCOPE_DEVICE, 30000,
                 20100, 20200, 20300, 20400,
-                2333, 3332, 2444, 4442, 2555, 5552);
+                2333, 3332, 2444, 4442, 2555, 5552, 123, 456);
 
         if (includeUserBatteryConsumer) {
             builder.getOrCreateUserBatteryConsumerBuilder(USER_ID)
@@ -310,23 +314,23 @@
                 4321, 5432,
                 123, BatteryConsumer.POWER_MODEL_POWER_PROFILE, 345, POWER_MODEL_MEASURED_ENERGY,
                 456, 567, 678,
-                1777, 7771, 1888, 8881, 1999, 9991);
+                1777, 7771, 1888, 8881, 1999, 9991, 321, 654);
 
         addUidBatteryConsumer(builder, batteryStats, APP_UID2, "bar",
                 1111, 2222,
                 333, BatteryConsumer.POWER_MODEL_POWER_PROFILE, 444,
                 BatteryConsumer.POWER_MODEL_POWER_PROFILE, 555, 666, 777,
-                1777, 7771, 1888, 8881, 1999, 9991);
+                1777, 7771, 1888, 8881, 1999, 9991, 321, 654);
 
         addAggregateBatteryConsumer(builder,
                 BatteryUsageStats.AGGREGATE_BATTERY_CONSUMER_SCOPE_ALL_APPS, 0,
                 10123, 10234, 10345, 10456,
-                4333, 3334, 5444, 4445, 6555, 5556);
+                4333, 3334, 5444, 4445, 6555, 5556, 321, 654);
 
         addAggregateBatteryConsumer(builder,
                 BatteryUsageStats.AGGREGATE_BATTERY_CONSUMER_SCOPE_DEVICE, 12345,
                 20111, 20222, 20333, 20444,
-                7333, 3337, 8444, 4448, 9555, 5559);
+                7333, 3337, 8444, 4448, 9555, 5559, 123, 456);
 
         return builder;
     }
@@ -337,7 +341,7 @@
             int screenPowerModel, double cpuPower, int cpuPowerModel, double customComponentPower,
             int cpuDuration, int customComponentDuration, double cpuPowerForeground,
             int cpuDurationForeground, double cpuPowerBackground, int cpuDurationBackground,
-            double cpuPowerFgs, int cpuDurationFgs) {
+            double cpuPowerFgs, int cpuDurationFgs, double cpuPowerCached, long cpuDurationCached) {
         final BatteryStatsImpl.Uid batteryStatsUid = batteryStats.getUidStatsLocked(uid);
         final UidBatteryConsumer.Builder uidBuilder =
                 builder.getOrCreateUidBatteryConsumerBuilder(batteryStatsUid);
@@ -365,6 +369,9 @@
             final BatteryConsumer.Key cpuFgsKey = uidBuilder.getKey(
                     BatteryConsumer.POWER_COMPONENT_CPU,
                     BatteryConsumer.PROCESS_STATE_FOREGROUND_SERVICE);
+            final BatteryConsumer.Key cachedKey = uidBuilder.getKey(
+                    BatteryConsumer.POWER_COMPONENT_CPU,
+                    BatteryConsumer.PROCESS_STATE_CACHED);
             uidBuilder
                     .setConsumedPower(cpuFgKey, cpuPowerForeground,
                             BatteryConsumer.POWER_MODEL_POWER_PROFILE)
@@ -374,7 +381,10 @@
                     .setUsageDurationMillis(cpuBgKey, cpuDurationBackground)
                     .setConsumedPower(cpuFgsKey, cpuPowerFgs,
                             BatteryConsumer.POWER_MODEL_POWER_PROFILE)
-                    .setUsageDurationMillis(cpuFgsKey, cpuDurationFgs);
+                    .setUsageDurationMillis(cpuFgsKey, cpuDurationFgs)
+                    .setConsumedPower(cachedKey, cpuPowerCached,
+                            BatteryConsumer.POWER_MODEL_POWER_PROFILE)
+                    .setUsageDurationMillis(cachedKey, cpuDurationCached);
         }
     }
 
@@ -382,7 +392,7 @@
             double consumedPower, int cpuPower, int customComponentPower, int cpuDuration,
             int customComponentDuration, double cpuPowerForeground, long cpuDurationForeground,
             double cpuPowerBackground, long cpuDurationBackground, double cpuPowerFgs,
-            long cpuDurationFgs) {
+            long cpuDurationFgs, double cpuPowerCached, long cpuDurationCached) {
         final AggregateBatteryConsumer.Builder aggBuilder =
                 builder.getAggregateBatteryConsumerBuilder(scope)
                         .setConsumedPower(consumedPower)
@@ -406,6 +416,9 @@
             final BatteryConsumer.Key cpuFgsKey = aggBuilder.getKey(
                     BatteryConsumer.POWER_COMPONENT_CPU,
                     BatteryConsumer.PROCESS_STATE_FOREGROUND_SERVICE);
+            final BatteryConsumer.Key cpuCachedKey = aggBuilder.getKey(
+                    BatteryConsumer.POWER_COMPONENT_CPU,
+                    BatteryConsumer.PROCESS_STATE_CACHED);
             aggBuilder
                     .setConsumedPower(cpuFgKey, cpuPowerForeground,
                             BatteryConsumer.POWER_MODEL_POWER_PROFILE)
@@ -415,7 +428,10 @@
                     .setUsageDurationMillis(cpuBgKey, cpuDurationBackground)
                     .setConsumedPower(cpuFgsKey, cpuPowerFgs,
                             BatteryConsumer.POWER_MODEL_POWER_PROFILE)
-                    .setUsageDurationMillis(cpuFgsKey, cpuDurationFgs);
+                    .setUsageDurationMillis(cpuFgsKey, cpuDurationFgs)
+                    .setConsumedPower(cpuCachedKey, cpuPowerCached,
+                            BatteryConsumer.POWER_MODEL_POWER_PROFILE)
+                    .setUsageDurationMillis(cpuCachedKey, cpuDurationCached);
         }
     }
 
@@ -432,7 +448,7 @@
                         1000, 2000, 300, BatteryConsumer.POWER_MODEL_POWER_PROFILE, 400,
                         BatteryConsumer.POWER_MODEL_POWER_PROFILE,
                         500, 600, 800,
-                        true, 1777, 1888, 1999, 1777, 7771, 1888, 8881, 1999, 9991);
+                        true, 1777, 1888, 1999, 123, 1777, 7771, 1888, 8881, 1999, 9991, 123, 456);
             } else {
                 fail("Unexpected UID " + uidBatteryConsumer.getUid());
             }
@@ -484,8 +500,10 @@
             int cpuPowerModel, double customComponentPower, int cpuDuration,
             int customComponentDuration, boolean processStateDataIncluded,
             double totalPowerForeground, double totalPowerBackground, double totalPowerFgs,
-            double cpuPowerForeground, int cpuDurationForeground, double cpuPowerBackground,
-            int cpuDurationBackground, double cpuPowerFgs, int cpuDurationFgs) {
+            double totalPowerCached, double cpuPowerForeground, int cpuDurationForeground,
+            double cpuPowerBackground,
+            int cpuDurationBackground, double cpuPowerFgs, int cpuDurationFgs,
+            int cpuPowerCached, int cpuDurationCached) {
         assertThat(uidBatteryConsumer.getConsumedPower()).isEqualTo(consumedPower);
         assertThat(uidBatteryConsumer.getPackageWithHighestDrain()).isEqualTo(
                 packageWithHighestDrain);
@@ -525,6 +543,10 @@
                     new BatteryConsumer.Dimensions(POWER_COMPONENT_ANY,
                             PROCESS_STATE_FOREGROUND_SERVICE)))
                     .isEqualTo(totalPowerFgs);
+            assertThat(uidBatteryConsumer.getConsumedPower(
+                    new BatteryConsumer.Dimensions(POWER_COMPONENT_ANY,
+                            PROCESS_STATE_CACHED)))
+                    .isEqualTo(totalPowerCached);
         }
 
         final BatteryConsumer.Key cpuFgKey = uidBatteryConsumer.getKey(
@@ -563,6 +585,19 @@
         } else {
             assertThat(cpuFgsKey).isNotNull();
         }
+
+        final BatteryConsumer.Key cachedKey = uidBatteryConsumer.getKey(
+                BatteryConsumer.POWER_COMPONENT_CPU,
+                BatteryConsumer.PROCESS_STATE_CACHED);
+        if (processStateDataIncluded) {
+            assertThat(cachedKey).isNotNull();
+            assertThat(uidBatteryConsumer.getConsumedPower(cachedKey))
+                    .isEqualTo(cpuPowerCached);
+            assertThat(uidBatteryConsumer.getUsageDurationMillis(cachedKey))
+                    .isEqualTo(cpuDurationCached);
+        } else {
+            assertThat(cpuFgsKey).isNotNull();
+        }
     }
 
     private void assertUserBatteryConsumer(UserBatteryConsumer userBatteryConsumer,
diff --git a/core/tests/coretests/src/com/android/internal/os/BluetoothPowerCalculatorTest.java b/core/tests/coretests/src/com/android/internal/os/BluetoothPowerCalculatorTest.java
index 448f666..fdbf071 100644
--- a/core/tests/coretests/src/com/android/internal/os/BluetoothPowerCalculatorTest.java
+++ b/core/tests/coretests/src/com/android/internal/os/BluetoothPowerCalculatorTest.java
@@ -145,10 +145,14 @@
         final BatteryConsumer.Key fgs = uidConsumer.getKey(
                 BatteryConsumer.POWER_COMPONENT_BLUETOOTH,
                 BatteryConsumer.PROCESS_STATE_FOREGROUND_SERVICE);
+        final BatteryConsumer.Key cached = uidConsumer.getKey(
+                BatteryConsumer.POWER_COMPONENT_BLUETOOTH,
+                BatteryConsumer.PROCESS_STATE_CACHED);
 
         assertThat(uidConsumer.getConsumedPower(foreground)).isWithin(PRECISION).of(0.081);
         assertThat(uidConsumer.getConsumedPower(background)).isWithin(PRECISION).of(0.0416666);
         assertThat(uidConsumer.getConsumedPower(fgs)).isWithin(PRECISION).of(0);
+        assertThat(uidConsumer.getConsumedPower(cached)).isWithin(PRECISION).of(0);
     }
 
     @Test
@@ -261,10 +265,14 @@
         final BatteryConsumer.Key fgs = uidConsumer.getKey(
                 BatteryConsumer.POWER_COMPONENT_BLUETOOTH,
                 BatteryConsumer.PROCESS_STATE_FOREGROUND_SERVICE);
+        final BatteryConsumer.Key cached = uidConsumer.getKey(
+                BatteryConsumer.POWER_COMPONENT_BLUETOOTH,
+                BatteryConsumer.PROCESS_STATE_CACHED);
 
         assertThat(uidConsumer.getConsumedPower(foreground)).isWithin(PRECISION).of(0.4965352);
         assertThat(uidConsumer.getConsumedPower(background)).isWithin(PRECISION).of(0.3255208);
         assertThat(uidConsumer.getConsumedPower(fgs)).isWithin(PRECISION).of(0);
+        assertThat(uidConsumer.getConsumedPower(cached)).isWithin(PRECISION).of(0);
     }
 
 
diff --git a/core/tests/coretests/src/com/android/internal/os/LooperStatsTest.java b/core/tests/coretests/src/com/android/internal/os/LooperStatsTest.java
index b89e8bc..ec45a01 100644
--- a/core/tests/coretests/src/com/android/internal/os/LooperStatsTest.java
+++ b/core/tests/coretests/src/com/android/internal/os/LooperStatsTest.java
@@ -232,7 +232,7 @@
         assertThat(entry3.handlerClassName).isEqualTo(
                 "com.android.internal.os.LooperStatsTest$TestHandlerSecond");
         assertThat(entry3.messageName).startsWith(
-                "com.android.internal.os.LooperStatsTest$$ExternalSyntheticLambda4");
+                "com.android.internal.os.LooperStatsTest$$ExternalSyntheticLambda5");
         assertThat(entry3.messageCount).isEqualTo(1);
         assertThat(entry3.recordedMessageCount).isEqualTo(1);
         assertThat(entry3.exceptionCount).isEqualTo(0);
diff --git a/core/tests/coretests/src/com/android/internal/widget/CachingIconViewTest.java b/core/tests/coretests/src/com/android/internal/widget/CachingIconViewTest.java
new file mode 100644
index 0000000..0d4b449
--- /dev/null
+++ b/core/tests/coretests/src/com/android/internal/widget/CachingIconViewTest.java
@@ -0,0 +1,250 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.widget;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.annotation.Nullable;
+import android.content.Context;
+import android.graphics.drawable.BitmapDrawable;
+import android.graphics.drawable.Drawable;
+import android.graphics.drawable.Icon;
+import android.graphics.drawable.InsetDrawable;
+import android.net.Uri;
+import android.util.TypedValue;
+import android.view.LayoutInflater;
+
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+import androidx.test.platform.app.InstrumentationRegistry;
+
+import com.android.frameworks.coretests.R;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@RunWith(AndroidJUnit4.class)
+public class CachingIconViewTest {
+
+    private Context mContext;
+
+    @Before
+    public void setUp() {
+        mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
+    }
+
+    @Test
+    public void customDrawable_setImageIcon_skipsResizeSuccessfully() {
+        CachingIconView view = (CachingIconView) LayoutInflater.from(mContext).inflate(
+                R.layout.caching_icon_view_test_max_size, null);
+        view.setImageIcon(Icon.createWithResource(mContext, R.drawable.custom_drawable));
+        Drawable drawable = view.getDrawable();
+        assertThat(drawable).isInstanceOf(InsetDrawable.class);
+    }
+
+    @Test
+    public void customDrawable_setImageIconAsync_skipsResizeSuccessfully() {
+        CachingIconView view = (CachingIconView) LayoutInflater.from(mContext).inflate(
+                R.layout.caching_icon_view_test_max_size, null);
+        view.setImageIconAsync(Icon.createWithResource(mContext, R.drawable.custom_drawable)).run();
+        Drawable drawable = view.getDrawable();
+        assertThat(drawable).isInstanceOf(InsetDrawable.class);
+    }
+
+    @Test
+    public void customDrawable_setImageResource_skipsResizeSuccessfully() {
+        CachingIconView view = (CachingIconView) LayoutInflater.from(mContext).inflate(
+                R.layout.caching_icon_view_test_max_size, null);
+        view.setImageResource(R.drawable.custom_drawable);
+        Drawable drawable = view.getDrawable();
+        assertThat(drawable).isInstanceOf(InsetDrawable.class);
+    }
+
+    @Test
+    public void customDrawable_setImageResourceAsync_skipsResizeSuccessfully() {
+        CachingIconView view = (CachingIconView) LayoutInflater.from(mContext).inflate(
+                R.layout.caching_icon_view_test_max_size, null);
+        view.setImageResourceAsync(R.drawable.custom_drawable).run();
+        Drawable drawable = view.getDrawable();
+        assertThat(drawable).isInstanceOf(InsetDrawable.class);
+    }
+
+    @Test
+    public void customDrawable_setImageUri_skipsResizeSuccessfully() {
+        CachingIconView view = (CachingIconView) LayoutInflater.from(mContext).inflate(
+                R.layout.caching_icon_view_test_max_size, null);
+        view.setImageURI(Uri.parse(
+                "android.resource://com.android.frameworks.coretests/"
+                        + R.drawable.custom_drawable));
+        Drawable drawable = view.getDrawable();
+        assertThat(drawable).isInstanceOf(InsetDrawable.class);
+    }
+
+    @Test
+    public void customDrawable_setImageUriAsync_skipsResizeSuccessfully() {
+        CachingIconView view = (CachingIconView) LayoutInflater.from(mContext).inflate(
+                R.layout.caching_icon_view_test_max_size, null);
+        view.setImageURIAsync(Uri.parse(
+                "android.resource://com.android.frameworks.coretests/"
+                        + R.drawable.custom_drawable)).run();
+        Drawable drawable = view.getDrawable();
+        assertThat(drawable).isInstanceOf(InsetDrawable.class);
+    }
+
+    @Test
+    public void maxDrawableDimensionsSet_setImageIcon_resizesImageIcon() {
+        CachingIconView view = (CachingIconView) LayoutInflater.from(mContext).inflate(
+                R.layout.caching_icon_view_test_max_size, null);
+        view.setImageIcon(Icon.createWithResource(mContext, R.drawable.big_a));
+
+        assertDrawableResized(view);
+    }
+
+    @Test
+    public void maxDrawableWithNoDimensionsSet_setImageIcon_doesNotResizeImageIcon() {
+        CachingIconView view = (CachingIconView) LayoutInflater.from(mContext).inflate(
+                R.layout.caching_icon_view_test_no_max_size, null);
+        view.setImageIcon(Icon.createWithResource(mContext, R.drawable.big_a));
+
+        assertDrawableNotResized(view);
+    }
+
+    @Test
+    public void maxDrawableDimensionsSet_setImageIconAsync_resizesImageIcon() {
+        CachingIconView view = (CachingIconView) LayoutInflater.from(mContext).inflate(
+                R.layout.caching_icon_view_test_max_size, null);
+        view.setImageIconAsync(Icon.createWithResource(mContext, R.drawable.big_a)).run();
+
+        assertDrawableResized(view);
+    }
+
+    @Test
+    public void maxDrawableWithNoDimensionsSet_setImageIconAsync_doesNotResizeImageIcon() {
+        CachingIconView view = (CachingIconView) LayoutInflater.from(mContext).inflate(
+                R.layout.caching_icon_view_test_no_max_size, null);
+        view.setImageIconAsync(Icon.createWithResource(mContext, R.drawable.big_a)).run();
+
+        assertDrawableNotResized(view);
+    }
+
+    @Test
+    public void maxDrawableDimensionsSet_setImageResource_resizesImageIcon() {
+        CachingIconView view = (CachingIconView) LayoutInflater.from(mContext).inflate(
+                R.layout.caching_icon_view_test_max_size, null);
+        view.setImageResource(R.drawable.big_a);
+
+        assertDrawableResized(view);
+    }
+
+    @Test
+    public void maxDrawableWithNoDimensionsSet_setImageResource_doesNotResizeImageIcon() {
+        CachingIconView view = (CachingIconView) LayoutInflater.from(mContext).inflate(
+                R.layout.caching_icon_view_test_no_max_size, null);
+        view.setImageResource(R.drawable.big_a);
+
+        assertDrawableNotResized(view);
+    }
+
+    @Test
+    public void maxDrawableDimensionsSet_setImageResourceAsync_resizesImageIcon() {
+        CachingIconView view = (CachingIconView) LayoutInflater.from(mContext).inflate(
+                R.layout.caching_icon_view_test_max_size, null);
+        view.setImageResourceAsync(R.drawable.big_a).run();
+
+        assertDrawableResized(view);
+    }
+
+    @Test
+    public void maxDrawableWithNoDimensionsSet_setImageResourceAsync_doesNotResizeImageIcon() {
+        CachingIconView view = (CachingIconView) LayoutInflater.from(mContext).inflate(
+                R.layout.caching_icon_view_test_no_max_size, null);
+        view.setImageResourceAsync(R.drawable.big_a).run();
+
+        assertDrawableNotResized(view);
+    }
+
+    @Test
+    public void maxDrawableDimensionsSet_setImageUri_resizesImageIcon() {
+        CachingIconView view = (CachingIconView) LayoutInflater.from(mContext).inflate(
+                R.layout.caching_icon_view_test_max_size, null);
+        view.setImageURI(Uri.parse(
+                "android.resource://com.android.frameworks.coretests/" + R.drawable.big_a));
+
+        assertDrawableResized(view);
+    }
+
+    @Test
+    public void maxDrawableWithNoDimensionsSet_setImageUri_doesNotResizeImageIcon() {
+        CachingIconView view = (CachingIconView) LayoutInflater.from(mContext).inflate(
+                R.layout.caching_icon_view_test_no_max_size, null);
+        view.setImageURI(Uri.parse(
+                "android.resource://com.android.frameworks.coretests/" + R.drawable.big_a));
+
+        assertDrawableNotResized(view);
+    }
+
+    @Test
+    public void maxDrawableDimensionsSet_setImageUriAsync_resizesImageIcon() {
+        CachingIconView view = (CachingIconView) LayoutInflater.from(mContext).inflate(
+                R.layout.caching_icon_view_test_max_size, null);
+        view.setImageURIAsync(Uri.parse(
+                "android.resource://com.android.frameworks.coretests/" + R.drawable.big_a)).run();
+
+        assertDrawableResized(view);
+    }
+
+    @Test
+    public void maxDrawableWithNoDimensionsSet_setImageUriAsync_doesNotResizeImageIcon() {
+        CachingIconView view = (CachingIconView) LayoutInflater.from(mContext).inflate(
+                R.layout.caching_icon_view_test_no_max_size, null);
+        view.setImageURIAsync(Uri.parse(
+                "android.resource://com.android.frameworks.coretests/" + R.drawable.big_a)).run();
+
+        assertDrawableNotResized(view);
+    }
+
+
+    private void assertDrawableResized(@Nullable CachingIconView view) {
+        assertThat(view).isNotNull();
+        int maxSize =
+                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80f,
+                        mContext.getResources().getDisplayMetrics());
+        assertThat(view.getMaxDrawableHeight()).isEqualTo(maxSize);
+        assertThat(view.getMaxDrawableWidth()).isEqualTo(maxSize);
+
+        Drawable drawable = view.getDrawable();
+        assertThat(drawable).isInstanceOf(BitmapDrawable.class);
+        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
+        assertThat(bitmapDrawable.getBitmap().getWidth()).isLessThan(maxSize + 1);
+        assertThat(bitmapDrawable.getBitmap().getHeight()).isLessThan(maxSize + 1);
+    }
+
+    private void assertDrawableNotResized(@Nullable CachingIconView view) {
+        assertThat(view).isNotNull();
+        int maxSize =
+                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80f,
+                        mContext.getResources().getDisplayMetrics());
+        assertThat(view.getMaxDrawableHeight()).isEqualTo(-1);
+        assertThat(view.getMaxDrawableWidth()).isEqualTo(-1);
+
+        Drawable drawable = view.getDrawable();
+        assertThat(drawable).isInstanceOf(BitmapDrawable.class);
+        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
+        assertThat(bitmapDrawable.getBitmap().getWidth()).isGreaterThan(maxSize);
+        assertThat(bitmapDrawable.getBitmap().getHeight()).isGreaterThan(maxSize);
+    }
+}
diff --git a/core/tests/coretests/src/com/android/internal/widget/LocalImageResolverTest.java b/core/tests/coretests/src/com/android/internal/widget/LocalImageResolverTest.java
new file mode 100644
index 0000000..8dcb4a2
--- /dev/null
+++ b/core/tests/coretests/src/com/android/internal/widget/LocalImageResolverTest.java
@@ -0,0 +1,169 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.widget;
+
+import android.content.Context;
+import android.graphics.BitmapFactory;
+import android.graphics.drawable.AdaptiveIconDrawable;
+import android.graphics.drawable.BitmapDrawable;
+import android.graphics.drawable.Drawable;
+import android.graphics.drawable.Icon;
+
+import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;
+import androidx.test.platform.app.InstrumentationRegistry;
+
+import com.android.frameworks.coretests.R;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.io.IOException;
+
+@RunWith(AndroidJUnit4ClassRunner.class)
+public class LocalImageResolverTest {
+
+    private final Context mContext = InstrumentationRegistry.getInstrumentation().getContext();
+
+    @Test
+    public void resolveImage_largeBitmapIcon_defaultSize_resizeToDefaultSize() throws
+            IOException {
+        Icon icon = Icon.createWithBitmap(
+                BitmapFactory.decodeResource(mContext.getResources(), R.drawable.big_a));
+        Drawable d = LocalImageResolver.resolveImage(icon, mContext);
+
+        assertThat(d).isInstanceOf(BitmapDrawable.class);
+        BitmapDrawable bd = (BitmapDrawable) d;
+        // No isLessOrEqualThan sadly.
+        assertThat(bd.getBitmap().getWidth()).isLessThan(
+                LocalImageResolver.DEFAULT_MAX_SAFE_ICON_SIZE_PX + 1);
+        assertThat(bd.getBitmap().getHeight()).isLessThan(
+                LocalImageResolver.DEFAULT_MAX_SAFE_ICON_SIZE_PX + 1);
+    }
+
+    @Test
+    public void resolveImage_largeAdaptiveBitmapIcon_defaultSize_resizeToDefaultSize() throws
+            IOException {
+        Icon icon = Icon.createWithAdaptiveBitmap(
+                BitmapFactory.decodeResource(mContext.getResources(), R.drawable.big_a));
+        Drawable d = LocalImageResolver.resolveImage(icon, mContext);
+
+        assertThat(d).isInstanceOf(AdaptiveIconDrawable.class);
+        BitmapDrawable bd = (BitmapDrawable) ((AdaptiveIconDrawable) d).getForeground();
+        // No isLessOrEqualThan sadly.
+        assertThat(bd.getBitmap().getWidth()).isLessThan(
+                LocalImageResolver.DEFAULT_MAX_SAFE_ICON_SIZE_PX + 1);
+        assertThat(bd.getBitmap().getHeight()).isLessThan(
+                LocalImageResolver.DEFAULT_MAX_SAFE_ICON_SIZE_PX + 1);
+    }
+
+    @Test
+    public void resolveImage_largeResourceIcon_defaultSize_resizeToDefaultSize() throws
+            IOException {
+        Icon icon = Icon.createWithResource(mContext, R.drawable.big_a);
+        Drawable d = LocalImageResolver.resolveImage(icon, mContext);
+
+        assertThat(d).isInstanceOf(BitmapDrawable.class);
+        BitmapDrawable bd = (BitmapDrawable) d;
+        // No isLessOrEqualThan sadly.
+        assertThat(bd.getBitmap().getWidth()).isLessThan(
+                LocalImageResolver.DEFAULT_MAX_SAFE_ICON_SIZE_PX + 1);
+        assertThat(bd.getBitmap().getHeight()).isLessThan(
+                LocalImageResolver.DEFAULT_MAX_SAFE_ICON_SIZE_PX + 1);
+    }
+
+    @Test
+    public void resolveImage_largeResourceIcon_passedSize_resizeToDefinedSize() throws
+            IOException {
+        Icon icon = Icon.createWithResource(mContext, R.drawable.big_a);
+        Drawable d = LocalImageResolver.resolveImage(icon, mContext, 100, 50);
+
+        assertThat(d).isInstanceOf(BitmapDrawable.class);
+        BitmapDrawable bd = (BitmapDrawable) d;
+        assertThat(bd.getBitmap().getWidth()).isLessThan(101);
+        assertThat(bd.getBitmap().getHeight()).isLessThan(51);
+    }
+
+    @Test
+    public void resolveImage_largeBitmapIcon_passedSize_resizeToDefinedSize() throws
+            IOException {
+        Icon icon = Icon.createWithBitmap(
+                BitmapFactory.decodeResource(mContext.getResources(), R.drawable.big_a));
+        Drawable d = LocalImageResolver.resolveImage(icon, mContext, 100, 50);
+
+        assertThat(d).isInstanceOf(BitmapDrawable.class);
+        BitmapDrawable bd = (BitmapDrawable) d;
+        assertThat(bd.getBitmap().getWidth()).isLessThan(101);
+        assertThat(bd.getBitmap().getHeight()).isLessThan(51);
+    }
+
+    @Test
+    public void resolveImage_largeAdaptiveBitmapIcon_passedSize_resizeToDefinedSize() throws
+            IOException {
+        Icon icon = Icon.createWithAdaptiveBitmap(
+                BitmapFactory.decodeResource(mContext.getResources(), R.drawable.big_a));
+        Drawable d = LocalImageResolver.resolveImage(icon, mContext, 100, 50);
+
+        assertThat(d).isInstanceOf(AdaptiveIconDrawable.class);
+        BitmapDrawable bd = (BitmapDrawable) ((AdaptiveIconDrawable) d).getForeground();
+        assertThat(bd.getBitmap().getWidth()).isLessThan(101);
+        assertThat(bd.getBitmap().getHeight()).isLessThan(51);
+    }
+
+
+    @Test
+    public void resolveImage_smallResourceIcon_defaultSize_untouched() throws IOException {
+        Icon icon = Icon.createWithResource(mContext, R.drawable.test32x24);
+        Drawable d = LocalImageResolver.resolveImage(icon, mContext);
+
+        assertThat(d).isInstanceOf(BitmapDrawable.class);
+        BitmapDrawable bd = (BitmapDrawable) d;
+        assertThat(bd.getBitmap().getWidth()).isEqualTo(32);
+        assertThat(bd.getBitmap().getHeight()).isEqualTo(24);
+    }
+
+    @Test
+    public void resolveImage_smallBitmapIcon_defaultSize_untouched() throws IOException {
+        Icon icon = Icon.createWithBitmap(
+                BitmapFactory.decodeResource(mContext.getResources(), R.drawable.test32x24));
+        final int originalWidth = icon.getBitmap().getWidth();
+        final int originalHeight = icon.getBitmap().getHeight();
+
+        Drawable d = LocalImageResolver.resolveImage(icon, mContext);
+
+        assertThat(d).isInstanceOf(BitmapDrawable.class);
+        BitmapDrawable bd = (BitmapDrawable) d;
+        assertThat(bd.getBitmap().getWidth()).isEqualTo(originalWidth);
+        assertThat(bd.getBitmap().getHeight()).isEqualTo(originalHeight);
+    }
+
+    @Test
+    public void resolveImage_smallAdaptiveBitmapIcon_defaultSize_untouched() throws IOException {
+        Icon icon = Icon.createWithAdaptiveBitmap(
+                BitmapFactory.decodeResource(mContext.getResources(), R.drawable.test32x24));
+        final int originalWidth = icon.getBitmap().getWidth();
+        final int originalHeight = icon.getBitmap().getHeight();
+
+        Drawable d = LocalImageResolver.resolveImage(icon, mContext);
+        assertThat(d).isInstanceOf(AdaptiveIconDrawable.class);
+        BitmapDrawable bd = (BitmapDrawable) ((AdaptiveIconDrawable) d).getForeground();
+        assertThat(bd.getBitmap().getWidth()).isEqualTo(originalWidth);
+        assertThat(bd.getBitmap().getHeight()).isEqualTo(originalHeight);
+
+    }
+}
diff --git a/core/tests/coretests/src/com/android/internal/widget/LockPatternViewTest.java b/core/tests/coretests/src/com/android/internal/widget/LockPatternViewTest.java
new file mode 100644
index 0000000..8ba4966
--- /dev/null
+++ b/core/tests/coretests/src/com/android/internal/widget/LockPatternViewTest.java
@@ -0,0 +1,247 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.widget;
+
+import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
+
+import android.content.Context;
+
+import androidx.test.annotation.UiThreadTest;
+
+import android.util.AttributeSet;
+import android.view.MotionEvent;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.Toolbar;
+
+
+import static org.hamcrest.Matchers.contains;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.atLeastOnce;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+
+import androidx.test.InstrumentationRegistry;
+import androidx.test.filters.SmallTest;
+import androidx.test.rule.UiThreadTestRule;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.mockito.ArgumentCaptor;
+import org.mockito.ArgumentMatchers;
+import org.mockito.Captor;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import com.android.internal.R;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+
+@RunWith(Parameterized.class)
+@SmallTest
+public class LockPatternViewTest {
+
+    @Rule
+    public UiThreadTestRule uiThreadTestRule = new UiThreadTestRule();
+
+    private final int mViewSize;
+    private final float mDefaultError;
+    private final float mDot1x;
+    private final float mDot1y;
+    private final float mDot2x;
+    private final float mDot2y;
+    private final float mDot3x;
+    private final float mDot3y;
+    private final float mDot5x;
+    private final float mDot5y;
+    private final float mDot7x;
+    private final float mDot7y;
+    private final float mDot9x;
+    private final float mDot9y;
+
+    private Context mContext;
+    private LockPatternView mLockPatternView;
+    @Mock
+    private LockPatternView.OnPatternListener mPatternListener;
+    @Captor
+    private ArgumentCaptor<List<LockPatternView.Cell>> mCellsArgumentCaptor;
+
+    public LockPatternViewTest(int viewSize) {
+        mViewSize = viewSize;
+        float cellSize = viewSize / 3f;
+        mDefaultError = cellSize * 0.2f;
+        mDot1x = cellSize / 2f;
+        mDot1y = cellSize / 2f;
+        mDot2x = cellSize + mDot1x;
+        mDot2y = mDot1y;
+        mDot3x = cellSize + mDot2x;
+        mDot3y = mDot1y;
+        // dot4 is skipped as redundant
+        mDot5x = cellSize + mDot1x;
+        mDot5y = cellSize + mDot1y;
+        // dot6 is skipped as redundant
+        mDot7x = mDot1x;
+        mDot7y = cellSize * 2 + mDot1y;
+        // dot8 is skipped as redundant
+        mDot9x = cellSize * 2 + mDot7x;
+        mDot9y = mDot7y;
+    }
+
+    @Parameterized.Parameters
+    public static Collection primeNumbers() {
+        return Arrays.asList(192, 512, 768, 1024);
+    }
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+        mContext = InstrumentationRegistry.getContext();
+        mLockPatternView = new LockPatternView(mContext, null);
+        int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(mViewSize,
+                View.MeasureSpec.EXACTLY);
+        int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(mViewSize,
+                View.MeasureSpec.EXACTLY);
+        mLockPatternView.measure(widthMeasureSpec, heightMeasureSpec);
+        mLockPatternView.layout(0, 0, mLockPatternView.getMeasuredWidth(),
+                mLockPatternView.getMeasuredHeight());
+    }
+
+    @UiThreadTest
+    @Test
+    public void downStartsPattern() {
+        mLockPatternView.setOnPatternListener(mPatternListener);
+        mLockPatternView.onTouchEvent(
+                MotionEvent.obtain(0, 0, MotionEvent.ACTION_DOWN, mDot1x, mDot1y, 1));
+        verify(mPatternListener).onPatternStart();
+    }
+
+    @UiThreadTest
+    @Test
+    public void up_completesPattern() {
+        mLockPatternView.setOnPatternListener(mPatternListener);
+        mLockPatternView.onTouchEvent(
+                MotionEvent.obtain(0, 0, MotionEvent.ACTION_DOWN, mDot1x, mDot1y, 1));
+        mLockPatternView.onTouchEvent(
+                MotionEvent.obtain(0, 0, MotionEvent.ACTION_UP, mDot1x, mDot1y, 1));
+        verify(mPatternListener).onPatternDetected(any());
+    }
+
+    @UiThreadTest
+    @Test
+    public void moveToDot_hitsDot() {
+        mLockPatternView.setOnPatternListener(mPatternListener);
+        mLockPatternView.onTouchEvent(
+                MotionEvent.obtain(0, 0, MotionEvent.ACTION_DOWN, 1f, 1f, 1));
+        mLockPatternView.onTouchEvent(
+                MotionEvent.obtain(0, 0, MotionEvent.ACTION_MOVE, mDot1x, mDot1y, 1));
+        verify(mPatternListener).onPatternStart();
+    }
+
+    @UiThreadTest
+    @Test
+    public void moveOutside_doesNotHitsDot() {
+        mLockPatternView.setOnPatternListener(mPatternListener);
+        mLockPatternView.onTouchEvent(
+                MotionEvent.obtain(0, 0, MotionEvent.ACTION_DOWN, 1f, 1f, 1));
+        mLockPatternView.onTouchEvent(
+                MotionEvent.obtain(0, 0, MotionEvent.ACTION_MOVE, 2f, 2f, 1));
+        verify(mPatternListener, never()).onPatternStart();
+    }
+
+    @UiThreadTest
+    @Test
+    public void moveAlongTwoDots_hitsTwo() {
+        mLockPatternView.setOnPatternListener(mPatternListener);
+        mLockPatternView.onTouchEvent(
+                MotionEvent.obtain(0, 0, MotionEvent.ACTION_DOWN, 1f, 1f, 1));
+        makeMove(mDot1x, mDot1y, mDot2x, mDot2y, 6);
+        mLockPatternView.onTouchEvent(
+                MotionEvent.obtain(0, 3, MotionEvent.ACTION_UP, mDot2x, mDot2y, 1));
+
+        verify(mPatternListener).onPatternDetected(mCellsArgumentCaptor.capture());
+        List<LockPatternView.Cell> patternCells = mCellsArgumentCaptor.getValue();
+        assertThat(patternCells, hasSize(2));
+        assertThat(patternCells,
+                contains(LockPatternView.Cell.of(0, 0), LockPatternView.Cell.of(0, 1)));
+    }
+
+    @UiThreadTest
+    @Test
+    public void moveAlongTwoDotsDiagonally_hitsTwo() {
+        mLockPatternView.setOnPatternListener(mPatternListener);
+        mLockPatternView.onTouchEvent(
+                MotionEvent.obtain(0, 0, MotionEvent.ACTION_DOWN, 1f, 1f, 1));
+        makeMove(mDot1x, mDot1y, mDot5x, mDot5y, 6);
+        mLockPatternView.onTouchEvent(
+                MotionEvent.obtain(0, 3, MotionEvent.ACTION_UP, mDot5x, mDot5y, 1));
+
+        verify(mPatternListener).onPatternDetected(mCellsArgumentCaptor.capture());
+        List<LockPatternView.Cell> patternCells = mCellsArgumentCaptor.getValue();
+        assertThat(patternCells, hasSize(2));
+        assertThat(patternCells,
+                contains(LockPatternView.Cell.of(0, 0), LockPatternView.Cell.of(1, 1)));
+    }
+
+    @UiThreadTest
+    @Test
+    public void moveAlongZPattern_hitsDots() {
+        mLockPatternView.setOnPatternListener(mPatternListener);
+        mLockPatternView.onTouchEvent(
+                MotionEvent.obtain(0, 0, MotionEvent.ACTION_DOWN, 1f, 1f, 1));
+        makeMove(mDot1x, mDot1y, mDot3x + mDefaultError, mDot3y, 10);
+        makeMove(mDot3x - mDefaultError, mDot3y, mDot7x, mDot7y, 10);
+        makeMove(mDot7x, mDot7y - mDefaultError, mDot9x, mDot9y - mDefaultError, 10);
+        mLockPatternView.onTouchEvent(
+                MotionEvent.obtain(0, 0, MotionEvent.ACTION_UP, mViewSize - mDefaultError,
+                        mViewSize - mDefaultError, 1));
+
+        verify(mPatternListener).onPatternDetected(mCellsArgumentCaptor.capture());
+        List<LockPatternView.Cell> patternCells = mCellsArgumentCaptor.getValue();
+        assertThat(patternCells, hasSize(7));
+        assertThat(patternCells,
+                contains(LockPatternView.Cell.of(0, 0),
+                        LockPatternView.Cell.of(0, 1),
+                        LockPatternView.Cell.of(0, 2),
+                        LockPatternView.Cell.of(1, 1),
+                        LockPatternView.Cell.of(2, 0),
+                        LockPatternView.Cell.of(2, 1),
+                        LockPatternView.Cell.of(2, 2)));
+    }
+
+    private void makeMove(float xFrom, float yFrom, float xTo, float yTo, int numberOfSteps) {
+        for (int i = 0; i < numberOfSteps; i++) {
+            float progress = i / (numberOfSteps - 1f);
+            float rest = 1f - progress;
+            mLockPatternView.onTouchEvent(
+                    MotionEvent.obtain(0, 0, MotionEvent.ACTION_MOVE,
+                            /* x= */ xFrom * rest + xTo * progress,
+                            /* y= */ yFrom * rest + yTo * progress,
+                            1));
+        }
+    }
+}
diff --git a/core/tests/utiltests/src/com/android/internal/util/LockPatternUtilsTest.java b/core/tests/utiltests/src/com/android/internal/util/LockPatternUtilsTest.java
index b659f37..940ca96 100644
--- a/core/tests/utiltests/src/com/android/internal/util/LockPatternUtilsTest.java
+++ b/core/tests/utiltests/src/com/android/internal/util/LockPatternUtilsTest.java
@@ -19,14 +19,20 @@
 import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_MANAGED;
 import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.anyInt;
+import static org.mockito.Mockito.anyString;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
+import android.content.ComponentName;
 import android.content.Context;
 import android.content.ContextWrapper;
 import android.content.pm.UserInfo;
@@ -50,6 +56,7 @@
 import org.mockito.Mockito;
 
 import java.nio.charset.StandardCharsets;
+import java.util.List;
 
 @RunWith(AndroidJUnit4.class)
 @SmallTest
@@ -164,6 +171,16 @@
         verify(ils).isWeakEscrowTokenValid(eq(testHandle), eq(testToken), eq(testUserId));
     }
 
+    @Test
+    public void testGetEnabledTrustAgentsNotNull() throws RemoteException {
+        int testUserId = 10;
+        ILockSettings ils = createTestLockSettings();
+        when(ils.getString(anyString(), any(), anyInt())).thenReturn("");
+        List<ComponentName> trustAgents = mLockPatternUtils.getEnabledTrustAgents(testUserId);
+        assertNotNull(trustAgents);
+        assertEquals(0, trustAgents.size());
+    }
+
     private ILockSettings createTestLockSettings() {
         final Context context = spy(new ContextWrapper(InstrumentationRegistry.getTargetContext()));
         mLockPatternUtils = spy(new LockPatternUtils(context));
diff --git a/data/etc/com.android.launcher3.xml b/data/etc/com.android.launcher3.xml
index 598d202..36a5134 100644
--- a/data/etc/com.android.launcher3.xml
+++ b/data/etc/com.android.launcher3.xml
@@ -16,6 +16,7 @@
   -->
 <permissions>
     <privapp-permissions package="com.android.launcher3">
+        <permission name="android.permission.ALLOW_SLIPPERY_TOUCHES"/>
         <permission name="android.permission.BIND_APPWIDGET"/>
         <permission name="android.permission.CONTROL_REMOTE_APP_TRANSITION_ANIMATIONS"/>
         <permission name="android.permission.GET_ACCOUNTS_PRIVILEGED"/>
diff --git a/data/etc/com.android.systemui.xml b/data/etc/com.android.systemui.xml
index ae350ec..2d1db71 100644
--- a/data/etc/com.android.systemui.xml
+++ b/data/etc/com.android.systemui.xml
@@ -17,6 +17,7 @@
 <permissions>
     <privapp-permissions package="com.android.systemui">
         <permission name="android.permission.CAPTURE_AUDIO_OUTPUT"/>
+        <permission name="android.permission.ALLOW_SLIPPERY_TOUCHES"/>
         <permission name="android.permission.BATTERY_STATS"/>
         <permission name="android.permission.BIND_APPWIDGET"/>
         <permission name="android.permission.BLUETOOTH_PRIVILEGED"/>
@@ -75,5 +76,8 @@
         <permission name="android.permission.FORCE_STOP_PACKAGES" />
         <permission name="android.permission.ACCESS_FPS_COUNTER" />
         <permission name="android.permission.CHANGE_CONFIGURATION" />
+        <permission name="android.permission.LOG_COMPAT_CHANGE" />
+        <permission name="android.permission.READ_COMPAT_CHANGE_CONFIG" />
+        <permission name="android.permission.READ_DEVICE_CONFIG" />
     </privapp-permissions>
 </permissions>
diff --git a/data/etc/platform.xml b/data/etc/platform.xml
index 88920c8..a829339 100644
--- a/data/etc/platform.xml
+++ b/data/etc/platform.xml
@@ -241,7 +241,7 @@
     </split-permission>
     <split-permission name="android.permission.READ_EXTERNAL_STORAGE"
                       targetSdk="33">
-        <new-permission name="android.permission.READ_MEDIA_IMAGE" />
+        <new-permission name="android.permission.READ_MEDIA_IMAGES" />
     </split-permission>
     <split-permission name="android.permission.BLUETOOTH"
                       targetSdk="31">
diff --git a/data/etc/privapp-permissions-platform.xml b/data/etc/privapp-permissions-platform.xml
index deffa3a..3e91eed 100644
--- a/data/etc/privapp-permissions-platform.xml
+++ b/data/etc/privapp-permissions-platform.xml
@@ -452,10 +452,11 @@
         <permission name="android.permission.WIFI_ACCESS_COEX_UNSAFE_CHANNELS" />
         <permission name="android.permission.WIFI_UPDATE_COEX_UNSAFE_CHANNELS" />
         <permission name="android.permission.NEARBY_WIFI_DEVICES" />
+        <permission name="android.permission.MANAGE_WIFI_INTERFACES" />
         <permission name="android.permission.OVERRIDE_WIFI_CONFIG" />
         <!-- Permission needed for CTS test - ConcurrencyTest#testP2pExternalApprover
-             P2P external approver API sets require MANAGE_WIFI_AUTO_JOIN permission. -->
-        <permission name="android.permission.MANAGE_WIFI_AUTO_JOIN" />
+             P2P external approver API sets require MANAGE_WIFI_NETWORK_SELECTION permission. -->
+        <permission name="android.permission.MANAGE_WIFI_NETWORK_SELECTION" />
         <!-- Permission required for CTS test CarrierMessagingServiceWrapperTest -->
         <permission name="android.permission.BIND_CARRIER_SERVICES"/>
         <!-- Permission required for CTS test - MusicRecognitionManagerTest -->
diff --git a/data/etc/services.core.protolog.json b/data/etc/services.core.protolog.json
index df2b2a3..e898f57 100644
--- a/data/etc/services.core.protolog.json
+++ b/data/etc/services.core.protolog.json
@@ -1309,6 +1309,12 @@
       "group": "WM_DEBUG_STATES",
       "at": "com\/android\/server\/wm\/ActivityRecord.java"
     },
+    "-711194343": {
+      "message": "Setting Activity.mLauncherTaskBehind to false. Activity=%s",
+      "level": "DEBUG",
+      "group": "WM_DEBUG_BACK_PREVIEW",
+      "at": "com\/android\/server\/wm\/BackNavigationController.java"
+    },
     "-706481945": {
       "message": "TaskFragment parent info changed name=%s parentTaskId=%d",
       "level": "VERBOSE",
@@ -2587,6 +2593,12 @@
       "group": "WM_DEBUG_APP_TRANSITIONS",
       "at": "com\/android\/server\/wm\/WindowState.java"
     },
+    "599897753": {
+      "message": "Previous Activity is %s. Back type is %s",
+      "level": "DEBUG",
+      "group": "WM_DEBUG_BACK_PREVIEW",
+      "at": "com\/android\/server\/wm\/BackNavigationController.java"
+    },
     "600140673": {
       "message": "checkBootAnimationComplete: Waiting for anim complete",
       "level": "INFO",
@@ -2671,12 +2683,6 @@
       "group": "WM_ERROR",
       "at": "com\/android\/server\/wm\/WindowManagerService.java"
     },
-    "664667685": {
-      "message": "Activity %s: enableOnBackInvokedCallback=false. Returning null BackNavigationInfo.",
-      "level": "DEBUG",
-      "group": "WM_DEBUG_BACK_PREVIEW",
-      "at": "com\/android\/server\/wm\/BackNavigationController.java"
-    },
     "665256544": {
       "message": "All windows drawn!",
       "level": "DEBUG",
@@ -2887,6 +2893,12 @@
       "group": "WM_DEBUG_STATES",
       "at": "com\/android\/server\/wm\/TaskFragment.java"
     },
+    "948208142": {
+      "message": "Setting Activity.mLauncherTaskBehind to true. Activity=%s",
+      "level": "DEBUG",
+      "group": "WM_DEBUG_BACK_PREVIEW",
+      "at": "com\/android\/server\/wm\/BackNavigationController.java"
+    },
     "950074526": {
       "message": "setLockTaskMode: Can't lock due to auth",
       "level": "WARN",
@@ -3103,6 +3115,12 @@
       "group": "WM_DEBUG_APP_TRANSITIONS",
       "at": "com\/android\/server\/wm\/DisplayContent.java"
     },
+    "1172542963": {
+      "message": "onBackNavigationDone backType=%s, task=%s, prevTaskTopActivity=%s",
+      "level": "DEBUG",
+      "group": "WM_DEBUG_BACK_PREVIEW",
+      "at": "com\/android\/server\/wm\/BackNavigationController.java"
+    },
     "1178653181": {
       "message": "Old wallpaper still the target.",
       "level": "VERBOSE",
@@ -3415,11 +3433,11 @@
       "group": "WM_DEBUG_APP_TRANSITIONS",
       "at": "com\/android\/server\/wm\/ActivityRecord.java"
     },
-    "1554795024": {
-      "message": "Previous Activity is %s",
+    "1544805551": {
+      "message": "Skipping app transition animation. task=%s",
       "level": "DEBUG",
       "group": "WM_DEBUG_BACK_PREVIEW",
-      "at": "com\/android\/server\/wm\/BackNavigationController.java"
+      "at": "com\/android\/server\/wm\/Task.java"
     },
     "1557732761": {
       "message": "For Intent %s bringing to top: %s",
diff --git a/graphics/java/android/graphics/drawable/RippleDrawable.java b/graphics/java/android/graphics/drawable/RippleDrawable.java
index ffaa4ea..c1addbf 100644
--- a/graphics/java/android/graphics/drawable/RippleDrawable.java
+++ b/graphics/java/android/graphics/drawable/RippleDrawable.java
@@ -1098,19 +1098,16 @@
         }
 
         // Draw the appropriate mask anchored to (0,0).
+        final int saveCount = mMaskCanvas.save();
         final int left = bounds.left;
         final int top = bounds.top;
-        if (mState.mRippleStyle == STYLE_SOLID) {
-            mMaskCanvas.translate(-left, -top);
-        }
+        mMaskCanvas.translate(-left, -top);
         if (maskType == MASK_EXPLICIT) {
             drawMask(mMaskCanvas);
         } else if (maskType == MASK_CONTENT) {
             drawContent(mMaskCanvas);
         }
-        if (mState.mRippleStyle == STYLE_SOLID) {
-            mMaskCanvas.translate(left, top);
-        }
+        mMaskCanvas.restoreToCount(saveCount);
         if (mState.mRippleStyle == STYLE_PATTERNED) {
             for (int i = 0; i < mRunningAnimations.size(); i++) {
                 mRunningAnimations.get(i).getProperties().getShader().setShader(mMaskShader);
@@ -1210,9 +1207,13 @@
         updateMaskShaderIfNeeded();
 
         // Position the shader to account for canvas translation.
-        if (mMaskShader != null && mState.mRippleStyle == STYLE_SOLID) {
+        if (mMaskShader != null) {
             final Rect bounds = getBounds();
-            mMaskMatrix.setTranslate(bounds.left - x, bounds.top - y);
+            if (mState.mRippleStyle == STYLE_PATTERNED) {
+                mMaskMatrix.setTranslate(bounds.left, bounds.top);
+            } else {
+                mMaskMatrix.setTranslate(bounds.left - x, bounds.top - y);
+            }
             mMaskShader.setLocalMatrix(mMaskMatrix);
         }
 
diff --git a/graphics/java/android/graphics/text/LineBreakConfig.java b/graphics/java/android/graphics/text/LineBreakConfig.java
index cffdf28..d083e44 100644
--- a/graphics/java/android/graphics/text/LineBreakConfig.java
+++ b/graphics/java/android/graphics/text/LineBreakConfig.java
@@ -26,7 +26,7 @@
 /**
  * Indicates the strategies can be used when calculating the text wrapping.
  *
- * See <a href="https://drafts.csswg.org/css-text/#line-break-property">the line-break property</a>
+ * See <a href="https://www.w3.org/TR/css-text-3/#line-break-property">the line-break property</a>
  */
 public final class LineBreakConfig {
 
@@ -78,21 +78,87 @@
     @Retention(RetentionPolicy.SOURCE)
     public @interface LineBreakWordStyle {}
 
-    private @LineBreakStyle int mLineBreakStyle = LINE_BREAK_STYLE_NONE;
-    private @LineBreakWordStyle int mLineBreakWordStyle = LINE_BREAK_WORD_STYLE_NONE;
+    /**
+     * A builder for creating {@link LineBreakConfig}.
+     */
+    public static final class Builder {
+        // The line break style for the LineBreakConfig.
+        private @LineBreakStyle int mLineBreakStyle = LineBreakConfig.LINE_BREAK_STYLE_NONE;
 
-    public LineBreakConfig() {
+        // The line break word style for the LineBreakConfig.
+        private @LineBreakWordStyle int mLineBreakWordStyle =
+                LineBreakConfig.LINE_BREAK_WORD_STYLE_NONE;
+
+        /**
+         * Builder constructor with line break parameters.
+         */
+        public Builder() {
+        }
+
+        /**
+         * Set the line break style.
+         *
+         * @param lineBreakStyle the new line break style.
+         * @return this Builder
+         */
+        public @NonNull Builder setLineBreakStyle(@LineBreakStyle int lineBreakStyle) {
+            mLineBreakStyle = lineBreakStyle;
+            return this;
+        }
+
+        /**
+         * Set the line break word style.
+         *
+         * @param lineBreakWordStyle the new line break word style.
+         * @return this Builder
+         */
+        public @NonNull Builder setLineBreakWordStyle(@LineBreakWordStyle int lineBreakWordStyle) {
+            mLineBreakWordStyle = lineBreakWordStyle;
+            return this;
+        }
+
+        /**
+         * Build the {@link LineBreakConfig}
+         *
+         * @return the LineBreakConfig instance.
+         */
+        public @NonNull LineBreakConfig build() {
+            return new LineBreakConfig(mLineBreakStyle, mLineBreakWordStyle);
+        }
     }
 
     /**
-     * Set the line break configuration.
+     * Create the LineBreakConfig instance.
      *
-     * @param lineBreakConfig the new line break configuration.
+     * @param lineBreakStyle the line break style for text wrapping.
+     * @param lineBreakWordStyle the line break word style for text wrapping.
+     * @return the {@link LineBreakConfig} instance.
+     * @hide
      */
-    public void set(@NonNull LineBreakConfig lineBreakConfig) {
-        Objects.requireNonNull(lineBreakConfig);
-        mLineBreakStyle = lineBreakConfig.getLineBreakStyle();
-        mLineBreakWordStyle = lineBreakConfig.getLineBreakWordStyle();
+    public static @NonNull LineBreakConfig getLineBreakConfig(@LineBreakStyle int lineBreakStyle,
+            @LineBreakWordStyle int lineBreakWordStyle) {
+        LineBreakConfig.Builder builder = new LineBreakConfig.Builder();
+        return builder.setLineBreakStyle(lineBreakStyle)
+                .setLineBreakWordStyle(lineBreakWordStyle)
+                .build();
+    }
+
+    /** @hide */
+    public static final LineBreakConfig NONE =
+            new Builder().setLineBreakStyle(LINE_BREAK_STYLE_NONE)
+                    .setLineBreakWordStyle(LINE_BREAK_WORD_STYLE_NONE).build();
+
+    private final @LineBreakStyle int mLineBreakStyle;
+    private final @LineBreakWordStyle int mLineBreakWordStyle;
+
+    /**
+     * Constructor with the line break parameters.
+     * Use the {@link LineBreakConfig.Builder} to create the LineBreakConfig instance.
+     */
+    private LineBreakConfig(@LineBreakStyle int lineBreakStyle,
+            @LineBreakWordStyle int lineBreakWordStyle) {
+        mLineBreakStyle = lineBreakStyle;
+        mLineBreakWordStyle = lineBreakWordStyle;
     }
 
     /**
@@ -105,15 +171,6 @@
     }
 
     /**
-     * Set the line break style.
-     *
-     * @param lineBreakStyle the new line break style.
-     */
-    public void setLineBreakStyle(@LineBreakStyle int lineBreakStyle) {
-        mLineBreakStyle = lineBreakStyle;
-    }
-
-    /**
      * Get the line break word style.
      *
      * @return The current line break word style to be used for the text wrapping.
@@ -122,15 +179,6 @@
         return mLineBreakWordStyle;
     }
 
-    /**
-     * Set the line break word style.
-     *
-     * @param lineBreakWordStyle the new line break word style.
-     */
-    public void setLineBreakWordStyle(@LineBreakWordStyle int lineBreakWordStyle) {
-        mLineBreakWordStyle = lineBreakWordStyle;
-    }
-
     @Override
     public boolean equals(Object o) {
         if (o == null) return false;
diff --git a/keystore/java/android/security/keystore2/AndroidKeyStoreKeyPairGeneratorSpi.java b/keystore/java/android/security/keystore2/AndroidKeyStoreKeyPairGeneratorSpi.java
index 31dd10a..e7961c9 100644
--- a/keystore/java/android/security/keystore2/AndroidKeyStoreKeyPairGeneratorSpi.java
+++ b/keystore/java/android/security/keystore2/AndroidKeyStoreKeyPairGeneratorSpi.java
@@ -108,6 +108,16 @@
         }
     }
 
+    /**
+     * XDH represents Curve 25519 providers.
+     */
+    public static class XDH extends AndroidKeyStoreKeyPairGeneratorSpi {
+        // XDH is treated as EC.
+        public XDH() {
+            super(KeymasterDefs.KM_ALGORITHM_EC);
+        }
+    }
+
     /*
      * These must be kept in sync with system/security/keystore/defaults.h
      */
@@ -242,6 +252,23 @@
                 } catch (NullPointerException | IllegalArgumentException e) {
                     throw new InvalidAlgorithmParameterException(e);
                 }
+            } else if (params instanceof NamedParameterSpec) {
+                NamedParameterSpec namedSpec = (NamedParameterSpec) params;
+                // Android Keystore cannot support initialization from a NamedParameterSpec
+                // because an alias for the key is needed (a KeyGenParameterSpec cannot be
+                // constructed).
+                if (namedSpec.getName().equalsIgnoreCase(NamedParameterSpec.X25519.getName())
+                        || namedSpec.getName().equalsIgnoreCase(
+                        NamedParameterSpec.ED25519.getName())) {
+                    throw new IllegalArgumentException(
+                            "This KeyPairGenerator cannot be initialized using NamedParameterSpec."
+                                    + " use " + KeyGenParameterSpec.class.getName() + " or "
+                                    + KeyPairGeneratorSpec.class.getName());
+                } else {
+                    throw new InvalidAlgorithmParameterException(
+                            "Unsupported algorithm specified via NamedParameterSpec: "
+                            + namedSpec.getName());
+                }
             } else {
                 throw new InvalidAlgorithmParameterException(
                         "Unsupported params class: " + params.getClass().getName()
diff --git a/keystore/java/android/security/keystore2/AndroidKeyStoreProvider.java b/keystore/java/android/security/keystore2/AndroidKeyStoreProvider.java
index e5d1276..d31499e 100644
--- a/keystore/java/android/security/keystore2/AndroidKeyStoreProvider.java
+++ b/keystore/java/android/security/keystore2/AndroidKeyStoreProvider.java
@@ -83,10 +83,12 @@
         // java.security.KeyPairGenerator
         put("KeyPairGenerator.EC", PACKAGE_NAME + ".AndroidKeyStoreKeyPairGeneratorSpi$EC");
         put("KeyPairGenerator.RSA", PACKAGE_NAME +  ".AndroidKeyStoreKeyPairGeneratorSpi$RSA");
+        put("KeyPairGenerator.XDH", PACKAGE_NAME +  ".AndroidKeyStoreKeyPairGeneratorSpi$XDH");
 
         // java.security.KeyFactory
         putKeyFactoryImpl("EC");
         putKeyFactoryImpl("RSA");
+        putKeyFactoryImpl("XDH");
 
         // javax.crypto.KeyGenerator
         put("KeyGenerator.AES", PACKAGE_NAME + ".AndroidKeyStoreKeyGeneratorSpi$AES");
diff --git a/libs/WindowManager/Jetpack/src/androidx/window/extensions/embedding/TaskFragmentAnimationAdapter.java b/libs/WindowManager/Jetpack/src/androidx/window/extensions/embedding/TaskFragmentAnimationAdapter.java
index 89d7a40..b3becad 100644
--- a/libs/WindowManager/Jetpack/src/androidx/window/extensions/embedding/TaskFragmentAnimationAdapter.java
+++ b/libs/WindowManager/Jetpack/src/androidx/window/extensions/embedding/TaskFragmentAnimationAdapter.java
@@ -33,6 +33,12 @@
  * The base adapter can be used for {@link RemoteAnimationTarget} that is simple open/close.
  */
 class TaskFragmentAnimationAdapter {
+
+    /**
+     * If {@link #mOverrideLayer} is set to this value, we don't want to override the surface layer.
+     */
+    private static final int LAYER_NO_OVERRIDE = -1;
+
     final Animation mAnimation;
     final RemoteAnimationTarget mTarget;
     final SurfaceControl mLeash;
@@ -42,6 +48,7 @@
     final float[] mVecs = new float[4];
     final Rect mRect = new Rect();
     private boolean mIsFirstFrame = true;
+    private int mOverrideLayer = LAYER_NO_OVERRIDE;
 
     TaskFragmentAnimationAdapter(@NonNull Animation animation,
             @NonNull RemoteAnimationTarget target) {
@@ -58,10 +65,21 @@
         mLeash = leash;
     }
 
+    /**
+     * Surface layer to be set at the first frame of the animation. We will not set the layer if it
+     * is set to {@link #LAYER_NO_OVERRIDE}.
+     */
+    final void overrideLayer(int layer) {
+        mOverrideLayer = layer;
+    }
+
     /** Called on frame update. */
     final void onAnimationUpdate(@NonNull SurfaceControl.Transaction t, long currentPlayTime) {
         if (mIsFirstFrame) {
             t.show(mLeash);
+            if (mOverrideLayer != LAYER_NO_OVERRIDE) {
+                t.setLayer(mLeash, mOverrideLayer);
+            }
             mIsFirstFrame = false;
         }
 
diff --git a/libs/WindowManager/Jetpack/src/androidx/window/extensions/embedding/TaskFragmentAnimationRunner.java b/libs/WindowManager/Jetpack/src/androidx/window/extensions/embedding/TaskFragmentAnimationRunner.java
index 46bdf6d..1ac3317 100644
--- a/libs/WindowManager/Jetpack/src/androidx/window/extensions/embedding/TaskFragmentAnimationRunner.java
+++ b/libs/WindowManager/Jetpack/src/androidx/window/extensions/embedding/TaskFragmentAnimationRunner.java
@@ -25,6 +25,7 @@
 import static android.view.WindowManager.TRANSIT_OLD_TASK_FRAGMENT_CLOSE;
 import static android.view.WindowManager.TRANSIT_OLD_TASK_FRAGMENT_OPEN;
 import static android.view.WindowManager.TRANSIT_OLD_TASK_OPEN;
+import static android.view.WindowManagerPolicyConstants.TYPE_LAYER_OFFSET;
 
 import android.animation.Animator;
 import android.animation.ValueAnimator;
@@ -181,18 +182,22 @@
 
     private List<TaskFragmentAnimationAdapter> createOpenAnimationAdapters(
             @NonNull RemoteAnimationTarget[] targets) {
-        return createOpenCloseAnimationAdapters(targets,
+        return createOpenCloseAnimationAdapters(targets, true /* isOpening */,
                 mAnimationSpec::loadOpenAnimation);
     }
 
     private List<TaskFragmentAnimationAdapter> createCloseAnimationAdapters(
             @NonNull RemoteAnimationTarget[] targets) {
-        return createOpenCloseAnimationAdapters(targets,
+        return createOpenCloseAnimationAdapters(targets, false /* isOpening */,
                 mAnimationSpec::loadCloseAnimation);
     }
 
+    /**
+     * Creates {@link TaskFragmentAnimationAdapter} for OPEN and CLOSE types of transition.
+     * @param isOpening {@code true} for OPEN type, {@code false} for CLOSE type.
+     */
     private List<TaskFragmentAnimationAdapter> createOpenCloseAnimationAdapters(
-            @NonNull RemoteAnimationTarget[] targets,
+            @NonNull RemoteAnimationTarget[] targets, boolean isOpening,
             @NonNull BiFunction<RemoteAnimationTarget, Rect, Animation> animationProvider) {
         // We need to know if the target window is only a partial of the whole animation screen.
         // If so, we will need to adjust it to make the whole animation screen looks like one.
@@ -210,14 +215,25 @@
             }
         }
 
+        // For OPEN transition, open windows should be above close windows.
+        // For CLOSE transition, open windows should be below close windows.
+        int offsetLayer = TYPE_LAYER_OFFSET;
         final List<TaskFragmentAnimationAdapter> adapters = new ArrayList<>();
         for (RemoteAnimationTarget target : openingTargets) {
-            adapters.add(createOpenCloseAnimationAdapter(target, animationProvider,
-                    openingWholeScreenBounds));
+            final TaskFragmentAnimationAdapter adapter = createOpenCloseAnimationAdapter(target,
+                    animationProvider, openingWholeScreenBounds);
+            if (isOpening) {
+                adapter.overrideLayer(offsetLayer++);
+            }
+            adapters.add(adapter);
         }
         for (RemoteAnimationTarget target : closingTargets) {
-            adapters.add(createOpenCloseAnimationAdapter(target, animationProvider,
-                    closingWholeScreenBounds));
+            final TaskFragmentAnimationAdapter adapter = createOpenCloseAnimationAdapter(target,
+                    animationProvider, closingWholeScreenBounds);
+            if (!isOpening) {
+                adapter.overrideLayer(offsetLayer++);
+            }
+            adapters.add(adapter);
         }
         return adapters;
     }
diff --git a/libs/WindowManager/OWNERS b/libs/WindowManager/OWNERS
index 2c61df9..780e4c1 100644
--- a/libs/WindowManager/OWNERS
+++ b/libs/WindowManager/OWNERS
@@ -1,3 +1,6 @@
 set noparent
 
 include /services/core/java/com/android/server/wm/OWNERS
+
+# Give submodule owners in shell resource approval
+per-file Shell/res*/*/*.xml = hwwang@google.com, lbill@google.com, madym@google.com
diff --git a/libs/WindowManager/Shell/res/values-land/styles.xml b/libs/WindowManager/Shell/res/values-land/styles.xml
index 0ed9368..e89f65b 100644
--- a/libs/WindowManager/Shell/res/values-land/styles.xml
+++ b/libs/WindowManager/Shell/res/values-land/styles.xml
@@ -16,7 +16,7 @@
 
 <resources xmlns:android="http://schemas.android.com/apk/res/android">
     <style name="DockedDividerBackground">
-        <item name="android:layout_width">10dp</item>
+        <item name="android:layout_width">@dimen/split_divider_bar_width</item>
         <item name="android:layout_height">match_parent</item>
         <item name="android:layout_gravity">center_horizontal</item>
         <item name="android:background">@color/split_divider_background</item>
diff --git a/libs/WindowManager/Shell/res/values-television/config.xml b/libs/WindowManager/Shell/res/values-television/config.xml
index 552048e..dcb4c10 100644
--- a/libs/WindowManager/Shell/res/values-television/config.xml
+++ b/libs/WindowManager/Shell/res/values-television/config.xml
@@ -33,4 +33,10 @@
     <!-- The default gravity for the picture-in-picture window.
          Currently, this maps to Gravity.BOTTOM | Gravity.RIGHT -->
     <integer name="config_defaultPictureInPictureGravity">0x55</integer>
+
+    <!-- Fraction of screen width/height restricted keep clear areas can move the PiP. -->
+    <fraction name="config_pipMaxRestrictedMoveDistance">15%</fraction>
+
+    <!-- Duration (in milliseconds) the PiP stays stashed before automatically unstashing. -->
+    <integer name="config_pipStashDuration">5000</integer>
 </resources>
diff --git a/libs/WindowManager/Shell/res/values-television/dimen.xml b/libs/WindowManager/Shell/res/values-television/dimen.xml
new file mode 100644
index 0000000..14e89f8
--- /dev/null
+++ b/libs/WindowManager/Shell/res/values-television/dimen.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2022 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+
+<!-- These resources are around just to allow their values to be customized
+     for TV products.  Do not translate. -->
+<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
+
+    <!-- Padding between PIP and keep clear areas that caused it to move. -->
+    <dimen name="pip_keep_clear_area_padding">16dp</dimen>
+</resources>
diff --git a/libs/WindowManager/Shell/res/values/strings_tv.xml b/libs/WindowManager/Shell/res/values/strings_tv.xml
index c7b8a13..09ed9b8 100644
--- a/libs/WindowManager/Shell/res/values/strings_tv.xml
+++ b/libs/WindowManager/Shell/res/values/strings_tv.xml
@@ -39,5 +39,10 @@
 
     <!-- Button to collapse/shrink the picture-in-picture (PIP) window [CHAR LIMIT=30] -->
     <string name="pip_collapse">Collapse PIP</string>
+
+    <!-- Educative text instructing the user to double press the HOME button to access the pip
+        controls menu [CHAR LIMIT=50] -->
+    <string name="pip_edu_text"> Double press <annotation icon="home_icon"> HOME </annotation> for
+        controls </string>
 </resources>
 
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/ShellCommandHandlerImpl.java b/libs/WindowManager/Shell/src/com/android/wm/shell/ShellCommandHandlerImpl.java
index 908a31d..8483f07 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/ShellCommandHandlerImpl.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/ShellCommandHandlerImpl.java
@@ -21,6 +21,7 @@
 import com.android.wm.shell.apppairs.AppPairsController;
 import com.android.wm.shell.common.ShellExecutor;
 import com.android.wm.shell.hidedisplaycutout.HideDisplayCutoutController;
+import com.android.wm.shell.kidsmode.KidsModeTaskOrganizer;
 import com.android.wm.shell.legacysplitscreen.LegacySplitScreenController;
 import com.android.wm.shell.onehanded.OneHandedController;
 import com.android.wm.shell.pip.Pip;
@@ -46,11 +47,13 @@
     private final Optional<AppPairsController> mAppPairsOptional;
     private final Optional<RecentTasksController> mRecentTasks;
     private final ShellTaskOrganizer mShellTaskOrganizer;
+    private final KidsModeTaskOrganizer mKidsModeTaskOrganizer;
     private final ShellExecutor mMainExecutor;
     private final HandlerImpl mImpl = new HandlerImpl();
 
     public ShellCommandHandlerImpl(
             ShellTaskOrganizer shellTaskOrganizer,
+            KidsModeTaskOrganizer kidsModeTaskOrganizer,
             Optional<LegacySplitScreenController> legacySplitScreenOptional,
             Optional<SplitScreenController> splitScreenOptional,
             Optional<Pip> pipOptional,
@@ -60,6 +63,7 @@
             Optional<RecentTasksController> recentTasks,
             ShellExecutor mainExecutor) {
         mShellTaskOrganizer = shellTaskOrganizer;
+        mKidsModeTaskOrganizer = kidsModeTaskOrganizer;
         mRecentTasks = recentTasks;
         mLegacySplitScreenOptional = legacySplitScreenOptional;
         mSplitScreenOptional = splitScreenOptional;
@@ -92,6 +96,9 @@
         pw.println();
         pw.println();
         mRecentTasks.ifPresent(recentTasks -> recentTasks.dump(pw, ""));
+        pw.println();
+        pw.println();
+        mKidsModeTaskOrganizer.dump(pw, "");
     }
 
 
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/ShellInitImpl.java b/libs/WindowManager/Shell/src/com/android/wm/shell/ShellInitImpl.java
index c3ce362..b729fe1 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/ShellInitImpl.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/ShellInitImpl.java
@@ -29,6 +29,7 @@
 import com.android.wm.shell.freeform.FreeformTaskListener;
 import com.android.wm.shell.fullscreen.FullscreenTaskListener;
 import com.android.wm.shell.fullscreen.FullscreenUnfoldController;
+import com.android.wm.shell.kidsmode.KidsModeTaskOrganizer;
 import com.android.wm.shell.pip.phone.PipTouchHandler;
 import com.android.wm.shell.recents.RecentTasksController;
 import com.android.wm.shell.splitscreen.SplitScreenController;
@@ -48,6 +49,7 @@
     private final DisplayInsetsController mDisplayInsetsController;
     private final DragAndDropController mDragAndDropController;
     private final ShellTaskOrganizer mShellTaskOrganizer;
+    private final KidsModeTaskOrganizer mKidsModeTaskOrganizer;
     private final Optional<BubbleController> mBubblesOptional;
     private final Optional<SplitScreenController> mSplitScreenOptional;
     private final Optional<AppPairsController> mAppPairsOptional;
@@ -68,6 +70,7 @@
             DisplayInsetsController displayInsetsController,
             DragAndDropController dragAndDropController,
             ShellTaskOrganizer shellTaskOrganizer,
+            KidsModeTaskOrganizer kidsModeTaskOrganizer,
             Optional<BubbleController> bubblesOptional,
             Optional<SplitScreenController> splitScreenOptional,
             Optional<AppPairsController> appPairsOptional,
@@ -84,6 +87,7 @@
         mDisplayInsetsController = displayInsetsController;
         mDragAndDropController = dragAndDropController;
         mShellTaskOrganizer = shellTaskOrganizer;
+        mKidsModeTaskOrganizer = kidsModeTaskOrganizer;
         mBubblesOptional = bubblesOptional;
         mSplitScreenOptional = splitScreenOptional;
         mAppPairsOptional = appPairsOptional;
@@ -136,6 +140,9 @@
 
         mFullscreenUnfoldController.ifPresent(FullscreenUnfoldController::init);
         mRecentTasks.ifPresent(RecentTasksController::init);
+
+        // Initialize kids mode task organizer
+        mKidsModeTaskOrganizer.initialize(mStartingWindow);
     }
 
     @ExternalThread
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/ShellTaskOrganizer.java b/libs/WindowManager/Shell/src/com/android/wm/shell/ShellTaskOrganizer.java
index 3f8343a..8442994 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/ShellTaskOrganizer.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/ShellTaskOrganizer.java
@@ -196,8 +196,8 @@
     }
 
     @VisibleForTesting
-    ShellTaskOrganizer(ITaskOrganizerController taskOrganizerController, ShellExecutor mainExecutor,
-            Context context, @Nullable CompatUIController compatUI,
+    protected ShellTaskOrganizer(ITaskOrganizerController taskOrganizerController,
+            ShellExecutor mainExecutor, Context context, @Nullable CompatUIController compatUI,
             Optional<RecentTasksController> recentTasks) {
         super(taskOrganizerController, mainExecutor);
         mCompatUI = compatUI;
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/back/BackAnimationController.java b/libs/WindowManager/Shell/src/com/android/wm/shell/back/BackAnimationController.java
index 8d5fdfb..08cb252 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/back/BackAnimationController.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/back/BackAnimationController.java
@@ -52,14 +52,17 @@
  */
 public class BackAnimationController implements RemoteCallable<BackAnimationController> {
 
-    private static final String BACK_PREDICTABILITY_PROP = "persist.debug.back_predictability";
-    public static final boolean IS_ENABLED = SystemProperties
-            .getInt(BACK_PREDICTABILITY_PROP, 0) > 0;
     private static final String BACK_PREDICTABILITY_PROGRESS_THRESHOLD_PROP =
             "persist.debug.back_predictability_progress_threshold";
+    // By default, enable new back dispatching without any animations.
+    private static final int BACK_PREDICTABILITY_PROP =
+            SystemProperties.getInt("persist.debug.back_predictability", 1);
+    public static final boolean IS_ENABLED = BACK_PREDICTABILITY_PROP > 0;
     private static final int PROGRESS_THRESHOLD = SystemProperties
             .getInt(BACK_PREDICTABILITY_PROGRESS_THRESHOLD_PROP, -1);
     private static final String TAG = "BackAnimationController";
+    @VisibleForTesting
+    boolean mEnableAnimations = (BACK_PREDICTABILITY_PROP & (1 << 1)) != 0;
 
     /**
      * Location of the initial touch event of the back gesture.
@@ -255,7 +258,7 @@
                         backNavigationInfo.getTaskWindowConfiguration());
             }
             mTransaction.apply();
-        } else if (backType == BackNavigationInfo.TYPE_RETURN_TO_HOME) {
+        } else if (shouldDispatchToLauncher(backType)) {
             targetCallback = mBackToLauncherCallback;
         } else if (backType == BackNavigationInfo.TYPE_CALLBACK) {
             targetCallback = mBackNavigationInfo.getOnBackInvokedCallback();
@@ -309,7 +312,7 @@
 
         BackEvent backEvent = new BackEvent(0, 0, progress, swipeEdge, animationTarget);
         IOnBackInvokedCallback targetCallback = null;
-        if (backType == BackNavigationInfo.TYPE_RETURN_TO_HOME) {
+        if (shouldDispatchToLauncher(backType)) {
             targetCallback = mBackToLauncherCallback;
         } else if (backType == BackNavigationInfo.TYPE_CROSS_TASK
                 || backType == BackNavigationInfo.TYPE_CROSS_ACTIVITY) {
@@ -330,8 +333,7 @@
             return;
         }
         int backType = mBackNavigationInfo.getType();
-        boolean shouldDispatchToLauncher = backType == BackNavigationInfo.TYPE_RETURN_TO_HOME
-                && mBackToLauncherCallback != null;
+        boolean shouldDispatchToLauncher = shouldDispatchToLauncher(backType);
         IOnBackInvokedCallback targetCallback = shouldDispatchToLauncher
                 ? mBackToLauncherCallback
                 : mBackNavigationInfo.getOnBackInvokedCallback();
@@ -356,6 +358,17 @@
         }
     }
 
+    private boolean shouldDispatchToLauncher(int backType) {
+        return backType == BackNavigationInfo.TYPE_RETURN_TO_HOME
+                && mBackToLauncherCallback != null
+                && mEnableAnimations;
+    }
+
+    @VisibleForTesting
+    void setEnableAnimations(boolean shouldEnable) {
+        mEnableAnimations = shouldEnable;
+    }
+
     private static void dispatchOnBackStarted(IOnBackInvokedCallback callback) {
         if (callback == null) {
             return;
@@ -468,7 +481,7 @@
             return;
         }
         RemoteAnimationTarget animationTarget = backNavigationInfo.getDepartingAnimationTarget();
-        if (animationTarget != null && mTriggerBack) {
+        if (animationTarget != null) {
             if (animationTarget.leash != null && animationTarget.leash.isValid()) {
                 mTransaction.remove(animationTarget.leash);
             }
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/bubbles/BubbleController.java b/libs/WindowManager/Shell/src/com/android/wm/shell/bubbles/BubbleController.java
index d0138a4..d2a1c55 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/bubbles/BubbleController.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/bubbles/BubbleController.java
@@ -383,23 +383,15 @@
         mTaskStackListener.addListener(new TaskStackListenerCallback() {
             @Override
             public void onTaskMovedToFront(int taskId) {
-                if (mSysuiProxy == null) {
-                    return;
-                }
-
-                mSysuiProxy.isNotificationShadeExpand((expand) -> {
-                    mMainExecutor.execute(() -> {
-                        int expandedId = INVALID_TASK_ID;
-                        if (mStackView != null && mStackView.getExpandedBubble() != null
-                                && isStackExpanded() && !mStackView.isExpansionAnimating()
-                                && !expand) {
-                            expandedId = mStackView.getExpandedBubble().getTaskId();
-                        }
-
-                        if (expandedId != INVALID_TASK_ID && expandedId != taskId) {
-                            mBubbleData.setExpanded(false);
-                        }
-                    });
+                mMainExecutor.execute(() -> {
+                    int expandedId = INVALID_TASK_ID;
+                    if (mStackView != null && mStackView.getExpandedBubble() != null
+                            && isStackExpanded() && !mStackView.isExpansionAnimating()) {
+                        expandedId = mStackView.getExpandedBubble().getTaskId();
+                    }
+                    if (expandedId != INVALID_TASK_ID && expandedId != taskId) {
+                        mBubbleData.setExpanded(false);
+                    }
                 });
             }
 
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/common/DisplayController.java b/libs/WindowManager/Shell/src/com/android/wm/shell/common/DisplayController.java
index d22fb50..4ba32e9 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/common/DisplayController.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/common/DisplayController.java
@@ -110,6 +110,14 @@
     }
 
     /**
+     *  Get the InsetsState of a display.
+     */
+    public InsetsState getInsetsState(int displayId) {
+        final DisplayRecord r = mDisplays.get(displayId);
+        return r != null ? r.mInsetsState : null;
+    }
+
+    /**
      * Updates the insets for a given display.
      */
     public void updateDisplayInsets(int displayId, InsetsState state) {
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/common/SystemWindows.java b/libs/WindowManager/Shell/src/com/android/wm/shell/common/SystemWindows.java
index 51067a4..4583389 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/common/SystemWindows.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/common/SystemWindows.java
@@ -344,7 +344,7 @@
         @Override
         public void resized(ClientWindowFrames frames, boolean reportDraw,
                 MergedConfiguration newMergedConfiguration, boolean forceLayout,
-                boolean alwaysConsumeSystemBars, int displayId) {}
+                boolean alwaysConsumeSystemBars, int displayId, int syncSeqId) {}
 
         @Override
         public void insetsChanged(InsetsState insetsState, boolean willMove, boolean willResize) {}
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/common/split/SplitLayout.java b/libs/WindowManager/Shell/src/com/android/wm/shell/common/split/SplitLayout.java
index b52c8d1..5246117 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/common/split/SplitLayout.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/common/split/SplitLayout.java
@@ -98,6 +98,7 @@
     private WindowContainerToken mWinToken2;
     private int mDividePosition;
     private boolean mInitialized = false;
+    private boolean mFreezeDividerWindow = false;
     private int mOrientation;
     private int mRotation;
 
@@ -225,11 +226,6 @@
         mDividerSnapAlgorithm = getSnapAlgorithm(mContext, mRootBounds, null);
         initDividerPosition(mTempRect);
 
-        if (mInitialized) {
-            release();
-            init();
-        }
-
         return true;
     }
 
@@ -298,20 +294,37 @@
     }
 
     /** Releases the surface holding the current {@link DividerView}. */
-    public void release() {
+    public void release(SurfaceControl.Transaction t) {
         if (!mInitialized) return;
         mInitialized = false;
-        mSplitWindowManager.release();
+        mSplitWindowManager.release(t);
         mDisplayImeController.removePositionProcessor(mImePositionProcessor);
         mImePositionProcessor.reset();
     }
 
+    public void release() {
+        release(null /* t */);
+    }
+
+    /** Releases and re-inflates {@link DividerView} on the root surface. */
+    public void update(SurfaceControl.Transaction t) {
+        if (!mInitialized) return;
+        mSplitWindowManager.release(t);
+        mImePositionProcessor.reset();
+        mSplitWindowManager.init(this, mInsetsState);
+    }
+
     @Override
     public void insetsChanged(InsetsState insetsState) {
         mInsetsState.set(insetsState);
         if (!mInitialized) {
             return;
         }
+        if (mFreezeDividerWindow) {
+            // DO NOT change its layout before transition actually run because it might cause
+            // flicker.
+            return;
+        }
         mSplitWindowManager.onInsetsChanged(insetsState);
     }
 
@@ -323,6 +336,10 @@
         }
     }
 
+    public void setFreezeDividerWindow(boolean freezeDividerWindow) {
+        mFreezeDividerWindow = freezeDividerWindow;
+    }
+
     /**
      * Updates bounds with the passing position. Usually used to update recording bounds while
      * performing animation or dragging divider bar to resize the splits.
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/common/split/SplitWindowManager.java b/libs/WindowManager/Shell/src/com/android/wm/shell/common/split/SplitWindowManager.java
index 4903f9d..833d9d5 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/common/split/SplitWindowManager.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/common/split/SplitWindowManager.java
@@ -58,6 +58,9 @@
     private SurfaceControl mLeash;
     private DividerView mDividerView;
 
+    // Used to "pass" a transaction to WWM.remove so that view removal can be synchronized.
+    private SurfaceControl.Transaction mSyncTransaction = null;
+
     public interface ParentContainerCallbacks {
         void attachToParentSurface(SurfaceControl.Builder b);
         void onLeashReady(SurfaceControl leash);
@@ -130,22 +133,38 @@
      * Releases the surface control of the current {@link DividerView} and tear down the view
      * hierarchy.
      */
-    void release() {
+    void release(@Nullable SurfaceControl.Transaction t) {
         if (mDividerView != null) {
             mDividerView = null;
         }
 
         if (mViewHost != null){
+            mSyncTransaction = t;
             mViewHost.release();
+            mSyncTransaction = null;
             mViewHost = null;
         }
 
         if (mLeash != null) {
-            new SurfaceControl.Transaction().remove(mLeash).apply();
+            if (t == null) {
+                new SurfaceControl.Transaction().remove(mLeash).apply();
+            } else {
+                t.remove(mLeash);
+            }
             mLeash = null;
         }
     }
 
+    @Override
+    protected void removeSurface(SurfaceControl sc) {
+        // This gets called via SurfaceControlViewHost.release()
+        if (mSyncTransaction != null) {
+            mSyncTransaction.remove(sc);
+        } else {
+            super.removeSurface(sc);
+        }
+    }
+
     void setInteractive(boolean interactive) {
         if (mDividerView == null) return;
         mDividerView.setInteractive(interactive);
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/dagger/TvPipModule.java b/libs/WindowManager/Shell/src/com/android/wm/shell/dagger/TvPipModule.java
index 1e934c5..1dd5ebc 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/dagger/TvPipModule.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/dagger/TvPipModule.java
@@ -69,7 +69,8 @@
             TaskStackListenerImpl taskStackListener,
             DisplayController displayController,
             WindowManagerShellWrapper windowManagerShellWrapper,
-            @ShellMainThread ShellExecutor mainExecutor) {
+            @ShellMainThread ShellExecutor mainExecutor,
+            @ShellMainThread Handler mainHandler) {
         return Optional.of(
                 TvPipController.create(
                         context,
@@ -83,7 +84,8 @@
                         taskStackListener,
                         displayController,
                         windowManagerShellWrapper,
-                        mainExecutor));
+                        mainExecutor,
+                        mainHandler));
     }
 
     @WMSingleton
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/dagger/WMShellBaseModule.java b/libs/WindowManager/Shell/src/com/android/wm/shell/dagger/WMShellBaseModule.java
index 0362b3f..bf0337d 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/dagger/WMShellBaseModule.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/dagger/WMShellBaseModule.java
@@ -68,6 +68,7 @@
 import com.android.wm.shell.fullscreen.FullscreenUnfoldController;
 import com.android.wm.shell.hidedisplaycutout.HideDisplayCutout;
 import com.android.wm.shell.hidedisplaycutout.HideDisplayCutoutController;
+import com.android.wm.shell.kidsmode.KidsModeTaskOrganizer;
 import com.android.wm.shell.legacysplitscreen.LegacySplitScreen;
 import com.android.wm.shell.legacysplitscreen.LegacySplitScreenController;
 import com.android.wm.shell.onehanded.OneHanded;
@@ -184,7 +185,23 @@
 
     @WMSingleton
     @Provides
-    static Optional<CompatUI> provideCompatUI(CompatUIController compatUIController) {
+    static KidsModeTaskOrganizer provideKidsModeTaskOrganizer(
+            @ShellMainThread ShellExecutor mainExecutor,
+            @ShellMainThread Handler mainHandler,
+            Context context,
+            CompatUIController compatUI,
+            SyncTransactionQueue syncTransactionQueue,
+            DisplayController displayController,
+            DisplayInsetsController displayInsetsController,
+            Optional<RecentTasksController> recentTasksOptional
+    ) {
+        return new KidsModeTaskOrganizer(mainExecutor, mainHandler, context, compatUI,
+                syncTransactionQueue, displayController, displayInsetsController,
+                recentTasksOptional);
+    }
+
+    @WMSingleton
+    @Provides static Optional<CompatUI> provideCompatUI(CompatUIController compatUIController) {
         return Optional.of(compatUIController.asCompatUI());
     }
 
@@ -637,6 +654,7 @@
             DisplayInsetsController displayInsetsController,
             DragAndDropController dragAndDropController,
             ShellTaskOrganizer shellTaskOrganizer,
+            KidsModeTaskOrganizer kidsModeTaskOrganizer,
             Optional<BubbleController> bubblesOptional,
             Optional<SplitScreenController> splitScreenOptional,
             Optional<AppPairsController> appPairsOptional,
@@ -653,6 +671,7 @@
                 displayInsetsController,
                 dragAndDropController,
                 shellTaskOrganizer,
+                kidsModeTaskOrganizer,
                 bubblesOptional,
                 splitScreenOptional,
                 appPairsOptional,
@@ -680,6 +699,7 @@
     @Provides
     static ShellCommandHandlerImpl provideShellCommandHandlerImpl(
             ShellTaskOrganizer shellTaskOrganizer,
+            KidsModeTaskOrganizer kidsModeTaskOrganizer,
             Optional<LegacySplitScreenController> legacySplitScreenOptional,
             Optional<SplitScreenController> splitScreenOptional,
             Optional<Pip> pipOptional,
@@ -688,7 +708,7 @@
             Optional<AppPairsController> appPairsOptional,
             Optional<RecentTasksController> recentTasksOptional,
             @ShellMainThread ShellExecutor mainExecutor) {
-        return new ShellCommandHandlerImpl(shellTaskOrganizer,
+        return new ShellCommandHandlerImpl(shellTaskOrganizer, kidsModeTaskOrganizer,
                 legacySplitScreenOptional, splitScreenOptional, pipOptional, oneHandedOptional,
                 hideDisplayCutout, appPairsOptional, recentTasksOptional, mainExecutor);
     }
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/kidsmode/KidsModeTaskOrganizer.java b/libs/WindowManager/Shell/src/com/android/wm/shell/kidsmode/KidsModeTaskOrganizer.java
new file mode 100644
index 0000000..429eb99
--- /dev/null
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/kidsmode/KidsModeTaskOrganizer.java
@@ -0,0 +1,341 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.wm.shell.kidsmode;
+
+import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
+import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
+import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
+import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
+import static android.view.Display.DEFAULT_DISPLAY;
+
+import android.app.ActivityManager;
+import android.content.Context;
+import android.content.res.Configuration;
+import android.graphics.Rect;
+import android.os.Binder;
+import android.os.Handler;
+import android.os.IBinder;
+import android.view.InsetsSource;
+import android.view.InsetsState;
+import android.view.SurfaceControl;
+import android.window.ITaskOrganizerController;
+import android.window.TaskAppearedInfo;
+import android.window.WindowContainerToken;
+import android.window.WindowContainerTransaction;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.policy.ForceShowNavigationBarSettingsObserver;
+import com.android.wm.shell.ShellTaskOrganizer;
+import com.android.wm.shell.common.DisplayController;
+import com.android.wm.shell.common.DisplayInsetsController;
+import com.android.wm.shell.common.DisplayLayout;
+import com.android.wm.shell.common.ShellExecutor;
+import com.android.wm.shell.common.SyncTransactionQueue;
+import com.android.wm.shell.compatui.CompatUIController;
+import com.android.wm.shell.recents.RecentTasksController;
+import com.android.wm.shell.startingsurface.StartingWindowController;
+
+import java.io.PrintWriter;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+
+/**
+ * A dedicated task organizer when kids mode is enabled.
+ *  - Creates a root task with bounds that exclude the navigation bar area
+ *  - Launch all task into the root task except for Launcher
+ */
+public class KidsModeTaskOrganizer extends ShellTaskOrganizer {
+    private static final String TAG = "KidsModeTaskOrganizer";
+
+    private static final int[] CONTROLLED_ACTIVITY_TYPES =
+            {ACTIVITY_TYPE_UNDEFINED, ACTIVITY_TYPE_STANDARD};
+    private static final int[] CONTROLLED_WINDOWING_MODES =
+            {WINDOWING_MODE_FULLSCREEN, WINDOWING_MODE_UNDEFINED};
+
+    private final Handler mMainHandler;
+    private final Context mContext;
+    private final SyncTransactionQueue mSyncQueue;
+    private final DisplayController mDisplayController;
+    private final DisplayInsetsController mDisplayInsetsController;
+
+    @VisibleForTesting
+    ActivityManager.RunningTaskInfo mLaunchRootTask;
+    @VisibleForTesting
+    SurfaceControl mLaunchRootLeash;
+    @VisibleForTesting
+    final IBinder mCookie = new Binder();
+
+    private final InsetsState mInsetsState = new InsetsState();
+    private int mDisplayWidth;
+    private int mDisplayHeight;
+
+    private ForceShowNavigationBarSettingsObserver mForceShowNavigationBarSettingsObserver;
+    private boolean mEnabled;
+
+    DisplayController.OnDisplaysChangedListener mOnDisplaysChangedListener =
+            new DisplayController.OnDisplaysChangedListener() {
+        @Override
+        public void onDisplayConfigurationChanged(int displayId, Configuration newConfig) {
+            if (displayId != DEFAULT_DISPLAY) {
+                return;
+            }
+            final DisplayLayout displayLayout =
+                    mDisplayController.getDisplayLayout(DEFAULT_DISPLAY);
+            if (displayLayout == null) {
+                return;
+            }
+            final int displayWidth = displayLayout.width();
+            final int displayHeight = displayLayout.height();
+            if (displayWidth == mDisplayWidth || displayHeight == mDisplayHeight) {
+                return;
+            }
+            mDisplayWidth = displayWidth;
+            mDisplayHeight = displayHeight;
+            updateBounds();
+        }
+    };
+
+    DisplayInsetsController.OnInsetsChangedListener mOnInsetsChangedListener =
+            new DisplayInsetsController.OnInsetsChangedListener() {
+        @Override
+        public void insetsChanged(InsetsState insetsState) {
+            // Update bounds only when the insets of navigation bar or task bar is changed.
+            if (Objects.equals(insetsState.peekSource(InsetsState.ITYPE_NAVIGATION_BAR),
+                    mInsetsState.peekSource(InsetsState.ITYPE_NAVIGATION_BAR))
+                    && Objects.equals(insetsState.peekSource(
+                            InsetsState.ITYPE_EXTRA_NAVIGATION_BAR),
+                    mInsetsState.peekSource(InsetsState.ITYPE_EXTRA_NAVIGATION_BAR))) {
+                return;
+            }
+            mInsetsState.set(insetsState);
+            updateBounds();
+        }
+    };
+
+    @VisibleForTesting
+    KidsModeTaskOrganizer(
+            ITaskOrganizerController taskOrganizerController,
+            ShellExecutor mainExecutor,
+            Handler mainHandler,
+            Context context,
+            @Nullable CompatUIController compatUI,
+            SyncTransactionQueue syncTransactionQueue,
+            DisplayController displayController,
+            DisplayInsetsController displayInsetsController,
+            Optional<RecentTasksController> recentTasks,
+            ForceShowNavigationBarSettingsObserver forceShowNavigationBarSettingsObserver) {
+        super(taskOrganizerController, mainExecutor, context, compatUI, recentTasks);
+        mContext = context;
+        mMainHandler = mainHandler;
+        mSyncQueue = syncTransactionQueue;
+        mDisplayController = displayController;
+        mDisplayInsetsController = displayInsetsController;
+        mForceShowNavigationBarSettingsObserver = forceShowNavigationBarSettingsObserver;
+    }
+
+    public KidsModeTaskOrganizer(
+            ShellExecutor mainExecutor,
+            Handler mainHandler,
+            Context context,
+            @Nullable CompatUIController compatUI,
+            SyncTransactionQueue syncTransactionQueue,
+            DisplayController displayController,
+            DisplayInsetsController displayInsetsController,
+            Optional<RecentTasksController> recentTasks) {
+        super(mainExecutor, context, compatUI, recentTasks);
+        mContext = context;
+        mMainHandler = mainHandler;
+        mSyncQueue = syncTransactionQueue;
+        mDisplayController = displayController;
+        mDisplayInsetsController = displayInsetsController;
+    }
+
+    /**
+     * Initializes kids mode status.
+     */
+    public void initialize(StartingWindowController startingWindowController) {
+        initStartingWindow(startingWindowController);
+        if (mForceShowNavigationBarSettingsObserver == null) {
+            mForceShowNavigationBarSettingsObserver = new ForceShowNavigationBarSettingsObserver(
+                    mMainHandler, mContext);
+        }
+        mForceShowNavigationBarSettingsObserver.setOnChangeRunnable(() -> updateKidsModeState());
+        updateKidsModeState();
+        mForceShowNavigationBarSettingsObserver.register();
+    }
+
+    @Override
+    public void onTaskAppeared(ActivityManager.RunningTaskInfo taskInfo, SurfaceControl leash) {
+        if (mEnabled && mLaunchRootTask == null && taskInfo.launchCookies != null
+                && taskInfo.launchCookies.contains(mCookie)) {
+            mLaunchRootTask = taskInfo;
+            mLaunchRootLeash = leash;
+            updateTask();
+        }
+        super.onTaskAppeared(taskInfo, leash);
+
+        mSyncQueue.runInSync(t -> {
+            // Reset several properties back to fullscreen (PiP, for example, leaves all these
+            // properties in a bad state).
+            t.setCrop(leash, null);
+            t.setPosition(leash, 0, 0);
+            t.setAlpha(leash, 1f);
+            t.setMatrix(leash, 1, 0, 0, 1);
+            t.show(leash);
+        });
+    }
+
+    @Override
+    public void onTaskInfoChanged(ActivityManager.RunningTaskInfo taskInfo) {
+        if (mLaunchRootTask != null && mLaunchRootTask.taskId == taskInfo.taskId
+                && !taskInfo.equals(mLaunchRootTask)) {
+            mLaunchRootTask = taskInfo;
+        }
+
+        super.onTaskInfoChanged(taskInfo);
+    }
+
+    @VisibleForTesting
+    void updateKidsModeState() {
+        final boolean enabled = mForceShowNavigationBarSettingsObserver.isEnabled();
+        if (mEnabled == enabled) {
+            return;
+        }
+        mEnabled = enabled;
+        if (mEnabled) {
+            enable();
+        } else {
+            disable();
+        }
+    }
+
+    @VisibleForTesting
+    void enable() {
+        final DisplayLayout displayLayout = mDisplayController.getDisplayLayout(DEFAULT_DISPLAY);
+        if (displayLayout != null) {
+            mDisplayWidth = displayLayout.width();
+            mDisplayHeight = displayLayout.height();
+        }
+        mInsetsState.set(mDisplayController.getInsetsState(DEFAULT_DISPLAY));
+        mDisplayInsetsController.addInsetsChangedListener(DEFAULT_DISPLAY,
+                mOnInsetsChangedListener);
+        mDisplayController.addDisplayWindowListener(mOnDisplaysChangedListener);
+        List<TaskAppearedInfo> taskAppearedInfos = registerOrganizer();
+        for (int i = 0; i < taskAppearedInfos.size(); i++) {
+            final TaskAppearedInfo info = taskAppearedInfos.get(i);
+            onTaskAppeared(info.getTaskInfo(), info.getLeash());
+        }
+        createRootTask(DEFAULT_DISPLAY, WINDOWING_MODE_FULLSCREEN, mCookie);
+        updateTask();
+    }
+
+    @VisibleForTesting
+    void disable() {
+        mDisplayInsetsController.removeInsetsChangedListener(DEFAULT_DISPLAY,
+                mOnInsetsChangedListener);
+        mDisplayController.removeDisplayWindowListener(mOnDisplaysChangedListener);
+        updateTask();
+        final WindowContainerToken token = mLaunchRootTask.token;
+        if (token != null) {
+            deleteRootTask(token);
+        }
+        mLaunchRootTask = null;
+        mLaunchRootLeash = null;
+        unregisterOrganizer();
+    }
+
+    private void updateTask() {
+        updateTask(getWindowContainerTransaction());
+    }
+
+    private void updateTask(WindowContainerTransaction wct) {
+        if (mLaunchRootTask == null || mLaunchRootLeash == null) {
+            return;
+        }
+        final Rect taskBounds = calculateBounds();
+        final WindowContainerToken rootToken = mLaunchRootTask.token;
+        wct.setBounds(rootToken, mEnabled ? taskBounds : null);
+        wct.setLaunchRoot(rootToken,
+                mEnabled ? CONTROLLED_WINDOWING_MODES : null,
+                mEnabled ? CONTROLLED_ACTIVITY_TYPES : null);
+        wct.reparentTasks(
+                mEnabled ? null : rootToken /* currentParent */,
+                mEnabled ? rootToken : null /* newParent */,
+                CONTROLLED_WINDOWING_MODES,
+                CONTROLLED_ACTIVITY_TYPES,
+                true /* onTop */);
+        wct.reorder(rootToken, mEnabled /* onTop */);
+        mSyncQueue.queue(wct);
+        final SurfaceControl rootLeash = mLaunchRootLeash;
+        mSyncQueue.runInSync(t -> {
+            t.setPosition(rootLeash, taskBounds.left, taskBounds.top);
+            t.setWindowCrop(rootLeash, taskBounds.width(), taskBounds.height());
+        });
+    }
+
+    private Rect calculateBounds() {
+        final Rect bounds = new Rect(0, 0, mDisplayWidth, mDisplayHeight);
+        final InsetsSource navBarSource = mInsetsState.peekSource(InsetsState.ITYPE_NAVIGATION_BAR);
+        final InsetsSource taskBarSource = mInsetsState.peekSource(
+                InsetsState.ITYPE_EXTRA_NAVIGATION_BAR);
+        if (navBarSource != null && !navBarSource.getFrame().isEmpty()) {
+            bounds.inset(navBarSource.calculateInsets(bounds, false /* ignoreVisibility */));
+        } else if (taskBarSource != null && !taskBarSource.getFrame().isEmpty()) {
+            bounds.inset(taskBarSource.calculateInsets(bounds, false /* ignoreVisibility */));
+        } else {
+            bounds.setEmpty();
+        }
+        return bounds;
+    }
+
+    private void updateBounds() {
+        if (mLaunchRootTask == null) {
+            return;
+        }
+        final WindowContainerTransaction wct = getWindowContainerTransaction();
+        final Rect taskBounds = calculateBounds();
+        wct.setBounds(mLaunchRootTask.token, taskBounds);
+        mSyncQueue.queue(wct);
+        final SurfaceControl finalLeash = mLaunchRootLeash;
+        mSyncQueue.runInSync(t -> {
+            t.setPosition(finalLeash, taskBounds.left, taskBounds.top);
+            t.setWindowCrop(finalLeash, taskBounds.width(), taskBounds.height());
+        });
+    }
+
+    @VisibleForTesting
+    WindowContainerTransaction getWindowContainerTransaction() {
+        return new WindowContainerTransaction();
+    }
+
+    @Override
+    public void dump(@NonNull PrintWriter pw, String prefix) {
+        final String innerPrefix = prefix + "  ";
+        pw.println(prefix + TAG);
+        pw.println(innerPrefix + " mEnabled=" + mEnabled);
+        pw.println(innerPrefix + " mLaunchRootTask=" + mLaunchRootTask);
+        pw.println(innerPrefix + " mLaunchRootLeash=" + mLaunchRootLeash);
+        pw.println(innerPrefix + " mDisplayWidth=" + mDisplayWidth);
+        pw.println(innerPrefix + " mDisplayHeight=" + mDisplayHeight);
+        pw.println(innerPrefix + " mInsetsState=" + mInsetsState);
+        super.dump(pw, innerPrefix);
+    }
+}
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipBoundsAlgorithm.java b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipBoundsAlgorithm.java
index 797df41..c2d5823 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipBoundsAlgorithm.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipBoundsAlgorithm.java
@@ -74,7 +74,7 @@
     /**
      * TODO: move the resources to SysUI package.
      */
-    protected void reloadResources(Context context) {
+    private void reloadResources(Context context) {
         final Resources res = context.getResources();
         mDefaultAspectRatio = res.getFloat(
                 R.dimen.config_pictureInPictureDefaultAspectRatio);
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipBoundsState.java b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipBoundsState.java
index ddcd4bd..17d7f5d 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipBoundsState.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipBoundsState.java
@@ -55,11 +55,15 @@
     public static final int STASH_TYPE_NONE = 0;
     public static final int STASH_TYPE_LEFT = 1;
     public static final int STASH_TYPE_RIGHT = 2;
+    public static final int STASH_TYPE_BOTTOM = 3;
+    public static final int STASH_TYPE_TOP = 4;
 
     @IntDef(prefix = { "STASH_TYPE_" }, value =  {
             STASH_TYPE_NONE,
             STASH_TYPE_LEFT,
-            STASH_TYPE_RIGHT
+            STASH_TYPE_RIGHT,
+            STASH_TYPE_BOTTOM,
+            STASH_TYPE_TOP
     })
     @Retention(RetentionPolicy.SOURCE)
     public @interface StashType {}
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipTaskOrganizer.java b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipTaskOrganizer.java
index b266189..c1e7825 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipTaskOrganizer.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipTaskOrganizer.java
@@ -18,7 +18,6 @@
 
 import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
 import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
-import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
 import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
 import static android.util.RotationUtils.deltaRotation;
 import static android.util.RotationUtils.rotateBounds;
@@ -450,11 +449,7 @@
             tx.setWindowCrop(mLeash, destinationBounds.width(), destinationBounds.height());
             // We set to fullscreen here for now, but later it will be set to UNDEFINED for
             // the proper windowing mode to take place. See #applyWindowingModeChangeOnExit.
-            wct.setActivityWindowingMode(mToken,
-                    direction == TRANSITION_DIRECTION_LEAVE_PIP_TO_SPLIT_SCREEN
-                            && !requestEnterSplit
-                            ? WINDOWING_MODE_SPLIT_SCREEN_SECONDARY
-                            : WINDOWING_MODE_FULLSCREEN);
+            wct.setActivityWindowingMode(mToken, WINDOWING_MODE_FULLSCREEN);
             wct.setBounds(mToken, destinationBounds);
             wct.setBoundsChangeTransaction(mToken, tx);
         }
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipDismissTargetHandler.java b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipDismissTargetHandler.java
index 3115f8a..11633a9 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipDismissTargetHandler.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipDismissTargetHandler.java
@@ -253,11 +253,11 @@
     private WindowManager.LayoutParams getDismissTargetLayoutParams() {
         final Point windowSize = new Point();
         mWindowManager.getDefaultDisplay().getRealSize(windowSize);
-
+        int height = Math.min(windowSize.y, mDismissAreaHeight);
         final WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
                 WindowManager.LayoutParams.MATCH_PARENT,
-                mDismissAreaHeight,
-                0, windowSize.y - mDismissAreaHeight,
+                height,
+                0, windowSize.y - height,
                 WindowManager.LayoutParams.TYPE_NAVIGATION_BAR_PANEL,
                 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                         | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipInputConsumer.java b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipInputConsumer.java
index e57abc2..0f3ff36 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipInputConsumer.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipInputConsumer.java
@@ -148,6 +148,7 @@
         mMainExecutor.execute(() -> {
             // Choreographer.getSfInstance() must be called on the thread that the input event
             // receiver should be receiving events
+            // TODO(b/222697646): remove getSfInstance usage and use vsyncId for transactions
             mInputEventReceiver = new InputEventReceiver(inputChannel,
                 Looper.myLooper(), Choreographer.getSfInstance());
             if (mRegistrationListener != null) {
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipMotionHelper.java b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipMotionHelper.java
index f3789fd..fa0f092 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipMotionHelper.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipMotionHelper.java
@@ -95,6 +95,8 @@
                 final FrameCallbackScheduler scheduler = new FrameCallbackScheduler() {
                     @Override
                     public void postFrameCallback(@androidx.annotation.NonNull Runnable runnable) {
+                        // TODO(b/222697646): remove getSfInstance usage and use vsyncId for
+                        //  transactions
                         Choreographer.getSfInstance().postFrameCallback(t -> runnable.run());
                     }
 
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipResizeGestureHandler.java b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipResizeGestureHandler.java
index c816f18..abf1a95 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipResizeGestureHandler.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipResizeGestureHandler.java
@@ -625,6 +625,7 @@
 
     class PipResizeInputEventReceiver extends BatchedInputEventReceiver {
         PipResizeInputEventReceiver(InputChannel channel, Looper looper) {
+            // TODO(b/222697646): remove getSfInstance usage and use vsyncId for transactions
             super(channel, looper, Choreographer.getSfInstance());
         }
 
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipBoundsAlgorithm.java b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipBoundsAlgorithm.java
index 8ab78e6..d6dacd1 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipBoundsAlgorithm.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipBoundsAlgorithm.java
@@ -28,15 +28,22 @@
 import android.content.Context;
 import android.content.res.Resources;
 import android.graphics.Rect;
-import android.util.Log;
+import android.os.SystemClock;
+import android.util.ArraySet;
 import android.util.Size;
 import android.view.Gravity;
 
 import androidx.annotation.NonNull;
 
+import com.android.internal.protolog.common.ProtoLog;
+import com.android.wm.shell.R;
 import com.android.wm.shell.common.DisplayLayout;
 import com.android.wm.shell.pip.PipBoundsAlgorithm;
 import com.android.wm.shell.pip.PipSnapAlgorithm;
+import com.android.wm.shell.pip.tv.TvPipKeepClearAlgorithm.Placement;
+import com.android.wm.shell.protolog.ShellProtoLogGroup;
+
+import java.util.Set;
 
 /**
  * Contains pip bounds calculations that are specific to TV.
@@ -46,91 +53,148 @@
     private static final String TAG = TvPipBoundsAlgorithm.class.getSimpleName();
     private static final boolean DEBUG = TvPipController.DEBUG;
 
-    private final @android.annotation.NonNull TvPipBoundsState mTvPipBoundsState;
+    private final @NonNull TvPipBoundsState mTvPipBoundsState;
 
     private int mFixedExpandedHeightInPx;
     private int mFixedExpandedWidthInPx;
 
+    private final TvPipKeepClearAlgorithm mKeepClearAlgorithm;
+
     public TvPipBoundsAlgorithm(Context context,
             @NonNull TvPipBoundsState tvPipBoundsState,
             @NonNull PipSnapAlgorithm pipSnapAlgorithm) {
         super(context, tvPipBoundsState, pipSnapAlgorithm);
         this.mTvPipBoundsState = tvPipBoundsState;
+        this.mKeepClearAlgorithm = new TvPipKeepClearAlgorithm(SystemClock::uptimeMillis);
+        reloadResources(context);
     }
 
-    @Override
-    protected void reloadResources(Context context) {
-        super.reloadResources(context);
+    private void reloadResources(Context context) {
         final Resources res = context.getResources();
         mFixedExpandedHeightInPx = res.getDimensionPixelSize(
                 com.android.internal.R.dimen.config_pictureInPictureExpandedHorizontalHeight);
         mFixedExpandedWidthInPx = res.getDimensionPixelSize(
                 com.android.internal.R.dimen.config_pictureInPictureExpandedVerticalWidth);
+        mKeepClearAlgorithm.setPipAreaPadding(
+                res.getDimensionPixelSize(R.dimen.pip_keep_clear_area_padding));
+        mKeepClearAlgorithm.setMaxRestrictedDistanceFraction(
+                res.getFraction(R.fraction.config_pipMaxRestrictedMoveDistance, 1, 1));
+        mKeepClearAlgorithm.setStashDuration(res.getInteger(R.integer.config_pipStashDuration));
+    }
+
+    @Override
+    public void onConfigurationChanged(Context context) {
+        super.onConfigurationChanged(context);
+        reloadResources(context);
     }
 
     /** Returns the destination bounds to place the PIP window on entry. */
     @Override
     public Rect getEntryDestinationBounds() {
-        if (DEBUG) Log.d(TAG, "getEntryDestinationBounds()");
-        if (mTvPipBoundsState.getTvExpandedAspectRatio() != 0
-                && !mTvPipBoundsState.isTvPipManuallyCollapsed()) {
-            updatePositionOnExpandToggled(Gravity.NO_GRAVITY, true);
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: getEntryDestinationBounds()", TAG);
         }
-        return getTvPipBounds(true);
+        if (mTvPipBoundsState.isTvExpandedPipSupported()
+                && mTvPipBoundsState.getDesiredTvExpandedAspectRatio() != 0
+                && !mTvPipBoundsState.isTvPipManuallyCollapsed()) {
+            updateExpandedPipSize();
+            updateGravityOnExpandToggled(Gravity.NO_GRAVITY, true);
+            mTvPipBoundsState.setTvPipExpanded(true);
+        }
+        return getTvPipBounds().getBounds();
     }
 
     /** Returns the current bounds adjusted to the new aspect ratio, if valid. */
     @Override
     public Rect getAdjustedDestinationBounds(Rect currentBounds, float newAspectRatio) {
-        if (DEBUG) Log.d(TAG, "getAdjustedDestinationBounds: " + newAspectRatio);
-        return getTvPipBounds(mTvPipBoundsState.isTvPipExpanded());
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: getAdjustedDestinationBounds: %f", TAG, newAspectRatio);
+        }
+        return getTvPipBounds().getBounds();
     }
 
     /**
-     * The normal bounds at a different position on the screen.
+     * Calculates the PiP bounds.
      */
-    public Rect getTvNormalBounds() {
-        Rect normalBounds = getNormalBounds();
-        Rect insetBounds = new Rect();
+    public Placement getTvPipBounds() {
+        final Size pipSize = getPipSize();
+        final Rect displayBounds = mTvPipBoundsState.getDisplayBounds();
+        final Size screenSize = new Size(displayBounds.width(), displayBounds.height());
+        final Rect insetBounds = new Rect();
         getInsetBounds(insetBounds);
 
+        Set<Rect> restrictedKeepClearAreas = mTvPipBoundsState.getRestrictedKeepClearAreas();
+        Set<Rect> unrestrictedKeepClearAreas = mTvPipBoundsState.getUnrestrictedKeepClearAreas();
+
         if (mTvPipBoundsState.isImeShowing()) {
-            if (DEBUG) Log.d(TAG, "IME showing, height: " + mTvPipBoundsState.getImeHeight());
-            insetBounds.bottom -= mTvPipBoundsState.getImeHeight();
+            if (DEBUG) {
+                ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                        "%s: IME showing, height: %d",
+                        TAG, mTvPipBoundsState.getImeHeight());
+            }
+
+            final Rect imeBounds = new Rect(
+                    0,
+                    insetBounds.bottom - mTvPipBoundsState.getImeHeight(),
+                    insetBounds.right,
+                    insetBounds.bottom);
+
+            unrestrictedKeepClearAreas = new ArraySet<>(unrestrictedKeepClearAreas);
+            unrestrictedKeepClearAreas.add(imeBounds);
         }
 
-        Rect result = new Rect();
-        Gravity.apply(mTvPipBoundsState.getTvPipGravity(), normalBounds.width(),
-                normalBounds.height(), insetBounds, result);
+        mKeepClearAlgorithm.setGravity(mTvPipBoundsState.getTvPipGravity());
+        mKeepClearAlgorithm.setScreenSize(screenSize);
+        mKeepClearAlgorithm.setMovementBounds(insetBounds);
+        mKeepClearAlgorithm.setStashOffset(mTvPipBoundsState.getStashOffset());
+
+        final Placement placement = mKeepClearAlgorithm.calculatePipPosition(
+                pipSize,
+                restrictedKeepClearAreas,
+                unrestrictedKeepClearAreas);
 
         if (DEBUG) {
-            Log.d(TAG, "normalBounds: " + normalBounds.toShortString());
-            Log.d(TAG, "insetBounds: " + insetBounds.toShortString());
-            Log.d(TAG, "gravity: " + Gravity.toString(mTvPipBoundsState.getTvPipGravity()));
-            Log.d(TAG, "resultBounds: " + result.toShortString());
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: screenSize: %s", TAG, screenSize);
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: stashOffset: %d", TAG, mTvPipBoundsState.getStashOffset());
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: insetBounds: %s", TAG, insetBounds.toShortString());
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: pipSize: %s", TAG, pipSize);
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: gravity: %s", TAG, Gravity.toString(mTvPipBoundsState.getTvPipGravity()));
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: restrictedKeepClearAreas: %s", TAG, restrictedKeepClearAreas);
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: unrestrictedKeepClearAreas: %s", TAG, unrestrictedKeepClearAreas);
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: placement: %s", TAG, placement);
         }
 
-        mTvPipBoundsState.setTvPipExpanded(false);
-
-        return result;
+        return placement;
     }
 
     /**
-     * @return previous gravity if it is to be saved, or Gravity.NO_GRAVITY if not.
+     * @return previous gravity if it is to be saved, or {@link Gravity#NO_GRAVITY} if not.
      */
-    int updatePositionOnExpandToggled(int previousGravity, boolean expanding) {
+    int updateGravityOnExpandToggled(int previousGravity, boolean expanding) {
         if (DEBUG) {
-            Log.d(TAG, "updatePositionOnExpandToggle(), expanding: " + expanding
-                    + ", mOrientation: " + mTvPipBoundsState.getTvFixedPipOrientation()
-                    + ", previous gravity: " + Gravity.toString(previousGravity));
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: updateGravityOnExpandToggled(), expanding: %b"
+                    + ", mOrientation: %d, previous gravity: %s",
+                    TAG, expanding, mTvPipBoundsState.getTvFixedPipOrientation(),
+                    Gravity.toString(previousGravity));
         }
 
-        if (!mTvPipBoundsState.isTvExpandedPipEnabled()) {
+        if (!mTvPipBoundsState.isTvExpandedPipSupported()) {
             return Gravity.NO_GRAVITY;
         }
 
         if (expanding && mTvPipBoundsState.getTvFixedPipOrientation() == ORIENTATION_UNDETERMINED) {
-            float expandedRatio = mTvPipBoundsState.getTvExpandedAspectRatio();
+            float expandedRatio = mTvPipBoundsState.getDesiredTvExpandedAspectRatio();
             if (expandedRatio == 0) {
                 return Gravity.NO_GRAVITY;
             }
@@ -139,7 +203,6 @@
             } else {
                 mTvPipBoundsState.setTvFixedPipOrientation(ORIENTATION_HORIZONTAL);
             }
-
         }
 
         int gravityToSave = Gravity.NO_GRAVITY;
@@ -175,16 +238,22 @@
             }
         }
         mTvPipBoundsState.setTvPipGravity(updatedGravity);
-        if (DEBUG) Log.d(TAG, "new gravity: " + Gravity.toString(updatedGravity));
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: new gravity: %s", TAG, Gravity.toString(updatedGravity));
+        }
 
         return gravityToSave;
     }
 
     /**
-     * @return true if position changed
+     * @return true if gravity changed
      */
-    boolean updatePosition(int keycode) {
-        if (DEBUG) Log.d(TAG, "updatePosition, keycode: " + keycode);
+    boolean updateGravity(int keycode) {
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: updateGravity, keycode: %d", TAG, keycode);
+        }
 
         // Check if position change is valid
         if (mTvPipBoundsState.isTvPipExpanded()) {
@@ -241,32 +310,42 @@
 
         if (updatedGravity != currentGravity) {
             mTvPipBoundsState.setTvPipGravity(updatedGravity);
-            if (DEBUG) Log.d(TAG, "new gravity: " + Gravity.toString(updatedGravity));
+            if (DEBUG) {
+                ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                        "%s: new gravity: %s", TAG, Gravity.toString(updatedGravity));
+            }
             return true;
         }
         return false;
     }
 
+    private Size getPipSize() {
+        final boolean isExpanded =
+                mTvPipBoundsState.isTvExpandedPipSupported() && mTvPipBoundsState.isTvPipExpanded()
+                        && mTvPipBoundsState.getDesiredTvExpandedAspectRatio() != 0;
+        if (isExpanded) {
+            return mTvPipBoundsState.getTvExpandedSize();
+        } else {
+            final Rect normalBounds = getNormalBounds();
+            return new Size(normalBounds.width(), normalBounds.height());
+        }
+    }
+
     /**
-     * Calculates the PiP bounds.
+     * Updates {@link TvPipBoundsState#getTvExpandedSize()} based on
+     * {@link TvPipBoundsState#getDesiredTvExpandedAspectRatio()}, the screen size.
      */
-    public Rect getTvPipBounds(boolean expandedIfPossible) {
-        if (DEBUG) {
-            Log.d(TAG, "getExpandedBoundsIfPossible with gravity "
-                    + Gravity.toString(mTvPipBoundsState.getTvPipGravity())
-                    + ", fixed orientation: " + mTvPipBoundsState.getTvFixedPipOrientation());
-        }
+    void updateExpandedPipSize() {
+        final DisplayLayout displayLayout = mTvPipBoundsState.getDisplayLayout();
+        final float expandedRatio =
+                mTvPipBoundsState.getDesiredTvExpandedAspectRatio(); // width / height
 
-        if (!mTvPipBoundsState.isTvExpandedPipEnabled() || !expandedIfPossible) {
-            return getTvNormalBounds();
-        }
-
-        DisplayLayout displayLayout = mTvPipBoundsState.getDisplayLayout();
-        float expandedRatio = mTvPipBoundsState.getTvExpandedAspectRatio(); // width / height
-        Size expandedSize;
+        final Size expandedSize;
         if (expandedRatio == 0) {
-            Log.d(TAG, "Expanded mode not supported");
-            return getTvNormalBounds();
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                       "%s: updateExpandedPipSize(): Expanded mode aspect ratio"
+                               + " of 0 not supported", TAG);
+            return;
         } else if (expandedRatio < 1) {
             // vertical
             if (mTvPipBoundsState.getTvFixedPipOrientation() == ORIENTATION_HORIZONTAL) {
@@ -276,10 +355,16 @@
                 float aspectRatioHeight = mFixedExpandedWidthInPx / expandedRatio;
 
                 if (maxHeight > aspectRatioHeight) {
-                    if (DEBUG) Log.d(TAG, "Accommodate aspect ratio");
+                    if (DEBUG) {
+                        ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                                "%s: Accommodate aspect ratio", TAG);
+                    }
                     expandedSize = new Size(mFixedExpandedWidthInPx, (int) aspectRatioHeight);
                 } else {
-                    if (DEBUG) Log.d(TAG, "Aspect ratio is too extreme, use max size");
+                    if (DEBUG) {
+                        ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                                "%s: Aspect ratio is too extreme, use max size", TAG);
+                    }
                     expandedSize = new Size(mFixedExpandedWidthInPx, maxHeight);
                 }
             }
@@ -291,35 +376,30 @@
                 int maxWidth = displayLayout.width() - (2 * mScreenEdgeInsets.x);
                 float aspectRatioWidth = mFixedExpandedHeightInPx * expandedRatio;
                 if (maxWidth > aspectRatioWidth) {
-                    if (DEBUG) Log.d(TAG, "Accommodate aspect ratio");
+                    if (DEBUG) {
+                        ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                                "%s: Accommodate aspect ratio", TAG);
+                    }
                     expandedSize = new Size((int) aspectRatioWidth, mFixedExpandedHeightInPx);
                 } else {
-                    if (DEBUG) Log.d(TAG, "Aspect ratio is too extreme, use max size");
+                    if (DEBUG) {
+                        ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                                "%s: Aspect ratio is too extreme, use max size", TAG);
+                    }
                     expandedSize = new Size(maxWidth, mFixedExpandedHeightInPx);
                 }
             }
         }
 
-        if (expandedSize == null) {
-            return getTvNormalBounds();
-        }
-
-        if (DEBUG) {
-            Log.d(TAG, "expanded size, width: " + expandedSize.getWidth()
-                    + ", height: " + expandedSize.getHeight());
-        }
-
-        Rect insetBounds = new Rect();
-        getInsetBounds(insetBounds);
-
-        Rect expandedBounds = new Rect();
-        Gravity.apply(mTvPipBoundsState.getTvPipGravity(), expandedSize.getWidth(),
-                expandedSize.getHeight(), insetBounds, expandedBounds);
-        if (DEBUG) Log.d(TAG, "expanded bounds: " + expandedBounds.toShortString());
-
         mTvPipBoundsState.setTvExpandedSize(expandedSize);
-        mTvPipBoundsState.setTvPipExpanded(true);
-        return expandedBounds;
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                       "%s: updateExpandedPipSize(): expanded size, width: %d, height: %d",
+                    TAG, expandedSize.getWidth(), expandedSize.getHeight());
+        }
     }
 
+    void keepUnstashedForCurrentKeepClearAreas() {
+        mKeepClearAlgorithm.keepUnstashedForCurrentKeepClearAreas();
+    }
 }
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipBoundsState.java b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipBoundsState.java
index 9370e33..d880f82 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipBoundsState.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipBoundsState.java
@@ -53,10 +53,10 @@
 
     public static final int DEFAULT_TV_GRAVITY = Gravity.BOTTOM | Gravity.RIGHT;
 
-    private boolean mIsTvExpandedPipEnabled;
+    private final boolean mIsTvExpandedPipSupported;
     private boolean mIsTvPipExpanded;
     private boolean mTvPipManuallyCollapsed;
-    private float mTvExpandedAspectRatio;
+    private float mDesiredTvExpandedAspectRatio;
     private @Orientation int mTvFixedPipOrientation;
     private int mTvPipGravity;
     private @Nullable Size mTvExpandedSize;
@@ -64,8 +64,8 @@
 
     public TvPipBoundsState(@NonNull Context context) {
         super(context);
-        setIsTvExpandedPipEnabled(context.getPackageManager().hasSystemFeature(
-                PackageManager.FEATURE_EXPANDED_PICTURE_IN_PICTURE));
+        mIsTvExpandedPipSupported = context.getPackageManager().hasSystemFeature(
+                PackageManager.FEATURE_EXPANDED_PICTURE_IN_PICTURE);
     }
 
     /**
@@ -75,7 +75,7 @@
     public void setBoundsStateForEntry(ComponentName componentName, ActivityInfo activityInfo,
             PictureInPictureParams params, PipBoundsAlgorithm pipBoundsAlgorithm) {
         super.setBoundsStateForEntry(componentName, activityInfo, params, pipBoundsAlgorithm);
-        setTvExpandedAspectRatio(params.getExpandedAspectRatio(), true);
+        setDesiredTvExpandedAspectRatio(params.getExpandedAspectRatio(), true);
     }
 
     /** Resets the TV PiP state for a new activity. */
@@ -85,32 +85,32 @@
     }
 
     /** Set the tv expanded bounds of PIP */
-    public void setTvExpandedSize(@Nullable Size bounds) {
-        mTvExpandedSize = bounds;
+    public void setTvExpandedSize(@Nullable Size size) {
+        mTvExpandedSize = size;
     }
 
-    /** Get the PIP tv expanded bounds. */
+    /** Get the expanded size of the PiP. */
     @Nullable
     public Size getTvExpandedSize() {
         return mTvExpandedSize;
     }
 
     /** Set the PIP aspect ratio for the expanded PIP (TV) that is desired by the app. */
-    public void setTvExpandedAspectRatio(float aspectRatio, boolean override) {
+    public void setDesiredTvExpandedAspectRatio(float aspectRatio, boolean override) {
         if (override || mTvFixedPipOrientation == ORIENTATION_UNDETERMINED || aspectRatio == 0) {
-            mTvExpandedAspectRatio = aspectRatio;
+            mDesiredTvExpandedAspectRatio = aspectRatio;
             resetTvPipState();
             return;
         }
         if ((aspectRatio > 1 && mTvFixedPipOrientation == ORIENTATION_HORIZONTAL)
                 || (aspectRatio <= 1 && mTvFixedPipOrientation == ORIENTATION_VERTICAL)) {
-            mTvExpandedAspectRatio = aspectRatio;
+            mDesiredTvExpandedAspectRatio = aspectRatio;
         }
     }
 
     /** Get the PIP aspect ratio for the expanded PIP (TV) that is desired by the app. */
-    public float getTvExpandedAspectRatio() {
-        return mTvExpandedAspectRatio;
+    public float getDesiredTvExpandedAspectRatio() {
+        return mDesiredTvExpandedAspectRatio;
     }
 
     /** Sets the orientation the expanded TV PiP activity has been fixed to. */
@@ -154,13 +154,9 @@
         return mTvPipManuallyCollapsed;
     }
 
-    /** Sets whether expanded PiP is supported by the device. */
-    public void setIsTvExpandedPipEnabled(boolean enabled) {
-        mIsTvExpandedPipEnabled = enabled;
+    /** Returns whether expanded PiP is supported by the device. */
+    public boolean isTvExpandedPipSupported() {
+        return mIsTvExpandedPipSupported;
     }
 
-    /** Returns whether expanded PiP is supported by the device. */
-    public boolean isTvExpandedPipEnabled() {
-        return mIsTvExpandedPipEnabled;
-    }
 }
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipController.java b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipController.java
index 3c830e0..f397ac0 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipController.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipController.java
@@ -30,11 +30,11 @@
 import android.content.res.Configuration;
 import android.content.res.Resources;
 import android.graphics.Rect;
+import android.os.Handler;
 import android.os.RemoteException;
-import android.util.Log;
-import android.view.DisplayInfo;
 import android.view.Gravity;
 
+import com.android.internal.protolog.common.ProtoLog;
 import com.android.wm.shell.R;
 import com.android.wm.shell.WindowManagerShellWrapper;
 import com.android.wm.shell.common.DisplayController;
@@ -45,9 +45,12 @@
 import com.android.wm.shell.pip.PinnedStackListenerForwarder;
 import com.android.wm.shell.pip.Pip;
 import com.android.wm.shell.pip.PipAnimationController;
+import com.android.wm.shell.pip.PipBoundsState;
 import com.android.wm.shell.pip.PipMediaController;
 import com.android.wm.shell.pip.PipTaskOrganizer;
 import com.android.wm.shell.pip.PipTransitionController;
+import com.android.wm.shell.pip.tv.TvPipKeepClearAlgorithm.Placement;
+import com.android.wm.shell.protolog.ShellProtoLogGroup;
 
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
@@ -62,6 +65,7 @@
     private static final String TAG = "TvPipController";
     static final boolean DEBUG = false;
 
+    private static final double EPS = 1e-7;
     private static final int NONEXISTENT_TASK_ID = -1;
 
     @Retention(RetentionPolicy.SOURCE)
@@ -97,11 +101,13 @@
     private final TvPipNotificationController mPipNotificationController;
     private final TvPipMenuController mTvPipMenuController;
     private final ShellExecutor mMainExecutor;
+    private final Handler mMainHandler;
     private final TvPipImpl mImpl = new TvPipImpl();
 
     private @State int mState = STATE_NO_PIP;
     private int mPreviousGravity = TvPipBoundsState.DEFAULT_TV_GRAVITY;
     private int mPinnedTaskId = NONEXISTENT_TASK_ID;
+    private Runnable mUnstashRunnable;
 
     private int mResizeAnimationDuration;
 
@@ -117,7 +123,8 @@
             TaskStackListenerImpl taskStackListener,
             DisplayController displayController,
             WindowManagerShellWrapper wmShell,
-            ShellExecutor mainExecutor) {
+            ShellExecutor mainExecutor,
+            Handler mainHandler) {
         return new TvPipController(
                 context,
                 tvPipBoundsState,
@@ -130,7 +137,8 @@
                 taskStackListener,
                 displayController,
                 wmShell,
-                mainExecutor).mImpl;
+                mainExecutor,
+                mainHandler).mImpl;
     }
 
     private TvPipController(
@@ -145,9 +153,11 @@
             TaskStackListenerImpl taskStackListener,
             DisplayController displayController,
             WindowManagerShellWrapper wmShell,
-            ShellExecutor mainExecutor) {
+            ShellExecutor mainExecutor,
+            Handler mainHandler) {
         mContext = context;
         mMainExecutor = mainExecutor;
+        mMainHandler = mainHandler;
 
         mTvPipBoundsState = tvPipBoundsState;
         mTvPipBoundsState.setDisplayId(context.getDisplayId());
@@ -173,15 +183,22 @@
     }
 
     private void onConfigurationChanged(Configuration newConfig) {
-        if (DEBUG) Log.d(TAG, "onConfigurationChanged(), state=" + stateToName(mState));
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: onConfigurationChanged(), state=%s", TAG, stateToName(mState));
+        }
 
         if (isPipShown()) {
-            if (DEBUG) Log.d(TAG, "  > closing Pip.");
+            if (DEBUG) {
+                ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                        "%s:  > closing Pip.", TAG);
+            }
             closePip();
         }
 
         loadConfigurations();
         mPipNotificationController.onConfigurationChanged(mContext);
+        mTvPipBoundsAlgorithm.onConfigurationChanged(mContext);
     }
 
     /**
@@ -198,21 +215,37 @@
      */
     @Override
     public void showPictureInPictureMenu() {
-        if (DEBUG) Log.d(TAG, "showPictureInPictureMenu(), state=" + stateToName(mState));
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: showPictureInPictureMenu(), state=%s", TAG, stateToName(mState));
+        }
 
         if (mState == STATE_NO_PIP) {
-            if (DEBUG) Log.d(TAG, "  > cannot open Menu from the current state.");
+            if (DEBUG) {
+                ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                        "%s:  > cannot open Menu from the current state.", TAG);
+            }
             return;
         }
 
         setState(STATE_PIP_MENU);
-        movePinnedStack();
+        updatePinnedStackBounds();
     }
 
     @Override
     public void closeMenu() {
-        if (DEBUG) Log.d(TAG, "closeMenu(), state before=" + stateToName(mState));
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: closeMenu(), state before=%s", TAG, stateToName(mState));
+        }
         setState(STATE_PIP);
+        mTvPipBoundsAlgorithm.keepUnstashedForCurrentKeepClearAreas();
+        updatePinnedStackBounds();
+    }
+
+    @Override
+    public void onInMoveModeChanged() {
+        updatePinnedStackBounds();
     }
 
     /**
@@ -220,7 +253,10 @@
      */
     @Override
     public void movePipToFullscreen() {
-        if (DEBUG) Log.d(TAG, "movePipToFullscreen(), state=" + stateToName(mState));
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: movePipToFullscreen(), state=%s", TAG, stateToName(mState));
+        }
 
         mPipTaskOrganizer.exitPip(mResizeAnimationDuration, false /* requestEnterSplit */);
         onPipDisappeared();
@@ -228,26 +264,32 @@
 
     @Override
     public void togglePipExpansion() {
-        if (DEBUG) Log.d(TAG, "togglePipExpansion()");
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: togglePipExpansion()", TAG);
+        }
         boolean expanding = !mTvPipBoundsState.isTvPipExpanded();
         int saveGravity = mTvPipBoundsAlgorithm
-                .updatePositionOnExpandToggled(mPreviousGravity, expanding);
+                .updateGravityOnExpandToggled(mPreviousGravity, expanding);
         if (saveGravity != Gravity.NO_GRAVITY) {
             mPreviousGravity = saveGravity;
         }
         mTvPipBoundsState.setTvPipManuallyCollapsed(!expanding);
         mTvPipBoundsState.setTvPipExpanded(expanding);
-        movePinnedStack();
+        updatePinnedStackBounds();
     }
 
     @Override
     public void movePip(int keycode) {
-        if (mTvPipBoundsAlgorithm.updatePosition(keycode)) {
+        if (mTvPipBoundsAlgorithm.updateGravity(keycode)) {
             mTvPipMenuController.updateGravity(mTvPipBoundsState.getTvPipGravity());
             mPreviousGravity = Gravity.NO_GRAVITY;
-            movePinnedStack();
+            updatePinnedStackBounds();
         } else {
-            if (DEBUG) Log.d(TAG, "Position hasn't changed");
+            if (DEBUG) {
+                ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                        "%s: Position hasn't changed", TAG);
+            }
         }
     }
 
@@ -265,23 +307,57 @@
             Set<Rect> unrestricted) {
         if (mTvPipBoundsState.getDisplayId() == displayId) {
             mTvPipBoundsState.setKeepClearAreas(restricted, unrestricted);
-            movePinnedStack();
+            updatePinnedStackBounds();
         }
     }
 
     /**
-     * Animate to the updated position of the PiP based on the state and position of the PiP.
+     * Update the PiP bounds based on the state of the PiP and keep clear areas.
+     * Animates to the current PiP bounds, and schedules unstashing the PiP if necessary.
      */
-    private void movePinnedStack() {
+    private void updatePinnedStackBounds() {
         if (mState == STATE_NO_PIP) {
             return;
         }
 
-        Rect bounds = mTvPipBoundsAlgorithm.getTvPipBounds(mTvPipBoundsState.isTvPipExpanded());
-        if (DEBUG) Log.d(TAG, "movePinnedStack() - new pip bounds: " + bounds.toShortString());
+        final boolean stayAtAnchorPosition = mTvPipMenuController.isInMoveMode();
+        final boolean disallowStashing = mState == STATE_PIP_MENU || stayAtAnchorPosition;
+        final Placement placement = mTvPipBoundsAlgorithm.getTvPipBounds();
+
+        int stashType =
+                disallowStashing ? PipBoundsState.STASH_TYPE_NONE : placement.getStashType();
+        mTvPipBoundsState.setStashed(stashType);
+
+        if (stayAtAnchorPosition) {
+            movePinnedStackTo(placement.getAnchorBounds());
+        } else if (disallowStashing) {
+            movePinnedStackTo(placement.getUnstashedBounds());
+        } else {
+            movePinnedStackTo(placement.getBounds());
+        }
+
+        if (mUnstashRunnable != null) {
+            mMainHandler.removeCallbacks(mUnstashRunnable);
+            mUnstashRunnable = null;
+        }
+        if (!disallowStashing && placement.getUnstashDestinationBounds() != null) {
+            mUnstashRunnable = () -> movePinnedStackTo(placement.getUnstashDestinationBounds());
+            mMainHandler.postAtTime(mUnstashRunnable, placement.getUnstashTime());
+        }
+    }
+
+    /** Animates the PiP to the given bounds. */
+    private void movePinnedStackTo(Rect bounds) {
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: movePinnedStack() - new pip bounds: %s", TAG, bounds.toShortString());
+        }
         mPipTaskOrganizer.scheduleAnimateResizePip(bounds,
                 mResizeAnimationDuration, rect -> {
-                    if (DEBUG) Log.d(TAG, "movePinnedStack() animation done");
+                    if (DEBUG) {
+                        ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                                "%s: movePinnedStack() animation done", TAG);
+                    }
                     mTvPipMenuController.updateExpansionState();
                 });
     }
@@ -291,7 +367,10 @@
      */
     @Override
     public void closePip() {
-        if (DEBUG) Log.d(TAG, "closePip(), state=" + stateToName(mState));
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: closePip(), state=%s", TAG, stateToName(mState));
+        }
 
         removeTask(mPinnedTaskId);
         onPipDisappeared();
@@ -303,7 +382,10 @@
 
     private void checkIfPinnedTaskAppeared() {
         final TaskInfo pinnedTask = getPinnedTaskInfo();
-        if (DEBUG) Log.d(TAG, "checkIfPinnedTaskAppeared(), task=" + pinnedTask);
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: checkIfPinnedTaskAppeared(), task=%s", TAG, pinnedTask);
+        }
         if (pinnedTask == null || pinnedTask.topActivity == null) return;
         mPinnedTaskId = pinnedTask.taskId;
 
@@ -312,16 +394,23 @@
     }
 
     private void checkIfPinnedTaskIsGone() {
-        if (DEBUG) Log.d(TAG, "onTaskStackChanged()");
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: onTaskStackChanged()", TAG);
+        }
 
         if (isPipShown() && getPinnedTaskInfo() == null) {
-            Log.w(TAG, "Pinned task is gone.");
+            ProtoLog.w(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: Pinned task is gone.", TAG);
             onPipDisappeared();
         }
     }
 
     private void onPipDisappeared() {
-        if (DEBUG) Log.d(TAG, "onPipDisappeared() state=" + stateToName(mState));
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: onPipDisappeared() state=%s", TAG, stateToName(mState));
+        }
 
         mPipNotificationController.dismiss();
         mTvPipMenuController.hideMenu();
@@ -332,12 +421,18 @@
 
     @Override
     public void onPipTransitionStarted(int direction, Rect pipBounds) {
-        if (DEBUG) Log.d(TAG, "onPipTransition_Started(), state=" + stateToName(mState));
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: onPipTransition_Started(), state=%s", TAG, stateToName(mState));
+        }
     }
 
     @Override
     public void onPipTransitionCanceled(int direction) {
-        if (DEBUG) Log.d(TAG, "onPipTransition_Canceled(), state=" + stateToName(mState));
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: onPipTransition_Canceled(), state=%s", TAG, stateToName(mState));
+        }
     }
 
     @Override
@@ -345,20 +440,29 @@
         if (PipAnimationController.isInPipDirection(direction) && mState == STATE_NO_PIP) {
             setState(STATE_PIP);
         }
-        if (DEBUG) Log.d(TAG, "onPipTransition_Finished(), state=" + stateToName(mState));
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: onPipTransition_Finished(), state=%s", TAG, stateToName(mState));
+        }
     }
 
     private void setState(@State int state) {
         if (DEBUG) {
-            Log.d(TAG, "setState(), state=" + stateToName(state) + ", prev="
-                    + stateToName(mState));
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: setState(), state=%s, prev=%s",
+                    TAG, stateToName(state), stateToName(mState));
         }
         mState = state;
 
         if (mState == STATE_PIP_MENU) {
-            if (DEBUG) Log.d(TAG, "  > show menu");
+            if (DEBUG) {
+                ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                        "%s:  > show menu", TAG);
+            }
             mTvPipMenuController.showMenu();
         }
+
+        updatePinnedStackBounds();
     }
 
     private void loadConfigurations() {
@@ -366,12 +470,6 @@
         mResizeAnimationDuration = res.getInteger(R.integer.config_pipResizeAnimationDuration);
     }
 
-    private DisplayInfo getDisplayInfo() {
-        final DisplayInfo displayInfo = new DisplayInfo();
-        mContext.getDisplay().getDisplayInfo(displayInfo);
-        return displayInfo;
-    }
-
     private void registerTaskStackListenerCallback(TaskStackListenerImpl taskStackListener) {
         taskStackListener.addListener(new TaskStackListenerCallback() {
             @Override
@@ -388,7 +486,10 @@
             public void onActivityRestartAttempt(ActivityManager.RunningTaskInfo task,
                     boolean homeTaskVisible, boolean clearedTask, boolean wasVisible) {
                 if (task.getWindowingMode() == WINDOWING_MODE_PINNED) {
-                    if (DEBUG) Log.d(TAG, "onPinnedActivityRestartAttempt()");
+                    if (DEBUG) {
+                        ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                                "%s: onPinnedActivityRestartAttempt()", TAG);
+                    }
 
                     // If the "Pip-ed" Activity is launched again by Launcher or intent, make it
                     // fullscreen.
@@ -404,8 +505,9 @@
                 @Override
                 public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {
                     if (DEBUG) {
-                        Log.d(TAG, "onImeVisibilityChanged(), visible=" + imeVisible
-                                + ", height=" + imeHeight);
+                        ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                                "%s: onImeVisibilityChanged(), visible=%b, height=%d",
+                                TAG, imeVisible, imeHeight);
                     }
 
                     if (imeVisible == mTvPipBoundsState.isImeShowing()
@@ -417,62 +519,72 @@
                     mTvPipBoundsState.setImeVisibility(imeVisible, imeHeight);
 
                     if (mState != STATE_NO_PIP) {
-                        movePinnedStack();
+                        updatePinnedStackBounds();
                     }
                 }
 
                 @Override
                 public void onAspectRatioChanged(float ratio) {
-                    if (DEBUG) Log.d(TAG, "onAspectRatioChanged: " + ratio);
+                    if (DEBUG) {
+                        ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                                "%s: onAspectRatioChanged: %f", TAG, ratio);
+                    }
 
                     boolean ratioChanged = mTvPipBoundsState.getAspectRatio() != ratio;
                     mTvPipBoundsState.setAspectRatio(ratio);
 
                     if (!mTvPipBoundsState.isTvPipExpanded() && ratioChanged) {
-                        movePinnedStack();
+                        updatePinnedStackBounds();
                     }
                 }
 
                 @Override
                 public void onExpandedAspectRatioChanged(float ratio) {
-                    if (DEBUG) Log.d(TAG, "onExpandedAspectRatioChanged: " + ratio);
+                    if (DEBUG) {
+                        ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                                "%s: onExpandedAspectRatioChanged: %f", TAG, ratio);
+                    }
 
                     // 0) No update to the ratio --> don't do anything
-                    if (mTvPipBoundsState.getTvExpandedAspectRatio() == ratio) {
+
+                    if (Math.abs(mTvPipBoundsState.getDesiredTvExpandedAspectRatio() - ratio)
+                            < EPS) {
                         return;
                     }
 
-                    mTvPipBoundsState.setTvExpandedAspectRatio(ratio, false);
+                    mTvPipBoundsState.setDesiredTvExpandedAspectRatio(ratio, false);
 
                     // 1) PiP is expanded and only aspect ratio changed, but wasn't disabled
                     // --> update bounds, but don't toggle
                     if (mTvPipBoundsState.isTvPipExpanded() && ratio != 0) {
-                        movePinnedStack();
+                        mTvPipBoundsAlgorithm.updateExpandedPipSize();
+                        updatePinnedStackBounds();
                     }
 
                     // 2) PiP is expanded, but expanded PiP was disabled
                     // --> collapse PiP
                     if (mTvPipBoundsState.isTvPipExpanded() && ratio == 0) {
                         int saveGravity = mTvPipBoundsAlgorithm
-                                .updatePositionOnExpandToggled(mPreviousGravity, false);
+                                .updateGravityOnExpandToggled(mPreviousGravity, false);
                         if (saveGravity != Gravity.NO_GRAVITY) {
                             mPreviousGravity = saveGravity;
                         }
                         mTvPipBoundsState.setTvPipExpanded(false);
-                        movePinnedStack();
+                        updatePinnedStackBounds();
                     }
 
                     // 3) PiP not expanded and not manually collapsed and expand was enabled
                     // --> expand to new ratio
                     if (!mTvPipBoundsState.isTvPipExpanded() && ratio != 0
                             && !mTvPipBoundsState.isTvPipManuallyCollapsed()) {
+                        mTvPipBoundsAlgorithm.updateExpandedPipSize();
                         int saveGravity = mTvPipBoundsAlgorithm
-                                .updatePositionOnExpandToggled(mPreviousGravity, true);
+                                .updateGravityOnExpandToggled(mPreviousGravity, true);
                         if (saveGravity != Gravity.NO_GRAVITY) {
                             mPreviousGravity = saveGravity;
                         }
                         mTvPipBoundsState.setTvPipExpanded(true);
-                        movePinnedStack();
+                        updatePinnedStackBounds();
                     }
                 }
 
@@ -481,35 +593,50 @@
 
                 @Override
                 public void onActionsChanged(ParceledListSlice<RemoteAction> actions) {
-                    if (DEBUG) Log.d(TAG, "onActionsChanged()");
+                    if (DEBUG) {
+                        ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                                "%s: onActionsChanged()", TAG);
+                    }
 
                     mTvPipMenuController.setAppActions(actions);
                 }
             });
         } catch (RemoteException e) {
-            Log.e(TAG, "Failed to register pinned stack listener", e);
+            ProtoLog.e(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: Failed to register pinned stack listener, %s", TAG, e);
         }
     }
 
     private static TaskInfo getPinnedTaskInfo() {
-        if (DEBUG) Log.d(TAG, "getPinnedTaskInfo()");
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: getPinnedTaskInfo()", TAG);
+        }
         try {
             final TaskInfo taskInfo = ActivityTaskManager.getService().getRootTaskInfo(
                     WINDOWING_MODE_PINNED, ACTIVITY_TYPE_UNDEFINED);
-            if (DEBUG) Log.d(TAG, "  > taskInfo=" + taskInfo);
+            if (DEBUG) {
+                ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                        "%s:   > taskInfo=%s", TAG, taskInfo);
+            }
             return taskInfo;
         } catch (RemoteException e) {
-            Log.e(TAG, "getRootTaskInfo() failed", e);
+            ProtoLog.e(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: getRootTaskInfo() failed, %s", TAG, e);
             return null;
         }
     }
 
     private static void removeTask(int taskId) {
-        if (DEBUG) Log.d(TAG, "removeTask(), taskId=" + taskId);
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: removeTask(), taskId=%d", TAG, taskId);
+        }
         try {
             ActivityTaskManager.getService().removeTask(taskId);
         } catch (Exception e) {
-            Log.e(TAG, "Atm.removeTask() failed", e);
+            ProtoLog.e(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: Atm.removeTask() failed, %s", TAG, e);
         }
     }
 
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipKeepClearAlgorithm.kt b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipKeepClearAlgorithm.kt
new file mode 100644
index 0000000..5ac7a72
--- /dev/null
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipKeepClearAlgorithm.kt
@@ -0,0 +1,741 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.wm.shell.pip.tv
+
+import android.graphics.Point
+import android.graphics.Rect
+import android.util.Size
+import android.view.Gravity
+import com.android.wm.shell.pip.PipBoundsState
+import com.android.wm.shell.pip.PipBoundsState.STASH_TYPE_BOTTOM
+import com.android.wm.shell.pip.PipBoundsState.STASH_TYPE_LEFT
+import com.android.wm.shell.pip.PipBoundsState.STASH_TYPE_NONE
+import com.android.wm.shell.pip.PipBoundsState.STASH_TYPE_RIGHT
+import com.android.wm.shell.pip.PipBoundsState.STASH_TYPE_TOP
+import kotlin.math.abs
+import kotlin.math.max
+import kotlin.math.min
+import kotlin.math.roundToInt
+
+private const val DEFAULT_PIP_MARGINS = 48
+private const val DEFAULT_STASH_DURATION = 5000L
+private const val RELAX_DEPTH = 1
+private const val DEFAULT_MAX_RESTRICTED_DISTANCE_FRACTION = 0.15
+
+/**
+ * This class calculates an appropriate position for a Picture-In-Picture (PiP) window, taking
+ * into account app defined keep clear areas.
+ *
+ * @param clock A function returning a current timestamp (in milliseconds)
+ */
+class TvPipKeepClearAlgorithm(private val clock: () -> Long) {
+    /**
+     * Result of the positioning algorithm.
+     *
+     * @param bounds The bounds the PiP should be placed at
+     * @param anchorBounds The bounds of the PiP anchor position
+     *     (where the PiP would be placed if there were no keep clear areas)
+     * @param stashType Where the PiP has been stashed, if at all
+     * @param unstashDestinationBounds If stashed, the PiP should move to this position after
+     *     [stashDuration] has passed.
+     * @param unstashTime If stashed, the time at which the PiP should move
+     *     to [unstashDestinationBounds]
+     */
+    data class Placement(
+        val bounds: Rect,
+        val anchorBounds: Rect,
+        @PipBoundsState.StashType val stashType: Int = STASH_TYPE_NONE,
+        val unstashDestinationBounds: Rect? = null,
+        val unstashTime: Long = 0L
+    ) {
+        /** Bounds to use if the PiP should not be stashed. */
+        fun getUnstashedBounds() = unstashDestinationBounds ?: bounds
+    }
+
+    /**  The size of the screen */
+    private var screenSize = Size(0, 0)
+
+    /** The bounds the PiP is allowed to move in */
+    private var movementBounds = Rect()
+
+    /** Padding to add between a keep clear area that caused the PiP to move and the PiP */
+    var pipAreaPadding = DEFAULT_PIP_MARGINS
+
+    /** The distance the PiP peeks into the screen when stashed */
+    var stashOffset = DEFAULT_PIP_MARGINS
+
+    /**
+     * How long (in milliseconds) the PiP should stay stashed for after the last time the
+     * keep clear areas causing the PiP to stash have changed.
+     */
+    var stashDuration = DEFAULT_STASH_DURATION
+
+    /** The fraction of screen width/height restricted keep clear areas can move the PiP */
+    var maxRestrictedDistanceFraction = DEFAULT_MAX_RESTRICTED_DISTANCE_FRACTION
+
+    private var pipGravity = Gravity.BOTTOM or Gravity.RIGHT
+    private var transformedScreenBounds = Rect()
+    private var transformedMovementBounds = Rect()
+
+    private var lastAreasOverlappingUnstashPosition: Set<Rect> = emptySet()
+    private var lastStashTime: Long = Long.MIN_VALUE
+
+    /**
+     * Calculates the position the PiP should be placed at, taking into consideration the
+     * given keep clear areas.
+     *
+     * Restricted keep clear areas can move the PiP only by a limited amount, and may be ignored
+     * if there is no space for the PiP to move to.
+     * Apps holding the permission [android.Manifest.permission.USE_UNRESTRICTED_KEEP_CLEAR_AREAS]
+     * can declare unrestricted keep clear areas, which can move the PiP farther and placement will
+     * always try to respect these areas.
+     *
+     * If no free space the PiP is allowed to move to can be found, a stashed position is returned
+     * as [Placement.bounds], along with a position to move to once [Placement.unstashTime] has
+     * passed as [Placement.unstashDestinationBounds].
+     *
+     * @param pipSize The size of the PiP window
+     * @param restrictedAreas The restricted keep clear areas
+     * @param unrestrictedAreas The unrestricted keep clear areas
+     *
+     */
+    fun calculatePipPosition(
+        pipSize: Size,
+        restrictedAreas: Set<Rect>,
+        unrestrictedAreas: Set<Rect>
+    ): Placement {
+        val transformedRestrictedAreas = transformAndFilterAreas(restrictedAreas)
+        val transformedUnrestrictedAreas = transformAndFilterAreas(unrestrictedAreas)
+        val pipAnchorBounds = getNormalPipAnchorBounds(pipSize, transformedMovementBounds)
+
+        val result = calculatePipPositionTransformed(
+            pipAnchorBounds,
+            transformedRestrictedAreas,
+            transformedUnrestrictedAreas
+        )
+
+        val screenSpaceBounds = fromTransformedSpace(result.bounds)
+        return Placement(
+            screenSpaceBounds,
+            fromTransformedSpace(result.anchorBounds),
+            getStashType(screenSpaceBounds, movementBounds),
+            result.unstashDestinationBounds?.let { fromTransformedSpace(it) },
+            result.unstashTime
+        )
+    }
+
+    /**
+     * Filters out areas that encompass the entire movement bounds and returns them mapped to
+     * the base case space.
+     *
+     * Areas encompassing the entire movement bounds can occur when a full-screen View gets focused,
+     * but we don't want this to cause the PiP to get stashed.
+     */
+    private fun transformAndFilterAreas(areas: Set<Rect>): Set<Rect> {
+        return areas.mapNotNullTo(mutableSetOf()) {
+            when {
+                it.contains(movementBounds) -> null
+                else -> toTransformedSpace(it)
+            }
+        }
+    }
+
+    /**
+     * Calculates the position the PiP should be placed at, taking into consideration the
+     * given keep clear areas.
+     * All parameters are transformed from screen space to the base case space, where the PiP
+     * anchor is in the bottom right corner / on the right side.
+     *
+     * @see [calculatePipPosition]
+     */
+    private fun calculatePipPositionTransformed(
+        pipAnchorBounds: Rect,
+        restrictedAreas: Set<Rect>,
+        unrestrictedAreas: Set<Rect>
+    ): Placement {
+        if (restrictedAreas.isEmpty() && unrestrictedAreas.isEmpty()) {
+            return Placement(pipAnchorBounds, pipAnchorBounds)
+        }
+
+        // First try to find a free position to move to
+        val freeMovePos = findFreeMovePosition(pipAnchorBounds, restrictedAreas, unrestrictedAreas)
+        if (freeMovePos != null) {
+            lastAreasOverlappingUnstashPosition = emptySet()
+            return Placement(freeMovePos, pipAnchorBounds)
+        }
+
+        // If no free position is found, we have to stash the PiP.
+        // Find the position the PiP should return to once it unstashes by doing a relaxed
+        // search, or ignoring restricted areas, or returning to the anchor position
+        val unstashBounds =
+            findRelaxedMovePosition(pipAnchorBounds, restrictedAreas, unrestrictedAreas)
+                ?: findFreeMovePosition(pipAnchorBounds, emptySet(), unrestrictedAreas)
+                ?: pipAnchorBounds
+
+        val keepClearAreas = restrictedAreas + unrestrictedAreas
+        val areasOverlappingUnstashPosition =
+            keepClearAreas.filter { Rect.intersects(it, unstashBounds) }.toSet()
+        val areasOverlappingUnstashPositionChanged =
+            !lastAreasOverlappingUnstashPosition.containsAll(areasOverlappingUnstashPosition)
+        lastAreasOverlappingUnstashPosition = areasOverlappingUnstashPosition
+
+        val now = clock()
+        if (areasOverlappingUnstashPositionChanged) {
+            lastStashTime = now
+        }
+
+        // If overlapping areas haven't changed and the stash duration has passed, we can
+        // place the PiP at the unstash position
+        val unstashTime = lastStashTime + stashDuration
+        if (now >= unstashTime) {
+            return Placement(unstashBounds, pipAnchorBounds)
+        }
+
+        // Otherwise, we'll stash it close to the unstash position
+        val stashedBounds = getNearbyStashedPosition(unstashBounds, keepClearAreas)
+        return Placement(
+            stashedBounds,
+            pipAnchorBounds,
+            getStashType(stashedBounds, transformedMovementBounds),
+            unstashBounds,
+            unstashTime
+        )
+    }
+
+    @PipBoundsState.StashType
+    private fun getStashType(stashedBounds: Rect, movementBounds: Rect): Int {
+        return when {
+            stashedBounds.left < movementBounds.left -> STASH_TYPE_LEFT
+            stashedBounds.right > movementBounds.right -> STASH_TYPE_RIGHT
+            stashedBounds.top < movementBounds.top -> STASH_TYPE_TOP
+            stashedBounds.bottom > movementBounds.bottom -> STASH_TYPE_BOTTOM
+            else -> STASH_TYPE_NONE
+        }
+    }
+
+    private fun findRelaxedMovePosition(
+        pipAnchorBounds: Rect,
+        restrictedAreas: Set<Rect>,
+        unrestrictedAreas: Set<Rect>
+    ): Rect? {
+        if (RELAX_DEPTH <= 0) {
+            // relaxed search disabled
+            return null
+        }
+
+        return findRelaxedMovePosition(
+            RELAX_DEPTH,
+            pipAnchorBounds,
+            restrictedAreas.toMutableSet(),
+            unrestrictedAreas
+        )
+    }
+
+    private fun findRelaxedMovePosition(
+        depth: Int,
+        pipAnchorBounds: Rect,
+        restrictedAreas: MutableSet<Rect>,
+        unrestrictedAreas: Set<Rect>
+    ): Rect? {
+        if (depth == 0) {
+            return findFreeMovePosition(pipAnchorBounds, restrictedAreas, unrestrictedAreas)
+        }
+
+        val candidates = mutableListOf<Rect>()
+        val areasToExclude = restrictedAreas.toList()
+        for (area in areasToExclude) {
+            restrictedAreas.remove(area)
+            val candidate = findRelaxedMovePosition(
+                depth - 1,
+                pipAnchorBounds,
+                restrictedAreas,
+                unrestrictedAreas
+            )
+            restrictedAreas.add(area)
+
+            if (candidate != null) {
+                candidates.add(candidate)
+            }
+        }
+        return candidates.minByOrNull { candidateCost(it, pipAnchorBounds) }
+    }
+
+    /** Cost function to evaluate candidate bounds */
+    private fun candidateCost(candidateBounds: Rect, pipAnchorBounds: Rect): Int {
+        // squared euclidean distance of corresponding rect corners
+        val dx = candidateBounds.left - pipAnchorBounds.left
+        val dy = candidateBounds.top - pipAnchorBounds.top
+        return dx * dx + dy * dy
+    }
+
+    private fun findFreeMovePosition(
+        pipAnchorBounds: Rect,
+        restrictedAreas: Set<Rect>,
+        unrestrictedAreas: Set<Rect>
+    ): Rect? {
+        val movementBounds = transformedMovementBounds
+        val candidateEdgeRects = mutableListOf<Rect>()
+        val minRestrictedLeft =
+            pipAnchorBounds.right - screenSize.width * maxRestrictedDistanceFraction
+
+        candidateEdgeRects.add(
+            movementBounds.offsetCopy(movementBounds.width() + pipAreaPadding, 0)
+        )
+        candidateEdgeRects.addAll(unrestrictedAreas)
+        candidateEdgeRects.addAll(restrictedAreas.filter { it.left >= minRestrictedLeft })
+
+        // throw out edges that are too close to the left screen edge to fit the PiP
+        val minLeft = movementBounds.left + pipAnchorBounds.width()
+        candidateEdgeRects.retainAll { it.left - pipAreaPadding > minLeft }
+        candidateEdgeRects.sortBy { -it.left }
+
+        val maxRestrictedDY = (screenSize.height * maxRestrictedDistanceFraction).roundToInt()
+
+        val candidateBounds = mutableListOf<Rect>()
+        for (edgeRect in candidateEdgeRects) {
+            val edge = edgeRect.left - pipAreaPadding
+            val dx = (edge - pipAnchorBounds.width()) - pipAnchorBounds.left
+            val candidatePipBounds = pipAnchorBounds.offsetCopy(dx, 0)
+            val searchUp = true
+            val searchDown = !isPipAnchoredToCorner()
+
+            if (searchUp) {
+                val event = findMinMoveUp(candidatePipBounds, restrictedAreas, unrestrictedAreas)
+                val padding = if (event.start) 0 else pipAreaPadding
+                val dy = event.pos - pipAnchorBounds.bottom - padding
+                val maxDY = if (event.unrestricted) movementBounds.height() else maxRestrictedDY
+                val candidate = pipAnchorBounds.offsetCopy(dx, dy)
+                val isOnScreen = candidate.top > movementBounds.top
+                val hangingMidAir = !candidate.intersectsY(edgeRect)
+                if (isOnScreen && abs(dy) <= maxDY && !hangingMidAir) {
+                    candidateBounds.add(candidate)
+                }
+            }
+
+            if (searchDown) {
+                val event = findMinMoveDown(candidatePipBounds, restrictedAreas, unrestrictedAreas)
+                val padding = if (event.start) 0 else pipAreaPadding
+                val dy = event.pos - pipAnchorBounds.top + padding
+                val maxDY = if (event.unrestricted) movementBounds.height() else maxRestrictedDY
+                val candidate = pipAnchorBounds.offsetCopy(dx, dy)
+                val isOnScreen = candidate.bottom < movementBounds.bottom
+                val hangingMidAir = !candidate.intersectsY(edgeRect)
+                if (isOnScreen && abs(dy) <= maxDY && !hangingMidAir) {
+                    candidateBounds.add(candidate)
+                }
+            }
+        }
+
+        candidateBounds.sortBy { candidateCost(it, pipAnchorBounds) }
+        return candidateBounds.firstOrNull()
+    }
+
+    private fun getNearbyStashedPosition(bounds: Rect, keepClearAreas: Set<Rect>): Rect {
+        val screenBounds = transformedScreenBounds
+        val stashCandidates = Array(2) { Rect(bounds) }
+        val areasOverlappingPipX = keepClearAreas.filter { it.intersectsX(bounds) }
+        val areasOverlappingPipY = keepClearAreas.filter { it.intersectsY(bounds) }
+
+        if (screenBounds.bottom - bounds.bottom <= bounds.top - screenBounds.top) {
+            // bottom is closer than top, stash downwards
+            val fullStashTop = screenBounds.bottom - stashOffset
+
+            val maxBottom = areasOverlappingPipX.maxByOrNull { it.bottom }!!.bottom
+            val partialStashTop = maxBottom + pipAreaPadding
+
+            val downPosition = stashCandidates[0]
+            downPosition.offsetTo(bounds.left, min(fullStashTop, partialStashTop))
+        } else {
+            // top is closer than bottom, stash upwards
+            val fullStashY = screenBounds.top - bounds.height() + stashOffset
+
+            val minTop = areasOverlappingPipX.minByOrNull { it.top }!!.top
+            val partialStashY = minTop - bounds.height() - pipAreaPadding
+
+            val upPosition = stashCandidates[0]
+            upPosition.offsetTo(bounds.left, max(fullStashY, partialStashY))
+        }
+
+        if (screenBounds.right - bounds.right <= bounds.left - screenBounds.left) {
+            // right is closer than left, stash rightwards
+            val fullStashLeft = screenBounds.right - stashOffset
+
+            val maxRight = areasOverlappingPipY.maxByOrNull { it.right }!!.right
+            val partialStashLeft = maxRight + pipAreaPadding
+
+            val rightPosition = stashCandidates[1]
+            rightPosition.offsetTo(min(fullStashLeft, partialStashLeft), bounds.top)
+        } else {
+            // left is closer than right, stash leftwards
+            val fullStashLeft = screenBounds.left - bounds.width() + stashOffset
+
+            val minLeft = areasOverlappingPipY.minByOrNull { it.left }!!.left
+            val partialStashLeft = minLeft - bounds.width() - pipAreaPadding
+
+            val rightPosition = stashCandidates[1]
+            rightPosition.offsetTo(max(fullStashLeft, partialStashLeft), bounds.top)
+        }
+
+        return stashCandidates.minByOrNull {
+            val dx = abs(it.left - bounds.left)
+            val dy = abs(it.top - bounds.top)
+            dx * bounds.height() + dy * bounds.width()
+        }!!
+    }
+
+    /**
+     * Prevents the PiP from being stashed for the current set of keep clear areas.
+     * The PiP may stash again if keep clear areas change.
+     */
+    fun keepUnstashedForCurrentKeepClearAreas() {
+        lastStashTime = Long.MIN_VALUE
+    }
+
+    /**
+     * Updates the size of the screen.
+     *
+     * @param size The new size of the screen
+     */
+    fun setScreenSize(size: Size) {
+        if (screenSize == size) {
+            return
+        }
+
+        screenSize = size
+        transformedScreenBounds =
+            toTransformedSpace(Rect(0, 0, screenSize.width, screenSize.height))
+        transformedMovementBounds = toTransformedSpace(transformedMovementBounds)
+    }
+
+    /**
+     * Updates the bounds within which the PiP is allowed to move.
+     *
+     * @param bounds The new movement bounds
+     */
+    fun setMovementBounds(bounds: Rect) {
+        if (movementBounds == bounds) {
+            return
+        }
+
+        movementBounds.set(bounds)
+        transformedMovementBounds = toTransformedSpace(movementBounds)
+    }
+
+    /**
+     * Sets the corner/side of the PiP's home position.
+     */
+    fun setGravity(gravity: Int) {
+        if (pipGravity == gravity) return
+
+        pipGravity = gravity
+        transformedScreenBounds =
+            toTransformedSpace(Rect(0, 0, screenSize.width, screenSize.height))
+        transformedMovementBounds = toTransformedSpace(movementBounds)
+    }
+
+    /**
+     * @param open Whether this event marks the opening of an occupied segment
+     * @param pos The coordinate of this event
+     * @param unrestricted Whether this event was generated by an unrestricted keep clear area
+     * @param start Marks the special start event. Earlier events are skipped when sweeping
+     */
+    data class SweepLineEvent(
+        val open: Boolean,
+        val pos: Int,
+        val unrestricted: Boolean,
+        val start: Boolean = false
+    )
+
+    /**
+     * Returns a [SweepLineEvent] representing the minimal move up from [pipBounds] that clears
+     * the given keep clear areas.
+     */
+    private fun findMinMoveUp(
+        pipBounds: Rect,
+        restrictedAreas: Set<Rect>,
+        unrestrictedAreas: Set<Rect>
+    ): SweepLineEvent {
+        val events = mutableListOf<SweepLineEvent>()
+        val generateEvents: (Boolean) -> (Rect) -> Unit = { unrestricted ->
+            { area ->
+                if (pipBounds.intersectsX(area)) {
+                    events.add(SweepLineEvent(true, area.bottom, unrestricted))
+                    events.add(SweepLineEvent(false, area.top, unrestricted))
+                }
+            }
+        }
+
+        restrictedAreas.forEach(generateEvents(false))
+        unrestrictedAreas.forEach(generateEvents(true))
+
+        return sweepLineFindEarliestGap(
+            events,
+            pipBounds.height() + pipAreaPadding,
+            pipBounds.bottom,
+            pipBounds.height()
+        )
+    }
+
+    /**
+     * Returns a [SweepLineEvent] representing the minimal move down from [pipBounds] that clears
+     * the given keep clear areas.
+     */
+    private fun findMinMoveDown(
+        pipBounds: Rect,
+        restrictedAreas: Set<Rect>,
+        unrestrictedAreas: Set<Rect>
+    ): SweepLineEvent {
+        val events = mutableListOf<SweepLineEvent>()
+        val generateEvents: (Boolean) -> (Rect) -> Unit = { unrestricted ->
+            { area ->
+                if (pipBounds.intersectsX(area)) {
+                    events.add(SweepLineEvent(true, -area.top, unrestricted))
+                    events.add(SweepLineEvent(false, -area.bottom, unrestricted))
+                }
+            }
+        }
+
+        restrictedAreas.forEach(generateEvents(false))
+        unrestrictedAreas.forEach(generateEvents(true))
+
+        val earliestEvent = sweepLineFindEarliestGap(
+            events,
+            pipBounds.height() + pipAreaPadding,
+            -pipBounds.top,
+            pipBounds.height()
+        )
+
+        return earliestEvent.copy(pos = -earliestEvent.pos)
+    }
+
+    /**
+     * Takes a list of events representing the starts & ends of occupied segments, and
+     * returns the earliest event whose position is unoccupied and has [gapSize] distance to the
+     * next event.
+     *
+     * @param events List of [SweepLineEvent] representing occupied segments
+     * @param gapSize Size of the gap to search for
+     * @param startPos The position to start the search on.
+     *     Inserts a special event marked with [SweepLineEvent.start].
+     * @param startGapSize Used instead of [gapSize] for the start event
+     */
+    private fun sweepLineFindEarliestGap(
+        events: MutableList<SweepLineEvent>,
+        gapSize: Int,
+        startPos: Int,
+        startGapSize: Int
+    ): SweepLineEvent {
+        events.add(
+            SweepLineEvent(
+                open = false,
+                pos = startPos,
+                unrestricted = true,
+                start = true
+            )
+        )
+        events.sortBy { -it.pos }
+
+        // sweep
+        var openCount = 0
+        var i = 0
+        while (i < events.size) {
+            val event = events[i]
+            if (!event.start) {
+                if (event.open) {
+                    openCount++
+                } else {
+                    openCount--
+                }
+            }
+
+            if (openCount == 0) {
+                // check if placement is possible
+                val candidate = event.pos
+                if (candidate > startPos) {
+                    i++
+                    continue
+                }
+
+                val eventGapSize = if (event.start) startGapSize else gapSize
+                val nextEvent = events.getOrNull(i + 1)
+                if (nextEvent == null || nextEvent.pos < candidate - eventGapSize) {
+                    return event
+                }
+            }
+            i++
+        }
+
+        return events.last()
+    }
+
+    private fun shouldTransformFlipX(): Boolean {
+        return when (pipGravity) {
+            (Gravity.TOP), (Gravity.TOP or Gravity.CENTER_HORIZONTAL) -> true
+            (Gravity.TOP or Gravity.LEFT) -> true
+            (Gravity.LEFT), (Gravity.LEFT or Gravity.CENTER_VERTICAL) -> true
+            (Gravity.BOTTOM or Gravity.LEFT) -> true
+            else -> false
+        }
+    }
+
+    private fun shouldTransformFlipY(): Boolean {
+        return when (pipGravity) {
+            (Gravity.TOP or Gravity.LEFT) -> true
+            (Gravity.TOP or Gravity.RIGHT) -> true
+            else -> false
+        }
+    }
+
+    private fun shouldTransformRotate(): Boolean {
+        val horizontalGravity = pipGravity and Gravity.HORIZONTAL_GRAVITY_MASK
+        val leftOrRight = horizontalGravity == Gravity.LEFT || horizontalGravity == Gravity.RIGHT
+
+        if (leftOrRight) return false
+        return when (pipGravity and Gravity.VERTICAL_GRAVITY_MASK) {
+            (Gravity.TOP) -> true
+            (Gravity.BOTTOM) -> true
+            else -> false
+        }
+    }
+
+    /**
+     * Transforms the given rect from screen space into the base case space, where the PiP
+     * anchor is positioned in the bottom right corner or on the right side (for expanded PiP).
+     *
+     * @see [fromTransformedSpace]
+     */
+    private fun toTransformedSpace(r: Rect): Rect {
+        var screenWidth = screenSize.width
+        var screenHeight = screenSize.height
+
+        val tl = Point(r.left, r.top)
+        val tr = Point(r.right, r.top)
+        val br = Point(r.right, r.bottom)
+        val bl = Point(r.left, r.bottom)
+        val corners = arrayOf(tl, tr, br, bl)
+
+        // rotate first (CW)
+        if (shouldTransformRotate()) {
+            corners.forEach { p ->
+                val px = p.x
+                val py = p.y
+                p.x = py
+                p.y = -px
+                p.y += screenWidth // shift back screen into positive quadrant
+            }
+            screenWidth = screenSize.height
+            screenHeight = screenSize.width
+        }
+
+        // flip second
+        corners.forEach {
+            if (shouldTransformFlipX()) it.x = screenWidth - it.x
+            if (shouldTransformFlipY()) it.y = screenHeight - it.y
+        }
+
+        val top = corners.minByOrNull { it.y }!!.y
+        val right = corners.maxByOrNull { it.x }!!.x
+        val bottom = corners.maxByOrNull { it.y }!!.y
+        val left = corners.minByOrNull { it.x }!!.x
+
+        return Rect(left, top, right, bottom)
+    }
+
+    /**
+     * Transforms the given rect from the base case space, where the PiP anchor is positioned in
+     * the bottom right corner or on the right side, back into screen space.
+     *
+     * @see [toTransformedSpace]
+     */
+    private fun fromTransformedSpace(r: Rect): Rect {
+        val rotate = shouldTransformRotate()
+        val transformedScreenWidth = if (rotate) screenSize.height else screenSize.width
+        val transformedScreenHeight = if (rotate) screenSize.width else screenSize.height
+
+        val tl = Point(r.left, r.top)
+        val tr = Point(r.right, r.top)
+        val br = Point(r.right, r.bottom)
+        val bl = Point(r.left, r.bottom)
+        val corners = arrayOf(tl, tr, br, bl)
+
+        // flip first
+        corners.forEach {
+            if (shouldTransformFlipX()) it.x = transformedScreenWidth - it.x
+            if (shouldTransformFlipY()) it.y = transformedScreenHeight - it.y
+        }
+
+        // rotate second (CCW)
+        if (rotate) {
+            corners.forEach { p ->
+                p.y -= screenSize.width // undo shift back screen into positive quadrant
+                val px = p.x
+                val py = p.y
+                p.x = -py
+                p.y = px
+            }
+        }
+
+        val top = corners.minByOrNull { it.y }!!.y
+        val right = corners.maxByOrNull { it.x }!!.x
+        val bottom = corners.maxByOrNull { it.y }!!.y
+        val left = corners.minByOrNull { it.x }!!.x
+
+        return Rect(left, top, right, bottom)
+    }
+
+    /** PiP anchor bounds in base case for given gravity */
+    private fun getNormalPipAnchorBounds(pipSize: Size, movementBounds: Rect): Rect {
+        var size = pipSize
+        val rotateCW = shouldTransformRotate()
+        if (rotateCW) {
+            size = Size(pipSize.height, pipSize.width)
+        }
+
+        val pipBounds = Rect()
+        if (isPipAnchoredToCorner()) {
+            // bottom right
+            Gravity.apply(
+                Gravity.BOTTOM or Gravity.RIGHT,
+                size.width,
+                size.height,
+                movementBounds,
+                pipBounds
+            )
+            return pipBounds
+        } else {
+            // expanded, right side
+            Gravity.apply(Gravity.RIGHT, size.width, size.height, movementBounds, pipBounds)
+            return pipBounds
+        }
+    }
+
+    private fun isPipAnchoredToCorner(): Boolean {
+        val left = (pipGravity and Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.LEFT
+        val right = (pipGravity and Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.RIGHT
+        val top = (pipGravity and Gravity.VERTICAL_GRAVITY_MASK) == Gravity.TOP
+        val bottom = (pipGravity and Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM
+
+        val horizontal = left || right
+        val vertical = top || bottom
+
+        return horizontal && vertical
+    }
+
+    private fun Rect.offsetCopy(dx: Int, dy: Int) = Rect(this).apply { offset(dx, dy) }
+    private fun Rect.intersectsY(other: Rect) = bottom >= other.top && top <= other.bottom
+    private fun Rect.intersectsX(other: Rect) = right >= other.left && left <= other.right
+}
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipMenuController.java b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipMenuController.java
index 32ebe2d..1a035c5 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipMenuController.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipMenuController.java
@@ -30,17 +30,18 @@
 import android.graphics.RectF;
 import android.os.Handler;
 import android.os.RemoteException;
-import android.util.Log;
 import android.view.SurfaceControl;
 import android.view.SyncRtSurfaceTransactionApplier;
 import android.view.WindowManagerGlobal;
 
 import androidx.annotation.Nullable;
 
+import com.android.internal.protolog.common.ProtoLog;
 import com.android.wm.shell.R;
 import com.android.wm.shell.common.SystemWindows;
 import com.android.wm.shell.pip.PipMediaController;
 import com.android.wm.shell.pip.PipMenuController;
+import com.android.wm.shell.protolog.ShellProtoLogGroup;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -110,7 +111,10 @@
     }
 
     void setDelegate(Delegate delegate) {
-        if (DEBUG) Log.d(TAG, "setDelegate(), delegate=" + delegate);
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: setDelegate(), delegate=%s", TAG, delegate);
+        }
         if (mDelegate != null) {
             throw new IllegalStateException(
                     "The delegate has already been set and should not change.");
@@ -133,7 +137,10 @@
     }
 
     private void attachPipMenuView() {
-        if (DEBUG) Log.d(TAG, "attachPipMenuView()");
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: attachPipMenuView()", TAG);
+        }
 
         if (mPipMenuView != null) {
             detachPipMenuView();
@@ -148,7 +155,10 @@
 
     @Override
     public void showMenu() {
-        if (DEBUG) Log.d(TAG, "showMenu()");
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: showMenu()", TAG);
+        }
 
         if (mPipMenuView != null) {
             Rect menuBounds = getMenuBounds(mTvPipBoundsState.getBounds());
@@ -174,8 +184,8 @@
     }
 
     void updateExpansionState() {
-        mPipMenuView.setExpandedModeEnabled(mTvPipBoundsState.isTvExpandedPipEnabled()
-                && mTvPipBoundsState.getTvExpandedAspectRatio() != 0);
+        mPipMenuView.setExpandedModeEnabled(mTvPipBoundsState.isTvExpandedPipSupported()
+                && mTvPipBoundsState.getDesiredTvExpandedAspectRatio() != 0);
         mPipMenuView.setIsExpanded(mTvPipBoundsState.isTvPipExpanded());
     }
 
@@ -189,10 +199,16 @@
 
     void hideMenu() {
         if (!isMenuVisible()) {
-            if (DEBUG) Log.d(TAG, "hideMenu() - Menu isn't visible, so don't hide");
+            if (DEBUG) {
+                ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                        "%s: hideMenu() - Menu isn't visible, so don't hide", TAG);
+            }
             return;
         } else {
-            if (DEBUG) Log.d(TAG, "hideMenu()");
+            if (DEBUG) {
+                ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                        "%s: hideMenu()", TAG);
+            }
         }
 
         mPipMenuView.hide();
@@ -202,21 +218,33 @@
         }
     }
 
+    boolean isInMoveMode() {
+        return mInMoveMode;
+    }
+
     @Override
     public void onEnterMoveMode() {
-        if (DEBUG) Log.d(TAG, "onEnterMoveMode - " + mInMoveMode);
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: onEnterMoveMode - %b", TAG, mInMoveMode);
+        }
         mInMoveMode = true;
         mPipMenuView.showMenuButtons(false);
         mPipMenuView.showMovementHints(mDelegate.getPipGravity());
+        mDelegate.onInMoveModeChanged();
     }
 
     @Override
     public boolean onExitMoveMode() {
-        if (DEBUG) Log.d(TAG, "onExitMoveMode - " + mInMoveMode);
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: onExitMoveMode - %b", TAG, mInMoveMode);
+        }
         if (mInMoveMode) {
             mInMoveMode = false;
             mPipMenuView.showMenuButtons(true);
             mPipMenuView.hideMovementHints();
+            mDelegate.onInMoveModeChanged();
             return true;
         }
         return false;
@@ -224,7 +252,10 @@
 
     @Override
     public boolean onPipMovement(int keycode) {
-        if (DEBUG) Log.d(TAG, "onPipMovement - " + mInMoveMode);
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: onPipMovement - %b", TAG, mInMoveMode);
+        }
         if (mInMoveMode) {
             mDelegate.movePip(keycode);
         }
@@ -240,12 +271,18 @@
 
     @Override
     public void setAppActions(ParceledListSlice<RemoteAction> actions) {
-        if (DEBUG) Log.d(TAG, "setAppActions()");
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: setAppActions()", TAG);
+        }
         updateAdditionalActionsList(mAppActions, actions.getList());
     }
 
     private void onMediaActionsChanged(List<RemoteAction> actions) {
-        if (DEBUG) Log.d(TAG, "onMediaActionsChanged()");
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: onMediaActionsChanged()", TAG);
+        }
 
         // Hide disabled actions.
         List<RemoteAction> enabledActions = new ArrayList<>();
@@ -286,7 +323,10 @@
     @Override
     public boolean isMenuVisible() {
         boolean isVisible = mPipMenuView != null && mPipMenuView.isVisible();
-        if (DEBUG) Log.d(TAG, "isMenuVisible: " + isVisible);
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: isMenuVisible: %b", TAG, isVisible);
+        }
         return isVisible;
     }
 
@@ -297,7 +337,10 @@
     public void resizePipMenu(@android.annotation.Nullable SurfaceControl pipLeash,
             @android.annotation.Nullable SurfaceControl.Transaction t,
             Rect destinationBounds) {
-        if (DEBUG) Log.d(TAG, "resizePipMenu: " + destinationBounds.toShortString());
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: resizePipMenu: %s", TAG, destinationBounds.toShortString());
+        }
         if (destinationBounds.isEmpty()) {
             return;
         }
@@ -329,10 +372,16 @@
     @Override
     public void movePipMenu(SurfaceControl pipLeash, SurfaceControl.Transaction transaction,
             Rect pipDestBounds) {
-        if (DEBUG) Log.d(TAG, "movePipMenu: " + pipDestBounds.toShortString());
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: movePipMenu: %s", TAG, pipDestBounds.toShortString());
+        }
 
         if (pipDestBounds.isEmpty()) {
-            if (transaction == null && DEBUG) Log.d(TAG, "no transaction given");
+            if (transaction == null && DEBUG) {
+                ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                        "%s: no transaction given", TAG);
+            }
             return;
         }
         if (!maybeCreateSyncApplier()) {
@@ -345,10 +394,16 @@
         // resizing and the PiP menu is also resized. We then want to do a scale from the current
         // new menu bounds.
         if (pipLeash != null && transaction != null) {
-            if (DEBUG) Log.d(TAG, "mTmpSourceBounds based on mPipMenuView.getBoundsOnScreen()");
+            if (DEBUG) {
+                ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                        "%s: mTmpSourceBounds based on mPipMenuView.getBoundsOnScreen()", TAG);
+            }
             mPipMenuView.getBoundsOnScreen(mTmpSourceBounds);
         } else {
-            if (DEBUG) Log.d(TAG, "mTmpSourceBounds based on menu width and height");
+            if (DEBUG) {
+                ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                        "%s: mTmpSourceBounds based on menu width and height", TAG);
+            }
             mTmpSourceBounds.set(0, 0, menuDestBounds.width(), menuDestBounds.height());
         }
 
@@ -383,7 +438,8 @@
 
     private boolean maybeCreateSyncApplier() {
         if (mPipMenuView == null || mPipMenuView.getViewRootImpl() == null) {
-            Log.v(TAG, "Not going to move PiP, either menu or its parent is not created.");
+            ProtoLog.v(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: Not going to move PiP, either menu or its parent is not created.", TAG);
             return false;
         }
 
@@ -406,7 +462,10 @@
     @Override
     public void updateMenuBounds(Rect destinationBounds) {
         Rect menuBounds = getMenuBounds(destinationBounds);
-        if (DEBUG) Log.d(TAG, "updateMenuBounds: " + menuBounds.toShortString());
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: updateMenuBounds: %s", TAG, menuBounds.toShortString());
+        }
         mSystemWindows.updateViewLayout(mPipMenuView,
                 getPipMenuLayoutParams(MENU_WINDOW_TITLE, menuBounds.width(),
                         menuBounds.height()));
@@ -417,7 +476,7 @@
 
     @Override
     public void onFocusTaskChanged(ActivityManager.RunningTaskInfo taskInfo) {
-        Log.d(TAG, "onFocusTaskChanged");
+        ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE, "%s: onFocusTaskChanged", TAG);
     }
 
     @Override
@@ -447,6 +506,8 @@
 
         void movePip(int keycode);
 
+        void onInMoveModeChanged();
+
         int getPipGravity();
 
         void togglePipExpansion();
@@ -457,13 +518,17 @@
     }
 
     private void grantPipMenuFocus(boolean grantFocus) {
-        if (DEBUG) Log.d(TAG, "grantWindowFocus(" + grantFocus + ")");
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: grantWindowFocus(%b)", TAG, grantFocus);
+        }
 
         try {
             WindowManagerGlobal.getWindowSession().grantEmbeddedWindowFocus(null /* window */,
                     mSystemWindows.getFocusGrantToken(mPipMenuView), grantFocus);
         } catch (Exception e) {
-            Log.e(TAG, "Unable to update focus", e);
+            ProtoLog.e(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: Unable to update focus, %s", TAG, e);
         }
     }
 }
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipMenuView.java b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipMenuView.java
index 3090139..984dea2 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipMenuView.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipMenuView.java
@@ -31,7 +31,6 @@
 import android.graphics.Rect;
 import android.os.Handler;
 import android.util.AttributeSet;
-import android.util.Log;
 import android.view.Gravity;
 import android.view.KeyEvent;
 import android.view.SurfaceControl;
@@ -45,7 +44,9 @@
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
 
+import com.android.internal.protolog.common.ProtoLog;
 import com.android.wm.shell.R;
+import com.android.wm.shell.protolog.ShellProtoLogGroup;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -118,17 +119,24 @@
     }
 
     void updateLayout(Rect updatedBounds) {
-        Log.d(TAG, "update menu layout: " + updatedBounds.toShortString());
+        ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                "%s: update menu layout: %s", TAG, updatedBounds.toShortString());
         boolean previouslyVertical =
                 mCurrentBounds != null && mCurrentBounds.height() > mCurrentBounds.width();
         boolean vertical = updatedBounds.height() > updatedBounds.width();
 
         mCurrentBounds = updatedBounds;
         if (previouslyVertical == vertical) {
-            if (DEBUG) Log.d(TAG, "no update for menu layout");
+            if (DEBUG) {
+                ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                        "%s: no update for menu layout", TAG);
+            }
             return;
         } else {
-            if (DEBUG) Log.d(TAG, "change menu layout to vertical: " + vertical);
+            if (DEBUG) {
+                ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                        "%s: change menu layout to vertical: %b", TAG, vertical);
+            }
         }
 
         if (vertical) {
@@ -154,7 +162,10 @@
     }
 
     void setIsExpanded(boolean expanded) {
-        if (DEBUG) Log.d(TAG, "setIsExpanded, expanded: " + expanded);
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: setIsExpanded, expanded: %b", TAG, expanded);
+        }
         mExpandButton.setImageResource(
                 expanded ? R.drawable.pip_ic_collapse : R.drawable.pip_ic_expand);
         mExpandButton.setTextAndDescription(
@@ -162,7 +173,10 @@
     }
 
     void show(boolean inMoveMode, int gravity) {
-        if (DEBUG) Log.d(TAG, "show(), inMoveMode: " + inMoveMode);
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: show(), inMoveMode: %b", TAG, inMoveMode);
+        }
         if (inMoveMode) {
             showMovementHints(gravity);
         } else {
@@ -172,7 +186,9 @@
     }
 
     void hide() {
-        if (DEBUG) Log.d(TAG, "hide()");
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE, "%s: hide()", TAG);
+        }
         animateAlphaTo(0, mActionButtonsContainer);
         animateAlphaTo(0, mMenuFrameView);
         hideMovementHints();
@@ -205,7 +221,10 @@
     }
 
     void setAdditionalActions(List<RemoteAction> actions, Handler mainHandler) {
-        if (DEBUG) Log.d(TAG, "setAdditionalActions()");
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: setAdditionalActions()", TAG);
+        }
 
         // Make sure we exactly as many additional buttons as we have actions to display.
         final int actionsNumber = actions.size();
@@ -278,10 +297,12 @@
                 try {
                     action.getActionIntent().send();
                 } catch (PendingIntent.CanceledException e) {
-                    Log.w(TAG, "Failed to send action", e);
+                    ProtoLog.w(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                            "%s: Failed to send action, %s", TAG, e);
                 }
             } else {
-                Log.w(TAG, "RemoteAction is null");
+                ProtoLog.w(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                        "%s: RemoteAction is null", TAG);
             }
         }
     }
@@ -289,8 +310,9 @@
     @Override
     public boolean dispatchKeyEvent(KeyEvent event) {
         if (DEBUG) {
-            Log.d(TAG, "dispatchKeyEvent, action: " + event.getAction()
-                    + ", keycode: " + event.getKeyCode());
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: dispatchKeyEvent, action: %d, keycode: %d",
+                    TAG, event.getAction(), event.getKeyCode());
         }
         if (mListener != null && event.getAction() == ACTION_UP) {
             switch (event.getKeyCode()) {
@@ -317,7 +339,10 @@
      * Shows user hints for moving the PiP, e.g. arrows.
      */
     public void showMovementHints(int gravity) {
-        if (DEBUG) Log.d(TAG, "showMovementHints(), position: " + Gravity.toString(gravity));
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: showMovementHints(), position: %s", TAG, Gravity.toString(gravity));
+        }
 
         animateAlphaTo(checkGravity(gravity, Gravity.BOTTOM) ? 1f : 0f, mArrowUp);
         animateAlphaTo(checkGravity(gravity, Gravity.TOP) ? 1f : 0f, mArrowDown);
@@ -333,7 +358,10 @@
      * Hides user hints for moving the PiP, e.g. arrows.
      */
     public void hideMovementHints() {
-        if (DEBUG) Log.d(TAG, "hideMovementHints()");
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: hideMovementHints()", TAG);
+        }
         animateAlphaTo(0, mArrowUp);
         animateAlphaTo(0, mArrowRight);
         animateAlphaTo(0, mArrowDown);
@@ -344,7 +372,10 @@
      * Show or hide the pip user actions.
      */
     public void showMenuButtons(boolean show) {
-        if (DEBUG) Log.d(TAG, "showMenuButtons: " + show);
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: showMenuButtons: %b", TAG, show);
+        }
         animateAlphaTo(show ? 1 : 0, mActionButtonsContainer);
     }
 
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipNotificationController.java b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipNotificationController.java
index dd7e294..7bd3ce9 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipNotificationController.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipNotificationController.java
@@ -28,13 +28,13 @@
 import android.graphics.Bitmap;
 import android.media.MediaMetadata;
 import android.os.Handler;
-import android.os.UserHandle;
 import android.text.TextUtils;
-import android.util.Log;
 
 import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
+import com.android.internal.protolog.common.ProtoLog;
 import com.android.wm.shell.R;
 import com.android.wm.shell.pip.PipMediaController;
+import com.android.wm.shell.protolog.ShellProtoLogGroup;
 
 import java.util.Objects;
 
@@ -98,7 +98,10 @@
     }
 
     void setDelegate(Delegate delegate) {
-        if (DEBUG) Log.d(TAG, "setDelegate(), delegate=" + delegate);
+        if (DEBUG) {
+            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                    "%s: setDelegate(), delegate=%s", TAG, delegate);
+        }
         if (mDelegate != null) {
             throw new IllegalStateException(
                     "The delegate has already been set and should not change.");
@@ -240,7 +243,10 @@
         @Override
         public void onReceive(Context context, Intent intent) {
             final String action = intent.getAction();
-            if (DEBUG) Log.d(TAG, "on(Broadcast)Receive(), action=" + action);
+            if (DEBUG) {
+                ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
+                        "%s: on(Broadcast)Receive(), action=%s", TAG, action);
+            }
 
             if (ACTION_SHOW_PIP_MENU.equals(action)) {
                 mDelegate.showPictureInPictureMenu();
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/splitscreen/StageCoordinator.java b/libs/WindowManager/Shell/src/com/android/wm/shell/splitscreen/StageCoordinator.java
index 1a5e3f2..ec1ddf0 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/splitscreen/StageCoordinator.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/splitscreen/StageCoordinator.java
@@ -29,6 +29,7 @@
 import static android.view.WindowManager.TRANSIT_TO_FRONT;
 import static android.view.WindowManager.transitTypeToString;
 import static android.view.WindowManagerPolicyConstants.SPLIT_DIVIDER_LAYER;
+import static android.window.TransitionInfo.FLAG_IS_DISPLAY;
 
 import static com.android.wm.shell.common.split.SplitScreenConstants.SPLIT_POSITION_BOTTOM_OR_RIGHT;
 import static com.android.wm.shell.common.split.SplitScreenConstants.SPLIT_POSITION_TOP_OR_LEFT;
@@ -1117,9 +1118,9 @@
             return SPLIT_POSITION_UNDEFINED;
         }
 
-        if (token.equals(mMainStage.mRootTaskInfo.getToken())) {
+        if (mMainStage.containsToken(token)) {
             return getMainStagePosition();
-        } else if (token.equals(mSideStage.mRootTaskInfo.getToken())) {
+        } else if (mSideStage.containsToken(token)) {
             return getSideStagePosition();
         }
 
@@ -1171,6 +1172,8 @@
                 updateUnfoldBounds();
                 return;
             }
+
+            mSplitLayout.update(null /* t */);
             onLayoutSizeChanged(mSplitLayout);
         }
     }
@@ -1198,7 +1201,6 @@
         if (!ENABLE_SHELL_TRANSITIONS) return;
 
         final SurfaceControl.Transaction t = mTransactionPool.acquire();
-        setDividerVisibility(false, t);
         mDisplayLayout.rotateTo(mContext.getResources(), toRotation);
         mSplitLayout.rotateTo(toRotation, mDisplayLayout.stableInsets());
         updateWindowBounds(mSplitLayout, wct);
@@ -1255,8 +1257,15 @@
             @Nullable TransitionRequestInfo request) {
         final ActivityManager.RunningTaskInfo triggerTask = request.getTriggerTask();
         if (triggerTask == null) {
-            // Still want to monitor everything while in split-screen, so return non-null.
-            return mMainStage.isActive() ? new WindowContainerTransaction() : null;
+            if (mMainStage.isActive()) {
+                if (request.getType() == TRANSIT_CHANGE && request.getDisplayChange() != null) {
+                    mSplitLayout.setFreezeDividerWindow(true);
+                }
+                // Still want to monitor everything while in split-screen, so return non-null.
+                return new WindowContainerTransaction();
+            } else {
+                return null;
+            }
         } else if (triggerTask.displayId != mDisplayId) {
             // Skip handling task on the other display.
             return null;
@@ -1329,8 +1338,11 @@
         // Once the pending enter transition got merged, make sure to bring divider bar visible and
         // clear the pending transition from cache to prevent mess-up the following state.
         if (transition == mSplitTransitions.mPendingEnter) {
-            finishEnterSplitScreen(null);
+            final SurfaceControl.Transaction t = mTransactionPool.acquire();
+            finishEnterSplitScreen(t);
             mSplitTransitions.mPendingEnter = null;
+            t.apply();
+            mTransactionPool.release(t);
         }
     }
 
@@ -1349,8 +1361,14 @@
             // If we're not in split-mode, just abort so something else can handle it.
             if (!mMainStage.isActive()) return false;
 
+            mSplitLayout.setFreezeDividerWindow(false);
             for (int iC = 0; iC < info.getChanges().size(); ++iC) {
                 final TransitionInfo.Change change = info.getChanges().get(iC);
+                if (change.getMode() == TRANSIT_CHANGE
+                        && (change.getFlags() & FLAG_IS_DISPLAY) != 0) {
+                    mSplitLayout.update(startTransaction);
+                }
+
                 final ActivityManager.RunningTaskInfo taskInfo = change.getTaskInfo();
                 if (taskInfo == null || !taskInfo.hasParentTask()) continue;
                 final StageTaskListener stage = getStageOfTask(taskInfo);
@@ -1365,10 +1383,6 @@
                         Log.w(TAG, "Expected onTaskVanished on " + stage + " to have been called"
                                 + " with " + taskInfo.taskId + " before startAnimation().");
                     }
-                } else if (info.getType() == TRANSIT_CHANGE
-                        && change.getStartRotation() != change.getEndRotation()) {
-                    // Show the divider after transition finished.
-                    setDividerVisibility(true, finishTransaction);
                 }
             }
             if (mMainStage.getChildCount() == 0 || mSideStage.getChildCount() == 0) {
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/splitscreen/StageTaskListener.java b/libs/WindowManager/Shell/src/com/android/wm/shell/splitscreen/StageTaskListener.java
index 109f96e..5f0cd01 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/splitscreen/StageTaskListener.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/splitscreen/StageTaskListener.java
@@ -124,6 +124,20 @@
         return mChildrenTaskInfo.contains(taskId);
     }
 
+    boolean containsToken(WindowContainerToken token) {
+        if (token.equals(mRootTaskInfo.token)) {
+            return true;
+        }
+
+        for (int i = mChildrenTaskInfo.size() - 1; i >= 0; --i) {
+            if (token.equals(mChildrenTaskInfo.valueAt(i).token)) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
     /**
      * Returns the top visible child task's id.
      */
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/startingsurface/TaskSnapshotWindow.java b/libs/WindowManager/Shell/src/com/android/wm/shell/startingsurface/TaskSnapshotWindow.java
index 1bef552e..49f907b 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/startingsurface/TaskSnapshotWindow.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/startingsurface/TaskSnapshotWindow.java
@@ -62,6 +62,7 @@
 import android.graphics.Rect;
 import android.graphics.RectF;
 import android.hardware.HardwareBuffer;
+import android.os.Bundle;
 import android.os.IBinder;
 import android.os.RemoteException;
 import android.os.Trace;
@@ -243,7 +244,7 @@
             Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "TaskSnapshot#relayout");
             session.relayout(window, layoutParams, -1, -1, View.VISIBLE, 0,
                     tmpFrames, tmpMergedConfiguration, surfaceControl, tmpInsetsState,
-                    tmpControls);
+                    tmpControls, new Bundle());
             Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
         } catch (RemoteException e) {
             snapshotSurface.clearWindowSynced();
@@ -517,7 +518,7 @@
 
     private void reportDrawn() {
         try {
-            mSession.finishDrawing(mWindow, null /* postDrawTransaction */);
+            mSession.finishDrawing(mWindow, null /* postDrawTransaction */, Integer.MAX_VALUE);
         } catch (RemoteException e) {
             clearWindowSynced();
         }
@@ -534,7 +535,7 @@
         @Override
         public void resized(ClientWindowFrames frames, boolean reportDraw,
                 MergedConfiguration mergedConfiguration, boolean forceLayout,
-                boolean alwaysConsumeSystemBars, int displayId) {
+                boolean alwaysConsumeSystemBars, int displayId, int seqId) {
             if (mOuter != null) {
                 mOuter.mSplashScreenExecutor.execute(() -> {
                     if (mergedConfiguration != null
diff --git a/libs/WindowManager/Shell/tests/flicker/src/com/android/wm/shell/flicker/bubble/LaunchBubbleFromLockScreen.kt b/libs/WindowManager/Shell/tests/flicker/src/com/android/wm/shell/flicker/bubble/LaunchBubbleFromLockScreen.kt
index 61e27f2..fb404b9 100644
--- a/libs/WindowManager/Shell/tests/flicker/src/com/android/wm/shell/flicker/bubble/LaunchBubbleFromLockScreen.kt
+++ b/libs/WindowManager/Shell/tests/flicker/src/com/android/wm/shell/flicker/bubble/LaunchBubbleFromLockScreen.kt
@@ -16,6 +16,7 @@
 
 package com.android.wm.shell.flicker.bubble
 
+import android.platform.test.annotations.Presubmit
 import androidx.test.filters.FlakyTest
 import androidx.test.filters.RequiresDevice
 import androidx.test.uiautomator.By
@@ -24,6 +25,8 @@
 import com.android.server.wm.flicker.FlickerTestParameter
 import com.android.server.wm.flicker.annotation.Group4
 import com.android.server.wm.flicker.dsl.FlickerBuilder
+import com.android.server.wm.flicker.helpers.isShellTransitionsEnabled
+import org.junit.Assume
 import org.junit.runner.RunWith
 import org.junit.Test
 import org.junit.runners.Parameterized
@@ -69,9 +72,19 @@
             }
         }
 
-    @FlakyTest
+    @Presubmit
     @Test
     fun testAppIsVisibleAtEnd() {
+        Assume.assumeFalse(isShellTransitionsEnabled)
+        testSpec.assertLayersEnd {
+            this.isVisible(testApp.component)
+        }
+    }
+
+    @FlakyTest
+    @Test
+    fun testAppIsVisibleAtEnd_ShellTransit() {
+        Assume.assumeTrue(isShellTransitionsEnabled)
         testSpec.assertLayersEnd {
             this.isVisible(testApp.component)
         }
diff --git a/libs/WindowManager/Shell/tests/flicker/src/com/android/wm/shell/flicker/bubble/LaunchBubbleScreen.kt b/libs/WindowManager/Shell/tests/flicker/src/com/android/wm/shell/flicker/bubble/LaunchBubbleScreen.kt
index a57d3e6..c43230e 100644
--- a/libs/WindowManager/Shell/tests/flicker/src/com/android/wm/shell/flicker/bubble/LaunchBubbleScreen.kt
+++ b/libs/WindowManager/Shell/tests/flicker/src/com/android/wm/shell/flicker/bubble/LaunchBubbleScreen.kt
@@ -16,6 +16,7 @@
 
 package com.android.wm.shell.flicker.bubble
 
+import android.platform.test.annotations.Presubmit
 import androidx.test.filters.FlakyTest
 import android.platform.test.annotations.RequiresDevice
 import com.android.server.wm.flicker.FlickerParametersRunnerFactory
@@ -57,9 +58,19 @@
             }
         }
 
-    @FlakyTest(bugId = 218642026)
+    @Presubmit
     @Test
     open fun testAppIsAlwaysVisible() {
+        Assume.assumeFalse(isShellTransitionsEnabled)
+        testSpec.assertLayers {
+            this.isVisible(testApp.component)
+        }
+    }
+
+    @FlakyTest(bugId = 218642026)
+    @Test
+    open fun testAppIsAlwaysVisible_ShellTransit() {
+        Assume.assumeTrue(isShellTransitionsEnabled)
         testSpec.assertLayers {
             this.isVisible(testApp.component)
         }
diff --git a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/back/BackAnimationControllerTest.java b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/back/BackAnimationControllerTest.java
index 9054685..3e7ee25 100644
--- a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/back/BackAnimationControllerTest.java
+++ b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/back/BackAnimationControllerTest.java
@@ -78,6 +78,7 @@
         MockitoAnnotations.initMocks(this);
         mController = new BackAnimationController(
                 mShellExecutor, mTransaction, mActivityTaskManager, mContext);
+        mController.setEnableAnimations(true);
     }
 
     private void createNavigationInfo(RemoteAnimationTarget topAnimationTarget,
diff --git a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/common/split/SplitWindowManagerTests.java b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/common/split/SplitWindowManagerTests.java
index 9bb54a1..2e5078d 100644
--- a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/common/split/SplitWindowManagerTests.java
+++ b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/common/split/SplitWindowManagerTests.java
@@ -61,7 +61,7 @@
     public void testInitRelease() {
         mSplitWindowManager.init(mSplitLayout, new InsetsState());
         assertThat(mSplitWindowManager.getSurfaceControl()).isNotNull();
-        mSplitWindowManager.release();
+        mSplitWindowManager.release(null /* t */);
         assertThat(mSplitWindowManager.getSurfaceControl()).isNull();
     }
 }
diff --git a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/kidsmode/KidsModeTaskOrganizerTest.java b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/kidsmode/KidsModeTaskOrganizerTest.java
new file mode 100644
index 0000000..78903dc
--- /dev/null
+++ b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/kidsmode/KidsModeTaskOrganizerTest.java
@@ -0,0 +1,149 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.wm.shell.kidsmode;
+
+import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
+import static android.view.Display.DEFAULT_DISPLAY;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import android.app.ActivityManager;
+import android.content.Context;
+import android.content.pm.ParceledListSlice;
+import android.os.Handler;
+import android.os.IBinder;
+import android.os.RemoteException;
+import android.view.InsetsState;
+import android.view.SurfaceControl;
+import android.window.ITaskOrganizerController;
+import android.window.TaskAppearedInfo;
+import android.window.WindowContainerToken;
+import android.window.WindowContainerTransaction;
+
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+import androidx.test.filters.SmallTest;
+
+import com.android.internal.policy.ForceShowNavigationBarSettingsObserver;
+import com.android.wm.shell.common.DisplayController;
+import com.android.wm.shell.common.DisplayInsetsController;
+import com.android.wm.shell.common.ShellExecutor;
+import com.android.wm.shell.common.SyncTransactionQueue;
+import com.android.wm.shell.compatui.CompatUIController;
+import com.android.wm.shell.startingsurface.StartingWindowController;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.util.ArrayList;
+import java.util.Optional;
+
+@SmallTest
+@RunWith(AndroidJUnit4.class)
+public class KidsModeTaskOrganizerTest {
+    @Mock private ITaskOrganizerController mTaskOrganizerController;
+    @Mock private Context mContext;
+    @Mock private Handler mHandler;
+    @Mock private CompatUIController mCompatUI;
+    @Mock private SyncTransactionQueue mSyncTransactionQueue;
+    @Mock private ShellExecutor mTestExecutor;
+    @Mock private DisplayController mDisplayController;
+    @Mock private SurfaceControl mLeash;
+    @Mock private WindowContainerToken mToken;
+    @Mock private WindowContainerTransaction mTransaction;
+    @Mock private ForceShowNavigationBarSettingsObserver mObserver;
+    @Mock private StartingWindowController mStartingWindowController;
+    @Mock private DisplayInsetsController mDisplayInsetsController;
+
+    KidsModeTaskOrganizer mOrganizer;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        try {
+            doReturn(ParceledListSlice.<TaskAppearedInfo>emptyList())
+                    .when(mTaskOrganizerController).registerTaskOrganizer(any());
+        } catch (RemoteException e) { }
+        mOrganizer = spy(new KidsModeTaskOrganizer(mTaskOrganizerController, mTestExecutor,
+                mHandler, mContext, mCompatUI, mSyncTransactionQueue, mDisplayController,
+                mDisplayInsetsController, Optional.empty(), mObserver));
+        mOrganizer.initialize(mStartingWindowController);
+        doReturn(mTransaction).when(mOrganizer).getWindowContainerTransaction();
+        doReturn(new InsetsState()).when(mDisplayController).getInsetsState(DEFAULT_DISPLAY);
+    }
+
+    @Test
+    public void testKidsModeOn() {
+        doReturn(true).when(mObserver).isEnabled();
+
+        mOrganizer.updateKidsModeState();
+
+        verify(mOrganizer, times(1)).enable();
+        verify(mOrganizer, times(1)).registerOrganizer();
+        verify(mOrganizer, times(1)).createRootTask(
+                eq(DEFAULT_DISPLAY), eq(WINDOWING_MODE_FULLSCREEN), eq(mOrganizer.mCookie));
+
+        final ActivityManager.RunningTaskInfo rootTask = createTaskInfo(12,
+                WINDOWING_MODE_FULLSCREEN, mOrganizer.mCookie);
+        mOrganizer.onTaskAppeared(rootTask, mLeash);
+
+        assertThat(mOrganizer.mLaunchRootLeash).isEqualTo(mLeash);
+        assertThat(mOrganizer.mLaunchRootTask).isEqualTo(rootTask);
+    }
+
+    @Test
+    public void testKidsModeOff() {
+        doReturn(true).when(mObserver).isEnabled();
+        mOrganizer.updateKidsModeState();
+        final ActivityManager.RunningTaskInfo rootTask = createTaskInfo(12,
+                WINDOWING_MODE_FULLSCREEN, mOrganizer.mCookie);
+        mOrganizer.onTaskAppeared(rootTask, mLeash);
+
+        doReturn(false).when(mObserver).isEnabled();
+        mOrganizer.updateKidsModeState();
+
+
+        verify(mOrganizer, times(1)).disable();
+        verify(mOrganizer, times(1)).unregisterOrganizer();
+        verify(mOrganizer, times(1)).deleteRootTask(rootTask.token);
+        assertThat(mOrganizer.mLaunchRootLeash).isNull();
+        assertThat(mOrganizer.mLaunchRootTask).isNull();
+    }
+
+    private ActivityManager.RunningTaskInfo createTaskInfo(
+            int taskId, int windowingMode, IBinder cookies) {
+        ActivityManager.RunningTaskInfo taskInfo = new ActivityManager.RunningTaskInfo();
+        taskInfo.taskId = taskId;
+        taskInfo.token = mToken;
+        taskInfo.configuration.windowConfiguration.setWindowingMode(windowingMode);
+        final ArrayList<IBinder> launchCookies = new ArrayList<>();
+        if (cookies != null) {
+            launchCookies.add(cookies);
+        }
+        taskInfo.launchCookies = launchCookies;
+        return taskInfo;
+    }
+}
diff --git a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/pip/tv/TvPipKeepClearAlgorithmTest.kt b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/pip/tv/TvPipKeepClearAlgorithmTest.kt
new file mode 100644
index 0000000..e6ba70e
--- /dev/null
+++ b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/pip/tv/TvPipKeepClearAlgorithmTest.kt
@@ -0,0 +1,469 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.wm.shell.pip.tv
+
+import android.graphics.Rect
+import android.testing.AndroidTestingRunner
+import android.util.Size
+import android.view.Gravity
+import org.junit.runner.RunWith
+import com.android.wm.shell.pip.PipBoundsState.STASH_TYPE_NONE
+import com.android.wm.shell.pip.PipBoundsState.STASH_TYPE_BOTTOM
+import com.android.wm.shell.pip.PipBoundsState.STASH_TYPE_RIGHT
+import com.android.wm.shell.pip.tv.TvPipKeepClearAlgorithm.Placement
+import org.junit.Before
+import org.junit.Test
+import junit.framework.Assert.assertEquals
+import junit.framework.Assert.assertNull
+
+@RunWith(AndroidTestingRunner::class)
+class TvPipKeepClearAlgorithmTest {
+    private val DEFAULT_PIP_SIZE = Size(384, 216)
+    private val EXPANDED_WIDE_PIP_SIZE = Size(384*2, 216)
+    private val DASHBOARD_WIDTH = 484
+    private val BOTTOM_SHEET_HEIGHT = 524
+    private val STASH_OFFSET = 64
+    private val PADDING = 16
+    private val SCREEN_SIZE = Size(1920, 1080)
+    private val SCREEN_EDGE_INSET = 50
+
+    private lateinit var pipSize: Size
+    private lateinit var movementBounds: Rect
+    private lateinit var algorithm: TvPipKeepClearAlgorithm
+    private var currentTime = 0L
+    private var restrictedAreas = mutableSetOf<Rect>()
+    private var unrestrictedAreas = mutableSetOf<Rect>()
+    private var gravity: Int = 0
+
+    @Before
+    fun setup() {
+        movementBounds = Rect(0, 0, SCREEN_SIZE.width, SCREEN_SIZE.height)
+        movementBounds.inset(SCREEN_EDGE_INSET, SCREEN_EDGE_INSET)
+
+        restrictedAreas.clear()
+        unrestrictedAreas.clear()
+        currentTime = 0L
+        pipSize = DEFAULT_PIP_SIZE
+        gravity = Gravity.BOTTOM or Gravity.RIGHT
+
+        algorithm = TvPipKeepClearAlgorithm({ currentTime })
+        algorithm.setScreenSize(SCREEN_SIZE)
+        algorithm.setMovementBounds(movementBounds)
+        algorithm.pipAreaPadding = PADDING
+        algorithm.stashOffset = STASH_OFFSET
+        algorithm.stashDuration = 5000L
+        algorithm.setGravity(gravity)
+        algorithm.maxRestrictedDistanceFraction = 0.3
+    }
+
+    @Test
+    fun testAnchorPosition_BottomRight() {
+        gravity = Gravity.BOTTOM or Gravity.RIGHT
+        testAnchorPosition()
+    }
+
+    @Test
+    fun testAnchorPosition_TopRight() {
+        gravity = Gravity.TOP or Gravity.RIGHT
+        testAnchorPosition()
+    }
+
+    @Test
+    fun testAnchorPosition_TopLeft() {
+        gravity = Gravity.TOP or Gravity.LEFT
+        testAnchorPosition()
+    }
+
+    @Test
+    fun testAnchorPosition_BottomLeft() {
+        gravity = Gravity.BOTTOM or Gravity.LEFT
+        testAnchorPosition()
+    }
+
+    @Test
+    fun testAnchorPosition_Right() {
+        gravity = Gravity.RIGHT
+        testAnchorPosition()
+    }
+
+    @Test
+    fun testAnchorPosition_Left() {
+        gravity = Gravity.LEFT
+        testAnchorPosition()
+    }
+
+    @Test
+    fun testAnchorPosition_Top() {
+        gravity = Gravity.TOP
+        testAnchorPosition()
+    }
+
+    @Test
+    fun testAnchorPosition_Bottom() {
+        gravity = Gravity.BOTTOM
+        testAnchorPosition()
+    }
+
+    @Test
+    fun testAnchorPosition_TopCenterHorizontal() {
+        gravity = Gravity.TOP or Gravity.CENTER_HORIZONTAL
+        testAnchorPosition()
+    }
+
+    @Test
+    fun testAnchorPosition_BottomCenterHorizontal() {
+        gravity = Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL
+        testAnchorPosition()
+    }
+
+    @Test
+    fun testAnchorPosition_RightCenterVertical() {
+        gravity = Gravity.RIGHT or Gravity.CENTER_VERTICAL
+        testAnchorPosition()
+    }
+
+    @Test
+    fun testAnchorPosition_LeftCenterVertical() {
+        gravity = Gravity.LEFT or Gravity.CENTER_VERTICAL
+        testAnchorPosition()
+    }
+
+    fun testAnchorPosition() {
+        val placement = getActualPlacement()
+
+        assertEquals(getExpectedAnchorBounds(), placement.bounds)
+        assertNotStashed(placement)
+    }
+
+    @Test
+    fun test_AnchorBottomRight_KeepClearNotObstructing_StayAtAnchor() {
+        gravity = Gravity.BOTTOM or Gravity.RIGHT
+
+        val sidebar = makeSideBar(DASHBOARD_WIDTH, Gravity.LEFT)
+        unrestrictedAreas.add(sidebar)
+
+        val expectedBounds = getExpectedAnchorBounds()
+
+        val placement = getActualPlacement()
+        assertEquals(expectedBounds, placement.bounds)
+        assertNotStashed(placement)
+    }
+
+    @Test
+    fun test_AnchorBottomRight_UnrestrictedRightSidebar_PushedLeft() {
+        gravity = Gravity.BOTTOM or Gravity.RIGHT
+
+        val sidebar = makeSideBar(DASHBOARD_WIDTH, Gravity.RIGHT)
+        unrestrictedAreas.add(sidebar)
+
+        val expectedBounds = anchorBoundsOffsetBy(SCREEN_EDGE_INSET - sidebar.width() - PADDING, 0)
+
+        val placement = getActualPlacement()
+        assertEquals(expectedBounds, placement.bounds)
+        assertNotStashed(placement)
+    }
+
+    @Test
+    fun test_AnchorTopRight_UnrestrictedRightSidebar_PushedLeft() {
+        gravity = Gravity.TOP or Gravity.RIGHT
+
+        val sidebar = makeSideBar(DASHBOARD_WIDTH, Gravity.RIGHT)
+        unrestrictedAreas.add(sidebar)
+
+        val expectedBounds = anchorBoundsOffsetBy(SCREEN_EDGE_INSET - sidebar.width() - PADDING, 0)
+
+        val placement = getActualPlacement()
+        assertEquals(expectedBounds, placement.bounds)
+        assertNotStashed(placement)
+    }
+
+    @Test
+    fun test_AnchorBottomLeft_UnrestrictedRightSidebar_StayAtAnchor() {
+        gravity = Gravity.BOTTOM or Gravity.LEFT
+
+        val sidebar = makeSideBar(DASHBOARD_WIDTH, Gravity.RIGHT)
+        unrestrictedAreas.add(sidebar)
+
+        val expectedBounds = anchorBoundsOffsetBy(0, 0)
+
+        val placement = getActualPlacement()
+        assertEquals(expectedBounds, placement.bounds)
+        assertNotStashed(placement)
+    }
+
+    @Test
+    fun test_AnchorBottom_UnrestrictedRightSidebar_StayAtAnchor() {
+        gravity = Gravity.BOTTOM
+
+        val sidebar = makeSideBar(DASHBOARD_WIDTH, Gravity.RIGHT)
+        unrestrictedAreas.add(sidebar)
+
+        val expectedBounds = anchorBoundsOffsetBy(0, 0)
+
+        val placement = getActualPlacement()
+        assertEquals(expectedBounds, placement.bounds)
+        assertNotStashed(placement)
+    }
+
+    @Test
+    fun testExpanded_AnchorBottom_UnrestrictedRightSidebar_StayAtAnchor() {
+        pipSize = EXPANDED_WIDE_PIP_SIZE
+        gravity = Gravity.BOTTOM
+
+        val sidebar = makeSideBar(DASHBOARD_WIDTH, Gravity.RIGHT)
+        unrestrictedAreas.add(sidebar)
+
+        val expectedBounds = anchorBoundsOffsetBy(0, 0)
+
+        val placement = getActualPlacement()
+        assertEquals(expectedBounds, placement.bounds)
+        assertNotStashed(placement)
+    }
+
+    @Test
+    fun test_AnchorBottomRight_RestrictedSmallBottomBar_PushedUp() {
+        gravity = Gravity.BOTTOM or Gravity.RIGHT
+
+        val bottomBar = makeBottomBar(96)
+        restrictedAreas.add(bottomBar)
+
+        val expectedBounds = anchorBoundsOffsetBy(0,
+                SCREEN_EDGE_INSET - bottomBar.height() - PADDING)
+
+        val placement = getActualPlacement()
+        assertEquals(expectedBounds, placement.bounds)
+        assertNotStashed(placement)
+    }
+
+    @Test
+    fun test_AnchorBottomRight_RestrictedBottomSheet_StashDownAtAnchor() {
+        gravity = Gravity.BOTTOM or Gravity.RIGHT
+
+        val bottomBar = makeBottomBar(BOTTOM_SHEET_HEIGHT)
+        restrictedAreas.add(bottomBar)
+
+        val expectedBounds = getExpectedAnchorBounds()
+        expectedBounds.offsetTo(expectedBounds.left, SCREEN_SIZE.height - STASH_OFFSET)
+
+        val placement = getActualPlacement()
+        assertEquals(expectedBounds, placement.bounds)
+        assertEquals(STASH_TYPE_BOTTOM, placement.stashType)
+        assertEquals(getExpectedAnchorBounds(), placement.unstashDestinationBounds)
+        assertEquals(algorithm.stashDuration, placement.unstashTime)
+    }
+
+    @Test
+    fun test_AnchorBottomRight_UnrestrictedBottomSheet_PushUp() {
+        gravity = Gravity.BOTTOM or Gravity.RIGHT
+
+        val bottomBar = makeBottomBar(BOTTOM_SHEET_HEIGHT)
+        unrestrictedAreas.add(bottomBar)
+
+        val expectedBounds = anchorBoundsOffsetBy(0,
+                SCREEN_EDGE_INSET - bottomBar.height() - PADDING)
+
+        val placement = getActualPlacement()
+        assertEquals(expectedBounds, placement.bounds)
+        assertNotStashed(placement)
+    }
+
+    @Test
+    fun test_AnchorBottomRight_UnrestrictedBottomSheet_RestrictedSidebar_StashAboveBottomSheet() {
+        gravity = Gravity.BOTTOM or Gravity.RIGHT
+
+        val bottomBar = makeBottomBar(BOTTOM_SHEET_HEIGHT)
+        unrestrictedAreas.add(bottomBar)
+
+        val maxRestrictedHorizontalPush =
+                (algorithm.maxRestrictedDistanceFraction * SCREEN_SIZE.width).toInt()
+        val sideBar = makeSideBar(maxRestrictedHorizontalPush + 100, Gravity.RIGHT)
+        restrictedAreas.add(sideBar)
+
+        val expectedUnstashBounds =
+                anchorBoundsOffsetBy(0, SCREEN_EDGE_INSET - bottomBar.height() - PADDING)
+
+        val expectedBounds = Rect(expectedUnstashBounds)
+        expectedBounds.offsetTo(SCREEN_SIZE.width - STASH_OFFSET, expectedBounds.top)
+
+        val placement = getActualPlacement()
+        assertEquals(expectedBounds, placement.bounds)
+        assertEquals(STASH_TYPE_RIGHT, placement.stashType)
+        assertEquals(expectedUnstashBounds, placement.unstashDestinationBounds)
+        assertEquals(algorithm.stashDuration, placement.unstashTime)
+    }
+
+    @Test
+    fun test_AnchorBottomRight_UnrestrictedBottomSheet_UnrestrictedSidebar_PushUpLeft() {
+        gravity = Gravity.BOTTOM or Gravity.RIGHT
+
+        val bottomBar = makeBottomBar(BOTTOM_SHEET_HEIGHT)
+        unrestrictedAreas.add(bottomBar)
+
+        val maxRestrictedHorizontalPush =
+                (algorithm.maxRestrictedDistanceFraction * SCREEN_SIZE.width).toInt()
+        val sideBar = makeSideBar(maxRestrictedHorizontalPush + 100, Gravity.RIGHT)
+        unrestrictedAreas.add(sideBar)
+
+        val expectedBounds = anchorBoundsOffsetBy(
+                SCREEN_EDGE_INSET - sideBar.width() - PADDING,
+                SCREEN_EDGE_INSET - bottomBar.height() - PADDING
+        )
+
+        val placement = getActualPlacement()
+        assertEquals(expectedBounds, placement.bounds)
+        assertNotStashed(placement)
+    }
+
+    @Test
+    fun test_Stashed_UnstashBoundsBecomeUnobstructed_Unstashes() {
+        gravity = Gravity.BOTTOM or Gravity.RIGHT
+
+        val bottomBar = makeBottomBar(BOTTOM_SHEET_HEIGHT)
+        unrestrictedAreas.add(bottomBar)
+
+        val maxRestrictedHorizontalPush =
+                (algorithm.maxRestrictedDistanceFraction * SCREEN_SIZE.width).toInt()
+        val sideBar = makeSideBar(maxRestrictedHorizontalPush + 100, Gravity.RIGHT)
+        restrictedAreas.add(sideBar)
+
+        val expectedUnstashBounds =
+                anchorBoundsOffsetBy(0, SCREEN_EDGE_INSET - bottomBar.height() - PADDING)
+
+        val expectedBounds = Rect(expectedUnstashBounds)
+        expectedBounds.offsetTo(SCREEN_SIZE.width - STASH_OFFSET, expectedBounds.top)
+
+        var placement = getActualPlacement()
+        assertEquals(expectedBounds, placement.bounds)
+        assertEquals(STASH_TYPE_RIGHT, placement.stashType)
+        assertEquals(expectedUnstashBounds, placement.unstashDestinationBounds)
+        assertEquals(algorithm.stashDuration, placement.unstashTime)
+
+        currentTime += 1000
+
+        restrictedAreas.remove(sideBar)
+        placement = getActualPlacement()
+        assertEquals(expectedUnstashBounds, placement.bounds)
+        assertNotStashed(placement)
+    }
+
+    @Test
+    fun test_Stashed_UnstashBoundsStaysObstructed_UnstashesAfterTimeout() {
+        gravity = Gravity.BOTTOM or Gravity.RIGHT
+
+        val bottomBar = makeBottomBar(BOTTOM_SHEET_HEIGHT)
+        unrestrictedAreas.add(bottomBar)
+
+        val maxRestrictedHorizontalPush =
+                (algorithm.maxRestrictedDistanceFraction * SCREEN_SIZE.width).toInt()
+        val sideBar = makeSideBar(maxRestrictedHorizontalPush + 100, Gravity.RIGHT)
+        restrictedAreas.add(sideBar)
+
+        val expectedUnstashBounds =
+                anchorBoundsOffsetBy(0, SCREEN_EDGE_INSET - bottomBar.height() - PADDING)
+
+        val expectedBounds = Rect(expectedUnstashBounds)
+        expectedBounds.offsetTo(SCREEN_SIZE.width - STASH_OFFSET, expectedBounds.top)
+
+        var placement = getActualPlacement()
+        assertEquals(expectedBounds, placement.bounds)
+        assertEquals(STASH_TYPE_RIGHT, placement.stashType)
+        assertEquals(expectedUnstashBounds, placement.unstashDestinationBounds)
+        assertEquals(algorithm.stashDuration, placement.unstashTime)
+
+        currentTime += algorithm.stashDuration
+
+        placement = getActualPlacement()
+        assertEquals(expectedUnstashBounds, placement.bounds)
+        assertNotStashed(placement)
+    }
+
+    @Test
+    fun test_Stashed_UnstashBoundsObstructionChanges_UnstashTimeExtended() {
+        gravity = Gravity.BOTTOM or Gravity.RIGHT
+
+        val bottomBar = makeBottomBar(BOTTOM_SHEET_HEIGHT)
+        unrestrictedAreas.add(bottomBar)
+
+        val maxRestrictedHorizontalPush =
+                (algorithm.maxRestrictedDistanceFraction * SCREEN_SIZE.width).toInt()
+        val sideBar = makeSideBar(maxRestrictedHorizontalPush + 100, Gravity.RIGHT)
+        restrictedAreas.add(sideBar)
+
+        val expectedUnstashBounds =
+                anchorBoundsOffsetBy(0, SCREEN_EDGE_INSET - bottomBar.height() - PADDING)
+
+        val expectedBounds = Rect(expectedUnstashBounds)
+        expectedBounds.offsetTo(SCREEN_SIZE.width - STASH_OFFSET, expectedBounds.top)
+
+        var placement = getActualPlacement()
+        assertEquals(expectedBounds, placement.bounds)
+        assertEquals(STASH_TYPE_RIGHT, placement.stashType)
+        assertEquals(expectedUnstashBounds, placement.unstashDestinationBounds)
+        assertEquals(algorithm.stashDuration, placement.unstashTime)
+
+        currentTime += 1000
+
+        val newObstruction = Rect(
+                0,
+                expectedUnstashBounds.top,
+                expectedUnstashBounds.right,
+                expectedUnstashBounds.bottom
+        )
+        restrictedAreas.add(newObstruction)
+
+        placement = getActualPlacement()
+        assertEquals(expectedBounds, placement.bounds)
+        assertEquals(STASH_TYPE_RIGHT, placement.stashType)
+        assertEquals(expectedUnstashBounds, placement.unstashDestinationBounds)
+        assertEquals(currentTime + algorithm.stashDuration, placement.unstashTime)
+    }
+
+    private fun makeSideBar(width: Int, @Gravity.GravityFlags side: Int): Rect {
+        val sidebar = Rect(0, 0, width, SCREEN_SIZE.height)
+        if (side == Gravity.RIGHT) {
+            sidebar.offsetTo(SCREEN_SIZE.width - width, 0)
+        }
+        return sidebar
+    }
+
+    private fun makeBottomBar(height: Int): Rect {
+        return Rect(0, SCREEN_SIZE.height - height, SCREEN_SIZE.width, SCREEN_SIZE.height)
+    }
+
+    private fun getExpectedAnchorBounds(): Rect {
+        val expectedBounds = Rect()
+        Gravity.apply(gravity, pipSize.width, pipSize.height, movementBounds, expectedBounds)
+        return expectedBounds
+    }
+
+    private fun anchorBoundsOffsetBy(dx: Int, dy: Int): Rect {
+        val bounds = getExpectedAnchorBounds()
+        bounds.offset(dx, dy)
+        return bounds
+    }
+
+    private fun getActualPlacement(): Placement {
+        algorithm.setGravity(gravity)
+        return algorithm.calculatePipPosition(pipSize, restrictedAreas, unrestrictedAreas)
+    }
+
+    private fun assertNotStashed(actual: Placement) {
+        assertEquals(STASH_TYPE_NONE, actual.stashType)
+        assertNull(actual.unstashDestinationBounds)
+        assertEquals(0L, actual.unstashTime)
+    }
+}
diff --git a/libs/androidfw/Android.bp b/libs/androidfw/Android.bp
index 63b831d..c80fb18 100644
--- a/libs/androidfw/Android.bp
+++ b/libs/androidfw/Android.bp
@@ -118,7 +118,7 @@
                 "libz",
             ],
         },
-        linux_glibc: {
+        host_linux: {
             srcs: [
                 "CursorWindow.cpp",
             ],
diff --git a/libs/hwui/Properties.cpp b/libs/hwui/Properties.cpp
index bcfe9c3..30ca7d15 100644
--- a/libs/hwui/Properties.cpp
+++ b/libs/hwui/Properties.cpp
@@ -135,7 +135,7 @@
     skpCaptureEnabled = debuggingEnabled && base::GetBoolProperty(PROPERTY_CAPTURE_SKP_ENABLED, false);
 
     SkAndroidFrameworkTraceUtil::setEnableTracing(
-            base::GetBoolProperty(PROPERTY_SKIA_ATRACE_ENABLED, false));
+            base::GetBoolProperty(PROPERTY_SKIA_ATRACE_ENABLED, true));
 
     runningInEmulator = base::GetBoolProperty(PROPERTY_IS_EMULATOR, false);
 
diff --git a/libs/storage/IMountService.cpp b/libs/storage/IMountService.cpp
index 055dbb2..99508a2 100644
--- a/libs/storage/IMountService.cpp
+++ b/libs/storage/IMountService.cpp
@@ -48,8 +48,6 @@
     TRANSACTION_isObbMounted,
     TRANSACTION_getMountedObbPath,
     TRANSACTION_isExternalStorageEmulated,
-    TRANSACTION_decryptStorage,
-    TRANSACTION_encryptStorage,
 };
 
 class BpMountService: public BpInterface<IMountService>
@@ -517,40 +515,6 @@
         path = reply.readString16();
         return true;
     }
-
-    int32_t decryptStorage(const String16& password)
-    {
-        Parcel data, reply;
-        data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
-        data.writeString16(password);
-        if (remote()->transact(TRANSACTION_decryptStorage, data, &reply) != NO_ERROR) {
-            ALOGD("decryptStorage could not contact remote\n");
-            return -1;
-        }
-        int32_t err = reply.readExceptionCode();
-        if (err < 0) {
-            ALOGD("decryptStorage caught exception %d\n", err);
-            return err;
-        }
-        return reply.readInt32();
-    }
-
-    int32_t encryptStorage(const String16& password)
-    {
-        Parcel data, reply;
-        data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
-        data.writeString16(password);
-        if (remote()->transact(TRANSACTION_encryptStorage, data, &reply) != NO_ERROR) {
-            ALOGD("encryptStorage could not contact remote\n");
-            return -1;
-        }
-        int32_t err = reply.readExceptionCode();
-        if (err < 0) {
-            ALOGD("encryptStorage caught exception %d\n", err);
-            return err;
-        }
-        return reply.readInt32();
-    }
 };
 
 IMPLEMENT_META_INTERFACE(MountService, "android.os.storage.IStorageManager")
diff --git a/libs/storage/include/storage/IMountService.h b/libs/storage/include/storage/IMountService.h
index 5b07318..5a9c39b 100644
--- a/libs/storage/include/storage/IMountService.h
+++ b/libs/storage/include/storage/IMountService.h
@@ -70,8 +70,6 @@
             const sp<IObbActionListener>& token, const int32_t nonce) = 0;
     virtual bool isObbMounted(const String16& filename) = 0;
     virtual bool getMountedObbPath(const String16& filename, String16& path) = 0;
-    virtual int32_t decryptStorage(const String16& password) = 0;
-    virtual int32_t encryptStorage(const String16& password) = 0;
 };
 
 // ----------------------------------------------------------------------------
diff --git a/media/java/android/media/AudioManager.java b/media/java/android/media/AudioManager.java
index 432196c..1a56b15 100644
--- a/media/java/android/media/AudioManager.java
+++ b/media/java/android/media/AudioManager.java
@@ -6952,7 +6952,8 @@
         for (Integer format : formatsList) {
             int btSourceCodec = AudioSystem.audioFormatToBluetoothSourceCodec(format);
             if (btSourceCodec != BluetoothCodecConfig.SOURCE_CODEC_TYPE_INVALID) {
-                codecConfigList.add(new BluetoothCodecConfig(btSourceCodec));
+                codecConfigList.add(
+                        new BluetoothCodecConfig.Builder().setCodecType(btSourceCodec).build());
             }
         }
         return codecConfigList;
diff --git a/media/java/android/media/MediaRouter2.java b/media/java/android/media/MediaRouter2.java
index 47e402f..d8995b4 100644
--- a/media/java/android/media/MediaRouter2.java
+++ b/media/java/android/media/MediaRouter2.java
@@ -1118,6 +1118,11 @@
     private List<MediaRoute2Info> filterRoutesWithIndividualPreference(
             List<MediaRoute2Info> routes, RouteDiscoveryPreference discoveryPreference) {
         List<MediaRoute2Info> filteredRoutes = new ArrayList<>();
+        if (isSystemRouter()) {
+            // Individual discovery preferences do not apply for the system router.
+            filteredRoutes.addAll(routes);
+            return filteredRoutes;
+        }
         for (MediaRoute2Info route : routes) {
             if (!route.hasAnyFeatures(discoveryPreference.getPreferredFeatures())) {
                 continue;
diff --git a/media/java/android/media/tv/interactive/AppLinkInfo.java b/media/java/android/media/tv/interactive/AppLinkInfo.java
index cd201f7..0eb6fa8 100644
--- a/media/java/android/media/tv/interactive/AppLinkInfo.java
+++ b/media/java/android/media/tv/interactive/AppLinkInfo.java
@@ -18,6 +18,7 @@
 
 import android.annotation.NonNull;
 import android.annotation.Nullable;
+import android.content.ComponentName;
 import android.os.Parcel;
 import android.os.Parcelable;
 
@@ -25,8 +26,7 @@
  * App link information used by TV interactive app to launch Android apps.
  */
 public final class AppLinkInfo implements Parcelable {
-    private @NonNull String mPackageName;
-    private @NonNull String mClassName;
+    private @NonNull ComponentName mComponentName;
     private @Nullable String mUriScheme;
     private @Nullable String mUriHost;
     private @Nullable String mUriPrefix;
@@ -41,12 +41,11 @@
             @Nullable String uriScheme,
             @Nullable String uriHost,
             @Nullable String uriPrefix) {
-        this.mPackageName = packageName;
         com.android.internal.util.AnnotationValidations.validate(
-                NonNull.class, null, mPackageName);
-        this.mClassName = className;
+                NonNull.class, null, packageName);
         com.android.internal.util.AnnotationValidations.validate(
-                NonNull.class, null, mClassName);
+                NonNull.class, null, className);
+        this.mComponentName = new ComponentName(packageName, className);
         this.mUriScheme = uriScheme;
         this.mUriHost = uriHost;
         this.mUriPrefix = uriPrefix;
@@ -57,7 +56,7 @@
      */
     @NonNull
     public String getPackageName() {
-        return mPackageName;
+        return mComponentName.getPackageName();
     }
 
     /**
@@ -65,7 +64,7 @@
      */
     @NonNull
     public String getClassName() {
-        return mClassName;
+        return mComponentName.getClassName();
     }
 
     /**
@@ -95,8 +94,8 @@
     @Override
     public String toString() {
         return "AppLinkInfo { "
-                + "packageName = " + mPackageName + ", "
-                + "className = " + mClassName + ", "
+                + "packageName = " + mComponentName.getPackageName() + ", "
+                + "className = " + mComponentName.getClassName() + ", "
                 + "uriScheme = " + mUriScheme + ", "
                 + "uriHost = " + mUriHost + ", "
                 + "uriPrefix = " + mUriPrefix
@@ -105,8 +104,8 @@
 
     @Override
     public void writeToParcel(@NonNull Parcel dest, int flags) {
-        dest.writeString(mPackageName);
-        dest.writeString(mClassName);
+        dest.writeString(mComponentName.getPackageName());
+        dest.writeString(mComponentName.getClassName());
         dest.writeString(mUriScheme);
         dest.writeString(mUriHost);
         dest.writeString(mUriPrefix);
@@ -124,12 +123,11 @@
         String uriHost = in.readString();
         String uriPrefix = in.readString();
 
-        this.mPackageName = packageName;
         com.android.internal.util.AnnotationValidations.validate(
-                NonNull.class, null, mPackageName);
-        this.mClassName = className;
+                NonNull.class, null, packageName);
         com.android.internal.util.AnnotationValidations.validate(
-                NonNull.class, null, mClassName);
+                NonNull.class, null, className);
+        this.mComponentName = new ComponentName(packageName, className);
         this.mUriScheme = uriScheme;
         this.mUriHost = uriHost;
         this.mUriPrefix = uriPrefix;
@@ -174,28 +172,10 @@
         }
 
         /**
-         * Sets package name of the App link.
-         */
-        @NonNull
-        public Builder setPackageName(@NonNull String value) {
-            mPackageName = value;
-            return this;
-        }
-
-        /**
-         * Sets app name of the App link.
-         */
-        @NonNull
-        public Builder setClassName(@NonNull String value) {
-            mClassName = value;
-            return this;
-        }
-
-        /**
          * Sets URI scheme of the App link.
          */
         @NonNull
-        public Builder setUriScheme(@Nullable String value) {
+        public Builder setUriScheme(@NonNull String value) {
             mUriScheme = value;
             return this;
         }
@@ -204,7 +184,7 @@
          * Sets URI host of the App link.
          */
         @NonNull
-        public Builder setUriHost(@Nullable String value) {
+        public Builder setUriHost(@NonNull String value) {
             mUriHost = value;
             return this;
         }
@@ -213,7 +193,7 @@
          * Sets URI prefix of the App link.
          */
         @NonNull
-        public Builder setUriPrefix(@Nullable String value) {
+        public Builder setUriPrefix(@NonNull String value) {
             mUriPrefix = value;
             return this;
         }
diff --git a/media/java/android/media/tv/interactive/TvInteractiveAppInfo.java b/media/java/android/media/tv/interactive/TvInteractiveAppInfo.java
index e1f535c..6103db0 100644
--- a/media/java/android/media/tv/interactive/TvInteractiveAppInfo.java
+++ b/media/java/android/media/tv/interactive/TvInteractiveAppInfo.java
@@ -131,6 +131,10 @@
         dest.writeInt(mTypes);
     }
 
+    /**
+     * Returns a unique ID for this TV interactive app service. The ID is generated from the package
+     * and class name implementing the TV interactive app service.
+     */
     @NonNull
     public String getId() {
         return mId;
diff --git a/media/java/android/media/tv/interactive/TvInteractiveAppManager.java b/media/java/android/media/tv/interactive/TvInteractiveAppManager.java
index f75aa56..9eb4a6c 100755
--- a/media/java/android/media/tv/interactive/TvInteractiveAppManager.java
+++ b/media/java/android/media/tv/interactive/TvInteractiveAppManager.java
@@ -626,6 +626,7 @@
         /**
          * This is called when the state of the interactive app service is changed.
          *
+         * @param iAppServiceId The ID of the TV Interactive App service.
          * @param type the interactive app type
          * @param state the current state of the service of the given type
          * @param err the error code for error state. {@link #ERROR_NONE} is used when the state is
@@ -760,7 +761,12 @@
     }
 
     /**
-     * Registers app link info.
+     * Registers an Android application link info record which can be used to launch the specific
+     * Android application by TV interactive App RTE.
+     *
+     * @param tvIAppServiceId The ID of TV interactive service which the command to be sent to. The
+     *                        ID can be found in {@link TvInputInfo#getId()}.
+     * @param appLinkInfo The Android application link info record to be registered.
      */
     public void registerAppLinkInfo(
             @NonNull String tvIAppServiceId, @NonNull AppLinkInfo appLinkInfo) {
@@ -772,7 +778,12 @@
     }
 
     /**
-     * Unregisters app link info.
+     * Unregisters an Android application link info record which can be used to launch the specific
+     * Android application by TV interactive App RTE.
+     *
+     * @param tvIAppServiceId The ID of TV interactive service which the command to be sent to. The
+     *                        ID can be found in {@link TvInputInfo#getId()}.
+     * @param appLinkInfo The Android application link info record to be unregistered.
      */
     public void unregisterAppLinkInfo(
             @NonNull String tvIAppServiceId, @NonNull AppLinkInfo appLinkInfo) {
@@ -805,8 +816,8 @@
      * @param executor A {@link Executor} that the status change will be delivered to.
      */
     public void registerCallback(
-            @NonNull TvInteractiveAppCallback callback,
-            @CallbackExecutor @NonNull Executor executor) {
+            @CallbackExecutor @NonNull Executor executor,
+            @NonNull TvInteractiveAppCallback callback) {
         Preconditions.checkNotNull(callback);
         Preconditions.checkNotNull(executor);
         synchronized (mLock) {
@@ -1813,8 +1824,8 @@
         }
 
         /**
-         * This is called when {@link TvIAppService.Session#notifyTeletextAppStateChanged} is
-         * called.
+         * This is called when {@link TvInteractiveAppService.Session#notifyTeletextAppStateChanged}
+         * is called.
          *
          * @param session A {@link TvInteractiveAppManager.Session} associated with this callback.
          * @param state the current state.
diff --git a/media/java/android/media/tv/interactive/TvInteractiveAppService.java b/media/java/android/media/tv/interactive/TvInteractiveAppService.java
index 52b00b7..d22fd83 100755
--- a/media/java/android/media/tv/interactive/TvInteractiveAppService.java
+++ b/media/java/android/media/tv/interactive/TvInteractiveAppService.java
@@ -16,9 +16,11 @@
 
 package android.media.tv.interactive;
 
+import android.annotation.CallSuper;
 import android.annotation.MainThread;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
+import android.annotation.SdkConstant;
 import android.annotation.StringDef;
 import android.annotation.SuppressLint;
 import android.app.ActivityManager;
@@ -76,15 +78,14 @@
 
     private static final int DETACH_MEDIA_VIEW_TIMEOUT_MS = 5000;
 
-    // TODO: cleanup and unhide APIs.
-
     /**
      * This is the interface name that a service implementing a TV Interactive App service should
      * say that it supports -- that is, this is the action it uses for its intent filter. To be
      * supported, the service must also require the
-     * android.Manifest.permission#BIND_TV_INTERACTIVE_APP permission so that other applications
-     * cannot abuse it.
+     * {@link android.Manifest.permission#BIND_TV_INTERACTIVE_APP} permission so that other
+     * applications cannot abuse it.
      */
+    @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
     public static final String SERVICE_INTERFACE =
             "android.media.tv.interactive.TvInteractiveAppService";
 
@@ -244,13 +245,13 @@
     public abstract void onPrepare(@TvInteractiveAppInfo.InteractiveAppType int type);
 
     /**
-     * Registers App link info.
+     * Called when a request to register an Android application link info record is received.
      */
     public void onRegisterAppLinkInfo(@NonNull AppLinkInfo appLinkInfo) {
     }
 
     /**
-     * Unregisters App link info.
+     * Called when a request to unregister an Android application link info record is received.
      */
     public void onUnregisterAppLinkInfo(@NonNull AppLinkInfo appLinkInfo) {
     }
@@ -351,6 +352,7 @@
          * @param enable {@code true} if you want to enable the media view. {@code false}
          *            otherwise.
          */
+        @CallSuper
         public void setMediaViewEnabled(final boolean enable) {
             mHandler.post(new Runnable() {
                 @Override
@@ -383,7 +385,7 @@
         }
 
         /**
-         * Resets TvIAppService session.
+         * Resets TvInteractiveAppService session.
          */
         public void onResetInteractiveApp() {
         }
@@ -467,11 +469,11 @@
          * in {@link #onSetSurface}. This method is always called at least once, after
          * {@link #onSetSurface} is called with non-null surface.
          *
-         * @param format The new PixelFormat of the surface.
+         * @param format The new {@link PixelFormat} of the surface.
          * @param width The new width of the surface.
          * @param height The new height of the surface.
          */
-        public void onSurfaceChanged(int format, int width, int height) {
+        public void onSurfaceChanged(@PixelFormat.Format int format, int width, int height) {
         }
 
         /**
@@ -631,6 +633,7 @@
          * @param right Right position in pixels, relative to the overlay view.
          * @param bottom Bottom position in pixels, relative to the overlay view.
          */
+        @CallSuper
         public void layoutSurface(final int left, final int top, final int right,
                 final int bottom) {
             if (left > right || top > bottom) {
@@ -659,6 +662,7 @@
          * Requests broadcast related information from the related TV input.
          * @param request the request for broadcast info
          */
+        @CallSuper
         public void requestBroadcastInfo(@NonNull final BroadcastInfoRequest request) {
             executeOrPostRunnableOnMainThread(new Runnable() {
                 @MainThread
@@ -683,6 +687,7 @@
          * Remove broadcast information request from the related TV input.
          * @param requestId the ID of the request
          */
+        @CallSuper
         public void removeBroadcastInfo(final int requestId) {
             executeOrPostRunnableOnMainThread(new Runnable() {
                 @MainThread
@@ -709,6 +714,7 @@
          * @param cmdType type of the specific command
          * @param parameters parameters of the specific command
          */
+        @CallSuper
         public void sendPlaybackCommandRequest(
                 @PlaybackCommandType @NonNull String cmdType, @Nullable Bundle parameters) {
             executeOrPostRunnableOnMainThread(new Runnable() {
@@ -733,6 +739,7 @@
         /**
          * Sets broadcast video bounds.
          */
+        @CallSuper
         public void setVideoBounds(@NonNull Rect rect) {
             executeOrPostRunnableOnMainThread(new Runnable() {
                 @MainThread
@@ -755,6 +762,7 @@
         /**
          * Requests the URI of the current channel.
          */
+        @CallSuper
         public void requestCurrentChannelUri() {
             executeOrPostRunnableOnMainThread(new Runnable() {
                 @MainThread
@@ -777,6 +785,7 @@
         /**
          * Requests the logic channel number (LCN) of the current channel.
          */
+        @CallSuper
         public void requestCurrentChannelLcn() {
             executeOrPostRunnableOnMainThread(new Runnable() {
                 @MainThread
@@ -799,6 +808,7 @@
         /**
          * Requests stream volume.
          */
+        @CallSuper
         public void requestStreamVolume() {
             executeOrPostRunnableOnMainThread(new Runnable() {
                 @MainThread
@@ -821,6 +831,7 @@
         /**
          * Requests the list of {@link TvTrackInfo}.
          */
+        @CallSuper
         public void requestTrackInfoList() {
             executeOrPostRunnableOnMainThread(new Runnable() {
                 @MainThread
@@ -845,6 +856,7 @@
          *
          * @see android.media.tv.TvInputInfo
          */
+        @CallSuper
         public void requestCurrentTvInputId() {
             executeOrPostRunnableOnMainThread(new Runnable() {
                 @MainThread
@@ -869,6 +881,7 @@
          *
          * @param request The advertisement request
          */
+        @CallSuper
         public void requestAd(@NonNull final AdRequest request) {
             executeOrPostRunnableOnMainThread(new Runnable() {
                 @MainThread
@@ -1032,6 +1045,7 @@
          *            used when the state is not
          *            {@link TvInteractiveAppManager#INTERACTIVE_APP_STATE_ERROR}.
          */
+        @CallSuper
         public void notifySessionStateChanged(
                 @TvInteractiveAppManager.InteractiveAppState int state,
                 @TvInteractiveAppManager.ErrorCode int err) {
@@ -1062,6 +1076,7 @@
          *
          * @see #onCreateBiInteractiveApp(Uri, Bundle)
          */
+        @CallSuper
         public final void notifyBiInteractiveAppCreated(
                 @NonNull Uri biIAppUri, @Nullable String biIAppId) {
             executeOrPostRunnableOnMainThread(new Runnable() {
@@ -1087,6 +1102,7 @@
          * Notifies when the digital teletext app state is changed.
          * @param state the current state.
          */
+        @CallSuper
         public final void notifyTeletextAppStateChanged(
                 @TvInteractiveAppManager.TeletextAppState int state) {
             executeOrPostRunnableOnMainThread(new Runnable() {
diff --git a/media/java/android/media/tv/tuner/Lnb.java b/media/java/android/media/tv/tuner/Lnb.java
index 50a2083..3b70890 100644
--- a/media/java/android/media/tv/tuner/Lnb.java
+++ b/media/java/android/media/tv/tuner/Lnb.java
@@ -167,10 +167,10 @@
 
     private Lnb() {}
 
-    void setCallbackAndOwner(Executor executor, @Nullable LnbCallback callback, Tuner tuner) {
+    void setCallbackAndOwner(Tuner tuner, Executor executor, @Nullable LnbCallback callback) {
         synchronized (mCallbackLock) {
             if (callback != null && executor != null) {
-                addCallback(callback, executor);
+                addCallback(executor, callback);
             }
         }
         setOwner(tuner);
@@ -179,12 +179,12 @@
     /**
      * Adds LnbCallback
      *
-     * @param callback the callback to receive notifications from LNB.
      * @param executor the executor on which callback will be invoked. Cannot be null.
+     * @param callback the callback to receive notifications from LNB.
      */
-    public void addCallback(@NonNull  LnbCallback callback, @NonNull Executor executor) {
-        Objects.requireNonNull(callback, "callback must not be null");
+    public void addCallback(@NonNull Executor executor, @NonNull LnbCallback callback) {
         Objects.requireNonNull(executor, "executor must not be null");
+        Objects.requireNonNull(callback, "callback must not be null");
         synchronized (mCallbackLock) {
             mCallbackMap.put(callback, executor);
         }
diff --git a/media/java/android/media/tv/tuner/Tuner.java b/media/java/android/media/tv/tuner/Tuner.java
index 1e32cad..ef0270b 100644
--- a/media/java/android/media/tv/tuner/Tuner.java
+++ b/media/java/android/media/tv/tuner/Tuner.java
@@ -62,7 +62,9 @@
 import android.os.Message;
 import android.os.Process;
 import android.util.Log;
+
 import com.android.internal.util.FrameworkStatsLog;
+
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.ref.WeakReference;
@@ -2147,12 +2149,12 @@
             Objects.requireNonNull(executor, "executor must not be null");
             Objects.requireNonNull(cb, "LnbCallback must not be null");
             if (mLnb != null) {
-                mLnb.setCallbackAndOwner(executor, cb, this);
+                mLnb.setCallbackAndOwner(this, executor, cb);
                 return mLnb;
             }
             if (checkResource(TunerResourceManager.TUNER_RESOURCE_TYPE_LNB, mLnbLock)
                     && mLnb != null) {
-                mLnb.setCallbackAndOwner(executor, cb, this);
+                mLnb.setCallbackAndOwner(this, executor, cb);
                 setLnb(mLnb);
                 return mLnb;
             }
@@ -2186,7 +2188,7 @@
                     mLnbHandle = null;
                 }
                 mLnb = newLnb;
-                mLnb.setCallbackAndOwner(executor, cb, this);
+                mLnb.setCallbackAndOwner(this, executor, cb);
                 setLnb(mLnb);
             }
             return mLnb;
diff --git a/packages/BackupRestoreConfirmation/res/values/strings.xml b/packages/BackupRestoreConfirmation/res/values/strings.xml
index 3fb3fd4..5c90fd0 100644
--- a/packages/BackupRestoreConfirmation/res/values/strings.xml
+++ b/packages/BackupRestoreConfirmation/res/values/strings.xml
@@ -44,8 +44,6 @@
     <string name="backup_enc_password_text">Please enter a password to use for encrypting the full backup data. If this is left blank, your current backup password will be used:</string>
     <!-- Text for message to user that they may optionally supply an encryption password to use for a full backup operation. -->
     <string name="backup_enc_password_optional">If you wish to encrypt the full backup data, enter a password below:</string>
-    <!-- Text for message to user that they must supply an encryption password to use for a full backup operation because their phone is locked. -->
-    <string name="backup_enc_password_required">Since your device is encrypted, you are required to encrypt your backup. Please enter a password below:</string>
 
     <!-- Text for message to user when performing a full restore operation, explaining that they must enter the password originally used to encrypt the full backup data. -->
     <string name="restore_enc_password_text">If the restore data is encrypted, please enter the password below:</string>
diff --git a/packages/BackupRestoreConfirmation/src/com/android/backupconfirm/BackupRestoreConfirmation.java b/packages/BackupRestoreConfirmation/src/com/android/backupconfirm/BackupRestoreConfirmation.java
index d6b6bf8..3c790f0 100644
--- a/packages/BackupRestoreConfirmation/src/com/android/backupconfirm/BackupRestoreConfirmation.java
+++ b/packages/BackupRestoreConfirmation/src/com/android/backupconfirm/BackupRestoreConfirmation.java
@@ -27,8 +27,6 @@
 import android.os.Message;
 import android.os.RemoteException;
 import android.os.ServiceManager;
-import android.os.storage.IStorageManager;
-import android.os.storage.StorageManager;
 import android.text.Editable;
 import android.text.TextWatcher;
 import android.util.Slog;
@@ -66,10 +64,8 @@
 
     Handler mHandler;
     IBackupManager mBackupManager;
-    IStorageManager mStorageManager;
     FullObserver mObserver;
     int mToken;
-    boolean mIsEncrypted;
     boolean mDidAcknowledge;
     String mAction;
 
@@ -144,7 +140,6 @@
         }
 
         mBackupManager = IBackupManager.Stub.asInterface(ServiceManager.getService(Context.BACKUP_SERVICE));
-        mStorageManager = IStorageManager.Stub.asInterface(ServiceManager.getService("mount"));
 
         mHandler = new ObserverHandler(getApplicationContext());
         final Object oldObserver = getLastNonConfigurationInstance();
@@ -248,20 +243,13 @@
             mDenyButton.setEnabled(!mDidAcknowledge);
         }
 
-        // We vary the password prompt depending on whether one is predefined, and whether
-        // the device is encrypted.
-        mIsEncrypted = deviceIsEncrypted();
+        // We vary the password prompt depending on whether one is predefined.
         if (!haveBackupPassword()) {
             curPwDesc.setVisibility(View.GONE);
             mCurPassword.setVisibility(View.GONE);
             if (layoutId == R.layout.confirm_backup) {
                 TextView encPwDesc = findViewById(R.id.enc_password_desc);
-                if (mIsEncrypted) {
-                    encPwDesc.setText(R.string.backup_enc_password_required);
-                    monitorEncryptionPassword();
-                } else {
-                    encPwDesc.setText(R.string.backup_enc_password_optional);
-                }
+                encPwDesc.setText(R.string.backup_enc_password_optional);
             }
         }
     }
@@ -312,20 +300,6 @@
         }
     }
 
-    boolean deviceIsEncrypted() {
-        try {
-            return mStorageManager.getEncryptionState()
-                     != StorageManager.ENCRYPTION_STATE_NONE
-                && mStorageManager.getPasswordType()
-                     != StorageManager.CRYPT_TYPE_DEFAULT;
-        } catch (Exception e) {
-            // If we can't talk to the storagemanager service we have a serious problem; fail
-            // "secure" i.e. assuming that the device is encrypted.
-            Slog.e(TAG, "Unable to communicate with storagemanager service: " + e.getMessage());
-            return true;
-        }
-    }
-
     boolean haveBackupPassword() {
         try {
             return mBackupManager.hasBackupPassword();
diff --git a/packages/CompanionDeviceManager/res/drawable/btn_negative_multiple_devices.xml b/packages/CompanionDeviceManager/res/drawable/btn_negative_multiple_devices.xml
new file mode 100644
index 0000000..ece7bba
--- /dev/null
+++ b/packages/CompanionDeviceManager/res/drawable/btn_negative_multiple_devices.xml
@@ -0,0 +1,25 @@
+<!--
+  ~ Copyright (C) 2022 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+
+<shape xmlns:android="http://schemas.android.com/apk/res/android"
+       android:shape="rectangle">
+    <solid android:color="@android:color/transparent" />
+    <corners android:topLeftRadius="16dp" android:topRightRadius="16dp"
+             android:bottomLeftRadius="16dp" android:bottomRightRadius="16dp"/>
+    <stroke
+        android:width="1dp"
+        android:color="@android:color/system_accent1_600" />
+</shape>
\ No newline at end of file
diff --git a/packages/CompanionDeviceManager/res/drawable/btn_negative_top.xml b/packages/CompanionDeviceManager/res/drawable/btn_negative_top.xml
new file mode 100644
index 0000000..7df92bb
--- /dev/null
+++ b/packages/CompanionDeviceManager/res/drawable/btn_negative_top.xml
@@ -0,0 +1,22 @@
+<!--
+  ~ Copyright (C) 2022 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+
+<shape xmlns:android="http://schemas.android.com/apk/res/android"
+       android:shape="rectangle">
+    <solid android:color="@android:color/system_accent1_100"/>
+    <corners android:topLeftRadius="12dp" android:topRightRadius="12dp"
+             android:bottomLeftRadius="4dp" android:bottomRightRadius="4dp"/>
+</shape>
diff --git a/packages/CompanionDeviceManager/res/drawable/btn_positive_bottom.xml b/packages/CompanionDeviceManager/res/drawable/btn_positive_bottom.xml
new file mode 100644
index 0000000..55e96f6
--- /dev/null
+++ b/packages/CompanionDeviceManager/res/drawable/btn_positive_bottom.xml
@@ -0,0 +1,22 @@
+<!--
+  ~ Copyright (C) 2022 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+
+<shape xmlns:android="http://schemas.android.com/apk/res/android"
+       android:shape="rectangle">
+    <solid android:color="@android:color/system_accent1_100"/>
+    <corners android:topLeftRadius="4dp" android:topRightRadius="4dp"
+             android:bottomLeftRadius="12dp" android:bottomRightRadius="12dp"/>
+</shape>
diff --git a/packages/CompanionDeviceManager/res/drawable/ic_device_other.xml b/packages/CompanionDeviceManager/res/drawable/ic_device_other.xml
new file mode 100644
index 0000000..f8515c3
--- /dev/null
+++ b/packages/CompanionDeviceManager/res/drawable/ic_device_other.xml
@@ -0,0 +1,23 @@
+<!--
+  ~ Copyright (C) 2022 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="24dp" android:height="24dp"
+        android:viewportWidth="24"
+        android:viewportHeight="24"
+        android:tint="?attr/colorControlNormal">
+    <path android:fillColor="@android:color/white"
+          android:pathData="M7,20H4Q3.175,20 2.588,19.413Q2,18.825 2,18V6Q2,5.175 2.588,4.588Q3.175,4 4,4H20V6H4Q4,6 4,6Q4,6 4,6V18Q4,18 4,18Q4,18 4,18H7ZM9,20V18.2Q8.55,17.775 8.275,17.225Q8,16.675 8,16Q8,15.325 8.275,14.775Q8.55,14.225 9,13.8V12H13V13.8Q13.45,14.225 13.725,14.775Q14,15.325 14,16Q14,16.675 13.725,17.225Q13.45,17.775 13,18.2V20ZM11,17.5Q11.65,17.5 12.075,17.075Q12.5,16.65 12.5,16Q12.5,15.35 12.075,14.925Q11.65,14.5 11,14.5Q10.35,14.5 9.925,14.925Q9.5,15.35 9.5,16Q9.5,16.65 9.925,17.075Q10.35,17.5 11,17.5ZM21,20H16Q15.575,20 15.288,19.712Q15,19.425 15,19V10Q15,9.575 15.288,9.287Q15.575,9 16,9H21Q21.425,9 21.712,9.287Q22,9.575 22,10V19Q22,19.425 21.712,19.712Q21.425,20 21,20ZM17,18H20V11H17Z"/>
+</vector>
\ No newline at end of file
diff --git a/packages/CompanionDeviceManager/res/drawable/ic_watch.xml b/packages/CompanionDeviceManager/res/drawable/ic_watch.xml
new file mode 100644
index 0000000..44a40b9f
--- /dev/null
+++ b/packages/CompanionDeviceManager/res/drawable/ic_watch.xml
@@ -0,0 +1,25 @@
+<!--
+  ~ Copyright (C) 2022 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="24dp"
+        android:height="24dp"
+        android:viewportWidth="24"
+        android:viewportHeight="24"
+        android:tint="?attr/colorControlNormal">
+    <path android:fillColor="@android:color/white"
+          android:pathData="M9,22 L7.65,17.45Q6.45,16.5 5.725,15.075Q5,13.65 5,12Q5,10.35 5.725,8.925Q6.45,7.5 7.65,6.55L9,2H15L16.35,6.55Q17.55,7.5 18.275,8.925Q19,10.35 19,12Q19,13.65 18.275,15.075Q17.55,16.5 16.35,17.45L15,22ZM12,17Q14.075,17 15.538,15.537Q17,14.075 17,12Q17,9.925 15.538,8.462Q14.075,7 12,7Q9.925,7 8.463,8.462Q7,9.925 7,12Q7,14.075 8.463,15.537Q9.925,17 12,17ZM10.1,5.25Q11.075,4.975 12,4.975Q12.925,4.975 13.9,5.25L13.5,4H10.5ZM10.5,20H13.5L13.9,18.75Q12.925,19.025 12,19.025Q11.075,19.025 10.1,18.75ZM10.1,4H10.5H13.5H13.9Q12.925,4 12,4Q11.075,4 10.1,4ZM10.5,20H10.1Q11.075,20 12,20Q12.925,20 13.9,20H13.5Z"/>
+</vector>
\ No newline at end of file
diff --git a/packages/CompanionDeviceManager/res/layout/activity_confirmation.xml b/packages/CompanionDeviceManager/res/layout/activity_confirmation.xml
index 82fe72b..8eec33a 100644
--- a/packages/CompanionDeviceManager/res/layout/activity_confirmation.xml
+++ b/packages/CompanionDeviceManager/res/layout/activity_confirmation.xml
@@ -20,36 +20,64 @@
     <!-- A header for selfManaged devices only. -->
     <include layout="@layout/vendor_header" />
 
+    <ImageView
+        android:id="@+id/profile_icon"
+        android:layout_width="match_parent"
+        android:layout_height="32dp"
+        android:gravity="center"
+        android:layout_marginBottom="12dp"
+        android:layout_marginTop="1dp"
+        android:tint="@android:color/system_accent1_600"/>
+
     <!-- Do NOT change the ID of the root LinearLayout above: it's referenced in CTS tests. -->
 
     <TextView
-            android:id="@+id/title"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:gravity="center"
-            android:paddingHorizontal="12dp"
-            style="@*android:style/TextAppearance.Widget.Toolbar.Title"/>
+        android:id="@+id/title"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:gravity="center"
+        android:paddingHorizontal="12dp"
+        android:layout_marginBottom="12dp"
+        style="@*android:style/TextAppearance.Widget.Toolbar.Title" />
 
     <TextView
-            android:id="@+id/summary"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:layout_marginTop="12dp"
-            android:layout_marginBottom="12dp"
-            android:gravity="center"
-            android:textColor="?android:attr/textColorSecondary"
-            android:textSize="14sp" />
+        android:id="@+id/summary"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="12dp"
+        android:layout_marginBottom="12dp"
+        android:gravity="center"
+        android:textColor="?android:attr/textColorSecondary"
+        android:textSize="14sp" />
 
     <RelativeLayout
-            android:layout_width="match_parent"
-            android:layout_height="0dp"
-            android:layout_weight="1">
+        android:layout_width="match_parent"
+        android:layout_height="0dp"
+        android:layout_weight="1">
 
-        <androidx.recyclerview.widget.RecyclerView
-            android:id="@+id/device_list"
+        <LinearLayout
+            android:id="@+id/multiple_device_list"
             android:layout_width="match_parent"
-            android:scrollbars="vertical"
-            android:layout_height="200dp" />
+            android:layout_height="wrap_content"
+            android:orientation="vertical"
+            android:visibility="gone">
+
+            <View
+                android:id="@+id/border_top"
+                android:layout_marginTop="12dp"
+                style="@style/DeviceListBorder" />
+
+            <androidx.recyclerview.widget.RecyclerView
+                android:id="@+id/device_list"
+                android:layout_width="match_parent"
+                android:scrollbars="vertical"
+                android:layout_height="200dp" />
+
+            <View
+                android:id="@+id/border_bottom"
+                style="@style/DeviceListBorder" />
+
+        </LinearLayout>
 
         <androidx.recyclerview.widget.RecyclerView
             android:id="@+id/permission_list"
@@ -59,27 +87,31 @@
     </RelativeLayout>
 
     <LinearLayout
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:orientation="horizontal"
-            android:gravity="end">
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:orientation="vertical"
+        android:layout_marginTop="24dp">
 
         <!-- Do NOT change the IDs of the buttons: they are referenced in CTS tests. -->
 
         <Button
-                android:id="@+id/btn_negative"
-                style="@android:style/Widget.Material.Button.Borderless.Colored"
-                android:layout_width="wrap_content"
-                android:layout_height="wrap_content"
-                android:text="@string/consent_no"
-                android:textColor="?android:attr/textColorSecondary" />
+            android:id="@+id/btn_negative"
+            style="@style/NegativeButton"
+            android:text="@string/consent_no" />
 
         <Button
-                android:id="@+id/btn_positive"
-                style="@android:style/Widget.Material.Button.Borderless.Colored"
-                android:layout_width="wrap_content"
-                android:layout_height="wrap_content"
-                android:text="@string/consent_yes" />
+            android:id="@+id/btn_positive"
+            style="@style/PositiveButton"
+            android:text="@string/consent_yes" />
+
+        <Button
+            android:id="@+id/btn_negative_multiple_devices"
+            android:layout_marginLeft="170dp"
+            android:layout_marginBottom="10dp"
+            style="@style/NegativeButtonMultipleDevices"
+            android:textColor = "?android:textColorPrimary"
+            android:visibility="gone"
+            android:text="@string/consent_no" />
 
     </LinearLayout>
 
diff --git a/packages/CompanionDeviceManager/res/layout/data_transfer_confirmation.xml b/packages/CompanionDeviceManager/res/layout/data_transfer_confirmation.xml
index a1855fd..7c50814 100644
--- a/packages/CompanionDeviceManager/res/layout/data_transfer_confirmation.xml
+++ b/packages/CompanionDeviceManager/res/layout/data_transfer_confirmation.xml
@@ -28,45 +28,40 @@
     <!-- Do NOT change the ID of the root LinearLayout above: it's referenced in CTS tests. -->
 
     <TextView
-            android:id="@+id/title"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:gravity="center"
-            android:paddingHorizontal="12dp"
-            style="@*android:style/TextAppearance.Widget.Toolbar.Title"/>
+        android:id="@+id/title"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:gravity="center"
+        android:paddingHorizontal="12dp"
+        style="@*android:style/TextAppearance.Widget.Toolbar.Title"/>
 
     <TextView
-            android:id="@+id/summary"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:layout_marginTop="12dp"
-            android:layout_marginBottom="12dp"
-            android:gravity="center"
-            android:textColor="?android:attr/textColorSecondary"
-            android:textSize="14sp" />
+        android:id="@+id/summary"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="12dp"
+        android:layout_marginBottom="12dp"
+        android:gravity="center"
+        android:textColor="?android:attr/textColorSecondary"
+        android:textSize="14sp" />
 
     <LinearLayout
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:orientation="horizontal"
-            android:gravity="end">
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:orientation="vertical"
+        android:layout_marginTop="24dp">
 
         <!-- Do NOT change the IDs of the buttons: they are referenced in CTS tests. -->
 
         <Button
-                android:id="@+id/btn_negative"
-                style="@android:style/Widget.Material.Button.Borderless.Colored"
-                android:layout_width="wrap_content"
-                android:layout_height="wrap_content"
-                android:text="@string/consent_no"
-                android:textColor="?android:attr/textColorSecondary" />
+            android:id="@+id/btn_negative"
+            style="@style/NegativeButton"
+            android:text="@string/consent_no" />
 
         <Button
-                android:id="@+id/btn_positive"
-                style="@android:style/Widget.Material.Button.Borderless.Colored"
-                android:layout_width="wrap_content"
-                android:layout_height="wrap_content"
-                android:text="@string/consent_yes" />
+            android:id="@+id/btn_positive"
+            style="@style/PositiveButton"
+            android:text="@string/consent_yes" />
 
     </LinearLayout>
 
diff --git a/packages/CompanionDeviceManager/res/layout/helper_confirmation.xml b/packages/CompanionDeviceManager/res/layout/helper_confirmation.xml
index 8fd1fb0..c177039 100644
--- a/packages/CompanionDeviceManager/res/layout/helper_confirmation.xml
+++ b/packages/CompanionDeviceManager/res/layout/helper_confirmation.xml
@@ -60,4 +60,4 @@
 
     </LinearLayout>
 
-</LinearLayout>
+</LinearLayout>
\ No newline at end of file
diff --git a/packages/CompanionDeviceManager/res/layout/list_item_device.xml b/packages/CompanionDeviceManager/res/layout/list_item_device.xml
index 153fc1f..3c8a81f 100644
--- a/packages/CompanionDeviceManager/res/layout/list_item_device.xml
+++ b/packages/CompanionDeviceManager/res/layout/list_item_device.xml
@@ -25,16 +25,17 @@
     <!-- Do NOT change the ID of the root LinearLayout above: it's referenced in CTS tests. -->
 
     <ImageView
-            android:id="@android:id/icon"
-            android:layout_width="24dp"
-            android:layout_height="24dp"
-            android:layout_marginRight="12dp"/>
+        android:id="@android:id/icon"
+        android:layout_width="24dp"
+        android:layout_height="24dp"
+        android:layout_marginRight="12dp"
+        android:tint="@android:color/system_accent1_600"/>
 
     <TextView
-            android:id="@android:id/text1"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:singleLine="true"
-            android:textAppearance="?android:attr/textAppearanceListItemSmall"/>
+        android:id="@android:id/text1"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:singleLine="true"
+        android:textAppearance="?android:attr/textAppearanceListItemSmall"/>
 
 </LinearLayout>
\ No newline at end of file
diff --git a/packages/CompanionDeviceManager/res/layout/list_item_permission.xml b/packages/CompanionDeviceManager/res/layout/list_item_permission.xml
index b8a0f79..79aa4e7 100644
--- a/packages/CompanionDeviceManager/res/layout/list_item_permission.xml
+++ b/packages/CompanionDeviceManager/res/layout/list_item_permission.xml
@@ -27,6 +27,7 @@
         android:layout_height="24dp"
         android:layout_marginTop="7dp"
         android:layout_marginEnd="12dp"
+        android:tint="@android:color/system_accent1_600"
         android:contentDescription="Permission Icon"/>
 
     <LinearLayout
diff --git a/packages/CompanionDeviceManager/res/values-night/themes.xml b/packages/CompanionDeviceManager/res/values-night/themes.xml
new file mode 100644
index 0000000..6eb16e7
--- /dev/null
+++ b/packages/CompanionDeviceManager/res/values-night/themes.xml
@@ -0,0 +1,26 @@
+<!--
+  ~ Copyright (C) 2022 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+
+<resources>
+
+    <style name="ChooserActivity"
+           parent="@android:style/Theme.DeviceDefault.Dialog.NoActionBar">
+        <item name="*android:windowFixedHeightMajor">100%</item>
+        <item name="*android:windowFixedHeightMinor">100%</item>
+        <item name="android:windowBackground">@android:color/transparent</item>
+    </style>
+
+</resources>
diff --git a/packages/CompanionDeviceManager/res/values/styles.xml b/packages/CompanionDeviceManager/res/values/styles.xml
index bba45e9..6eaffd4 100644
--- a/packages/CompanionDeviceManager/res/values/styles.xml
+++ b/packages/CompanionDeviceManager/res/values/styles.xml
@@ -35,4 +35,40 @@
         <item name="android:background">@drawable/helper_ok_button</item>
     </style>
 
+    <style name="NegativeButton"
+           parent="@android:style/Widget.Material.Button.Borderless.Colored">
+        <item name="android:layout_width">match_parent</item>
+        <item name="android:layout_height">wrap_content</item>
+        <item name="android:textAllCaps">false</item>
+        <item name="android:textSize">14sp</item>
+        <item name="android:textColor">@android:color/system_neutral1_900</item>
+        <item name="android:background">@drawable/btn_negative_top</item>
+    </style>
+
+    <style name="PositiveButton"
+           parent="@android:style/Widget.Material.Button.Borderless.Colored">
+        <item name="android:layout_width">match_parent</item>
+        <item name="android:layout_height">wrap_content</item>
+        <item name="android:textAllCaps">false</item>
+        <item name="android:textSize">14sp</item>
+        <item name="android:textColor">@android:color/system_neutral1_900</item>
+        <item name="android:layout_marginTop">4dp</item>
+        <item name="android:background">@drawable/btn_positive_bottom</item>
+    </style>
+
+    <style name="NegativeButtonMultipleDevices"
+           parent="@android:style/Widget.Material.Button.Colored">
+        <item name="android:layout_width">100dp</item>
+        <item name="android:layout_height">35dp</item>
+        <item name="android:layout_marginTop">20dp</item>
+        <item name="android:textAllCaps">false</item>
+        <item name="android:background">@drawable/btn_negative_multiple_devices</item>
+    </style>
+
+    <style name="DeviceListBorder">
+        <item name="android:layout_width">match_parent</item>
+        <item name="android:layout_height">1dp</item>
+        <item name="android:background">@android:color/system_accent1_300</item>
+    </style>
+
 </resources>
\ No newline at end of file
diff --git a/packages/CompanionDeviceManager/res/values/themes.xml b/packages/CompanionDeviceManager/res/values/themes.xml
index 7240432..e3fc67c 100644
--- a/packages/CompanionDeviceManager/res/values/themes.xml
+++ b/packages/CompanionDeviceManager/res/values/themes.xml
@@ -17,11 +17,10 @@
 <resources>
 
     <style name="ChooserActivity"
-           parent="@android:style/Theme.DeviceDefault.Light.Dialog">
+           parent="@android:style/Theme.DeviceDefault.Light.Dialog.NoActionBar">
         <item name="*android:windowFixedHeightMajor">100%</item>
         <item name="*android:windowFixedHeightMinor">100%</item>
         <item name="android:windowBackground">@android:color/transparent</item>
-        <item name="android:forceDarkAllowed">true</item>
     </style>
 
 </resources>
diff --git a/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/CompanionDeviceActivity.java b/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/CompanionDeviceActivity.java
index 1b62691..f752b69 100644
--- a/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/CompanionDeviceActivity.java
+++ b/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/CompanionDeviceActivity.java
@@ -29,6 +29,7 @@
 import static com.android.companiondevicemanager.PermissionListAdapter.TYPE_STORAGE;
 import static com.android.companiondevicemanager.Utils.getApplicationLabel;
 import static com.android.companiondevicemanager.Utils.getHtmlFromResources;
+import static com.android.companiondevicemanager.Utils.getIcon;
 import static com.android.companiondevicemanager.Utils.getVendorHeaderIcon;
 import static com.android.companiondevicemanager.Utils.getVendorHeaderName;
 import static com.android.companiondevicemanager.Utils.prepareResultReceiverForIpc;
@@ -106,6 +107,9 @@
     private TextView mTitle;
     private TextView mSummary;
 
+    // Present for single device and multiple device only.
+    private ImageView mProfileIcon;
+
     // Only present for selfManaged devices.
     private ImageView mVendorHeaderImage;
     private TextView mVendorHeaderName;
@@ -118,10 +122,12 @@
     // Present for self-managed association requests and "single-device" regular association
     // regular.
     private Button mButtonAllow;
-    // Present for all associations.
     private Button mButtonNotAllow;
+    // Present for multiple device association requests only.
+    private Button mButtonNotAllowMultipleDevices;
 
     private LinearLayout mAssociationConfirmationDialog;
+    private LinearLayout mMultipleDeviceList;
     private RelativeLayout mVendorHeader;
 
     // The recycler view is only shown for multiple-device regular association request, after
@@ -129,6 +135,7 @@
     private @Nullable RecyclerView mDeviceListRecyclerView;
     private @Nullable DeviceListAdapter mDeviceAdapter;
 
+
     // The recycler view is only shown for selfManaged association request.
     private @Nullable RecyclerView mPermissionListRecyclerView;
     private @Nullable PermissionListAdapter mPermissionListAdapter;
@@ -242,12 +249,15 @@
 
         setContentView(R.layout.activity_confirmation);
 
+        mMultipleDeviceList = findViewById(R.id.multiple_device_list);
         mAssociationConfirmationDialog = findViewById(R.id.activity_confirmation);
         mVendorHeader = findViewById(R.id.vendor_header);
 
         mTitle = findViewById(R.id.title);
         mSummary = findViewById(R.id.summary);
 
+        mProfileIcon = findViewById(R.id.profile_icon);
+
         mVendorHeaderImage = findViewById(R.id.vendor_header_image);
         mVendorHeaderName = findViewById(R.id.vendor_header_name);
         mVendorHeaderButton = findViewById(R.id.vendor_header_button);
@@ -260,9 +270,12 @@
 
         mButtonAllow = findViewById(R.id.btn_positive);
         mButtonNotAllow = findViewById(R.id.btn_negative);
+        mButtonNotAllowMultipleDevices = findViewById(R.id.btn_negative_multiple_devices);
 
         mButtonAllow.setOnClickListener(this::onPositiveButtonClick);
         mButtonNotAllow.setOnClickListener(this::onNegativeButtonClick);
+        mButtonNotAllowMultipleDevices.setOnClickListener(this::onNegativeButtonClick);
+
         mVendorHeaderButton.setOnClickListener(this::onShowHelperDialog);
 
         if (mRequest.isSelfManaged()) {
@@ -416,6 +429,7 @@
         mVendorHeaderName.setText(vendorName);
 
         mDeviceListRecyclerView.setVisibility(View.GONE);
+        mProfileIcon.setVisibility(View.GONE);
         mVendorHeader.setVisibility(View.VISIBLE);
     }
 
@@ -443,17 +457,22 @@
         final Spanned title = getHtmlFromResources(
                 this, R.string.confirmation_title, appLabel, deviceName);
         final Spanned summary;
+        final Drawable profileIcon;
 
         if (deviceProfile == null) {
             summary = getHtmlFromResources(this, R.string.summary_generic);
+            profileIcon = getIcon(this, R.drawable.ic_device_other);
+            mSummary.setVisibility(View.GONE);
         } else if (deviceProfile.equals(DEVICE_PROFILE_WATCH)) {
             summary = getHtmlFromResources(this, R.string.summary_watch, appLabel, deviceName);
+            profileIcon = getIcon(this, R.drawable.ic_watch);
         } else {
             throw new RuntimeException("Unsupported profile " + deviceProfile);
         }
 
         mTitle.setText(title);
         mSummary.setText(summary);
+        mProfileIcon.setImageDrawable(profileIcon);
     }
 
     private void initUiForMultipleDevices(CharSequence appLabel) {
@@ -463,12 +482,16 @@
 
         final String profileName;
         final Spanned summary;
+        final Drawable profileIcon;
         if (deviceProfile == null) {
             profileName = getString(R.string.profile_name_generic);
             summary = getHtmlFromResources(this, R.string.summary_generic);
+            profileIcon = getIcon(this, R.drawable.ic_device_other);
+            mSummary.setVisibility(View.GONE);
         } else if (deviceProfile.equals(DEVICE_PROFILE_WATCH)) {
             profileName = getString(R.string.profile_name_watch);
             summary = getHtmlFromResources(this, R.string.summary_watch, appLabel);
+            profileIcon = getIcon(this, R.drawable.ic_watch);
         } else {
             throw new RuntimeException("Unsupported profile " + deviceProfile);
         }
@@ -477,6 +500,7 @@
 
         mTitle.setText(title);
         mSummary.setText(summary);
+        mProfileIcon.setImageDrawable(profileIcon);
 
         mDeviceAdapter = new DeviceListAdapter(this, this::onListItemClick);
 
@@ -490,6 +514,9 @@
 
         // "Remove" consent button: users would need to click on the list item.
         mButtonAllow.setVisibility(View.GONE);
+        mButtonNotAllow.setVisibility(View.GONE);
+        mButtonNotAllowMultipleDevices.setVisibility(View.VISIBLE);
+        mMultipleDeviceList.setVisibility(View.VISIBLE);
     }
 
     private void onListItemClick(int position) {
diff --git a/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/Utils.java b/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/Utils.java
index d5b2f0a..1852e82 100644
--- a/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/Utils.java
+++ b/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/Utils.java
@@ -22,7 +22,6 @@
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.PackageManager.ApplicationInfoFlags;
-import android.graphics.Color;
 import android.graphics.drawable.Drawable;
 import android.os.Bundle;
 import android.os.Handler;
@@ -124,7 +123,6 @@
 
     static @NonNull Drawable getIcon(@NonNull Context context, int resId) {
         Drawable icon = context.getResources().getDrawable(resId, null);
-        icon.setTint(Color.DKGRAY);
         return icon;
     }
 
diff --git a/packages/ConnectivityT/framework-t/Android.bp b/packages/ConnectivityT/framework-t/Android.bp
index 6652780..217a1f67c3 100644
--- a/packages/ConnectivityT/framework-t/Android.bp
+++ b/packages/ConnectivityT/framework-t/Android.bp
@@ -154,17 +154,17 @@
     ],
 }
 
+// TODO: remove this empty filegroup.
 filegroup {
     name: "framework-connectivity-tiramisu-sources",
-    srcs: [
-        ":framework-connectivity-ethernet-sources",
-    ],
+    srcs: [],
     visibility: ["//frameworks/base"],
 }
 
 filegroup {
     name: "framework-connectivity-tiramisu-updatable-sources",
     srcs: [
+        ":framework-connectivity-ethernet-sources",
         ":framework-connectivity-ipsec-sources",
         ":framework-connectivity-netstats-sources",
         ":framework-connectivity-nsd-sources",
diff --git a/packages/ConnectivityT/framework-t/src/android/net/ConnectivityFrameworkInitializerTiramisu.java b/packages/ConnectivityT/framework-t/src/android/net/ConnectivityFrameworkInitializerTiramisu.java
index 9bffbfb..61b34d0 100644
--- a/packages/ConnectivityT/framework-t/src/android/net/ConnectivityFrameworkInitializerTiramisu.java
+++ b/packages/ConnectivityT/framework-t/src/android/net/ConnectivityFrameworkInitializerTiramisu.java
@@ -34,8 +34,9 @@
     private ConnectivityFrameworkInitializerTiramisu() {}
 
     /**
-     * Called by {@link SystemServiceRegistry}'s static initializer and registers nsd services to
-     * {@link Context}, so that {@link Context#getSystemService} can return them.
+     * Called by {@link SystemServiceRegistry}'s static initializer and registers NetworkStats, nsd,
+     * ipsec and ethernet services to {@link Context}, so that {@link Context#getSystemService} can
+     * return them.
      *
      * @throws IllegalStateException if this is called anywhere besides
      * {@link SystemServiceRegistry}.
@@ -68,5 +69,14 @@
                     return new NetworkStatsManager(context, service);
                 }
         );
+
+        SystemServiceRegistry.registerContextAwareService(
+                Context.ETHERNET_SERVICE,
+                EthernetManager.class,
+                (context, serviceBinder) -> {
+                    IEthernetManager service = IEthernetManager.Stub.asInterface(serviceBinder);
+                    return new EthernetManager(context, service);
+                }
+        );
     }
 }
diff --git a/packages/ConnectivityT/framework-t/src/android/net/EthernetManager.java b/packages/ConnectivityT/framework-t/src/android/net/EthernetManager.java
index 72243f9..793f28d 100644
--- a/packages/ConnectivityT/framework-t/src/android/net/EthernetManager.java
+++ b/packages/ConnectivityT/framework-t/src/android/net/EthernetManager.java
@@ -33,7 +33,7 @@
 import android.os.RemoteException;
 
 import com.android.internal.annotations.GuardedBy;
-import com.android.internal.os.BackgroundThread;
+import com.android.modules.utils.BackgroundThread;
 
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
@@ -511,7 +511,6 @@
             NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
             android.Manifest.permission.NETWORK_STACK,
             android.Manifest.permission.MANAGE_ETHERNET_NETWORKS})
-    @RequiresFeature(PackageManager.FEATURE_AUTOMOTIVE)
     public void updateConfiguration(
             @NonNull String iface,
             @NonNull EthernetNetworkUpdateRequest request,
diff --git a/packages/ConnectivityT/framework-t/src/android/net/EthernetNetworkUpdateRequest.java b/packages/ConnectivityT/framework-t/src/android/net/EthernetNetworkUpdateRequest.java
index a626971..43f4c40f 100644
--- a/packages/ConnectivityT/framework-t/src/android/net/EthernetNetworkUpdateRequest.java
+++ b/packages/ConnectivityT/framework-t/src/android/net/EthernetNetworkUpdateRequest.java
@@ -24,36 +24,52 @@
 
 import java.util.Objects;
 
-/** @hide */
+/**
+ * Represents a request to update an existing Ethernet interface.
+ *
+ * @see EthernetManager#updateConfiguration
+ *
+ * @hide
+ */
 @SystemApi
 public final class EthernetNetworkUpdateRequest implements Parcelable {
     @NonNull
     private final IpConfiguration mIpConfig;
-    @NonNull
+    @Nullable
     private final NetworkCapabilities mNetworkCapabilities;
 
+    /**
+     * @return the new {@link IpConfiguration}.
+     */
     @NonNull
     public IpConfiguration getIpConfiguration() {
         return new IpConfiguration(mIpConfig);
     }
 
-    @NonNull
+    /**
+     * Setting the {@link NetworkCapabilities} is optional in {@link EthernetNetworkUpdateRequest}.
+     * When set to null, the existing NetworkCapabilities are not updated.
+     *
+     * @return the new {@link NetworkCapabilities} or null.
+     */
+    @Nullable
     public NetworkCapabilities getNetworkCapabilities() {
-        return new NetworkCapabilities(mNetworkCapabilities);
+        return mNetworkCapabilities == null ? null : new NetworkCapabilities(mNetworkCapabilities);
     }
 
     private EthernetNetworkUpdateRequest(@NonNull final IpConfiguration ipConfig,
-            @NonNull final NetworkCapabilities networkCapabilities) {
+            @Nullable final NetworkCapabilities networkCapabilities) {
         Objects.requireNonNull(ipConfig);
-        Objects.requireNonNull(networkCapabilities);
-        mIpConfig = new IpConfiguration(ipConfig);
-        mNetworkCapabilities = new NetworkCapabilities(networkCapabilities);
+        mIpConfig = ipConfig;
+        mNetworkCapabilities = networkCapabilities;
     }
 
     private EthernetNetworkUpdateRequest(@NonNull final Parcel source) {
         Objects.requireNonNull(source);
-        mIpConfig = IpConfiguration.CREATOR.createFromParcel(source);
-        mNetworkCapabilities = NetworkCapabilities.CREATOR.createFromParcel(source);
+        mIpConfig = source.readParcelable(IpConfiguration.class.getClassLoader(),
+                IpConfiguration.class);
+        mNetworkCapabilities = source.readParcelable(NetworkCapabilities.class.getClassLoader(),
+                NetworkCapabilities.class);
     }
 
     /**
@@ -75,7 +91,8 @@
         public Builder(@NonNull final EthernetNetworkUpdateRequest request) {
             Objects.requireNonNull(request);
             mBuilderIpConfig = new IpConfiguration(request.mIpConfig);
-            mBuilderNetworkCapabilities = new NetworkCapabilities(request.mNetworkCapabilities);
+            mBuilderNetworkCapabilities = null == request.mNetworkCapabilities
+                    ? null : new NetworkCapabilities(request.mNetworkCapabilities);
         }
 
         /**
@@ -85,7 +102,6 @@
          */
         @NonNull
         public Builder setIpConfiguration(@NonNull final IpConfiguration ipConfig) {
-            Objects.requireNonNull(ipConfig);
             mBuilderIpConfig = new IpConfiguration(ipConfig);
             return this;
         }
@@ -96,9 +112,8 @@
          * @return The builder to facilitate chaining.
          */
         @NonNull
-        public Builder setNetworkCapabilities(@NonNull final NetworkCapabilities nc) {
-            Objects.requireNonNull(nc);
-            mBuilderNetworkCapabilities = new NetworkCapabilities(nc);
+        public Builder setNetworkCapabilities(@Nullable final NetworkCapabilities nc) {
+            mBuilderNetworkCapabilities = nc == null ? null : new NetworkCapabilities(nc);
             return this;
         }
 
@@ -135,8 +150,8 @@
 
     @Override
     public void writeToParcel(@NonNull Parcel dest, int flags) {
-        mIpConfig.writeToParcel(dest, flags);
-        mNetworkCapabilities.writeToParcel(dest, flags);
+        dest.writeParcelable(mIpConfig, flags);
+        dest.writeParcelable(mNetworkCapabilities, flags);
     }
 
     @Override
diff --git a/packages/ConnectivityT/framework-t/src/android/net/NetworkStats.java b/packages/ConnectivityT/framework-t/src/android/net/NetworkStats.java
index f681ba1..06f2a62 100644
--- a/packages/ConnectivityT/framework-t/src/android/net/NetworkStats.java
+++ b/packages/ConnectivityT/framework-t/src/android/net/NetworkStats.java
@@ -327,16 +327,11 @@
          * @param uid uid of this {@link Entry}. {@link #UID_TETHERING} if this {@link Entry} is
          *            for tethering. Or {@link #UID_ALL} if this {@link NetworkStats} is only
          *            counting iface stats.
-         * @param set usage state of this {@link Entry}. Should be one of the following
-         *            values: {@link #SET_DEFAULT}, {@link #SET_FOREGROUND}.
+         * @param set usage state of this {@link Entry}.
          * @param tag tag of this {@link Entry}.
-         * @param metered metered state of this {@link Entry}. Should be one of the following
-         *                values: {link #METERED_YES}, {link #METERED_NO}.
-         * @param roaming roaming state of this {@link Entry}. Should be one of the following
-         *                values: {link #ROAMING_YES}, {link #ROAMING_NO}.
-         * @param defaultNetwork default network status of this {@link Entry}. Should be one
-         *                       of the following values: {link #DEFAULT_NETWORK_YES},
-         *                       {link #DEFAULT_NETWORK_NO}.
+         * @param metered metered state of this {@link Entry}.
+         * @param roaming roaming state of this {@link Entry}.
+         * @param defaultNetwork default network status of this {@link Entry}.
          * @param rxBytes Number of bytes received for this {@link Entry}. Statistics should
          *                represent the contents of IP packets, including IP headers.
          * @param rxPackets Number of packets received for this {@link Entry}. Statistics should
@@ -401,8 +396,7 @@
         }
 
         /**
-         * @return the set state of this entry. Should be one of the following
-         * values: {@link #SET_DEFAULT}, {@link #SET_FOREGROUND}.
+         * @return the set state of this entry.
          */
         @State public int getSet() {
             return set;
@@ -416,24 +410,21 @@
         }
 
         /**
-         * @return the metered state. Should be one of the following
-         * values: {link #METERED_YES}, {link #METERED_NO}.
+         * @return the metered state.
          */
         @Meteredness public int getMetered() {
             return metered;
         }
 
         /**
-         * @return the roaming state. Should be one of the following
-         * values: {link #ROAMING_YES}, {link #ROAMING_NO}.
+         * @return the roaming state.
          */
         @Roaming public int getRoaming() {
             return roaming;
         }
 
         /**
-         * @return the default network state. Should be one of the following
-         * values: {link #DEFAULT_NETWORK_YES}, {link #DEFAULT_NETWORK_NO}.
+         * @return the default network state.
          */
         @DefaultNetwork public int getDefaultNetwork() {
             return defaultNetwork;
diff --git a/packages/ConnectivityT/framework-t/src/android/net/NetworkStatsCollection.java b/packages/ConnectivityT/framework-t/src/android/net/NetworkStatsCollection.java
index 735c44d..e385b33 100644
--- a/packages/ConnectivityT/framework-t/src/android/net/NetworkStatsCollection.java
+++ b/packages/ConnectivityT/framework-t/src/android/net/NetworkStatsCollection.java
@@ -36,6 +36,7 @@
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.annotation.SystemApi;
+import android.net.NetworkStats.State;
 import android.net.NetworkStatsHistory.Entry;
 import android.os.Binder;
 import android.service.NetworkStatsCollectionKeyProto;
@@ -102,7 +103,7 @@
 
     private ArrayMap<Key, NetworkStatsHistory> mStats = new ArrayMap<>();
 
-    private final long mBucketDuration;
+    private final long mBucketDurationMillis;
 
     private long mStartMillis;
     private long mEndMillis;
@@ -115,8 +116,8 @@
      * @param bucketDuration duration of the buckets in this object, in milliseconds.
      * @hide
      */
-    public NetworkStatsCollection(long bucketDuration) {
-        mBucketDuration = bucketDuration;
+    public NetworkStatsCollection(long bucketDurationMillis) {
+        mBucketDurationMillis = bucketDurationMillis;
         reset();
     }
 
@@ -148,7 +149,7 @@
         if (mStartMillis == Long.MAX_VALUE) {
             return Long.MAX_VALUE;
         } else {
-            return mStartMillis + mBucketDuration;
+            return mStartMillis + mBucketDurationMillis;
         }
     }
 
@@ -184,10 +185,10 @@
                 || time == SubscriptionPlan.TIME_UNKNOWN) {
             return time;
         } else {
-            final long mod = time % mBucketDuration;
+            final long mod = time % mBucketDurationMillis;
             if (mod > 0) {
                 time -= mod;
-                time += mBucketDuration;
+                time += mBucketDurationMillis;
             }
             return time;
         }
@@ -200,7 +201,7 @@
                 || time == SubscriptionPlan.TIME_UNKNOWN) {
             return time;
         } else {
-            final long mod = time % mBucketDuration;
+            final long mod = time % mBucketDurationMillis;
             if (mod > 0) {
                 time -= mod;
             }
@@ -247,10 +248,10 @@
         // 180 days of history should be enough for anyone; if we end up needing
         // more, we'll dynamically grow the history object.
         final int bucketEstimate = (int) NetworkStatsUtils.constrain(
-                ((end - start) / mBucketDuration), 0,
-                (180 * DateUtils.DAY_IN_MILLIS) / mBucketDuration);
+                ((end - start) / mBucketDurationMillis), 0,
+                (180 * DateUtils.DAY_IN_MILLIS) / mBucketDurationMillis);
         final NetworkStatsHistory combined = new NetworkStatsHistory(
-                mBucketDuration, bucketEstimate, fields);
+                mBucketDurationMillis, bucketEstimate, fields);
 
         // shortcut when we know stats will be empty
         if (start == end) return combined;
@@ -343,7 +344,7 @@
 
             // Finally we can slice data as originally requested
             final NetworkStatsHistory sliced = new NetworkStatsHistory(
-                    mBucketDuration, bucketEstimate, fields);
+                    mBucketDurationMillis, bucketEstimate, fields);
             sliced.recordHistory(combined, start, end);
             return sliced;
         } else {
@@ -458,9 +459,9 @@
         // update when no existing, or when bucket duration changed
         NetworkStatsHistory updated = null;
         if (existing == null) {
-            updated = new NetworkStatsHistory(mBucketDuration, 10);
-        } else if (existing.getBucketDuration() != mBucketDuration) {
-            updated = new NetworkStatsHistory(existing, mBucketDuration);
+            updated = new NetworkStatsHistory(mBucketDurationMillis, 10);
+        } else if (existing.getBucketDuration() != mBucketDurationMillis) {
+            updated = new NetworkStatsHistory(existing, mBucketDurationMillis);
         }
 
         if (updated != null) {
@@ -702,7 +703,7 @@
 
     private int estimateBuckets() {
         return (int) (Math.min(mEndMillis - mStartMillis, WEEK_IN_MILLIS * 5)
-                / mBucketDuration);
+                / mBucketDurationMillis);
     }
 
     private ArrayList<Key> getSortedKeys() {
@@ -828,7 +829,7 @@
      * Builder class for {@link NetworkStatsCollection}.
      */
     public static final class Builder {
-        private final long mBucketDuration;
+        private final long mBucketDurationMillis;
         private final ArrayMap<Key, NetworkStatsHistory> mEntries = new ArrayMap<>();
 
         /**
@@ -836,8 +837,8 @@
          *
          * @param bucketDuration Duration of the buckets of the object, in milliseconds.
          */
-        public Builder(long bucketDuration) {
-            mBucketDuration = bucketDuration;
+        public Builder(long bucketDurationMillis) {
+            mBucketDurationMillis = bucketDurationMillis;
         }
 
         /**
@@ -855,7 +856,7 @@
             final List<Entry> historyEntries = history.getEntries();
 
             final NetworkStatsHistory.Builder historyBuilder =
-                    new NetworkStatsHistory.Builder(mBucketDuration, historyEntries.size());
+                    new NetworkStatsHistory.Builder(mBucketDurationMillis, historyEntries.size());
             for (Entry entry : historyEntries) {
                 historyBuilder.addEntry(entry);
             }
@@ -871,7 +872,8 @@
          */
         @NonNull
         public NetworkStatsCollection build() {
-            final NetworkStatsCollection collection = new NetworkStatsCollection(mBucketDuration);
+            final NetworkStatsCollection collection =
+                    new NetworkStatsCollection(mBucketDurationMillis);
             for (int i = 0; i < mEntries.size(); i++) {
                 collection.recordHistory(mEntries.keyAt(i), mEntries.valueAt(i));
             }
@@ -883,7 +885,7 @@
      * the identifier that associate with the {@link NetworkStatsHistory} object to identify
      * a certain record in the {@link NetworkStatsCollection} object.
      */
-    public static class Key {
+    public static final class Key {
         /** @hide */
         public final NetworkIdentitySet ident;
         /** @hide */
@@ -903,7 +905,7 @@
          * @param set Set of the record, see {@code NetworkStats#SET_*}.
          * @param tag Tag of the record, see {@link TrafficStats#setThreadStatsTag(int)}.
          */
-        public Key(@NonNull Set<NetworkIdentity> ident, int uid, int set, int tag) {
+        public Key(@NonNull Set<NetworkIdentity> ident, int uid, @State int set, int tag) {
             this(new NetworkIdentitySet(Objects.requireNonNull(ident)), uid, set, tag);
         }
 
diff --git a/packages/ConnectivityT/service/Android.bp b/packages/ConnectivityT/service/Android.bp
index 5100e7c..4b799c5 100644
--- a/packages/ConnectivityT/service/Android.bp
+++ b/packages/ConnectivityT/service/Android.bp
@@ -102,17 +102,16 @@
     ],
     path: "src",
     visibility: [
-        "//frameworks/opt/net/ethernet",
+        "//frameworks/opt/net/ethernet/tests",
     ],
 }
 
 // Connectivity-T common libraries.
 
+// TODO: remove this empty filegroup.
 filegroup {
     name: "services.connectivity-tiramisu-sources",
-    srcs: [
-        ":services.connectivity-ethernet-sources",
-    ],
+    srcs: [],
     path: "src",
     visibility: ["//frameworks/base/services/core"],
 }
@@ -120,6 +119,7 @@
 filegroup {
     name: "services.connectivity-tiramisu-updatable-sources",
     srcs: [
+        ":services.connectivity-ethernet-sources",
         ":services.connectivity-ipsec-sources",
         ":services.connectivity-netstats-sources",
         ":services.connectivity-nsd-sources",
diff --git a/packages/ConnectivityT/service/src/com/android/server/net/NetworkStatsService.java b/packages/ConnectivityT/service/src/com/android/server/net/NetworkStatsService.java
index b5e0539..1d22908 100644
--- a/packages/ConnectivityT/service/src/com/android/server/net/NetworkStatsService.java
+++ b/packages/ConnectivityT/service/src/com/android/server/net/NetworkStatsService.java
@@ -565,7 +565,7 @@
                 return new BpfMap<U32, U8>(UID_COUNTERSET_MAP_PATH, BpfMap.BPF_F_RDWR,
                         U32.class, U8.class);
             } catch (ErrnoException e) {
-                Log.wtf(TAG, "Cannot create uid counter set map: " + e);
+                Log.wtf(TAG, "Cannot open uid counter set map: " + e);
                 return null;
             }
         }
@@ -576,7 +576,7 @@
                 return new BpfMap<CookieTagMapKey, CookieTagMapValue>(COOKIE_TAG_MAP_PATH,
                         BpfMap.BPF_F_RDWR, CookieTagMapKey.class, CookieTagMapValue.class);
             } catch (ErrnoException e) {
-                Log.wtf(TAG, "Cannot create cookie tag map: " + e);
+                Log.wtf(TAG, "Cannot open cookie tag map: " + e);
                 return null;
             }
         }
@@ -587,7 +587,7 @@
                 return new BpfMap<StatsMapKey, StatsMapValue>(STATS_MAP_A_PATH,
                         BpfMap.BPF_F_RDWR, StatsMapKey.class, StatsMapValue.class);
             } catch (ErrnoException e) {
-                Log.wtf(TAG, "Cannot create stats map A: " + e);
+                Log.wtf(TAG, "Cannot open stats map A: " + e);
                 return null;
             }
         }
@@ -598,7 +598,7 @@
                 return new BpfMap<StatsMapKey, StatsMapValue>(STATS_MAP_B_PATH,
                         BpfMap.BPF_F_RDWR, StatsMapKey.class, StatsMapValue.class);
             } catch (ErrnoException e) {
-                Log.wtf(TAG, "Cannot create stats map B: " + e);
+                Log.wtf(TAG, "Cannot open stats map B: " + e);
                 return null;
             }
         }
@@ -609,7 +609,7 @@
                 return new BpfMap<UidStatsMapKey, StatsMapValue>(APP_UID_STATS_MAP_PATH,
                         BpfMap.BPF_F_RDWR, UidStatsMapKey.class, StatsMapValue.class);
             } catch (ErrnoException e) {
-                Log.wtf(TAG, "Cannot create app uid stats map: " + e);
+                Log.wtf(TAG, "Cannot open app uid stats map: " + e);
                 return null;
             }
         }
diff --git a/services/tests/servicestests/src/com/android/server/net/IpConfigStoreTest.java b/packages/ConnectivityT/tests/unit/java/com/android/server/net/IpConfigStoreTest.java
similarity index 92%
rename from services/tests/servicestests/src/com/android/server/net/IpConfigStoreTest.java
rename to packages/ConnectivityT/tests/unit/java/com/android/server/net/IpConfigStoreTest.java
index 2f77126..ad0be58 100644
--- a/services/tests/servicestests/src/com/android/server/net/IpConfigStoreTest.java
+++ b/packages/ConnectivityT/tests/unit/java/com/android/server/net/IpConfigStoreTest.java
@@ -48,10 +48,16 @@
  */
 @RunWith(AndroidJUnit4.class)
 public class IpConfigStoreTest {
+    private static final int KEY_CONFIG = 17;
+    private static final String IFACE_1 = "eth0";
+    private static final String IFACE_2 = "eth1";
+    private static final String IP_ADDR_1 = "192.168.1.10/24";
+    private static final String IP_ADDR_2 = "192.168.1.20/24";
+    private static final String DNS_IP_ADDR_1 = "1.2.3.4";
+    private static final String DNS_IP_ADDR_2 = "5.6.7.8";
 
     @Test
     public void backwardCompatibility2to3() throws IOException {
-        final int KEY_CONFIG = 17;
         ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
         DataOutputStream outputStream = new DataOutputStream(byteStream);
 
@@ -73,13 +79,6 @@
 
     @Test
     public void staticIpMultiNetworks() throws Exception {
-        final String IFACE_1 = "eth0";
-        final String IFACE_2 = "eth1";
-        final String IP_ADDR_1 = "192.168.1.10/24";
-        final String IP_ADDR_2 = "192.168.1.20/24";
-        final String DNS_IP_ADDR_1 = "1.2.3.4";
-        final String DNS_IP_ADDR_2 = "5.6.7.8";
-
         final ArrayList<InetAddress> dnsServers = new ArrayList<>();
         dnsServers.add(InetAddresses.parseNumericAddress(DNS_IP_ADDR_1));
         dnsServers.add(InetAddresses.parseNumericAddress(DNS_IP_ADDR_2));
@@ -144,11 +143,11 @@
 
     /** Synchronously writes into given byte steam */
     private static class MockedDelayedDiskWrite extends DelayedDiskWrite {
-        final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
+        final ByteArrayOutputStream mByteStream = new ByteArrayOutputStream();
 
         @Override
         public void write(String filePath, Writer w) {
-            DataOutputStream outputStream = new DataOutputStream(byteStream);
+            DataOutputStream outputStream = new DataOutputStream(mByteStream);
 
             try {
                 w.onWriteCalled(outputStream);
diff --git a/packages/SettingsLib/ActivityEmbedding/src/com/android/settingslib/activityembedding/ActivityEmbeddingUtils.java b/packages/SettingsLib/ActivityEmbedding/src/com/android/settingslib/activityembedding/ActivityEmbeddingUtils.java
index 44b3b4e..706aba3d 100644
--- a/packages/SettingsLib/ActivityEmbedding/src/com/android/settingslib/activityembedding/ActivityEmbeddingUtils.java
+++ b/packages/SettingsLib/ActivityEmbedding/src/com/android/settingslib/activityembedding/ActivityEmbeddingUtils.java
@@ -51,27 +51,34 @@
     }
 
     /**
-     * Whether current activity is embedded in the Settings app or not.
+     * Whether the current activity is embedded in the Settings app or not.
+     *
+     * @param activity Activity that needs the check
      */
     public static boolean isActivityEmbedded(Activity activity) {
         return SplitController.getInstance().isActivityEmbedded(activity);
     }
 
     /**
-     * Whether current activity is suggested to show back button or not.
+     * Whether the current activity should hide the navigate up button.
+     *
+     * @param activity             Activity that needs the check
+     * @param isSecondLayerPage indicates if the activity(page) is shown in the 2nd layer of
+     *                             Settings app
      */
-    public static boolean shouldHideBackButton(Activity activity, boolean isSecondaryLayerPage) {
+    public static boolean shouldHideNavigateUpButton(Activity activity, boolean isSecondLayerPage) {
         if (!BuildCompat.isAtLeastT()) {
             return false;
         }
-        if (!isSecondaryLayerPage) {
+        if (!isSecondLayerPage) {
             return false;
         }
-        final String shouldHideBackButton = Settings.Global.getString(activity.getContentResolver(),
-                "settings_hide_secondary_page_back_button_in_two_pane");
+        final String shouldHideNavigateUpButton =
+                Settings.Global.getString(activity.getContentResolver(),
+                        "settings_hide_second_layer_page_navigate_up_button_in_two_pane");
 
-        if (TextUtils.isEmpty(shouldHideBackButton)
-                || TextUtils.equals("true", shouldHideBackButton)) {
+        if (TextUtils.isEmpty(shouldHideNavigateUpButton)
+                || Boolean.parseBoolean(shouldHideNavigateUpButton)) {
             return isActivityEmbedded(activity);
         }
         return false;
diff --git a/packages/SettingsLib/res/values-v31/styles.xml b/packages/SettingsLib/res/values-v31/styles.xml
index 343de2c..0b703c9 100644
--- a/packages/SettingsLib/res/values-v31/styles.xml
+++ b/packages/SettingsLib/res/values-v31/styles.xml
@@ -15,7 +15,8 @@
   limitations under the License.
   -->
 <resources>
-    <style name="SettingsLibTabsTextAppearance" parent="@android:style/TextAppearance.DeviceDefault.Widget.ActionBar.Title">
+    <style name="SettingsLibTabsTextAppearance"
+           parent="@android:style/TextAppearance.DeviceDefault.Widget.ActionBar.Title">
         <item name="android:textSize">16sp</item>
     </style>
 
@@ -25,6 +26,7 @@
         <item name="android:layout_height">48dp</item>
         <item name="android:layout_marginStart">?android:attr/listPreferredItemPaddingStart</item>
         <item name="android:layout_marginEnd">?android:attr/listPreferredItemPaddingEnd</item>
+        <item name="tabMaxWidth">0dp</item>
         <item name="tabGravity">fill</item>
         <item name="tabBackground">@drawable/settingslib_tabs_background</item>
         <item name="tabIndicator">@drawable/settingslib_tabs_indicator_background</item>
diff --git a/packages/SettingsLib/res/values/strings.xml b/packages/SettingsLib/res/values/strings.xml
index e524405..579e3d7 100644
--- a/packages/SettingsLib/res/values/strings.xml
+++ b/packages/SettingsLib/res/values/strings.xml
@@ -1293,6 +1293,8 @@
     <string name="cancel">Cancel</string>
     <!-- Button label for generic OK action [CHAR LIMIT=20] -->
     <string name="okay">OK</string>
+    <!-- Button label for generic Done action, to be pressed when an action has been completed [CHAR LIMIT=20] -->
+    <string name="done">Done</string>
 
     <!-- Label for the settings activity for controlling apps that can schedule alarms [CHAR LIMIT=30] -->
     <string name="alarms_and_reminders_label">Alarms and reminders</string>
@@ -1342,7 +1344,9 @@
     <string name="notice_header" translatable="false"></string>
 
     <!-- Name of the phone device. [CHAR LIMIT=30] -->
-    <string name="media_transfer_this_device_name">This phone</string>
+    <string name="media_transfer_this_device_name" product="default">This phone</string>
+    <!-- Name of the tablet device. [CHAR LIMIT=30] -->
+    <string name="media_transfer_this_device_name" product="tablet">This tablet</string>
     <!-- Name of the phone device with an active remote session. [CHAR LIMIT=30] -->
     <string name="media_transfer_this_phone">This phone</string>
 
@@ -1572,4 +1576,10 @@
     <!-- Content description for a default user icon. [CHAR LIMIT=NONE] -->
     <string name="default_user_icon_description">Default user icon</string>
 
+    <!-- Title for the 'physical keyboard' settings screen. [CHAR LIMIT=35] -->
+    <string name="physical_keyboard_title">Physical keyboard</string>
+    <!-- Title for the keyboard layout preference dialog. [CHAR LIMIT=35] -->
+    <string name="keyboard_layout_dialog_title">Choose keyboard layout</string>
+    <!-- Label of the default keyboard layout.  [CHAR LIMIT=35] -->
+    <string name="keyboard_layout_default_label">Default</string>
 </resources>
diff --git a/packages/SettingsLib/src/com/android/settingslib/RestrictedPreferenceHelper.java b/packages/SettingsLib/src/com/android/settingslib/RestrictedPreferenceHelper.java
index 17db45d..e4efae2 100644
--- a/packages/SettingsLib/src/com/android/settingslib/RestrictedPreferenceHelper.java
+++ b/packages/SettingsLib/src/com/android/settingslib/RestrictedPreferenceHelper.java
@@ -23,7 +23,6 @@
 import android.app.admin.DevicePolicyManager;
 import android.content.Context;
 import android.content.res.TypedArray;
-import android.os.Build;
 import android.os.UserHandle;
 import android.text.TextUtils;
 import android.util.AttributeSet;
@@ -33,8 +32,6 @@
 import androidx.preference.Preference;
 import androidx.preference.PreferenceViewHolder;
 
-import com.android.internal.util.Preconditions;
-
 /**
  * Helper class for managing settings preferences that can be disabled
  * by device admins via user restrictions.
@@ -42,8 +39,8 @@
 public class RestrictedPreferenceHelper {
     private final Context mContext;
     private final Preference mPreference;
-    final String packageName;
-    final int uid;
+    String packageName;
+    int uid;
 
     private boolean mDisabledByAdmin;
     private EnforcedAdmin mEnforcedAdmin;
@@ -219,6 +216,11 @@
         return mDisabledByAppOps;
     }
 
+    public void updatePackageDetails(String packageName, int uid) {
+        this.packageName = packageName;
+        this.uid = uid;
+    }
+
     private void updateDisabledState() {
         if (!(mPreference instanceof RestrictedTopLevelPreference)) {
             mPreference.setEnabled(!(mDisabledByAdmin || mDisabledByAppOps));
diff --git a/packages/SettingsLib/src/com/android/settingslib/RestrictedSwitchPreference.java b/packages/SettingsLib/src/com/android/settingslib/RestrictedSwitchPreference.java
index c607d74..091e322 100644
--- a/packages/SettingsLib/src/com/android/settingslib/RestrictedSwitchPreference.java
+++ b/packages/SettingsLib/src/com/android/settingslib/RestrictedSwitchPreference.java
@@ -18,8 +18,11 @@
 
 import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
 
+import android.annotation.NonNull;
+import android.app.AppOpsManager;
 import android.content.Context;
 import android.content.res.TypedArray;
+import android.os.Process;
 import android.os.UserHandle;
 import android.util.AttributeSet;
 import android.util.TypedValue;
@@ -28,6 +31,7 @@
 import android.widget.LinearLayout;
 import android.widget.TextView;
 
+import androidx.annotation.VisibleForTesting;
 import androidx.core.content.res.TypedArrayUtils;
 import androidx.preference.PreferenceManager;
 import androidx.preference.PreferenceViewHolder;
@@ -39,6 +43,7 @@
  */
 public class RestrictedSwitchPreference extends SwitchPreference {
     RestrictedPreferenceHelper mHelper;
+    AppOpsManager mAppOpsManager;
     boolean mUseAdditionalSummary = false;
     CharSequence mRestrictedSwitchSummary;
     private int mIconSize;
@@ -90,6 +95,11 @@
         this(context, null);
     }
 
+    @VisibleForTesting
+    public void setAppOps(AppOpsManager appOps) {
+        mAppOpsManager = appOps;
+    }
+
     public void setIconSize(int iconSize) {
         mIconSize = iconSize;
     }
@@ -97,6 +107,12 @@
     @Override
     public void onBindViewHolder(PreferenceViewHolder holder) {
         super.onBindViewHolder(holder);
+        final View switchView = holder.findViewById(android.R.id.switch_widget);
+        if (switchView != null) {
+            final View rootView = switchView.getRootView();
+            rootView.setFilterTouchesWhenObscured(true);
+        }
+
         mHelper.onBindViewHolder(holder);
 
         CharSequence switchSummary;
@@ -164,11 +180,18 @@
 
     @Override
     public void setEnabled(boolean enabled) {
+        boolean changed = false;
         if (enabled && isDisabledByAdmin()) {
             mHelper.setDisabledByAdmin(null);
-            return;
+            changed = true;
         }
-        super.setEnabled(enabled);
+        if (enabled && isDisabledByAppOps()) {
+            mHelper.setDisabledByAppOps(false);
+            changed = true;
+        }
+        if (!changed) {
+            super.setEnabled(enabled);
+        }
     }
 
     public void setDisabledByAdmin(EnforcedAdmin admin) {
@@ -180,4 +203,38 @@
     public boolean isDisabledByAdmin() {
         return mHelper.isDisabledByAdmin();
     }
+
+    private void setDisabledByAppOps(boolean disabled) {
+        if (mHelper.setDisabledByAppOps(disabled)) {
+            notifyChanged();
+        }
+    }
+
+    public boolean isDisabledByAppOps() {
+        return mHelper.isDisabledByAppOps();
+    }
+
+    public int getUid() {
+        return mHelper != null ? mHelper.uid : Process.INVALID_UID;
+    }
+
+    public String getPackageName() {
+        return mHelper != null ? mHelper.packageName : null;
+    }
+
+    public void updateState(@NonNull String packageName, int uid, boolean isEnabled) {
+        mHelper.updatePackageDetails(packageName, uid);
+        if (mAppOpsManager == null) {
+            mAppOpsManager = getContext().getSystemService(AppOpsManager.class);
+        }
+        final int mode = mAppOpsManager.noteOpNoThrow(
+                AppOpsManager.OP_ACCESS_RESTRICTED_SETTINGS,
+                uid, packageName);
+        final boolean appOpsAllowed = mode == AppOpsManager.MODE_ALLOWED;
+        if (appOpsAllowed || isEnabled) {
+            setEnabled(true);
+        } else {
+            setDisabledByAppOps(true);
+        }
+    }
 }
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java
index 1c9d9cf..3d91c5a 100644
--- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java
+++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java
@@ -285,6 +285,12 @@
 
     public void disconnect() {
         synchronized (mProfileLock) {
+            if (getGroupId() != BluetoothCsipSetCoordinator.GROUP_ID_INVALID) {
+                for (CachedBluetoothDevice member : getMemberDevice()) {
+                    Log.d(TAG, "Disconnect the member(" + member.getAddress() + ")");
+                    member.disconnect();
+                }
+            }
             mDevice.disconnect();
         }
         // Disconnect  PBAP server in case its connected
@@ -399,6 +405,12 @@
             }
 
             mDevice.connect();
+            if (getGroupId() != BluetoothCsipSetCoordinator.GROUP_ID_INVALID) {
+                for (CachedBluetoothDevice member : getMemberDevice()) {
+                    Log.d(TAG, "connect the member(" + member.getAddress() + ")");
+                    member.connect();
+                }
+            }
         }
     }
 
diff --git a/packages/SettingsLib/src/com/android/settingslib/users/AvatarPickerActivity.java b/packages/SettingsLib/src/com/android/settingslib/users/AvatarPickerActivity.java
index 75bb70a..8a1e91b 100644
--- a/packages/SettingsLib/src/com/android/settingslib/users/AvatarPickerActivity.java
+++ b/packages/SettingsLib/src/com/android/settingslib/users/AvatarPickerActivity.java
@@ -106,13 +106,13 @@
 
         FooterButton secondaryButton =
                 new FooterButton.Builder(this)
-                        .setText("Cancel")
+                        .setText(getString(android.R.string.cancel))
                         .setListener(view -> cancel())
                         .build();
 
         mDoneButton =
                 new FooterButton.Builder(this)
-                        .setText("Done")
+                        .setText(getString(R.string.done))
                         .setListener(view -> mAdapter.returnSelectionResult())
                         .build();
         mDoneButton.setEnabled(false);
diff --git a/packages/SettingsLib/src/com/android/settingslib/wifi/WifiUtils.java b/packages/SettingsLib/src/com/android/settingslib/wifi/WifiUtils.java
index 4ab6542..9ef6bdf 100644
--- a/packages/SettingsLib/src/com/android/settingslib/wifi/WifiUtils.java
+++ b/packages/SettingsLib/src/com/android/settingslib/wifi/WifiUtils.java
@@ -43,6 +43,31 @@
     private static final int INVALID_RSSI = -127;
 
     /**
+     * The intent action shows Wi-Fi dialog to connect Wi-Fi network.
+     * <p>
+     * Input: The calling package should put the chosen
+     * com.android.wifitrackerlib.WifiEntry#getKey() to a string extra in the request bundle into
+     * the {@link #EXTRA_CHOSEN_WIFI_ENTRY_KEY}.
+     * <p>
+     * Output: Nothing.
+     */
+    @VisibleForTesting
+    static final String ACTION_WIFI_DIALOG = "com.android.settings.WIFI_DIALOG";
+
+    /**
+     * Specify a key that indicates the WifiEntry to be configured.
+     */
+    @VisibleForTesting
+    static final String EXTRA_CHOSEN_WIFI_ENTRY_KEY = "key_chosen_wifientry_key";
+
+    /**
+     * The lookup key for a boolean that indicates whether a chosen WifiEntry request to connect to.
+     * {@code true} means a chosen WifiEntry request to connect to.
+     */
+    @VisibleForTesting
+    static final String EXTRA_CONNECT_FOR_CALLER = "connect_for_caller";
+
+    /**
      * The intent action shows network details settings to allow configuration of Wi-Fi.
      * <p>
      * In some cases, a matching Activity may not exist, so ensure you
@@ -325,6 +350,19 @@
     }
 
     /**
+     * Returns the Intent for Wi-Fi dialog.
+     *
+     * @param key              The Wi-Fi entry key
+     * @param connectForCaller True if a chosen WifiEntry request to connect to
+     */
+    public static Intent getWifiDialogIntent(String key, boolean connectForCaller) {
+        final Intent intent = new Intent(ACTION_WIFI_DIALOG);
+        intent.putExtra(EXTRA_CHOSEN_WIFI_ENTRY_KEY, key);
+        intent.putExtra(EXTRA_CONNECT_FOR_CALLER, connectForCaller);
+        return intent;
+    }
+
+    /**
      * Returns the Intent for Wi-Fi network details settings.
      *
      * @param key The Wi-Fi entry key
diff --git a/packages/SettingsLib/tests/integ/src/com/android/settingslib/users/AvatarPhotoControllerTest.java b/packages/SettingsLib/tests/integ/src/com/android/settingslib/users/AvatarPhotoControllerTest.java
index 1d08711..d988111 100644
--- a/packages/SettingsLib/tests/integ/src/com/android/settingslib/users/AvatarPhotoControllerTest.java
+++ b/packages/SettingsLib/tests/integ/src/com/android/settingslib/users/AvatarPhotoControllerTest.java
@@ -89,6 +89,10 @@
 
     @After
     public void tearDown() {
+        String[] entries = mImagesDir.list();
+        for (String entry : entries) {
+            new File(mImagesDir, entry).delete();
+        }
         mImagesDir.delete();
     }
 
@@ -233,7 +237,8 @@
     public void internalCropUsedIfNoSystemCropperFound() throws IOException {
         when(mMockAvatarUi.startSystemActivityForResult(any(), anyInt())).thenReturn(false);
 
-        new File(mImagesDir, "file.txt").createNewFile();
+        File file = new File(mImagesDir, "file.txt");
+        saveBitmapToFile(file);
 
         Intent intent = new Intent();
         intent.setData(Uri.parse(
@@ -241,10 +246,6 @@
         mController.onActivityResult(
                 REQUEST_CODE_TAKE_PHOTO, Activity.RESULT_OK, intent);
 
-        Intent startIntent = verifyStartSystemActivityForResult(
-                "com.android.camera.action.CROP", REQUEST_CODE_CROP_PHOTO);
-        assertThat(startIntent.getData()).isNotEqualTo(mTakePhotoUri);
-
         verify(mMockAvatarUi, timeout(TIMEOUT_MILLIS)).returnUriResult(mCropPhotoUri);
 
         InputStream imageStream = mContext.getContentResolver().openInputStream(mCropPhotoUri);
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/wifi/WifiUtilsTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/wifi/WifiUtilsTest.java
index e7b3fe9..6956105 100644
--- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/wifi/WifiUtilsTest.java
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/wifi/WifiUtilsTest.java
@@ -156,6 +156,27 @@
     }
 
     @Test
+    public void getWifiDialogIntent_returnsCorrectValues() {
+        String key = "test_key";
+
+        // Test that connectForCaller is true.
+        Intent intent = WifiUtils.getWifiDialogIntent(key, true /* connectForCaller */);
+
+        assertThat(intent.getAction()).isEqualTo(WifiUtils.ACTION_WIFI_DIALOG);
+        assertThat(intent.getStringExtra(WifiUtils.EXTRA_CHOSEN_WIFI_ENTRY_KEY)).isEqualTo(key);
+        assertThat(intent.getBooleanExtra(WifiUtils.EXTRA_CONNECT_FOR_CALLER, true))
+                .isEqualTo(true /* connectForCaller */);
+
+        // Test that connectForCaller is false.
+        intent = WifiUtils.getWifiDialogIntent(key, false /* connectForCaller */);
+
+        assertThat(intent.getAction()).isEqualTo(WifiUtils.ACTION_WIFI_DIALOG);
+        assertThat(intent.getStringExtra(WifiUtils.EXTRA_CHOSEN_WIFI_ENTRY_KEY)).isEqualTo(key);
+        assertThat(intent.getBooleanExtra(WifiUtils.EXTRA_CONNECT_FOR_CALLER, true))
+                .isEqualTo(false /* connectForCaller */);
+    }
+
+    @Test
     public void getWifiDetailsSettingsIntent_returnsCorrectValues() {
         final String key = "test_key";
 
diff --git a/packages/SettingsProvider/src/android/provider/settings/validators/SecureSettingsValidators.java b/packages/SettingsProvider/src/android/provider/settings/validators/SecureSettingsValidators.java
index fa3360c..cbd71c0 100644
--- a/packages/SettingsProvider/src/android/provider/settings/validators/SecureSettingsValidators.java
+++ b/packages/SettingsProvider/src/android/provider/settings/validators/SecureSettingsValidators.java
@@ -328,7 +328,5 @@
             return true;
         });
         VALIDATORS.put(Secure.ODI_CAPTIONS_VOLUME_UI_ENABLED, BOOLEAN_VALIDATOR);
-        VALIDATORS.put(Secure.FAST_PAIR_SCAN_ENABLED, BOOLEAN_VALIDATOR);
-
     }
 }
diff --git a/packages/Shell/AndroidManifest.xml b/packages/Shell/AndroidManifest.xml
index dae63a8..e93371d 100644
--- a/packages/Shell/AndroidManifest.xml
+++ b/packages/Shell/AndroidManifest.xml
@@ -536,10 +536,11 @@
     <uses-permission android:name="android.permission.WIFI_ACCESS_COEX_UNSAFE_CHANNELS" />
     <uses-permission android:name="android.permission.WIFI_UPDATE_COEX_UNSAFE_CHANNELS" />
     <uses-permission android:name="android.permission.NEARBY_WIFI_DEVICES" />
+    <uses-permission android:name="android.permission.MANAGE_WIFI_INTERFACES" />
     <uses-permission android:name="android.permission.OVERRIDE_WIFI_CONFIG" />
     <!-- Permission needed for CTS test - ConcurrencyTest#testP2pExternalApprover
-         P2P external approver API sets require MANAGE_WIFI_AUTO_JOIN permission. -->
-    <uses-permission android:name="android.permission.MANAGE_WIFI_AUTO_JOIN" />
+         P2P external approver API sets require MANAGE_WIFI_NETWORK_SELECTION permission. -->
+    <uses-permission android:name="android.permission.MANAGE_WIFI_NETWORK_SELECTION" />
 
     <!-- Permission required for CTS tests to enable/disable rate limiting toasts. -->
     <uses-permission android:name="android.permission.MANAGE_TOAST_RATE_LIMITING" />
@@ -668,6 +669,9 @@
     <!-- Permission required for CTS test - CtsTelephonyTestCases -->
     <uses-permission android:name="android.permission.BIND_TELECOM_CONNECTION_SERVICE" />
 
+    <!-- Permission required for CTS test - CtsAppEnumerationTestCases -->
+    <uses-permission android:name="android.permission.MAKE_UID_VISIBLE" />
+
     <application android:label="@string/app_label"
                 android:theme="@android:style/Theme.DeviceDefault.DayNight"
                 android:defaultToDeviceProtectedStorage="true"
diff --git a/packages/SystemUI/AndroidManifest.xml b/packages/SystemUI/AndroidManifest.xml
index 7f8b2f5..4b45cc3 100644
--- a/packages/SystemUI/AndroidManifest.xml
+++ b/packages/SystemUI/AndroidManifest.xml
@@ -314,6 +314,11 @@
     <!-- To change system captions state -->
     <uses-permission android:name="android.permission.SET_SYSTEM_AUDIO_CAPTION" />
 
+    <!-- Compat framework -->
+    <uses-permission android:name="android.permission.LOG_COMPAT_CHANGE" />
+    <uses-permission android:name="android.permission.READ_COMPAT_CHANGE_CONFIG" />
+    <uses-permission android:name="android.permission.READ_DEVICE_CONFIG" />
+
     <protected-broadcast android:name="com.android.settingslib.action.REGISTER_SLICE_RECEIVER" />
     <protected-broadcast android:name="com.android.settingslib.action.UNREGISTER_SLICE_RECEIVER" />
     <protected-broadcast android:name="com.android.settings.flashlight.action.FLASHLIGHT_CHANGED" />
@@ -794,7 +799,7 @@
                   android:noHistory="true"
                   android:showForAllUsers="true"
                   android:finishOnTaskLaunch="true"
-                  android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation|keyboard|keyboardHidden"
+                  android:configChanges="screenSize|smallestScreenSize|screenLayout|keyboard|keyboardHidden"
                   android:visibleToInstantApps="true">
         </activity>
 
@@ -805,7 +810,7 @@
                   android:showForAllUsers="true"
                   android:finishOnTaskLaunch="true"
                   android:launchMode="singleInstance"
-                  android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation|keyboard|keyboardHidden"
+                  android:configChanges="screenSize|smallestScreenSize|screenLayout|keyboard|keyboardHidden"
                   android:visibleToInstantApps="true">
         </activity>
 
diff --git a/packages/SystemUI/animation/res/values/ids.xml b/packages/SystemUI/animation/res/values/ids.xml
index ef60a24..4e9a4be 100644
--- a/packages/SystemUI/animation/res/values/ids.xml
+++ b/packages/SystemUI/animation/res/values/ids.xml
@@ -15,5 +15,14 @@
      limitations under the License.
 -->
 <resources>
+    <!-- DialogLaunchAnimator -->
     <item type="id" name="launch_animation_running"/>
+
+    <!-- ViewBoundsAnimator -->
+    <item type="id" name="tag_animator"/>
+    <item type="id" name="tag_layout_listener"/>
+    <item type="id" name="tag_override_bottom"/>
+    <item type="id" name="tag_override_left"/>
+    <item type="id" name="tag_override_right"/>
+    <item type="id" name="tag_override_top"/>
 </resources>
\ No newline at end of file
diff --git a/packages/SystemUI/animation/src/com/android/systemui/animation/ViewBoundAnimator.kt b/packages/SystemUI/animation/src/com/android/systemui/animation/ViewBoundAnimator.kt
new file mode 100644
index 0000000..5593fdf
--- /dev/null
+++ b/packages/SystemUI/animation/src/com/android/systemui/animation/ViewBoundAnimator.kt
@@ -0,0 +1,328 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.animation
+
+import android.animation.Animator
+import android.animation.AnimatorListenerAdapter
+import android.animation.ObjectAnimator
+import android.animation.PropertyValuesHolder
+import android.util.IntProperty
+import android.view.View
+import android.view.ViewGroup
+import android.view.animation.Interpolator
+
+/**
+ * A class that allows changes in bounds within a view hierarchy to animate seamlessly between the
+ * start and end state.
+ */
+class ViewBoundAnimator {
+    // TODO(b/221418522): make this private once it can't be passed as an arg anymore.
+    enum class Bound(val label: String, val overrideTag: Int) {
+        LEFT("left", R.id.tag_override_left) {
+            override fun setValue(view: View, value: Int) {
+                view.left = value
+            }
+
+            override fun getValue(view: View): Int {
+                return view.left
+            }
+        },
+        TOP("top", R.id.tag_override_top) {
+            override fun setValue(view: View, value: Int) {
+                view.top = value
+            }
+
+            override fun getValue(view: View): Int {
+                return view.top
+            }
+        },
+        RIGHT("right", R.id.tag_override_right) {
+            override fun setValue(view: View, value: Int) {
+                view.right = value
+            }
+
+            override fun getValue(view: View): Int {
+                return view.right
+            }
+        },
+        BOTTOM("bottom", R.id.tag_override_bottom) {
+            override fun setValue(view: View, value: Int) {
+                view.bottom = value
+            }
+
+            override fun getValue(view: View): Int {
+                return view.bottom
+            }
+        };
+
+        abstract fun setValue(view: View, value: Int)
+        abstract fun getValue(view: View): Int
+    }
+
+    companion object {
+        /** Default values for the animation. These can all be overridden at call time. */
+        private const val DEFAULT_DURATION = 500L
+        private val DEFAULT_INTERPOLATOR = Interpolators.EMPHASIZED
+        private val DEFAULT_BOUNDS = setOf(Bound.LEFT, Bound.TOP, Bound.RIGHT, Bound.BOTTOM)
+
+        /** The properties used to animate the view bounds. */
+        private val PROPERTIES = mapOf(
+            Bound.LEFT to createViewProperty(Bound.LEFT),
+            Bound.TOP to createViewProperty(Bound.TOP),
+            Bound.RIGHT to createViewProperty(Bound.RIGHT),
+            Bound.BOTTOM to createViewProperty(Bound.BOTTOM)
+        )
+
+        private fun createViewProperty(bound: Bound): IntProperty<View> {
+            return object : IntProperty<View>(bound.label) {
+                override fun setValue(view: View, value: Int) {
+                    setBound(view, bound, value)
+                }
+
+                override fun get(view: View): Int {
+                    return getBound(view, bound) ?: bound.getValue(view)
+                }
+            }
+        }
+
+        /**
+         * Instruct the animator to watch for changes to the layout of [rootView] and its children
+         * and animate them. The animation can be limited to a subset of [bounds]. It uses the
+         * given [interpolator] and [duration].
+         *
+         * If a new layout change happens while an animation is already in progress, the animation
+         * is updated to continue from the current values to the new end state.
+         *
+         * The animator continues to respond to layout changes until [stopAnimating] is called.
+         *
+         * Successive calls to this method override the previous settings ([interpolator] and
+         * [duration]). The changes take effect on the next animation.
+         *
+         * TODO(b/221418522): remove the ability to select which bounds to animate and always
+         * animate all of them.
+         */
+        @JvmOverloads
+        fun animate(
+            rootView: View,
+            bounds: Set<Bound> = DEFAULT_BOUNDS,
+            interpolator: Interpolator = DEFAULT_INTERPOLATOR,
+            duration: Long = DEFAULT_DURATION
+        ) {
+            animate(rootView, bounds, interpolator, duration, false /* ephemeral */)
+        }
+
+        /**
+         * Like [animate], but only takes effect on the next layout update, then unregisters itself
+         * once the first animation is complete.
+         *
+         * TODO(b/221418522): remove the ability to select which bounds to animate and always
+         * animate all of them.
+         */
+        @JvmOverloads
+        fun animateNextUpdate(
+            rootView: View,
+            bounds: Set<Bound> = DEFAULT_BOUNDS,
+            interpolator: Interpolator = DEFAULT_INTERPOLATOR,
+            duration: Long = DEFAULT_DURATION
+        ) {
+            animate(rootView, bounds, interpolator, duration, true /* ephemeral */)
+        }
+
+        private fun animate(
+            rootView: View,
+            bounds: Set<Bound>,
+            interpolator: Interpolator,
+            duration: Long,
+            ephemeral: Boolean
+        ) {
+            val listener = createListener(bounds, interpolator, duration, ephemeral)
+            recursivelyAddListener(rootView, listener)
+        }
+
+        /**
+         * Instruct the animator to stop watching for changes to the layout of [rootView] and its
+         * children.
+         *
+         * Any animations already in progress continue until their natural conclusion.
+         */
+        fun stopAnimating(rootView: View) {
+            val listener = rootView.getTag(R.id.tag_layout_listener)
+            if (listener != null && listener is View.OnLayoutChangeListener) {
+                rootView.setTag(R.id.tag_layout_listener, null /* tag */)
+                rootView.removeOnLayoutChangeListener(listener)
+            }
+
+            if (rootView is ViewGroup) {
+                for (i in 0 until rootView.childCount) {
+                    stopAnimating(rootView.getChildAt(i))
+                }
+            }
+        }
+
+        private fun createListener(
+            bounds: Set<Bound>,
+            interpolator: Interpolator,
+            duration: Long,
+            ephemeral: Boolean
+        ): View.OnLayoutChangeListener {
+            return object : View.OnLayoutChangeListener {
+                override fun onLayoutChange(
+                    view: View?,
+                    left: Int,
+                    top: Int,
+                    right: Int,
+                    bottom: Int,
+                    oldLeft: Int,
+                    oldTop: Int,
+                    oldRight: Int,
+                    oldBottom: Int
+                ) {
+                    if (view == null) return
+
+                    val startLeft = getBound(view, Bound.LEFT) ?: oldLeft
+                    val startTop = getBound(view, Bound.TOP) ?: oldTop
+                    val startRight = getBound(view, Bound.RIGHT) ?: oldRight
+                    val startBottom = getBound(view, Bound.BOTTOM) ?: oldBottom
+
+                    (view.getTag(R.id.tag_animator) as? ObjectAnimator)?.cancel()
+
+                    if (view.visibility == View.GONE || view.visibility == View.INVISIBLE) {
+                        setBound(view, Bound.LEFT, left)
+                        setBound(view, Bound.TOP, top)
+                        setBound(view, Bound.RIGHT, right)
+                        setBound(view, Bound.BOTTOM, bottom)
+                        return
+                    }
+
+                    val startValues = mapOf(
+                        Bound.LEFT to startLeft,
+                        Bound.TOP to startTop,
+                        Bound.RIGHT to startRight,
+                        Bound.BOTTOM to startBottom
+                    )
+                    val endValues = mapOf(
+                        Bound.LEFT to left,
+                        Bound.TOP to top,
+                        Bound.RIGHT to right,
+                        Bound.BOTTOM to bottom
+                    )
+
+                    val boundsToAnimate = bounds.toMutableSet()
+                    if (left == startLeft) boundsToAnimate.remove(Bound.LEFT)
+                    if (top == startTop) boundsToAnimate.remove(Bound.TOP)
+                    if (right == startRight) boundsToAnimate.remove(Bound.RIGHT)
+                    if (bottom == startBottom) boundsToAnimate.remove(Bound.BOTTOM)
+
+                    if (boundsToAnimate.isNotEmpty()) {
+                        startAnimation(
+                            view,
+                            boundsToAnimate,
+                            startValues,
+                            endValues,
+                            interpolator,
+                            duration,
+                            ephemeral
+                        )
+                    }
+                }
+            }
+        }
+
+        private fun recursivelyAddListener(view: View, listener: View.OnLayoutChangeListener) {
+            // Make sure that only one listener is active at a time.
+            val oldListener = view.getTag(R.id.tag_layout_listener)
+            if (oldListener != null && oldListener is View.OnLayoutChangeListener) {
+                view.removeOnLayoutChangeListener(oldListener)
+            }
+
+            view.addOnLayoutChangeListener(listener)
+            view.setTag(R.id.tag_layout_listener, listener)
+            if (view is ViewGroup) {
+                for (i in 0 until view.childCount) {
+                    recursivelyAddListener(view.getChildAt(i), listener)
+                }
+            }
+        }
+
+        private fun getBound(view: View, bound: Bound): Int? {
+            return view.getTag(bound.overrideTag) as? Int
+        }
+
+        private fun setBound(view: View, bound: Bound, value: Int) {
+            view.setTag(bound.overrideTag, value)
+            bound.setValue(view, value)
+        }
+
+        /**
+         * Initiates the animation of a single bound by creating the animator, registering it with
+         * the [view], and starting it. If [ephemeral], the layout change listener is unregistered
+         * at the end of the animation, so no more animations happen.
+         */
+        private fun startAnimation(
+            view: View,
+            bounds: Set<Bound>,
+            startValues: Map<Bound, Int>,
+            endValues: Map<Bound, Int>,
+            interpolator: Interpolator,
+            duration: Long,
+            ephemeral: Boolean
+        ) {
+            val propertyValuesHolders = buildList {
+                bounds.forEach { bound ->
+                    add(
+                        PropertyValuesHolder.ofInt(
+                            PROPERTIES[bound],
+                            startValues.getValue(bound),
+                            endValues.getValue(bound)
+                        )
+                    )
+                }
+            }.toTypedArray()
+
+            val animator = ObjectAnimator.ofPropertyValuesHolder(view, *propertyValuesHolders)
+            animator.interpolator = interpolator
+            animator.duration = duration
+            animator.addListener(object : AnimatorListenerAdapter() {
+                var cancelled = false
+
+                override fun onAnimationEnd(animation: Animator) {
+                    view.setTag(R.id.tag_animator, null /* tag */)
+                    bounds.forEach { view.setTag(it.overrideTag, null /* tag */) }
+
+                    // When an animation is cancelled, a new one might be taking over. We shouldn't
+                    // unregister the listener yet.
+                    if (ephemeral && !cancelled) {
+                        val listener = view.getTag(R.id.tag_layout_listener)
+                        if (listener != null && listener is View.OnLayoutChangeListener) {
+                            view.setTag(R.id.tag_layout_listener, null /* tag */)
+                            view.removeOnLayoutChangeListener(listener)
+                        }
+                    }
+                }
+
+                override fun onAnimationCancel(animation: Animator?) {
+                    cancelled = true
+                }
+            })
+
+            bounds.forEach { bound -> setBound(view, bound, startValues.getValue(bound)) }
+
+            view.setTag(R.id.tag_animator, animator)
+            animator.start()
+        }
+    }
+}
diff --git a/packages/SystemUI/res-keyguard/drawable/media_squiggly_progress.xml b/packages/SystemUI/res-keyguard/drawable/media_squiggly_progress.xml
new file mode 100644
index 0000000..9e61236
--- /dev/null
+++ b/packages/SystemUI/res-keyguard/drawable/media_squiggly_progress.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2022 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+<com.android.systemui.media.SquigglyProgress />
\ No newline at end of file
diff --git a/packages/SystemUI/res-keyguard/values/styles.xml b/packages/SystemUI/res-keyguard/values/styles.xml
index 6375698..625ffd3 100644
--- a/packages/SystemUI/res-keyguard/values/styles.xml
+++ b/packages/SystemUI/res-keyguard/values/styles.xml
@@ -17,13 +17,14 @@
 */
 -->
 
-<resources xmlns:android="http://schemas.android.com/apk/res/android">
+<resources xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:androidprv="http://schemas.android.com/apk/prv/res/android">
     <!-- Keyguard PIN pad styles -->
     <style name="Keyguard.TextView" parent="@android:style/Widget.DeviceDefault.TextView">
         <item name="android:textSize">@dimen/kg_status_line_font_size</item>
     </style>
     <style name="Keyguard.TextView.EmergencyButton" parent="Theme.SystemUI">
-        <item name="android:textColor">?android:attr/textColorPrimaryInverse</item>
+        <item name="android:textColor">?androidprv:attr/textColorOnAccent</item>
         <item name="android:textSize">14dp</item>
         <item name="android:background">@drawable/kg_emergency_button_background</item>
         <item name="android:fontFamily">@*android:string/config_headlineFontFamily</item>
diff --git a/packages/SystemUI/res/layout/clipboard_edit_text_activity.xml b/packages/SystemUI/res/layout/clipboard_edit_text_activity.xml
index 8f6753a..da4088f 100644
--- a/packages/SystemUI/res/layout/clipboard_edit_text_activity.xml
+++ b/packages/SystemUI/res/layout/clipboard_edit_text_activity.xml
@@ -36,6 +36,7 @@
         android:tooltipText="@*android:string/share"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintTop_toTopOf="@+id/copy_button"
+        android:tint="?android:attr/textColorPrimary"
         android:src="@drawable/ic_screenshot_share" />
 
     <ScrollView
diff --git a/packages/SystemUI/res/layout/clipboard_overlay.xml b/packages/SystemUI/res/layout/clipboard_overlay.xml
index c58c001..2782300 100644
--- a/packages/SystemUI/res/layout/clipboard_overlay.xml
+++ b/packages/SystemUI/res/layout/clipboard_overlay.xml
@@ -26,7 +26,7 @@
         android:layout_width="match_parent"
         android:layout_gravity="bottom"
         android:src="@drawable/overlay_actions_background_protection"/>
-    <com.android.systemui.clipboardoverlay.DraggableConstraintLayout
+    <com.android.systemui.screenshot.DraggableConstraintLayout
         android:id="@+id/clipboard_ui"
         android:theme="@style/FloatingOverlay"
         android:layout_width="match_parent"
@@ -146,5 +146,5 @@
                 android:layout_margin="@dimen/overlay_dismiss_button_margin"
                 android:src="@drawable/overlay_cancel"/>
         </FrameLayout>
-    </com.android.systemui.clipboardoverlay.DraggableConstraintLayout>
+    </com.android.systemui.screenshot.DraggableConstraintLayout>
 </FrameLayout>
\ No newline at end of file
diff --git a/packages/SystemUI/res/layout/dream_overlay_complication_clock_date.xml b/packages/SystemUI/res/layout/dream_overlay_complication_clock_date.xml
index 91d81a2..cb63300 100644
--- a/packages/SystemUI/res/layout/dream_overlay_complication_clock_date.xml
+++ b/packages/SystemUI/res/layout/dream_overlay_complication_clock_date.xml
@@ -19,6 +19,7 @@
     android:id="@+id/date_view"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
+    android:paddingHorizontal="@dimen/dream_overlay_complication_shadow_padding"
     android:gravity="center_horizontal"
     android:textColor="@android:color/white"
     android:shadowColor="@color/keyguard_shadow_color"
diff --git a/packages/SystemUI/res/layout/dream_overlay_complication_weather.xml b/packages/SystemUI/res/layout/dream_overlay_complication_weather.xml
index 3900ea5..76fe58c 100644
--- a/packages/SystemUI/res/layout/dream_overlay_complication_weather.xml
+++ b/packages/SystemUI/res/layout/dream_overlay_complication_weather.xml
@@ -19,6 +19,7 @@
     android:id="@+id/weather_view"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
+    android:paddingHorizontal="@dimen/dream_overlay_complication_shadow_padding"
     android:textColor="@android:color/white"
     android:shadowColor="@color/keyguard_shadow_color"
     android:shadowRadius="?attr/shadowRadius"
diff --git a/packages/SystemUI/res/layout/media_output_list_item.xml b/packages/SystemUI/res/layout/media_output_list_item.xml
index 806804f..eeb37c7 100644
--- a/packages/SystemUI/res/layout/media_output_list_item.xml
+++ b/packages/SystemUI/res/layout/media_output_list_item.xml
@@ -62,20 +62,22 @@
             android:layout_height="wrap_content"
             android:layout_gravity="center_vertical|start"
             android:layout_marginStart="56dp"
+            android:layout_marginEnd="56dp"
             android:ellipsize="end"
             android:maxLines="1"
             android:fontFamily="@*android:string/config_headlineFontFamilyMedium"
             android:textSize="16sp"/>
 
-        <RelativeLayout
+        <LinearLayout
             android:id="@+id/two_line_layout"
+            android:orientation="vertical"
             android:layout_width="wrap_content"
             android:layout_gravity="center_vertical|start"
             android:layout_height="48dp"
+            android:layout_marginEnd="56dp"
             android:layout_marginStart="56dp">
             <TextView
                 android:id="@+id/two_line_title"
-                android:gravity="center_vertical"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:ellipsize="end"
@@ -85,18 +87,15 @@
                 android:textSize="16sp"/>
             <TextView
                 android:id="@+id/subtitle"
-                android:layout_gravity="center_vertical"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
-                android:layout_marginTop="4dp"
-                android:layout_alignParentBottom="true"
                 android:ellipsize="end"
                 android:maxLines="1"
                 android:textColor="@color/media_dialog_inactive_item_main_content"
                 android:textSize="14sp"
                 android:fontFamily="@*android:string/config_bodyFontFamily"
                 android:visibility="gone"/>
-        </RelativeLayout>
+        </LinearLayout>
 
         <ProgressBar
             android:id="@+id/volume_indeterminate_progress"
diff --git a/packages/SystemUI/res/layout/media_session_view.xml b/packages/SystemUI/res/layout/media_session_view.xml
index 9ad0152..f030f31 100644
--- a/packages/SystemUI/res/layout/media_session_view.xml
+++ b/packages/SystemUI/res/layout/media_session_view.xml
@@ -44,6 +44,7 @@
         android:background="@drawable/qs_media_scrim"
         />
 
+    <!-- Guideline for output switcher -->
     <androidx.constraintlayout.widget.Guideline
         android:id="@+id/center_vertical_guideline"
         android:layout_width="wrap_content"
@@ -51,6 +52,14 @@
         android:orientation="vertical"
         app:layout_constraintGuide_percent="0.6" />
 
+    <!-- Guideline for action buttons (collapsed view only) -->
+    <androidx.constraintlayout.widget.Guideline
+        android:id="@+id/action_button_guideline"
+        android:layout_width="0dp"
+        android:layout_height="0dp"
+        android:orientation="vertical"
+        app:layout_constraintGuide_end="@dimen/qs_media_session_collapsed_guideline" />
+
     <!-- App icon -->
     <com.android.internal.widget.CachingIconView
         android:id="@+id/icon"
@@ -118,15 +127,7 @@
         android:singleLine="true"
         android:textSize="16sp"
         android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:layout_marginTop="20dp"
-        android:layout_marginStart="@dimen/qs_media_padding"
-        android:layout_marginEnd="@dimen/qs_media_padding"
-        app:layout_constrainedWidth="true"
-        app:layout_constraintTop_toBottomOf="@id/icon"
-        app:layout_constraintStart_toStartOf="parent"
-        app:layout_constraintEnd_toStartOf="@id/actionPlayPause"
-        app:layout_constraintHorizontal_bias="0" />
+        android:layout_height="wrap_content" />
 
     <!-- Artist name -->
     <TextView
@@ -136,15 +137,7 @@
         style="@style/MediaPlayer.Subtitle"
         android:textSize="14sp"
         android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:layout_marginEnd="@dimen/qs_media_padding"
-        app:layout_constrainedWidth="true"
-        android:layout_marginTop="0dp"
-        app:layout_constraintTop_toBottomOf="@id/header_title"
-        app:layout_constraintStart_toStartOf="@id/header_title"
-        app:layout_constraintEnd_toStartOf="@id/actionPlayPause"
-        app:layout_constraintBottom_toBottomOf="@id/actionPlayPause"
-        app:layout_constraintHorizontal_bias="0" />
+        android:layout_height="wrap_content" />
 
     <ImageButton
         android:id="@+id/actionPlayPause"
@@ -153,9 +146,33 @@
         android:layout_height="48dp"
         android:layout_marginStart="@dimen/qs_media_padding"
         android:layout_marginEnd="@dimen/qs_media_padding"
-        android:layout_marginTop="0dp"
-        android:layout_marginBottom="0dp" />
+         />
 
+    <!-- See comment in media_session_collapsed.xml for how these barriers are used -->
+    <androidx.constraintlayout.widget.Barrier
+        android:id="@+id/media_action_barrier"
+        android:layout_width="0dp"
+        android:layout_height="0dp"
+        android:orientation="vertical"
+        app:layout_constraintTop_toBottomOf="@id/header_title"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:barrierDirection="start"
+        app:constraint_referenced_ids="actionPrev,media_progress_bar,actionNext,action0,action1,action2,action3,action4"
+        />
+    <androidx.constraintlayout.widget.Barrier
+        android:id="@+id/media_action_barrier_end"
+        android:layout_width="0dp"
+        android:layout_height="0dp"
+        android:orientation="vertical"
+        app:layout_constraintTop_toBottomOf="@id/media_seamless"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:barrierDirection="end"
+        app:constraint_referenced_ids="actionPrev,media_progress_bar,actionNext,action0,action1,action2,action3,action4"
+        app:layout_constraintStart_toStartOf="parent"
+        />
+
+    <!-- Button visibility will be controlled in code -->
     <ImageButton
         android:id="@+id/actionPrev"
         style="@style/MediaPlayer.SessionAction"
@@ -163,10 +180,9 @@
         android:layout_height="48dp"
         android:layout_marginStart="4dp"
         android:layout_marginEnd="0dp"
-        android:layout_marginBottom="0dp"
+        android:layout_marginBottom="@dimen/qs_media_padding"
         android:layout_marginTop="0dp"
-        app:layout_constraintHorizontal_bias="1"
-        app:layout_constraintHorizontal_chainStyle="packed" />
+        />
 
     <!-- Seek Bar -->
     <!-- As per Material Design on Bidirectionality, this is forced to LTR in code -->
@@ -174,12 +190,12 @@
         android:id="@+id/media_progress_bar"
         style="@style/MediaPlayer.ProgressBar"
         android:layout_width="0dp"
-        android:layout_height="wrap_content"
+        android:layout_height="48dp"
         android:paddingTop="@dimen/qs_media_session_enabled_seekbar_vertical_padding"
         android:paddingBottom="12dp"
         android:maxHeight="@dimen/qs_media_enabled_seekbar_height"
         android:splitTrack="false"
-        android:layout_marginBottom="0dp"
+        android:layout_marginBottom="@dimen/qs_media_padding"
         android:layout_marginTop="0dp"
         android:layout_marginStart="0dp"
         android:layout_marginEnd="0dp" />
@@ -191,29 +207,58 @@
         android:layout_height="48dp"
         android:layout_marginStart="0dp"
         android:layout_marginEnd="@dimen/qs_media_action_spacing"
-        android:layout_marginBottom="0dp"
+        android:layout_marginBottom="@dimen/qs_media_padding"
         android:layout_marginTop="0dp" />
 
     <ImageButton
-        android:id="@+id/actionStart"
+        android:id="@+id/action0"
         style="@style/MediaPlayer.SessionAction"
         android:layout_width="48dp"
         android:layout_height="48dp"
         android:layout_marginStart="@dimen/qs_media_action_spacing"
         android:layout_marginEnd="@dimen/qs_media_action_spacing"
-        android:layout_marginBottom="0dp"
-        android:layout_marginTop="0dp" />
+        android:layout_marginBottom="@dimen/qs_media_padding"
+        android:layout_marginTop="0dp"/>
 
     <ImageButton
-        android:id="@+id/actionEnd"
+        android:id="@+id/action1"
         style="@style/MediaPlayer.SessionAction"
         android:layout_width="48dp"
         android:layout_height="48dp"
         android:layout_marginStart="@dimen/qs_media_action_spacing"
         android:layout_marginEnd="4dp"
-        android:layout_marginBottom="0dp"
-        android:layout_marginTop="0dp"
-        app:layout_constraintHorizontal_chainStyle="packed" />
+        android:layout_marginBottom="@dimen/qs_media_padding"
+        android:layout_marginTop="0dp" />
+
+    <ImageButton
+        android:id="@+id/action2"
+        style="@style/MediaPlayer.SessionAction"
+        android:layout_width="48dp"
+        android:layout_height="48dp"
+        android:layout_marginStart="@dimen/qs_media_action_spacing"
+        android:layout_marginEnd="4dp"
+        android:layout_marginBottom="@dimen/qs_media_padding"
+        android:layout_marginTop="0dp" />
+
+    <ImageButton
+        android:id="@+id/action3"
+        style="@style/MediaPlayer.SessionAction"
+        android:layout_width="48dp"
+        android:layout_height="48dp"
+        android:layout_marginStart="@dimen/qs_media_action_spacing"
+        android:layout_marginEnd="4dp"
+        android:layout_marginBottom="@dimen/qs_media_padding"
+        android:layout_marginTop="0dp" />
+
+    <ImageButton
+        android:id="@+id/action4"
+        style="@style/MediaPlayer.SessionAction"
+        android:layout_width="48dp"
+        android:layout_height="48dp"
+        android:layout_marginStart="@dimen/qs_media_action_spacing"
+        android:layout_marginEnd="4dp"
+        android:layout_marginBottom="@dimen/qs_media_padding"
+        android:layout_marginTop="0dp" />
 
     <!-- Long press menu -->
     <TextView
diff --git a/packages/SystemUI/res/layout/ongoing_privacy_chip.xml b/packages/SystemUI/res/layout/ongoing_privacy_chip.xml
index 8122776..ad2bc79 100644
--- a/packages/SystemUI/res/layout/ongoing_privacy_chip.xml
+++ b/packages/SystemUI/res/layout/ongoing_privacy_chip.xml
@@ -22,7 +22,10 @@
     android:layout_height="match_parent"
     android:layout_width="wrap_content"
     android:layout_gravity="center_vertical|end"
-    android:focusable="true" >
+    android:focusable="true"
+    android:clipChildren="false"
+    android:clipToPadding="false"
+    >
 
         <LinearLayout
             android:id="@+id/icons_container"
@@ -34,5 +37,7 @@
             android:layout_gravity="center"
             android:minWidth="@dimen/ongoing_appops_chip_min_width"
             android:maxWidth="@dimen/ongoing_appops_chip_max_width"
+            android:clipChildren="false"
+            android:clipToPadding="false"
             />
 </com.android.systemui.privacy.OngoingPrivacyChip>
\ No newline at end of file
diff --git a/packages/SystemUI/res/layout/screenshot_static.xml b/packages/SystemUI/res/layout/screenshot_static.xml
index 813bb60..8de8084 100644
--- a/packages/SystemUI/res/layout/screenshot_static.xml
+++ b/packages/SystemUI/res/layout/screenshot_static.xml
@@ -14,25 +14,25 @@
   ~ See the License for the specific language governing permissions and
   ~ limitations under the License.
   -->
-<androidx.constraintlayout.widget.ConstraintLayout
+<com.android.systemui.screenshot.DraggableConstraintLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
     <ImageView
-        android:id="@+id/screenshot_actions_container_background"
+        android:id="@+id/actions_container_background"
         android:visibility="gone"
         android:layout_height="0dp"
         android:layout_width="0dp"
         android:elevation="1dp"
         android:background="@drawable/action_chip_container_background"
         android:layout_marginStart="@dimen/overlay_action_container_margin_horizontal"
-        app:layout_constraintBottom_toBottomOf="@+id/screenshot_actions_container"
+        app:layout_constraintBottom_toBottomOf="@+id/actions_container"
         app:layout_constraintStart_toStartOf="parent"
-        app:layout_constraintTop_toTopOf="@+id/screenshot_actions_container"
-        app:layout_constraintEnd_toEndOf="@+id/screenshot_actions_container"/>
+        app:layout_constraintTop_toTopOf="@+id/actions_container"
+        app:layout_constraintEnd_toEndOf="@+id/actions_container"/>
     <HorizontalScrollView
-        android:id="@+id/screenshot_actions_container"
+        android:id="@+id/actions_container"
         android:layout_width="0dp"
         android:layout_height="wrap_content"
         android:layout_marginEnd="@dimen/overlay_action_container_margin_horizontal"
@@ -130,4 +130,4 @@
         app:layout_constraintStart_toStartOf="@id/screenshot_preview"
         app:layout_constraintTop_toTopOf="@id/screenshot_preview"
         android:elevation="@dimen/overlay_preview_elevation"/>
-</androidx.constraintlayout.widget.ConstraintLayout>
+</com.android.systemui.screenshot.DraggableConstraintLayout>
diff --git a/packages/SystemUI/res/layout/system_event_animation_window.xml b/packages/SystemUI/res/layout/system_event_animation_window.xml
index c92dec9..e6868b3 100644
--- a/packages/SystemUI/res/layout/system_event_animation_window.xml
+++ b/packages/SystemUI/res/layout/system_event_animation_window.xml
@@ -19,17 +19,7 @@
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_gravity="center_vertical|end"
-    android:paddingTop="@dimen/status_bar_padding_top"
-    android:paddingEnd="8dp"
+    android:clipChildren="false"
+    android:clipToPadding="false"
     >
-
-    <ImageView
-        android:id="@+id/dot_view"
-        android:layout_width="10dp"
-        android:layout_height="10dp"
-        android:layout_gravity="center_vertical|end"
-        android:src="@drawable/system_animation_ongoing_dot"
-        android:visibility="invisible"
-        />
-
 </FrameLayout>
\ No newline at end of file
diff --git a/packages/SystemUI/res/values-land/styles.xml b/packages/SystemUI/res/values-land/styles.xml
index f3d83645..8919198 100644
--- a/packages/SystemUI/res/values-land/styles.xml
+++ b/packages/SystemUI/res/values-land/styles.xml
@@ -18,15 +18,4 @@
     <style name="BrightnessDialogContainer" parent="@style/BaseBrightnessDialogContainer">
         <item name="android:layout_width">360dp</item>
     </style>
-
-    <style name="DockedDividerBackground">
-        <item name="android:layout_width">10dp</item>
-        <item name="android:layout_height">match_parent</item>
-        <item name="android:layout_gravity">center_horizontal</item>
-    </style>
-
-    <style name="DockedDividerMinimizedShadow">
-        <item name="android:layout_width">8dp</item>
-        <item name="android:layout_height">match_parent</item>
-    </style>
 </resources>
diff --git a/packages/SystemUI/res/values-sw600dp-port/dimens.xml b/packages/SystemUI/res/values-sw600dp-port/dimens.xml
index c990605..f5c0509 100644
--- a/packages/SystemUI/res/values-sw600dp-port/dimens.xml
+++ b/packages/SystemUI/res/values-sw600dp-port/dimens.xml
@@ -15,7 +15,7 @@
   ~ limitations under the License.
   -->
 <resources>
-    <dimen name="notification_panel_margin_horizontal">60dp</dimen>
+    <dimen name="notification_panel_margin_horizontal">48dp</dimen>
     <dimen name="status_view_margin_horizontal">62dp</dimen>
     <dimen name="keyguard_clock_top_margin">40dp</dimen>
     <dimen name="keyguard_status_view_bottom_margin">40dp</dimen>
diff --git a/packages/SystemUI/res/values-sw720dp-port/dimens.xml b/packages/SystemUI/res/values-sw720dp-port/dimens.xml
index e5f502f..44f8f3a 100644
--- a/packages/SystemUI/res/values-sw720dp-port/dimens.xml
+++ b/packages/SystemUI/res/values-sw720dp-port/dimens.xml
@@ -21,8 +21,11 @@
      for different hardware and product builds. -->
 <resources>
     <dimen name="status_view_margin_horizontal">124dp</dimen>
-    <dimen name="notification_panel_margin_horizontal">120dp</dimen>
     <dimen name="keyguard_clock_top_margin">80dp</dimen>
     <dimen name="keyguard_status_view_bottom_margin">80dp</dimen>
     <dimen name="bouncer_user_switcher_y_trans">90dp</dimen>
+
+    <dimen name="notification_panel_margin_horizontal">96dp</dimen>
+    <dimen name="notification_side_paddings">40dp</dimen>
+    <dimen name="notification_section_divider_height">16dp</dimen>
 </resources>
diff --git a/packages/SystemUI/res/values/dimens.xml b/packages/SystemUI/res/values/dimens.xml
index 5ca7285..ffae601 100644
--- a/packages/SystemUI/res/values/dimens.xml
+++ b/packages/SystemUI/res/values/dimens.xml
@@ -139,7 +139,7 @@
     <!-- Height of a heads up notification in the status bar -->
     <dimen name="notification_max_heads_up_height_increased">188dp</dimen>
 
-    <!-- Side padding on the lockscreen on the side of notifications -->
+    <!-- Side padding on the side of notifications -->
     <dimen name="notification_side_paddings">16dp</dimen>
 
     <!-- padding between the heads up and the statusbar -->
@@ -979,6 +979,11 @@
     <dimen name="qs_media_session_disabled_seekbar_vertical_padding">16dp</dimen>
     <dimen name="qs_media_session_height_expanded">184dp</dimen>
     <dimen name="qs_media_session_height_collapsed">128dp</dimen>
+    <dimen name="qs_media_seekbar_progress_wavelength">20dp</dimen>
+    <dimen name="qs_media_seekbar_progress_amplitude">1.5dp</dimen>
+    <dimen name="qs_media_seekbar_progress_phase">8dp</dimen>
+    <dimen name="qs_media_seekbar_progress_stroke_width">2dp</dimen>
+    <dimen name="qs_media_session_collapsed_guideline">144dp</dimen>
 
     <!-- Size of Smartspace media recommendations cards in the QSPanel carousel -->
     <dimen name="qs_aa_media_rec_album_size_collapsed">72dp</dimen>
@@ -1362,9 +1367,6 @@
     <dimen name="dream_overlay_status_icon_margin">8dp</dimen>
     <dimen name="dream_overlay_status_bar_icon_size">
         @*android:dimen/status_bar_system_icon_size</dimen>
-    <!-- Height of the area at the top of the dream overlay to allow dragging down the notifications
-         shade. -->
-    <dimen name="dream_overlay_notifications_drag_area_height">100dp</dimen>
     <dimen name="dream_overlay_camera_mic_off_indicator_size">8dp</dimen>
     <dimen name="dream_overlay_notification_indicator_size">6dp</dimen>
 
@@ -1374,6 +1376,7 @@
     <dimen name="dream_overlay_complication_weather_text_size">18sp</dimen>
     <dimen name="dream_overlay_complication_preview_text_size">36sp</dimen>
     <dimen name="dream_overlay_complication_preview_icon_padding">28dp</dimen>
+    <dimen name="dream_overlay_complication_shadow_padding">2dp</dimen>
 
     <!-- The position of the end guide, which dream overlay complications can align their start with
          if their end is aligned with the parent end. Represented as the percentage over from the
diff --git a/packages/SystemUI/res/values/strings.xml b/packages/SystemUI/res/values/strings.xml
index 9f2f1c1..d230f33 100644
--- a/packages/SystemUI/res/values/strings.xml
+++ b/packages/SystemUI/res/values/strings.xml
@@ -1193,10 +1193,7 @@
     <string name="wallet_lockscreen_settings_label">Lock screen settings</string>
 
     <!-- QR Code Scanner label, title [CHAR LIMIT=32] -->
-    <string name="qr_code_scanner_title">QR code</string>
-
-    <!-- QR Code Scanner description [CHAR LIMIT=NONE] -->
-    <string name="qr_code_scanner_description">Tap to scan</string>
+    <string name="qr_code_scanner_title">Scan QR code</string>
 
     <!-- Name of the work status bar icon. -->
     <string name="status_bar_work">Work profile</string>
diff --git a/packages/SystemUI/res/values/styles.xml b/packages/SystemUI/res/values/styles.xml
index f2eaa75..3ae21e0 100644
--- a/packages/SystemUI/res/values/styles.xml
+++ b/packages/SystemUI/res/values/styles.xml
@@ -590,6 +590,7 @@
 
     <style name="MediaPlayer.ProgressBar" parent="@android:style/Widget.ProgressBar.Horizontal">
         <item name="android:thumbTint">?android:attr/textColorPrimary</item>
+        <item name="android:progressDrawable">@drawable/media_squiggly_progress</item>
         <item name="android:progressTint">?android:attr/textColorPrimary</item>
         <item name="android:progressBackgroundTint">?android:attr/textColorTertiary</item>
         <item name="android:clickable">true</item>
diff --git a/packages/SystemUI/res/xml/media_session_collapsed.xml b/packages/SystemUI/res/xml/media_session_collapsed.xml
index c6e18a6..f00e031 100644
--- a/packages/SystemUI/res/xml/media_session_collapsed.xml
+++ b/packages/SystemUI/res/xml/media_session_collapsed.xml
@@ -19,6 +19,13 @@
     xmlns:app="http://schemas.android.com/apk/res-auto">
 
     <Constraint
+        android:id="@+id/media_action_barrier"
+        android:layout_width="0dp"
+        android:layout_height="0dp"
+        app:layout_constraintTop_toBottomOf="@id/media_seamless"
+        app:layout_constraintStart_toEndOf="@id/action_button_guideline" />
+
+    <Constraint
         android:id="@+id/album_art"
         android:layout_width="match_parent"
         android:layout_height="@dimen/qs_media_session_height_collapsed"
@@ -28,33 +35,73 @@
         app:layout_constraintBottom_toBottomOf="parent" />
 
     <Constraint
+        android:id="@+id/header_title"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="20dp"
+        android:layout_marginStart="@dimen/qs_media_padding"
+        android:layout_marginEnd="@dimen/qs_media_padding"
+        app:layout_constraintEnd_toStartOf="@id/action_button_guideline"
+        app:layout_constrainedWidth="true"
+        app:layout_constraintTop_toBottomOf="@id/icon"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintHorizontal_bias="0" />
+    <Constraint
+        android:id="@+id/header_artist"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginEnd="@dimen/qs_media_padding"
+        android:layout_marginTop="0dp"
+        app:layout_constraintEnd_toStartOf="@id/action_button_guideline"
+        app:layout_constrainedWidth="true"
+        app:layout_constraintTop_toBottomOf="@id/header_title"
+        app:layout_constraintStart_toStartOf="@id/header_title"
+        app:layout_constraintVertical_bias="0"
+        app:layout_constraintHorizontal_bias="0" />
+
+    <Constraint
         android:id="@+id/actionPlayPause"
         android:layout_width="48dp"
         android:layout_height="48dp"
         android:layout_marginEnd="@dimen/qs_media_padding"
-        app:layout_constraintStart_toEndOf="@id/actionEnd"
+        android:layout_marginBottom="@dimen/qs_media_padding"
+        app:layout_constraintVertical_bias="1"
         app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintTop_toBottomOf="@id/media_seamless"
-        app:layout_constraintBottom_toBottomOf="parent" />
+        app:layout_constraintStart_toEndOf="@id/media_action_barrier_end" />
 
+    <!--
+    There will only be 3 action buttons shown at most in this layout (controlled in code).
+    Play/Pause should always be at the end, but the other buttons should remain in the same order
+    when in RTL.
+    This is accomplished by setting two barriers at the start and end of the small buttons,
+    anchored to a guideline set at 3x button width from the end. The text and play/pause button are
+    positioned relative to the barriers, and the small buttons use right/left constraints to stay
+    in the correct order inside the barriers.
+    -->
     <Constraint
         android:id="@+id/actionPrev"
         android:layout_width="48dp"
         android:layout_height="48dp"
+        android:layout_marginBottom="@dimen/qs_media_padding"
         app:layout_constraintHorizontal_bias="1"
+        app:layout_constraintVertical_bias="1"
         app:layout_constraintHorizontal_chainStyle="packed"
-        app:layout_constraintStart_toEndOf="@id/header_artist"
-        app:layout_constraintEnd_toStartOf="@id/media_progress_bar"
+        app:layout_constraintRight_toLeftOf="@id/media_progress_bar"
         app:layout_constraintBottom_toBottomOf="parent"
-        app:layout_constraintTop_toBottomOf="@id/media_seamless" />
+        app:layout_constraintTop_toBottomOf="@id/media_seamless"
+        app:layout_constraintLeft_toRightOf="@id/media_action_barrier" />
 
     <Constraint
         android:id="@+id/media_progress_bar"
         android:layout_width="0dp"
         android:layout_height="wrap_content"
+        android:layout_marginBottom="@dimen/qs_media_padding"
         android:visibility="gone"
-        app:layout_constraintStart_toEndOf="@id/actionPrev"
-        app:layout_constraintEnd_toStartOf="@id/actionNext"
+        app:layout_constraintVertical_bias="1"
+        app:layout_constraintLeft_toRightOf="@id/actionPrev"
+        app:layout_constraintRight_toLeftOf="@id/actionNext"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintTop_toBottomOf="@id/media_seamless" />
 
@@ -62,29 +109,70 @@
         android:id="@+id/actionNext"
         android:layout_width="48dp"
         android:layout_height="48dp"
-        app:layout_constraintStart_toEndOf="@id/media_progress_bar"
-        app:layout_constraintEnd_toStartOf="@id/actionStart"
+        android:layout_marginBottom="@dimen/qs_media_padding"
+        app:layout_constraintVertical_bias="1"
+        app:layout_constraintLeft_toRightOf="@id/media_progress_bar"
+        app:layout_constraintRight_toLeftOf="@id/action0"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintTop_toBottomOf="@id/media_seamless" />
 
     <Constraint
-        android:id="@+id/actionStart"
+        android:id="@+id/action0"
         android:layout_width="48dp"
         android:layout_height="48dp"
+        android:layout_marginBottom="@dimen/qs_media_padding"
         android:visibility="gone"
-        app:layout_constraintStart_toEndOf="@id/actionNext"
-        app:layout_constraintEnd_toStartOf="@id/actionEnd"
+        app:layout_constraintVertical_bias="1"
+        app:layout_constraintLeft_toRightOf="@id/actionNext"
+        app:layout_constraintRight_toLeftOf="@id/action1"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintTop_toBottomOf="@id/media_seamless" />
 
     <Constraint
-        android:id="@+id/actionEnd"
+        android:id="@+id/action1"
         android:layout_width="48dp"
         android:layout_height="48dp"
+        android:layout_marginBottom="@dimen/qs_media_padding"
         android:visibility="gone"
-        app:layout_constraintStart_toEndOf="@id/actionStart"
-        app:layout_constraintEnd_toStartOf="@id/actionPlayPause"
+        app:layout_constraintVertical_bias="1"
+        app:layout_constraintLeft_toRightOf="@id/action0"
+        app:layout_constraintRight_toLeftOf="@id/action2"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintTop_toBottomOf="@id/media_seamless" />
 
-</ConstraintSet>
\ No newline at end of file
+    <Constraint
+        android:id="@+id/action2"
+        android:layout_width="48dp"
+        android:layout_height="48dp"
+        android:layout_marginBottom="@dimen/qs_media_padding"
+        android:visibility="gone"
+        app:layout_constraintVertical_bias="1"
+        app:layout_constraintLeft_toRightOf="@id/action1"
+        app:layout_constraintRight_toLeftOf="@id/action3"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintTop_toBottomOf="@id/media_seamless" />
+
+    <Constraint
+        android:id="@+id/action3"
+        android:layout_width="48dp"
+        android:layout_height="48dp"
+        android:layout_marginBottom="@dimen/qs_media_padding"
+        android:visibility="gone"
+        app:layout_constraintVertical_bias="1"
+        app:layout_constraintLeft_toRightOf="@id/action2"
+        app:layout_constraintRight_toLeftOf="@id/action4"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintTop_toBottomOf="@id/media_seamless" />
+
+    <Constraint
+        android:id="@+id/action4"
+        android:layout_width="48dp"
+        android:layout_height="48dp"
+        android:layout_marginBottom="@dimen/qs_media_padding"
+        android:visibility="gone"
+        app:layout_constraintVertical_bias="1"
+        app:layout_constraintLeft_toRightOf="@id/action3"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintTop_toBottomOf="@id/media_seamless"
+        app:layout_constraintRight_toLeftOf="@id/media_action_barrier_end" />
+</ConstraintSet>
diff --git a/packages/SystemUI/res/xml/media_session_expanded.xml b/packages/SystemUI/res/xml/media_session_expanded.xml
index 18ec7aa..10da704 100644
--- a/packages/SystemUI/res/xml/media_session_expanded.xml
+++ b/packages/SystemUI/res/xml/media_session_expanded.xml
@@ -28,57 +28,118 @@
         app:layout_constraintBottom_toBottomOf="parent" />
 
     <Constraint
+        android:id="@+id/header_title"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="20dp"
+        android:layout_marginStart="@dimen/qs_media_padding"
+        android:layout_marginEnd="@dimen/qs_media_padding"
+        app:layout_constraintEnd_toStartOf="@id/actionPlayPause"
+        app:layout_constrainedWidth="true"
+        app:layout_constraintTop_toBottomOf="@id/icon"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintHorizontal_bias="0" />
+    <Constraint
+        android:id="@+id/header_artist"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginEnd="@dimen/qs_media_padding"
+        android:layout_marginTop="0dp"
+        app:layout_constrainedWidth="true"
+        app:layout_constraintEnd_toStartOf="@id/actionPlayPause"
+        app:layout_constraintBottom_toTopOf="@id/media_action_barrier"
+        app:layout_constraintTop_toBottomOf="@id/header_title"
+        app:layout_constraintStart_toStartOf="@id/header_title"
+        app:layout_constraintVertical_bias="0"
+        app:layout_constraintHorizontal_bias="0" />
+
+    <Constraint
         android:id="@+id/actionPlayPause"
         android:layout_width="48dp"
         android:layout_height="48dp"
+        android:layout_marginStart="@dimen/qs_media_padding"
         android:layout_marginEnd="@dimen/qs_media_padding"
+        android:layout_marginBottom="0dp"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintTop_toBottomOf="@id/media_seamless"
-        app:layout_constraintBottom_toTopOf="@id/actionEnd" />
+        app:layout_constraintBottom_toBottomOf="@id/header_artist" />
 
+    <!--
+    The bottom row of action buttons should remain in the same order when RTL, so their constraints
+    are set with right/left instead of start/end.
+    The chain is set to "spread" so that the progress bar can be weighted to fill any empty space.
+     -->
     <Constraint
         android:id="@+id/actionPrev"
         android:layout_width="48dp"
         android:layout_height="48dp"
-        app:layout_constraintStart_toStartOf="parent"
-        app:layout_constraintEnd_toStartOf="@id/media_progress_bar"
+        app:layout_constraintLeft_toLeftOf="parent"
+        app:layout_constraintRight_toLeftOf="@id/media_progress_bar"
         app:layout_constraintBottom_toBottomOf="parent"
-        app:layout_constraintTop_toBottomOf="@id/actionPlayPause" />
+        app:layout_constraintTop_toBottomOf="@id/header_artist"
+        app:layout_constraintHorizontal_chainStyle="spread" />
 
     <Constraint
         android:id="@+id/media_progress_bar"
         android:layout_width="0dp"
-        android:layout_height="wrap_content"
-        app:layout_constraintStart_toEndOf="@id/actionPrev"
-        app:layout_constraintEnd_toStartOf="@id/actionNext"
+        android:layout_height="48dp"
+        app:layout_constraintLeft_toRightOf="@id/actionPrev"
+        app:layout_constraintRight_toLeftOf="@id/actionNext"
         app:layout_constraintBottom_toBottomOf="parent"
-        app:layout_constraintTop_toBottomOf="@id/actionPlayPause" />
+        app:layout_constraintTop_toBottomOf="@id/header_artist"
+        app:layout_constraintHorizontal_weight="1" />
 
     <Constraint
         android:id="@+id/actionNext"
         android:layout_width="48dp"
         android:layout_height="48dp"
-        app:layout_constraintStart_toEndOf="@id/media_progress_bar"
-        app:layout_constraintEnd_toStartOf="@id/actionStart"
+        app:layout_constraintLeft_toRightOf="@id/media_progress_bar"
+        app:layout_constraintRight_toLeftOf="@id/action0"
         app:layout_constraintBottom_toBottomOf="parent"
-        app:layout_constraintTop_toBottomOf="@id/actionPlayPause" />
+        app:layout_constraintTop_toBottomOf="@id/header_artist" />
 
     <Constraint
-        android:id="@+id/actionStart"
+        android:id="@+id/action0"
         android:layout_width="48dp"
         android:layout_height="48dp"
-        app:layout_constraintStart_toEndOf="@id/actionNext"
-        app:layout_constraintEnd_toStartOf="@id/actionEnd"
+        app:layout_constraintLeft_toRightOf="@id/actionNext"
+        app:layout_constraintRight_toLeftOf="@id/action1"
         app:layout_constraintBottom_toBottomOf="parent"
-        app:layout_constraintTop_toBottomOf="@id/actionPlayPause" />
+        app:layout_constraintTop_toBottomOf="@id/header_artist" />
 
     <Constraint
-        android:id="@+id/actionEnd"
+        android:id="@+id/action1"
         android:layout_width="48dp"
         android:layout_height="48dp"
-        app:layout_constraintStart_toEndOf="@id/actionStart"
-        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintLeft_toRightOf="@id/action0"
+        app:layout_constraintRight_toLeftOf="@id/action2"
         app:layout_constraintBottom_toBottomOf="parent"
-        app:layout_constraintTop_toBottomOf="@id/actionPlayPause" />
+        app:layout_constraintTop_toBottomOf="@id/header_artist" />
 
-</ConstraintSet>
\ No newline at end of file
+    <Constraint
+        android:id="@+id/action2"
+        android:layout_width="48dp"
+        android:layout_height="48dp"
+        app:layout_constraintLeft_toRightOf="@id/action1"
+        app:layout_constraintRight_toLeftOf="@id/action3"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintTop_toBottomOf="@id/header_artist" />
+
+    <Constraint
+        android:id="@+id/action3"
+        android:layout_width="48dp"
+        android:layout_height="48dp"
+        app:layout_constraintLeft_toRightOf="@id/action2"
+        app:layout_constraintRight_toLeftOf="@id/action4"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintTop_toBottomOf="@id/header_artist" />
+
+    <Constraint
+        android:id="@+id/action4"
+        android:layout_width="48dp"
+        android:layout_height="48dp"
+        app:layout_constraintLeft_toRightOf="@id/action3"
+        app:layout_constraintRight_toRightOf="parent"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintTop_toBottomOf="@id/header_artist" />
+</ConstraintSet>
diff --git a/packages/SystemUI/shared/src/com/android/systemui/shared/system/ActivityOptionsCompat.java b/packages/SystemUI/shared/src/com/android/systemui/shared/system/ActivityOptionsCompat.java
index db62f88..e38d2ba 100644
--- a/packages/SystemUI/shared/src/com/android/systemui/shared/system/ActivityOptionsCompat.java
+++ b/packages/SystemUI/shared/src/com/android/systemui/shared/system/ActivityOptionsCompat.java
@@ -17,36 +17,22 @@
 package com.android.systemui.shared.system;
 
 import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
-import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
-import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
 
 import android.app.ActivityOptions;
 import android.content.Context;
 import android.os.Handler;
 
-import com.android.systemui.shared.recents.model.Task;
-
 /**
  * Wrapper around internal ActivityOptions creation.
  */
 public abstract class ActivityOptionsCompat {
 
     /**
+     * @Deprecated
      * @return ActivityOptions for starting a task in split screen as the primary window.
      */
     public static ActivityOptions makeSplitScreenOptions(boolean dockTopLeft) {
-        return makeSplitScreenOptions(dockTopLeft, true);
-    }
-
-    /**
-     * @return ActivityOptions for starting a task in split screen.
-     */
-    public static ActivityOptions makeSplitScreenOptions(boolean dockTopLeft, boolean isPrimary) {
-        final ActivityOptions options = ActivityOptions.makeBasic();
-        options.setLaunchWindowingMode(isPrimary
-                ? WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
-                : WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
-        return options;
+        return ActivityOptions.makeBasic();
     }
 
     /**
diff --git a/packages/SystemUI/shared/src/com/android/systemui/shared/system/WindowManagerWrapper.java b/packages/SystemUI/shared/src/com/android/systemui/shared/system/WindowManagerWrapper.java
index 85d5de0..32299f5 100644
--- a/packages/SystemUI/shared/src/com/android/systemui/shared/system/WindowManagerWrapper.java
+++ b/packages/SystemUI/shared/src/com/android/systemui/shared/system/WindowManagerWrapper.java
@@ -78,10 +78,6 @@
     public static final int WINDOWING_MODE_MULTI_WINDOW =
             WindowConfiguration.WINDOWING_MODE_MULTI_WINDOW;
 
-    public static final int WINDOWING_MODE_SPLIT_SCREEN_PRIMARY =
-            WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
-    public static final int WINDOWING_MODE_SPLIT_SCREEN_SECONDARY =
-            WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
     public static final int WINDOWING_MODE_FREEFORM = WindowConfiguration.WINDOWING_MODE_FREEFORM;
 
     public static final int ITYPE_EXTRA_NAVIGATION_BAR = InsetsState.ITYPE_EXTRA_NAVIGATION_BAR;
diff --git a/packages/SystemUI/shared/src/com/android/systemui/unfold/util/ScopedUnfoldTransitionProgressProvider.kt b/packages/SystemUI/shared/src/com/android/systemui/unfold/util/ScopedUnfoldTransitionProgressProvider.kt
index 7b67917..8491f83 100644
--- a/packages/SystemUI/shared/src/com/android/systemui/unfold/util/ScopedUnfoldTransitionProgressProvider.kt
+++ b/packages/SystemUI/shared/src/com/android/systemui/unfold/util/ScopedUnfoldTransitionProgressProvider.kt
@@ -28,7 +28,7 @@
  * If the transition has already started by the moment when the clients are ready to play the
  * transition then it will report transition started callback and current animation progress.
  */
-class ScopedUnfoldTransitionProgressProvider
+open class ScopedUnfoldTransitionProgressProvider
 @JvmOverloads
 constructor(source: UnfoldTransitionProgressProvider? = null) :
     UnfoldTransitionProgressProvider, TransitionProgressListener {
diff --git a/packages/SystemUI/src/com/android/keyguard/EmergencyButton.java b/packages/SystemUI/src/com/android/keyguard/EmergencyButton.java
index ffd1546..458d22e 100644
--- a/packages/SystemUI/src/com/android/keyguard/EmergencyButton.java
+++ b/packages/SystemUI/src/com/android/keyguard/EmergencyButton.java
@@ -96,7 +96,7 @@
      **/
     public void reloadColors() {
         int color = Utils.getColorAttrDefaultColor(getContext(),
-                android.R.attr.textColorPrimaryInverse);
+                com.android.internal.R.attr.textColorOnAccent);
         setTextColor(color);
         setBackground(getContext()
                 .getDrawable(com.android.systemui.R.drawable.kg_emergency_button_background));
diff --git a/packages/SystemUI/src/com/android/keyguard/KeyguardPINView.java b/packages/SystemUI/src/com/android/keyguard/KeyguardPINView.java
index 1efda7e..77044ed 100644
--- a/packages/SystemUI/src/com/android/keyguard/KeyguardPINView.java
+++ b/packages/SystemUI/src/com/android/keyguard/KeyguardPINView.java
@@ -188,14 +188,13 @@
 
         enableClipping(false);
         setTranslationY(0);
-        AppearAnimationUtils.startTranslationYAnimation(this, 0 /* delay */, 280 /* duration */,
-                mDisappearYTranslation, mDisappearAnimationUtils.getInterpolator(),
-                getAnimationListener(InteractionJankMonitor.CUJ_LOCKSCREEN_PIN_DISAPPEAR));
         DisappearAnimationUtils disappearAnimationUtils = needsSlowUnlockTransition
                         ? mDisappearAnimationUtilsLocked
                         : mDisappearAnimationUtils;
-        disappearAnimationUtils.startAnimation2d(mViews,
-                () -> {
+        disappearAnimationUtils.createAnimation(
+                this, 0, 200, mDisappearYTranslation, false,
+                mDisappearAnimationUtils.getInterpolator(), () -> {
+                    getAnimationListener(InteractionJankMonitor.CUJ_LOCKSCREEN_PIN_DISAPPEAR);
                     enableClipping(true);
                     if (finishRunnable != null) {
                         finishRunnable.run();
diff --git a/packages/SystemUI/src/com/android/keyguard/KeyguardPasswordViewController.java b/packages/SystemUI/src/com/android/keyguard/KeyguardPasswordViewController.java
index 28f21af..0529cdbc 100644
--- a/packages/SystemUI/src/com/android/keyguard/KeyguardPasswordViewController.java
+++ b/packages/SystemUI/src/com/android/keyguard/KeyguardPasswordViewController.java
@@ -183,7 +183,7 @@
     @Override
     void resetState() {
         mPasswordEntry.setTextOperationUser(UserHandle.of(KeyguardUpdateMonitor.getCurrentUser()));
-        mMessageAreaController.setMessage(R.string.keyguard_enter_your_password);
+        mMessageAreaController.setMessage("");
         final boolean wasDisabled = mPasswordEntry.isEnabled();
         mView.setPasswordEntryEnabled(true);
         mView.setPasswordEntryInputEnabled(true);
diff --git a/packages/SystemUI/src/com/android/keyguard/KeyguardPatternViewController.java b/packages/SystemUI/src/com/android/keyguard/KeyguardPatternViewController.java
index 7635f919..41f9240 100644
--- a/packages/SystemUI/src/com/android/keyguard/KeyguardPatternViewController.java
+++ b/packages/SystemUI/src/com/android/keyguard/KeyguardPatternViewController.java
@@ -358,7 +358,7 @@
     }
 
     private void displayDefaultSecurityMessage() {
-        mMessageAreaController.setMessage(R.string.keyguard_enter_your_pattern);
+        mMessageAreaController.setMessage("");
     }
 
     private void handleAttemptLockout(long elapsedRealtimeDeadline) {
diff --git a/packages/SystemUI/src/com/android/keyguard/KeyguardPinBasedInputViewController.java b/packages/SystemUI/src/com/android/keyguard/KeyguardPinBasedInputViewController.java
index cc7e4f7..f7423ed 100644
--- a/packages/SystemUI/src/com/android/keyguard/KeyguardPinBasedInputViewController.java
+++ b/packages/SystemUI/src/com/android/keyguard/KeyguardPinBasedInputViewController.java
@@ -132,7 +132,6 @@
     @Override
     void resetState() {
         mView.setPasswordEntryEnabled(true);
-        mMessageAreaController.setMessage(R.string.keyguard_enter_your_pin);
     }
 
     @Override
diff --git a/packages/SystemUI/src/com/android/keyguard/KeyguardPinViewController.java b/packages/SystemUI/src/com/android/keyguard/KeyguardPinViewController.java
index 160d82a..9f4585f 100644
--- a/packages/SystemUI/src/com/android/keyguard/KeyguardPinViewController.java
+++ b/packages/SystemUI/src/com/android/keyguard/KeyguardPinViewController.java
@@ -76,6 +76,12 @@
     }
 
     @Override
+    void resetState() {
+        super.resetState();
+        mMessageAreaController.setMessage("");
+    }
+
+    @Override
     public boolean startDisappearAnimation(Runnable finishRunnable) {
         return mView.startDisappearAnimation(
                 mKeyguardUpdateMonitor.needsSlowUnlockTransition(), finishRunnable);
diff --git a/packages/SystemUI/src/com/android/keyguard/KeyguardStatusView.java b/packages/SystemUI/src/com/android/keyguard/KeyguardStatusView.java
index 31f466f..087817f 100644
--- a/packages/SystemUI/src/com/android/keyguard/KeyguardStatusView.java
+++ b/packages/SystemUI/src/com/android/keyguard/KeyguardStatusView.java
@@ -113,6 +113,22 @@
         }
     }
 
+    /** Sets a translationY value on every child view except for the media view. */
+    public void setChildrenTranslationYExcludingMediaView(float translationY) {
+        setChildrenTranslationYExcluding(translationY, Set.of(mMediaHostContainer));
+    }
+
+    /** Sets a translationY value on every view except for the views in the provided set. */
+    private void setChildrenTranslationYExcluding(float translationY, Set<View> excludedViews) {
+        for (int i = 0; i < mStatusViewContainer.getChildCount(); i++) {
+            final View child = mStatusViewContainer.getChildAt(i);
+
+            if (!excludedViews.contains(child)) {
+                child.setTranslationY(translationY);
+            }
+        }
+    }
+
     public float getChildrenAlphaExcludingSmartSpace() {
         return mChildrenAlphaExcludingSmartSpace;
     }
diff --git a/packages/SystemUI/src/com/android/keyguard/KeyguardStatusViewController.java b/packages/SystemUI/src/com/android/keyguard/KeyguardStatusViewController.java
index af3da9f..14c9cb2 100644
--- a/packages/SystemUI/src/com/android/keyguard/KeyguardStatusViewController.java
+++ b/packages/SystemUI/src/com/android/keyguard/KeyguardStatusViewController.java
@@ -137,6 +137,13 @@
     }
 
     /**
+     * Sets a translationY on the views on the keyguard, except on the media view.
+     */
+    public void setTranslationYExcludingMedia(float translationY) {
+        mView.setChildrenTranslationYExcludingMediaView(translationY);
+    }
+
+    /**
      * Set keyguard status view alpha.
      */
     public void setAlpha(float alpha) {
diff --git a/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java b/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java
index 1ef6dea..3858f9c 100644
--- a/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java
+++ b/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java
@@ -326,7 +326,6 @@
     private int mActiveMobileDataSubscription = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
     private final Executor mBackgroundExecutor;
     private SensorPrivacyManager mSensorPrivacyManager;
-    private int mFaceAuthUserId;
 
     /**
      * Short delay before restarting fingerprint authentication after a successful try. This should
@@ -1030,8 +1029,8 @@
         boolean cameraPrivacyEnabled = false;
         if (mSensorPrivacyManager != null) {
             cameraPrivacyEnabled = mSensorPrivacyManager
-                    .isSensorPrivacyEnabled(SensorPrivacyManager.Sensors.CAMERA,
-                    mFaceAuthUserId);
+                    .isSensorPrivacyEnabled(SensorPrivacyManager.TOGGLE_TYPE_SOFTWARE,
+                    SensorPrivacyManager.Sensors.CAMERA);
         }
 
         if (msgId == FaceManager.FACE_ERROR_CANCELED
@@ -2599,7 +2598,6 @@
             // This would need to be updated for multi-sensor devices
             final boolean supportsFaceDetection = !mFaceSensorProperties.isEmpty()
                     && mFaceSensorProperties.get(0).supportsFaceDetection;
-            mFaceAuthUserId = userId;
             if (isEncryptedOrLockdown(userId) && supportsFaceDetection) {
                 mFaceManager.detectFace(mFaceCancelSignal, mFaceDetectionCallback, userId);
             } else {
diff --git a/packages/SystemUI/src/com/android/keyguard/LockIconViewController.java b/packages/SystemUI/src/com/android/keyguard/LockIconViewController.java
index 370686a..74659f7 100644
--- a/packages/SystemUI/src/com/android/keyguard/LockIconViewController.java
+++ b/packages/SystemUI/src/com/android/keyguard/LockIconViewController.java
@@ -558,7 +558,7 @@
         switch(event.getActionMasked()) {
             case MotionEvent.ACTION_DOWN:
             case MotionEvent.ACTION_HOVER_ENTER:
-                if (!mDownDetected) {
+                if (!mDownDetected && mAccessibilityManager.isTouchExplorationEnabled()) {
                     mVibrator.vibrate(
                             Process.myUid(),
                             getContext().getOpPackageName(),
diff --git a/packages/SystemUI/src/com/android/keyguard/NumPadButton.java b/packages/SystemUI/src/com/android/keyguard/NumPadButton.java
index e0d0fc2..7b98f27 100644
--- a/packages/SystemUI/src/com/android/keyguard/NumPadButton.java
+++ b/packages/SystemUI/src/com/android/keyguard/NumPadButton.java
@@ -19,6 +19,8 @@
 import android.content.res.ColorStateList;
 import android.content.res.Configuration;
 import android.content.res.TypedArray;
+import android.graphics.drawable.Drawable;
+import android.graphics.drawable.GradientDrawable;
 import android.graphics.drawable.VectorDrawable;
 import android.util.AttributeSet;
 import android.view.MotionEvent;
@@ -37,8 +39,14 @@
     public NumPadButton(Context context, AttributeSet attrs) {
         super(context, attrs);
 
-        mAnimator = new NumPadAnimator(context, getBackground().mutate(),
-                attrs.getStyleAttribute());
+        Drawable background = getBackground();
+        if (background instanceof GradientDrawable) {
+            mAnimator = new NumPadAnimator(context, background.mutate(),
+                    attrs.getStyleAttribute());
+        } else {
+            mAnimator = null;
+        }
+
     }
 
     @Override
diff --git a/packages/SystemUI/src/com/android/keyguard/NumPadKey.java b/packages/SystemUI/src/com/android/keyguard/NumPadKey.java
index 88a0bce..5cab547 100644
--- a/packages/SystemUI/src/com/android/keyguard/NumPadKey.java
+++ b/packages/SystemUI/src/com/android/keyguard/NumPadKey.java
@@ -18,6 +18,8 @@
 import android.content.Context;
 import android.content.res.Configuration;
 import android.content.res.TypedArray;
+import android.graphics.drawable.Drawable;
+import android.graphics.drawable.GradientDrawable;
 import android.os.PowerManager;
 import android.os.SystemClock;
 import android.util.AttributeSet;
@@ -129,8 +131,13 @@
 
         setContentDescription(mDigitText.getText().toString());
 
-        mAnimator = new NumPadAnimator(context, getBackground().mutate(),
-                R.style.NumPadKey, mDigitText);
+        Drawable background = getBackground();
+        if (background instanceof GradientDrawable) {
+            mAnimator = new NumPadAnimator(context, background.mutate(),
+                    R.style.NumPadKey, mDigitText);
+        } else {
+            mAnimator = null;
+        }
     }
 
     @Override
diff --git a/packages/SystemUI/src/com/android/systemui/ScreenDecorHwcLayer.kt b/packages/SystemUI/src/com/android/systemui/ScreenDecorHwcLayer.kt
index 4b86862..498e715 100644
--- a/packages/SystemUI/src/com/android/systemui/ScreenDecorHwcLayer.kt
+++ b/packages/SystemUI/src/com/android/systemui/ScreenDecorHwcLayer.kt
@@ -119,9 +119,13 @@
         if (useInvertedAlphaColor) {
             canvas.drawColor(bgColor)
         }
+
+        // We may clear the color(if useInvertedAlphaColor is true) of the rounded corner rects
+        // before drawing rounded corners. If the cutout happens to be inside one of these rects, it
+        // will be cleared, so we have to draw rounded corners before cutout.
+        drawRoundedCorners(canvas)
         // Cutouts are drawn in DisplayCutoutBaseView.onDraw()
         super.onDraw(canvas)
-        drawRoundedCorners(canvas)
 
         debugTransparentRegionPaint?.let {
             calculateTransparentRect()
diff --git a/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java b/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java
index 3a6165c..031e378 100644
--- a/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java
+++ b/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java
@@ -27,6 +27,7 @@
 import android.content.pm.ApplicationInfo;
 import android.content.res.Configuration;
 import android.os.Bundle;
+import android.os.Looper;
 import android.os.Process;
 import android.os.RemoteException;
 import android.os.SystemProperties;
@@ -105,6 +106,10 @@
         mBootCompleteCache = mSysUIComponent.provideBootCacheImpl();
         log.traceEnd();
 
+        // Enable Looper trace points.
+        // This allows us to see Handler callbacks on traces.
+        Looper.getMainLooper().setTraceTag(Trace.TRACE_TAG_APP);
+
         // Set the application theme that is inherited by all services. Note that setting the
         // application theme in the manifest does only work for activities. Keep this in sync with
         // the theme set there.
diff --git a/packages/SystemUI/src/com/android/systemui/biometrics/UdfpsController.java b/packages/SystemUI/src/com/android/systemui/biometrics/UdfpsController.java
index bf42db5..bc7a3f6 100644
--- a/packages/SystemUI/src/com/android/systemui/biometrics/UdfpsController.java
+++ b/packages/SystemUI/src/com/android/systemui/biometrics/UdfpsController.java
@@ -584,16 +584,18 @@
     }
 
     /**
-     * Play haptic to signal udfps scanning started.
+     * If a11y touchExplorationEnabled, play haptic to signal UDFPS scanning started.
      */
     @VisibleForTesting
     public void playStartHaptic() {
-        mVibrator.vibrate(
-                Process.myUid(),
-                mContext.getOpPackageName(),
-                EFFECT_CLICK,
-                "udfps-onStart-click",
-                VIBRATION_ATTRIBUTES);
+        if (mAccessibilityManager.isTouchExplorationEnabled()) {
+            mVibrator.vibrate(
+                    Process.myUid(),
+                    mContext.getOpPackageName(),
+                    EFFECT_CLICK,
+                    "udfps-onStart-click",
+                    VIBRATION_ATTRIBUTES);
+        }
     }
 
     @Nullable
diff --git a/packages/SystemUI/src/com/android/systemui/broadcast/BroadcastSender.kt b/packages/SystemUI/src/com/android/systemui/broadcast/BroadcastSender.kt
new file mode 100644
index 0000000..6615f6b
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/broadcast/BroadcastSender.kt
@@ -0,0 +1,132 @@
+package com.android.systemui.broadcast
+
+import android.annotation.AnyThread
+import android.content.Context
+import android.content.Intent
+import android.os.Bundle
+import android.os.UserHandle
+import com.android.systemui.dagger.SysUISingleton
+import com.android.systemui.dagger.qualifiers.Background
+import com.android.systemui.util.wakelock.WakeLock
+import java.util.concurrent.Executor
+import javax.inject.Inject
+
+/**
+ * SystemUI master Broadcast sender
+ *
+ * This class dispatches broadcasts on background thread to avoid synchronous call to binder. Use
+ * this class instead of calling [Context.sendBroadcast] directly.
+ */
+@SysUISingleton
+class BroadcastSender @Inject constructor(
+    private val context: Context,
+    private val wakeLockBuilder: WakeLock.Builder,
+    @Background private val bgExecutor: Executor
+) {
+
+    private val WAKE_LOCK_TAG = "SysUI:BroadcastSender"
+    private val WAKE_LOCK_SEND_REASON = "sendInBackground"
+
+    /**
+     * Sends broadcast via [Context.sendBroadcast] on background thread to avoid blocking
+     * synchronous binder call.
+     */
+    @AnyThread
+    fun sendBroadcast(intent: Intent) {
+        sendInBackground {
+            context.sendBroadcast(intent)
+        }
+    }
+
+    /**
+     * Sends broadcast via [Context.sendBroadcast] on background thread to avoid blocking
+     * synchronous binder call.
+     */
+    @AnyThread
+    fun sendBroadcast(intent: Intent, receiverPermission: String?) {
+        sendInBackground {
+            context.sendBroadcast(intent, receiverPermission)
+        }
+    }
+
+    /**
+     * Sends broadcast via [Context.sendBroadcastAsUser] on background thread to avoid blocking
+     * synchronous binder call.
+     */
+    @AnyThread
+    fun sendBroadcastAsUser(intent: Intent, userHandle: UserHandle) {
+        sendInBackground {
+            context.sendBroadcastAsUser(intent, userHandle)
+        }
+    }
+
+    /**
+     * Sends broadcast via [Context.sendBroadcastAsUser] on background thread to avoid blocking
+     * synchronous binder call.
+     */
+    @AnyThread
+    fun sendBroadcastAsUser(intent: Intent, userHandle: UserHandle, receiverPermission: String?) {
+        sendInBackground {
+            context.sendBroadcastAsUser(intent, userHandle, receiverPermission)
+        }
+    }
+
+    /**
+     * Sends broadcast via [Context.sendBroadcastAsUser] on background thread to avoid blocking
+     * synchronous binder call.
+     */
+    @AnyThread
+    fun sendBroadcastAsUser(
+        intent: Intent,
+        userHandle: UserHandle,
+        receiverPermission: String?,
+        options: Bundle?
+    ) {
+        sendInBackground {
+            context.sendBroadcastAsUser(intent, userHandle, receiverPermission, options)
+        }
+    }
+
+    /**
+     * Sends broadcast via [Context.sendBroadcastAsUser] on background thread to avoid blocking
+     * synchronous binder call.
+     */
+    @AnyThread
+    fun sendBroadcastAsUser(
+        intent: Intent,
+        userHandle: UserHandle,
+        receiverPermission: String?,
+        appOp: Int
+    ) {
+        sendInBackground {
+            context.sendBroadcastAsUser(intent, userHandle, receiverPermission, appOp)
+        }
+    }
+
+    /**
+     * Sends [Intent.ACTION_CLOSE_SYSTEM_DIALOGS] broadcast to the system.
+     */
+    @AnyThread
+    fun closeSystemDialogs() {
+        sendInBackground {
+            context.sendBroadcast(Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS))
+        }
+    }
+
+    /**
+     * Dispatches parameter on background executor while holding a wakelock.
+     */
+    private fun sendInBackground(callable: () -> Unit) {
+        val broadcastWakelock = wakeLockBuilder.setTag(WAKE_LOCK_TAG)
+                                .setMaxTimeout(5000)
+                                .build()
+        broadcastWakelock.acquire(WAKE_LOCK_SEND_REASON)
+        bgExecutor.execute {
+            try {
+                callable.invoke()
+            } finally {
+                broadcastWakelock.release(WAKE_LOCK_SEND_REASON)
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/packages/SystemUI/src/com/android/systemui/charging/WirelessChargingAnimation.java b/packages/SystemUI/src/com/android/systemui/charging/WirelessChargingAnimation.java
index 508262d..835025b 100644
--- a/packages/SystemUI/src/com/android/systemui/charging/WirelessChargingAnimation.java
+++ b/packages/SystemUI/src/com/android/systemui/charging/WirelessChargingAnimation.java
@@ -16,6 +16,8 @@
 
 package com.android.systemui.charging;
 
+import static com.android.systemui.charging.WirelessChargingLayout.UNKNOWN_BATTERY_LEVEL;
+
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.content.Context;
@@ -76,6 +78,16 @@
     }
 
     /**
+     * Creates a charging animation object using mostly default values for non-dozing and unknown
+     * battery level without charging number shown.
+     */
+    public static WirelessChargingAnimation makeChargingAnimationWithNoBatteryLevel(
+            @NonNull Context context, UiEventLogger uiEventLogger) {
+        return makeWirelessChargingAnimation(context, null,
+                UNKNOWN_BATTERY_LEVEL, UNKNOWN_BATTERY_LEVEL, null, false, uiEventLogger);
+    }
+
+    /**
      * Show the view for the specified duration.
      */
     public void show(long delay) {
diff --git a/packages/SystemUI/src/com/android/systemui/clipboardoverlay/ClipboardOverlayController.java b/packages/SystemUI/src/com/android/systemui/clipboardoverlay/ClipboardOverlayController.java
index 56e4d7e..0f937d1 100644
--- a/packages/SystemUI/src/com/android/systemui/clipboardoverlay/ClipboardOverlayController.java
+++ b/packages/SystemUI/src/com/android/systemui/clipboardoverlay/ClipboardOverlayController.java
@@ -73,6 +73,7 @@
 
 import com.android.internal.policy.PhoneWindow;
 import com.android.systemui.R;
+import com.android.systemui.screenshot.DraggableConstraintLayout;
 import com.android.systemui.screenshot.FloatingWindowUtil;
 import com.android.systemui.screenshot.OverlayActionChip;
 import com.android.systemui.screenshot.TimeoutHandler;
@@ -166,8 +167,28 @@
         mRemoteCopyChip = requireNonNull(mView.findViewById(R.id.remote_copy_chip));
         mDismissButton = requireNonNull(mView.findViewById(R.id.dismiss_button));
 
-        mView.setOnDismissEndCallback(this::hideImmediate);
-        mView.setOnInteractionCallback(mTimeoutHandler::resetTimeout);
+        mView.setCallbacks(new DraggableConstraintLayout.SwipeDismissCallbacks() {
+            @Override
+            public void onInteraction() {
+                mTimeoutHandler.resetTimeout();
+            }
+
+            @Override
+            public void onSwipeDismissInitiated(Animator animator) {
+                animator.addListener(new AnimatorListenerAdapter() {
+                    @Override
+                    public void onAnimationStart(Animator animation) {
+                        super.onAnimationStart(animation);
+                        mContainer.animate().alpha(0).start();
+                    }
+                });
+            }
+
+            @Override
+            public void onDismissComplete() {
+                hideImmediate();
+            }
+        });
 
         mDismissButton.setOnClickListener(view -> animateOut());
 
diff --git a/packages/SystemUI/src/com/android/systemui/clipboardoverlay/DraggableConstraintLayout.java b/packages/SystemUI/src/com/android/systemui/clipboardoverlay/DraggableConstraintLayout.java
deleted file mode 100644
index 57dc162..0000000
--- a/packages/SystemUI/src/com/android/systemui/clipboardoverlay/DraggableConstraintLayout.java
+++ /dev/null
@@ -1,163 +0,0 @@
-/*
- * Copyright (C) 2021 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.systemui.clipboardoverlay;
-
-import android.animation.Animator;
-import android.content.Context;
-import android.graphics.Rect;
-import android.graphics.Region;
-import android.util.AttributeSet;
-import android.view.GestureDetector;
-import android.view.MotionEvent;
-import android.view.View;
-import android.view.ViewTreeObserver;
-
-import androidx.constraintlayout.widget.ConstraintLayout;
-
-import com.android.systemui.R;
-import com.android.systemui.screenshot.SwipeDismissHandler;
-
-import java.util.function.Consumer;
-
-/**
- * ConstraintLayout that is draggable when touched in a specific region
- */
-public class DraggableConstraintLayout extends ConstraintLayout
-        implements ViewTreeObserver.OnComputeInternalInsetsListener {
-    private final SwipeDismissHandler mSwipeDismissHandler;
-    private final GestureDetector mSwipeDetector;
-    private Consumer<Animator> mOnDismissInitiated;
-    private Runnable mOnDismissComplete;
-    private Runnable mOnInteraction;
-
-    public DraggableConstraintLayout(Context context) {
-        this(context, null);
-    }
-
-    public DraggableConstraintLayout(Context context, AttributeSet attrs) {
-        this(context, attrs, 0);
-    }
-
-    public DraggableConstraintLayout(Context context, AttributeSet attrs, int defStyleAttr) {
-        super(context, attrs, defStyleAttr);
-
-        mSwipeDismissHandler = new SwipeDismissHandler(mContext, this,
-                new SwipeDismissHandler.SwipeDismissCallbacks() {
-                    @Override
-                    public void onInteraction() {
-                        if (mOnInteraction != null) {
-                            mOnInteraction.run();
-                        }
-                    }
-
-                    @Override
-                    public void onSwipeDismissInitiated(Animator animator) {
-                        if (mOnDismissInitiated != null) {
-                            mOnDismissInitiated.accept(animator);
-                        }
-                    }
-
-                    @Override
-                    public void onDismissComplete() {
-                        if (mOnDismissComplete != null) {
-                            mOnDismissComplete.run();
-                        }
-                    }
-                });
-        setOnTouchListener(mSwipeDismissHandler);
-
-        mSwipeDetector = new GestureDetector(mContext,
-                new GestureDetector.SimpleOnGestureListener() {
-                    final Rect mActionsRect = new Rect();
-
-                    @Override
-                    public boolean onScroll(
-                            MotionEvent ev1, MotionEvent ev2, float distanceX, float distanceY) {
-                        View actionsContainer = findViewById(R.id.actions_container);
-                        actionsContainer.getBoundsOnScreen(mActionsRect);
-                        // return true if we aren't in the actions bar, or if we are but it isn't
-                        // scrollable in the direction of movement
-                        return !mActionsRect.contains((int) ev2.getRawX(), (int) ev2.getRawY())
-                                || !actionsContainer.canScrollHorizontally((int) distanceX);
-                    }
-                });
-        mSwipeDetector.setIsLongpressEnabled(false);
-    }
-
-    @Override
-    public boolean onInterceptTouchEvent(MotionEvent ev) {
-        if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
-            mSwipeDismissHandler.onTouch(this, ev);
-        }
-
-        return mSwipeDetector.onTouchEvent(ev);
-    }
-
-    /**
-     * Dismiss the view, with animation controlled by SwipeDismissHandler
-     */
-    public void dismiss() {
-        mSwipeDismissHandler.dismiss();
-    }
-
-    /**
-     * Set the callback to be run after view is dismissed (before animation; receives animator as
-     * input)
-     */
-    public void setOnDismissStartCallback(Consumer<Animator> callback) {
-        mOnDismissInitiated = callback;
-    }
-
-    /**
-     * Set the callback to be run after view is dismissed
-     */
-    public void setOnDismissEndCallback(Runnable callback) {
-        mOnDismissComplete = callback;
-    }
-
-    /**
-     * Set the callback to be run when the view is interacted with (e.g. tapped)
-     */
-    public void setOnInteractionCallback(Runnable callback) {
-        mOnInteraction = callback;
-    }
-
-    @Override
-    protected void onAttachedToWindow() {
-        super.onAttachedToWindow();
-        getViewTreeObserver().addOnComputeInternalInsetsListener(this);
-    }
-
-    @Override
-    protected void onDetachedFromWindow() {
-        super.onDetachedFromWindow();
-        getViewTreeObserver().removeOnComputeInternalInsetsListener(this);
-    }
-
-    @Override
-    public void onComputeInternalInsets(ViewTreeObserver.InternalInsetsInfo inoutInfo) {
-        // Only child views are touchable.
-        Region r = new Region();
-        Rect rect = new Rect();
-        for (int i = 0; i < getChildCount(); i++) {
-            getChildAt(i).getGlobalVisibleRect(rect);
-            r.op(rect, Region.Op.UNION);
-        }
-        inoutInfo.setTouchableInsets(ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_REGION);
-        inoutInfo.touchableRegion.set(r);
-    }
-}
diff --git a/packages/SystemUI/src/com/android/systemui/controls/management/ControlAdapter.kt b/packages/SystemUI/src/com/android/systemui/controls/management/ControlAdapter.kt
index 40662536..f9115b2 100644
--- a/packages/SystemUI/src/com/android/systemui/controls/management/ControlAdapter.kt
+++ b/packages/SystemUI/src/com/android/systemui/controls/management/ControlAdapter.kt
@@ -17,10 +17,13 @@
 package com.android.systemui.controls.management
 
 import android.content.ComponentName
+import android.content.res.Configuration
+import android.content.res.Resources
 import android.graphics.Rect
 import android.os.Bundle
 import android.service.controls.Control
 import android.service.controls.DeviceTypes
+import android.util.TypedValue
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
@@ -32,7 +35,6 @@
 import androidx.core.view.AccessibilityDelegateCompat
 import androidx.core.view.ViewCompat
 import androidx.core.view.accessibility.AccessibilityNodeInfoCompat
-import androidx.recyclerview.widget.GridLayoutManager
 import androidx.recyclerview.widget.RecyclerView
 import com.android.systemui.R
 import com.android.systemui.controls.ControlInterface
@@ -56,11 +58,32 @@
         const val TYPE_ZONE = 0
         const val TYPE_CONTROL = 1
         const val TYPE_DIVIDER = 2
-    }
 
-    val spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
-        override fun getSpanSize(position: Int): Int {
-            return if (getItemViewType(position) != TYPE_CONTROL) 2 else 1
+        /**
+         * For low-dp width screens that also employ an increased font scale, adjust the
+         * number of columns. This helps prevent text truncation on these devices.
+         *
+         */
+        @JvmStatic
+        fun findMaxColumns(res: Resources): Int {
+            var maxColumns = res.getInteger(R.integer.controls_max_columns)
+            val maxColumnsAdjustWidth =
+                    res.getInteger(R.integer.controls_max_columns_adjust_below_width_dp)
+
+            val outValue = TypedValue()
+            res.getValue(R.dimen.controls_max_columns_adjust_above_font_scale, outValue, true)
+            val maxColumnsAdjustFontScale = outValue.getFloat()
+
+            val config = res.configuration
+            val isPortrait = config.orientation == Configuration.ORIENTATION_PORTRAIT
+            if (isPortrait &&
+                    config.screenWidthDp != Configuration.SCREEN_WIDTH_DP_UNDEFINED &&
+                    config.screenWidthDp <= maxColumnsAdjustWidth &&
+                    config.fontScale >= maxColumnsAdjustFontScale) {
+                maxColumns--
+            }
+
+            return maxColumns
         }
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/controls/management/ControlsEditingActivity.kt b/packages/SystemUI/src/com/android/systemui/controls/management/ControlsEditingActivity.kt
index 6f94943..f611c3e 100644
--- a/packages/SystemUI/src/com/android/systemui/controls/management/ControlsEditingActivity.kt
+++ b/packages/SystemUI/src/com/android/systemui/controls/management/ControlsEditingActivity.kt
@@ -195,10 +195,11 @@
         val margin = resources
                 .getDimensionPixelSize(R.dimen.controls_card_margin)
         val itemDecorator = MarginItemDecorator(margin, margin)
+        val spanCount = ControlAdapter.findMaxColumns(resources)
 
         recyclerView.apply {
             this.adapter = adapter
-            layoutManager = object : GridLayoutManager(recyclerView.context, 2) {
+            layoutManager = object : GridLayoutManager(recyclerView.context, spanCount) {
 
                 // This will remove from the announcement the row corresponding to the divider,
                 // as it's not something that should be announced.
@@ -210,7 +211,12 @@
                     return if (initial > 0) initial - 1 else initial
                 }
             }.apply {
-                spanSizeLookup = adapter.spanSizeLookup
+                spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
+                    override fun getSpanSize(position: Int): Int {
+                        return if (adapter?.getItemViewType(position)
+                                != ControlAdapter.TYPE_CONTROL) spanCount else 1
+                    }
+                }
             }
             addItemDecoration(itemDecorator)
         }
diff --git a/packages/SystemUI/src/com/android/systemui/controls/management/StructureAdapter.kt b/packages/SystemUI/src/com/android/systemui/controls/management/StructureAdapter.kt
index cb67454..747bcbe 100644
--- a/packages/SystemUI/src/com/android/systemui/controls/management/StructureAdapter.kt
+++ b/packages/SystemUI/src/com/android/systemui/controls/management/StructureAdapter.kt
@@ -60,11 +60,17 @@
             val margin = itemView.context.resources
                 .getDimensionPixelSize(R.dimen.controls_card_margin)
             val itemDecorator = MarginItemDecorator(margin, margin)
+            val spanCount = ControlAdapter.findMaxColumns(itemView.resources)
 
             recyclerView.apply {
                 this.adapter = controlAdapter
-                layoutManager = GridLayoutManager(recyclerView.context, 2).apply {
-                    spanSizeLookup = controlAdapter.spanSizeLookup
+                layoutManager = GridLayoutManager(recyclerView.context, spanCount).apply {
+                    spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
+                        override fun getSpanSize(position: Int): Int {
+                            return if (adapter?.getItemViewType(position)
+                                    != ControlAdapter.TYPE_CONTROL) spanCount else 1
+                        }
+                    }
                 }
                 addItemDecoration(itemDecorator)
             }
diff --git a/packages/SystemUI/src/com/android/systemui/controls/ui/ControlActionCoordinatorImpl.kt b/packages/SystemUI/src/com/android/systemui/controls/ui/ControlActionCoordinatorImpl.kt
index 5c1d8c3..e53f267 100644
--- a/packages/SystemUI/src/com/android/systemui/controls/ui/ControlActionCoordinatorImpl.kt
+++ b/packages/SystemUI/src/com/android/systemui/controls/ui/ControlActionCoordinatorImpl.kt
@@ -16,11 +16,11 @@
 
 package com.android.systemui.controls.ui
 
+import android.annotation.AnyThread
 import android.annotation.MainThread
 import android.app.Dialog
 import android.app.PendingIntent
 import android.content.Context
-import android.content.Intent
 import android.content.pm.PackageManager
 import android.content.pm.ResolveInfo
 import android.database.ContentObserver
@@ -35,6 +35,7 @@
 import android.util.Log
 import android.view.HapticFeedbackConstants
 import com.android.internal.annotations.VisibleForTesting
+import com.android.systemui.broadcast.BroadcastSender
 import com.android.systemui.controls.ControlsMetricsLogger
 import com.android.systemui.dagger.SysUISingleton
 import com.android.systemui.dagger.qualifiers.Main
@@ -53,6 +54,7 @@
     private val bgExecutor: DelayableExecutor,
     @Main private val uiExecutor: DelayableExecutor,
     private val activityStarter: ActivityStarter,
+    private val broadcastSender: BroadcastSender,
     private val keyguardStateController: KeyguardStateController,
     private val taskViewFactory: Optional<TaskViewFactory>,
     private val controlsMetricsLogger: ControlsMetricsLogger,
@@ -199,11 +201,12 @@
             false
         }
 
+    @AnyThread
     @VisibleForTesting
     fun bouncerOrRun(action: Action, authRequired: Boolean) {
         if (keyguardStateController.isShowing() && authRequired) {
             if (isLocked) {
-                context.sendBroadcast(Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS))
+                broadcastSender.closeSystemDialogs()
 
                 // pending actions will only run after the control state has been refreshed
                 pendingAction = action
@@ -233,7 +236,10 @@
                 // make sure the intent is valid before attempting to open the dialog
                 if (activities.isNotEmpty() && taskViewFactory.isPresent) {
                     taskViewFactory.get().create(context, uiExecutor, {
-                        dialog = DetailDialog(activityContext, it, pendingIntent, cvh).also {
+                        dialog = DetailDialog(
+                            activityContext, broadcastSender,
+                            it, pendingIntent, cvh
+                        ).also {
                             it.setOnDismissListener { _ -> dialog = null }
                             it.show()
                         }
diff --git a/packages/SystemUI/src/com/android/systemui/controls/ui/ControlsUiControllerImpl.kt b/packages/SystemUI/src/com/android/systemui/controls/ui/ControlsUiControllerImpl.kt
index 59c291c..1268250 100644
--- a/packages/SystemUI/src/com/android/systemui/controls/ui/ControlsUiControllerImpl.kt
+++ b/packages/SystemUI/src/com/android/systemui/controls/ui/ControlsUiControllerImpl.kt
@@ -25,12 +25,10 @@
 import android.content.Context
 import android.content.Intent
 import android.content.SharedPreferences
-import android.content.res.Configuration
 import android.graphics.drawable.Drawable
 import android.graphics.drawable.LayerDrawable
 import android.service.controls.Control
 import android.util.Log
-import android.util.TypedValue
 import android.view.ContextThemeWrapper
 import android.view.LayoutInflater
 import android.view.View
@@ -51,6 +49,7 @@
 import com.android.systemui.controls.controller.ControlInfo
 import com.android.systemui.controls.controller.ControlsController
 import com.android.systemui.controls.controller.StructureInfo
+import com.android.systemui.controls.management.ControlAdapter
 import com.android.systemui.controls.management.ControlsEditingActivity
 import com.android.systemui.controls.management.ControlsFavoritingActivity
 import com.android.systemui.controls.management.ControlsListingController
@@ -386,7 +385,7 @@
             visibility = View.VISIBLE
         }
 
-        val maxColumns = findMaxColumns()
+        val maxColumns = ControlAdapter.findMaxColumns(activityContext.resources)
 
         val listView = parent.requireViewById(R.id.global_actions_controls_list) as ViewGroup
         var lastRow: ViewGroup = createRow(inflater, listView)
@@ -432,32 +431,6 @@
         }
     }
 
-    /**
-     * For low-dp width screens that also employ an increased font scale, adjust the
-     * number of columns. This helps prevent text truncation on these devices.
-     */
-    private fun findMaxColumns(): Int {
-        val res = activityContext.resources
-        var maxColumns = res.getInteger(R.integer.controls_max_columns)
-        val maxColumnsAdjustWidth =
-            res.getInteger(R.integer.controls_max_columns_adjust_below_width_dp)
-
-        val outValue = TypedValue()
-        res.getValue(R.dimen.controls_max_columns_adjust_above_font_scale, outValue, true)
-        val maxColumnsAdjustFontScale = outValue.getFloat()
-
-        val config = res.configuration
-        val isPortrait = config.orientation == Configuration.ORIENTATION_PORTRAIT
-        if (isPortrait &&
-            config.screenWidthDp != Configuration.SCREEN_WIDTH_DP_UNDEFINED &&
-            config.screenWidthDp <= maxColumnsAdjustWidth &&
-            config.fontScale >= maxColumnsAdjustFontScale) {
-            maxColumns--
-        }
-
-        return maxColumns
-    }
-
     override fun getPreferredStructure(structures: List<StructureInfo>): StructureInfo {
         if (structures.isEmpty()) return EMPTY_STRUCTURE
 
diff --git a/packages/SystemUI/src/com/android/systemui/controls/ui/DetailDialog.kt b/packages/SystemUI/src/com/android/systemui/controls/ui/DetailDialog.kt
index dc3d1b5..80589a2 100644
--- a/packages/SystemUI/src/com/android/systemui/controls/ui/DetailDialog.kt
+++ b/packages/SystemUI/src/com/android/systemui/controls/ui/DetailDialog.kt
@@ -33,6 +33,7 @@
 import android.widget.ImageView
 import com.android.internal.policy.ScreenDecorationsUtils
 import com.android.systemui.R
+import com.android.systemui.broadcast.BroadcastSender
 import com.android.wm.shell.TaskView
 
 /**
@@ -42,6 +43,7 @@
  */
 class DetailDialog(
     val activityContext: Context,
+    val broadcastSender: BroadcastSender,
     val taskView: TaskView,
     val pendingIntent: PendingIntent,
     val cvh: ControlViewHolder
@@ -147,7 +149,7 @@
                 // startActivity() below is called.
                 removeDetailTask()
                 dismiss()
-                context.sendBroadcast(Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS))
+                broadcastSender.closeSystemDialogs()
                 pendingIntent.send()
             }
         }
diff --git a/packages/SystemUI/src/com/android/systemui/doze/DozeScreenBrightness.java b/packages/SystemUI/src/com/android/systemui/doze/DozeScreenBrightness.java
index b8b4092..dfb27ef 100644
--- a/packages/SystemUI/src/com/android/systemui/doze/DozeScreenBrightness.java
+++ b/packages/SystemUI/src/com/android/systemui/doze/DozeScreenBrightness.java
@@ -89,6 +89,7 @@
     private boolean mPaused = false;
     private boolean mScreenOff = false;
     private int mLastSensorValue = -1;
+    private DozeMachine.State mState = DozeMachine.State.UNINITIALIZED;
 
     /**
      * Debug value used for emulating various display brightness buckets:
@@ -135,6 +136,7 @@
 
     @Override
     public void transitionTo(DozeMachine.State oldState, DozeMachine.State newState) {
+        mState = newState;
         switch (newState) {
             case INITIALIZED:
                 resetBrightnessToDefault();
@@ -262,8 +264,9 @@
      */
     private int clampToDimBrightnessForScreenOff(int brightness) {
         final boolean screenTurningOff =
-                mDozeParameters.shouldClampToDimBrightness()
-                        || mWakefulnessLifecycle.getWakefulness() == WAKEFULNESS_GOING_TO_SLEEP;
+                (mDozeParameters.shouldClampToDimBrightness()
+                        || mWakefulnessLifecycle.getWakefulness() == WAKEFULNESS_GOING_TO_SLEEP)
+                && mState == DozeMachine.State.INITIALIZED;
         if (screenTurningOff
                 && mWakefulnessLifecycle.getLastSleepReason() == GO_TO_SLEEP_REASON_TIMEOUT) {
             return Math.max(
diff --git a/packages/SystemUI/src/com/android/systemui/dreams/DreamOverlayContainerViewController.java b/packages/SystemUI/src/com/android/systemui/dreams/DreamOverlayContainerViewController.java
index 69f15e6..995df19 100644
--- a/packages/SystemUI/src/com/android/systemui/dreams/DreamOverlayContainerViewController.java
+++ b/packages/SystemUI/src/com/android/systemui/dreams/DreamOverlayContainerViewController.java
@@ -23,8 +23,6 @@
 import android.view.View;
 import android.view.ViewGroup;
 
-import com.android.internal.annotations.VisibleForTesting;
-import com.android.systemui.R;
 import com.android.systemui.dagger.qualifiers.Main;
 import com.android.systemui.dreams.complication.ComplicationHostViewController;
 import com.android.systemui.dreams.dagger.DreamOverlayComponent;
@@ -39,9 +37,6 @@
  */
 @DreamOverlayComponent.DreamOverlayScope
 public class DreamOverlayContainerViewController extends ViewController<DreamOverlayContainerView> {
-    // The height of the area at the top of the dream overlay to allow dragging down the
-    // notifications shade.
-    private final int mDreamOverlayNotificationsDragAreaHeight;
     private final DreamOverlayStatusBarViewController mStatusBarViewController;
 
     private final ComplicationHostViewController mComplicationHostViewController;
@@ -79,9 +74,6 @@
         super(containerView);
         mDreamOverlayContentView = contentView;
         mStatusBarViewController = statusBarViewController;
-        mDreamOverlayNotificationsDragAreaHeight =
-                mView.getResources().getDimensionPixelSize(
-                        R.dimen.dream_overlay_notifications_drag_area_height);
 
         mComplicationHostViewController = complicationHostViewController;
         final View view = mComplicationHostViewController.getView();
@@ -117,11 +109,6 @@
         return mView;
     }
 
-    @VisibleForTesting
-    int getDreamOverlayNotificationsDragAreaHeight() {
-        return mDreamOverlayNotificationsDragAreaHeight;
-    }
-
     private void updateBurnInOffsets() {
         int burnInOffset = mMaxBurnInOffset;
 
diff --git a/packages/SystemUI/src/com/android/systemui/flags/Flags.java b/packages/SystemUI/src/com/android/systemui/flags/Flags.java
index b590412..61cfe92 100644
--- a/packages/SystemUI/src/com/android/systemui/flags/Flags.java
+++ b/packages/SystemUI/src/com/android/systemui/flags/Flags.java
@@ -16,6 +16,7 @@
 
 package com.android.systemui.flags;
 
+import com.android.internal.annotations.Keep;
 import com.android.systemui.R;
 
 import java.lang.reflect.Field;
@@ -144,7 +145,7 @@
     /***************************************/
     // 900 - media
     public static final BooleanFlag MEDIA_TAP_TO_TRANSFER = new BooleanFlag(900, true);
-    public static final BooleanFlag MEDIA_SESSION_ACTIONS = new BooleanFlag(901, true);
+    public static final BooleanFlag MEDIA_SESSION_ACTIONS = new BooleanFlag(901, false);
     public static final BooleanFlag MEDIA_SESSION_LAYOUT = new BooleanFlag(902, true);
     public static final BooleanFlag MEDIA_NEARBY_DEVICES = new BooleanFlag(903, true);
     public static final BooleanFlag MEDIA_MUTE_AWAIT = new BooleanFlag(904, true);
@@ -154,6 +155,7 @@
             new BooleanFlag(1000, true);
 
     // 1100 - windowing
+    @Keep
     public static final SysPropBooleanFlag WM_ENABLE_SHELL_TRANSITIONS =
             new SysPropBooleanFlag(1100, "persist.wm.debug.shell_transit", false);
 
diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java
index c01d2c3..736e2e0 100644
--- a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java
+++ b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java
@@ -1706,14 +1706,6 @@
                 if (DEBUG) Log.d(TAG, "doKeyguard: not showing because lockscreen is off");
                 return;
             }
-
-            if (mLockPatternUtils.checkVoldPassword(KeyguardUpdateMonitor.getCurrentUser())) {
-                if (DEBUG) Log.d(TAG, "Not showing lock screen since just decrypted");
-                // Without this, settings is not enabled until the lock screen first appears
-                setShowingLocked(false);
-                hideLocked();
-                return;
-            }
         }
 
         if (DEBUG) Log.d(TAG, "doKeyguard: showing the lock screen");
diff --git a/packages/SystemUI/src/com/android/systemui/media/MediaControlPanel.java b/packages/SystemUI/src/com/android/systemui/media/MediaControlPanel.java
index 831a606..ffdd537 100644
--- a/packages/SystemUI/src/com/android/systemui/media/MediaControlPanel.java
+++ b/packages/SystemUI/src/com/android/systemui/media/MediaControlPanel.java
@@ -54,6 +54,7 @@
 import com.android.systemui.R;
 import com.android.systemui.animation.ActivityLaunchAnimator;
 import com.android.systemui.animation.GhostedViewLaunchAnimatorController;
+import com.android.systemui.broadcast.BroadcastSender;
 import com.android.systemui.dagger.qualifiers.Background;
 import com.android.systemui.media.dialog.MediaOutputDialogFactory;
 import com.android.systemui.monet.ColorScheme;
@@ -104,10 +105,18 @@
             R.id.action4
     };
 
+    // Buttons to show in small player when using semantic actions
+    private static final List<Integer> SEMANTIC_ACTION_IDS = List.of(
+            R.id.actionPlayPause,
+            R.id.actionPrev,
+            R.id.actionNext
+    );
+
     private final SeekBarViewModel mSeekBarViewModel;
     private SeekBarObserver mSeekBarObserver;
     protected final Executor mBackgroundExecutor;
     private final ActivityStarter mActivityStarter;
+    private final BroadcastSender mBroadcastSender;
 
     private Context mContext;
     private MediaViewHolder mMediaViewHolder;
@@ -128,7 +137,6 @@
     private MediaCarouselController mMediaCarouselController;
     private final MediaOutputDialogFactory mMediaOutputDialogFactory;
     private final FalsingManager mFalsingManager;
-    private final MediaFlags mMediaFlags;
 
     // Used for swipe-to-dismiss logging.
     protected boolean mIsImpressed = false;
@@ -142,21 +150,22 @@
      */
     @Inject
     public MediaControlPanel(Context context, @Background Executor backgroundExecutor,
-            ActivityStarter activityStarter, MediaViewController mediaViewController,
-            SeekBarViewModel seekBarViewModel, Lazy<MediaDataManager> lazyMediaDataManager,
+            ActivityStarter activityStarter, BroadcastSender broadcastSender,
+            MediaViewController mediaViewController, SeekBarViewModel seekBarViewModel,
+            Lazy<MediaDataManager> lazyMediaDataManager,
             MediaOutputDialogFactory mediaOutputDialogFactory,
             MediaCarouselController mediaCarouselController,
-            FalsingManager falsingManager, MediaFlags mediaFlags, SystemClock systemClock) {
+            FalsingManager falsingManager, SystemClock systemClock) {
         mContext = context;
         mBackgroundExecutor = backgroundExecutor;
         mActivityStarter = activityStarter;
+        mBroadcastSender = broadcastSender;
         mSeekBarViewModel = seekBarViewModel;
         mMediaViewController = mediaViewController;
         mMediaDataManagerLazy = lazyMediaDataManager;
         mMediaOutputDialogFactory = mediaOutputDialogFactory;
         mMediaCarouselController = mediaCarouselController;
         mFalsingManager = falsingManager;
-        mMediaFlags = mediaFlags;
         mSystemClock = systemClock;
         loadDimens();
 
@@ -495,17 +504,16 @@
         List<MediaAction> actionIcons = data.getActions();
         List<Integer> actionsWhenCollapsed = data.getActionsToShowInCompact();
 
-        // If the session actions flag is enabled, but we're still using the regular layout, use
-        // the session actions anyways
-        if (mMediaFlags.areMediaSessionActionsEnabled() && data.getSemanticActions() != null) {
+        // If we got session actions, use those instead
+        if (data.getSemanticActions() != null) {
             MediaButton semanticActions = data.getSemanticActions();
 
             actionIcons = new ArrayList<MediaAction>();
-            actionIcons.add(semanticActions.getStartCustom());
+            actionIcons.add(semanticActions.getCustom0());
             actionIcons.add(semanticActions.getPrevOrCustom());
             actionIcons.add(semanticActions.getPlayOrPause());
             actionIcons.add(semanticActions.getNextOrCustom());
-            actionIcons.add(semanticActions.getEndCustom());
+            actionIcons.add(semanticActions.getCustom1());
 
             actionsWhenCollapsed = new ArrayList<Integer>();
             actionsWhenCollapsed.add(1);
@@ -562,6 +570,9 @@
 
     /** Bind elements specific to PlayerSessionViewHolder */
     private void bindSessionPlayer(@NonNull MediaData data, String key) {
+        ConstraintSet expandedSet = mMediaViewController.getExpandedLayout();
+        ConstraintSet collapsedSet = mMediaViewController.getCollapsedLayout();
+
         // Default colors
         int surfaceColor = mBackgroundColor;
         int accentPrimary = com.android.settingslib.Utils.getColorAttr(mContext,
@@ -575,25 +586,6 @@
         int textTertiary = com.android.settingslib.Utils.getColorAttr(mContext,
                 com.android.internal.R.attr.textColorTertiary).getDefaultColor();
 
-        // App icon - use launcher icon
-        ImageView appIconView = mMediaViewHolder.getAppIcon();
-        appIconView.clearColorFilter();
-        try {
-            Drawable icon = mContext.getPackageManager().getApplicationIcon(
-                    data.getPackageName());
-            appIconView.setImageDrawable(icon);
-        } catch (PackageManager.NameNotFoundException e) {
-            Log.w(TAG, "Cannot find icon for package " + data.getPackageName(), e);
-            // Fall back to notification icon
-            if (data.getAppIcon() != null) {
-                appIconView.setImageIcon(data.getAppIcon());
-            } else {
-                appIconView.setImageResource(R.drawable.ic_music_note);
-            }
-            int color = mContext.getColor(R.color.material_dynamic_secondary10);
-            appIconView.setColorFilter(color);
-        }
-
         // Album art
         ColorScheme colorScheme = null;
         ImageView albumView = mMediaViewHolder.getAlbumView();
@@ -640,6 +632,25 @@
                 ColorStateList.valueOf(surfaceColor));
         mMediaViewHolder.getPlayer().setBackgroundTintList(bgColorList);
 
+        // App icon - use notification icon
+        ImageView appIconView = mMediaViewHolder.getAppIcon();
+        appIconView.clearColorFilter();
+        if (data.getAppIcon() != null && !data.getResumption()) {
+            appIconView.setImageIcon(data.getAppIcon());
+            appIconView.setColorFilter(accentPrimary);
+        } else {
+            // Resume players use launcher icon
+            appIconView.setColorFilter(getGrayscaleFilter());
+            try {
+                Drawable icon = mContext.getPackageManager().getApplicationIcon(
+                        data.getPackageName());
+                appIconView.setImageDrawable(icon);
+            } catch (PackageManager.NameNotFoundException e) {
+                Log.w(TAG, "Cannot find icon for package " + data.getPackageName(), e);
+                appIconView.setImageResource(R.drawable.ic_music_note);
+            }
+        }
+
         // Metadata text
         mMediaViewHolder.getTitleText().setTextColor(textPrimary);
         mMediaViewHolder.getArtistText().setTextColor(textSecondary);
@@ -660,26 +671,68 @@
 
         // Media action buttons
         MediaButton semanticActions = data.getSemanticActions();
+        PlayerSessionViewHolder sessionHolder = (PlayerSessionViewHolder) mMediaViewHolder;
+        ImageButton[] genericButtons = new ImageButton[]{
+                sessionHolder.getAction0(),
+                sessionHolder.getAction1(),
+                sessionHolder.getAction2(),
+                sessionHolder.getAction3(),
+                sessionHolder.getAction4()};
+
+        ImageButton[] semanticButtons = new ImageButton[]{
+                sessionHolder.getActionPlayPause(),
+                sessionHolder.getActionNext(),
+                sessionHolder.getActionPrev()};
+
         if (semanticActions != null) {
-            PlayerSessionViewHolder sessionHolder = (PlayerSessionViewHolder) mMediaViewHolder;
+            // Hide all the generic buttons
+            for (ImageButton b: genericButtons) {
+                setVisibleAndAlpha(collapsedSet, b.getId(), false);
+                setVisibleAndAlpha(expandedSet, b.getId(), false);
+            }
 
             // Play/pause button has a background
             sessionHolder.getActionPlayPause().setBackgroundTintList(accentColorList);
             setSemanticButton(sessionHolder.getActionPlayPause(), semanticActions.getPlayOrPause(),
-                    ColorStateList.valueOf(textPrimaryInverse));
+                    ColorStateList.valueOf(textPrimaryInverse), collapsedSet, expandedSet, true);
 
             setSemanticButton(sessionHolder.getActionNext(), semanticActions.getNextOrCustom(),
-                    textColorList);
+                    textColorList, collapsedSet, expandedSet, true);
             setSemanticButton(sessionHolder.getActionPrev(), semanticActions.getPrevOrCustom(),
-                    textColorList);
-            setSemanticButton(sessionHolder.getActionStart(), semanticActions.getStartCustom(),
-                    textColorList);
-            setSemanticButton(sessionHolder.getActionEnd(), semanticActions.getEndCustom(),
-                    textColorList);
+                    textColorList, collapsedSet, expandedSet, true);
+            setSemanticButton(sessionHolder.getAction0(), semanticActions.getCustom0(),
+                    textColorList, collapsedSet, expandedSet, false);
+            setSemanticButton(sessionHolder.getAction1(), semanticActions.getCustom1(),
+                    textColorList, collapsedSet, expandedSet, false);
         } else {
-            Log.w(TAG, "Using semantic player, but did not get buttons");
+            // Hide all the semantic buttons
+            for (int id : SEMANTIC_ACTION_IDS) {
+                setVisibleAndAlpha(collapsedSet, id, false);
+                setVisibleAndAlpha(expandedSet, id, false);
+            }
+
+            // Set all the generic buttons
+            List<Integer> actionsWhenCollapsed = data.getActionsToShowInCompact();
+            List<MediaAction> actions = data.getActions();
+            int i = 0;
+            for (; i < actions.size(); i++) {
+                boolean showInCompact = actionsWhenCollapsed.contains(i);
+                setSemanticButton(genericButtons[i], actions.get(i), textColorList, collapsedSet,
+                        expandedSet, showInCompact);
+            }
+            for (; i < 5; i++) {
+                // Hide any unused buttons
+                setSemanticButton(genericButtons[i], null, textColorList, collapsedSet,
+                        expandedSet, false);
+            }
         }
 
+        // If disabled, set progress bar to INVISIBLE instead of GONE so layout weights still work
+        boolean seekbarEnabled = mSeekBarViewModel.getEnabled();
+        expandedSet.setVisibility(R.id.media_progress_bar,
+                seekbarEnabled ? ConstraintSet.VISIBLE : ConstraintSet.INVISIBLE);
+        expandedSet.setAlpha(R.id.media_progress_bar, seekbarEnabled ? 1.0f : 0.0f);
+
         // Long press buttons
         mMediaViewHolder.getLongPressText().setTextColor(textColorList);
         mMediaViewHolder.getSettingsText().setTextColor(textColorList);
@@ -688,11 +741,11 @@
         mMediaViewHolder.getCancelText().setBackgroundTintList(accentColorList);
         mMediaViewHolder.getDismissText().setTextColor(textColorList);
         mMediaViewHolder.getDismissText().setBackgroundTintList(accentColorList);
-
     }
 
     private void setSemanticButton(final ImageButton button, MediaAction mediaAction,
-            ColorStateList fgColor) {
+            ColorStateList fgColor, ConstraintSet collapsedSet, ConstraintSet expandedSet,
+            boolean showInCompact) {
         button.setImageTintList(fgColor);
         if (mediaAction != null) {
             button.setImageIcon(mediaAction.getIcon());
@@ -716,6 +769,9 @@
             button.setContentDescription(null);
             button.setEnabled(false);
         }
+
+        setVisibleAndAlpha(collapsedSet, button.getId(), mediaAction != null && showInCompact);
+        setVisibleAndAlpha(expandedSet, button.getId(), mediaAction != null);
     }
 
     @Nullable
@@ -899,7 +955,7 @@
                 // Dismiss the card Smartspace data through Smartspace trampoline activity.
                 mContext.startActivity(dismissIntent);
             } else {
-                mContext.sendBroadcast(dismissIntent);
+                mBroadcastSender.sendBroadcast(dismissIntent);
             }
         });
 
diff --git a/packages/SystemUI/src/com/android/systemui/media/MediaData.kt b/packages/SystemUI/src/com/android/systemui/media/MediaData.kt
index 500e82e..4cf6291 100644
--- a/packages/SystemUI/src/com/android/systemui/media/MediaData.kt
+++ b/packages/SystemUI/src/com/android/systemui/media/MediaData.kt
@@ -149,11 +149,11 @@
     /**
      * First custom action space
      */
-    var startCustom: MediaAction? = null,
+    var custom0: MediaAction? = null,
     /**
-     * Last custom action space
+     * Second custom action space
      */
-    var endCustom: MediaAction? = null
+    var custom1: MediaAction? = null
 )
 
 /** State of a media action. */
diff --git a/packages/SystemUI/src/com/android/systemui/media/MediaDataFilter.kt b/packages/SystemUI/src/com/android/systemui/media/MediaDataFilter.kt
index ae5c1f2..de44a9c 100644
--- a/packages/SystemUI/src/com/android/systemui/media/MediaDataFilter.kt
+++ b/packages/SystemUI/src/com/android/systemui/media/MediaDataFilter.kt
@@ -21,6 +21,7 @@
 import android.util.Log
 import com.android.internal.annotations.VisibleForTesting
 import com.android.systemui.broadcast.BroadcastDispatcher
+import com.android.systemui.broadcast.BroadcastSender
 import com.android.systemui.dagger.qualifiers.Main
 import com.android.systemui.settings.CurrentUserTracker
 import com.android.systemui.statusbar.NotificationLockscreenUserManager
@@ -56,6 +57,7 @@
 class MediaDataFilter @Inject constructor(
     private val context: Context,
     private val broadcastDispatcher: BroadcastDispatcher,
+    private val broadcastSender: BroadcastSender,
     private val lockscreenUserManager: NotificationLockscreenUserManager,
     @Main private val executor: Executor,
     private val systemClock: SystemClock
@@ -249,7 +251,7 @@
                 // Dismiss the card Smartspace data through Smartspace trampoline activity.
                 context.startActivity(dismissIntent)
             } else {
-                context.sendBroadcast(dismissIntent)
+                broadcastSender.sendBroadcast(dismissIntent)
             }
             smartspaceMediaData = EMPTY_SMARTSPACE_MEDIA_DATA.copy(
                 targetId = smartspaceMediaData.targetId, isValid = smartspaceMediaData.isValid)
diff --git a/packages/SystemUI/src/com/android/systemui/media/MediaDataManager.kt b/packages/SystemUI/src/com/android/systemui/media/MediaDataManager.kt
index fab06c2..ea92abd 100644
--- a/packages/SystemUI/src/com/android/systemui/media/MediaDataManager.kt
+++ b/packages/SystemUI/src/com/android/systemui/media/MediaDataManager.kt
@@ -610,7 +610,8 @@
         var actionIcons: List<MediaAction> = emptyList()
         var actionsToShowCollapsed: List<Int> = emptyList()
         var semanticActions: MediaButton? = null
-        if (mediaFlags.areMediaSessionActionsEnabled() && mediaController.playbackState != null) {
+        if (mediaFlags.areMediaSessionActionsEnabled(sbn.packageName, sbn.user.identifier) &&
+                mediaController.playbackState != null) {
             semanticActions = createActionsFromState(sbn.packageName, mediaController)
         } else {
             val actions = createActionsFromNotification(sbn)
@@ -726,7 +727,7 @@
                 }
             }
 
-            // Finally, assign the remaining button slots: C A play/pause B D
+            // Finally, assign the remaining button slots: play/pause A B C D
             // A = previous, else custom action (if not reserved)
             // B = next, else custom action (if not reserved)
             // C and D are always custom actions
@@ -752,8 +753,8 @@
                 null
             }
 
-            actions.startCustom = customActions[customIdx++]
-            actions.endCustom = customActions[customIdx++]
+            actions.custom0 = customActions[customIdx++]
+            actions.custom1 = customActions[customIdx++]
         }
         return actions
     }
diff --git a/packages/SystemUI/src/com/android/systemui/media/MediaFlags.kt b/packages/SystemUI/src/com/android/systemui/media/MediaFlags.kt
index dd35a9a..f5200f9 100644
--- a/packages/SystemUI/src/com/android/systemui/media/MediaFlags.kt
+++ b/packages/SystemUI/src/com/android/systemui/media/MediaFlags.kt
@@ -16,6 +16,7 @@
 
 package com.android.systemui.media
 
+import android.app.StatusBarManager
 import com.android.systemui.dagger.SysUISingleton
 import com.android.systemui.flags.FeatureFlags
 import com.android.systemui.flags.Flags
@@ -26,16 +27,17 @@
     /**
      * Check whether media control actions should be based on PlaybackState instead of notification
      */
-    fun areMediaSessionActionsEnabled(): Boolean {
-        return featureFlags.isEnabled(Flags.MEDIA_SESSION_ACTIONS)
+    fun areMediaSessionActionsEnabled(packageName: String, userId: Int): Boolean {
+        val enabled = StatusBarManager.useMediaSessionActionsForApp(packageName, userId)
+        // Allow global override with flag
+        return enabled || featureFlags.isEnabled(Flags.MEDIA_SESSION_ACTIONS)
     }
 
     /**
      * Check whether media controls should use the new session-based layout
      */
     fun useMediaSessionLayout(): Boolean {
-        return featureFlags.isEnabled(Flags.MEDIA_SESSION_ACTIONS) &&
-            featureFlags.isEnabled(Flags.MEDIA_SESSION_LAYOUT)
+        return featureFlags.isEnabled(Flags.MEDIA_SESSION_LAYOUT)
     }
 
     /**
diff --git a/packages/SystemUI/src/com/android/systemui/media/MediaHierarchyManager.kt b/packages/SystemUI/src/com/android/systemui/media/MediaHierarchyManager.kt
index 18b6699..7a4dee2 100644
--- a/packages/SystemUI/src/com/android/systemui/media/MediaHierarchyManager.kt
+++ b/packages/SystemUI/src/com/android/systemui/media/MediaHierarchyManager.kt
@@ -292,6 +292,18 @@
     }
 
     /**
+     * Returns the amount of translationY of the media container, during the current guided
+     * transformation, if running. If there is no guided transformation running, it will return 0.
+     */
+    fun getGuidedTransformationTranslationY(): Int {
+        if (!isCurrentlyInGuidedTransformation()) {
+            return -1
+        }
+        val startHost = getHost(previousLocation) ?: return 0
+        return targetBounds.top - startHost.currentBounds.top
+    }
+
+    /**
      * Is the shade currently collapsing from the expanded qs? If we're on the lockscreen and in qs,
      * we wouldn't want to transition in that case.
      */
diff --git a/packages/SystemUI/src/com/android/systemui/media/MediaHost.kt b/packages/SystemUI/src/com/android/systemui/media/MediaHost.kt
index 0a4b68b..eb209f7 100644
--- a/packages/SystemUI/src/com/android/systemui/media/MediaHost.kt
+++ b/packages/SystemUI/src/com/android/systemui/media/MediaHost.kt
@@ -192,6 +192,14 @@
                 }
             }
 
+        override var squishFraction: Float = 1.0f
+            set(value) {
+                if (!value.equals(field)) {
+                    field = value
+                    changedListener?.invoke()
+                }
+            }
+
         override var showsOnlyActiveMedia: Boolean = false
             set(value) {
                 if (!value.equals(field)) {
@@ -242,6 +250,7 @@
         override fun copy(): MediaHostState {
             val mediaHostState = MediaHostStateHolder()
             mediaHostState.expansion = expansion
+            mediaHostState.squishFraction = squishFraction
             mediaHostState.showsOnlyActiveMedia = showsOnlyActiveMedia
             mediaHostState.measurementInput = measurementInput?.copy()
             mediaHostState.visible = visible
@@ -260,6 +269,9 @@
             if (expansion != other.expansion) {
                 return false
             }
+            if (squishFraction != other.squishFraction) {
+                return false
+            }
             if (showsOnlyActiveMedia != other.showsOnlyActiveMedia) {
                 return false
             }
@@ -278,6 +290,7 @@
         override fun hashCode(): Int {
             var result = measurementInput?.hashCode() ?: 0
             result = 31 * result + expansion.hashCode()
+            result = 31 * result + squishFraction.hashCode()
             result = 31 * result + falsingProtectionNeeded.hashCode()
             result = 31 * result + showsOnlyActiveMedia.hashCode()
             result = 31 * result + if (visible) 1 else 2
@@ -318,6 +331,11 @@
     var expansion: Float
 
     /**
+     * Fraction of the height animation.
+     */
+    var squishFraction: Float
+
+    /**
      * Is this host only showing active media or is it showing all of them including resumption?
      */
     var showsOnlyActiveMedia: Boolean
diff --git a/packages/SystemUI/src/com/android/systemui/media/MediaProjectionPermissionActivity.java b/packages/SystemUI/src/com/android/systemui/media/MediaProjectionPermissionActivity.java
index 77873e8..3860409 100644
--- a/packages/SystemUI/src/com/android/systemui/media/MediaProjectionPermissionActivity.java
+++ b/packages/SystemUI/src/com/android/systemui/media/MediaProjectionPermissionActivity.java
@@ -54,7 +54,7 @@
     private int mUid;
     private IMediaProjectionManager mService;
 
-    private SystemUIDialog mDialog;
+    private AlertDialog mDialog;
 
     @Override
     public void onCreate(Bundle icicle) {
@@ -141,13 +141,18 @@
             dialogTitle = getString(R.string.media_projection_dialog_title, appName);
         }
 
-        mDialog = new SystemUIDialog(this);
-        mDialog.setTitle(dialogTitle);
-        mDialog.setIcon(R.drawable.ic_media_projection_permission);
-        mDialog.setMessage(dialogText);
-        mDialog.setPositiveButton(R.string.media_projection_action_text, this);
-        mDialog.setNeutralButton(android.R.string.cancel, this);
-        mDialog.setOnCancelListener(this);
+        mDialog = new AlertDialog.Builder(this, R.style.Theme_SystemUI_Dialog)
+                .setTitle(dialogTitle)
+                .setIcon(R.drawable.ic_media_projection_permission)
+                .setMessage(dialogText)
+                .setPositiveButton(R.string.media_projection_action_text, this)
+                .setNeutralButton(android.R.string.cancel, this)
+                .setOnCancelListener(this)
+                .create();
+
+        SystemUIDialog.registerDismissListener(mDialog);
+        SystemUIDialog.applyFlags(mDialog);
+        SystemUIDialog.setDialogSize(mDialog);
 
         mDialog.create();
         mDialog.getButton(DialogInterface.BUTTON_POSITIVE).setFilterTouchesWhenObscured(true);
@@ -186,7 +191,7 @@
     private Intent getMediaProjectionIntent(int uid, String packageName)
             throws RemoteException {
         IMediaProjection projection = mService.createProjection(uid, packageName,
-                 MediaProjectionManager.TYPE_SCREEN_CAPTURE, false /* permanentGrant */);
+                MediaProjectionManager.TYPE_SCREEN_CAPTURE, false /* permanentGrant */);
         Intent intent = new Intent();
         intent.putExtra(MediaProjectionManager.EXTRA_MEDIA_PROJECTION, projection.asBinder());
         return intent;
diff --git a/packages/SystemUI/src/com/android/systemui/media/MediaViewController.kt b/packages/SystemUI/src/com/android/systemui/media/MediaViewController.kt
index 591aad1..a60016b 100644
--- a/packages/SystemUI/src/com/android/systemui/media/MediaViewController.kt
+++ b/packages/SystemUI/src/com/android/systemui/media/MediaViewController.kt
@@ -18,13 +18,11 @@
 
 import android.content.Context
 import android.content.res.Configuration
+import androidx.annotation.VisibleForTesting
 import androidx.constraintlayout.widget.ConstraintSet
 import com.android.systemui.R
 import com.android.systemui.statusbar.policy.ConfigurationController
-import com.android.systemui.util.animation.MeasurementOutput
-import com.android.systemui.util.animation.TransitionLayout
-import com.android.systemui.util.animation.TransitionLayoutController
-import com.android.systemui.util.animation.TransitionViewState
+import com.android.systemui.util.animation.*
 import javax.inject.Inject
 
 /**
@@ -270,7 +268,6 @@
             TYPE.PLAYER_SESSION -> PlayerSessionViewHolder.gutsIds
             TYPE.RECOMMENDATION -> RecommendationViewHolder.gutsIds
         }
-
         controlsIds.forEach { id ->
             viewState.widgetStates.get(id)?.let { state ->
                 // Make sure to use the unmodified state if guts are not visible.
@@ -282,59 +279,79 @@
             viewState.widgetStates.get(id)?.alpha = if (isGutsVisible) 1f else 0f
             viewState.widgetStates.get(id)?.gone = !isGutsVisible
         }
-
         if (shouldHideGutsSettings) {
             viewState.widgetStates.get(R.id.settings)?.gone = true
         }
     }
 
     /**
+     * Apply squishFraction to a copy of viewState such that the cached version is untouched.
+     */
+    private fun squishViewState(viewState: TransitionViewState,
+                                squishFraction: Float): TransitionViewState {
+        val squishedViewState = viewState.copy()
+        squishedViewState.height = (squishedViewState.height * squishFraction).toInt()
+        val albumArtViewState = viewState.widgetStates.get(R.id.album_art)
+        if (albumArtViewState != null) {
+            albumArtViewState.height = squishedViewState.height
+        }
+        return squishedViewState;
+    }
+
+    /**
      * Obtain a new viewState for a given media state. This usually returns a cached state, but if
      * it's not available, it will recreate one by measuring, which may be expensive.
      */
-    private fun obtainViewState(state: MediaHostState?): TransitionViewState? {
+    @VisibleForTesting
+    public fun obtainViewState(state: MediaHostState?): TransitionViewState? {
         if (state == null || state.measurementInput == null) {
             return null
         }
         // Only a subset of the state is relevant to get a valid viewState. Let's get the cachekey
         var cacheKey = getKey(state, isGutsVisible, tmpKey)
         val viewState = viewStates[cacheKey]
+
         if (viewState != null) {
             // we already have cached this measurement, let's continue
+            if (state.squishFraction < 1f) {
+                return squishViewState(viewState, state.squishFraction);
+            }
             return viewState
         }
         // Copy the key since this might call recursively into it and we're using tmpKey
         cacheKey = cacheKey.copy()
         val result: TransitionViewState?
-        if (transitionLayout != null) {
-            // Let's create a new measurement
-            if (state.expansion == 0.0f || state.expansion == 1.0f) {
-                result = transitionLayout!!.calculateViewState(
-                        state.measurementInput!!,
-                        constraintSetForExpansion(state.expansion),
-                        TransitionViewState())
-
-                setGutsViewState(result)
-                // We don't want to cache interpolated or null states as this could quickly fill up
-                // our cache. We only cache the start and the end states since the interpolation
-                // is cheap
-                viewStates[cacheKey] = result
-            } else {
-                // This is an interpolated state
-                val startState = state.copy().also { it.expansion = 0.0f }
-
-                // Given that we have a measurement and a view, let's get (guaranteed) viewstates
-                // from the start and end state and interpolate them
-                val startViewState = obtainViewState(startState) as TransitionViewState
-                val endState = state.copy().also { it.expansion = 1.0f }
-                val endViewState = obtainViewState(endState) as TransitionViewState
-                result = layoutController.getInterpolatedState(
-                        startViewState,
-                        endViewState,
-                        state.expansion)
-            }
+        if (transitionLayout == null) {
+            return null
+        }
+        // Not cached. Let's create a new measurement
+        if (state.expansion == 0.0f || state.expansion == 1.0f) {
+            result = transitionLayout!!.calculateViewState(
+                    state.measurementInput!!,
+                    constraintSetForExpansion(state.expansion),
+                    TransitionViewState())
+            // We don't want to cache interpolated or null states as this could quickly fill up
+            // our cache. We only cache the start and the end states since the interpolation
+            // is cheap
+            setGutsViewState(result)
+            viewStates[cacheKey] = result
         } else {
-            result = null
+            // This is an interpolated state
+            val startState = state.copy().also { it.expansion = 0.0f }
+
+            // Given that we have a measurement and a view, let's get (guaranteed) viewstates
+            // from the start and end state and interpolate them
+            val startViewState = obtainViewState(startState) as TransitionViewState
+            val endState = state.copy().also { it.expansion = 1.0f }
+
+            val endViewState = obtainViewState(endState) as TransitionViewState
+            result = layoutController.getInterpolatedState(
+                    startViewState,
+                    endViewState,
+                    state.expansion)
+        }
+        if (state.squishFraction < 1f) {
+            return squishViewState(result, state.squishFraction);
         }
         return result
     }
diff --git a/packages/SystemUI/src/com/android/systemui/media/MediaViewHolder.kt b/packages/SystemUI/src/com/android/systemui/media/MediaViewHolder.kt
index e57b247..5f60696 100644
--- a/packages/SystemUI/src/com/android/systemui/media/MediaViewHolder.kt
+++ b/packages/SystemUI/src/com/android/systemui/media/MediaViewHolder.kt
@@ -60,12 +60,24 @@
     val settings = itemView.requireViewById<View>(R.id.settings)
     val settingsText = itemView.requireViewById<TextView>(R.id.settings_text)
 
+    // Action Buttons
+    val action0 = itemView.requireViewById<ImageButton>(R.id.action0)
+    val action1 = itemView.requireViewById<ImageButton>(R.id.action1)
+    val action2 = itemView.requireViewById<ImageButton>(R.id.action2)
+    val action3 = itemView.requireViewById<ImageButton>(R.id.action3)
+    val action4 = itemView.requireViewById<ImageButton>(R.id.action4)
+
     init {
         (player.background as IlluminationDrawable).let {
             it.registerLightSource(seamless)
             it.registerLightSource(cancel)
             it.registerLightSource(dismiss)
             it.registerLightSource(settings)
+            it.registerLightSource(action0)
+            it.registerLightSource(action1)
+            it.registerLightSource(action2)
+            it.registerLightSource(action3)
+            it.registerLightSource(action4)
         }
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/media/PlayerSessionViewHolder.kt b/packages/SystemUI/src/com/android/systemui/media/PlayerSessionViewHolder.kt
index 87d2cff..6928ebb 100644
--- a/packages/SystemUI/src/com/android/systemui/media/PlayerSessionViewHolder.kt
+++ b/packages/SystemUI/src/com/android/systemui/media/PlayerSessionViewHolder.kt
@@ -31,16 +31,12 @@
     val actionPlayPause = itemView.requireViewById<ImageButton>(R.id.actionPlayPause)
     val actionNext = itemView.requireViewById<ImageButton>(R.id.actionNext)
     val actionPrev = itemView.requireViewById<ImageButton>(R.id.actionPrev)
-    val actionStart = itemView.requireViewById<ImageButton>(R.id.actionStart)
-    val actionEnd = itemView.requireViewById<ImageButton>(R.id.actionEnd)
 
     init {
         (player.background as IlluminationDrawable).let {
             it.registerLightSource(actionPlayPause)
             it.registerLightSource(actionNext)
             it.registerLightSource(actionPrev)
-            it.registerLightSource(actionStart)
-            it.registerLightSource(actionEnd)
         }
     }
 
@@ -49,8 +45,11 @@
             R.id.actionPlayPause -> actionPlayPause
             R.id.actionNext -> actionNext
             R.id.actionPrev -> actionPrev
-            R.id.actionStart -> actionStart
-            R.id.actionEnd -> actionEnd
+            R.id.action0 -> action0
+            R.id.action1 -> action1
+            R.id.action2 -> action2
+            R.id.action3 -> action3
+            R.id.action4 -> action4
             else -> {
                 throw IllegalArgumentException()
             }
@@ -90,8 +89,11 @@
                 R.id.actionPlayPause,
                 R.id.actionNext,
                 R.id.actionPrev,
-                R.id.actionStart,
-                R.id.actionEnd,
+                R.id.action0,
+                R.id.action1,
+                R.id.action2,
+                R.id.action3,
+                R.id.action4,
                 R.id.icon
         )
         val gutsIds = setOf(
diff --git a/packages/SystemUI/src/com/android/systemui/media/PlayerViewHolder.kt b/packages/SystemUI/src/com/android/systemui/media/PlayerViewHolder.kt
index 20b2d4a..dd3fa89 100644
--- a/packages/SystemUI/src/com/android/systemui/media/PlayerViewHolder.kt
+++ b/packages/SystemUI/src/com/android/systemui/media/PlayerViewHolder.kt
@@ -33,23 +33,6 @@
     override val elapsedTimeView = itemView.requireViewById<TextView>(R.id.media_elapsed_time)
     override val totalTimeView = itemView.requireViewById<TextView>(R.id.media_total_time)
 
-    // Action Buttons
-    val action0 = itemView.requireViewById<ImageButton>(R.id.action0)
-    val action1 = itemView.requireViewById<ImageButton>(R.id.action1)
-    val action2 = itemView.requireViewById<ImageButton>(R.id.action2)
-    val action3 = itemView.requireViewById<ImageButton>(R.id.action3)
-    val action4 = itemView.requireViewById<ImageButton>(R.id.action4)
-
-    init {
-        (player.background as IlluminationDrawable).let {
-            it.registerLightSource(action0)
-            it.registerLightSource(action1)
-            it.registerLightSource(action2)
-            it.registerLightSource(action3)
-            it.registerLightSource(action4)
-        }
-    }
-
     override fun getAction(id: Int): ImageButton {
         return when (id) {
             R.id.action0 -> action0
diff --git a/packages/SystemUI/src/com/android/systemui/media/SeekBarObserver.kt b/packages/SystemUI/src/com/android/systemui/media/SeekBarObserver.kt
index cf997055..57701ab 100644
--- a/packages/SystemUI/src/com/android/systemui/media/SeekBarObserver.kt
+++ b/packages/SystemUI/src/com/android/systemui/media/SeekBarObserver.kt
@@ -50,25 +50,46 @@
                 .getDimensionPixelSize(R.dimen.qs_media_disabled_seekbar_vertical_padding)
     }
 
+    init {
+        val seekBarProgressWavelength = holder.seekBar.context.resources
+                .getDimensionPixelSize(R.dimen.qs_media_seekbar_progress_wavelength).toFloat()
+        val seekBarProgressAmplitude = holder.seekBar.context.resources
+                .getDimensionPixelSize(R.dimen.qs_media_seekbar_progress_amplitude).toFloat()
+        val seekBarProgressPhase = holder.seekBar.context.resources
+                .getDimensionPixelSize(R.dimen.qs_media_seekbar_progress_phase).toFloat()
+        val seekBarProgressStrokeWidth = holder.seekBar.context.resources
+                .getDimensionPixelSize(R.dimen.qs_media_seekbar_progress_stroke_width).toFloat()
+        val progressDrawable = holder.seekBar.progressDrawable as? SquigglyProgress
+        progressDrawable?.let {
+            it.waveLength = seekBarProgressWavelength
+            it.lineAmplitude = seekBarProgressAmplitude
+            it.phaseSpeed = seekBarProgressPhase
+            it.strokeWidth = seekBarProgressStrokeWidth
+        }
+    }
+
     /** Updates seek bar views when the data model changes. */
     @UiThread
     override fun onChanged(data: SeekBarViewModel.Progress) {
+        val progressDrawable = holder.seekBar.progressDrawable as? SquigglyProgress
         if (!data.enabled) {
             if (holder.seekBar.maxHeight != seekBarDisabledHeight) {
                 holder.seekBar.maxHeight = seekBarDisabledHeight
                 setVerticalPadding(seekBarDisabledVerticalPadding)
             }
-            holder.seekBar.setEnabled(false)
-            holder.seekBar.getThumb().setAlpha(0)
-            holder.seekBar.setProgress(0)
-            holder.elapsedTimeView?.setText("")
-            holder.totalTimeView?.setText("")
+            holder.seekBar.isEnabled = false
+            progressDrawable?.animate = false
+            holder.seekBar.thumb.alpha = 0
+            holder.seekBar.progress = 0
+            holder.elapsedTimeView?.text = ""
+            holder.totalTimeView?.text = ""
             holder.seekBar.contentDescription = ""
             return
         }
 
-        holder.seekBar.getThumb().setAlpha(if (data.seekAvailable) 255 else 0)
-        holder.seekBar.setEnabled(data.seekAvailable)
+        holder.seekBar.thumb.alpha = if (data.seekAvailable) 255 else 0
+        holder.seekBar.isEnabled = data.seekAvailable
+        progressDrawable?.animate = data.playing
 
         if (holder.seekBar.maxHeight != seekBarEnabledMaxHeight) {
             holder.seekBar.maxHeight = seekBarEnabledMaxHeight
diff --git a/packages/SystemUI/src/com/android/systemui/media/SeekBarViewModel.kt b/packages/SystemUI/src/com/android/systemui/media/SeekBarViewModel.kt
index 9cf9c48..49cd161 100644
--- a/packages/SystemUI/src/com/android/systemui/media/SeekBarViewModel.kt
+++ b/packages/SystemUI/src/com/android/systemui/media/SeekBarViewModel.kt
@@ -31,6 +31,7 @@
 import androidx.lifecycle.LiveData
 import androidx.lifecycle.MutableLiveData
 import com.android.systemui.dagger.qualifiers.Background
+import com.android.systemui.statusbar.NotificationMediaManager
 import com.android.systemui.util.concurrency.RepeatableExecutor
 import javax.inject.Inject
 
@@ -73,7 +74,7 @@
 class SeekBarViewModel @Inject constructor(
     @Background private val bgExecutor: RepeatableExecutor
 ) {
-    private var _data = Progress(false, false, null, 0)
+    private var _data = Progress(false, false, false, null, 0)
         set(value) {
             field = value
             _progress.postValue(value)
@@ -131,6 +132,8 @@
 
     lateinit var logSmartspaceClick: () -> Unit
 
+    fun getEnabled() = _data.enabled
+
     /**
      * Event indicating that the user has started interacting with the seek bar.
      */
@@ -192,10 +195,12 @@
         val seekAvailable = ((playbackState?.actions ?: 0L) and PlaybackState.ACTION_SEEK_TO) != 0L
         val position = playbackState?.position?.toInt()
         val duration = mediaMetadata?.getLong(MediaMetadata.METADATA_KEY_DURATION)?.toInt() ?: 0
+        val playing = NotificationMediaManager
+                .isPlayingState(playbackState?.state ?: PlaybackState.STATE_NONE)
         val enabled = if (playbackState == null ||
                 playbackState?.getState() == PlaybackState.STATE_NONE ||
                 (duration <= 0)) false else true
-        _data = Progress(enabled, seekAvailable, position, duration)
+        _data = Progress(enabled, seekAvailable, playing, position, duration)
         checkIfPollingNeeded()
     }
 
@@ -412,6 +417,7 @@
     data class Progress(
         val enabled: Boolean,
         val seekAvailable: Boolean,
+        val playing: Boolean,
         val elapsedTime: Int?,
         val duration: Int
     )
diff --git a/packages/SystemUI/src/com/android/systemui/media/SquigglyProgress.kt b/packages/SystemUI/src/com/android/systemui/media/SquigglyProgress.kt
new file mode 100644
index 0000000..f1058e2
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/media/SquigglyProgress.kt
@@ -0,0 +1,184 @@
+package com.android.systemui.media
+
+import android.animation.Animator
+import android.animation.AnimatorListenerAdapter
+import android.animation.ValueAnimator
+import android.content.res.ColorStateList
+import android.graphics.Canvas
+import android.graphics.Color
+import android.graphics.ColorFilter
+import android.graphics.Paint
+import android.graphics.Path
+import android.graphics.PixelFormat
+import android.graphics.drawable.Drawable
+import android.os.SystemClock
+import androidx.annotation.VisibleForTesting
+import com.android.systemui.animation.Interpolators
+import kotlin.math.abs
+import kotlin.math.cos
+
+private const val TAG = "Squiggly"
+
+private const val TWO_PI = (Math.PI * 2f).toFloat()
+@VisibleForTesting
+internal const val DISABLED_ALPHA = 77
+
+class SquigglyProgress : Drawable() {
+
+    private val wavePaint = Paint()
+    private val linePaint = Paint()
+    private val path = Path()
+    private var heightFraction = 0f
+    private var heightAnimator: ValueAnimator? = null
+    private var phaseOffset = 0f
+    private var lastFrameTime = -1L
+
+    // Horizontal length of the sine wave
+    var waveLength = 0f
+    // Height of each peak of the sine wave
+    var lineAmplitude = 0f
+    // Line speed in px per second
+    var phaseSpeed = 0f
+    // Progress stroke width, both for wave and solid line
+    var strokeWidth = 0f
+        set(value) {
+            if (field == value) {
+                return
+            }
+            field = value
+            wavePaint.strokeWidth = value
+            linePaint.strokeWidth = value
+        }
+
+    init {
+        wavePaint.strokeCap = Paint.Cap.ROUND
+        linePaint.strokeCap = Paint.Cap.ROUND
+        linePaint.style = Paint.Style.STROKE
+        wavePaint.style = Paint.Style.STROKE
+        linePaint.alpha = DISABLED_ALPHA
+    }
+
+    var animate: Boolean = false
+        set(value) {
+            if (field == value) {
+                return
+            }
+            field = value
+            if (field) {
+                lastFrameTime = SystemClock.uptimeMillis()
+            }
+            heightAnimator?.cancel()
+            heightAnimator = ValueAnimator.ofFloat(heightFraction, if (animate) 1f else 0f).apply {
+                if (animate) {
+                    startDelay = 60
+                    duration = 800
+                    interpolator = Interpolators.EMPHASIZED_DECELERATE
+                } else {
+                    duration = 550
+                    interpolator = Interpolators.STANDARD_DECELERATE
+                }
+                addUpdateListener {
+                    heightFraction = it.animatedValue as Float
+                    invalidateSelf()
+                }
+                addListener(object : AnimatorListenerAdapter() {
+                    override fun onAnimationEnd(animation: Animator?) {
+                        heightAnimator = null
+                    }
+                })
+                start()
+            }
+        }
+
+    override fun draw(canvas: Canvas) {
+        if (animate) {
+            invalidateSelf()
+            val now = SystemClock.uptimeMillis()
+            phaseOffset -= (now - lastFrameTime) / 1000f * phaseSpeed
+            phaseOffset %= waveLength
+            lastFrameTime = now
+        }
+
+        val totalProgressPx = (bounds.width() * (level / 10_000f))
+        canvas.save()
+        canvas.translate(bounds.left.toFloat(), bounds.centerY().toFloat())
+        // Clip drawing, so we stop at the thumb
+        canvas.clipRect(
+                0f,
+                -lineAmplitude - strokeWidth,
+                totalProgressPx,
+                lineAmplitude + strokeWidth)
+
+        // The squiggly line
+        val start = phaseOffset
+        var currentX = start
+        var waveSign = 1f
+        path.rewind()
+        path.moveTo(start, lineAmplitude * heightFraction)
+        while (currentX < totalProgressPx) {
+            val nextX = currentX + waveLength / 2f
+            val nextWaveSign = waveSign * -1
+            path.cubicTo(
+                    currentX + waveLength / 4f, lineAmplitude * waveSign * heightFraction,
+                    nextX - waveLength / 4f, lineAmplitude * nextWaveSign * heightFraction,
+                    nextX, lineAmplitude * nextWaveSign * heightFraction)
+            currentX = nextX
+            waveSign = nextWaveSign
+        }
+        wavePaint.style = Paint.Style.STROKE
+        canvas.drawPath(path, wavePaint)
+        canvas.restore()
+
+        // Draw round line cap at the beginning of the wave
+        val startAmp = cos(abs(phaseOffset) / waveLength * TWO_PI)
+        val p = Paint()
+        p.color = Color.WHITE
+        canvas.drawPoint(
+                bounds.left.toFloat(),
+                bounds.centerY() + startAmp * lineAmplitude * heightFraction,
+                wavePaint)
+
+        // Draw continuous line, to the right of the thumb
+        canvas.drawLine(
+                bounds.left.toFloat() + totalProgressPx,
+                bounds.centerY().toFloat(),
+                bounds.width().toFloat(),
+                bounds.centerY().toFloat(),
+                linePaint)
+    }
+
+    override fun getOpacity(): Int {
+        return PixelFormat.TRANSLUCENT
+    }
+
+    override fun setColorFilter(colorFilter: ColorFilter?) {
+        wavePaint.colorFilter = colorFilter
+        linePaint.colorFilter = colorFilter
+    }
+
+    override fun setAlpha(alpha: Int) {
+        wavePaint.alpha = alpha
+        linePaint.alpha = (DISABLED_ALPHA * (alpha / 255f)).toInt()
+    }
+
+    override fun getAlpha(): Int {
+        return wavePaint.alpha
+    }
+
+    override fun setTint(tintColor: Int) {
+        wavePaint.color = tintColor
+        linePaint.color = tintColor
+    }
+
+    override fun onLevelChange(level: Int): Boolean {
+        return animate
+    }
+
+    override fun setTintList(tint: ColorStateList?) {
+        if (tint == null) {
+            return
+        }
+        wavePaint.color = tint.defaultColor
+        linePaint.color = tint.defaultColor
+    }
+}
\ No newline at end of file
diff --git a/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputBaseAdapter.java b/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputBaseAdapter.java
index c96aca3..62d5c8e 100644
--- a/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputBaseAdapter.java
+++ b/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputBaseAdapter.java
@@ -34,7 +34,6 @@
 import android.widget.ImageView;
 import android.widget.LinearLayout;
 import android.widget.ProgressBar;
-import android.widget.RelativeLayout;
 import android.widget.SeekBar;
 import android.widget.TextView;
 
@@ -129,7 +128,7 @@
         final ImageView mTitleIcon;
         final ProgressBar mProgressBar;
         final SeekBar mSeekBar;
-        final RelativeLayout mTwoLineLayout;
+        final LinearLayout mTwoLineLayout;
         final ImageView mStatusIcon;
         final CheckBox mCheckBox;
         private String mDeviceId;
diff --git a/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputBaseDialog.java b/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputBaseDialog.java
index 355c69f..a8141c0 100644
--- a/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputBaseDialog.java
+++ b/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputBaseDialog.java
@@ -21,7 +21,6 @@
 
 import android.app.WallpaperColors;
 import android.content.Context;
-import android.content.Intent;
 import android.content.res.Configuration;
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
@@ -55,6 +54,7 @@
 import androidx.recyclerview.widget.RecyclerView;
 
 import com.android.systemui.R;
+import com.android.systemui.broadcast.BroadcastSender;
 import com.android.systemui.statusbar.phone.SystemUIDialog;
 
 /**
@@ -71,6 +71,7 @@
 
     final Context mContext;
     final MediaOutputController mMediaOutputController;
+    final BroadcastSender mBroadcastSender;
 
     @VisibleForTesting
     View mDialogView;
@@ -98,11 +99,13 @@
         }
     };
 
-    public MediaOutputBaseDialog(Context context, MediaOutputController mediaOutputController) {
+    public MediaOutputBaseDialog(Context context, BroadcastSender broadcastSender,
+            MediaOutputController mediaOutputController) {
         super(context, R.style.Theme_SystemUI_Dialog_Media);
 
         // Save the context that is wrapped with our theme.
         mContext = getContext();
+        mBroadcastSender = broadcastSender;
         mMediaOutputController = mediaOutputController;
         mLayoutManager = new LinearLayoutManager(mContext);
         mListMaxHeight = context.getResources().getDimensionPixelSize(
@@ -152,7 +155,7 @@
             dismiss();
         });
         mAppButton.setOnClickListener(v -> {
-            mContext.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
+            mBroadcastSender.closeSystemDialogs();
             if (mMediaOutputController.getAppLaunchIntent() != null) {
                 mContext.startActivity(mMediaOutputController.getAppLaunchIntent());
             }
diff --git a/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputController.java b/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputController.java
index 0c202e0..0b6c68d 100644
--- a/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputController.java
+++ b/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputController.java
@@ -53,7 +53,6 @@
 import androidx.mediarouter.media.MediaRouter;
 import androidx.mediarouter.media.MediaRouterParams;
 
-import com.android.internal.logging.UiEventLogger;
 import com.android.settingslib.RestrictedLockUtilsInternal;
 import com.android.settingslib.Utils;
 import com.android.settingslib.bluetooth.BluetoothUtils;
@@ -70,7 +69,6 @@
 import com.android.systemui.plugins.ActivityStarter;
 import com.android.systemui.statusbar.notification.collection.NotificationEntry;
 import com.android.systemui.statusbar.notification.collection.notifcollection.CommonNotifCollection;
-import com.android.systemui.statusbar.phone.ShadeController;
 
 import java.util.ArrayList;
 import java.util.Collection;
@@ -95,12 +93,9 @@
     private final String mPackageName;
     private final Context mContext;
     private final MediaSessionManager mMediaSessionManager;
-    private final LocalBluetoothManager mLocalBluetoothManager;
-    private final ShadeController mShadeController;
     private final ActivityStarter mActivityStarter;
     private final DialogLaunchAnimator mDialogLaunchAnimator;
     private final List<MediaDevice> mGroupMediaDevices = new CopyOnWriteArrayList<>();
-    private final boolean mAboveStatusbar;
     private final CommonNotifCollection mNotifCollection;
     @VisibleForTesting
     final List<MediaDevice> mMediaDevices = new CopyOnWriteArrayList<>();
@@ -114,7 +109,6 @@
     LocalMediaManager mLocalMediaManager;
 
     private MediaOutputMetricLogger mMetricLogger;
-    private UiEventLogger mUiEventLogger;
 
     private int mColorActiveItem;
     private int mColorInactiveItem;
@@ -124,23 +118,19 @@
 
     @Inject
     public MediaOutputController(@NonNull Context context, String packageName,
-            boolean aboveStatusbar, MediaSessionManager mediaSessionManager, LocalBluetoothManager
-            lbm, ShadeController shadeController, ActivityStarter starter,
-            CommonNotifCollection notifCollection, UiEventLogger uiEventLogger,
+            MediaSessionManager mediaSessionManager, LocalBluetoothManager
+            lbm, ActivityStarter starter,
+            CommonNotifCollection notifCollection,
             DialogLaunchAnimator dialogLaunchAnimator,
             Optional<NearbyMediaDevicesManager> nearbyMediaDevicesManagerOptional) {
         mContext = context;
         mPackageName = packageName;
         mMediaSessionManager = mediaSessionManager;
-        mLocalBluetoothManager = lbm;
-        mShadeController = shadeController;
         mActivityStarter = starter;
-        mAboveStatusbar = aboveStatusbar;
         mNotifCollection = notifCollection;
         InfoMediaManager imm = new InfoMediaManager(mContext, packageName, null, lbm);
         mLocalMediaManager = new LocalMediaManager(mContext, lbm, imm, packageName);
         mMetricLogger = new MediaOutputMetricLogger(mContext, mPackageName);
-        mUiEventLogger = uiEventLogger;
         mDialogLaunchAnimator = dialogLaunchAnimator;
         mNearbyMediaDevicesManager = nearbyMediaDevicesManagerOptional.orElse(null);
         mColorActiveItem = Utils.getColorStateListDefaultColor(mContext,
@@ -630,18 +620,6 @@
         mActivityStarter.startActivity(launchIntent, true, controller);
     }
 
-    void launchMediaOutputGroupDialog(View mediaOutputDialog) {
-        // We show the output group dialog from the output dialog.
-        MediaOutputController controller = new MediaOutputController(mContext, mPackageName,
-                mAboveStatusbar, mMediaSessionManager, mLocalBluetoothManager, mShadeController,
-                mActivityStarter, mNotifCollection, mUiEventLogger, mDialogLaunchAnimator,
-                Optional.of(mNearbyMediaDevicesManager));
-
-        MediaOutputGroupDialog dialog = new MediaOutputGroupDialog(mContext, mAboveStatusbar,
-                controller);
-        mDialogLaunchAnimator.showFromView(dialog, mediaOutputDialog);
-    }
-
     boolean isActiveRemoteDevice(@NonNull MediaDevice device) {
         final List<String> features = device.getFeatures();
         return (features.contains(MediaRoute2Info.FEATURE_REMOTE_PLAYBACK)
diff --git a/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputDialog.java b/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputDialog.java
index 7696a1f..7834ec0 100644
--- a/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputDialog.java
+++ b/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputDialog.java
@@ -28,6 +28,7 @@
 import com.android.internal.logging.UiEvent;
 import com.android.internal.logging.UiEventLogger;
 import com.android.systemui.R;
+import com.android.systemui.broadcast.BroadcastSender;
 import com.android.systemui.dagger.SysUISingleton;
 
 /**
@@ -37,9 +38,9 @@
 public class MediaOutputDialog extends MediaOutputBaseDialog {
     final UiEventLogger mUiEventLogger;
 
-    MediaOutputDialog(Context context, boolean aboveStatusbar, MediaOutputController
-            mediaOutputController, UiEventLogger uiEventLogger) {
-        super(context, mediaOutputController);
+    MediaOutputDialog(Context context, boolean aboveStatusbar, BroadcastSender broadcastSender,
+            MediaOutputController mediaOutputController, UiEventLogger uiEventLogger) {
+        super(context, broadcastSender, mediaOutputController);
         mUiEventLogger = uiEventLogger;
         mAdapter = new MediaOutputAdapter(mMediaOutputController, this);
         if (!aboveStatusbar) {
diff --git a/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputDialogFactory.kt b/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputDialogFactory.kt
index a7e5480..0d7d60a 100644
--- a/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputDialogFactory.kt
+++ b/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputDialogFactory.kt
@@ -22,10 +22,10 @@
 import com.android.internal.logging.UiEventLogger
 import com.android.settingslib.bluetooth.LocalBluetoothManager
 import com.android.systemui.animation.DialogLaunchAnimator
+import com.android.systemui.broadcast.BroadcastSender
 import com.android.systemui.media.nearby.NearbyMediaDevicesManager
 import com.android.systemui.plugins.ActivityStarter
 import com.android.systemui.statusbar.notification.collection.notifcollection.CommonNotifCollection
-import com.android.systemui.statusbar.phone.ShadeController
 import java.util.Optional
 import javax.inject.Inject
 
@@ -36,8 +36,8 @@
     private val context: Context,
     private val mediaSessionManager: MediaSessionManager,
     private val lbm: LocalBluetoothManager?,
-    private val shadeController: ShadeController,
     private val starter: ActivityStarter,
+    private val broadcastSender: BroadcastSender,
     private val notifCollection: CommonNotifCollection,
     private val uiEventLogger: UiEventLogger,
     private val dialogLaunchAnimator: DialogLaunchAnimator,
@@ -52,10 +52,11 @@
         // Dismiss the previous dialog, if any.
         mediaOutputDialog?.dismiss()
 
-        val controller = MediaOutputController(context, packageName, aboveStatusBar,
-                mediaSessionManager, lbm, shadeController, starter, notifCollection,
-                uiEventLogger, dialogLaunchAnimator, nearbyMediaDevicesManagerOptional)
-        val dialog = MediaOutputDialog(context, aboveStatusBar, controller, uiEventLogger)
+        val controller = MediaOutputController(context, packageName,
+                mediaSessionManager, lbm, starter, notifCollection,
+                dialogLaunchAnimator, nearbyMediaDevicesManagerOptional)
+        val dialog =
+            MediaOutputDialog(context, aboveStatusBar, broadcastSender, controller, uiEventLogger)
         mediaOutputDialog = dialog
 
         // Show the dialog.
diff --git a/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputGroupDialog.java b/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputGroupDialog.java
index f1c6601..bb3f969 100644
--- a/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputGroupDialog.java
+++ b/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputGroupDialog.java
@@ -25,6 +25,7 @@
 import androidx.core.graphics.drawable.IconCompat;
 
 import com.android.systemui.R;
+import com.android.systemui.broadcast.BroadcastSender;
 
 /**
  * Dialog for media output group.
@@ -32,9 +33,9 @@
 // TODO(b/203073091): Remove this class once group logic been implemented.
 public class MediaOutputGroupDialog extends MediaOutputBaseDialog {
 
-    MediaOutputGroupDialog(Context context, boolean aboveStatusbar, MediaOutputController
-            mediaOutputController) {
-        super(context, mediaOutputController);
+    MediaOutputGroupDialog(Context context, boolean aboveStatusbar, BroadcastSender broadcastSender,
+            MediaOutputController mediaOutputController) {
+        super(context, broadcastSender, mediaOutputController);
         mMediaOutputController.resetGroupMediaDevices();
         mAdapter = new MediaOutputGroupAdapter(mMediaOutputController);
         if (!aboveStatusbar) {
diff --git a/packages/SystemUI/src/com/android/systemui/power/PowerNotificationWarnings.java b/packages/SystemUI/src/com/android/systemui/power/PowerNotificationWarnings.java
index b4e20fd..2ac34b2 100644
--- a/packages/SystemUI/src/com/android/systemui/power/PowerNotificationWarnings.java
+++ b/packages/SystemUI/src/com/android/systemui/power/PowerNotificationWarnings.java
@@ -60,6 +60,7 @@
 import com.android.settingslib.utils.PowerUtil;
 import com.android.systemui.R;
 import com.android.systemui.SystemUIApplication;
+import com.android.systemui.broadcast.BroadcastSender;
 import com.android.systemui.dagger.SysUISingleton;
 import com.android.systemui.plugins.ActivityStarter;
 import com.android.systemui.statusbar.phone.SystemUIDialog;
@@ -160,17 +161,20 @@
     @VisibleForTesting SystemUIDialog mUsbHighTempDialog;
     private BatteryStateSnapshot mCurrentBatterySnapshot;
     private ActivityStarter mActivityStarter;
+    private final BroadcastSender mBroadcastSender;
 
     /**
      */
     @Inject
-    public PowerNotificationWarnings(Context context, ActivityStarter activityStarter) {
+    public PowerNotificationWarnings(Context context, ActivityStarter activityStarter,
+            BroadcastSender broadcastSender) {
         mContext = context;
         mNoMan = mContext.getSystemService(NotificationManager.class);
         mPowerMan = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
         mKeyguard = mContext.getSystemService(KeyguardManager.class);
         mReceiver.init();
         mActivityStarter = activityStarter;
+        mBroadcastSender = broadcastSender;
         mUseSevereDialog = mContext.getResources().getBoolean(R.bool.config_severe_battery_dialog);
     }
 
@@ -258,7 +262,7 @@
 
     protected void showWarningNotification() {
         if (showSevereLowBatteryDialog()) {
-            mContext.sendBroadcast(new Intent(ACTION_ENABLE_SEVERE_BATTERY_DIALOG)
+            mBroadcastSender.sendBroadcast(new Intent(ACTION_ENABLE_SEVERE_BATTERY_DIALOG)
                     .setPackage(mContext.getPackageName())
                     .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
                             | Intent.FLAG_RECEIVER_FOREGROUND));
@@ -716,9 +720,9 @@
                         mSaverConfirmation.dismiss();
                     }
                     // Also close the notification shade, if it's open.
-                    mContext.sendBroadcast(
+                    mBroadcastSender.sendBroadcast(
                             new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)
-                            .setFlags(Intent.FLAG_RECEIVER_FOREGROUND));
+                                    .setFlags(Intent.FLAG_RECEIVER_FOREGROUND));
 
                     final Uri uri = Uri.parse(getURL());
                     Context context = widget.getContext();
diff --git a/packages/SystemUI/src/com/android/systemui/privacy/OngoingPrivacyChip.kt b/packages/SystemUI/src/com/android/systemui/privacy/OngoingPrivacyChip.kt
index 9cd97ff8..2a6ca1a 100644
--- a/packages/SystemUI/src/com/android/systemui/privacy/OngoingPrivacyChip.kt
+++ b/packages/SystemUI/src/com/android/systemui/privacy/OngoingPrivacyChip.kt
@@ -22,13 +22,14 @@
 import android.widget.LinearLayout
 import com.android.settingslib.Utils
 import com.android.systemui.R
+import com.android.systemui.statusbar.events.BackgroundAnimatableView
 
 class OngoingPrivacyChip @JvmOverloads constructor(
     context: Context,
     attrs: AttributeSet? = null,
     defStyleAttrs: Int = 0,
     defStyleRes: Int = 0
-) : FrameLayout(context, attrs, defStyleAttrs, defStyleRes) {
+) : FrameLayout(context, attrs, defStyleAttrs, defStyleRes), BackgroundAnimatableView {
 
     private var iconMargin = 0
     private var iconSize = 0
@@ -50,6 +51,16 @@
         updateResources()
     }
 
+    /**
+     * When animating as a chip in the status bar, we want to animate the width for the container
+     * of the privacy items. We have to subtract our own top and left offset because the bounds
+     * come to us as absolute on-screen bounds, and `iconsContainer` is laid out relative to the
+     * frame layout's bounds.
+     */
+    override fun setBoundsForAnimation(l: Int, t: Int, r: Int, b: Int) {
+        iconsContainer.setLeftTopRightBottom(l - left, t - top, r - left, b - top)
+    }
+
     // Should only be called if the builder icons or app changed
     private fun updateView(builder: PrivacyChipBuilder) {
         fun setIcons(chipBuilder: PrivacyChipBuilder, iconsContainer: ViewGroup) {
diff --git a/packages/SystemUI/src/com/android/systemui/qrcodescanner/controller/QRCodeScannerController.java b/packages/SystemUI/src/com/android/systemui/qrcodescanner/controller/QRCodeScannerController.java
index e26c768..8000bdc 100644
--- a/packages/SystemUI/src/com/android/systemui/qrcodescanner/controller/QRCodeScannerController.java
+++ b/packages/SystemUI/src/com/android/systemui/qrcodescanner/controller/QRCodeScannerController.java
@@ -160,14 +160,15 @@
      * Returns true if lock screen entry point for QR Code Scanner is to be enabled.
      */
     public boolean isEnabledForLockScreenButton() {
-        return mQRCodeScannerEnabled && mIntent != null && mConfigEnableLockScreenButton;
+        return mQRCodeScannerEnabled && mIntent != null && mConfigEnableLockScreenButton
+                && isActivityCallable(mIntent);
     }
 
     /**
      * Returns true if quick settings entry point for QR Code Scanner is to be enabled.
      */
     public boolean isEnabledForQuickSettings() {
-        return mIntent != null;
+        return mIntent != null && isActivityCallable(mIntent);
     }
 
     /**
@@ -278,7 +279,7 @@
             intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
         }
 
-        if (isActivityCallable(intent)) {
+        if (isActivityAvailable(intent)) {
             mQRCodeScannerActivity = qrCodeScannerActivity;
             mComponentName = componentName;
             mIntent = intent;
@@ -293,7 +294,7 @@
         }
     }
 
-    private boolean isActivityCallable(Intent intent) {
+    private boolean isActivityAvailable(Intent intent) {
         // Our intent should always be explicit and should have a component set
         if (intent.getComponent() == null) return false;
 
@@ -307,6 +308,17 @@
                 flags).isEmpty();
     }
 
+    private boolean isActivityCallable(Intent intent) {
+        // Our intent should always be explicit and should have a component set
+        if (intent.getComponent() == null) return false;
+
+        int flags = PackageManager.MATCH_DIRECT_BOOT_AWARE
+                | PackageManager.MATCH_DIRECT_BOOT_UNAWARE
+                | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS;
+        return !mContext.getPackageManager().queryIntentActivities(intent,
+                flags).isEmpty();
+    }
+
     private void unregisterUserChangeObservers() {
         mUserTracker.removeCallback(mUserChangedListener);
 
diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSAnimator.java b/packages/SystemUI/src/com/android/systemui/qs/QSAnimator.java
index d1b569f..4640205 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/QSAnimator.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/QSAnimator.java
@@ -241,7 +241,13 @@
 
     private void addNonFirstPageAnimators(int page) {
         Pair<HeightExpansionAnimator, TouchAnimator> pair = createSecondaryPageAnimators(page);
-        mNonFirstPageQSAnimators.put(page, pair);
+        if (pair != null) {
+            // pair is null in one of two cases:
+            // * mPagedTileLayout is null, meaning we are still setting up.
+            // * the page has no tiles
+            // In either case, don't add the animators to the map.
+            mNonFirstPageQSAnimators.put(page, pair);
+        }
     }
 
     @Override
@@ -518,6 +524,13 @@
         SideLabelTileLayout qqsLayout = (SideLabelTileLayout) mQuickQsPanel.getTileLayout();
         View view = mQs.getView();
         List<String> specs = mPagedLayout.getSpecsForPage(page);
+        if (specs.isEmpty()) {
+            // specs should not be empty in a valid secondary page, as we scrolled to it.
+            // We may crash later on because there's a null animator.
+            specs = mQsPanelController.getHost().mTileSpecs;
+            Log.e(TAG, "Trying to create animators for empty page " + page + ". Tiles: " + specs);
+            // return null;
+        }
 
         int row = -1;
         int lastTileTop = -1;
diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSFragment.java b/packages/SystemUI/src/com/android/systemui/qs/QSFragment.java
index 3ef7220..fe8c309 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/QSFragment.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/QSFragment.java
@@ -573,6 +573,7 @@
         if (mQSAnimator != null) {
             mQSAnimator.setPosition(expansion);
         }
+        mQqsMediaHost.setSquishFraction(mSquishinessFraction);
         updateMediaPositions();
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/qs/QuickStatusBarHeader.java b/packages/SystemUI/src/com/android/systemui/qs/QuickStatusBarHeader.java
index 8c08873..f2dd770 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/QuickStatusBarHeader.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/QuickStatusBarHeader.java
@@ -102,7 +102,7 @@
     private int mTopViewMeasureHeight;
 
     @NonNull
-    private List<String> mRssiIgnoredSlots;
+    private List<String> mRssiIgnoredSlots = List.of();
     private boolean mIsSingleCarrier;
 
     private boolean mHasCenterCutout;
diff --git a/packages/SystemUI/src/com/android/systemui/qs/external/TileServices.java b/packages/SystemUI/src/com/android/systemui/qs/external/TileServices.java
index 32515a2..4cacbba 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/external/TileServices.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/external/TileServices.java
@@ -15,26 +15,22 @@
  */
 package com.android.systemui.qs.external;
 
-import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.Context;
-import android.content.Intent;
-import android.content.IntentFilter;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
 import android.graphics.drawable.Icon;
 import android.os.Binder;
 import android.os.Handler;
 import android.os.IBinder;
-import android.os.Looper;
 import android.os.RemoteException;
 import android.os.UserHandle;
 import android.service.quicksettings.IQSService;
 import android.service.quicksettings.Tile;
-import android.service.quicksettings.TileService;
 import android.util.ArrayMap;
 import android.util.Log;
 
+import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
 
 import com.android.internal.statusbar.StatusBarIcon;
@@ -42,6 +38,7 @@
 import com.android.systemui.dagger.qualifiers.Main;
 import com.android.systemui.qs.QSTileHost;
 import com.android.systemui.settings.UserTracker;
+import com.android.systemui.statusbar.CommandQueue;
 import com.android.systemui.statusbar.phone.StatusBarIconController;
 import com.android.systemui.statusbar.policy.KeyguardStateController;
 
@@ -51,6 +48,7 @@
 import java.util.Objects;
 
 import javax.inject.Inject;
+import javax.inject.Provider;
 
 /**
  * Runs the day-to-day operations of which tiles should be bound and when.
@@ -64,11 +62,12 @@
     private final ArrayMap<ComponentName, CustomTile> mTiles = new ArrayMap<>();
     private final ArrayMap<IBinder, CustomTile> mTokenMap = new ArrayMap<>();
     private final Context mContext;
-    private final Handler mHandler;
     private final Handler mMainHandler;
+    private final Provider<Handler> mHandlerProvider;
     private final QSTileHost mHost;
     private final KeyguardStateController mKeyguardStateController;
     private final BroadcastDispatcher mBroadcastDispatcher;
+    private final CommandQueue mCommandQueue;
     private final UserTracker mUserTracker;
 
     private int mMaxBound = DEFAULT_MAX_BOUND;
@@ -76,23 +75,20 @@
     @Inject
     public TileServices(
             QSTileHost host,
-            @Main Looper looper,
+            @Main Provider<Handler> handlerProvider,
             BroadcastDispatcher broadcastDispatcher,
             UserTracker userTracker,
-            KeyguardStateController keyguardStateController) {
+            KeyguardStateController keyguardStateController,
+            CommandQueue commandQueue) {
         mHost = host;
         mKeyguardStateController = keyguardStateController;
         mContext = mHost.getContext();
         mBroadcastDispatcher = broadcastDispatcher;
-        mHandler = new Handler(looper);
-        mMainHandler = new Handler(Looper.getMainLooper());
+        mHandlerProvider = handlerProvider;
+        mMainHandler = mHandlerProvider.get();
         mUserTracker = userTracker;
-        mBroadcastDispatcher.registerReceiver(
-                mRequestListeningReceiver,
-                new IntentFilter(TileService.ACTION_REQUEST_LISTENING),
-                null, // Use the default Executor
-                UserHandle.ALL
-        );
+        mCommandQueue = commandQueue;
+        mCommandQueue.addCallback(mRequestListeningCallback);
     }
 
     public Context getContext() {
@@ -118,7 +114,7 @@
 
     protected TileServiceManager onCreateTileService(ComponentName component,
             BroadcastDispatcher broadcastDispatcher) {
-        return new TileServiceManager(this, mHandler, component,
+        return new TileServiceManager(this, mHandlerProvider.get(), component,
                 broadcastDispatcher, mUserTracker);
     }
 
@@ -354,21 +350,14 @@
     public void destroy() {
         synchronized (mServices) {
             mServices.values().forEach(service -> service.handleDestroy());
-            mBroadcastDispatcher.unregisterReceiver(mRequestListeningReceiver);
         }
+        mCommandQueue.removeCallback(mRequestListeningCallback);
     }
 
-    private final BroadcastReceiver mRequestListeningReceiver = new BroadcastReceiver() {
+    private final CommandQueue.Callbacks mRequestListeningCallback = new CommandQueue.Callbacks() {
         @Override
-        public void onReceive(Context context, Intent intent) {
-            if (TileService.ACTION_REQUEST_LISTENING.equals(intent.getAction())) {
-                try {
-                    ComponentName c = intent.getParcelableExtra(Intent.EXTRA_COMPONENT_NAME);
-                    requestListening(c);
-                } catch (ClassCastException ex) {
-                    Log.e(TAG, "Bad component name", ex);
-                }
-            }
+        public void requestTileServiceListeningState(@NonNull ComponentName componentName) {
+            mMainHandler.post(() -> requestListening(componentName));
         }
     };
 
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/QRCodeScannerTile.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/QRCodeScannerTile.java
index b658025..b415022 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/tiles/QRCodeScannerTile.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/QRCodeScannerTile.java
@@ -117,7 +117,6 @@
         state.icon = ResourceIcon.get(R.drawable.ic_qr_code_scanner);
         state.state = mQRCodeScannerController.isEnabledForQuickSettings() ? Tile.STATE_ACTIVE
                 : Tile.STATE_UNAVAILABLE;
-        state.secondaryLabel = mContext.getString(R.string.qr_code_scanner_description);
     }
 
     @Override
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/InternetAdapter.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/InternetAdapter.java
index e1d2070..8bad2de 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/InternetAdapter.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/InternetAdapter.java
@@ -47,9 +47,6 @@
 public class InternetAdapter extends RecyclerView.Adapter<InternetAdapter.InternetViewHolder> {
 
     private static final String TAG = "InternetAdapter";
-    private static final String ACTION_WIFI_DIALOG = "com.android.settings.WIFI_DIALOG";
-    private static final String EXTRA_CHOSEN_WIFI_ENTRY_KEY = "key_chosen_wifientry_key";
-    private static final String EXTRA_CONNECT_FOR_CALLER = "connect_for_caller";
 
     private final InternetDialogController mInternetDialogController;
     @Nullable
@@ -169,11 +166,10 @@
             }
             mWifiListLayout.setOnClickListener(v -> {
                 if (wifiEntry.shouldEditBeforeConnect()) {
-                    final Intent intent = new Intent(ACTION_WIFI_DIALOG);
+                    final Intent intent = WifiUtils.getWifiDialogIntent(wifiEntry.getKey(),
+                            true /* connectForCaller */);
                     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                     intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
-                    intent.putExtra(EXTRA_CHOSEN_WIFI_ENTRY_KEY, wifiEntry.getKey());
-                    intent.putExtra(EXTRA_CONNECT_FOR_CALLER, false);
                     mContext.startActivity(intent);
                 }
                 mInternetDialogController.connect(wifiEntry);
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/InternetDialogController.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/InternetDialogController.java
index b3bc3be..b322cbf 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/InternetDialogController.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/InternetDialogController.java
@@ -112,7 +112,6 @@
             "android.settings.NETWORK_PROVIDER_SETTINGS";
     private static final String ACTION_WIFI_SCANNING_SETTINGS =
             "android.settings.WIFI_SCANNING_SETTINGS";
-    private static final String EXTRA_CHOSEN_WIFI_ENTRY_KEY = "key_chosen_wifientry_key";
     public static final Drawable EMPTY_DRAWABLE = new ColorDrawable(Color.TRANSPARENT);
     public static final int NO_CELL_DATA_TYPE_ICON = 0;
     private static final int SUBTITLE_TEXT_WIFI_IS_OFF = R.string.wifi_is_off;
@@ -853,8 +852,8 @@
             }
 
             if (status == WifiEntry.ConnectCallback.CONNECT_STATUS_FAILURE_NO_CONFIG) {
-                final Intent intent = new Intent("com.android.settings.WIFI_DIALOG")
-                        .putExtra(EXTRA_CHOSEN_WIFI_ENTRY_KEY, mWifiEntry.getKey());
+                final Intent intent = WifiUtils.getWifiDialogIntent(mWifiEntry.getKey(),
+                        true /* connectForCaller */);
                 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                 mActivityStarter.startActivity(intent, false /* dismissShade */);
             } else if (status == CONNECT_STATUS_FAILURE_UNKNOWN) {
diff --git a/packages/SystemUI/src/com/android/systemui/screenshot/DraggableConstraintLayout.java b/packages/SystemUI/src/com/android/systemui/screenshot/DraggableConstraintLayout.java
new file mode 100644
index 0000000..0b98767
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/screenshot/DraggableConstraintLayout.java
@@ -0,0 +1,354 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.screenshot;
+
+import static com.android.systemui.screenshot.LogConfig.DEBUG_ANIM;
+import static com.android.systemui.screenshot.LogConfig.DEBUG_DISMISS;
+
+import android.animation.Animator;
+import android.animation.AnimatorListenerAdapter;
+import android.animation.ValueAnimator;
+import android.content.Context;
+import android.graphics.Rect;
+import android.graphics.Region;
+import android.util.AttributeSet;
+import android.util.DisplayMetrics;
+import android.util.Log;
+import android.util.MathUtils;
+import android.view.GestureDetector;
+import android.view.MotionEvent;
+import android.view.View;
+import android.view.ViewTreeObserver;
+
+import androidx.constraintlayout.widget.ConstraintLayout;
+
+import com.android.systemui.R;
+
+/**
+ * ConstraintLayout that is draggable when touched in a specific region
+ */
+public class DraggableConstraintLayout extends ConstraintLayout
+        implements ViewTreeObserver.OnComputeInternalInsetsListener {
+
+    private final SwipeDismissHandler mSwipeDismissHandler;
+    private final GestureDetector mSwipeDetector;
+    private View mActionsContainer;
+    private SwipeDismissCallbacks mCallbacks;
+
+    /**
+     * Stores the callbacks when the view is interacted with or dismissed.
+     */
+    public interface SwipeDismissCallbacks {
+        /**
+         * Run when the view is interacted with (touched)
+         */
+        default void onInteraction() {
+
+        }
+
+        /**
+         * Run when the view is dismissed (the distance threshold is met), pre-dismissal animation
+         */
+        default void onSwipeDismissInitiated(Animator animator) {
+
+        }
+
+        /**
+         * Run when the view is dismissed (the distance threshold is met), post-dismissal animation
+         */
+        default void onDismissComplete() {
+
+        }
+    }
+
+    public DraggableConstraintLayout(Context context) {
+        this(context, null);
+    }
+
+    public DraggableConstraintLayout(Context context, AttributeSet attrs) {
+        this(context, attrs, 0);
+    }
+
+    public DraggableConstraintLayout(Context context, AttributeSet attrs, int defStyleAttr) {
+        super(context, attrs, defStyleAttr);
+
+        mSwipeDismissHandler = new SwipeDismissHandler(mContext, this);
+        setOnTouchListener(mSwipeDismissHandler);
+
+        mSwipeDetector = new GestureDetector(mContext,
+                new GestureDetector.SimpleOnGestureListener() {
+                    final Rect mActionsRect = new Rect();
+
+                    @Override
+                    public boolean onScroll(
+                            MotionEvent ev1, MotionEvent ev2, float distanceX, float distanceY) {
+                        mActionsContainer.getBoundsOnScreen(mActionsRect);
+                        // return true if we aren't in the actions bar, or if we are but it isn't
+                        // scrollable in the direction of movement
+                        return !mActionsRect.contains((int) ev2.getRawX(), (int) ev2.getRawY())
+                                || !mActionsContainer.canScrollHorizontally((int) distanceX);
+                    }
+                });
+        mSwipeDetector.setIsLongpressEnabled(false);
+    }
+
+    public void setCallbacks(SwipeDismissCallbacks callbacks) {
+        mCallbacks = callbacks;
+    }
+
+    @Override // View
+    protected void onFinishInflate() {
+        mActionsContainer = findViewById(R.id.actions_container_background);
+    }
+
+    @Override
+    public boolean onInterceptTouchEvent(MotionEvent ev) {
+        if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
+            mSwipeDismissHandler.onTouch(this, ev);
+        }
+        return mSwipeDetector.onTouchEvent(ev);
+    }
+
+    public int getVisibleRight() {
+        return mActionsContainer.getRight();
+    }
+
+    /**
+     * Cancel current dismissal animation, if any
+     */
+    public void cancelDismissal() {
+        mSwipeDismissHandler.cancel();
+    }
+
+    /**
+     * Return whether the view is currently dismissing
+     */
+    public boolean isDismissing() {
+        return mSwipeDismissHandler.isDismissing();
+    }
+
+    /**
+     * Dismiss the view, with animation controlled by SwipeDismissHandler
+     */
+    public void dismiss() {
+        mSwipeDismissHandler.dismiss();
+    }
+
+
+    @Override
+    protected void onAttachedToWindow() {
+        super.onAttachedToWindow();
+        getViewTreeObserver().addOnComputeInternalInsetsListener(this);
+    }
+
+    @Override
+    protected void onDetachedFromWindow() {
+        super.onDetachedFromWindow();
+        getViewTreeObserver().removeOnComputeInternalInsetsListener(this);
+    }
+
+    @Override
+    public void onComputeInternalInsets(ViewTreeObserver.InternalInsetsInfo inoutInfo) {
+        // Only child views are touchable.
+        Region r = new Region();
+        Rect rect = new Rect();
+        for (int i = 0; i < getChildCount(); i++) {
+            getChildAt(i).getGlobalVisibleRect(rect);
+            r.op(rect, Region.Op.UNION);
+        }
+        inoutInfo.setTouchableInsets(ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_REGION);
+        inoutInfo.touchableRegion.set(r);
+    }
+
+    /**
+     * Allows a view to be swipe-dismissed, or returned to its location if distance threshold is not
+     * met
+     */
+    private class SwipeDismissHandler implements OnTouchListener {
+        private static final String TAG = "SwipeDismissHandler";
+
+        // distance needed to register a dismissal
+        private static final float DISMISS_DISTANCE_THRESHOLD_DP = 20;
+
+        private final DraggableConstraintLayout mView;
+        private final GestureDetector mGestureDetector;
+        private final DisplayMetrics mDisplayMetrics;
+        private ValueAnimator mDismissAnimation;
+
+        private float mStartX;
+        // Keeps track of the most recent direction (between the last two move events).
+        // -1 for left; +1 for right.
+        private int mDirectionX;
+        private float mPreviousX;
+
+        SwipeDismissHandler(Context context, DraggableConstraintLayout view) {
+            mView = view;
+            GestureDetector.OnGestureListener gestureListener = new SwipeDismissGestureListener();
+            mGestureDetector = new GestureDetector(context, gestureListener);
+            mDisplayMetrics = new DisplayMetrics();
+            context.getDisplay().getRealMetrics(mDisplayMetrics);
+            mCallbacks = new SwipeDismissCallbacks() {
+            }; // default to unimplemented callbacks
+        }
+
+        @Override
+        public boolean onTouch(View view, MotionEvent event) {
+            boolean gestureResult = mGestureDetector.onTouchEvent(event);
+            mCallbacks.onInteraction();
+            if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
+                mStartX = event.getRawX();
+                mPreviousX = mStartX;
+                return true;
+            } else if (event.getActionMasked() == MotionEvent.ACTION_UP) {
+                if (mDismissAnimation != null && mDismissAnimation.isRunning()) {
+                    return true;
+                }
+                if (isPastDismissThreshold()) {
+                    dismiss();
+                } else {
+                    // if we've moved, but not past the threshold, start the return animation
+                    if (DEBUG_DISMISS) {
+                        Log.d(TAG, "swipe gesture abandoned");
+                    }
+                    createSwipeReturnAnimation().start();
+                }
+                return true;
+            }
+            return gestureResult;
+        }
+
+        class SwipeDismissGestureListener extends GestureDetector.SimpleOnGestureListener {
+            @Override
+            public boolean onScroll(
+                    MotionEvent ev1, MotionEvent ev2, float distanceX, float distanceY) {
+                mView.setTranslationX(ev2.getRawX() - mStartX);
+                mDirectionX = (ev2.getRawX() < mPreviousX) ? -1 : 1;
+                mPreviousX = ev2.getRawX();
+                return true;
+            }
+
+            @Override
+            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
+                    float velocityY) {
+                if (mView.getTranslationX() * velocityX > 0
+                        && (mDismissAnimation == null || !mDismissAnimation.isRunning())) {
+                    ValueAnimator dismissAnimator =
+                            createSwipeDismissAnimation(velocityX / (float) 1000);
+                    mCallbacks.onSwipeDismissInitiated(dismissAnimator);
+                    dismiss(dismissAnimator);
+                    return true;
+                }
+                return false;
+            }
+        }
+
+        private boolean isPastDismissThreshold() {
+            float translationX = mView.getTranslationX();
+            // Determines whether the absolute translation from the start is in the same direction
+            // as the current movement. For example, if the user moves most of the way to the right,
+            // but then starts dragging back left, we do not dismiss even though the absolute
+            // distance is greater than the threshold.
+            if (translationX * mDirectionX > 0) {
+                return Math.abs(translationX) >= FloatingWindowUtil.dpToPx(mDisplayMetrics,
+                        DISMISS_DISTANCE_THRESHOLD_DP);
+            }
+            return false;
+        }
+
+        boolean isDismissing() {
+            return (mDismissAnimation != null && mDismissAnimation.isRunning());
+        }
+
+        void cancel() {
+            if (isDismissing()) {
+                if (DEBUG_ANIM) {
+                    Log.d(TAG, "cancelling dismiss animation");
+                }
+                mDismissAnimation.cancel();
+            }
+        }
+
+        void dismiss() {
+            ValueAnimator anim = createSwipeDismissAnimation(3);
+            mCallbacks.onSwipeDismissInitiated(anim);
+            dismiss(anim);
+        }
+
+        private void dismiss(ValueAnimator animator) {
+            mDismissAnimation = animator;
+            mDismissAnimation.addListener(new AnimatorListenerAdapter() {
+                private boolean mCancelled;
+
+                @Override
+                public void onAnimationCancel(Animator animation) {
+                    super.onAnimationCancel(animation);
+                    mCancelled = true;
+                }
+
+                @Override
+                public void onAnimationEnd(Animator animation) {
+                    super.onAnimationEnd(animation);
+                    if (!mCancelled) {
+                        mCallbacks.onDismissComplete();
+                    }
+                }
+            });
+            mDismissAnimation.start();
+        }
+
+        private ValueAnimator createSwipeDismissAnimation(float velocity) {
+            // velocity is measured in pixels per millisecond
+            velocity = Math.min(3, Math.max(1, velocity));
+            ValueAnimator anim = ValueAnimator.ofFloat(0, 1);
+            float startX = mView.getTranslationX();
+            // make sure the UI gets all the way off the screen in the direction of movement
+            // (the actions container background is guaranteed to be both the leftmost and
+            // rightmost UI element in LTR and RTL)
+            float finalX;
+            int layoutDir =
+                    mView.getContext().getResources().getConfiguration().getLayoutDirection();
+            if (startX > 0 || (startX == 0 && layoutDir == LAYOUT_DIRECTION_RTL)) {
+                finalX = mDisplayMetrics.widthPixels;
+            } else {
+                finalX = -1 * mActionsContainer.getRight();
+            }
+            float distance = Math.abs(finalX - startX);
+
+            anim.addUpdateListener(animation -> {
+                float translation = MathUtils.lerp(startX, finalX, animation.getAnimatedFraction());
+                mView.setTranslationX(translation);
+                mView.setAlpha(1 - animation.getAnimatedFraction());
+            });
+            anim.setDuration((long) (distance / Math.abs(velocity)));
+            return anim;
+        }
+
+        private ValueAnimator createSwipeReturnAnimation() {
+            ValueAnimator anim = ValueAnimator.ofFloat(0, 1);
+            float startX = mView.getTranslationX();
+            float finalX = 0;
+
+            anim.addUpdateListener(animation -> {
+                float translation = MathUtils.lerp(
+                        startX, finalX, animation.getAnimatedFraction());
+                mView.setTranslationX(translation);
+            });
+
+            return anim;
+        }
+    }
+}
diff --git a/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotController.java b/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotController.java
index 50765f2..009d4b9 100644
--- a/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotController.java
+++ b/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotController.java
@@ -88,10 +88,12 @@
 import com.android.internal.policy.PhoneWindow;
 import com.android.settingslib.applications.InterestingConfigChanges;
 import com.android.systemui.R;
+import com.android.systemui.broadcast.BroadcastSender;
 import com.android.systemui.clipboardoverlay.ClipboardOverlayController;
 import com.android.systemui.dagger.qualifiers.Main;
 import com.android.systemui.screenshot.ScreenshotController.SavedImageData.ActionTransition;
 import com.android.systemui.screenshot.TakeScreenshotService.RequestCallback;
+import com.android.systemui.util.Assert;
 
 import com.google.common.util.concurrent.ListenableFuture;
 
@@ -247,6 +249,7 @@
     private final ImageExporter mImageExporter;
     private final Executor mMainExecutor;
     private final ExecutorService mBgExecutor;
+    private final BroadcastSender mBroadcastSender;
 
     private final WindowManager mWindowManager;
     private final WindowManager.LayoutParams mWindowLayoutParams;
@@ -271,7 +274,6 @@
     private String mPackageName = "";
     private BroadcastReceiver mCopyBroadcastReceiver;
 
-
     /** Tracks config changes that require re-creating UI */
     private final InterestingConfigChanges mConfigChanges = new InterestingConfigChanges(
             ActivityInfo.CONFIG_ORIENTATION
@@ -293,7 +295,8 @@
             ScrollCaptureController scrollCaptureController,
             LongScreenshotData longScreenshotHolder,
             ActivityManager activityManager,
-            TimeoutHandler timeoutHandler) {
+            TimeoutHandler timeoutHandler,
+            BroadcastSender broadcastSender) {
         mScreenshotSmartActions = screenshotSmartActions;
         mNotificationsController = screenshotNotificationsController;
         mScrollCaptureClient = scrollCaptureClient;
@@ -304,6 +307,7 @@
         mLongScreenshotHolder = longScreenshotHolder;
         mIsLowRamDevice = activityManager.isLowRamDevice();
         mBgExecutor = Executors.newSingleThreadExecutor();
+        mBroadcastSender = broadcastSender;
 
         mScreenshotHandler = timeoutHandler;
         mScreenshotHandler.setDefaultTimeoutMillis(SCREENSHOT_CORNER_DEFAULT_TIMEOUT_MILLIS);
@@ -355,8 +359,10 @@
                 ClipboardOverlayController.SELF_PERMISSION, null, Context.RECEIVER_NOT_EXPORTED);
     }
 
+    @MainThread
     void takeScreenshotFullscreen(ComponentName topComponent, Consumer<Uri> finisher,
             RequestCallback requestCallback) {
+        Assert.isMainThread();
         mCurrentRequestCallback = requestCallback;
         DisplayMetrics displayMetrics = new DisplayMetrics();
         getDefaultDisplay().getRealMetrics(displayMetrics);
@@ -365,11 +371,12 @@
                 new Rect(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels));
     }
 
+    @MainThread
     void handleImageAsScreenshot(Bitmap screenshot, Rect screenshotScreenBounds,
             Insets visibleInsets, int taskId, int userId, ComponentName topComponent,
             Consumer<Uri> finisher, RequestCallback requestCallback) {
         // TODO: use task Id, userId, topComponent for smart handler
-
+        Assert.isMainThread();
         if (screenshot == null) {
             Log.e(TAG, "Got null bitmap from screenshot message");
             mNotificationsController.notifyScreenshotError(
@@ -392,8 +399,10 @@
     /**
      * Displays a screenshot selector
      */
+    @MainThread
     void takeScreenshotPartial(ComponentName topComponent,
             final Consumer<Uri> finisher, RequestCallback requestCallback) {
+        Assert.isMainThread();
         mScreenshotView.reset();
         mCurrentRequestCallback = requestCallback;
 
@@ -517,7 +526,7 @@
 
         saveScreenshot(screenshot, finisher, screenRect, Insets.NONE, topComponent, true);
 
-        mContext.sendBroadcast(new Intent(ClipboardOverlayController.SCREENSHOT_ACTION),
+        mBroadcastSender.sendBroadcast(new Intent(ClipboardOverlayController.SCREENSHOT_ACTION),
                 ClipboardOverlayController.SELF_PERMISSION);
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotView.java b/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotView.java
index 6d729b9..6af6e36 100644
--- a/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotView.java
+++ b/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotView.java
@@ -141,7 +141,7 @@
 
     private ScreenshotSelectorView mScreenshotSelectorView;
     private ImageView mScrollingScrim;
-    private View mScreenshotStatic;
+    private DraggableConstraintLayout mScreenshotStatic;
     private ImageView mScreenshotPreview;
     private View mScreenshotPreviewBorder;
     private ImageView mScrollablePreview;
@@ -159,7 +159,6 @@
     private UiEventLogger mUiEventLogger;
     private ScreenshotViewCallback mCallbacks;
     private boolean mPendingSharedTransition;
-    private SwipeDismissHandler mSwipeDismissHandler;
     private InputMonitorCompat mInputMonitor;
     private InputChannelCompat.InputEventReceiver mInputEventReceiver;
     private boolean mShowScrollablePreview;
@@ -332,19 +331,6 @@
         }
     }
 
-    @Override // ViewGroup
-    public boolean onInterceptTouchEvent(MotionEvent ev) {
-        // scrolling scrim should not be swipeable; return early if we're on the scrim
-        if (!getSwipeRegion().contains((int) ev.getRawX(), (int) ev.getRawY())) {
-            return false;
-        }
-        // always pass through the down event so the swipe handler knows the initial state
-        if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
-            mSwipeDismissHandler.onTouch(this, ev);
-        }
-        return mSwipeDetector.onTouchEvent(ev);
-    }
-
     @Override // View
     protected void onFinishInflate() {
         mScrollingScrim = requireNonNull(findViewById(R.id.screenshot_scrolling_scrim));
@@ -356,8 +342,8 @@
         mScreenshotPreview.setClipToOutline(true);
 
         mActionsContainerBackground = requireNonNull(findViewById(
-                R.id.screenshot_actions_container_background));
-        mActionsContainer = requireNonNull(findViewById(R.id.screenshot_actions_container));
+                R.id.actions_container_background));
+        mActionsContainer = requireNonNull(findViewById(R.id.actions_container));
         mActionsView = requireNonNull(findViewById(R.id.screenshot_actions));
         mBackgroundProtection = requireNonNull(
                 findViewById(R.id.screenshot_actions_background));
@@ -395,35 +381,34 @@
         setFocusableInTouchMode(true);
         requestFocus();
 
-        mSwipeDismissHandler = new SwipeDismissHandler(mContext, mScreenshotStatic,
-                new SwipeDismissHandler.SwipeDismissCallbacks() {
-                    @Override
-                    public void onInteraction() {
-                        mCallbacks.onUserInteraction();
-                    }
+        mScreenshotStatic.setCallbacks(new DraggableConstraintLayout.SwipeDismissCallbacks() {
+            @Override
+            public void onInteraction() {
+                mCallbacks.onUserInteraction();
+            }
 
+            @Override
+            public void onSwipeDismissInitiated(Animator animator) {
+                if (DEBUG_DISMISS) {
+                    Log.d(ScreenshotView.TAG, "dismiss triggered via swipe gesture");
+                }
+                mUiEventLogger.log(ScreenshotEvent.SCREENSHOT_SWIPE_DISMISSED, 0,
+                        mPackageName);
+                animator.addListener(new AnimatorListenerAdapter() {
                     @Override
-                    public void onSwipeDismissInitiated(Animator anim) {
-                        if (DEBUG_DISMISS) {
-                            Log.d(ScreenshotView.TAG, "dismiss triggered via swipe gesture");
-                        }
-                        mUiEventLogger.log(ScreenshotEvent.SCREENSHOT_SWIPE_DISMISSED, 0,
-                                mPackageName);
-                        anim.addListener(new AnimatorListenerAdapter() {
-                            @Override
-                            public void onAnimationStart(Animator animation) {
-                                super.onAnimationStart(animation);
-                                mBackgroundProtection.animate()
-                                        .alpha(0).setDuration(anim.getDuration()).start();
-                            }
-                        });
-                    }
-
-                    @Override
-                    public void onDismissComplete() {
-                        mCallbacks.onDismiss();
+                    public void onAnimationStart(Animator animation) {
+                        super.onAnimationStart(animation);
+                        mBackgroundProtection.animate()
+                                .alpha(0).setDuration(animation.getDuration()).start();
                     }
                 });
+            }
+
+            @Override
+            public void onDismissComplete() {
+                mCallbacks.onDismiss();
+            }
+        });
     }
 
     View getScreenshotPreview() {
@@ -648,8 +633,6 @@
                 requestLayout();
 
                 createScreenshotActionsShadeAnimation().start();
-
-                setOnTouchListener(mSwipeDismissHandler);
             }
         });
 
@@ -958,7 +941,7 @@
     }
 
     boolean isDismissing() {
-        return mSwipeDismissHandler.isDismissing();
+        return mScreenshotStatic.isDismissing();
     }
 
     boolean isPendingSharedTransition() {
@@ -966,15 +949,14 @@
     }
 
     void animateDismissal() {
-        mSwipeDismissHandler.dismiss();
+        mScreenshotStatic.dismiss();
     }
 
     void reset() {
         if (DEBUG_UI) {
             Log.d(TAG, "reset screenshot view");
         }
-
-        mSwipeDismissHandler.cancel();
+        mScreenshotStatic.cancelDismissal();
         if (DEBUG_WINDOW) {
             Log.d(TAG, "removing OnComputeInternalInsetsListener");
         }
diff --git a/packages/SystemUI/src/com/android/systemui/screenshot/SwipeDismissHandler.java b/packages/SystemUI/src/com/android/systemui/screenshot/SwipeDismissHandler.java
deleted file mode 100644
index 24b1249..0000000
--- a/packages/SystemUI/src/com/android/systemui/screenshot/SwipeDismissHandler.java
+++ /dev/null
@@ -1,237 +0,0 @@
-/*
- * Copyright (C) 2021 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.systemui.screenshot;
-
-import static com.android.systemui.screenshot.LogConfig.DEBUG_ANIM;
-import static com.android.systemui.screenshot.LogConfig.DEBUG_DISMISS;
-
-import android.animation.Animator;
-import android.animation.AnimatorListenerAdapter;
-import android.animation.ValueAnimator;
-import android.content.Context;
-import android.util.DisplayMetrics;
-import android.util.Log;
-import android.util.MathUtils;
-import android.view.GestureDetector;
-import android.view.MotionEvent;
-import android.view.View;
-
-/**
- * Allows a view to be swipe-dismissed, or returned to its location if distance threshold is not met
- */
-public class SwipeDismissHandler implements View.OnTouchListener {
-    private static final String TAG = "SwipeDismissHandler";
-
-    // distance needed to register a dismissal
-    private static final float DISMISS_DISTANCE_THRESHOLD_DP = 20;
-
-    /**
-     * Stores the callbacks when the view is interacted with or dismissed.
-     */
-    public interface SwipeDismissCallbacks {
-        /**
-         * Run when the view is interacted with (touched)
-         */
-        void onInteraction();
-
-        /**
-         * Run when the view is dismissed (the distance threshold is met), pre-dismissal animation
-         */
-        void onSwipeDismissInitiated(Animator animator);
-
-        /**
-         * Run when the view is dismissed (the distance threshold is met), post-dismissal animation
-         */
-        void onDismissComplete();
-    }
-
-    private final View mView;
-    private final SwipeDismissCallbacks mCallbacks;
-    private final GestureDetector mGestureDetector;
-    private DisplayMetrics mDisplayMetrics;
-    private ValueAnimator mDismissAnimation;
-
-
-    private float mStartX;
-    // Keeps track of the most recent direction (between the last two move events).
-    // -1 for left; +1 for right.
-    private int mDirectionX;
-    private float mPreviousX;
-
-    public SwipeDismissHandler(Context context, View view, SwipeDismissCallbacks callbacks) {
-        mView = view;
-        mCallbacks = callbacks;
-        GestureDetector.OnGestureListener gestureListener = new SwipeDismissGestureListener();
-        mGestureDetector = new GestureDetector(context, gestureListener);
-        mDisplayMetrics = new DisplayMetrics();
-        context.getDisplay().getRealMetrics(mDisplayMetrics);
-    }
-
-    @Override
-    public boolean onTouch(View view, MotionEvent event) {
-        boolean gestureResult = mGestureDetector.onTouchEvent(event);
-        mCallbacks.onInteraction();
-        if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
-            mStartX = event.getRawX();
-            mPreviousX = mStartX;
-            return true;
-        } else if (event.getActionMasked() == MotionEvent.ACTION_UP) {
-            if (mDismissAnimation != null && mDismissAnimation.isRunning()) {
-                return true;
-            }
-            if (isPastDismissThreshold()) {
-                ValueAnimator dismissAnimator = createSwipeDismissAnimation(1);
-                mCallbacks.onSwipeDismissInitiated(dismissAnimator);
-                dismiss(dismissAnimator);
-            } else {
-                // if we've moved, but not past the threshold, start the return animation
-                if (DEBUG_DISMISS) {
-                    Log.d(TAG, "swipe gesture abandoned");
-                }
-                createSwipeReturnAnimation().start();
-            }
-            return true;
-        }
-        return gestureResult;
-    }
-
-    class SwipeDismissGestureListener extends GestureDetector.SimpleOnGestureListener {
-        @Override
-        public boolean onScroll(
-                MotionEvent ev1, MotionEvent ev2, float distanceX, float distanceY) {
-            mView.setTranslationX(ev2.getRawX() - mStartX);
-            mDirectionX = (ev2.getRawX() < mPreviousX) ? -1 : 1;
-            mPreviousX = ev2.getRawX();
-            return true;
-        }
-
-        @Override
-        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
-                float velocityY) {
-            if (mView.getTranslationX() * velocityX > 0
-                    && (mDismissAnimation == null || !mDismissAnimation.isRunning())) {
-                ValueAnimator dismissAnimator =
-                        createSwipeDismissAnimation(velocityX / (float) 1000);
-                mCallbacks.onSwipeDismissInitiated(dismissAnimator);
-                dismiss(dismissAnimator);
-                return true;
-            }
-            return false;
-        }
-    }
-
-    private boolean isPastDismissThreshold() {
-        float translationX = mView.getTranslationX();
-        // Determines whether the absolute translation from the start is in the same direction
-        // as the current movement. For example, if the user moves most of the way to the right,
-        // but then starts dragging back left, we do not dismiss even though the absolute
-        // distance is greater than the threshold.
-        if (translationX * mDirectionX > 0) {
-            return Math.abs(translationX) >= FloatingWindowUtil.dpToPx(mDisplayMetrics,
-                    DISMISS_DISTANCE_THRESHOLD_DP);
-        }
-        return false;
-    }
-
-    /**
-     * Return whether the view is currently being dismissed
-     */
-    public boolean isDismissing() {
-        return (mDismissAnimation != null && mDismissAnimation.isRunning());
-    }
-
-    /**
-     * Cancel the currently-running dismissal animation, if any.
-     */
-    public void cancel() {
-        if (isDismissing()) {
-            if (DEBUG_ANIM) {
-                Log.d(TAG, "cancelling dismiss animation");
-            }
-            mDismissAnimation.cancel();
-        }
-    }
-
-    /**
-     * Start dismissal animation (will run onDismiss callback when animation complete)
-     */
-    public void dismiss() {
-        dismiss(createSwipeDismissAnimation(1));
-    }
-
-    private void dismiss(ValueAnimator animator) {
-        mDismissAnimation = animator;
-        mDismissAnimation.addListener(new AnimatorListenerAdapter() {
-            private boolean mCancelled;
-
-            @Override
-            public void onAnimationCancel(Animator animation) {
-                super.onAnimationCancel(animation);
-                mCancelled = true;
-            }
-
-            @Override
-            public void onAnimationEnd(Animator animation) {
-                super.onAnimationEnd(animation);
-                if (!mCancelled) {
-                    mCallbacks.onDismissComplete();
-                }
-            }
-        });
-        mDismissAnimation.start();
-    }
-
-    private ValueAnimator createSwipeDismissAnimation(float velocity) {
-        // velocity is measured in pixels per millisecond
-        velocity = Math.min(3, Math.max(1, velocity));
-        ValueAnimator anim = ValueAnimator.ofFloat(0, 1);
-        float startX = mView.getTranslationX();
-        // make sure the UI gets all the way off the screen in the direction of movement
-        // (the actions container background is guaranteed to be both the leftmost and
-        // rightmost UI element in LTR and RTL)
-        float finalX;
-        int layoutDir = mView.getContext().getResources().getConfiguration().getLayoutDirection();
-        if (startX > 0 || (startX == 0 && layoutDir == View.LAYOUT_DIRECTION_RTL)) {
-            finalX = mDisplayMetrics.widthPixels;
-        } else {
-            finalX = -1 * mView.getRight();
-        }
-        float distance = Math.abs(finalX - startX);
-
-        anim.addUpdateListener(animation -> {
-            float translation = MathUtils.lerp(startX, finalX, animation.getAnimatedFraction());
-            mView.setTranslationX(translation);
-            mView.setAlpha(1 - animation.getAnimatedFraction());
-        });
-        anim.setDuration((long) (distance / Math.abs(velocity)));
-        return anim;
-    }
-
-    private ValueAnimator createSwipeReturnAnimation() {
-        ValueAnimator anim = ValueAnimator.ofFloat(0, 1);
-        float startX = mView.getTranslationX();
-        float finalX = 0;
-
-        anim.addUpdateListener(animation -> {
-            float translation = MathUtils.lerp(
-                    startX, finalX, animation.getAnimatedFraction());
-            mView.setTranslationX(translation);
-        });
-
-        return anim;
-    }
-}
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/CommandQueue.java b/packages/SystemUI/src/com/android/systemui/statusbar/CommandQueue.java
index 5932a64..d9a98b1 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/CommandQueue.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/CommandQueue.java
@@ -163,6 +163,7 @@
     private static final int MSG_MEDIA_TRANSFER_RECEIVER_STATE = 65 << MSG_SHIFT;
     private static final int MSG_REGISTER_NEARBY_MEDIA_DEVICE_PROVIDER = 66 << MSG_SHIFT;
     private static final int MSG_UNREGISTER_NEARBY_MEDIA_DEVICE_PROVIDER = 67 << MSG_SHIFT;
+    private static final int MSG_TILE_SERVICE_REQUEST_LISTENING_STATE = 68 << MSG_SHIFT;
 
     public static final int FLAG_EXCLUDE_NONE = 0;
     public static final int FLAG_EXCLUDE_SEARCH_PANEL = 1 << 0;
@@ -433,6 +434,11 @@
         default void setNavigationBarLumaSamplingEnabled(int displayId, boolean enable) {}
 
         /**
+         * @see IStatusBar#requestTileServiceListeningState
+         */
+        default void requestTileServiceListeningState(@NonNull ComponentName componentName) {}
+
+        /**
          * @see IStatusBar#requestAddTile
          */
         default void requestAddTile(
@@ -1190,6 +1196,12 @@
     }
 
     @Override
+    public void requestTileServiceListeningState(@NonNull ComponentName componentName) {
+        mHandler.obtainMessage(MSG_TILE_SERVICE_REQUEST_LISTENING_STATE, componentName)
+                .sendToTarget();
+    }
+
+    @Override
     public void requestAddTile(
             @NonNull ComponentName componentName,
             @NonNull CharSequence appName,
@@ -1686,6 +1698,12 @@
                         mCallbacks.get(i).unregisterNearbyMediaDevicesProvider(provider);
                     }
                     break;
+                case MSG_TILE_SERVICE_REQUEST_LISTENING_STATE:
+                    ComponentName component = (ComponentName) msg.obj;
+                    for (int i = 0; i < mCallbacks.size(); i++) {
+                        mCallbacks.get(i).requestTileServiceListeningState(component);
+                    }
+                    break;
             }
         }
     }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/KeyguardIndicationController.java b/packages/SystemUI/src/com/android/systemui/statusbar/KeyguardIndicationController.java
index ccec0c2..16ddb0a 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/KeyguardIndicationController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/KeyguardIndicationController.java
@@ -293,6 +293,14 @@
         }
     }
 
+    /**
+     * Cleanup
+     */
+    public void destroy() {
+        mHandler.removeCallbacksAndMessages(null);
+        mBroadcastDispatcher.unregisterReceiver(mBroadcastReceiver);
+    }
+
     private void handleAlignStateChanged(int alignState) {
         String alignmentIndication = "";
         if (alignState == DockManager.ALIGN_STATE_POOR) {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/LockscreenShadeTransitionController.kt b/packages/SystemUI/src/com/android/systemui/statusbar/LockscreenShadeTransitionController.kt
index 1ab0345..ab4d0dd 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/LockscreenShadeTransitionController.kt
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/LockscreenShadeTransitionController.kt
@@ -406,6 +406,7 @@
 
                     mediaHierarchyManager.setTransitionToFullShadeAmount(field)
                     transitionToShadeAmountCommon(field)
+                    transitionToShadeAmountKeyguard(field)
                 }
             }
         }
@@ -420,11 +421,6 @@
         val scrimProgress = MathUtils.saturate(dragDownAmount / scrimTransitionDistance)
         scrimController.setTransitionToFullShadeProgress(scrimProgress)
 
-        // Fade out all content only visible on the lockscreen
-        val npvcProgress =
-            MathUtils.saturate(dragDownAmount / npvcKeyguardContentAlphaTransitionDistance)
-        notificationPanelController.setKeyguardOnlyContentAlpha(1.0f - npvcProgress)
-
         if (depthControllerTransitionDistance > 0) {
             val depthProgress =
                 MathUtils.saturate(dragDownAmount / depthControllerTransitionDistance)
@@ -438,6 +434,22 @@
         centralSurfaces.setTransitionToFullShadeProgress(statusBarProgress)
     }
 
+    private fun transitionToShadeAmountKeyguard(dragDownAmount: Float) {
+        // Fade out all content only visible on the lockscreen
+        val keyguardAlphaProgress =
+            MathUtils.saturate(dragDownAmount / npvcKeyguardContentAlphaTransitionDistance)
+        val keyguardAlpha = 1f - keyguardAlphaProgress
+        val keyguardTranslationY = if (useSplitShade) {
+            // On split-shade, the translationY of the keyguard should stay in sync with the
+            // translation of media.
+            mediaHierarchyManager.getGuidedTransformationTranslationY()
+        } else {
+            0
+        }
+        notificationPanelController
+            .setKeyguardTransitionProgress(keyguardAlpha, keyguardTranslationY)
+    }
+
     private fun setDragDownAmountAnimated(
         target: Float,
         delay: Long = 0,
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/NotificationRemoteInputManager.java b/packages/SystemUI/src/com/android/systemui/statusbar/NotificationRemoteInputManager.java
index 94a6d3e..66c1d87 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/NotificationRemoteInputManager.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/NotificationRemoteInputManager.java
@@ -507,12 +507,11 @@
                 Math.max(cx + cy, cx + (h - cy)),
                 Math.max((w - cx) + cy, (w - cx) + (h - cy)));
 
-        riv.setRevealParameters(cx, cy, r);
-        riv.setPendingIntent(pendingIntent);
+        riv.getController().setRevealParams(new RemoteInputView.RevealParams(cx, cy, r));
         riv.getController().setPendingIntent(pendingIntent);
-        riv.setRemoteInput(inputs, input, editedSuggestionInfo);
         riv.getController().setRemoteInput(input);
         riv.getController().setRemoteInputs(inputs);
+        riv.getController().setEditedSuggestionInfo(editedSuggestionInfo);
         riv.focusAnimated();
         if (userMessageContent != null) {
             riv.setEditTextContent(userMessageContent);
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/NotificationShadeDepthController.kt b/packages/SystemUI/src/com/android/systemui/statusbar/NotificationShadeDepthController.kt
index 267ee6d..a0388de 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/NotificationShadeDepthController.kt
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/NotificationShadeDepthController.kt
@@ -259,6 +259,7 @@
                 addListener(object : AnimatorListenerAdapter() {
                     override fun onAnimationEnd(animation: Animator?) {
                         keyguardAnimator = null
+                        wakeAndUnlockBlurRadius = 0f
                         scheduleUpdate()
                     }
                 })
@@ -439,7 +440,7 @@
             it.println("StatusBarWindowBlurController:")
             it.increaseIndent()
             it.println("shadeExpansion: $shadeExpansion")
-            it.println("shouldApplyShaeBlur: ${shouldApplyShadeBlur()}")
+            it.println("shouldApplyShadeBlur: ${shouldApplyShadeBlur()}")
             it.println("shadeAnimation: ${shadeAnimation.radius}")
             it.println("brightnessMirrorRadius: ${brightnessMirrorSpring.radius}")
             it.println("wakeAndUnlockBlur: $wakeAndUnlockBlurRadius")
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/events/StatusEvent.kt b/packages/SystemUI/src/com/android/systemui/statusbar/events/StatusEvent.kt
index d4d84c1..4e1404d 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/events/StatusEvent.kt
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/events/StatusEvent.kt
@@ -16,6 +16,7 @@
 
 package com.android.systemui.statusbar.events
 
+import android.annotation.SuppressLint
 import android.content.Context
 import android.graphics.Color
 import android.graphics.drawable.ColorDrawable
@@ -27,13 +28,15 @@
 import com.android.systemui.privacy.OngoingPrivacyChip
 import com.android.systemui.privacy.PrivacyItem
 
+typealias ViewCreator = (context: Context) -> BackgroundAnimatableView
+
 interface StatusEvent {
     val priority: Int
     // Whether or not to force the status bar open and show a dot
     val forceVisible: Boolean
     // Whether or not to show an animation for this event
     val showAnimation: Boolean
-    val viewCreator: (context: Context) -> View
+    val viewCreator: ViewCreator
     var contentDescription: String?
 
     // Update this event with values from another event.
@@ -47,14 +50,37 @@
     }
 }
 
+class BGView(
+    context: Context
+) : View(context), BackgroundAnimatableView {
+    override val view: View
+        get() = this
+
+    override fun setBoundsForAnimation(l: Int, t: Int, r: Int, b: Int) {
+        setLeftTopRightBottom(l, t, r, b)
+    }
+}
+
+@SuppressLint("AppCompatCustomView")
+class BGImageView(
+    context: Context
+) : ImageView(context), BackgroundAnimatableView {
+    override val view: View
+        get() = this
+
+    override fun setBoundsForAnimation(l: Int, t: Int, r: Int, b: Int) {
+        setLeftTopRightBottom(l, t, r, b)
+    }
+}
+
 class BatteryEvent : StatusEvent {
     override val priority = 50
     override val forceVisible = false
     override val showAnimation = true
     override var contentDescription: String? = ""
 
-    override val viewCreator: (context: Context) -> View = { context ->
-        val iv = ImageView(context)
+    override val viewCreator: (context: Context) -> BGImageView = { context ->
+        val iv = BGImageView(context)
         iv.setImageDrawable(ThemedBatteryDrawable(context, Color.WHITE))
         iv.setBackgroundDrawable(ColorDrawable(Color.GREEN))
         iv
@@ -72,7 +98,7 @@
     var privacyItems: List<PrivacyItem> = listOf()
     private var privacyChip: OngoingPrivacyChip? = null
 
-    override val viewCreator: (context: Context) -> View = { context ->
+    override val viewCreator: ViewCreator = { context ->
         val v = LayoutInflater.from(context)
                 .inflate(R.layout.ongoing_privacy_chip, null) as OngoingPrivacyChip
         v.privacyList = privacyItems
@@ -82,7 +108,7 @@
     }
 
     override fun toString(): String {
-        return javaClass.simpleName
+        return "${javaClass.simpleName}(forceVisible=$forceVisible, privacyItems=$privacyItems)"
     }
 
     override fun shouldUpdateFromEvent(other: StatusEvent?): Boolean {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/events/SystemEventChipAnimationController.kt b/packages/SystemUI/src/com/android/systemui/statusbar/events/SystemEventChipAnimationController.kt
index d5a0467..9795dcf 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/events/SystemEventChipAnimationController.kt
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/events/SystemEventChipAnimationController.kt
@@ -18,14 +18,16 @@
 
 import android.animation.ValueAnimator
 import android.content.Context
+import android.graphics.Point
 import android.view.Gravity
 import android.view.LayoutInflater
 import android.view.View
+import android.view.View.MeasureSpec.AT_MOST
 import android.view.ViewGroup.LayoutParams.MATCH_PARENT
 import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
 import android.widget.FrameLayout
 import com.android.systemui.R
-import com.android.systemui.statusbar.phone.StatusBarLocationPublisher
+import com.android.systemui.statusbar.phone.StatusBarContentInsetsProvider
 import com.android.systemui.statusbar.window.StatusBarWindowController
 import javax.inject.Inject
 
@@ -35,44 +37,73 @@
 class SystemEventChipAnimationController @Inject constructor(
     private val context: Context,
     private val statusBarWindowController: StatusBarWindowController,
-    private val locationPublisher: StatusBarLocationPublisher
+    private val contentInsetsProvider: StatusBarContentInsetsProvider
 ) : SystemStatusChipAnimationCallback {
-    var showPersistentDot = false
-        set(value) {
-            field = value
-            statusBarWindowController.setForceStatusBarVisible(value)
-            maybeUpdateShowDot()
-        }
 
     private lateinit var animationWindowView: FrameLayout
-    private lateinit var animationDotView: View
-    private var currentAnimatedView: View? = null
+
+    private var currentAnimatedView: BackgroundAnimatableView? = null
+
+    // Left for LTR, Right for RTL
+    private var animationDirection = LEFT
+    private var chipRight = 0
+    private var chipLeft = 0
+    private var chipWidth = 0
+    private var dotCenter = Point(0, 0)
+    private var dotSize = context.resources.getDimensionPixelSize(
+            R.dimen.ongoing_appops_dot_diameter)
+    // If the chip animates away to a persistent dot, then we modify the CHIP_OUT animation
+    private var isAnimatingToDot = false
 
     // TODO: move to dagger
     private var initialized = false
 
     override fun onChipAnimationStart(
-        viewCreator: (context: Context) -> View,
+        viewCreator: ViewCreator,
         @SystemAnimationState state: Int
     ) {
         if (!initialized) init()
 
         if (state == ANIMATING_IN) {
-            currentAnimatedView = viewCreator(context)
-            animationWindowView.addView(currentAnimatedView, layoutParamsDefault())
+            animationDirection = if (animationWindowView.isLayoutRtl) RIGHT else LEFT
 
-            // We are animating IN; chip comes in from View.END
+            // Initialize the animated view
+            val insets = contentInsetsProvider.getStatusBarContentInsetsForCurrentRotation()
+            currentAnimatedView = viewCreator(context).also {
+                animationWindowView.addView(
+                        it.view,
+                        layoutParamsDefault(
+                                if (animationWindowView.isLayoutRtl) insets.first
+                                else insets.second))
+                it.view.alpha = 0f
+                // For some reason, the window view's measured width is always 0 here, so use the
+                // parent (status bar)
+                it.view.measure(
+                        View.MeasureSpec.makeMeasureSpec(
+                                (animationWindowView.parent as View).width, AT_MOST),
+                        View.MeasureSpec.makeMeasureSpec(animationWindowView.height, AT_MOST))
+                chipWidth = it.chipWidth
+            }
+
+            // decide which direction we're animating from, and then set some screen coordinates
+            val contentRect = contentInsetsProvider.getStatusBarContentAreaForCurrentRotation()
+            when (animationDirection) {
+                LEFT -> {
+                    chipRight = contentRect.right
+                    chipLeft = contentRect.right - chipWidth
+                }
+                else /* RIGHT */ -> {
+                    chipLeft = contentRect.left
+                    chipRight = contentRect.left + chipWidth
+                }
+            }
+
             currentAnimatedView?.apply {
-                val translation = width.toFloat()
-                translationX = if (isLayoutRtl) -translation else translation
-                alpha = 0f
-                visibility = View.VISIBLE
-                setPadding(locationPublisher.marginLeft, 0, locationPublisher.marginRight, 0)
+                updateAnimatedViewBoundsForAmount(0.1f, this)
             }
         } else {
             // We are animating away
-            currentAnimatedView?.apply {
-                translationX = 0f
+            currentAnimatedView!!.view.apply {
                 alpha = 1f
             }
         }
@@ -82,15 +113,14 @@
         if (state == ANIMATING_IN) {
             // Finished animating in
             currentAnimatedView?.apply {
-                translationX = 0f
-                alpha = 1f
+                updateAnimatedViewBoundsForAmount(1f, this)
             }
         } else {
             // Finished animating away
-            currentAnimatedView?.apply {
+            currentAnimatedView!!.view.apply {
                 visibility = View.INVISIBLE
             }
-            animationWindowView.removeView(currentAnimatedView)
+            animationWindowView.removeView(currentAnimatedView!!.view)
         }
     }
 
@@ -98,22 +128,10 @@
         animator: ValueAnimator,
         @SystemAnimationState state: Int
     ) {
-        // Alpha is parameterized 0,1, and translation from (width, 0)
         currentAnimatedView?.apply {
-            val amt = animator.animatedValue as Float
-
-            alpha = amt
-
-            val w = width
-            val translation = (1 - amt) * w
-            translationX = if (isLayoutRtl) -translation else translation
-        }
-    }
-
-    private fun maybeUpdateShowDot() {
-        if (!initialized) return
-        if (!showPersistentDot && currentAnimatedView == null) {
-            animationDotView.visibility = View.INVISIBLE
+            val amt = (animator.animatedValue as Float).amt()
+            view.alpha = (animator.animatedValue as Float)
+            updateAnimatedViewBoundsForAmount(amt, this)
         }
     }
 
@@ -121,19 +139,56 @@
         initialized = true
         animationWindowView = LayoutInflater.from(context)
                 .inflate(R.layout.system_event_animation_window, null) as FrameLayout
-        animationDotView = animationWindowView.findViewById(R.id.dot_view)
         val lp = FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT)
         lp.gravity = Gravity.END or Gravity.CENTER_VERTICAL
         statusBarWindowController.addViewToWindow(animationWindowView, lp)
+        animationWindowView.clipToPadding = false
+        animationWindowView.clipChildren = false
+        animationWindowView.measureAllChildren = true
+    }
+
+    private fun layoutParamsDefault(marginEnd: Int): FrameLayout.LayoutParams =
+            FrameLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT).also {
+                it.gravity = Gravity.END or Gravity.CENTER_VERTICAL
+                it.marginEnd = marginEnd
+            }
+
+    private fun updateAnimatedViewBoundsForAmount(amt: Float, chip: BackgroundAnimatableView) {
+        when (animationDirection) {
+            LEFT -> {
+                chip.setBoundsForAnimation(
+                        (chipRight - (chipWidth * amt)).toInt(),
+                        chip.view.top,
+                        chipRight,
+                        chip.view.bottom)
+            }
+            else /* RIGHT */ -> {
+                chip.setBoundsForAnimation(
+                        chipLeft,
+                        chip.view.top,
+                        (chipLeft + (chipWidth * amt)).toInt(),
+                        chip.view.bottom)
+            }
+        }
     }
 
     private fun start() = if (animationWindowView.isLayoutRtl) right() else left()
-    private fun right() = locationPublisher.marginRight
-    private fun left() = locationPublisher.marginLeft
-
-    private fun layoutParamsDefault(): FrameLayout.LayoutParams =
-        FrameLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT).also {
-            it.gravity = Gravity.END or Gravity.CENTER_VERTICAL
-            it.marginStart = start()
-    }
+    private fun right() = contentInsetsProvider.getStatusBarContentAreaForCurrentRotation().right
+    private fun left() = contentInsetsProvider.getStatusBarContentAreaForCurrentRotation().left
+    private fun Float.amt() = 0.01f.coerceAtLeast(this)
 }
+
+/**
+ * Chips should provide a view that can be animated with something better than a fade-in
+ */
+interface BackgroundAnimatableView {
+    val view: View // Since this can't extend View, add a view prop
+        get() = this as View
+    val chipWidth: Int
+        get() = view.measuredWidth
+    fun setBoundsForAnimation(l: Int, t: Int, r: Int, b: Int)
+}
+
+// Animation directions
+private const val LEFT = 1
+private const val RIGHT = 2
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/events/SystemEventCoordinator.kt b/packages/SystemUI/src/com/android/systemui/statusbar/events/SystemEventCoordinator.kt
index 04f7492..fde5d39 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/events/SystemEventCoordinator.kt
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/events/SystemEventCoordinator.kt
@@ -70,11 +70,11 @@
     fun notifyPrivacyItemsChanged(showAnimation: Boolean = true) {
         val event = PrivacyEvent(showAnimation)
         event.privacyItems = privacyStateListener.currentPrivacyItems
-        event.contentDescription = {
+        event.contentDescription = run {
             val items = PrivacyChipBuilder(context, event.privacyItems).joinTypes()
             context.getString(
                     R.string.ongoing_privacy_chip_content_multiple_apps, items)
-        }()
+        }
         scheduler.onStatusEvent(event)
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/events/SystemStatusAnimationScheduler.kt b/packages/SystemUI/src/com/android/systemui/statusbar/events/SystemStatusAnimationScheduler.kt
index 5a27329..947f3eb 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/events/SystemStatusAnimationScheduler.kt
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/events/SystemStatusAnimationScheduler.kt
@@ -21,12 +21,12 @@
 import android.animation.AnimatorSet
 import android.animation.ValueAnimator
 import android.annotation.IntDef
-import android.content.Context
 import android.os.Process
 import android.provider.DeviceConfig
 import android.util.Log
-import android.view.View
 import com.android.systemui.Dumpable
+import com.android.systemui.animation.Interpolators.STANDARD_ACCELERATE
+import com.android.systemui.animation.Interpolators.STANDARD_DECELERATE
 
 import com.android.systemui.dagger.SysUISingleton
 import com.android.systemui.dagger.qualifiers.Main
@@ -45,7 +45,7 @@
  * Dead-simple scheduler for system status events. Obeys the following principles (all values TBD):
  *      - Avoiding log spam by only allowing 12 events per minute (1event/5s)
  *      - Waits 100ms to schedule any event for debouncing/prioritization
- *      - Simple prioritization: Privacy > Battery > connectivity (encoded in StatusEvent)
+ *      - Simple prioritization: Privacy > Battery > connectivity (encoded in [StatusEvent])
  *      - Only schedules a single event, and throws away lowest priority events
  *
  * There are 4 basic stages of animation at play here:
@@ -111,7 +111,7 @@
             scheduleEvent(event)
         } else if (scheduledEvent?.shouldUpdateFromEvent(event) == true) {
             if (DEBUG) {
-                Log.d(TAG, "updating current event from: $event")
+                Log.d(TAG, "updating current event from: $event. animationState=$animationState")
             }
             scheduledEvent?.updateFromEvent(event)
             if (event.forceVisible) {
@@ -172,12 +172,14 @@
             entranceAnimator.duration = ENTRANCE_ANIM_LENGTH
             entranceAnimator.addListener(systemAnimatorAdapter)
             entranceAnimator.addUpdateListener(systemUpdateListener)
+            entranceAnimator.interpolator = STANDARD_ACCELERATE
 
             val chipAnimator = ValueAnimator.ofFloat(0f, 1f)
             chipAnimator.duration = CHIP_ANIM_LENGTH
             chipAnimator.addListener(
                     ChipAnimatorAdapter(RUNNING_CHIP_ANIM, scheduledEvent!!.viewCreator))
             chipAnimator.addUpdateListener(chipUpdateListener)
+            chipAnimator.interpolator = STANDARD_DECELERATE
 
             val aSet2 = AnimatorSet()
             aSet2.playSequentially(entranceAnimator, chipAnimator)
@@ -190,6 +192,12 @@
                 systemAnimator.duration = ENTRANCE_ANIM_LENGTH
                 systemAnimator.addListener(systemAnimatorAdapter)
                 systemAnimator.addUpdateListener(systemUpdateListener)
+                systemAnimator.interpolator = STANDARD_DECELERATE
+                systemAnimator.addListener(object : AnimatorListenerAdapter() {
+                    override fun onAnimationEnd(animation: Animator?) {
+                        statusBarWindowController.setForceStatusBarVisible(false)
+                    }
+                })
 
                 val chipAnimator = ValueAnimator.ofFloat(1f, 0f)
                 chipAnimator.duration = CHIP_ANIM_LENGTH
@@ -201,6 +209,7 @@
                 chipAnimator.addListener(
                     ChipAnimatorAdapter(endState, scheduledEvent!!.viewCreator))
                 chipAnimator.addUpdateListener(chipUpdateListener)
+                chipAnimator.interpolator = STANDARD_ACCELERATE
 
                 val aSet2 = AnimatorSet()
 
@@ -212,7 +221,6 @@
 
                 aSet2.start()
 
-                statusBarWindowController.setForceStatusBarVisible(false)
                 scheduledEvent = null
             }, DISPLAY_LENGTH)
         }, DELAY)
@@ -313,7 +321,7 @@
 
     inner class ChipAnimatorAdapter(
         @SystemAnimationState val endState: Int,
-        val viewCreator: (context: Context) -> View
+        val viewCreator: ViewCreator
     ) : AnimatorListenerAdapter() {
         override fun onAnimationEnd(p0: Animator?) {
             chipAnimationController.onChipAnimationEnd(animationState)
@@ -359,7 +367,7 @@
     fun onChipAnimationUpdate(animator: ValueAnimator, @SystemAnimationState state: Int) {}
 
     fun onChipAnimationStart(
-        viewCreator: (context: Context) -> View,
+        viewCreator: ViewCreator,
         @SystemAnimationState state: Int
     ) {}
 
@@ -371,7 +379,7 @@
 @Retention(AnnotationRetention.SOURCE)
 @IntDef(
         value = [
-            IDLE, ANIMATING_IN, RUNNING_CHIP_ANIM, ANIMATING_OUT
+            IDLE, ANIMATING_IN, RUNNING_CHIP_ANIM, ANIMATING_OUT, SHOWING_PERSISTENT_DOT
         ]
 )
 annotation class SystemAnimationState
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/InstantAppNotifier.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/InstantAppNotifier.java
index 5b7d90b..df412ed 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/InstantAppNotifier.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/InstantAppNotifier.java
@@ -16,11 +16,9 @@
 
 package com.android.systemui.statusbar.notification;
 
-import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
 import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
 import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
 import static android.app.WindowConfiguration.WINDOWING_MODE_MULTI_WINDOW;
-import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
 
 import android.annotation.NonNull;
 import android.annotation.Nullable;
@@ -182,25 +180,6 @@
     }
 
     /**
-     * Posts an instant app notification if the top activity of the primary container in the
-     * splitted screen is an instant app and the corresponding instant app notification is not
-     * posted yet. If the notification already exists, this method removes it from {@code
-     * notifs} in the arguments.
-     */
-    private void checkAndPostForPrimaryScreen(
-            @NonNull ArraySet<Pair<String, Integer>> notifs,
-            @NonNull NotificationManager noMan,
-            @NonNull IPackageManager pm) {
-        try {
-            final RootTaskInfo info = ActivityTaskManager.getService().getRootTaskInfo(
-                    WINDOWING_MODE_SPLIT_SCREEN_PRIMARY, ACTIVITY_TYPE_UNDEFINED);
-            checkAndPostForStack(info, notifs, noMan, pm);
-        } catch (RemoteException e) {
-            e.rethrowFromSystemServer();
-        }
-    }
-
-    /**
      * Posts an instant app notification if the top activity of the given stack is an instant app
      * and the corresponding instant app notification is not posted yet. If the notification already
      * exists, this method removes it from {@code notifs} in the arguments.
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationLaunchAnimatorController.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationLaunchAnimatorController.kt
index 2c1296f..7fbb0f1 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationLaunchAnimatorController.kt
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationLaunchAnimatorController.kt
@@ -22,15 +22,18 @@
     private val headsUpManager: HeadsUpManagerPhone,
     private val jankMonitor: InteractionJankMonitor
 ) {
+    @JvmOverloads
     fun getAnimatorController(
-        notification: ExpandableNotificationRow
+        notification: ExpandableNotificationRow,
+        onFinishAnimationCallback: Runnable = Runnable {}
     ): NotificationLaunchAnimatorController {
         return NotificationLaunchAnimatorController(
             notificationShadeWindowViewController,
             notificationListContainer,
             headsUpManager,
             notification,
-            jankMonitor
+            jankMonitor,
+            onFinishAnimationCallback
         )
     }
 }
@@ -45,7 +48,8 @@
     private val notificationListContainer: NotificationListContainer,
     private val headsUpManager: HeadsUpManagerPhone,
     private val notification: ExpandableNotificationRow,
-    private val jankMonitor: InteractionJankMonitor
+    private val jankMonitor: InteractionJankMonitor,
+    private val onFinishAnimationCallback: Runnable
 ) : ActivityLaunchAnimator.Controller {
 
     companion object {
@@ -119,6 +123,7 @@
 
         if (!willAnimate) {
             removeHun(animate = true)
+            onFinishAnimationCallback.run()
         }
     }
 
@@ -137,6 +142,7 @@
         notificationShadeWindowViewController.setExpandAnimationRunning(false)
         notificationEntry.isExpandAnimationRunning = false
         removeHun(animate = true)
+        onFinishAnimationCallback.run()
     }
 
     override fun onLaunchAnimationStart(isExpandingFullyAbove: Boolean) {
@@ -156,6 +162,7 @@
         notificationListContainer.setExpandingNotification(null)
         applyParams(null)
         removeHun(animate = false)
+        onFinishAnimationCallback.run()
     }
 
     private fun applyParams(params: ExpandAnimationParameters?) {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/ShadeListBuilder.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/ShadeListBuilder.java
index 369ef34..2baa079 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/ShadeListBuilder.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/ShadeListBuilder.java
@@ -230,7 +230,13 @@
         mPipelineState.requireState(STATE_IDLE);
 
         mNotifSections.clear();
+        NotifSectioner lastSection = null;
         for (NotifSectioner sectioner : sectioners) {
+            if (lastSection != null && lastSection.getBucket() > sectioner.getBucket()) {
+                throw new IllegalArgumentException("setSectioners with non contiguous sections "
+                        + lastSection.getName() + " - " + lastSection.getBucket() + " & "
+                        + sectioner.getName() + " - " + sectioner.getBucket());
+            }
             final NotifSection section = new NotifSection(sectioner, mNotifSections.size());
             final NotifComparator sectionComparator = section.getComparator();
             mNotifSections.add(section);
@@ -238,6 +244,7 @@
             if (sectionComparator != null) {
                 sectionComparator.setInvalidationListener(this::onNotifComparatorInvalidated);
             }
+            lastSection = sectioner;
         }
 
         mNotifSections.add(new NotifSection(DEFAULT_SECTIONER, mNotifSections.size()));
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/HeadsUpCoordinator.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/HeadsUpCoordinator.kt
index 0df2162..da0169b 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/HeadsUpCoordinator.kt
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/HeadsUpCoordinator.kt
@@ -72,10 +72,11 @@
     private var mEndLifetimeExtension: OnEndLifetimeExtensionCallback? = null
     private lateinit var mNotifPipeline: NotifPipeline
     private var mNow: Long = -1
-    // notifs we've extended the lifetime for
-    private val mNotifsExtendingLifetime = ArraySet<NotificationEntry>()
     private val mPostedEntries = LinkedHashMap<String, PostedEntry>()
 
+    // notifs we've extended the lifetime for with cancellation callbacks
+    private val mNotifsExtendingLifetime = ArrayMap<NotificationEntry, Runnable?>()
+
     override fun attach(pipeline: NotifPipeline) {
         mNotifPipeline = pipeline
         mHeadsUpManager.addListener(mOnHeadsUpChangedListener)
@@ -460,23 +461,20 @@
             }
             if (isSticky(entry)) {
                 val removeAfterMillis = mHeadsUpManager.getEarliestRemovalTime(entry.key)
-                mExecutor.executeDelayed({
-                    val canStillRemove = mHeadsUpManager.canRemoveImmediately(entry.key)
-                    if (mNotifsExtendingLifetime.contains(entry) && canStillRemove) {
-                        mHeadsUpManager.removeNotification(entry.key, /* releaseImmediately */ true)
-                    }
+                mNotifsExtendingLifetime[entry] = mExecutor.executeDelayed({
+                    mHeadsUpManager.removeNotification(entry.key, /* releaseImmediately */ true)
                 }, removeAfterMillis)
             } else {
                 mExecutor.execute {
                     mHeadsUpManager.removeNotification(entry.key, /* releaseImmediately */ false)
                 }
+                mNotifsExtendingLifetime[entry] = null
             }
-            mNotifsExtendingLifetime.add(entry)
             return true
         }
 
         override fun cancelLifetimeExtension(entry: NotificationEntry) {
-            mNotifsExtendingLifetime.remove(entry)
+            mNotifsExtendingLifetime.remove(entry)?.run()
         }
     }
 
@@ -543,7 +541,8 @@
         mPostedEntries[entry.key]?.calculateShouldBeHeadsUpStrict ?: isAttemptingToShowHun(entry)
 
     private fun endNotifLifetimeExtensionIfExtended(entry: NotificationEntry) {
-        if (mNotifsExtendingLifetime.remove(entry)) {
+        if (mNotifsExtendingLifetime.contains(entry)) {
+            mNotifsExtendingLifetime.remove(entry)?.run()
             mEndLifetimeExtension?.onEndLifetimeExtension(mLifetimeExtender, entry)
         }
     }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/KeyguardCoordinator.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/KeyguardCoordinator.java
index 22300d8..1237c70 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/KeyguardCoordinator.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/KeyguardCoordinator.java
@@ -16,36 +16,16 @@
 
 package com.android.systemui.statusbar.notification.collection.coordinator;
 
-import static android.app.Notification.VISIBILITY_SECRET;
-
-import android.content.BroadcastReceiver;
-import android.content.Context;
-import android.content.Intent;
-import android.content.IntentFilter;
-import android.database.ContentObserver;
-import android.net.Uri;
-import android.os.Handler;
-import android.os.UserHandle;
-import android.provider.Settings;
-import android.service.notification.StatusBarNotification;
-
-import androidx.annotation.MainThread;
-
 import com.android.keyguard.KeyguardUpdateMonitor;
-import com.android.keyguard.KeyguardUpdateMonitorCallback;
-import com.android.systemui.broadcast.BroadcastDispatcher;
 import com.android.systemui.plugins.statusbar.StatusBarStateController;
-import com.android.systemui.statusbar.NotificationLockscreenUserManager;
 import com.android.systemui.statusbar.StatusBarState;
 import com.android.systemui.statusbar.notification.SectionHeaderVisibilityProvider;
-import com.android.systemui.statusbar.notification.collection.GroupEntry;
-import com.android.systemui.statusbar.notification.collection.ListEntry;
 import com.android.systemui.statusbar.notification.collection.NotifPipeline;
 import com.android.systemui.statusbar.notification.collection.NotificationEntry;
 import com.android.systemui.statusbar.notification.collection.coordinator.dagger.CoordinatorScope;
 import com.android.systemui.statusbar.notification.collection.listbuilder.pluggable.NotifFilter;
 import com.android.systemui.statusbar.notification.collection.provider.HighPriorityProvider;
-import com.android.systemui.statusbar.policy.KeyguardStateController;
+import com.android.systemui.statusbar.notification.interruption.KeyguardNotificationVisibilityProvider;
 
 import javax.inject.Inject;
 
@@ -56,171 +36,48 @@
 @CoordinatorScope
 public class KeyguardCoordinator implements Coordinator {
     private static final String TAG = "KeyguardCoordinator";
-
-    private final Context mContext;
-    private final Handler mMainHandler;
-    private final KeyguardStateController mKeyguardStateController;
-    private final NotificationLockscreenUserManager mLockscreenUserManager;
-    private final BroadcastDispatcher mBroadcastDispatcher;
     private final StatusBarStateController mStatusBarStateController;
     private final KeyguardUpdateMonitor mKeyguardUpdateMonitor;
     private final HighPriorityProvider mHighPriorityProvider;
     private final SectionHeaderVisibilityProvider mSectionHeaderVisibilityProvider;
-
-    private boolean mHideSilentNotificationsOnLockscreen;
+    private final KeyguardNotificationVisibilityProvider mKeyguardNotificationVisibilityProvider;
 
     @Inject
     public KeyguardCoordinator(
-            Context context,
-            @MainThread Handler mainThreadHandler,
-            KeyguardStateController keyguardStateController,
-            NotificationLockscreenUserManager lockscreenUserManager,
-            BroadcastDispatcher broadcastDispatcher,
             StatusBarStateController statusBarStateController,
             KeyguardUpdateMonitor keyguardUpdateMonitor,
             HighPriorityProvider highPriorityProvider,
-            SectionHeaderVisibilityProvider sectionHeaderVisibilityProvider) {
-        mContext = context;
-        mMainHandler = mainThreadHandler;
-        mKeyguardStateController = keyguardStateController;
-        mLockscreenUserManager = lockscreenUserManager;
-        mBroadcastDispatcher = broadcastDispatcher;
+            SectionHeaderVisibilityProvider sectionHeaderVisibilityProvider,
+            KeyguardNotificationVisibilityProvider keyguardNotificationVisibilityProvider) {
         mStatusBarStateController = statusBarStateController;
         mKeyguardUpdateMonitor = keyguardUpdateMonitor;
         mHighPriorityProvider = highPriorityProvider;
         mSectionHeaderVisibilityProvider = sectionHeaderVisibilityProvider;
+        mKeyguardNotificationVisibilityProvider = keyguardNotificationVisibilityProvider;
     }
 
     @Override
     public void attach(NotifPipeline pipeline) {
-        readShowSilentNotificationSetting();
 
         setupInvalidateNotifListCallbacks();
         // Filter at the "finalize" stage so that views remain bound by PreparationCoordinator
         pipeline.addFinalizeFilter(mNotifFilter);
-
+        mKeyguardNotificationVisibilityProvider
+                .addOnStateChangedListener(this::invalidateListFromFilter);
         updateSectionHeadersVisibility();
     }
 
     private final NotifFilter mNotifFilter = new NotifFilter(TAG) {
         @Override
         public boolean shouldFilterOut(NotificationEntry entry, long now) {
-            final StatusBarNotification sbn = entry.getSbn();
-
-            // FILTER OUT the notification when the keyguard is showing and...
-            if (mKeyguardStateController.isShowing()) {
-                // ... user settings or the device policy manager doesn't allow lockscreen
-                // notifications;
-                if (!mLockscreenUserManager.shouldShowLockscreenNotifications()) {
-                    return true;
-                }
-
-                final int currUserId = mLockscreenUserManager.getCurrentUserId();
-                final int notifUserId = (sbn.getUser().getIdentifier() == UserHandle.USER_ALL)
-                        ? currUserId : sbn.getUser().getIdentifier();
-
-                // ... user is in lockdown
-                if (mKeyguardUpdateMonitor.isUserInLockdown(currUserId)
-                        || mKeyguardUpdateMonitor.isUserInLockdown(notifUserId)) {
-                    return true;
-                }
-
-                // ... device is in public mode and the user's settings doesn't allow
-                // notifications to show in public mode
-                if (mLockscreenUserManager.isLockscreenPublicMode(currUserId)
-                        || mLockscreenUserManager.isLockscreenPublicMode(notifUserId)) {
-                    if (entry.getRanking().getLockscreenVisibilityOverride() == VISIBILITY_SECRET) {
-                        return true;
-                    }
-
-                    if (!mLockscreenUserManager.userAllowsNotificationsInPublic(currUserId)
-                            || !mLockscreenUserManager.userAllowsNotificationsInPublic(
-                            notifUserId)) {
-                        return true;
-                    }
-                }
-
-                // ... neither this notification nor its group have high enough priority
-                // to be shown on the lockscreen
-                if (entry.getParent() != null) {
-                    final GroupEntry parent = entry.getParent();
-                    if (priorityExceedsLockscreenShowingThreshold(parent)) {
-                        return false;
-                    }
-                }
-                return !priorityExceedsLockscreenShowingThreshold(entry);
-            }
-            return false;
+            return mKeyguardNotificationVisibilityProvider.hideNotification(entry);
         }
     };
 
-    private boolean priorityExceedsLockscreenShowingThreshold(ListEntry entry) {
-        if (entry == null) {
-            return false;
-        }
-        if (mHideSilentNotificationsOnLockscreen) {
-            return mHighPriorityProvider.isHighPriority(entry);
-        } else {
-            return entry.getRepresentativeEntry() != null
-                    && !entry.getRepresentativeEntry().getRanking().isAmbient();
-        }
-    }
-
     // TODO(b/206118999): merge this class with SensitiveContentCoordinator which also depends on
     // these same updates
     private void setupInvalidateNotifListCallbacks() {
-        // register onKeyguardShowing callback
-        mKeyguardStateController.addCallback(mKeyguardCallback);
-        mKeyguardUpdateMonitor.registerCallback(mKeyguardUpdateCallback);
 
-        // register lockscreen settings changed callbacks:
-        final ContentObserver settingsObserver = new ContentObserver(mMainHandler) {
-            @Override
-            public void onChange(boolean selfChange, Uri uri) {
-                if (uri.equals(Settings.Secure.getUriFor(
-                        Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS))) {
-                    readShowSilentNotificationSetting();
-                }
-
-                if (mKeyguardStateController.isShowing()) {
-                    invalidateListFromFilter("Settings " + uri + " changed");
-                }
-            }
-        };
-
-        mContext.getContentResolver().registerContentObserver(
-                Settings.Secure.getUriFor(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS),
-                false,
-                settingsObserver,
-                UserHandle.USER_ALL);
-
-        mContext.getContentResolver().registerContentObserver(
-                Settings.Secure.getUriFor(Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS),
-                true,
-                settingsObserver,
-                UserHandle.USER_ALL);
-
-        mContext.getContentResolver().registerContentObserver(
-                Settings.Global.getUriFor(Settings.Global.ZEN_MODE),
-                false,
-                settingsObserver);
-
-        mContext.getContentResolver().registerContentObserver(
-                Settings.Secure.getUriFor(Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS),
-                false,
-                settingsObserver,
-                UserHandle.USER_ALL);
-
-        // register (maybe) public mode changed callbacks:
-        mStatusBarStateController.addCallback(mStatusBarStateListener);
-        mBroadcastDispatcher.registerReceiver(new BroadcastReceiver() {
-            @Override
-            public void onReceive(Context context, Intent intent) {
-                if (mKeyguardStateController.isShowing()) {
-                    // maybe public mode changed
-                    invalidateListFromFilter(intent.getAction());
-                }
-            }}, new IntentFilter(Intent.ACTION_USER_SWITCHED));
     }
 
     private void invalidateListFromFilter(String reason) {
@@ -228,49 +85,10 @@
         mNotifFilter.invalidateList();
     }
 
-    private void readShowSilentNotificationSetting() {
-        mHideSilentNotificationsOnLockscreen =
-                Settings.Secure.getInt(
-                        mContext.getContentResolver(),
-                        Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS,
-                        1) == 0;
-    }
-
     private void updateSectionHeadersVisibility() {
         boolean onKeyguard = mStatusBarStateController.getState() == StatusBarState.KEYGUARD;
         boolean neverShowSections = mSectionHeaderVisibilityProvider.getNeverShowSectionHeaders();
         boolean showSections = !onKeyguard && !neverShowSections;
         mSectionHeaderVisibilityProvider.setSectionHeadersVisible(showSections);
     }
-
-    private final KeyguardStateController.Callback mKeyguardCallback =
-            new KeyguardStateController.Callback() {
-        @Override
-        public void onUnlockedChanged() {
-            invalidateListFromFilter("onUnlockedChanged");
-        }
-
-        @Override
-        public void onKeyguardShowingChanged() {
-            invalidateListFromFilter("onKeyguardShowingChanged");
-        }
-    };
-
-    private final StatusBarStateController.StateListener mStatusBarStateListener =
-            new StatusBarStateController.StateListener() {
-                @Override
-                public void onStateChanged(int newState) {
-                    // maybe public mode changed
-                    invalidateListFromFilter("onStatusBarStateChanged");
-                }
-    };
-
-    private final KeyguardUpdateMonitorCallback mKeyguardUpdateCallback =
-            new KeyguardUpdateMonitorCallback() {
-        @Override
-        public void onStrongAuthStateChanged(int userId) {
-            // maybe lockdown mode changed
-            invalidateListFromFilter("onStrongAuthStateChanged");
-        }
-    };
 }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/legacy/NotificationGroupManagerLegacy.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/legacy/NotificationGroupManagerLegacy.java
index cd2affe..7c4e449 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/legacy/NotificationGroupManagerLegacy.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/legacy/NotificationGroupManagerLegacy.java
@@ -73,7 +73,7 @@
         GroupExpansionManager,
         Dumpable {
 
-    private static final String TAG = "NotifGroupManager";
+    private static final String TAG = "LegacyNotifGroupManager";
     private static final boolean DEBUG = Compile.IS_DEBUG && Log.isLoggable(TAG, Log.DEBUG);
     private static final boolean SPEW = Compile.IS_DEBUG && Log.isLoggable(TAG, Log.VERBOSE);
     /**
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/render/NodeSpecBuilder.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/render/NodeSpecBuilder.kt
index c35b77c..6db544c 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/render/NodeSpecBuilder.kt
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/render/NodeSpecBuilder.kt
@@ -57,6 +57,7 @@
 
         var currentSection: NotifSection? = null
         val prevSections = mutableSetOf<NotifSection?>()
+        var lastSection: NotifSection? = null
         val showHeaders = sectionHeaderVisibilityProvider.sectionHeadersVisible
         val sectionOrder = mutableListOf<NotifSection?>()
         val sectionHeaders = mutableMapOf<NotifSection?, NodeController?>()
@@ -65,6 +66,14 @@
         for (entry in notifList) {
             val section = entry.section!!
 
+            lastSection?.let {
+                if (it.bucket > section.bucket) {
+                    throw IllegalStateException("buildNodeSpec with non contiguous section " +
+                            "buckets ${it.sectioner.name} - ${it.bucket} & " +
+                            "${it.sectioner.name} - ${it.bucket}")
+                }
+            }
+            lastSection = section
             if (prevSections.contains(section)) {
                 throw java.lang.RuntimeException("Section ${section.label} has been duplicated")
             }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/interruption/KeyguardNotificationVisibilityProvider.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/interruption/KeyguardNotificationVisibilityProvider.kt
new file mode 100644
index 0000000..70c9a16
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/interruption/KeyguardNotificationVisibilityProvider.kt
@@ -0,0 +1,186 @@
+package com.android.systemui.statusbar.notification.interruption
+
+import android.app.Notification
+import android.content.BroadcastReceiver
+import android.content.Context
+import android.content.Intent
+import android.content.IntentFilter
+import android.database.ContentObserver
+import android.net.Uri
+import android.os.Handler
+import android.os.UserHandle
+import android.provider.Settings
+import com.android.keyguard.KeyguardUpdateMonitor
+import com.android.keyguard.KeyguardUpdateMonitorCallback
+import com.android.systemui.CoreStartable
+import com.android.systemui.broadcast.BroadcastDispatcher
+import com.android.systemui.dagger.qualifiers.Main
+import com.android.systemui.plugins.statusbar.StatusBarStateController
+import com.android.systemui.statusbar.NotificationLockscreenUserManager
+import com.android.systemui.statusbar.notification.collection.ListEntry
+import com.android.systemui.statusbar.notification.collection.NotificationEntry
+import com.android.systemui.statusbar.notification.collection.provider.HighPriorityProvider
+import com.android.systemui.statusbar.policy.KeyguardStateController
+import com.android.systemui.util.ListenerSet
+import java.util.function.Consumer
+import javax.inject.Inject
+
+/**
+ * Determines if notifications should be visible based on the state of the keyguard
+ */
+class KeyguardNotificationVisibilityProvider @Inject constructor(
+    context: Context,
+    @Main private val handler: Handler,
+    private val keyguardStateController: KeyguardStateController,
+    private val lockscreenUserManager: NotificationLockscreenUserManager,
+    private val keyguardUpdateMonitor: KeyguardUpdateMonitor,
+    private val highPriorityProvider: HighPriorityProvider,
+    private val statusBarStateController: StatusBarStateController,
+    private val broadcastDispatcher: BroadcastDispatcher
+) : CoreStartable(context) {
+    private val onStateChangedListeners = ListenerSet<Consumer<String>>()
+    private var hideSilentNotificationsOnLockscreen: Boolean = false
+
+    override fun start() {
+        readShowSilentNotificationSetting()
+        keyguardStateController.addCallback(object : KeyguardStateController.Callback {
+            override fun onUnlockedChanged() {
+                notifyStateChanged("onUnlockedChanged")
+            }
+
+            override fun onKeyguardShowingChanged() {
+                notifyStateChanged("onKeyguardShowingChanged")
+            }
+        })
+        keyguardUpdateMonitor.registerCallback(object : KeyguardUpdateMonitorCallback() {
+            override fun onStrongAuthStateChanged(userId: Int) {
+                notifyStateChanged("onStrongAuthStateChanged")
+            }
+        })
+
+        // register lockscreen settings changed callbacks:
+        val settingsObserver: ContentObserver = object : ContentObserver(handler) {
+            override fun onChange(selfChange: Boolean, uri: Uri) {
+                if (keyguardStateController.isShowing) {
+                    notifyStateChanged("Settings $uri changed")
+                }
+            }
+        }
+
+        mContext.contentResolver.registerContentObserver(
+                Settings.Secure.getUriFor(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS),
+                false,
+                settingsObserver,
+                UserHandle.USER_ALL)
+
+        mContext.contentResolver.registerContentObserver(
+                Settings.Secure.getUriFor(Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS),
+                true,
+                settingsObserver,
+                UserHandle.USER_ALL)
+
+        mContext.contentResolver.registerContentObserver(
+                Settings.Global.getUriFor(Settings.Global.ZEN_MODE),
+                false,
+                settingsObserver)
+
+        mContext.contentResolver.registerContentObserver(
+                Settings.Secure.getUriFor(Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS),
+                false,
+                settingsObserver,
+                UserHandle.USER_ALL)
+
+        // register (maybe) public mode changed callbacks:
+        statusBarStateController.addCallback(object : StatusBarStateController.StateListener {
+            override fun onStateChanged(state: Int) {
+                notifyStateChanged("onStatusBarStateChanged")
+            }
+        })
+        broadcastDispatcher.registerReceiver(object : BroadcastReceiver() {
+            override fun onReceive(context: Context, intent: Intent) {
+                if (keyguardStateController.isShowing()) {
+                    // maybe public mode changed
+                    notifyStateChanged(intent.action)
+                }
+            }
+        }, IntentFilter(Intent.ACTION_USER_SWITCHED))
+    }
+
+    fun addOnStateChangedListener(listener: Consumer<String>) {
+        onStateChangedListeners.addIfAbsent(listener)
+    }
+
+    fun removeOnStateChangedListener(listener: Consumer<String>) {
+        onStateChangedListeners.remove(listener)
+    }
+
+    private fun notifyStateChanged(reason: String) {
+        onStateChangedListeners.forEach({ it.accept(reason) })
+    }
+
+    /**
+     * Determines if the given notification should be hidden based on the current keyguard state.
+     * If Listener#onKeyguardStateChanged is invoked, the results of this method may no longer
+     * be valid, and so should be re-queried
+     */
+    fun hideNotification(entry: NotificationEntry): Boolean {
+        val sbn = entry.sbn
+        // FILTER OUT the notification when the keyguard is showing and...
+        if (keyguardStateController.isShowing()) {
+            // ... user settings or the device policy manager doesn't allow lockscreen
+            // notifications;
+            if (!lockscreenUserManager.shouldShowLockscreenNotifications()) {
+                return true
+            }
+            val currUserId: Int = lockscreenUserManager.getCurrentUserId()
+            val notifUserId =
+                    if (sbn.user.identifier == UserHandle.USER_ALL) currUserId
+                    else sbn.user.identifier
+
+            // ... user is in lockdown
+            if (keyguardUpdateMonitor.isUserInLockdown(currUserId) ||
+                    keyguardUpdateMonitor.isUserInLockdown(notifUserId)) {
+                return true
+            }
+
+            // ... device is in public mode and the user's settings doesn't allow
+            // notifications to show in public mode
+            if (lockscreenUserManager.isLockscreenPublicMode(currUserId) ||
+                    lockscreenUserManager.isLockscreenPublicMode(notifUserId)) {
+                if (entry.ranking.lockscreenVisibilityOverride == Notification.VISIBILITY_SECRET) {
+                    return true
+                }
+                if (!lockscreenUserManager.userAllowsNotificationsInPublic(currUserId) ||
+                        !lockscreenUserManager.userAllowsNotificationsInPublic(
+                                notifUserId)) {
+                    return true
+                }
+            }
+
+            // ... neither this notification nor its group have high enough priority
+            // to be shown on the lockscreen
+            if (entry.parent != null) {
+                val parent = entry.parent
+                if (priorityExceedsLockscreenShowingThreshold(parent)) {
+                    return false
+                }
+            }
+            return !priorityExceedsLockscreenShowingThreshold(entry)
+        }
+        return false
+    }
+
+    private fun priorityExceedsLockscreenShowingThreshold(entry: ListEntry?): Boolean =
+        when {
+            entry == null -> false
+            hideSilentNotificationsOnLockscreen -> highPriorityProvider.isHighPriority(entry)
+            else -> entry.representativeEntry?.ranking?.isAmbient == false
+        }
+
+    private fun readShowSilentNotificationSetting() {
+        hideSilentNotificationsOnLockscreen = Settings.Secure.getInt(
+                mContext.getContentResolver(),
+                Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS,
+                1) == 0
+    }
+}
\ No newline at end of file
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/interruption/NotificationInterruptLogger.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/interruption/NotificationInterruptLogger.kt
index c991376..6c99e3a 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/interruption/NotificationInterruptLogger.kt
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/interruption/NotificationInterruptLogger.kt
@@ -211,6 +211,14 @@
             "Pulsing: $str1"
         })
     }
+
+    fun keyguardHideNotification(key: String) {
+        hunBuffer.log(TAG, DEBUG, {
+            str1 = key
+        }, {
+            "Keyguard Hide Notification: $str1"
+        })
+    }
 }
 
 private const val TAG = "InterruptionStateProvider"
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/interruption/NotificationInterruptStateProviderImpl.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/interruption/NotificationInterruptStateProviderImpl.java
index 7ed2699..c1771cc 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/interruption/NotificationInterruptStateProviderImpl.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/interruption/NotificationInterruptStateProviderImpl.java
@@ -36,6 +36,7 @@
 import com.android.systemui.dagger.qualifiers.Main;
 import com.android.systemui.plugins.statusbar.StatusBarStateController;
 import com.android.systemui.statusbar.StatusBarState;
+import com.android.systemui.statusbar.notification.NotifPipelineFlags;
 import com.android.systemui.statusbar.notification.NotificationFilter;
 import com.android.systemui.statusbar.notification.collection.NotificationEntry;
 import com.android.systemui.statusbar.policy.BatteryController;
@@ -66,6 +67,8 @@
     private final ContentObserver mHeadsUpObserver;
     private final HeadsUpManager mHeadsUpManager;
     private final NotificationInterruptLogger mLogger;
+    private final NotifPipelineFlags mFlags;
+    private final KeyguardNotificationVisibilityProvider mKeyguardNotificationVisibilityProvider;
 
     @VisibleForTesting
     protected boolean mUseHeadsUp = false;
@@ -81,7 +84,9 @@
             StatusBarStateController statusBarStateController,
             HeadsUpManager headsUpManager,
             NotificationInterruptLogger logger,
-            @Main Handler mainHandler) {
+            @Main Handler mainHandler,
+            NotifPipelineFlags flags,
+            KeyguardNotificationVisibilityProvider keyguardNotificationVisibilityProvider) {
         mContentResolver = contentResolver;
         mPowerManager = powerManager;
         mDreamManager = dreamManager;
@@ -91,6 +96,8 @@
         mStatusBarStateController = statusBarStateController;
         mHeadsUpManager = headsUpManager;
         mLogger = logger;
+        mFlags = flags;
+        mKeyguardNotificationVisibilityProvider = keyguardNotificationVisibilityProvider;
         mHeadsUpObserver = new ContentObserver(mainHandler) {
             @Override
             public void onChange(boolean selfChange) {
@@ -282,7 +289,7 @@
     private boolean canAlertCommon(NotificationEntry entry) {
         StatusBarNotification sbn = entry.getSbn();
 
-        if (mNotificationFilter.shouldFilterOut(entry)) {
+        if (!mFlags.isNewPipelineEnabled() && mNotificationFilter.shouldFilterOut(entry)) {
             mLogger.logNoAlertingFilteredOut(sbn);
             return false;
         }
@@ -305,6 +312,11 @@
             return false;
         }
 
+        if (mKeyguardNotificationVisibilityProvider.hideNotification(entry)) {
+            mLogger.keyguardHideNotification(entry.getKey());
+            return false;
+        }
+
         return true;
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/row/NotificationContentView.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/row/NotificationContentView.java
index 9cb5dc5..adb4ce6 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/row/NotificationContentView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/row/NotificationContentView.java
@@ -413,7 +413,10 @@
             if (mExpandedRemoteInput != null) {
                 mExpandedRemoteInput.onNotificationUpdateOrReset();
                 if (mExpandedRemoteInput.isActive()) {
-                    mPreviousExpandedRemoteInputIntent = mExpandedRemoteInput.getPendingIntent();
+                    if (mExpandedRemoteInputController != null) {
+                        mPreviousExpandedRemoteInputIntent =
+                                mExpandedRemoteInputController.getPendingIntent();
+                    }
                     mCachedExpandedRemoteInput = mExpandedRemoteInput;
                     mCachedExpandedRemoteInputViewController = mExpandedRemoteInputController;
                     mExpandedRemoteInput.dispatchStartTemporaryDetach();
@@ -460,7 +463,10 @@
             if (mHeadsUpRemoteInput != null) {
                 mHeadsUpRemoteInput.onNotificationUpdateOrReset();
                 if (mHeadsUpRemoteInput.isActive()) {
-                    mPreviousHeadsUpRemoteInputIntent = mHeadsUpRemoteInput.getPendingIntent();
+                    if (mHeadsUpRemoteInputController != null) {
+                        mPreviousHeadsUpRemoteInputIntent =
+                                mHeadsUpRemoteInputController.getPendingIntent();
+                    }
                     mCachedHeadsUpRemoteInput = mHeadsUpRemoteInput;
                     mCachedHeadsUpRemoteInputViewController = mHeadsUpRemoteInputController;
                     mHeadsUpRemoteInput.dispatchStartTemporaryDetach();
@@ -961,14 +967,16 @@
 
     private void transferRemoteInputFocus(int visibleType) {
         if (visibleType == VISIBLE_TYPE_HEADSUP
-                && mHeadsUpRemoteInput != null
-                && (mExpandedRemoteInput != null && mExpandedRemoteInput.isActive())) {
-            mHeadsUpRemoteInput.stealFocusFrom(mExpandedRemoteInput);
+                && mHeadsUpRemoteInputController != null
+                && mExpandedRemoteInputController != null
+                && mExpandedRemoteInputController.isActive()) {
+            mHeadsUpRemoteInputController.stealFocusFrom(mExpandedRemoteInputController);
         }
         if (visibleType == VISIBLE_TYPE_EXPANDED
-                && mExpandedRemoteInput != null
-                && (mHeadsUpRemoteInput != null && mHeadsUpRemoteInput.isActive())) {
-            mExpandedRemoteInput.stealFocusFrom(mHeadsUpRemoteInput);
+                && mExpandedRemoteInputController != null
+                && mHeadsUpRemoteInputController != null
+                && mHeadsUpRemoteInputController.isActive()) {
+            mExpandedRemoteInputController.stealFocusFrom(mHeadsUpRemoteInputController);
         }
     }
 
@@ -1313,7 +1321,6 @@
                     // If we find a matching action in the new notification, focus, otherwise close.
                     Notification.Action[] actions = entry.getSbn().getNotification().actions;
                     if (existingPendingIntent != null) {
-                        result.mView.setPendingIntent(existingPendingIntent);
                         result.mController.setPendingIntent(existingPendingIntent);
                     }
                     if (result.mController.updatePendingIntentFromActions(actions)) {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/NotificationStackScrollLayout.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/NotificationStackScrollLayout.java
index 5b9dbd0..27cc326 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/NotificationStackScrollLayout.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/NotificationStackScrollLayout.java
@@ -758,29 +758,30 @@
             mDebugTextUsedYPositions.clear();
         }
         int y = mTopPadding;
-        drawDebugInfo(canvas, y, Color.RED, /* label= */ "mTopPadding");
+        drawDebugInfo(canvas, y, Color.RED, /* label= */ "mTopPadding = "+y);
 
         y = getLayoutHeight();
-        drawDebugInfo(canvas, y, Color.YELLOW, /* label= */ "getLayoutHeight()");
+        drawDebugInfo(canvas, y, Color.YELLOW, /* label= */ "getLayoutHeight() = "+y);
 
         y = (int) mMaxLayoutHeight;
-        drawDebugInfo(canvas, y, Color.MAGENTA, /* label= */ "mMaxLayoutHeight");
+        drawDebugInfo(canvas, y, Color.MAGENTA, /* label= */ "mMaxLayoutHeight = "+y);
 
         if (mKeyguardBottomPadding >= 0) {
             y = getHeight() - (int) mKeyguardBottomPadding;
             drawDebugInfo(canvas, y, Color.GRAY,
-                    /* label= */ "getHeight() - mKeyguardBottomPadding");
+                    /* label= */ "getHeight() - mKeyguardBottomPadding = "+y);
         }
 
         y = getHeight() - getEmptyBottomMargin();
-        drawDebugInfo(canvas, y, Color.GREEN, /* label= */ "getHeight() - getEmptyBottomMargin()");
+        drawDebugInfo(canvas, y, Color.GREEN,
+                /* label= */ "getHeight() - getEmptyBottomMargin() = "+y);
 
         y = (int) (mAmbientState.getStackY());
-        drawDebugInfo(canvas, y, Color.CYAN, /* label= */ "mAmbientState.getStackY()");
+        drawDebugInfo(canvas, y, Color.CYAN, /* label= */ "mAmbientState.getStackY() = "+y);
 
         y = (int) (mAmbientState.getStackY() + mAmbientState.getStackHeight());
         drawDebugInfo(canvas, y, Color.BLUE,
-                /* label= */ "mAmbientState.getStackY() + mAmbientState.getStackHeight()");
+                /* label= */ "mAmbientState.getStackY() + mAmbientState.getStackHeight() = "+y);
     }
 
     private void drawDebugInfo(Canvas canvas, int y, int color, String label) {
@@ -1332,12 +1333,7 @@
      */
     @ShadeViewRefactor(RefactorComponent.COORDINATOR)
     public void setExpandedHeight(float height) {
-        final float shadeBottom = getHeight() - getEmptyBottomMargin();
         final boolean skipHeightUpdate = shouldSkipHeightUpdate();
-        if (!skipHeightUpdate) {
-            final float expansionFraction = MathUtils.saturate(height / shadeBottom);
-            mAmbientState.setExpansionFraction(expansionFraction);
-        }
         updateStackPosition();
 
         if (!skipHeightUpdate) {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/NotificationStackScrollLayoutController.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/NotificationStackScrollLayoutController.java
index 7df8e7d..6bbecc8 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/NotificationStackScrollLayoutController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/NotificationStackScrollLayoutController.java
@@ -1233,10 +1233,6 @@
         mView.forceNoOverlappingRendering(force);
     }
 
-    public void setTranslationX(float translation) {
-        mView.setTranslationX(translation);
-    }
-
     public void setExpandingVelocity(float velocity) {
         mView.setExpandingVelocity(velocity);
     }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/StackScrollAlgorithm.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/StackScrollAlgorithm.java
index 952cd9a..e1f8c35 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/StackScrollAlgorithm.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/StackScrollAlgorithm.java
@@ -202,12 +202,10 @@
             float newHeight = state.height;
             float newNotificationEnd = newYTranslation + newHeight;
             boolean isHeadsUp = (child instanceof ExpandableNotificationRow) && child.isPinned();
-            final boolean shadeClosedWithHUN =
-                    ambientState.isShadeOpening() && !ambientState.isShadeExpanded();
             if (mClipNotificationScrollToTop
                     && (!state.inShelf || (isHeadsUp && !firstHeadsUp))
                     && newYTranslation < clipStart
-                    && shadeClosedWithHUN) {
+                    && !ambientState.isShadeExpanded()) {
                 // The previous view is overlapping on top, clip!
                 float overlapAmount = clipStart - newYTranslation;
                 state.clipTopAmount = (int) overlapAmount;
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/CentralSurfaces.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/CentralSurfaces.java
index 84adc56..ec2d608 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/CentralSurfaces.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/CentralSurfaces.java
@@ -663,7 +663,6 @@
     protected final BatteryController mBatteryController;
     protected boolean mPanelExpanded;
     private UiModeManager mUiModeManager;
-    protected boolean mIsKeyguard;
     private LogMaker mStatusBarStateLog;
     protected final NotificationIconAreaController mNotificationIconAreaController;
     @Nullable private View mAmbientIndicationContainer;
@@ -1142,7 +1141,7 @@
         }
         if (leaveOpen) {
             mStatusBarStateController.setLeaveOpenOnKeyguardHide(true);
-            if (mIsKeyguard) {
+            if (mKeyguardStateController.isShowing()) {
                 // When device state changes on keyguard we don't want to keep the state of
                 // the shade and instead we open clean state of keyguard with shade closed.
                 // Normally some parts of QS state (like expanded/collapsed) are persisted and
@@ -2882,16 +2881,17 @@
     }
 
     boolean updateIsKeyguard(boolean forceStateChange) {
-        boolean wakeAndUnlocking = mBiometricUnlockController.getMode()
-                == BiometricUnlockController.MODE_WAKE_AND_UNLOCK;
+        boolean wakeAndUnlocking = mBiometricUnlockController.isWakeAndUnlock();
 
         // For dozing, keyguard needs to be shown whenever the device is non-interactive. Otherwise
         // there's no surface we can show to the user. Note that the device goes fully interactive
         // late in the transition, so we also allow the device to start dozing once the screen has
         // turned off fully.
         boolean keyguardForDozing = mDozeServiceHost.getDozingRequested()
-                && (!mDeviceInteractive || isGoingToSleep() && (isScreenFullyOff() || mIsKeyguard));
-        boolean isWakingAndOccluded = isOccluded() && isWaking();
+                && (!mDeviceInteractive || (isGoingToSleep()
+                    && (isScreenFullyOff()
+                        || (mKeyguardStateController.isShowing() && !isOccluded()))));
+        boolean isWakingAndOccluded = isOccluded() && isWakingOrAwake();
         boolean shouldBeKeyguard = (mStatusBarStateController.isKeyguardRequested()
                 || keyguardForDozing) && !wakeAndUnlocking && !isWakingAndOccluded;
         if (keyguardForDozing) {
@@ -2923,7 +2923,6 @@
 
     public void showKeyguardImpl() {
         Trace.beginSection("CentralSurfaces#showKeyguard");
-        mIsKeyguard = true;
         // In case we're locking while a smartspace transition is in progress, reset it.
         mKeyguardUnlockAnimationController.resetSmartspaceTransition();
         if (mKeyguardStateController.isLaunchTransitionFadingAway()) {
@@ -3044,7 +3043,6 @@
      * @return true if we would like to stay in the shade, false if it should go away entirely
      */
     public boolean hideKeyguardImpl(boolean forceStateChange) {
-        mIsKeyguard = false;
         Trace.beginSection("CentralSurfaces#hideKeyguard");
         boolean staying = mStatusBarStateController.leaveOpenOnKeyguardHide();
         int previousState = mStatusBarStateController.getState();
@@ -3077,7 +3075,6 @@
         mMessageRouter.cancelMessages(MSG_LAUNCH_TRANSITION_TIMEOUT);
         releaseGestureWakeLock();
         mNotificationPanelViewController.onAffordanceLaunchEnded();
-        mNotificationPanelViewController.cancelAnimation();
         mNotificationPanelViewController.resetAlpha();
         mNotificationPanelViewController.resetTranslation();
         mNotificationPanelViewController.resetViewGroupFade();
@@ -3703,8 +3700,9 @@
                 == WakefulnessLifecycle.WAKEFULNESS_GOING_TO_SLEEP;
     }
 
-    boolean isWaking() {
-        return mWakefulnessLifecycle.getWakefulness() == WakefulnessLifecycle.WAKEFULNESS_WAKING;
+    boolean isWakingOrAwake() {
+        return mWakefulnessLifecycle.getWakefulness() == WakefulnessLifecycle.WAKEFULNESS_WAKING
+                || mWakefulnessLifecycle.getWakefulness() == WakefulnessLifecycle.WAKEFULNESS_AWAKE;
     }
 
     public void notifyBiometricAuthModeChanged() {
@@ -3771,7 +3769,7 @@
             });
         } else if (mDozing && !unlocking) {
             mScrimController.transitionTo(ScrimState.AOD);
-        } else if (mIsKeyguard && !unlocking) {
+        } else if (mKeyguardStateController.isShowing() && !isOccluded() && !unlocking) {
             mScrimController.transitionTo(ScrimState.KEYGUARD);
         } else {
             mScrimController.transitionTo(ScrimState.UNLOCKED, mUnlockScrimCallback);
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardBottomAreaView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardBottomAreaView.java
index dc1af36..7f1611f 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardBottomAreaView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardBottomAreaView.java
@@ -555,6 +555,7 @@
         mControlsButton.setImageResource(mControlsComponent.getTileImageId());
         mControlsButton.setContentDescription(getContext()
                 .getString(mControlsComponent.getTileTitleId()));
+        updateAffordanceColors();
 
         boolean hasFavorites = mControlsComponent.getControlsController()
                 .map(c -> c.getFavorites().size() > 0)
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationPanelViewController.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationPanelViewController.java
index cc8a703..1891ab0 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationPanelViewController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationPanelViewController.java
@@ -18,13 +18,9 @@
 
 import static android.app.StatusBarManager.WINDOW_STATE_SHOWING;
 import static android.view.View.GONE;
-import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
 
-import static androidx.constraintlayout.widget.ConstraintSet.BOTTOM;
 import static androidx.constraintlayout.widget.ConstraintSet.END;
 import static androidx.constraintlayout.widget.ConstraintSet.PARENT_ID;
-import static androidx.constraintlayout.widget.ConstraintSet.START;
-import static androidx.constraintlayout.widget.ConstraintSet.TOP;
 
 import static com.android.internal.jank.InteractionJankMonitor.CUJ_NOTIFICATION_SHADE_QS_EXPAND_COLLAPSE;
 import static com.android.keyguard.KeyguardClockSwitch.LARGE;
@@ -620,6 +616,11 @@
      */
     private float mKeyguardOnlyContentAlpha = 1.0f;
 
+    /**
+     * The translationY of the views which only show on the keyguard but in shade / shade locked.
+     */
+    private int mKeyguardOnlyTransitionTranslationY = 0;
+
     private float mUdfpsMaxYBurnInOffset;
 
     /**
@@ -1080,14 +1081,10 @@
 
     public void updateResources() {
         mQuickQsOffsetHeight = SystemBarUtils.getQuickQsOffsetHeight(mView.getContext());
-        mSplitShadeStatusBarHeight = Utils.getSplitShadeStatusBarHeight(mView.getContext());
         mSplitShadeNotificationsScrimMarginBottom =
                 mResources.getDimensionPixelSize(
                         R.dimen.split_shade_notifications_scrim_margin_bottom);
 
-        int panelMarginHorizontal = mResources.getDimensionPixelSize(
-                R.dimen.notification_panel_margin_horizontal);
-
         final boolean newShouldUseSplitNotificationShade =
                 Utils.shouldUseSplitNotificationShade(mResources);
         final boolean splitNotificationShadeChanged =
@@ -1097,49 +1094,12 @@
         if (mQs != null) {
             mQs.setInSplitShade(mShouldUseSplitNotificationShade);
         }
-
-        int notificationsBottomMargin = mResources.getDimensionPixelSize(
-                R.dimen.notification_panel_margin_bottom);
+        mSplitShadeStatusBarHeight = Utils.getSplitShadeStatusBarHeight(mView.getContext());
         int topMargin = mShouldUseSplitNotificationShade ? mSplitShadeStatusBarHeight :
                 mResources.getDimensionPixelSize(R.dimen.notification_panel_margin_top);
         mSplitShadeHeaderController.setSplitShadeMode(mShouldUseSplitNotificationShade);
-
-        // To change the constraints at runtime, all children of the ConstraintLayout must have ids
-        ensureAllViewsHaveIds(mNotificationContainerParent);
-        ConstraintSet constraintSet = new ConstraintSet();
-        constraintSet.clone(mNotificationContainerParent);
-        int statusViewMarginHorizontal = mResources.getDimensionPixelSize(
-                R.dimen.status_view_margin_horizontal);
-        constraintSet.setMargin(R.id.keyguard_status_view, START, statusViewMarginHorizontal);
-        constraintSet.setMargin(R.id.keyguard_status_view, END, statusViewMarginHorizontal);
-        if (mShouldUseSplitNotificationShade) {
-            // width = 0 to take up all available space within constraints
-            constraintSet.connect(R.id.qs_frame, END, R.id.qs_edge_guideline, END);
-            constraintSet.connect(
-                    R.id.notification_stack_scroller, START,
-                    R.id.qs_edge_guideline, START);
-            constraintSet.constrainHeight(R.id.split_shade_status_bar, mSplitShadeStatusBarHeight);
-        } else {
-            constraintSet.connect(R.id.qs_frame, END, PARENT_ID, END);
-            constraintSet.connect(R.id.notification_stack_scroller, START, PARENT_ID, START);
-            if (mUseCombinedQSHeaders) {
-                constraintSet.constrainHeight(R.id.split_shade_status_bar, WRAP_CONTENT);
-            }
-        }
-        constraintSet.setMargin(R.id.notification_stack_scroller, START,
-                mShouldUseSplitNotificationShade ? 0 : panelMarginHorizontal);
-        constraintSet.setMargin(R.id.notification_stack_scroller, END, panelMarginHorizontal);
-        constraintSet.setMargin(R.id.notification_stack_scroller, TOP, topMargin);
-        constraintSet.setMargin(R.id.notification_stack_scroller, BOTTOM,
-                notificationsBottomMargin);
-        constraintSet.setMargin(R.id.qs_frame, START, panelMarginHorizontal);
-        constraintSet.setMargin(R.id.qs_frame, END,
-                mShouldUseSplitNotificationShade ? 0 : panelMarginHorizontal);
-        constraintSet.setMargin(R.id.qs_frame, TOP, topMargin);
-        constraintSet.applyTo(mNotificationContainerParent);
         mAmbientState.setStackTopMargin(topMargin);
-        mNotificationsQSContainerController.updateMargins();
-        mNotificationsQSContainerController.setSplitShadeEnabled(mShouldUseSplitNotificationShade);
+        mNotificationsQSContainerController.updateResources();
 
         updateKeyguardStatusViewAlignment(/* animate= */false);
 
@@ -1150,15 +1110,6 @@
         }
     }
 
-    private static void ensureAllViewsHaveIds(ViewGroup parentView) {
-        for (int i = 0; i < parentView.getChildCount(); i++) {
-            View childView = parentView.getChildAt(i);
-            if (childView.getId() == View.NO_ID) {
-                childView.setId(View.generateViewId());
-            }
-        }
-    }
-
     private View reInflateStub(int viewId, int stubId, int layoutId, boolean enabled) {
         View view = mView.findViewById(viewId);
         if (view != null) {
@@ -1629,6 +1580,8 @@
     private void updateClock() {
         float alpha = mClockPositionResult.clockAlpha * mKeyguardOnlyContentAlpha;
         mKeyguardStatusViewController.setAlpha(alpha);
+        mKeyguardStatusViewController
+                .setTranslationYExcludingMedia(mKeyguardOnlyTransitionTranslationY);
         if (mKeyguardQsUserSwitchController != null) {
             mKeyguardQsUserSwitchController.setAlpha(alpha);
         }
@@ -2786,11 +2739,12 @@
     }
 
     /**
-     * Set the alpha of the keyguard elements which only show on the lockscreen, but not in
-     * shade locked / shade. This is used when dragging down to the full shade.
+     * Set the alpha and translationY of the keyguard elements which only show on the lockscreen,
+     * but not in shade locked / shade. This is used when dragging down to the full shade.
      */
-    public void setKeyguardOnlyContentAlpha(float keyguardAlpha) {
+    public void setKeyguardTransitionProgress(float keyguardAlpha, int keyguardTranslationY) {
         mKeyguardOnlyContentAlpha = Interpolators.ALPHA_IN.getInterpolation(keyguardAlpha);
+        mKeyguardOnlyTransitionTranslationY = keyguardTranslationY;
         if (mBarState == KEYGUARD) {
             // If the animator is running, it's already fading out the content and this is a reset
             mBottomAreaShadeAlpha = mKeyguardOnlyContentAlpha;
@@ -3055,9 +3009,7 @@
     }
 
     private int calculatePanelHeightShade() {
-        int emptyBottomMargin = mNotificationStackScrollLayoutController.getEmptyBottomMargin();
-        int maxHeight = mNotificationStackScrollLayoutController.getHeight() - emptyBottomMargin;
-
+        final int maxHeight = mNotificationStackScrollLayoutController.getHeight();
         if (mBarState == KEYGUARD) {
             int minKeyguardPanelBottom = mClockPositionAlgorithm.getLockscreenStatusViewHeight()
                     + mNotificationStackScrollLayoutController.getIntrinsicContentHeight();
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationShadeWindowControllerImpl.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationShadeWindowControllerImpl.java
index 0ff010a..16e5732 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationShadeWindowControllerImpl.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationShadeWindowControllerImpl.java
@@ -386,10 +386,12 @@
             }
             visible = true;
         }
-        if (visible) {
-            mNotificationShadeView.setVisibility(View.VISIBLE);
-        } else {
-            mNotificationShadeView.setVisibility(View.INVISIBLE);
+        if (mNotificationShadeView != null) {
+            if (visible) {
+                mNotificationShadeView.setVisibility(View.VISIBLE);
+            } else {
+                mNotificationShadeView.setVisibility(View.INVISIBLE);
+            }
         }
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationsQSContainerController.kt b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationsQSContainerController.kt
index ebb09b1..7764d338 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationsQSContainerController.kt
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationsQSContainerController.kt
@@ -1,6 +1,15 @@
 package com.android.systemui.statusbar.phone
 
+import android.view.View
+import android.view.ViewGroup
+import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
 import android.view.WindowInsets
+import androidx.constraintlayout.widget.ConstraintSet
+import androidx.constraintlayout.widget.ConstraintSet.BOTTOM
+import androidx.constraintlayout.widget.ConstraintSet.END
+import androidx.constraintlayout.widget.ConstraintSet.PARENT_ID
+import androidx.constraintlayout.widget.ConstraintSet.START
+import androidx.constraintlayout.widget.ConstraintSet.TOP
 import com.android.systemui.R
 import com.android.systemui.flags.FeatureFlags
 import com.android.systemui.flags.Flags
@@ -10,6 +19,7 @@
 import com.android.systemui.recents.OverviewProxyService
 import com.android.systemui.recents.OverviewProxyService.OverviewProxyListener
 import com.android.systemui.shared.system.QuickStepContract
+import com.android.systemui.util.Utils
 import com.android.systemui.util.ViewController
 import java.util.function.Consumer
 import javax.inject.Inject
@@ -28,23 +38,20 @@
                 mView.invalidate()
             }
         }
-    var splitShadeEnabled = false
-        set(value) {
-            if (field != value) {
-                field = value
-                // in case device configuration changed while showing QS details/customizer
-                updateBottomSpacing()
-            }
-        }
-
+    private var splitShadeEnabled = false
     private var isQSDetailShowing = false
     private var isQSCustomizing = false
     private var isQSCustomizerAnimating = false
 
+    private var splitShadeStatusBarHeight = 0
     private var notificationsBottomMargin = 0
     private var scrimShadeBottomMargin = 0
     private var bottomStableInsets = 0
     private var bottomCutoutInsets = 0
+    private var panelMarginHorizontal = 0
+    private var topMargin = 0
+
+    private val useCombinedQSHeaders = featureFlags.isEnabled(Flags.COMBINED_QS_HEADERS)
 
     private var isGestureNavigation = true
     private var taskbarVisible = false
@@ -68,7 +75,6 @@
     }
 
     public override fun onViewAttached() {
-        updateMargins()
         updateResources()
         overviewProxyService.addCallback(taskbarVisibilityListener)
         mView.setInsetsChangedListener(windowInsetsListener)
@@ -83,7 +89,27 @@
         mView.setConfigurationChangedListener(null)
     }
 
-    private fun updateResources() {
+    fun updateResources() {
+        val newSplitShadeEnabled = Utils.shouldUseSplitNotificationShade(resources)
+        val splitShadeEnabledChanged = newSplitShadeEnabled != splitShadeEnabled
+        splitShadeEnabled = newSplitShadeEnabled
+        notificationsBottomMargin = resources.getDimensionPixelSize(
+                R.dimen.notification_panel_margin_bottom)
+        splitShadeStatusBarHeight = Utils.getSplitShadeStatusBarHeight(context)
+        panelMarginHorizontal = resources.getDimensionPixelSize(
+                R.dimen.notification_panel_margin_horizontal)
+        topMargin = if (splitShadeEnabled) {
+            splitShadeStatusBarHeight
+        } else {
+            resources.getDimensionPixelSize(R.dimen.notification_panel_margin_top)
+        }
+        updateConstraints()
+        if (splitShadeEnabledChanged) {
+            // Let's do it at the end when all margins/paddings were already applied.
+            // We need to updateBottomSpacing() in case device configuration changed while showing
+            // QS details/customizer
+            updateBottomSpacing()
+        }
         val previousScrimShadeBottomMargin = scrimShadeBottomMargin
         scrimShadeBottomMargin = resources.getDimensionPixelSize(
             R.dimen.split_shade_notifications_scrim_margin_bottom
@@ -94,15 +120,6 @@
         }
     }
 
-    /**
-     * Update the notification bottom margin.
-     *
-     * Will not call updateBottomSpacing
-     */
-    fun updateMargins() {
-        notificationsBottomMargin = mView.defaultNotificationsMarginBottom
-    }
-
     override fun setCustomizerAnimating(animating: Boolean) {
         if (isQSCustomizerAnimating != animating) {
             isQSCustomizerAnimating = animating
@@ -178,4 +195,66 @@
         }
         return containerPadding to stackScrollMargin
     }
-}
\ No newline at end of file
+
+    fun updateConstraints() {
+        // To change the constraints at runtime, all children of the ConstraintLayout must have ids
+        ensureAllViewsHaveIds(mView)
+        val constraintSet = ConstraintSet()
+        constraintSet.clone(mView)
+        setKeyguardStatusViewConstraints(constraintSet)
+        setQsConstraints(constraintSet)
+        setNotificationsConstraints(constraintSet)
+        setSplitShadeStatusBarConstraints(constraintSet)
+        mView.applyConstraints(constraintSet)
+    }
+
+    private fun setSplitShadeStatusBarConstraints(constraintSet: ConstraintSet) {
+        if (splitShadeEnabled) {
+            constraintSet.constrainHeight(R.id.split_shade_status_bar, splitShadeStatusBarHeight)
+        } else {
+            if (useCombinedQSHeaders) {
+                constraintSet.constrainHeight(R.id.split_shade_status_bar, WRAP_CONTENT)
+            }
+        }
+    }
+
+    private fun setNotificationsConstraints(constraintSet: ConstraintSet) {
+        val startConstraintId = if (splitShadeEnabled) R.id.qs_edge_guideline else PARENT_ID
+        constraintSet.apply {
+            connect(R.id.notification_stack_scroller, START, startConstraintId, START)
+            setMargin(R.id.notification_stack_scroller, START,
+                    if (splitShadeEnabled) 0 else panelMarginHorizontal)
+            setMargin(R.id.notification_stack_scroller, END, panelMarginHorizontal)
+            setMargin(R.id.notification_stack_scroller, TOP, topMargin)
+            setMargin(R.id.notification_stack_scroller, BOTTOM, notificationsBottomMargin)
+        }
+    }
+
+    private fun setQsConstraints(constraintSet: ConstraintSet) {
+        val endConstraintId = if (splitShadeEnabled) R.id.qs_edge_guideline else PARENT_ID
+        constraintSet.apply {
+            connect(R.id.qs_frame, END, endConstraintId, END)
+            setMargin(R.id.qs_frame, START, if (splitShadeEnabled) 0 else panelMarginHorizontal)
+            setMargin(R.id.qs_frame, END, if (splitShadeEnabled) 0 else panelMarginHorizontal)
+            setMargin(R.id.qs_frame, TOP, topMargin)
+        }
+    }
+
+    private fun setKeyguardStatusViewConstraints(constraintSet: ConstraintSet) {
+        val statusViewMarginHorizontal = resources.getDimensionPixelSize(
+                R.dimen.status_view_margin_horizontal)
+        constraintSet.apply {
+            setMargin(R.id.keyguard_status_view, START, statusViewMarginHorizontal)
+            setMargin(R.id.keyguard_status_view, END, statusViewMarginHorizontal)
+        }
+    }
+
+    private fun ensureAllViewsHaveIds(parentView: ViewGroup) {
+        for (i in 0 until parentView.childCount) {
+            val childView = parentView.getChildAt(i)
+            if (childView.id == View.NO_ID) {
+                childView.id = View.generateViewId()
+            }
+        }
+    }
+}
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationsQuickSettingsContainer.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationsQuickSettingsContainer.java
index c2b5f56..7caea06 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationsQuickSettingsContainer.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationsQuickSettingsContainer.java
@@ -26,6 +26,7 @@
 
 import androidx.annotation.Nullable;
 import androidx.constraintlayout.widget.ConstraintLayout;
+import androidx.constraintlayout.widget.ConstraintSet;
 
 import com.android.systemui.R;
 import com.android.systemui.fragments.FragmentHostManager;
@@ -123,10 +124,6 @@
         }
     }
 
-    public int getDefaultNotificationsMarginBottom() {
-        return ((LayoutParams) mStackScroller.getLayoutParams()).bottomMargin;
-    }
-
     public void setInsetsChangedListener(Consumer<WindowInsets> onInsetsChangedListener) {
         mInsetsChangedListener = onInsetsChangedListener;
     }
@@ -197,4 +194,7 @@
         }
     }
 
+    public void applyConstraints(ConstraintSet constraintSet) {
+        constraintSet.applyTo(this);
+    }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PanelViewController.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PanelViewController.java
index 1d560c4..24f5ff8 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PanelViewController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PanelViewController.java
@@ -382,7 +382,7 @@
 
     protected void startExpandMotion(float newX, float newY, boolean startTracking,
             float expandedHeight) {
-        if (!mHandlingPointerUp) {
+        if (!mHandlingPointerUp && !mStatusBarStateController.isDozing()) {
             beginJankMonitoring(CUJ_NOTIFICATION_SHADE_EXPAND_COLLAPSE);
         }
         mInitialOffsetOnTouch = expandedHeight;
@@ -654,7 +654,9 @@
 
             @Override
             public void onAnimationStart(Animator animation) {
-                beginJankMonitoring(CUJ_NOTIFICATION_SHADE_EXPAND_COLLAPSE);
+                if (!mStatusBarStateController.isDozing()) {
+                    beginJankMonitoring(CUJ_NOTIFICATION_SHADE_EXPAND_COLLAPSE);
+                }
             }
 
             @Override
@@ -794,6 +796,7 @@
             }
             mExpandedFraction = Math.min(1f,
                     maxPanelHeight == 0 ? 0 : mExpandedHeight / maxPanelHeight);
+            mAmbientState.setExpansionFraction(mExpandedFraction);
             onHeightUpdated(mExpandedHeight);
             updatePanelExpansionAndVisibility();
         });
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/ScrimController.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/ScrimController.java
index a3c795f..419661b7 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/ScrimController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/ScrimController.java
@@ -773,7 +773,7 @@
             }
             if (mUnOcclusionAnimationRunning && mState == ScrimState.KEYGUARD) {
                 // We're unoccluding the keyguard and don't want to have a bright flash.
-                mNotificationsAlpha = mScrimBehindAlphaKeyguard;
+                mNotificationsAlpha = ScrimState.KEYGUARD.getNotifAlpha();
                 mNotificationsTint = ScrimState.KEYGUARD.getNotifTint();
             }
         }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarNotificationActivityStarter.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarNotificationActivityStarter.java
index 108d98a..637e4be 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarNotificationActivityStarter.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarNotificationActivityStarter.java
@@ -370,6 +370,8 @@
             mLogger.logExpandingBubble(notificationKey);
             removeHunAfterClick(row);
             expandBubbleStackOnMainThread(entry);
+            mMainThreadHandler.post(
+                    () -> mLaunchEventsEmitter.notifyFinishLaunchNotifActivity(entry));
         } else {
             startNotificationIntent(intent, fillInIntent, entry, row, animate, isActivityIntent);
         }
@@ -395,7 +397,6 @@
             mMainThreadHandler.post(() -> {
                 final Runnable removeNotification = () -> {
                     mOnUserInteractionCallback.onDismiss(entry, REASON_CLICK, summaryToRemove);
-                    mLaunchEventsEmitter.notifyFinishLaunchNotifActivity(entry);
                 };
                 if (mPresenter.isCollapsing()) {
                     // To avoid lags we're only performing the remove
@@ -405,9 +406,6 @@
                     removeNotification.run();
                 }
             });
-        } else {
-            mMainThreadHandler.post(
-                    () -> mLaunchEventsEmitter.notifyFinishLaunchNotifActivity(entry));
         }
 
         mIsCollapsingToShowActivityOverLockscreen = false;
@@ -483,14 +481,19 @@
             boolean isActivityIntent) {
         mLogger.logStartNotificationIntent(entry.getKey(), intent);
         try {
+            Runnable onFinishAnimationCallback =
+                    () -> mLaunchEventsEmitter.notifyFinishLaunchNotifActivity(entry);
             ActivityLaunchAnimator.Controller animationController =
                     new StatusBarLaunchAnimatorController(
-                            mNotificationAnimationProvider.getAnimatorController(row),
+                            mNotificationAnimationProvider
+                                    .getAnimatorController(row, onFinishAnimationCallback),
                             mCentralSurfaces,
                             isActivityIntent);
-
-            mActivityLaunchAnimator.startPendingIntentWithAnimation(animationController,
-                    animate, intent.getCreatorPackage(), (adapter) -> {
+            mActivityLaunchAnimator.startPendingIntentWithAnimation(
+                    animationController,
+                    animate,
+                    intent.getCreatorPackage(),
+                    (adapter) -> {
                         long eventTime = row.getAndResetLastActionUpTime();
                         Bundle options = eventTime > 0
                                 ? getActivityOptions(
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/FlashlightControllerImpl.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/FlashlightControllerImpl.java
index ad47e2b..01fe865 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/FlashlightControllerImpl.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/FlashlightControllerImpl.java
@@ -16,6 +16,7 @@
 
 package com.android.systemui.statusbar.policy;
 
+import android.annotation.WorkerThread;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageManager;
@@ -219,6 +220,7 @@
             new CameraManager.TorchCallback() {
 
         @Override
+        @WorkerThread
         public void onTorchModeUnavailable(String cameraId) {
             if (TextUtils.equals(cameraId, mCameraId)) {
                 setCameraAvailable(false);
@@ -229,6 +231,7 @@
         }
 
         @Override
+        @WorkerThread
         public void onTorchModeChanged(String cameraId, boolean enabled) {
             if (TextUtils.equals(cameraId, mCameraId)) {
                 setCameraAvailable(true);
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/RemoteInputView.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/RemoteInputView.java
index 48949f92..4d6d05f 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/RemoteInputView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/RemoteInputView.java
@@ -22,8 +22,6 @@
 import android.animation.AnimatorListenerAdapter;
 import android.app.ActivityManager;
 import android.app.Notification;
-import android.app.PendingIntent;
-import android.app.RemoteInput;
 import android.content.Context;
 import android.content.pm.PackageManager;
 import android.content.res.ColorStateList;
@@ -75,7 +73,6 @@
 import com.android.systemui.R;
 import com.android.systemui.statusbar.RemoteInputController;
 import com.android.systemui.statusbar.notification.collection.NotificationEntry;
-import com.android.systemui.statusbar.notification.collection.NotificationEntry.EditedSuggestionInfo;
 import com.android.systemui.statusbar.notification.row.wrapper.NotificationViewWrapper;
 import com.android.systemui.statusbar.notification.stack.StackStateAnimator;
 import com.android.systemui.statusbar.phone.LightBarController;
@@ -111,21 +108,15 @@
     private ProgressBar mProgressBar;
     private ImageView mDelete;
     private ImageView mDeleteBg;
-    // TODO(b/193539698): remove reveal param fields, turn them into parameters where needed
-    private int mRevealCx;
-    private int mRevealCy;
-    private int mRevealR;
     private boolean mColorized;
     private int mTint;
     private boolean mResetting;
+    @Nullable private RevealParams mRevealParams;
 
     // TODO(b/193539698): move these to a Controller
     private RemoteInputController mController;
     private final UiEventLogger mUiEventLogger;
     private NotificationEntry mEntry;
-    private PendingIntent mPendingIntent;
-    private RemoteInput mRemoteInput;
-    private RemoteInput[] mRemoteInputs;
     private boolean mRemoved;
     private NotificationViewWrapper mWrapper;
 
@@ -397,9 +388,8 @@
         // During removal, we get reattached and lose focus. Not hiding in that
         // case to prevent flicker.
         if (!mRemoved) {
-            if (animate && mRevealR > 0) {
-                Animator reveal = ViewAnimationUtils.createCircularReveal(
-                        this, mRevealCx, mRevealCy, mRevealR, 0);
+            if (animate && mRevealParams != null && mRevealParams.radius > 0) {
+                Animator reveal = mRevealParams.createCircularHideAnimator(this);
                 reveal.setInterpolator(Interpolators.FAST_OUT_LINEAR_IN);
                 reveal.setDuration(StackStateAnimator.ANIMATION_DURATION_CLOSE_REMOTE_INPUT);
                 reveal.addListener(new AnimatorListenerAdapter() {
@@ -454,30 +444,12 @@
         mController.removeSpinning(mEntry.getKey(), mToken);
     }
 
-    public void setPendingIntent(PendingIntent pendingIntent) {
-        mPendingIntent = pendingIntent;
+    public void setHintText(CharSequence hintText) {
+        mEditText.setHint(hintText);
     }
 
-    /**
-     * Sets the remote input for this view.
-     *
-     * @param remoteInputs The remote inputs that need to be sent to the app.
-     * @param remoteInput The remote input that needs to be activated.
-     * @param editedSuggestionInfo The smart reply that should be inserted in the remote input, or
-     *         {@code null} if the user is not editing a smart reply.
-     */
-    public void setRemoteInput(RemoteInput[] remoteInputs, RemoteInput remoteInput,
-            @Nullable EditedSuggestionInfo editedSuggestionInfo) {
-        mRemoteInputs = remoteInputs;
-        mRemoteInput = remoteInput;
-        mEditText.setHint(mRemoteInput.getLabel());
-        mEditText.setSupportedMimeTypes(remoteInput.getAllowedDataTypes());
-
-        mEntry.editedSuggestionInfo = editedSuggestionInfo;
-        if (editedSuggestionInfo != null) {
-            mEntry.remoteInputText = editedSuggestionInfo.originalText;
-            mEntry.remoteInputAttachment = null;
-        }
+    public void setSupportedMimeTypes(Collection<String> mimeTypes) {
+        mEditText.setSupportedMimeTypes(mimeTypes);
     }
 
     /** Populates the text field of the remote input with the given content. */
@@ -486,9 +458,8 @@
     }
 
     public void focusAnimated() {
-        if (getVisibility() != VISIBLE) {
-            Animator animator = ViewAnimationUtils.createCircularReveal(
-                    this, mRevealCx, mRevealCy, 0, mRevealR);
+        if (getVisibility() != VISIBLE && mRevealParams != null) {
+            Animator animator = mRevealParams.createCircularRevealAnimator(this);
             animator.setDuration(StackStateAnimator.ANIMATION_DURATION_STANDARD);
             animator.setInterpolator(Interpolators.LINEAR_OUT_SLOW_IN);
             animator.start();
@@ -587,30 +558,12 @@
         return mEditText.isFocused() && mEditText.isEnabled();
     }
 
-    // TODO(b/193539698): move this to the controller
-    public void stealFocusFrom(RemoteInputView other) {
-        other.close();
-        setPendingIntent(other.mPendingIntent);
-        setRemoteInput(other.mRemoteInputs, other.mRemoteInput, mEntry.editedSuggestionInfo);
-        setRevealParameters(other.mRevealCx, other.mRevealCy, other.mRevealR);
-        getController().setPendingIntent(other.mPendingIntent);
-        getController().setRemoteInput(other.mRemoteInput);
-        getController().setRemoteInputs(other.mRemoteInputs);
-        focus();
-    }
-
-    public PendingIntent getPendingIntent() {
-        return mPendingIntent;
-    }
-
     public void setRemoved() {
         mRemoved = true;
     }
 
-    public void setRevealParameters(int cx, int cy, int r) {
-        mRevealCx = cx;
-        mRevealCy = cy;
-        mRevealR = r;
+    public void setRevealParameters(@Nullable RevealParams revealParams) {
+        mRevealParams = revealParams;
     }
 
     @Override
@@ -938,4 +891,24 @@
         }
 
     }
+
+    public static class RevealParams {
+        final int centerX;
+        final int centerY;
+        final int radius;
+
+        public RevealParams(int centerX, int centerY, int radius) {
+            this.centerX = centerX;
+            this.centerY = centerY;
+            this.radius = radius;
+        }
+
+        Animator createCircularRevealAnimator(View view) {
+            return ViewAnimationUtils.createCircularReveal(view, centerX, centerY, radius, 0);
+        }
+
+        Animator createCircularHideAnimator(View view) {
+            return ViewAnimationUtils.createCircularReveal(view, centerX, centerY, 0, radius);
+        }
+    }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/RemoteInputViewController.kt b/packages/SystemUI/src/com/android/systemui/statusbar/policy/RemoteInputViewController.kt
index ef0a5b4..bd87875 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/RemoteInputViewController.kt
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/RemoteInputViewController.kt
@@ -33,7 +33,9 @@
 import com.android.systemui.statusbar.NotificationRemoteInputManager
 import com.android.systemui.statusbar.RemoteInputController
 import com.android.systemui.statusbar.notification.collection.NotificationEntry
+import com.android.systemui.statusbar.notification.collection.NotificationEntry.EditedSuggestionInfo
 import com.android.systemui.statusbar.policy.RemoteInputView.NotificationRemoteInputEvent
+import com.android.systemui.statusbar.policy.RemoteInputView.RevealParams
 import com.android.systemui.statusbar.policy.dagger.RemoteInputViewScope
 import javax.inject.Inject
 
@@ -41,6 +43,8 @@
     fun bind()
     fun unbind()
 
+    val isActive: Boolean
+
     /**
      * A [NotificationRemoteInputManager.BouncerChecker] that will be used to determine if the
      * device needs to be unlocked before sending the RemoteInput.
@@ -55,6 +59,14 @@
     /** Other [RemoteInput]s from the notification associated with this Controller. */
     var remoteInputs: Array<RemoteInput>?
 
+    var revealParams: RevealParams?
+
+    /**
+     * Sets the smart reply that should be inserted in the remote input, or `null` if the user is
+     * not editing a smart reply.
+     */
+    fun setEditedSuggestionInfo(info: EditedSuggestionInfo?)
+
     /**
      * Tries to find an action in {@param actions} that matches the current pending intent
      * of this view and updates its state to that of the found action
@@ -68,6 +80,19 @@
 
     /** Unregisters a listener previously registered via [addOnSendRemoteInputListener] */
     fun removeOnSendRemoteInputListener(listener: OnSendRemoteInputListener)
+
+    fun close()
+
+    fun focus()
+
+    fun stealFocusFrom(other: RemoteInputViewController) {
+        other.close()
+        remoteInput = other.remoteInput
+        remoteInputs = other.remoteInputs
+        revealParams = other.revealParams
+        pendingIntent = other.pendingIntent
+        focus()
+    }
 }
 
 /** Listener for send events  */
@@ -100,15 +125,41 @@
 
     private var isBound = false
 
-    override var pendingIntent: PendingIntent? = null
     override var bouncerChecker: NotificationRemoteInputManager.BouncerChecker? = null
+
     override var remoteInput: RemoteInput? = null
+        set(value) {
+            field = value
+            value?.takeIf { isBound }?.let {
+                view.setHintText(it.label)
+                view.setSupportedMimeTypes(it.allowedDataTypes)
+            }
+        }
+
+    override var pendingIntent: PendingIntent? = null
     override var remoteInputs: Array<RemoteInput>? = null
 
+    override var revealParams: RevealParams? = null
+        set(value) {
+            field = value
+            if (isBound) {
+                view.setRevealParameters(value)
+            }
+        }
+
+    override val isActive: Boolean get() = view.isActive
+
     override fun bind() {
         if (isBound) return
         isBound = true
 
+        // TODO: refreshUI method?
+        remoteInput?.let {
+            view.setHintText(it.label)
+            view.setSupportedMimeTypes(it.allowedDataTypes)
+        }
+        view.setRevealParameters(revealParams)
+
         view.addOnEditTextFocusChangedListener(onFocusChangeListener)
         view.addOnSendRemoteInputListener(onSendRemoteInputListener)
     }
@@ -121,6 +172,14 @@
         view.removeOnSendRemoteInputListener(onSendRemoteInputListener)
     }
 
+    override fun setEditedSuggestionInfo(info: EditedSuggestionInfo?) {
+        entry.editedSuggestionInfo = info
+        if (info != null) {
+            entry.remoteInputText = info.originalText
+            entry.remoteInputAttachment = null
+        }
+    }
+
     override fun updatePendingIntentFromActions(actions: Array<Notification.Action>?): Boolean {
         actions ?: return false
         val current: Intent = pendingIntent?.intent ?: return false
@@ -132,8 +191,7 @@
             pendingIntent = actionIntent
             remoteInput = input
             remoteInputs = inputs
-            view.pendingIntent = actionIntent
-            view.setRemoteInput(inputs, input, null /* editedSuggestionInfo */)
+            setEditedSuggestionInfo(null)
             return true
         }
         return false
@@ -148,6 +206,14 @@
         onSendListeners.remove(listener)
     }
 
+    override fun close() {
+        view.close()
+    }
+
+    override fun focus() {
+        view.focus()
+    }
+
     private val onFocusChangeListener = View.OnFocusChangeListener { _, hasFocus ->
         remoteInputQuickSettingsDisabler.setRemoteInputActive(hasFocus)
     }
@@ -217,11 +283,12 @@
      * @return returns intent with granted URI permissions that should be used immediately
      */
     private fun prepareRemoteInput(remoteInput: RemoteInput): Intent =
-            if (entry.remoteInputAttachment == null) prepareRemoteInputFromText(remoteInput)
-            else prepareRemoteInputFromData(
-                    remoteInput,
-                    entry.remoteInputMimeType,
-                    entry.remoteInputUri)
+        if (entry.remoteInputAttachment == null)
+            prepareRemoteInputFromText(remoteInput)
+        else prepareRemoteInputFromData(
+                remoteInput,
+                entry.remoteInputMimeType,
+                entry.remoteInputUri)
 
     private fun prepareRemoteInputFromText(remoteInput: RemoteInput): Intent {
         val results = Bundle()
@@ -232,11 +299,10 @@
         view.clearAttachment()
         entry.remoteInputUri = null
         entry.remoteInputMimeType = null
-        if (entry.editedSuggestionInfo == null) {
-            RemoteInput.setResultsSource(fillInIntent, RemoteInput.SOURCE_FREE_FORM_INPUT)
-        } else {
-            RemoteInput.setResultsSource(fillInIntent, RemoteInput.SOURCE_CHOICE)
-        }
+        val resultSource = entry.editedSuggestionInfo
+                ?.let { RemoteInput.SOURCE_FREE_FORM_INPUT }
+                ?: RemoteInput.SOURCE_CHOICE
+        RemoteInput.setResultsSource(fillInIntent, resultSource)
         return fillInIntent
     }
 
@@ -266,11 +332,10 @@
         entry.remoteInputText = fullText
 
         // mirror prepareRemoteInputFromText for text input
-        if (entry.editedSuggestionInfo == null) {
-            RemoteInput.setResultsSource(fillInIntent, RemoteInput.SOURCE_FREE_FORM_INPUT)
-        } else if (entry.remoteInputAttachment == null) {
-            RemoteInput.setResultsSource(fillInIntent, RemoteInput.SOURCE_CHOICE)
-        }
+        val resultSource = entry.editedSuggestionInfo
+                ?.let { RemoteInput.SOURCE_FREE_FORM_INPUT }
+                ?: RemoteInput.SOURCE_CHOICE
+        RemoteInput.setResultsSource(fillInIntent, resultSource)
         return fillInIntent
     }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java
index fa26a35..763f041 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java
@@ -67,6 +67,7 @@
 import com.android.systemui.SystemUISecondaryUserService;
 import com.android.systemui.animation.DialogLaunchAnimator;
 import com.android.systemui.broadcast.BroadcastDispatcher;
+import com.android.systemui.broadcast.BroadcastSender;
 import com.android.systemui.dagger.SysUISingleton;
 import com.android.systemui.dagger.qualifiers.Background;
 import com.android.systemui.dagger.qualifiers.Main;
@@ -122,6 +123,7 @@
     protected final Handler mHandler;
     private final ActivityStarter mActivityStarter;
     private final BroadcastDispatcher mBroadcastDispatcher;
+    private final BroadcastSender mBroadcastSender;
     private final TelephonyListenerManager mTelephonyListenerManager;
     private final InteractionJankMonitor mInteractionJankMonitor;
     private final LatencyTracker mLatencyTracker;
@@ -165,6 +167,7 @@
             @Main Handler handler,
             ActivityStarter activityStarter,
             BroadcastDispatcher broadcastDispatcher,
+            BroadcastSender broadcastSender,
             UiEventLogger uiEventLogger,
             FalsingManager falsingManager,
             TelephonyListenerManager telephonyListenerManager,
@@ -179,6 +182,7 @@
         mActivityManager = activityManager;
         mUserTracker = userTracker;
         mBroadcastDispatcher = broadcastDispatcher;
+        mBroadcastSender = broadcastSender;
         mTelephonyListenerManager = telephonyListenerManager;
         mUiEventLogger = uiEventLogger;
         mFalsingManager = falsingManager;
@@ -1194,7 +1198,7 @@
                 }
                 // Use broadcast instead of ShadeController, as this dialog may have started in
                 // another process and normal dagger bindings are not available
-                getContext().sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
+                mBroadcastSender.closeSystemDialogs();
                 getContext().startActivityAsUser(
                         CreateUserActivity.createIntentForStart(getContext()),
                         mUserTracker.getUserHandle());
diff --git a/packages/SystemUI/tests/src/com/android/keyguard/KeyguardPatternViewControllerTest.kt b/packages/SystemUI/tests/src/com/android/keyguard/KeyguardPatternViewControllerTest.kt
index e980eb7..8e1e42a 100644
--- a/packages/SystemUI/tests/src/com/android/keyguard/KeyguardPatternViewControllerTest.kt
+++ b/packages/SystemUI/tests/src/com/android/keyguard/KeyguardPatternViewControllerTest.kt
@@ -88,6 +88,6 @@
     fun onPause_clearsTextField() {
         mKeyguardPatternViewController.init()
         mKeyguardPatternViewController.onPause()
-        verify(mKeyguardMessageAreaController).setMessage(R.string.keyguard_enter_your_pattern)
+        verify(mKeyguardMessageAreaController).setMessage("")
     }
 }
diff --git a/packages/SystemUI/tests/src/com/android/keyguard/KeyguardStatusViewControllerTest.java b/packages/SystemUI/tests/src/com/android/keyguard/KeyguardStatusViewControllerTest.java
index 2fc9122..1753157 100644
--- a/packages/SystemUI/tests/src/com/android/keyguard/KeyguardStatusViewControllerTest.java
+++ b/packages/SystemUI/tests/src/com/android/keyguard/KeyguardStatusViewControllerTest.java
@@ -112,4 +112,13 @@
         mKeyguardUpdateMonitorCallbackCaptor.getValue().onUserSwitchComplete(0);
         verify(mKeyguardClockSwitchController).refreshFormat();
     }
+
+    @Test
+    public void setTranslationYExcludingMedia_forwardsCallToView() {
+        float translationY = 123f;
+
+        mController.setTranslationYExcludingMedia(translationY);
+
+        verify(mKeyguardStatusView).setChildrenTranslationYExcludingMediaView(translationY);
+    }
 }
diff --git a/packages/SystemUI/tests/src/com/android/keyguard/KeyguardStatusViewTest.kt b/packages/SystemUI/tests/src/com/android/keyguard/KeyguardStatusViewTest.kt
new file mode 100644
index 0000000..ce44f4d
--- /dev/null
+++ b/packages/SystemUI/tests/src/com/android/keyguard/KeyguardStatusViewTest.kt
@@ -0,0 +1,55 @@
+package com.android.keyguard
+
+import android.test.suitebuilder.annotation.SmallTest
+import android.testing.AndroidTestingRunner
+import android.testing.TestableLooper.RunWithLooper
+import android.view.LayoutInflater
+import android.view.View
+import android.view.ViewGroup
+import com.android.systemui.R
+import com.android.systemui.SysuiTestCase
+import com.android.systemui.util.children
+import com.google.common.truth.Truth.assertThat
+import org.junit.Before
+import org.junit.Test
+import org.junit.runner.RunWith
+
+@SmallTest
+@RunWith(AndroidTestingRunner::class)
+@RunWithLooper(setAsMainLooper = true)
+class KeyguardStatusViewTest : SysuiTestCase() {
+
+    private lateinit var keyguardStatusView: KeyguardStatusView
+    private val mediaView: View
+        get() = keyguardStatusView.findViewById(R.id.status_view_media_container)
+    private val statusViewContainer: ViewGroup
+        get() = keyguardStatusView.findViewById(R.id.status_view_container)
+    private val childrenExcludingMedia
+        get() = statusViewContainer.children.filter { it != mediaView }
+
+    @Before
+    fun setUp() {
+        keyguardStatusView = LayoutInflater.from(context)
+                .inflate(R.layout.keyguard_status_view, /* root= */ null) as KeyguardStatusView
+    }
+
+    @Test
+    fun setChildrenTranslationYExcludingMediaView_mediaViewIsNotTranslated() {
+        val translationY = 1234f
+
+        keyguardStatusView.setChildrenTranslationYExcludingMediaView(translationY)
+
+        assertThat(mediaView.translationY).isEqualTo(0)
+    }
+
+    @Test
+    fun setChildrenTranslationYExcludingMediaView_childrenAreTranslated() {
+        val translationY = 1234f
+
+        keyguardStatusView.setChildrenTranslationYExcludingMediaView(translationY)
+
+        childrenExcludingMedia.forEach {
+            assertThat(it.translationY).isEqualTo(translationY)
+        }
+    }
+}
\ No newline at end of file
diff --git a/packages/SystemUI/tests/src/com/android/systemui/animation/ViewBoundAnimatorTest.kt b/packages/SystemUI/tests/src/com/android/systemui/animation/ViewBoundAnimatorTest.kt
new file mode 100644
index 0000000..214fd4d
--- /dev/null
+++ b/packages/SystemUI/tests/src/com/android/systemui/animation/ViewBoundAnimatorTest.kt
@@ -0,0 +1,277 @@
+package com.android.systemui.animation
+
+import android.animation.ObjectAnimator
+import android.testing.AndroidTestingRunner
+import android.testing.TestableLooper
+import android.view.View
+import android.view.ViewGroup
+import android.widget.LinearLayout
+import androidx.test.filters.SmallTest
+import com.android.systemui.SysuiTestCase
+import junit.framework.Assert.assertEquals
+import junit.framework.Assert.assertNotNull
+import junit.framework.Assert.assertNull
+import org.junit.After
+import org.junit.Before
+import org.junit.Test
+import org.junit.runner.RunWith
+
+@SmallTest
+@RunWith(AndroidTestingRunner::class)
+@TestableLooper.RunWithLooper
+class ViewBoundAnimatorTest : SysuiTestCase() {
+    companion object {
+        private const val TEST_DURATION = 1000L
+        private val TEST_INTERPOLATOR = Interpolators.LINEAR
+    }
+
+    private lateinit var rootView: LinearLayout
+
+    @Before
+    fun setUp() {
+        rootView = LinearLayout(mContext)
+    }
+
+    @After
+    fun tearDown() {
+        ViewBoundAnimator.stopAnimating(rootView)
+    }
+
+    @Test
+    fun respectsAnimationParameters() {
+        rootView.layout(10 /* l */, 10 /* t */, 50 /* r */, 50 /* b */)
+
+        ViewBoundAnimator.animate(
+            rootView, interpolator = TEST_INTERPOLATOR, duration = TEST_DURATION
+        )
+        rootView.layout(0 /* l */, 0 /* t */, 100 /* r */, 100 /* b */)
+
+        assertNotNull(rootView.getTag(R.id.tag_animator))
+        val animator = rootView.getTag(R.id.tag_animator) as ObjectAnimator
+        assertEquals(animator.interpolator, TEST_INTERPOLATOR)
+        assertEquals(animator.duration, TEST_DURATION)
+    }
+
+    @Test
+    fun animatesFromStartToEnd() {
+        rootView.layout(10 /* l */, 10 /* t */, 50 /* r */, 50 /* b */)
+
+        ViewBoundAnimator.animate(rootView)
+        // Change all bounds.
+        rootView.layout(0 /* l */, 15 /* t */, 70 /* r */, 80 /* b */)
+
+        assertNotNull(rootView.getTag(R.id.tag_animator))
+        // The initial values should be those of the previous layout.
+        checkBounds(rootView, l = 10, t = 10, r = 50, b = 50)
+        endAnimation(rootView)
+        assertNull(rootView.getTag(R.id.tag_animator))
+        // The end values should be those of the latest layout.
+        checkBounds(rootView, l = 0, t = 15, r = 70, b = 80)
+    }
+
+    @Test
+    fun animatesSuccessiveLayoutChanges() {
+        rootView.layout(10 /* l */, 10 /* t */, 50 /* r */, 50 /* b */)
+
+        ViewBoundAnimator.animate(rootView)
+        // Change all bounds.
+        rootView.layout(0 /* l */, 15 /* t */, 70 /* r */, 80 /* b */)
+
+        assertNotNull(rootView.getTag(R.id.tag_animator))
+        endAnimation(rootView)
+        assertNull(rootView.getTag(R.id.tag_animator))
+        checkBounds(rootView, l = 0, t = 15, r = 70, b = 80)
+
+        // Change only top and right.
+        rootView.layout(0 /* l */, 20 /* t */, 60 /* r */, 80 /* b */)
+
+        assertNotNull(rootView.getTag(R.id.tag_animator))
+        endAnimation(rootView)
+        assertNull(rootView.getTag(R.id.tag_animator))
+        checkBounds(rootView, l = 0, t = 20, r = 60, b = 80)
+
+        // Change all bounds again.
+        rootView.layout(5 /* l */, 25 /* t */, 55 /* r */, 95 /* b */)
+
+        assertNotNull(rootView.getTag(R.id.tag_animator))
+        endAnimation(rootView)
+        assertNull(rootView.getTag(R.id.tag_animator))
+        checkBounds(rootView, l = 5, t = 25, r = 55, b = 95)
+    }
+
+    @Test
+    fun animatesFromPreviousAnimationProgress() {
+        rootView.layout(10 /* l */, 10 /* t */, 50 /* r */, 50 /* b */)
+
+        ViewBoundAnimator.animateNextUpdate(rootView, interpolator = TEST_INTERPOLATOR)
+        // Change all bounds.
+        rootView.layout(0 /* l */, 20 /* t */, 70 /* r */, 80 /* b */)
+
+        assertNotNull(rootView.getTag(R.id.tag_animator))
+        advanceAnimation(rootView, fraction = 0.5f)
+        checkBounds(rootView, l = 5, t = 15, r = 60, b = 65)
+
+        // Change all bounds again.
+        rootView.layout(25 /* l */, 25 /* t */, 55 /* r */, 60 /* b */)
+
+        assertNotNull(rootView.getTag(R.id.tag_animator))
+        checkBounds(rootView, l = 5, t = 15, r = 60, b = 65)
+        endAnimation(rootView)
+        assertNull(rootView.getTag(R.id.tag_animator))
+        checkBounds(rootView, l = 25, t = 25, r = 55, b = 60)
+    }
+
+    @Test
+    fun animatesRootAndChildren() {
+        val firstChild = View(mContext)
+        rootView.addView(firstChild)
+        val secondChild = View(mContext)
+        rootView.addView(secondChild)
+        rootView.layout(0 /* l */, 0 /* t */, 150 /* r */, 100 /* b */)
+        firstChild.layout(0 /* l */, 0 /* t */, 100 /* r */, 100 /* b */)
+        secondChild.layout(100 /* l */, 0 /* t */, 150 /* r */, 100 /* b */)
+
+        ViewBoundAnimator.animate(rootView)
+        // Change all bounds.
+        rootView.layout(10 /* l */, 20 /* t */, 200 /* r */, 120 /* b */)
+        firstChild.layout(10 /* l */, 20 /* t */, 150 /* r */, 120 /* b */)
+        secondChild.layout(150 /* l */, 20 /* t */, 200 /* r */, 120 /* b */)
+
+        assertNotNull(rootView.getTag(R.id.tag_animator))
+        // The initial values should be those of the previous layout.
+        checkBounds(rootView, l = 0, t = 0, r = 150, b = 100)
+        checkBounds(firstChild, l = 0, t = 0, r = 100, b = 100)
+        checkBounds(secondChild, l = 100, t = 0, r = 150, b = 100)
+        endAnimation(rootView)
+        assertNull(rootView.getTag(R.id.tag_animator))
+        // The end values should be those of the latest layout.
+        checkBounds(rootView, l = 10, t = 20, r = 200, b = 120)
+        checkBounds(firstChild, l = 10, t = 20, r = 150, b = 120)
+        checkBounds(secondChild, l = 150, t = 20, r = 200, b = 120)
+    }
+
+    @Test
+    fun doesNotAnimateInvisibleViews() {
+        rootView.layout(10 /* l */, 10 /* t */, 50 /* r */, 50 /* b */)
+
+        ViewBoundAnimator.animate(rootView)
+        // GONE.
+        rootView.visibility = View.GONE
+        rootView.layout(0 /* l */, 15 /* t */, 55 /* r */, 80 /* b */)
+
+        assertNull(rootView.getTag(R.id.tag_animator))
+        checkBounds(rootView, l = 0, t = 15, r = 55, b = 80)
+
+        // INVISIBLE.
+        rootView.visibility = View.INVISIBLE
+        rootView.layout(0 /* l */, 20 /* t */, 0 /* r */, 20 /* b */)
+    }
+
+    @Test
+    fun doesNotAnimateUnchangingBounds() {
+        rootView.layout(10 /* l */, 10 /* t */, 50 /* r */, 50 /* b */)
+
+        ViewBoundAnimator.animate(rootView)
+        // No bounds are changed.
+        rootView.layout(10 /* l */, 10 /* t */, 50 /* r */, 50 /* b */)
+
+        assertNull(rootView.getTag(R.id.tag_animator))
+        checkBounds(rootView, l = 10, t = 10, r = 50, b = 50)
+
+        // Change only right and bottom.
+        rootView.layout(10 /* l */, 10 /* t */, 70 /* r */, 80 /* b */)
+
+        assertNotNull(rootView.getTag(R.id.tag_animator))
+        endAnimation(rootView)
+        assertNull(rootView.getTag(R.id.tag_animator))
+        checkBounds(rootView, l = 10, t = 10, r = 70, b = 80)
+    }
+
+    @Test
+    fun doesNotAnimateExcludedBounds() {
+        rootView.layout(10 /* l */, 10 /* t */, 50 /* r */, 50 /* b */)
+
+        ViewBoundAnimator.animate(
+            rootView,
+            bounds = setOf(ViewBoundAnimator.Bound.LEFT, ViewBoundAnimator.Bound.TOP),
+            interpolator = TEST_INTERPOLATOR
+        )
+        // Change all bounds.
+        rootView.layout(0 /* l */, 20 /* t */, 70 /* r */, 80 /* b */)
+
+        assertNotNull(rootView.getTag(R.id.tag_animator))
+        advanceAnimation(rootView, 0.5f)
+        checkBounds(rootView, l = 5, t = 15, r = 70, b = 80)
+        endAnimation(rootView)
+        assertNull(rootView.getTag(R.id.tag_animator))
+        checkBounds(rootView, l = 0, t = 20, r = 70, b = 80)
+    }
+
+    @Test
+    fun stopsAnimatingAfterSingleLayout() {
+        rootView.layout(10 /* l */, 10 /* t */, 50 /* r */, 50 /* b */)
+
+        ViewBoundAnimator.animateNextUpdate(rootView)
+        // Change all bounds.
+        rootView.layout(0 /* l */, 15 /* t */, 70 /* r */, 80 /* b */)
+
+        assertNotNull(rootView.getTag(R.id.tag_animator))
+        endAnimation(rootView)
+        assertNull(rootView.getTag(R.id.tag_animator))
+        checkBounds(rootView, l = 0, t = 15, r = 70, b = 80)
+
+        // Change all bounds again.
+        rootView.layout(10 /* l */, 10 /* t */, 50/* r */, 50 /* b */)
+
+        assertNull(rootView.getTag(R.id.tag_animator))
+        checkBounds(rootView, l = 10, t = 10, r = 50, b = 50)
+    }
+
+    @Test
+    fun stopsAnimatingWhenInstructed() {
+        rootView.layout(10 /* l */, 10 /* t */, 50 /* r */, 50 /* b */)
+
+        ViewBoundAnimator.animate(rootView)
+        // Change all bounds.
+        rootView.layout(0 /* l */, 15 /* t */, 70 /* r */, 80 /* b */)
+
+        assertNotNull(rootView.getTag(R.id.tag_animator))
+        endAnimation(rootView)
+        assertNull(rootView.getTag(R.id.tag_animator))
+        checkBounds(rootView, l = 0, t = 15, r = 70, b = 80)
+
+        ViewBoundAnimator.stopAnimating(rootView)
+        // Change all bounds again.
+        rootView.layout(10 /* l */, 10 /* t */, 50/* r */, 50 /* b */)
+
+        assertNull(rootView.getTag(R.id.tag_animator))
+        checkBounds(rootView, l = 10, t = 10, r = 50, b = 50)
+    }
+
+    private fun checkBounds(v: View, l: Int, t: Int, r: Int, b: Int) {
+        assertEquals(l, v.left)
+        assertEquals(t, v.top)
+        assertEquals(r, v.right)
+        assertEquals(b, v.bottom)
+    }
+
+    private fun advanceAnimation(rootView: View, fraction: Float) {
+        (rootView.getTag(R.id.tag_animator) as? ObjectAnimator)?.setCurrentFraction(fraction)
+
+        if (rootView is ViewGroup) {
+            for (i in 0 until rootView.childCount) {
+                advanceAnimation(rootView.getChildAt(i), fraction)
+            }
+        }
+    }
+
+    private fun endAnimation(rootView: View) {
+        (rootView.getTag(R.id.tag_animator) as? ObjectAnimator)?.end()
+
+        if (rootView is ViewGroup) {
+            for (i in 0 until rootView.childCount) {
+                endAnimation(rootView.getChildAt(i))
+            }
+        }
+    }
+}
diff --git a/packages/SystemUI/tests/src/com/android/systemui/biometrics/UdfpsControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/biometrics/UdfpsControllerTest.java
index 1856fda..613931f 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/biometrics/UdfpsControllerTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/biometrics/UdfpsControllerTest.java
@@ -48,6 +48,7 @@
 import android.testing.TestableLooper.RunWithLooper;
 import android.view.LayoutInflater;
 import android.view.MotionEvent;
+import android.view.View;
 import android.view.WindowManager;
 import android.view.accessibility.AccessibilityManager;
 
@@ -64,8 +65,8 @@
 import com.android.systemui.plugins.statusbar.StatusBarStateController;
 import com.android.systemui.statusbar.LockscreenShadeTransitionController;
 import com.android.systemui.statusbar.VibratorHelper;
-import com.android.systemui.statusbar.phone.KeyguardBypassController;
 import com.android.systemui.statusbar.phone.CentralSurfaces;
+import com.android.systemui.statusbar.phone.KeyguardBypassController;
 import com.android.systemui.statusbar.phone.StatusBarKeyguardViewManager;
 import com.android.systemui.statusbar.phone.SystemUIDialogManager;
 import com.android.systemui.statusbar.phone.UnlockedScreenOffAnimationController;
@@ -188,6 +189,7 @@
     @Captor private ArgumentCaptor<IUdfpsOverlayController> mOverlayCaptor;
     private IUdfpsOverlayController mOverlayController;
     @Captor private ArgumentCaptor<UdfpsView.OnTouchListener> mTouchListenerCaptor;
+    @Captor private ArgumentCaptor<View.OnHoverListener> mHoverListenerCaptor;
     @Captor private ArgumentCaptor<Runnable> mOnIlluminatedRunnableCaptor;
     @Captor private ArgumentCaptor<ScreenLifecycle.Observer> mScreenObserverCaptor;
     private ScreenLifecycle.Observer mScreenObserver;
@@ -568,23 +570,24 @@
     }
 
     @Test
-    public void playHapticOnTouchUdfpsArea() throws RemoteException {
+    public void playHapticOnTouchUdfpsArea_a11yTouchExplorationEnabled() throws RemoteException {
         // Configure UdfpsView to accept the ACTION_DOWN event
         when(mUdfpsView.isIlluminationRequested()).thenReturn(false);
         when(mUdfpsView.isWithinSensorArea(anyFloat(), anyFloat())).thenReturn(true);
 
-        // GIVEN that the overlay is showing
+        // GIVEN that the overlay is showing and a11y touch exploration enabled
+        when(mAccessibilityManager.isTouchExplorationEnabled()).thenReturn(true);
         mOverlayController.showUdfpsOverlay(TEST_UDFPS_SENSOR_ID,
                 BiometricOverlayConstants.REASON_AUTH_KEYGUARD, mUdfpsOverlayControllerCallback);
         mFgExecutor.runAllReady();
 
-        // WHEN ACTION_DOWN is received
-        verify(mUdfpsView).setOnTouchListener(mTouchListenerCaptor.capture());
-        MotionEvent downEvent = MotionEvent.obtain(0, 0, MotionEvent.ACTION_DOWN, 0, 0, 0);
-        mTouchListenerCaptor.getValue().onTouch(mUdfpsView, downEvent);
-        downEvent.recycle();
-        MotionEvent moveEvent = MotionEvent.obtain(0, 0, MotionEvent.ACTION_MOVE, 0, 0, 0);
-        mTouchListenerCaptor.getValue().onTouch(mUdfpsView, moveEvent);
+        // WHEN ACTION_HOVER is received
+        verify(mUdfpsView).setOnHoverListener(mHoverListenerCaptor.capture());
+        MotionEvent enterEvent = MotionEvent.obtain(0, 0, MotionEvent.ACTION_HOVER_ENTER, 0, 0, 0);
+        mHoverListenerCaptor.getValue().onHover(mUdfpsView, enterEvent);
+        enterEvent.recycle();
+        MotionEvent moveEvent = MotionEvent.obtain(0, 0, MotionEvent.ACTION_HOVER_MOVE, 0, 0, 0);
+        mHoverListenerCaptor.getValue().onHover(mUdfpsView, moveEvent);
         moveEvent.recycle();
 
         // THEN tick haptic is played
@@ -600,4 +603,34 @@
         assertEquals(VibrationAttributes.USAGE_COMMUNICATION_REQUEST,
                 UdfpsController.VIBRATION_ATTRIBUTES.getUsage());
     }
+
+    @Test
+    public void noHapticOnTouchUdfpsArea_a11yTouchExplorationDisabled() throws RemoteException {
+        // Configure UdfpsView to accept the ACTION_DOWN event
+        when(mUdfpsView.isIlluminationRequested()).thenReturn(false);
+        when(mUdfpsView.isWithinSensorArea(anyFloat(), anyFloat())).thenReturn(true);
+
+        // GIVEN that the overlay is showing and a11y touch exploration NOT enabled
+        when(mAccessibilityManager.isTouchExplorationEnabled()).thenReturn(false);
+        mOverlayController.showUdfpsOverlay(TEST_UDFPS_SENSOR_ID,
+                BiometricOverlayConstants.REASON_AUTH_KEYGUARD, mUdfpsOverlayControllerCallback);
+        mFgExecutor.runAllReady();
+
+        // WHEN ACTION_DOWN is received
+        verify(mUdfpsView).setOnTouchListener(mTouchListenerCaptor.capture());
+        MotionEvent downEvent = MotionEvent.obtain(0, 0, MotionEvent.ACTION_DOWN, 0, 0, 0);
+        mTouchListenerCaptor.getValue().onTouch(mUdfpsView, downEvent);
+        downEvent.recycle();
+        MotionEvent moveEvent = MotionEvent.obtain(0, 0, MotionEvent.ACTION_MOVE, 0, 0, 0);
+        mTouchListenerCaptor.getValue().onTouch(mUdfpsView, moveEvent);
+        moveEvent.recycle();
+
+        // THEN NO haptic played
+        verify(mVibrator, never()).vibrate(
+                anyInt(),
+                anyString(),
+                any(),
+                anyString(),
+                any());
+    }
 }
diff --git a/packages/SystemUI/tests/src/com/android/systemui/broadcast/BroadcastSenderTest.kt b/packages/SystemUI/tests/src/com/android/systemui/broadcast/BroadcastSenderTest.kt
new file mode 100644
index 0000000..fbd2c91
--- /dev/null
+++ b/packages/SystemUI/tests/src/com/android/systemui/broadcast/BroadcastSenderTest.kt
@@ -0,0 +1,145 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.broadcast
+
+import android.content.Context
+import android.content.Intent
+import android.os.Bundle
+import android.os.UserHandle
+import android.testing.AndroidTestingRunner
+import androidx.test.filters.SmallTest
+import com.android.systemui.SysuiTestCase
+import com.android.systemui.util.concurrency.FakeExecutor
+import com.android.systemui.util.time.FakeSystemClock
+import com.android.systemui.util.wakelock.WakeLockFake
+import com.google.common.truth.Truth.assertThat
+import org.junit.Before
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.mockito.ArgumentCaptor
+import org.mockito.Mock
+import org.mockito.Mockito.verify
+import org.mockito.MockitoAnnotations
+
+@RunWith(AndroidTestingRunner::class)
+@SmallTest
+class BroadcastSenderTest : SysuiTestCase() {
+
+    @Mock
+    private lateinit var mockContext: Context
+
+    private lateinit var broadcastSender: BroadcastSender
+    private lateinit var executor: FakeExecutor
+    private lateinit var wakeLock: WakeLockFake
+
+    @Before
+    fun setUp() {
+        MockitoAnnotations.initMocks(this)
+        executor = FakeExecutor(FakeSystemClock())
+        wakeLock = WakeLockFake()
+        val wakeLockBuilder = WakeLockFake.Builder(mContext)
+        wakeLockBuilder.setWakeLock(wakeLock)
+        broadcastSender = BroadcastSender(mockContext, wakeLockBuilder, executor)
+    }
+
+    @Test
+    fun sendBroadcast_dispatchesWithWakelock() {
+        val intent = Intent(Intent.ACTION_VIEW)
+        broadcastSender.sendBroadcast(intent)
+
+        runExecutorAssertingWakelock {
+            verify(mockContext).sendBroadcast(intent)
+        }
+    }
+
+    @Test
+    fun sendBroadcastWithPermission_dispatchesWithWakelock() {
+        val intent = Intent(Intent.ACTION_VIEW)
+        val permission = "Permission"
+        broadcastSender.sendBroadcast(intent, permission)
+
+        runExecutorAssertingWakelock {
+            verify(mockContext).sendBroadcast(intent, permission)
+        }
+    }
+
+    @Test
+    fun sendBroadcastAsUser_dispatchesWithWakelock() {
+        val intent = Intent(Intent.ACTION_VIEW)
+        broadcastSender.sendBroadcastAsUser(intent, UserHandle.ALL)
+
+        runExecutorAssertingWakelock {
+            verify(mockContext).sendBroadcastAsUser(intent, UserHandle.ALL)
+        }
+    }
+
+    @Test
+    fun sendBroadcastAsUserWithPermission_dispatchesWithWakelock() {
+        val intent = Intent(Intent.ACTION_VIEW)
+        val permission = "Permission"
+        broadcastSender.sendBroadcastAsUser(intent, UserHandle.ALL, permission)
+
+        runExecutorAssertingWakelock {
+            verify(mockContext).sendBroadcastAsUser(intent, UserHandle.ALL, permission)
+        }
+    }
+
+    @Test
+    fun sendBroadcastAsUserWithPermissionAndOptions_dispatchesWithWakelock() {
+        val intent = Intent(Intent.ACTION_VIEW)
+        val permission = "Permission"
+        val options = Bundle()
+        options.putString("key", "value")
+
+        broadcastSender.sendBroadcastAsUser(intent, UserHandle.ALL, permission, options)
+
+        runExecutorAssertingWakelock {
+            verify(mockContext).sendBroadcastAsUser(intent, UserHandle.ALL, permission, options)
+        }
+    }
+
+    @Test
+    fun sendBroadcastAsUserWithPermissionAndAppOp_dispatchesWithWakelock() {
+        val intent = Intent(Intent.ACTION_VIEW)
+        val permission = "Permission"
+
+        broadcastSender.sendBroadcastAsUser(intent, UserHandle.ALL, permission, 12)
+
+        runExecutorAssertingWakelock {
+            verify(mockContext).sendBroadcastAsUser(intent, UserHandle.ALL, permission, 12)
+        }
+    }
+
+    @Test
+    fun sendCloseSystemDialogs_dispatchesWithWakelock() {
+        val intentCaptor = ArgumentCaptor.forClass(Intent::class.java)
+
+        broadcastSender.closeSystemDialogs()
+
+        runExecutorAssertingWakelock {
+            verify(mockContext).sendBroadcast(intentCaptor.capture())
+            assertThat(intentCaptor.value.action).isEqualTo(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)
+        }
+    }
+
+    private fun runExecutorAssertingWakelock(verification: () -> Unit) {
+        assertThat(wakeLock.isHeld).isTrue()
+        executor.runAllReady()
+        verification.invoke()
+        assertThat(wakeLock.isHeld).isFalse()
+    }
+}
\ No newline at end of file
diff --git a/packages/SystemUI/tests/src/com/android/systemui/controls/controller/ControlActionCoordinatorImplTest.kt b/packages/SystemUI/tests/src/com/android/systemui/controls/controller/ControlActionCoordinatorImplTest.kt
index da25c62..49eaf823 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/controls/controller/ControlActionCoordinatorImplTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/controls/controller/ControlActionCoordinatorImplTest.kt
@@ -23,6 +23,7 @@
 import android.test.suitebuilder.annotation.SmallTest
 import android.testing.AndroidTestingRunner
 import com.android.systemui.SysuiTestCase
+import com.android.systemui.broadcast.BroadcastSender
 import com.android.systemui.controls.ControlsMetricsLogger
 import com.android.systemui.plugins.ActivityStarter
 import com.android.systemui.statusbar.VibratorHelper
@@ -62,6 +63,8 @@
     @Mock
     private lateinit var activityStarter: ActivityStarter
     @Mock
+    private lateinit var broadcastSender: BroadcastSender
+    @Mock
     private lateinit var taskViewFactory: Optional<TaskViewFactory>
     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     private lateinit var cvh: ControlViewHolder
@@ -94,6 +97,7 @@
                 bgExecutor,
                 uiExecutor,
                 activityStarter,
+                broadcastSender,
                 keyguardStateController,
                 taskViewFactory,
                 metricsLogger,
diff --git a/packages/SystemUI/tests/src/com/android/systemui/controls/ui/DetailDialogTest.kt b/packages/SystemUI/tests/src/com/android/systemui/controls/ui/DetailDialogTest.kt
index 87b9172..0166fa2 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/controls/ui/DetailDialogTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/controls/ui/DetailDialogTest.kt
@@ -21,6 +21,7 @@
 import android.testing.TestableLooper
 import androidx.test.filters.SmallTest
 import com.android.systemui.SysuiTestCase
+import com.android.systemui.broadcast.BroadcastSender
 import com.android.wm.shell.TaskView
 import org.junit.Before
 import org.junit.Test
@@ -39,6 +40,8 @@
     @Mock
     private lateinit var taskView: TaskView
     @Mock
+    private lateinit var broadcastSender: BroadcastSender
+    @Mock
     private lateinit var controlViewHolder: ControlViewHolder
     @Mock
     private lateinit var pendingIntent: PendingIntent
@@ -63,6 +66,7 @@
     private fun createDialog(pendingIntent: PendingIntent): DetailDialog {
         return DetailDialog(
             mContext,
+            broadcastSender,
             taskView,
             pendingIntent,
             controlViewHolder
diff --git a/packages/SystemUI/tests/src/com/android/systemui/doze/DozeScreenBrightnessTest.java b/packages/SystemUI/tests/src/com/android/systemui/doze/DozeScreenBrightnessTest.java
index badafa4..f4b378e 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/doze/DozeScreenBrightnessTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/doze/DozeScreenBrightnessTest.java
@@ -27,6 +27,7 @@
 import static com.android.systemui.doze.DozeMachine.State.FINISH;
 import static com.android.systemui.doze.DozeMachine.State.INITIALIZED;
 import static com.android.systemui.doze.DozeMachine.State.UNINITIALIZED;
+
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertNotSame;
@@ -468,37 +469,39 @@
     public void transitionToDoze_shouldClampBrightness_afterTimeout_clampsToDim() {
         when(mWakefulnessLifecycle.getLastSleepReason()).thenReturn(
                 PowerManager.GO_TO_SLEEP_REASON_TIMEOUT);
-        when(mDozeParameters.shouldControlUnlockedScreenOff()).thenReturn(true);
         when(mDozeParameters.shouldClampToDimBrightness()).thenReturn(true);
 
         mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
-        mScreen.transitionTo(INITIALIZED, DOZE);
 
         // If we're dozing after a timeout, and playing the unlocked screen animation, we should
         // stay at or below dim brightness, because the screen dims just before timeout.
         assertTrue(mServiceFake.screenBrightness <= DIM_BRIGHTNESS);
+
+        // Once we transition to Doze, use the doze brightness
+        mScreen.transitionTo(INITIALIZED, DOZE);
+        assertEquals(mServiceFake.screenBrightness, DEFAULT_BRIGHTNESS);
     }
 
     @Test
     public void transitionToDoze_shouldClampBrightness_notAfterTimeout_doesNotClampToDim() {
         when(mWakefulnessLifecycle.getLastSleepReason()).thenReturn(
                 PowerManager.GO_TO_SLEEP_REASON_POWER_BUTTON);
-        when(mDozeParameters.shouldControlUnlockedScreenOff()).thenReturn(true);
         when(mDozeParameters.shouldClampToDimBrightness()).thenReturn(true);
 
         mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
-        mScreen.transitionTo(INITIALIZED, DOZE);
 
         // If we're playing the unlocked screen off animation after a power button press, we should
         // leave the brightness alone.
         assertEquals(mServiceFake.screenBrightness, DEFAULT_BRIGHTNESS);
+
+        mScreen.transitionTo(INITIALIZED, DOZE);
+        assertEquals(mServiceFake.screenBrightness, DEFAULT_BRIGHTNESS);
     }
 
     @Test
     public void transitionToDoze_noClampBrightness_afterTimeout_noScreenOff_doesNotClampToDim() {
         when(mWakefulnessLifecycle.getLastSleepReason()).thenReturn(
                 PowerManager.GO_TO_SLEEP_REASON_TIMEOUT);
-        when(mDozeParameters.shouldControlUnlockedScreenOff()).thenReturn(false);
         when(mDozeParameters.shouldClampToDimBrightness()).thenReturn(false);
 
         mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
@@ -514,11 +517,9 @@
                 PowerManager.GO_TO_SLEEP_REASON_TIMEOUT);
         when(mWakefulnessLifecycle.getWakefulness()).thenReturn(
                 WakefulnessLifecycle.WAKEFULNESS_GOING_TO_SLEEP);
-        when(mDozeParameters.shouldControlUnlockedScreenOff()).thenReturn(false);
         when(mDozeParameters.shouldClampToDimBrightness()).thenReturn(false);
 
         mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
-        mScreen.transitionTo(INITIALIZED, DOZE);
 
         assertTrue(mServiceFake.screenBrightness <= DIM_BRIGHTNESS);
     }
@@ -529,7 +530,6 @@
                 PowerManager.GO_TO_SLEEP_REASON_POWER_BUTTON);
         when(mWakefulnessLifecycle.getWakefulness()).thenReturn(
                 WakefulnessLifecycle.WAKEFULNESS_GOING_TO_SLEEP);
-        when(mDozeParameters.shouldControlUnlockedScreenOff()).thenReturn(false);
         when(mDozeParameters.shouldClampToDimBrightness()).thenReturn(false);
 
         mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
diff --git a/packages/SystemUI/tests/src/com/android/systemui/dreams/DreamOverlayContainerViewControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/dreams/DreamOverlayContainerViewControllerTest.java
index ff9e13a..dcbdea0 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/dreams/DreamOverlayContainerViewControllerTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/dreams/DreamOverlayContainerViewControllerTest.java
@@ -16,7 +16,6 @@
 
 package com.android.systemui.dreams;
 
-import static org.junit.Assert.assertEquals;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.verify;
@@ -30,7 +29,6 @@
 
 import androidx.test.filters.SmallTest;
 
-import com.android.systemui.R;
 import com.android.systemui.SysuiTestCase;
 import com.android.systemui.dreams.complication.ComplicationHostViewController;
 
@@ -44,7 +42,6 @@
 @SmallTest
 @RunWith(AndroidTestingRunner.class)
 public class DreamOverlayContainerViewControllerTest extends SysuiTestCase {
-    private static final int DREAM_OVERLAY_NOTIFICATIONS_DRAG_AREA_HEIGHT = 100;
     private static final int MAX_BURN_IN_OFFSET = 20;
     private static final long BURN_IN_PROTECTION_UPDATE_INTERVAL = 10;
     private static final long MILLIS_UNTIL_FULL_JITTER = 240 * 1000;
@@ -76,9 +73,6 @@
     public void setup() {
         MockitoAnnotations.initMocks(this);
 
-        when(mResources.getDimensionPixelSize(
-                R.dimen.dream_overlay_notifications_drag_area_height)).thenReturn(
-                        DREAM_OVERLAY_NOTIFICATIONS_DRAG_AREA_HEIGHT);
         when(mDreamOverlayContainerView.getResources()).thenReturn(mResources);
         when(mDreamOverlayContainerView.getViewTreeObserver()).thenReturn(mViewTreeObserver);
 
@@ -100,13 +94,6 @@
     }
 
     @Test
-    public void testSetsDreamOverlayNotificationsDragAreaHeight() {
-        assertEquals(
-                mController.getDreamOverlayNotificationsDragAreaHeight(),
-                DREAM_OVERLAY_NOTIFICATIONS_DRAG_AREA_HEIGHT);
-    }
-
-    @Test
     public void testBurnInProtectionStartsWhenContentViewAttached() {
         mController.onViewAttached();
         verify(mHandler).postDelayed(any(Runnable.class), eq(BURN_IN_PROTECTION_UPDATE_INTERVAL));
diff --git a/packages/SystemUI/tests/src/com/android/systemui/dreams/DreamOverlayServiceTest.java b/packages/SystemUI/tests/src/com/android/systemui/dreams/DreamOverlayServiceTest.java
index 7d7ccb4..3657192 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/dreams/DreamOverlayServiceTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/dreams/DreamOverlayServiceTest.java
@@ -174,19 +174,19 @@
     }
 
     @Test
-    public void testShouldShowComplicationsTrueByDefault() {
+    public void testShouldShowComplicationsFalseByDefault() {
         mService.onBind(new Intent());
 
-        assertThat(mService.shouldShowComplications()).isTrue();
+        assertThat(mService.shouldShowComplications()).isFalse();
     }
 
     @Test
     public void testShouldShowComplicationsSetByIntentExtra() {
         final Intent intent = new Intent();
-        intent.putExtra(DreamService.EXTRA_SHOW_COMPLICATIONS, false);
+        intent.putExtra(DreamService.EXTRA_SHOW_COMPLICATIONS, true);
         mService.onBind(intent);
 
-        assertThat(mService.shouldShowComplications()).isFalse();
+        assertThat(mService.shouldShowComplications()).isTrue();
     }
 
     @Test
diff --git a/packages/SystemUI/tests/src/com/android/systemui/dreams/DreamOverlayStateControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/dreams/DreamOverlayStateControllerTest.java
index 49da4bd..3ce9889 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/dreams/DreamOverlayStateControllerTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/dreams/DreamOverlayStateControllerTest.java
@@ -158,6 +158,7 @@
     public void testComplicationFilteringWhenShouldShowComplications() {
         final DreamOverlayStateController stateController =
                 new DreamOverlayStateController(mExecutor);
+        stateController.setShouldShowComplications(true);
 
         final Complication alwaysAvailableComplication = Mockito.mock(Complication.class);
         final Complication weatherComplication = Mockito.mock(Complication.class);
diff --git a/packages/SystemUI/tests/src/com/android/systemui/media/MediaCarouselControllerTest.kt b/packages/SystemUI/tests/src/com/android/systemui/media/MediaCarouselControllerTest.kt
index dcbe0ab..daf81bd 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/media/MediaCarouselControllerTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/media/MediaCarouselControllerTest.kt
@@ -37,7 +37,6 @@
 import org.mockito.Mock
 import org.mockito.MockitoAnnotations
 import javax.inject.Provider
-import org.mockito.Mockito.`when` as whenever
 
 private val DATA = MediaData(
     userId = -1,
@@ -83,7 +82,6 @@
     @Before
     fun setup() {
         MockitoAnnotations.initMocks(this)
-        whenever(mediaFlags.areMediaSessionActionsEnabled()).thenReturn(true)
         mediaCarouselController = MediaCarouselController(
             context,
             mediaControlPanelFactory,
diff --git a/packages/SystemUI/tests/src/com/android/systemui/media/MediaControlPanelTest.kt b/packages/SystemUI/tests/src/com/android/systemui/media/MediaControlPanelTest.kt
index 708fc91..90eff1a 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/media/MediaControlPanelTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/media/MediaControlPanelTest.kt
@@ -40,6 +40,7 @@
 import androidx.test.filters.SmallTest
 import com.android.systemui.R
 import com.android.systemui.SysuiTestCase
+import com.android.systemui.broadcast.BroadcastSender
 import com.android.systemui.media.dialog.MediaOutputDialogFactory
 import com.android.systemui.plugins.ActivityStarter
 import com.android.systemui.plugins.FalsingManager
@@ -59,6 +60,7 @@
 import org.mockito.Mock
 import org.mockito.Mockito.mock
 import org.mockito.Mockito.never
+import org.mockito.Mockito.times
 import org.mockito.Mockito.verify
 import org.mockito.junit.MockitoJUnit
 import org.mockito.Mockito.`when` as whenever
@@ -85,6 +87,7 @@
 
     private lateinit var bgExecutor: FakeExecutor
     @Mock private lateinit var activityStarter: ActivityStarter
+    @Mock private lateinit var broadcastSender: BroadcastSender
 
     @Mock private lateinit var holder: PlayerViewHolder
     @Mock private lateinit var sessionHolder: PlayerSessionViewHolder
@@ -98,7 +101,6 @@
     @Mock private lateinit var mediaOutputDialogFactory: MediaOutputDialogFactory
     @Mock private lateinit var mediaCarouselController: MediaCarouselController
     @Mock private lateinit var falsingManager: FalsingManager
-    @Mock private lateinit var mediaFlags: MediaFlags
     private lateinit var appIcon: ImageView
     private lateinit var albumView: ImageView
     private lateinit var titleText: TextView
@@ -119,8 +121,6 @@
     private lateinit var actionPlayPause: ImageButton
     private lateinit var actionNext: ImageButton
     private lateinit var actionPrev: ImageButton
-    private lateinit var actionStart: ImageButton
-    private lateinit var actionEnd: ImageButton
     @Mock private lateinit var longPressText: TextView
     @Mock private lateinit var handler: Handler
     private lateinit var settings: View
@@ -144,9 +144,9 @@
         whenever(mediaViewController.expandedLayout).thenReturn(expandedSet)
         whenever(mediaViewController.collapsedLayout).thenReturn(collapsedSet)
 
-        player = MediaControlPanel(context, bgExecutor, activityStarter, mediaViewController,
-            seekBarViewModel, Lazy { mediaDataManager },
-            mediaOutputDialogFactory, mediaCarouselController, falsingManager, mediaFlags, clock)
+        player = MediaControlPanel(context, bgExecutor, activityStarter, broadcastSender,
+            mediaViewController, seekBarViewModel, Lazy { mediaDataManager },
+            mediaOutputDialogFactory, mediaCarouselController, falsingManager, clock)
         whenever(seekBarViewModel.progress).thenReturn(seekBarData)
 
         // Set up mock views for the players
@@ -168,6 +168,17 @@
         cancelText = TextView(context)
         dismiss = FrameLayout(context)
         dismissText = TextView(context)
+
+        action0 = ImageButton(context).also { it.setId(R.id.action0) }
+        action1 = ImageButton(context).also { it.setId(R.id.action1) }
+        action2 = ImageButton(context).also { it.setId(R.id.action2) }
+        action3 = ImageButton(context).also { it.setId(R.id.action3) }
+        action4 = ImageButton(context).also { it.setId(R.id.action4) }
+
+        actionPlayPause = ImageButton(context).also { it.setId(R.id.actionPlayPause) }
+        actionPrev = ImageButton(context).also { it.setId(R.id.actionPrev) }
+        actionNext = ImageButton(context).also { it.setId(R.id.actionNext) }
+
         initPlayerHolderMocks()
         initSessionHolderMocks()
 
@@ -203,95 +214,66 @@
                 device = device,
                 active = true,
                 resumeAction = null)
+    }
 
-        whenever(mediaFlags.areMediaSessionActionsEnabled()).thenReturn(false)
-        whenever(mediaFlags.useMediaSessionLayout()).thenReturn(false)
+    /**
+     * Initialize elements common to both view holders
+     */
+    private fun initMediaViewHolderMocks(viewHolder: MediaViewHolder) {
+        whenever(viewHolder.player).thenReturn(view)
+        whenever(viewHolder.appIcon).thenReturn(appIcon)
+        whenever(viewHolder.albumView).thenReturn(albumView)
+        whenever(viewHolder.titleText).thenReturn(titleText)
+        whenever(viewHolder.artistText).thenReturn(artistText)
+        whenever(seamlessBackground.getDrawable(0)).thenReturn(mock(GradientDrawable::class.java))
+        whenever(viewHolder.seamless).thenReturn(seamless)
+        whenever(viewHolder.seamlessButton).thenReturn(seamlessButton)
+        whenever(viewHolder.seamlessIcon).thenReturn(seamlessIcon)
+        whenever(viewHolder.seamlessText).thenReturn(seamlessText)
+        whenever(viewHolder.seekBar).thenReturn(seekBar)
+
+        // Action buttons
+        whenever(viewHolder.action0).thenReturn(action0)
+        whenever(viewHolder.getAction(R.id.action0)).thenReturn(action0)
+        whenever(viewHolder.action1).thenReturn(action1)
+        whenever(viewHolder.getAction(R.id.action1)).thenReturn(action1)
+        whenever(viewHolder.action2).thenReturn(action2)
+        whenever(viewHolder.getAction(R.id.action2)).thenReturn(action2)
+        whenever(viewHolder.action3).thenReturn(action3)
+        whenever(viewHolder.getAction(R.id.action3)).thenReturn(action3)
+        whenever(viewHolder.action4).thenReturn(action4)
+        whenever(viewHolder.getAction(R.id.action4)).thenReturn(action4)
+
+        // Long press menu
+        whenever(viewHolder.longPressText).thenReturn(longPressText)
+        whenever(longPressText.handler).thenReturn(handler)
+        whenever(viewHolder.settings).thenReturn(settings)
+        whenever(viewHolder.settingsText).thenReturn(settingsText)
+        whenever(viewHolder.cancel).thenReturn(cancel)
+        whenever(viewHolder.cancelText).thenReturn(cancelText)
+        whenever(viewHolder.dismiss).thenReturn(dismiss)
+        whenever(viewHolder.dismissText).thenReturn(dismissText)
     }
 
     /** Mock view holder for the notification player */
     private fun initPlayerHolderMocks() {
-        whenever(holder.player).thenReturn(view)
-        whenever(holder.appIcon).thenReturn(appIcon)
-        whenever(holder.albumView).thenReturn(albumView)
-        whenever(holder.titleText).thenReturn(titleText)
-        whenever(holder.artistText).thenReturn(artistText)
-        whenever(seamlessBackground.getDrawable(0)).thenReturn(mock(GradientDrawable::class.java))
-        whenever(holder.seamless).thenReturn(seamless)
-        whenever(holder.seamlessButton).thenReturn(seamlessButton)
-        whenever(holder.seamlessIcon).thenReturn(seamlessIcon)
-        whenever(holder.seamlessText).thenReturn(seamlessText)
-        whenever(holder.seekBar).thenReturn(seekBar)
+        initMediaViewHolderMocks(holder)
+
         whenever(holder.elapsedTimeView).thenReturn(elapsedTimeView)
         whenever(holder.totalTimeView).thenReturn(totalTimeView)
-
-        // Action buttons
-        action0 = ImageButton(context)
-        whenever(holder.action0).thenReturn(action0)
-        whenever(holder.getAction(R.id.action0)).thenReturn(action0)
-        action1 = ImageButton(context)
-        whenever(holder.action1).thenReturn(action1)
-        whenever(holder.getAction(R.id.action1)).thenReturn(action1)
-        action2 = ImageButton(context)
-        whenever(holder.action2).thenReturn(action2)
-        whenever(holder.getAction(R.id.action2)).thenReturn(action2)
-        action3 = ImageButton(context)
-        whenever(holder.action3).thenReturn(action3)
-        whenever(holder.getAction(R.id.action3)).thenReturn(action3)
-        action4 = ImageButton(context)
-        whenever(holder.action4).thenReturn(action4)
-        whenever(holder.getAction(R.id.action4)).thenReturn(action4)
-
-        // Long press menu
-        whenever(holder.longPressText).thenReturn(longPressText)
-        whenever(longPressText.handler).thenReturn(handler)
-        whenever(holder.settings).thenReturn(settings)
-        whenever(holder.settingsText).thenReturn(settingsText)
-        whenever(holder.cancel).thenReturn(cancel)
-        whenever(holder.cancelText).thenReturn(cancelText)
-        whenever(holder.dismiss).thenReturn(dismiss)
-        whenever(holder.dismissText).thenReturn(dismissText)
     }
 
     /** Mock view holder for session player */
     private fun initSessionHolderMocks() {
-        whenever(sessionHolder.player).thenReturn(view)
-        whenever(sessionHolder.albumView).thenReturn(albumView)
-        whenever(sessionHolder.appIcon).thenReturn(appIcon)
-        whenever(sessionHolder.titleText).thenReturn(titleText)
-        whenever(sessionHolder.artistText).thenReturn(artistText)
-        val seamlessBackground = mock(RippleDrawable::class.java)
-        whenever(seamlessBackground.getDrawable(0)).thenReturn(mock(GradientDrawable::class.java))
-        whenever(sessionHolder.seamless).thenReturn(seamless)
-        whenever(sessionHolder.seamlessButton).thenReturn(seamlessButton)
-        whenever(sessionHolder.seamlessIcon).thenReturn(seamlessIcon)
-        whenever(sessionHolder.seamlessText).thenReturn(seamlessText)
-        whenever(sessionHolder.seekBar).thenReturn(seekBar)
+        initMediaViewHolderMocks(sessionHolder)
 
-        // Action buttons
-        actionPlayPause = ImageButton(context)
+        // Semantic action buttons
         whenever(sessionHolder.actionPlayPause).thenReturn(actionPlayPause)
         whenever(sessionHolder.getAction(R.id.actionPlayPause)).thenReturn(actionPlayPause)
-        actionNext = ImageButton(context)
         whenever(sessionHolder.actionNext).thenReturn(actionNext)
         whenever(sessionHolder.getAction(R.id.actionNext)).thenReturn(actionNext)
-        actionPrev = ImageButton(context)
         whenever(sessionHolder.actionPrev).thenReturn(actionPrev)
         whenever(sessionHolder.getAction(R.id.actionPrev)).thenReturn(actionPrev)
-        actionStart = ImageButton(context)
-        whenever(sessionHolder.actionStart).thenReturn(actionStart)
-        whenever(sessionHolder.getAction(R.id.actionStart)).thenReturn(actionStart)
-        actionEnd = ImageButton(context)
-        whenever(sessionHolder.actionEnd).thenReturn(actionEnd)
-        whenever(sessionHolder.getAction(R.id.actionEnd)).thenReturn(actionEnd)
-
-        // Long press menu
-        whenever(sessionHolder.longPressText).thenReturn(longPressText)
-        whenever(sessionHolder.settings).thenReturn(settings)
-        whenever(sessionHolder.settingsText).thenReturn(settingsText)
-        whenever(sessionHolder.cancel).thenReturn(cancel)
-        whenever(sessionHolder.cancelText).thenReturn(cancelText)
-        whenever(sessionHolder.dismiss).thenReturn(dismiss)
-        whenever(sessionHolder.dismissText).thenReturn(dismissText)
     }
 
     @After
@@ -309,15 +291,12 @@
 
     @Test
     fun bindSemanticActionsOldLayout() {
-        whenever(mediaFlags.areMediaSessionActionsEnabled()).thenReturn(true)
-        whenever(mediaFlags.useMediaSessionLayout()).thenReturn(false)
-
         val icon = Icon.createWithResource(context, android.R.drawable.ic_media_play)
         val semanticActions = MediaButton(
             playOrPause = MediaAction(icon, Runnable {}, "play"),
             nextOrCustom = MediaAction(icon, Runnable {}, "next"),
-            startCustom = MediaAction(icon, null, "custom 1"),
-            endCustom = MediaAction(icon, null, "custom 2")
+            custom0 = MediaAction(icon, null, "custom 0"),
+            custom1 = MediaAction(icon, null, "custom 1")
         )
         val state = mediaData.copy(semanticActions = semanticActions)
 
@@ -325,7 +304,7 @@
         player.bindPlayer(state, PACKAGE)
 
         verify(expandedSet).setVisibility(R.id.action0, ConstraintSet.VISIBLE)
-        assertThat(action0.contentDescription).isEqualTo("custom 1")
+        assertThat(action0.contentDescription).isEqualTo("custom 0")
         assertThat(action0.isEnabled()).isFalse()
 
         verify(expandedSet).setVisibility(R.id.action1, ConstraintSet.INVISIBLE)
@@ -340,41 +319,103 @@
         assertThat(action3.contentDescription).isEqualTo("next")
 
         verify(expandedSet).setVisibility(R.id.action4, ConstraintSet.VISIBLE)
-        assertThat(action4.contentDescription).isEqualTo("custom 2")
+        assertThat(action4.contentDescription).isEqualTo("custom 1")
         assertThat(action4.isEnabled()).isFalse()
     }
 
     @Test
     fun bindSemanticActionsNewLayout() {
-        whenever(mediaFlags.areMediaSessionActionsEnabled()).thenReturn(true)
-        whenever(mediaFlags.useMediaSessionLayout()).thenReturn(true)
-
         val icon = Icon.createWithResource(context, android.R.drawable.ic_media_play)
         val semanticActions = MediaButton(
                 playOrPause = MediaAction(icon, Runnable {}, "play"),
                 nextOrCustom = MediaAction(icon, Runnable {}, "next"),
-                startCustom = MediaAction(icon, null, "custom 1"),
-                endCustom = MediaAction(icon, null, "custom 2")
+                custom0 = MediaAction(icon, null, "custom 0"),
+                custom1 = MediaAction(icon, null, "custom 1")
         )
         val state = mediaData.copy(semanticActions = semanticActions)
 
         player.attachPlayer(sessionHolder, MediaViewController.TYPE.PLAYER_SESSION)
         player.bindPlayer(state, PACKAGE)
 
-        assertThat(actionStart.contentDescription).isEqualTo("custom 1")
-        assertThat(actionStart.isEnabled()).isFalse()
-
         assertThat(actionPrev.isEnabled()).isFalse()
         assertThat(actionPrev.drawable).isNull()
+        verify(collapsedSet).setVisibility(R.id.actionPrev, ConstraintSet.GONE)
 
         assertThat(actionPlayPause.isEnabled()).isTrue()
         assertThat(actionPlayPause.contentDescription).isEqualTo("play")
+        verify(collapsedSet).setVisibility(R.id.actionPlayPause, ConstraintSet.VISIBLE)
 
         assertThat(actionNext.isEnabled()).isTrue()
         assertThat(actionNext.contentDescription).isEqualTo("next")
+        verify(collapsedSet).setVisibility(R.id.actionNext, ConstraintSet.VISIBLE)
 
-        assertThat(actionEnd.contentDescription).isEqualTo("custom 2")
-        assertThat(actionEnd.isEnabled()).isFalse()
+        // Called twice since these IDs are used as generic buttons
+        assertThat(action0.contentDescription).isEqualTo("custom 0")
+        assertThat(action0.isEnabled()).isFalse()
+        verify(collapsedSet, times(2)).setVisibility(R.id.action0, ConstraintSet.GONE)
+
+        assertThat(action1.contentDescription).isEqualTo("custom 1")
+        assertThat(action1.isEnabled()).isFalse()
+        verify(collapsedSet, times(2)).setVisibility(R.id.action1, ConstraintSet.GONE)
+
+        // Verify generic buttons are hidden
+        verify(collapsedSet).setVisibility(R.id.action2, ConstraintSet.GONE)
+        verify(expandedSet).setVisibility(R.id.action2, ConstraintSet.GONE)
+
+        verify(collapsedSet).setVisibility(R.id.action3, ConstraintSet.GONE)
+        verify(expandedSet).setVisibility(R.id.action3, ConstraintSet.GONE)
+
+        verify(collapsedSet).setVisibility(R.id.action4, ConstraintSet.GONE)
+        verify(expandedSet).setVisibility(R.id.action4, ConstraintSet.GONE)
+    }
+
+    @Test
+    fun bindNotificationActionsNewLayout() {
+        val icon = Icon.createWithResource(context, android.R.drawable.ic_media_play)
+        val actions = listOf(
+            MediaAction(icon, Runnable {}, "previous"),
+            MediaAction(icon, Runnable {}, "play"),
+            MediaAction(icon, null, "next"),
+            MediaAction(icon, null, "custom 0"),
+            MediaAction(icon, Runnable {}, "custom 1")
+        )
+        val state = mediaData.copy(actions = actions,
+            actionsToShowInCompact = listOf(1, 2),
+            semanticActions = null)
+
+        player.attachPlayer(sessionHolder, MediaViewController.TYPE.PLAYER_SESSION)
+        player.bindPlayer(state, PACKAGE)
+
+        // Verify semantic actions are hidden
+        verify(collapsedSet).setVisibility(R.id.actionPrev, ConstraintSet.GONE)
+        verify(expandedSet).setVisibility(R.id.actionPrev, ConstraintSet.GONE)
+
+        verify(collapsedSet).setVisibility(R.id.actionPlayPause, ConstraintSet.GONE)
+        verify(expandedSet).setVisibility(R.id.actionPlayPause, ConstraintSet.GONE)
+
+        verify(collapsedSet).setVisibility(R.id.actionNext, ConstraintSet.GONE)
+        verify(expandedSet).setVisibility(R.id.actionNext, ConstraintSet.GONE)
+
+        // Generic actions all enabled
+        assertThat(action0.contentDescription).isEqualTo("previous")
+        assertThat(action0.isEnabled()).isTrue()
+        verify(collapsedSet).setVisibility(R.id.action0, ConstraintSet.GONE)
+
+        assertThat(action1.contentDescription).isEqualTo("play")
+        assertThat(action1.isEnabled()).isTrue()
+        verify(collapsedSet).setVisibility(R.id.action1, ConstraintSet.VISIBLE)
+
+        assertThat(action2.contentDescription).isEqualTo("next")
+        assertThat(action2.isEnabled()).isFalse()
+        verify(collapsedSet).setVisibility(R.id.action2, ConstraintSet.VISIBLE)
+
+        assertThat(action3.contentDescription).isEqualTo("custom 0")
+        assertThat(action3.isEnabled()).isFalse()
+        verify(collapsedSet).setVisibility(R.id.action3, ConstraintSet.GONE)
+
+        assertThat(action4.contentDescription).isEqualTo("custom 1")
+        assertThat(action4.isEnabled()).isTrue()
+        verify(collapsedSet).setVisibility(R.id.action4, ConstraintSet.GONE)
     }
 
     @Test
diff --git a/packages/SystemUI/tests/src/com/android/systemui/media/MediaDataFilterTest.kt b/packages/SystemUI/tests/src/com/android/systemui/media/MediaDataFilterTest.kt
index 6b203bc..82a48ef 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/media/MediaDataFilterTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/media/MediaDataFilterTest.kt
@@ -23,6 +23,7 @@
 import android.testing.TestableLooper
 import com.android.systemui.SysuiTestCase
 import com.android.systemui.broadcast.BroadcastDispatcher
+import com.android.systemui.broadcast.BroadcastSender
 import com.android.systemui.statusbar.NotificationLockscreenUserManager
 import com.android.systemui.util.time.FakeSystemClock
 import com.google.common.truth.Truth.assertThat
@@ -65,6 +66,8 @@
     @Mock
     private lateinit var broadcastDispatcher: BroadcastDispatcher
     @Mock
+    private lateinit var broadcastSender: BroadcastSender
+    @Mock
     private lateinit var mediaResumeListener: MediaResumeListener
     @Mock
     private lateinit var mediaDataManager: MediaDataManager
@@ -87,7 +90,7 @@
     fun setup() {
         MockitoAnnotations.initMocks(this)
         MediaPlayerData.clear()
-        mediaDataFilter = MediaDataFilter(context, broadcastDispatcher,
+        mediaDataFilter = MediaDataFilter(context, broadcastDispatcher, broadcastSender,
                 lockscreenUserManager, executor, clock)
         mediaDataFilter.mediaDataManager = mediaDataManager
         mediaDataFilter.addListener(listener)
diff --git a/packages/SystemUI/tests/src/com/android/systemui/media/MediaDataManagerTest.kt b/packages/SystemUI/tests/src/com/android/systemui/media/MediaDataManagerTest.kt
index 021f70e..be2450d 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/media/MediaDataManagerTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/media/MediaDataManagerTest.kt
@@ -26,6 +26,7 @@
 import com.android.systemui.statusbar.SbnBuilder
 import com.android.systemui.tuner.TunerService
 import com.android.systemui.util.concurrency.FakeExecutor
+import com.android.systemui.util.mockito.any
 import com.android.systemui.util.mockito.capture
 import com.android.systemui.util.mockito.eq
 import com.android.systemui.util.time.FakeSystemClock
@@ -37,6 +38,7 @@
 import org.junit.runner.RunWith
 import org.mockito.ArgumentCaptor
 import org.mockito.ArgumentMatchers.anyBoolean
+import org.mockito.ArgumentMatchers.anyInt
 import org.mockito.Captor
 import org.mockito.Mock
 import org.mockito.Mockito
@@ -167,7 +169,7 @@
         whenever(mediaSmartspaceTarget.featureType).thenReturn(SmartspaceTarget.FEATURE_MEDIA)
         whenever(mediaSmartspaceTarget.iconGrid).thenReturn(listOf(mediaRecommendationItem))
         whenever(mediaSmartspaceTarget.creationTimeMillis).thenReturn(1234L)
-        whenever(mediaFlags.areMediaSessionActionsEnabled()).thenReturn(false)
+        whenever(mediaFlags.areMediaSessionActionsEnabled(any(), anyInt())).thenReturn(false)
     }
 
     @After
@@ -594,7 +596,7 @@
     @Test
     fun testPlaybackActions_noState_usesNotification() {
         val desc = "Notification Action"
-        whenever(mediaFlags.areMediaSessionActionsEnabled()).thenReturn(true)
+        whenever(mediaFlags.areMediaSessionActionsEnabled(any(), anyInt())).thenReturn(true)
         whenever(controller.playbackState).thenReturn(null)
 
         val notifWithAction = SbnBuilder().run {
@@ -621,7 +623,7 @@
     @Test
     fun testPlaybackActions_hasPrevNext() {
         val customDesc = arrayOf("custom 1", "custom 2", "custom 3", "custom 4")
-        whenever(mediaFlags.areMediaSessionActionsEnabled()).thenReturn(true)
+        whenever(mediaFlags.areMediaSessionActionsEnabled(any(), anyInt())).thenReturn(true)
         val stateActions = PlaybackState.ACTION_PLAY or
                 PlaybackState.ACTION_SKIP_TO_PREVIOUS or
                 PlaybackState.ACTION_SKIP_TO_NEXT
@@ -659,17 +661,17 @@
         actions.nextOrCustom!!.action!!.run()
         verify(transportControls).skipToNext()
 
-        assertThat(actions.startCustom).isNotNull()
-        assertThat(actions.startCustom!!.contentDescription).isEqualTo(customDesc[0])
+        assertThat(actions.custom0).isNotNull()
+        assertThat(actions.custom0!!.contentDescription).isEqualTo(customDesc[0])
 
-        assertThat(actions.endCustom).isNotNull()
-        assertThat(actions.endCustom!!.contentDescription).isEqualTo(customDesc[1])
+        assertThat(actions.custom1).isNotNull()
+        assertThat(actions.custom1!!.contentDescription).isEqualTo(customDesc[1])
     }
 
     @Test
     fun testPlaybackActions_noPrevNext_usesCustom() {
         val customDesc = arrayOf("custom 1", "custom 2", "custom 3", "custom 4", "custom 5")
-        whenever(mediaFlags.areMediaSessionActionsEnabled()).thenReturn(true)
+        whenever(mediaFlags.areMediaSessionActionsEnabled(any(), anyInt())).thenReturn(true)
         val stateActions = PlaybackState.ACTION_PLAY
         val stateBuilder = PlaybackState.Builder()
                 .setActions(stateActions)
@@ -697,17 +699,17 @@
         assertThat(actions.nextOrCustom).isNotNull()
         assertThat(actions.nextOrCustom!!.contentDescription).isEqualTo(customDesc[1])
 
-        assertThat(actions.startCustom).isNotNull()
-        assertThat(actions.startCustom!!.contentDescription).isEqualTo(customDesc[2])
+        assertThat(actions.custom0).isNotNull()
+        assertThat(actions.custom0!!.contentDescription).isEqualTo(customDesc[2])
 
-        assertThat(actions.endCustom).isNotNull()
-        assertThat(actions.endCustom!!.contentDescription).isEqualTo(customDesc[3])
+        assertThat(actions.custom1).isNotNull()
+        assertThat(actions.custom1!!.contentDescription).isEqualTo(customDesc[3])
     }
 
     @Test
     fun testPlaybackActions_reservedSpace() {
         val customDesc = arrayOf("custom 1", "custom 2", "custom 3", "custom 4")
-        whenever(mediaFlags.areMediaSessionActionsEnabled()).thenReturn(true)
+        whenever(mediaFlags.areMediaSessionActionsEnabled(any(), anyInt())).thenReturn(true)
         val stateActions = PlaybackState.ACTION_PLAY
         val stateBuilder = PlaybackState.Builder()
                 .setActions(stateActions)
@@ -737,10 +739,10 @@
         assertThat(actions.prevOrCustom).isNull()
         assertThat(actions.nextOrCustom).isNull()
 
-        assertThat(actions.startCustom).isNotNull()
-        assertThat(actions.startCustom!!.contentDescription).isEqualTo(customDesc[0])
+        assertThat(actions.custom0).isNotNull()
+        assertThat(actions.custom0!!.contentDescription).isEqualTo(customDesc[0])
 
-        assertThat(actions.endCustom).isNotNull()
-        assertThat(actions.endCustom!!.contentDescription).isEqualTo(customDesc[1])
+        assertThat(actions.custom1).isNotNull()
+        assertThat(actions.custom1!!.contentDescription).isEqualTo(customDesc[1])
     }
 }
diff --git a/packages/SystemUI/tests/src/com/android/systemui/media/MediaHierarchyManagerTest.kt b/packages/SystemUI/tests/src/com/android/systemui/media/MediaHierarchyManagerTest.kt
index b359ae5..8e201b5 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/media/MediaHierarchyManagerTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/media/MediaHierarchyManagerTest.kt
@@ -117,9 +117,9 @@
                 dreamOverlayStateController)
         verify(wakefulnessLifecycle).addObserver(wakefullnessObserver.capture())
         verify(statusBarStateController).addCallback(statusBarCallback.capture())
-        setupHost(lockHost, MediaHierarchyManager.LOCATION_LOCKSCREEN)
-        setupHost(qsHost, MediaHierarchyManager.LOCATION_QS)
-        setupHost(qqsHost, MediaHierarchyManager.LOCATION_QQS)
+        setupHost(lockHost, MediaHierarchyManager.LOCATION_LOCKSCREEN, LOCKSCREEN_TOP)
+        setupHost(qsHost, MediaHierarchyManager.LOCATION_QS, QS_TOP)
+        setupHost(qqsHost, MediaHierarchyManager.LOCATION_QQS, QQS_TOP)
         `when`(statusBarStateController.state).thenReturn(StatusBarState.SHADE)
         `when`(mediaCarouselController.mediaCarouselScrollHandler)
                 .thenReturn(mediaCarouselScrollHandler)
@@ -130,9 +130,9 @@
         clearInvocations(mediaCarouselController)
     }
 
-    private fun setupHost(host: MediaHost, location: Int) {
+    private fun setupHost(host: MediaHost, location: Int, top: Int) {
         `when`(host.location).thenReturn(location)
-        `when`(host.currentBounds).thenReturn(Rect())
+        `when`(host.currentBounds).thenReturn(Rect(0, top, 0, top))
         `when`(host.hostView).thenReturn(uniqueObjectHostView)
         `when`(host.visible).thenReturn(true)
         mediaHiearchyManager.register(host)
@@ -257,6 +257,20 @@
         verify(mediaCarouselController).closeGuts()
     }
 
+    @Test
+    fun getGuidedTransformationTranslationY_notInGuidedTransformation_returnsNegativeNumber() {
+        assertThat(mediaHiearchyManager.getGuidedTransformationTranslationY()).isLessThan(0)
+    }
+
+    @Test
+    fun getGuidedTransformationTranslationY_inGuidedTransformation_returnsCurrentTranslation() {
+        enterGuidedTransformation()
+
+        val expectedTranslation = LOCKSCREEN_TOP - QS_TOP
+        assertThat(mediaHiearchyManager.getGuidedTransformationTranslationY())
+                .isEqualTo(expectedTranslation)
+    }
+
     private fun enableSplitShade() {
         context.getOrCreateTestableResources().addOverride(
             R.bool.config_use_split_notification_shade, true
@@ -284,4 +298,16 @@
     private fun expandQS() {
         mediaHiearchyManager.qsExpansion = 1.0f
     }
+
+    private fun enterGuidedTransformation() {
+        mediaHiearchyManager.qsExpansion = 1.0f
+        goToLockscreen()
+        mediaHiearchyManager.setTransitionToFullShadeAmount(123f)
+    }
+
+    companion object {
+        private const val QQS_TOP = 123
+        private const val QS_TOP = 456
+        private const val LOCKSCREEN_TOP = 789
+    }
 }
diff --git a/packages/SystemUI/tests/src/com/android/systemui/media/MediaViewControllerTest.kt b/packages/SystemUI/tests/src/com/android/systemui/media/MediaViewControllerTest.kt
new file mode 100644
index 0000000..b7d5ba1
--- /dev/null
+++ b/packages/SystemUI/tests/src/com/android/systemui/media/MediaViewControllerTest.kt
@@ -0,0 +1,55 @@
+package com.android.systemui.media
+
+import android.testing.AndroidTestingRunner
+import android.testing.TestableLooper
+import android.view.View
+import androidx.test.filters.SmallTest
+import com.android.systemui.SysuiTestCase
+import com.android.systemui.util.animation.MeasurementInput
+import com.android.systemui.util.animation.TransitionLayout
+import com.android.systemui.util.animation.TransitionViewState
+import junit.framework.Assert.assertTrue
+import org.junit.Before
+import org.junit.Test
+import org.junit.runner.RunWith
+
+/**
+ * Tests for {@link MediaViewController}.
+ */
+@SmallTest
+@RunWith(AndroidTestingRunner::class)
+@TestableLooper.RunWithLooper
+class MediaViewControllerTest : SysuiTestCase() {
+    private val configurationController =
+            com.android.systemui.statusbar.phone.ConfigurationControllerImpl(context)
+    private val mediaHostStatesManager = MediaHostStatesManager()
+    private val mediaViewController =
+            MediaViewController(context, configurationController, mediaHostStatesManager)
+    private val mediaHostStateHolder = MediaHost.MediaHostStateHolder()
+    private var transitionLayout = TransitionLayout(context, /* attrs */ null, /* defStyleAttr */ 0)
+
+    @Before
+    fun setUp() {
+        mediaViewController.attach(transitionLayout, MediaViewController.TYPE.PLAYER)
+    }
+
+    @Test
+    fun testObtainViewState_applySquishFraction_toTransitionViewState_height() {
+        transitionLayout.measureState = TransitionViewState().apply {
+            this.height = 100
+        }
+        mediaHostStateHolder.expansion = 1f
+        val widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY)
+        val heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY)
+        mediaHostStateHolder.measurementInput =
+                MeasurementInput(widthMeasureSpec, heightMeasureSpec)
+
+        // Test no squish
+        mediaHostStateHolder.squishFraction = 1f
+        assertTrue(mediaViewController.obtainViewState(mediaHostStateHolder)!!.height == 100)
+
+        // Test half squish
+        mediaHostStateHolder.squishFraction = 0.5f
+        assertTrue(mediaViewController.obtainViewState(mediaHostStateHolder)!!.height == 50)
+    }
+}
\ No newline at end of file
diff --git a/packages/SystemUI/tests/src/com/android/systemui/media/SeekBarObserverTest.kt b/packages/SystemUI/tests/src/com/android/systemui/media/SeekBarObserverTest.kt
index 3c2392a..7ac15125 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/media/SeekBarObserverTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/media/SeekBarObserverTest.kt
@@ -26,29 +26,33 @@
 import com.android.systemui.SysuiTestCase
 import com.google.common.truth.Truth.assertThat
 import org.junit.Before
+import org.junit.Rule
 import org.junit.Test
 import org.junit.runner.RunWith
 import org.mockito.Mock
-import org.mockito.Mockito.mock
+import org.mockito.Mockito.verify
+import org.mockito.junit.MockitoJUnit
 import org.mockito.Mockito.`when` as whenever
 
 @SmallTest
 @RunWith(AndroidTestingRunner::class)
 @TestableLooper.RunWithLooper
-public class SeekBarObserverTest : SysuiTestCase() {
+class SeekBarObserverTest : SysuiTestCase() {
 
     private val disabledHeight = 1
     private val enabledHeight = 2
 
     private lateinit var observer: SeekBarObserver
     @Mock private lateinit var mockHolder: PlayerViewHolder
+    @Mock private lateinit var mockSquigglyProgress: SquigglyProgress
     private lateinit var seekBarView: SeekBar
     private lateinit var elapsedTimeView: TextView
     private lateinit var totalTimeView: TextView
 
+    @JvmField @Rule val mockitoRule = MockitoJUnit.rule()
+
     @Before
     fun setUp() {
-        mockHolder = mock(PlayerViewHolder::class.java)
 
         context.orCreateTestableResources
             .addOverride(R.dimen.qs_media_enabled_seekbar_height, enabledHeight)
@@ -56,6 +60,7 @@
             .addOverride(R.dimen.qs_media_disabled_seekbar_height, disabledHeight)
 
         seekBarView = SeekBar(context)
+        seekBarView.progressDrawable = mockSquigglyProgress
         elapsedTimeView = TextView(context)
         totalTimeView = TextView(context)
         whenever(mockHolder.seekBar).thenReturn(seekBarView)
@@ -69,7 +74,7 @@
     fun seekBarGone() {
         // WHEN seek bar is disabled
         val isEnabled = false
-        val data = SeekBarViewModel.Progress(isEnabled, false, null, 0)
+        val data = SeekBarViewModel.Progress(isEnabled, false, false, null, 0)
         observer.onChanged(data)
         // THEN seek bar shows just a thin line with no text
         assertThat(seekBarView.isEnabled()).isFalse()
@@ -84,7 +89,7 @@
     fun seekBarVisible() {
         // WHEN seek bar is enabled
         val isEnabled = true
-        val data = SeekBarViewModel.Progress(isEnabled, true, 3000, 12000)
+        val data = SeekBarViewModel.Progress(isEnabled, true, false, 3000, 12000)
         observer.onChanged(data)
         // THEN seek bar is visible and thick
         assertThat(seekBarView.getVisibility()).isEqualTo(View.VISIBLE)
@@ -96,7 +101,7 @@
     @Test
     fun seekBarProgress() {
         // WHEN part of the track has been played
-        val data = SeekBarViewModel.Progress(true, true, 3000, 120000)
+        val data = SeekBarViewModel.Progress(true, true, true, 3000, 120000)
         observer.onChanged(data)
         // THEN seek bar shows the progress
         assertThat(seekBarView.progress).isEqualTo(3000)
@@ -112,7 +117,7 @@
     fun seekBarDisabledWhenSeekNotAvailable() {
         // WHEN seek is not available
         val isSeekAvailable = false
-        val data = SeekBarViewModel.Progress(true, isSeekAvailable, 3000, 120000)
+        val data = SeekBarViewModel.Progress(true, isSeekAvailable, false, 3000, 120000)
         observer.onChanged(data)
         // THEN seek bar is not enabled
         assertThat(seekBarView.isEnabled()).isFalse()
@@ -122,9 +127,29 @@
     fun seekBarEnabledWhenSeekNotAvailable() {
         // WHEN seek is available
         val isSeekAvailable = true
-        val data = SeekBarViewModel.Progress(true, isSeekAvailable, 3000, 120000)
+        val data = SeekBarViewModel.Progress(true, isSeekAvailable, false, 3000, 120000)
         observer.onChanged(data)
         // THEN seek bar is not enabled
         assertThat(seekBarView.isEnabled()).isTrue()
     }
+
+    @Test
+    fun seekBarPlaying() {
+        // WHEN playing
+        val isPlaying = true
+        val data = SeekBarViewModel.Progress(true, true, isPlaying, 3000, 120000)
+        observer.onChanged(data)
+        // THEN progress drawable is animating
+        verify(mockSquigglyProgress).animate = true
+    }
+
+    @Test
+    fun seekBarNotPlaying() {
+        // WHEN not playing
+        val isPlaying = false
+        val data = SeekBarViewModel.Progress(true, true, isPlaying, 3000, 120000)
+        observer.onChanged(data)
+        // THEN progress drawable is not animating
+        verify(mockSquigglyProgress).animate = false
+    }
 }
diff --git a/packages/SystemUI/tests/src/com/android/systemui/media/SquigglyProgressTest.kt b/packages/SystemUI/tests/src/com/android/systemui/media/SquigglyProgressTest.kt
new file mode 100644
index 0000000..0787fd6
--- /dev/null
+++ b/packages/SystemUI/tests/src/com/android/systemui/media/SquigglyProgressTest.kt
@@ -0,0 +1,118 @@
+package com.android.systemui.media
+
+import android.graphics.Canvas
+import android.graphics.Color
+import android.graphics.LightingColorFilter
+import android.graphics.Paint
+import android.graphics.Rect
+import android.testing.AndroidTestingRunner
+import android.testing.TestableLooper
+import androidx.test.filters.SmallTest
+import com.android.systemui.SysuiTestCase
+import com.android.systemui.util.mockito.any
+import com.google.common.truth.Truth.assertThat
+import org.junit.Before
+import org.junit.Rule
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.mockito.ArgumentCaptor
+import org.mockito.Captor
+import org.mockito.Mock
+import org.mockito.Mockito.anyFloat
+import org.mockito.Mockito.verify
+import org.mockito.junit.MockitoJUnit
+
+@SmallTest
+@RunWith(AndroidTestingRunner::class)
+@TestableLooper.RunWithLooper
+class SquigglyProgressTest : SysuiTestCase() {
+
+    private val colorFilter = LightingColorFilter(Color.RED, Color.BLUE)
+    private val strokeWidth = 5f
+    private val alpha = 128
+    private val tint = Color.GREEN
+
+    lateinit var squigglyProgress: SquigglyProgress
+    @Mock lateinit var canvas: Canvas
+    @Captor lateinit var wavePaintCaptor: ArgumentCaptor<Paint>
+    @Captor lateinit var linePaintCaptor: ArgumentCaptor<Paint>
+    @JvmField @Rule val mockitoRule = MockitoJUnit.rule()
+
+    @Before
+    fun setup() {
+        squigglyProgress = SquigglyProgress()
+        squigglyProgress.waveLength = 30f
+        squigglyProgress.lineAmplitude = 10f
+        squigglyProgress.phaseSpeed = 8f
+        squigglyProgress.strokeWidth = strokeWidth
+        squigglyProgress.bounds = Rect(0, 0, 300, 30)
+    }
+
+    @Test
+    fun testDrawPathAndLine() {
+        squigglyProgress.draw(canvas)
+
+        verify(canvas).drawPath(any(), wavePaintCaptor.capture())
+        verify(canvas).drawLine(anyFloat(), anyFloat(), anyFloat(), anyFloat(),
+                linePaintCaptor.capture())
+    }
+
+    @Test
+    fun testOnLevelChanged() {
+        assertThat(squigglyProgress.setLevel(5)).isFalse()
+        squigglyProgress.animate = true
+        assertThat(squigglyProgress.setLevel(4)).isTrue()
+    }
+
+    @Test
+    fun testStrokeWidth() {
+        squigglyProgress.draw(canvas)
+
+        verify(canvas).drawPath(any(), wavePaintCaptor.capture())
+        verify(canvas).drawLine(anyFloat(), anyFloat(), anyFloat(), anyFloat(),
+                linePaintCaptor.capture())
+
+        assertThat(wavePaintCaptor.value.strokeWidth).isEqualTo(strokeWidth)
+        assertThat(linePaintCaptor.value.strokeWidth).isEqualTo(strokeWidth)
+    }
+
+    @Test
+    fun testAlpha() {
+        squigglyProgress.alpha = alpha
+        squigglyProgress.draw(canvas)
+
+        verify(canvas).drawPath(any(), wavePaintCaptor.capture())
+        verify(canvas).drawLine(anyFloat(), anyFloat(), anyFloat(), anyFloat(),
+                linePaintCaptor.capture())
+
+        assertThat(squigglyProgress.alpha).isEqualTo(alpha)
+        assertThat(wavePaintCaptor.value.alpha).isEqualTo(alpha)
+        assertThat(linePaintCaptor.value.alpha).isEqualTo((alpha / 255f * DISABLED_ALPHA).toInt())
+    }
+
+    @Test
+    fun testColorFilter() {
+        squigglyProgress.colorFilter = colorFilter
+        squigglyProgress.draw(canvas)
+
+        verify(canvas).drawPath(any(), wavePaintCaptor.capture())
+        verify(canvas).drawLine(anyFloat(), anyFloat(), anyFloat(), anyFloat(),
+                linePaintCaptor.capture())
+
+        assertThat(wavePaintCaptor.value.colorFilter).isEqualTo(colorFilter)
+        assertThat(linePaintCaptor.value.colorFilter).isEqualTo(colorFilter)
+    }
+
+    @Test
+    fun testTint() {
+        squigglyProgress.setTint(tint)
+        squigglyProgress.draw(canvas)
+
+        verify(canvas).drawPath(any(), wavePaintCaptor.capture())
+        verify(canvas).drawLine(anyFloat(), anyFloat(), anyFloat(), anyFloat(),
+                linePaintCaptor.capture())
+
+        assertThat(wavePaintCaptor.value.color).isEqualTo(tint)
+        assertThat(linePaintCaptor.value.color).isEqualTo(tint)
+    }
+}
\ No newline at end of file
diff --git a/packages/SystemUI/tests/src/com/android/systemui/media/dialog/MediaOutputBaseDialogTest.java b/packages/SystemUI/tests/src/com/android/systemui/media/dialog/MediaOutputBaseDialogTest.java
index 13e5821..380fa6d 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/media/dialog/MediaOutputBaseDialogTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/media/dialog/MediaOutputBaseDialogTest.java
@@ -37,15 +37,14 @@
 import androidx.core.graphics.drawable.IconCompat;
 import androidx.test.filters.SmallTest;
 
-import com.android.internal.logging.UiEventLogger;
 import com.android.settingslib.bluetooth.LocalBluetoothManager;
 import com.android.systemui.R;
 import com.android.systemui.SysuiTestCase;
 import com.android.systemui.animation.DialogLaunchAnimator;
+import com.android.systemui.broadcast.BroadcastSender;
 import com.android.systemui.media.nearby.NearbyMediaDevicesManager;
 import com.android.systemui.plugins.ActivityStarter;
 import com.android.systemui.statusbar.notification.NotificationEntryManager;
-import com.android.systemui.statusbar.phone.ShadeController;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -64,11 +63,10 @@
     private MediaOutputBaseAdapter mMediaOutputBaseAdapter = mock(MediaOutputBaseAdapter.class);
     private MediaSessionManager mMediaSessionManager = mock(MediaSessionManager.class);
     private LocalBluetoothManager mLocalBluetoothManager = mock(LocalBluetoothManager.class);
-    private ShadeController mShadeController = mock(ShadeController.class);
     private ActivityStarter mStarter = mock(ActivityStarter.class);
+    private BroadcastSender mBroadcastSender = mock(BroadcastSender.class);
     private NotificationEntryManager mNotificationEntryManager =
             mock(NotificationEntryManager.class);
-    private final UiEventLogger mUiEventLogger = mock(UiEventLogger.class);
     private NearbyMediaDevicesManager mNearbyMediaDevicesManager = mock(
             NearbyMediaDevicesManager.class);
     private final DialogLaunchAnimator mDialogLaunchAnimator = mock(DialogLaunchAnimator.class);
@@ -77,17 +75,16 @@
     private MediaOutputController mMediaOutputController;
     private int mHeaderIconRes;
     private IconCompat mIconCompat;
-    private Drawable mAppSourceDrawable;
     private CharSequence mHeaderTitle;
     private CharSequence mHeaderSubtitle;
 
     @Before
     public void setUp() {
-        mMediaOutputController = new MediaOutputController(mContext, TEST_PACKAGE, false,
-                mMediaSessionManager, mLocalBluetoothManager, mShadeController, mStarter,
-                mNotificationEntryManager, mUiEventLogger, mDialogLaunchAnimator,
+        mMediaOutputController = new MediaOutputController(mContext, TEST_PACKAGE,
+                mMediaSessionManager, mLocalBluetoothManager, mStarter,
+                mNotificationEntryManager, mDialogLaunchAnimator,
                 Optional.of(mNearbyMediaDevicesManager));
-        mMediaOutputBaseDialogImpl = new MediaOutputBaseDialogImpl(mContext,
+        mMediaOutputBaseDialogImpl = new MediaOutputBaseDialogImpl(mContext, mBroadcastSender,
                 mMediaOutputController);
         mMediaOutputBaseDialogImpl.onCreate(new Bundle());
     }
@@ -178,15 +175,16 @@
 
     class MediaOutputBaseDialogImpl extends MediaOutputBaseDialog {
 
-        MediaOutputBaseDialogImpl(Context context, MediaOutputController mediaOutputController) {
-            super(context, mediaOutputController);
+        MediaOutputBaseDialogImpl(Context context, BroadcastSender broadcastSender,
+                MediaOutputController mediaOutputController) {
+            super(context, broadcastSender, mediaOutputController);
 
             mAdapter = mMediaOutputBaseAdapter;
         }
 
         @Override
         Drawable getAppSourceIcon() {
-            return mAppSourceDrawable;
+            return null;
         }
 
         @Override
diff --git a/packages/SystemUI/tests/src/com/android/systemui/media/dialog/MediaOutputControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/media/dialog/MediaOutputControllerTest.java
index 6230700..d2dae74 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/media/dialog/MediaOutputControllerTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/media/dialog/MediaOutputControllerTest.java
@@ -45,7 +45,6 @@
 import androidx.core.graphics.drawable.IconCompat;
 import androidx.test.filters.SmallTest;
 
-import com.android.internal.logging.UiEventLogger;
 import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
 import com.android.settingslib.bluetooth.LocalBluetoothManager;
 import com.android.settingslib.media.LocalMediaManager;
@@ -57,7 +56,6 @@
 import com.android.systemui.plugins.ActivityStarter;
 import com.android.systemui.statusbar.notification.collection.NotificationEntry;
 import com.android.systemui.statusbar.notification.collection.notifcollection.CommonNotifCollection;
-import com.android.systemui.statusbar.phone.ShadeController;
 
 import com.google.common.collect.ImmutableList;
 
@@ -96,10 +94,8 @@
     private NearbyDevice mNearbyDevice2 = mock(NearbyDevice.class);
     private MediaMetadata mMediaMetadata = mock(MediaMetadata.class);
     private RoutingSessionInfo mRemoteSessionInfo = mock(RoutingSessionInfo.class);
-    private ShadeController mShadeController = mock(ShadeController.class);
     private ActivityStarter mStarter = mock(ActivityStarter.class);
     private CommonNotifCollection mNotifCollection = mock(CommonNotifCollection.class);
-    private final UiEventLogger mUiEventLogger = mock(UiEventLogger.class);
     private final DialogLaunchAnimator mDialogLaunchAnimator = mock(DialogLaunchAnimator.class);
     private final NearbyMediaDevicesManager mNearbyMediaDevicesManager = mock(
             NearbyMediaDevicesManager.class);
@@ -124,9 +120,9 @@
         when(mLocalBluetoothManager.getCachedDeviceManager()).thenReturn(
                 mCachedBluetoothDeviceManager);
 
-        mMediaOutputController = new MediaOutputController(mSpyContext, TEST_PACKAGE_NAME, false,
-                mMediaSessionManager, mLocalBluetoothManager, mShadeController, mStarter,
-                mNotifCollection, mUiEventLogger, mDialogLaunchAnimator,
+        mMediaOutputController = new MediaOutputController(mSpyContext, TEST_PACKAGE_NAME,
+                mMediaSessionManager, mLocalBluetoothManager, mStarter,
+                mNotifCollection, mDialogLaunchAnimator,
                 Optional.of(mNearbyMediaDevicesManager));
         mLocalMediaManager = spy(mMediaOutputController.mLocalMediaManager);
         mMediaOutputController.mLocalMediaManager = mLocalMediaManager;
@@ -176,9 +172,9 @@
 
     @Test
     public void start_withoutPackageName_verifyMediaControllerInit() {
-        mMediaOutputController = new MediaOutputController(mSpyContext, null, false,
-                mMediaSessionManager, mLocalBluetoothManager, mShadeController, mStarter,
-                mNotifCollection, mUiEventLogger, mDialogLaunchAnimator,
+        mMediaOutputController = new MediaOutputController(mSpyContext, null,
+                mMediaSessionManager, mLocalBluetoothManager, mStarter,
+                mNotifCollection, mDialogLaunchAnimator,
                 Optional.of(mNearbyMediaDevicesManager));
 
         mMediaOutputController.start(mCb);
@@ -205,9 +201,9 @@
 
     @Test
     public void stop_withoutPackageName_verifyMediaControllerDeinit() {
-        mMediaOutputController = new MediaOutputController(mSpyContext, null, false,
-                mMediaSessionManager, mLocalBluetoothManager, mShadeController, mStarter,
-                mNotifCollection, mUiEventLogger, mDialogLaunchAnimator,
+        mMediaOutputController = new MediaOutputController(mSpyContext, null,
+                mMediaSessionManager, mLocalBluetoothManager, mStarter,
+                mNotifCollection, mDialogLaunchAnimator,
                 Optional.of(mNearbyMediaDevicesManager));
 
         mMediaOutputController.start(mCb);
@@ -510,9 +506,9 @@
 
     @Test
     public void getNotificationLargeIcon_withoutPackageName_returnsNull() {
-        mMediaOutputController = new MediaOutputController(mSpyContext, null, false,
-                mMediaSessionManager, mLocalBluetoothManager, mShadeController, mStarter,
-                mNotifCollection, mUiEventLogger, mDialogLaunchAnimator,
+        mMediaOutputController = new MediaOutputController(mSpyContext, null,
+                mMediaSessionManager, mLocalBluetoothManager, mStarter,
+                mNotifCollection, mDialogLaunchAnimator,
                 Optional.of(mNearbyMediaDevicesManager));
 
         assertThat(mMediaOutputController.getNotificationIcon()).isNull();
diff --git a/packages/SystemUI/tests/src/com/android/systemui/media/dialog/MediaOutputDialogTest.java b/packages/SystemUI/tests/src/com/android/systemui/media/dialog/MediaOutputDialogTest.java
index cb52e7c..db56f87 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/media/dialog/MediaOutputDialogTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/media/dialog/MediaOutputDialogTest.java
@@ -37,10 +37,10 @@
 import com.android.settingslib.media.MediaDevice;
 import com.android.systemui.SysuiTestCase;
 import com.android.systemui.animation.DialogLaunchAnimator;
+import com.android.systemui.broadcast.BroadcastSender;
 import com.android.systemui.media.nearby.NearbyMediaDevicesManager;
 import com.android.systemui.plugins.ActivityStarter;
 import com.android.systemui.statusbar.notification.NotificationEntryManager;
-import com.android.systemui.statusbar.phone.ShadeController;
 
 import org.junit.After;
 import org.junit.Before;
@@ -61,8 +61,8 @@
     // Mock
     private final MediaSessionManager mMediaSessionManager = mock(MediaSessionManager.class);
     private final LocalBluetoothManager mLocalBluetoothManager = mock(LocalBluetoothManager.class);
-    private final ShadeController mShadeController = mock(ShadeController.class);
     private final ActivityStarter mStarter = mock(ActivityStarter.class);
+    private final BroadcastSender mBroadcastSender = mock(BroadcastSender.class);
     private final LocalMediaManager mLocalMediaManager = mock(LocalMediaManager.class);
     private final MediaDevice mMediaDevice = mock(MediaDevice.class);
     private final NotificationEntryManager mNotificationEntryManager =
@@ -78,12 +78,12 @@
 
     @Before
     public void setUp() {
-        mMediaOutputController = new MediaOutputController(mContext, TEST_PACKAGE, false,
-                mMediaSessionManager, mLocalBluetoothManager, mShadeController, mStarter,
-                mNotificationEntryManager, mUiEventLogger, mDialogLaunchAnimator,
+        mMediaOutputController = new MediaOutputController(mContext, TEST_PACKAGE,
+                mMediaSessionManager, mLocalBluetoothManager, mStarter,
+                mNotificationEntryManager, mDialogLaunchAnimator,
                 Optional.of(mNearbyMediaDevicesManager));
         mMediaOutputController.mLocalMediaManager = mLocalMediaManager;
-        mMediaOutputDialog = new MediaOutputDialog(mContext, false,
+        mMediaOutputDialog = new MediaOutputDialog(mContext, false, mBroadcastSender,
                 mMediaOutputController, mUiEventLogger);
         mMediaOutputDialog.show();
 
@@ -129,7 +129,7 @@
     // Check the visibility metric logging by creating a new MediaOutput dialog,
     // and verify if the calling times increases.
     public void onCreate_ShouldLogVisibility() {
-        MediaOutputDialog testDialog = new MediaOutputDialog(mContext, false,
+        MediaOutputDialog testDialog = new MediaOutputDialog(mContext, false, mBroadcastSender,
                 mMediaOutputController, mUiEventLogger);
         testDialog.show();
 
diff --git a/packages/SystemUI/tests/src/com/android/systemui/media/dialog/MediaOutputGroupDialogTest.java b/packages/SystemUI/tests/src/com/android/systemui/media/dialog/MediaOutputGroupDialogTest.java
index f186f57..0cdde07 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/media/dialog/MediaOutputGroupDialogTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/media/dialog/MediaOutputGroupDialogTest.java
@@ -28,17 +28,16 @@
 
 import androidx.test.filters.SmallTest;
 
-import com.android.internal.logging.UiEventLogger;
 import com.android.settingslib.bluetooth.LocalBluetoothManager;
 import com.android.settingslib.media.LocalMediaManager;
 import com.android.settingslib.media.MediaDevice;
 import com.android.systemui.R;
 import com.android.systemui.SysuiTestCase;
 import com.android.systemui.animation.DialogLaunchAnimator;
+import com.android.systemui.broadcast.BroadcastSender;
 import com.android.systemui.media.nearby.NearbyMediaDevicesManager;
 import com.android.systemui.plugins.ActivityStarter;
 import com.android.systemui.statusbar.notification.NotificationEntryManager;
-import com.android.systemui.statusbar.phone.ShadeController;
 
 import org.junit.After;
 import org.junit.Before;
@@ -59,14 +58,13 @@
     // Mock
     private MediaSessionManager mMediaSessionManager = mock(MediaSessionManager.class);
     private LocalBluetoothManager mLocalBluetoothManager = mock(LocalBluetoothManager.class);
-    private ShadeController mShadeController = mock(ShadeController.class);
     private ActivityStarter mStarter = mock(ActivityStarter.class);
+    private BroadcastSender mBroadcastSender = mock(BroadcastSender.class);
     private LocalMediaManager mLocalMediaManager = mock(LocalMediaManager.class);
     private MediaDevice mMediaDevice = mock(MediaDevice.class);
     private MediaDevice mMediaDevice1 = mock(MediaDevice.class);
     private NotificationEntryManager mNotificationEntryManager =
             mock(NotificationEntryManager.class);
-    private final UiEventLogger mUiEventLogger = mock(UiEventLogger.class);
     private final DialogLaunchAnimator mDialogLaunchAnimator = mock(DialogLaunchAnimator.class);
     private NearbyMediaDevicesManager mNearbyMediaDevicesManager = mock(
             NearbyMediaDevicesManager.class);
@@ -77,12 +75,12 @@
 
     @Before
     public void setUp() {
-        mMediaOutputController = new MediaOutputController(mContext, TEST_PACKAGE, false,
-                mMediaSessionManager, mLocalBluetoothManager, mShadeController, mStarter,
-                mNotificationEntryManager, mUiEventLogger, mDialogLaunchAnimator,
+        mMediaOutputController = new MediaOutputController(mContext, TEST_PACKAGE,
+                mMediaSessionManager, mLocalBluetoothManager, mStarter,
+                mNotificationEntryManager, mDialogLaunchAnimator,
                 Optional.of(mNearbyMediaDevicesManager));
         mMediaOutputController.mLocalMediaManager = mLocalMediaManager;
-        mMediaOutputGroupDialog = new MediaOutputGroupDialog(mContext, false,
+        mMediaOutputGroupDialog = new MediaOutputGroupDialog(mContext, false, mBroadcastSender,
                 mMediaOutputController);
         mMediaOutputGroupDialog.show();
         when(mLocalMediaManager.getSelectedMediaDevice()).thenReturn(mMediaDevices);
diff --git a/packages/SystemUI/tests/src/com/android/systemui/power/PowerNotificationWarningsTest.java b/packages/SystemUI/tests/src/com/android/systemui/power/PowerNotificationWarningsTest.java
index afb63ab..a156820 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/power/PowerNotificationWarningsTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/power/PowerNotificationWarningsTest.java
@@ -37,6 +37,7 @@
 
 import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
 import com.android.systemui.SysuiTestCase;
+import com.android.systemui.broadcast.BroadcastSender;
 import com.android.systemui.plugins.ActivityStarter;
 import com.android.systemui.util.NotificationChannels;
 
@@ -59,7 +60,9 @@
         // Test Instance.
         mContext.addMockSystemService(NotificationManager.class, mMockNotificationManager);
         ActivityStarter starter = mDependency.injectMockDependency(ActivityStarter.class);
-        mPowerNotificationWarnings = new PowerNotificationWarnings(mContext, starter);
+        BroadcastSender broadcastSender = mDependency.injectMockDependency(BroadcastSender.class);
+        mPowerNotificationWarnings = new PowerNotificationWarnings(mContext, starter,
+                broadcastSender);
         BatteryStateSnapshot snapshot = new BatteryStateSnapshot(100, false, false, 1,
                 BatteryManager.BATTERY_HEALTH_GOOD, 5, 15);
         mPowerNotificationWarnings.updateSnapshot(snapshot);
diff --git a/packages/SystemUI/tests/src/com/android/systemui/qs/external/TileServicesTest.java b/packages/SystemUI/tests/src/com/android/systemui/qs/external/TileServicesTest.java
index 19a9863..6b7e5b93 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/qs/external/TileServicesTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/qs/external/TileServicesTest.java
@@ -20,21 +20,18 @@
 
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyLong;
-import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
-import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.Intent;
-import android.content.IntentFilter;
 import android.os.Handler;
-import android.os.Looper;
+import android.os.RemoteException;
 import android.os.UserHandle;
-import android.service.quicksettings.TileService;
+import android.service.quicksettings.IQSTileService;
 import android.test.suitebuilder.annotation.SmallTest;
 import android.testing.AndroidTestingRunner;
 import android.testing.TestableLooper;
@@ -49,6 +46,7 @@
 import com.android.systemui.qs.tileimpl.QSFactoryImpl;
 import com.android.systemui.settings.UserTracker;
 import com.android.systemui.shared.plugins.PluginManager;
+import com.android.systemui.statusbar.CommandQueue;
 import com.android.systemui.statusbar.phone.AutoTileManager;
 import com.android.systemui.statusbar.phone.CentralSurfaces;
 import com.android.systemui.statusbar.phone.StatusBarIconController;
@@ -68,17 +66,25 @@
 import java.util.ArrayList;
 import java.util.Optional;
 
+import javax.inject.Provider;
+
 @SmallTest
 @RunWith(AndroidTestingRunner.class)
 @RunWithLooper
 public class TileServicesTest extends SysuiTestCase {
     private static int NUM_FAKES = TileServices.DEFAULT_MAX_BOUND * 2;
 
+    private static final ComponentName TEST_COMPONENT =
+            ComponentName.unflattenFromString("pkg/.cls");
+
     private TileServices mTileService;
+    private TestableLooper mTestableLooper;
     private ArrayList<TileServiceManager> mManagers;
     @Mock
     private BroadcastDispatcher mBroadcastDispatcher;
     @Mock
+    private CommandQueue mCommandQueue;
+    @Mock
     private StatusBarIconController mStatusBarIconController;
     @Mock
     private QSFactoryImpl mQSFactory;
@@ -116,17 +122,20 @@
         MockitoAnnotations.initMocks(this);
         mDependency.injectMockDependency(BluetoothController.class);
         mManagers = new ArrayList<>();
+        mTestableLooper = TestableLooper.get(this);
 
         when(mTileServiceRequestControllerBuilder.create(any()))
                 .thenReturn(mTileServiceRequestController);
         when(mTileLifecycleManagerFactory.create(any(Intent.class), any(UserHandle.class)))
                 .thenReturn(mTileLifecycleManager);
 
+        Provider<Handler> provider = () -> new Handler(mTestableLooper.getLooper());
+
         QSTileHost host = new QSTileHost(mContext,
                 mStatusBarIconController,
                 mQSFactory,
-                new Handler(),
-                Looper.myLooper(),
+                provider.get(),
+                mTestableLooper.getLooper(),
                 mPluginManager,
                 mTunerService,
                 () -> mAutoTileManager,
@@ -140,8 +149,8 @@
                 mock(CustomTileStatePersister.class),
                 mTileServiceRequestControllerBuilder,
                 mTileLifecycleManagerFactory);
-        mTileService = new TestTileServices(host, Looper.getMainLooper(), mBroadcastDispatcher,
-                mUserTracker, mKeyguardStateController);
+        mTileService = new TestTileServices(host, provider, mBroadcastDispatcher,
+                mUserTracker, mKeyguardStateController, mCommandQueue);
     }
 
     @After
@@ -152,24 +161,6 @@
     }
 
     @Test
-    public void testActiveTileListenerRegisteredOnAllUsers() {
-        ArgumentCaptor<IntentFilter> captor = ArgumentCaptor.forClass(IntentFilter.class);
-        verify(mBroadcastDispatcher).registerReceiver(any(), captor.capture(), any(), eq(
-                UserHandle.ALL));
-        assertTrue(captor.getValue().hasAction(TileService.ACTION_REQUEST_LISTENING));
-    }
-
-    @Test
-    public void testBadComponentName_doesntCrash() {
-        ArgumentCaptor<BroadcastReceiver> captor = ArgumentCaptor.forClass(BroadcastReceiver.class);
-        verify(mBroadcastDispatcher).registerReceiver(captor.capture(), any(), any(), eq(
-                UserHandle.ALL));
-        Intent intent = new Intent(TileService.ACTION_REQUEST_LISTENING)
-                .putExtra(Intent.EXTRA_COMPONENT_NAME, "abc");
-        captor.getValue().onReceive(mContext, intent);
-    }
-
-    @Test
     public void testRecalculateBindAllowance() {
         // Add some fake tiles.
         for (int i = 0; i < NUM_FAKES; i++) {
@@ -225,11 +216,36 @@
         }
     }
 
+    @Test
+    public void testRegisterCommand() {
+        verify(mCommandQueue).addCallback(any());
+    }
+
+    @Test
+    public void testRequestListeningStatusCommand() throws RemoteException {
+        ArgumentCaptor<CommandQueue.Callbacks> captor =
+                ArgumentCaptor.forClass(CommandQueue.Callbacks.class);
+        verify(mCommandQueue).addCallback(captor.capture());
+
+        CustomTile mockTile = mock(CustomTile.class);
+        when(mockTile.getComponent()).thenReturn(TEST_COMPONENT);
+
+        TileServiceManager manager = mTileService.getTileWrapper(mockTile);
+        when(manager.isActiveTile()).thenReturn(true);
+        when(manager.getTileService()).thenReturn(mock(IQSTileService.class));
+
+        captor.getValue().requestTileServiceListeningState(TEST_COMPONENT);
+        mTestableLooper.processAllMessages();
+        verify(manager).setBindRequested(true);
+        verify(manager.getTileService()).onStartListening();
+    }
+
     private class TestTileServices extends TileServices {
-        TestTileServices(QSTileHost host, Looper looper,
+        TestTileServices(QSTileHost host, Provider<Handler> handlerProvider,
                 BroadcastDispatcher broadcastDispatcher, UserTracker userTracker,
-                KeyguardStateController keyguardStateController) {
-            super(host, looper, broadcastDispatcher, userTracker, keyguardStateController);
+                KeyguardStateController keyguardStateController, CommandQueue commandQueue) {
+            super(host, handlerProvider, broadcastDispatcher, userTracker, keyguardStateController,
+                    commandQueue);
         }
 
         @Override
diff --git a/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/QRCodeScannerTileTest.java b/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/QRCodeScannerTileTest.java
index 8297850..b652aee 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/QRCodeScannerTileTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/QRCodeScannerTileTest.java
@@ -104,8 +104,6 @@
         assertEquals(state.label, mContext.getString(R.string.qr_code_scanner_title));
         assertEquals(state.contentDescription, mContext.getString(R.string.qr_code_scanner_title));
         assertEquals(state.icon, QSTileImpl.ResourceIcon.get(R.drawable.ic_qr_code_scanner));
-        assertEquals(state.secondaryLabel,
-                mContext.getString(R.string.qr_code_scanner_description));
     }
 
     @Test
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/KeyguardIndicationControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/KeyguardIndicationControllerTest.java
index 3c1a73e..ad643fe 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/KeyguardIndicationControllerTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/KeyguardIndicationControllerTest.java
@@ -226,6 +226,10 @@
     @After
     public void tearDown() throws Exception {
         mTextView.setAnimationsEnabled(true);
+        if (mController != null) {
+            mController.destroy();
+            mController = null;
+        }
     }
 
     private void createController() {
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/LockscreenShadeTransitionControllerTest.kt b/packages/SystemUI/tests/src/com/android/systemui/statusbar/LockscreenShadeTransitionControllerTest.kt
index 067caa9..64a0a23 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/LockscreenShadeTransitionControllerTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/LockscreenShadeTransitionControllerTest.kt
@@ -44,6 +44,7 @@
 import org.mockito.Mockito.verify
 import org.mockito.junit.MockitoJUnit
 import org.mockito.Mockito.`when` as whenever
+import org.mockito.ArgumentMatchers.eq
 
 private fun <T> anyObject(): T {
     return Mockito.anyObject<T>()
@@ -260,6 +261,49 @@
     }
 
     @Test
+    fun setDragAmount_setsKeyguardTransitionProgress() {
+        transitionController.dragDownAmount = 10f
+
+        verify(notificationPanelController).setKeyguardTransitionProgress(anyFloat(), anyInt())
+    }
+
+    @Test
+    fun setDragAmount_setsKeyguardAlphaBasedOnDistance() {
+        val alphaDistance = context.resources.getDimensionPixelSize(
+                R.dimen.lockscreen_shade_npvc_keyguard_content_alpha_transition_distance)
+        transitionController.dragDownAmount = 10f
+
+        val expectedAlpha = 1 - 10f / alphaDistance
+        verify(notificationPanelController)
+                .setKeyguardTransitionProgress(eq(expectedAlpha), anyInt())
+    }
+
+    @Test
+    fun setDragAmount_notInSplitShade_setsKeyguardTranslationToZero() {
+        val mediaTranslationY = 123
+        disableSplitShade()
+        whenever(mediaHierarchyManager.getGuidedTransformationTranslationY())
+                .thenReturn(mediaTranslationY)
+
+        transitionController.dragDownAmount = 10f
+
+        verify(notificationPanelController).setKeyguardTransitionProgress(anyFloat(), eq(0))
+    }
+
+    @Test
+    fun setDragAmount_inSplitShade_setsKeyguardTranslationBasedOnMediaTranslation() {
+        val mediaTranslationY = 123
+        enableSplitShade()
+        whenever(mediaHierarchyManager.getGuidedTransformationTranslationY())
+                .thenReturn(mediaTranslationY)
+
+        transitionController.dragDownAmount = 10f
+
+        verify(notificationPanelController)
+                .setKeyguardTransitionProgress(anyFloat(), eq(mediaTranslationY))
+    }
+
+    @Test
     fun setDragDownAmount_setsValueOnMediaHierarchyManager() {
         transitionController.dragDownAmount = 10f
 
@@ -276,8 +320,16 @@
     }
 
     private fun enableSplitShade() {
+        setSplitShadeEnabled(true)
+    }
+
+    private fun disableSplitShade() {
+        setSplitShadeEnabled(false)
+    }
+
+    private fun setSplitShadeEnabled(enabled: Boolean) {
         context.getOrCreateTestableResources().addOverride(
-            R.bool.config_use_split_notification_shade, true
+                R.bool.config_use_split_notification_shade, enabled
         )
         configurationController.notifyConfigurationChanged()
     }
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/NotificationLaunchAnimatorControllerTest.kt b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/NotificationLaunchAnimatorControllerTest.kt
index 3a60c04..9f82a567 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/NotificationLaunchAnimatorControllerTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/NotificationLaunchAnimatorControllerTest.kt
@@ -31,6 +31,7 @@
     @Mock lateinit var notificationListContainer: NotificationListContainer
     @Mock lateinit var headsUpManager: HeadsUpManagerPhone
     @Mock lateinit var jankMonitor: InteractionJankMonitor
+    @Mock lateinit var onFinishAnimationCallback: Runnable
 
     private lateinit var notificationTestHelper: NotificationTestHelper
     private lateinit var notification: ExpandableNotificationRow
@@ -52,7 +53,8 @@
                 notificationListContainer,
                 headsUpManager,
                 notification,
-                jankMonitor
+                jankMonitor,
+                onFinishAnimationCallback
         )
     }
 
@@ -61,7 +63,7 @@
     }
 
     @Test
-    fun testHunIsRemovedIfWeDontAnimateLaunch() {
+    fun testHunIsRemovedAndCallbackIsInvokedIfWeDontAnimateLaunch() {
         flagNotificationAsHun()
         controller.onIntentStarted(willAnimate = false)
 
@@ -69,10 +71,11 @@
         assertFalse(notification.entry.isExpandAnimationRunning)
         verify(headsUpManager).removeNotification(
                 notificationKey, true /* releaseImmediately */, true /* animate */)
+        verify(onFinishAnimationCallback).run()
     }
 
     @Test
-    fun testHunIsRemovedWhenAnimationIsCancelled() {
+    fun testHunIsRemovedAndCallbackIsInvokedWhenAnimationIsCancelled() {
         flagNotificationAsHun()
         controller.onLaunchAnimationCancelled()
 
@@ -80,10 +83,11 @@
         assertFalse(notification.entry.isExpandAnimationRunning)
         verify(headsUpManager).removeNotification(
                 notificationKey, true /* releaseImmediately */, true /* animate */)
+        verify(onFinishAnimationCallback).run()
     }
 
     @Test
-    fun testHunIsRemovedWhenAnimationEnds() {
+    fun testHunIsRemovedAndCallbackIsInvokedWhenAnimationEnds() {
         flagNotificationAsHun()
         controller.onLaunchAnimationEnd(isExpandingFullyAbove = true)
 
@@ -91,6 +95,7 @@
         assertFalse(notification.entry.isExpandAnimationRunning)
         verify(headsUpManager).removeNotification(
                 notificationKey, true /* releaseImmediately */, false /* animate */)
+        verify(onFinishAnimationCallback).run()
     }
 
     @Test
@@ -99,4 +104,4 @@
 
         assertTrue(notification.entry.isExpandAnimationRunning)
     }
-}
\ No newline at end of file
+}
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/coordinator/HeadsUpCoordinatorTest.kt b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/coordinator/HeadsUpCoordinatorTest.kt
index 144eefb..699f77f 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/coordinator/HeadsUpCoordinatorTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/coordinator/HeadsUpCoordinatorTest.kt
@@ -170,11 +170,41 @@
     }
 
     @Test
+    fun testCancelAndReAddStickyNotification() {
+        whenever(mHeadsUpManager.isSticky(anyString())).thenReturn(true)
+        addHUN(mEntry)
+        whenever(mHeadsUpManager.canRemoveImmediately(anyString())).thenReturn(false, true, false)
+        whenever(mHeadsUpManager.getEarliestRemovalTime(anyString())).thenReturn(1000L)
+        assertTrue(mNotifLifetimeExtender.maybeExtendLifetime(mEntry, 0))
+        addHUN(mEntry)
+        assertFalse(mNotifLifetimeExtender.maybeExtendLifetime(mEntry, 0))
+        mExecutor.advanceClockToLast()
+        mExecutor.runAllReady()
+        assertTrue(mNotifLifetimeExtender.maybeExtendLifetime(mEntry, 0))
+        verify(mHeadsUpManager, times(0)).removeNotification(anyString(), eq(false))
+        verify(mHeadsUpManager, times(0)).removeNotification(anyString(), eq(true))
+    }
+
+    @Test
+    fun hunNotRemovedWhenExtensionCancelled() {
+        whenever(mHeadsUpManager.isSticky(anyString())).thenReturn(true)
+        addHUN(mEntry)
+        whenever(mHeadsUpManager.canRemoveImmediately(anyString())).thenReturn(false)
+        whenever(mHeadsUpManager.getEarliestRemovalTime(anyString())).thenReturn(1000L)
+        assertTrue(mNotifLifetimeExtender.maybeExtendLifetime(mEntry, 0))
+        mNotifLifetimeExtender.cancelLifetimeExtension(mEntry)
+        mExecutor.advanceClockToLast()
+        mExecutor.runAllReady()
+        verify(mHeadsUpManager, times(0)).removeNotification(anyString(), any())
+    }
+
+    @Test
     fun testCancelUpdatedStickyNotification() {
         whenever(mHeadsUpManager.isSticky(anyString())).thenReturn(true)
         addHUN(mEntry)
         whenever(mHeadsUpManager.getEarliestRemovalTime(anyString())).thenReturn(1000L, 500L)
         assertTrue(mNotifLifetimeExtender.maybeExtendLifetime(mEntry, 0))
+        addHUN(mEntry)
         mExecutor.advanceClockToLast()
         mExecutor.runAllReady()
         verify(mHeadsUpManager, times(0)).removeNotification(anyString(), eq(false))
@@ -305,6 +335,7 @@
         mHuns.add(entry)
         whenever(mHeadsUpManager.topEntry).thenReturn(entry)
         mOnHeadsUpChangedListener.onHeadsUpStateChanged(entry, true)
+        mNotifLifetimeExtender.cancelLifetimeExtension(entry)
     }
 
     @Test
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/coordinator/KeyguardCoordinatorTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/coordinator/KeyguardCoordinatorTest.java
index d094749..52bacd2 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/coordinator/KeyguardCoordinatorTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/coordinator/KeyguardCoordinatorTest.java
@@ -16,18 +16,8 @@
 
 package com.android.systemui.statusbar.notification.collection.coordinator;
 
-import static android.app.Notification.VISIBILITY_PUBLIC;
-import static android.app.Notification.VISIBILITY_SECRET;
-import static android.app.NotificationManager.IMPORTANCE_HIGH;
-import static android.app.NotificationManager.IMPORTANCE_MIN;
-
-import static com.android.systemui.statusbar.notification.collection.EntryUtilKt.modifyEntry;
-
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
 
 import android.os.Handler;
 import android.os.UserHandle;
@@ -39,40 +29,41 @@
 import com.android.systemui.SysuiTestCase;
 import com.android.systemui.broadcast.BroadcastDispatcher;
 import com.android.systemui.plugins.statusbar.StatusBarStateController;
-import com.android.systemui.statusbar.NotificationLockscreenUserManager;
-import com.android.systemui.statusbar.RankingBuilder;
 import com.android.systemui.statusbar.notification.SectionHeaderVisibilityProvider;
-import com.android.systemui.statusbar.notification.collection.GroupEntry;
-import com.android.systemui.statusbar.notification.collection.GroupEntryBuilder;
 import com.android.systemui.statusbar.notification.collection.NotifPipeline;
 import com.android.systemui.statusbar.notification.collection.NotificationEntry;
 import com.android.systemui.statusbar.notification.collection.NotificationEntryBuilder;
 import com.android.systemui.statusbar.notification.collection.listbuilder.pluggable.NotifFilter;
 import com.android.systemui.statusbar.notification.collection.provider.HighPriorityProvider;
+import com.android.systemui.statusbar.notification.interruption.KeyguardNotificationVisibilityProvider;
 import com.android.systemui.statusbar.policy.KeyguardStateController;
 
 import org.junit.Before;
-import org.junit.Test;
+import org.junit.Ignore;
 import org.junit.runner.RunWith;
 import org.mockito.ArgumentCaptor;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 
+/**
+ * TODO(b/224771204) Create test cases
+ */
 @SmallTest
 @RunWith(AndroidTestingRunner.class)
+@Ignore
 public class KeyguardCoordinatorTest extends SysuiTestCase {
     private static final int NOTIF_USER_ID = 0;
     private static final int CURR_USER_ID = 1;
 
     @Mock private Handler mMainHandler;
     @Mock private KeyguardStateController mKeyguardStateController;
-    @Mock private NotificationLockscreenUserManager mLockscreenUserManager;
     @Mock private BroadcastDispatcher mBroadcastDispatcher;
     @Mock private StatusBarStateController mStatusBarStateController;
     @Mock private KeyguardUpdateMonitor mKeyguardUpdateMonitor;
     @Mock private HighPriorityProvider mHighPriorityProvider;
     @Mock private SectionHeaderVisibilityProvider mSectionHeaderVisibilityProvider;
     @Mock private NotifPipeline mNotifPipeline;
+    @Mock private KeyguardNotificationVisibilityProvider mKeyguardNotificationVisibilityProvider;
 
     private NotificationEntry mEntry;
     private NotifFilter mKeyguardFilter;
@@ -81,9 +72,9 @@
     public void setup() {
         MockitoAnnotations.initMocks(this);
         KeyguardCoordinator keyguardCoordinator = new KeyguardCoordinator(
-                mContext, mMainHandler, mKeyguardStateController, mLockscreenUserManager,
-                mBroadcastDispatcher, mStatusBarStateController,
-                mKeyguardUpdateMonitor, mHighPriorityProvider, mSectionHeaderVisibilityProvider);
+                mStatusBarStateController,
+                mKeyguardUpdateMonitor, mHighPriorityProvider, mSectionHeaderVisibilityProvider,
+                mKeyguardNotificationVisibilityProvider);
 
         mEntry = new NotificationEntryBuilder()
                 .setUser(new UserHandle(NOTIF_USER_ID))
@@ -94,171 +85,4 @@
         verify(mNotifPipeline, times(1)).addFinalizeFilter(filterCaptor.capture());
         mKeyguardFilter = filterCaptor.getValue();
     }
-
-    @Test
-    public void unfilteredState() {
-        // GIVEN an 'unfiltered-keyguard-showing' state
-        setupUnfilteredState(mEntry);
-
-        // THEN don't filter out the entry
-        assertFalse(mKeyguardFilter.shouldFilterOut(mEntry, 0));
-    }
-
-    @Test
-    public void keyguardNotShowing() {
-        // GIVEN the lockscreen isn't showing
-        setupUnfilteredState(mEntry);
-        when(mKeyguardStateController.isShowing()).thenReturn(false);
-
-        // THEN don't filter out the entry
-        assertFalse(mKeyguardFilter.shouldFilterOut(mEntry, 0));
-    }
-
-    @Test
-    public void doNotShowLockscreenNotifications() {
-        // GIVEN an 'unfiltered-keyguard-showing' state
-        setupUnfilteredState(mEntry);
-
-        // WHEN we shouldn't show any lockscreen notifications
-        when(mLockscreenUserManager.shouldShowLockscreenNotifications()).thenReturn(false);
-
-        // THEN filter out the entry
-        assertTrue(mKeyguardFilter.shouldFilterOut(mEntry, 0));
-    }
-
-    @Test
-    public void lockdown() {
-        // GIVEN an 'unfiltered-keyguard-showing' state
-        setupUnfilteredState(mEntry);
-
-        // WHEN the notification's user is in lockdown:
-        when(mKeyguardUpdateMonitor.isUserInLockdown(NOTIF_USER_ID)).thenReturn(true);
-
-        // THEN filter out the entry
-        assertTrue(mKeyguardFilter.shouldFilterOut(mEntry, 0));
-    }
-
-    @Test
-    public void publicMode_settingsDisallow() {
-        // GIVEN an 'unfiltered-keyguard-showing' state
-        setupUnfilteredState(mEntry);
-
-        // WHEN the notification's user is in public mode and settings are configured to disallow
-        // notifications in public mode
-        when(mLockscreenUserManager.isLockscreenPublicMode(NOTIF_USER_ID)).thenReturn(true);
-        when(mLockscreenUserManager.userAllowsNotificationsInPublic(NOTIF_USER_ID))
-                .thenReturn(false);
-
-        // THEN filter out the entry
-        assertTrue(mKeyguardFilter.shouldFilterOut(mEntry, 0));
-    }
-
-    @Test
-    public void publicMode_notifDisallowed() {
-        // GIVEN an 'unfiltered-keyguard-showing' state
-        setupUnfilteredState(mEntry);
-
-        // WHEN the notification's user is in public mode and settings are configured to disallow
-        // notifications in public mode
-        when(mLockscreenUserManager.isLockscreenPublicMode(CURR_USER_ID)).thenReturn(true);
-        mEntry.setRanking(new RankingBuilder()
-                .setKey(mEntry.getKey())
-                .setVisibilityOverride(VISIBILITY_SECRET).build());
-
-        // THEN filter out the entry
-        assertTrue(mKeyguardFilter.shouldFilterOut(mEntry, 0));
-    }
-
-    @Test
-    public void doesNotExceedThresholdToShow() {
-        // GIVEN an 'unfiltered-keyguard-showing' state
-        setupUnfilteredState(mEntry);
-
-        // WHEN the notification doesn't exceed the threshold to show on the lockscreen
-        mEntry.setRanking(new RankingBuilder()
-                .setKey(mEntry.getKey())
-                .setImportance(IMPORTANCE_MIN)
-                .build());
-        when(mHighPriorityProvider.isHighPriority(mEntry)).thenReturn(false);
-
-        // THEN filter out the entry
-        assertTrue(mKeyguardFilter.shouldFilterOut(mEntry, 0));
-    }
-
-    @Test
-    public void summaryExceedsThresholdToShow() {
-        // GIVEN the notification doesn't exceed the threshold to show on the lockscreen
-        // but it's part of a group (has a parent)
-        final NotificationEntry entryWithParent = new NotificationEntryBuilder()
-                .setUser(new UserHandle(NOTIF_USER_ID))
-                .build();
-
-        final GroupEntry parent = new GroupEntryBuilder()
-                .setKey("test_group_key")
-                .setSummary(new NotificationEntryBuilder()
-                        .setImportance(IMPORTANCE_HIGH)
-                        .build())
-                .addChild(entryWithParent)
-                .build();
-
-        setupUnfilteredState(entryWithParent);
-        entryWithParent.setRanking(new RankingBuilder()
-                .setKey(entryWithParent.getKey())
-                .setImportance(IMPORTANCE_MIN)
-                .build());
-
-        // WHEN its parent does exceed threshold tot show on the lockscreen
-        when(mHighPriorityProvider.isHighPriority(parent)).thenReturn(true);
-
-        // THEN don't filter out the entry
-        assertFalse(mKeyguardFilter.shouldFilterOut(entryWithParent, 0));
-
-        // WHEN its parent doesn't exceed threshold to show on lockscreen
-        when(mHighPriorityProvider.isHighPriority(parent)).thenReturn(false);
-        modifyEntry(parent.getSummary(), builder -> builder
-                .setImportance(IMPORTANCE_MIN)
-                .done());
-
-        // THEN filter out the entry
-        assertTrue(mKeyguardFilter.shouldFilterOut(entryWithParent, 0));
-    }
-
-    /**
-     * setup a state where the notification will not be filtered by the
-     * KeyguardNotificationCoordinator when the keyguard is showing.
-     */
-    private void setupUnfilteredState(NotificationEntry entry) {
-        // keyguard is showing
-        when(mKeyguardStateController.isShowing()).thenReturn(true);
-
-        // show notifications on the lockscreen
-        when(mLockscreenUserManager.shouldShowLockscreenNotifications()).thenReturn(true);
-
-        // neither the current user nor the notification's user is in lockdown
-        when(mLockscreenUserManager.getCurrentUserId()).thenReturn(CURR_USER_ID);
-        when(mKeyguardUpdateMonitor.isUserInLockdown(NOTIF_USER_ID)).thenReturn(false);
-        when(mKeyguardUpdateMonitor.isUserInLockdown(CURR_USER_ID)).thenReturn(false);
-
-        // not in public mode
-        when(mLockscreenUserManager.isLockscreenPublicMode(CURR_USER_ID)).thenReturn(false);
-        when(mLockscreenUserManager.isLockscreenPublicMode(NOTIF_USER_ID)).thenReturn(false);
-
-        // entry's ranking - should show on all lockscreens
-        // + priority of the notification exceeds the threshold to be shown on the lockscreen
-        entry.setRanking(new RankingBuilder()
-                .setKey(mEntry.getKey())
-                .setVisibilityOverride(VISIBILITY_PUBLIC)
-                .setImportance(IMPORTANCE_HIGH)
-                .build());
-
-        // settings allows notifications in public mode
-        when(mLockscreenUserManager.userAllowsNotificationsInPublic(CURR_USER_ID)).thenReturn(true);
-        when(mLockscreenUserManager.userAllowsNotificationsInPublic(NOTIF_USER_ID))
-                .thenReturn(true);
-
-        // notification doesn't have a summary
-
-        // notification is high priority, so it shouldn't be filtered
-        when(mHighPriorityProvider.isHighPriority(mEntry)).thenReturn(true);
-    }
 }
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/interruption/KeyguardNotificationVisibilityProviderTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/interruption/KeyguardNotificationVisibilityProviderTest.java
new file mode 100644
index 0000000..de9ea27
--- /dev/null
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/interruption/KeyguardNotificationVisibilityProviderTest.java
@@ -0,0 +1,262 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.statusbar.notification.interruption;
+
+import static android.app.Notification.VISIBILITY_PUBLIC;
+import static android.app.Notification.VISIBILITY_SECRET;
+import static android.app.NotificationManager.IMPORTANCE_HIGH;
+import static android.app.NotificationManager.IMPORTANCE_MIN;
+
+import static com.android.systemui.statusbar.notification.collection.EntryUtilKt.modifyEntry;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
+
+import android.os.Handler;
+import android.os.UserHandle;
+import android.testing.AndroidTestingRunner;
+
+import androidx.test.filters.SmallTest;
+
+import com.android.keyguard.KeyguardUpdateMonitor;
+import com.android.systemui.SysuiTestCase;
+import com.android.systemui.broadcast.BroadcastDispatcher;
+import com.android.systemui.plugins.statusbar.StatusBarStateController;
+import com.android.systemui.statusbar.NotificationLockscreenUserManager;
+import com.android.systemui.statusbar.RankingBuilder;
+import com.android.systemui.statusbar.notification.SectionHeaderVisibilityProvider;
+import com.android.systemui.statusbar.notification.collection.GroupEntry;
+import com.android.systemui.statusbar.notification.collection.GroupEntryBuilder;
+import com.android.systemui.statusbar.notification.collection.NotificationEntry;
+import com.android.systemui.statusbar.notification.collection.NotificationEntryBuilder;
+import com.android.systemui.statusbar.notification.collection.provider.HighPriorityProvider;
+import com.android.systemui.statusbar.policy.KeyguardStateController;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+@SmallTest
+@RunWith(AndroidTestingRunner.class)
+public class KeyguardNotificationVisibilityProviderTest  extends SysuiTestCase {
+    private static final int NOTIF_USER_ID = 0;
+    private static final int CURR_USER_ID = 1;
+
+    @Mock
+    private Handler mMainHandler;
+    @Mock private KeyguardStateController mKeyguardStateController;
+    @Mock private NotificationLockscreenUserManager mLockscreenUserManager;
+    @Mock private KeyguardUpdateMonitor mKeyguardUpdateMonitor;
+    @Mock private HighPriorityProvider mHighPriorityProvider;
+    @Mock private SectionHeaderVisibilityProvider mSectionHeaderVisibilityProvider;
+    @Mock private KeyguardNotificationVisibilityProvider mKeyguardNotificationVisibilityProvider;
+    @Mock private StatusBarStateController mStatusBarStateController;
+    @Mock private BroadcastDispatcher mBroadcastDispatcher;
+
+    private NotificationEntry mEntry;
+
+    @Before
+    public void setup() {
+        MockitoAnnotations.initMocks(this);
+        // TODO refactor the test of KeyguardNotificationVisibilityProvider out
+        mKeyguardNotificationVisibilityProvider = spy(new KeyguardNotificationVisibilityProvider(
+                mContext,
+                mMainHandler,
+                mKeyguardStateController,
+                mLockscreenUserManager,
+                mKeyguardUpdateMonitor,
+                mHighPriorityProvider,
+                mStatusBarStateController,
+                mBroadcastDispatcher
+        ));
+
+        mEntry = new NotificationEntryBuilder()
+                .setUser(new UserHandle(NOTIF_USER_ID))
+                .build();
+    }
+
+    @Test
+    public void unfilteredState() {
+        // GIVEN an 'unfiltered-keyguard-showing' state
+        setupUnfilteredState(mEntry);
+
+        // THEN don't filter out the entry
+        assertFalse(mKeyguardNotificationVisibilityProvider.hideNotification(mEntry));
+    }
+
+    @Test
+    public void keyguardNotShowing() {
+        // GIVEN the lockscreen isn't showing
+        setupUnfilteredState(mEntry);
+        when(mKeyguardStateController.isShowing()).thenReturn(false);
+
+        // THEN don't filter out the entry
+        assertFalse(mKeyguardNotificationVisibilityProvider.hideNotification(mEntry));
+    }
+
+    @Test
+    public void doNotShowLockscreenNotifications() {
+        // GIVEN an 'unfiltered-keyguard-showing' state
+        setupUnfilteredState(mEntry);
+
+        // WHEN we shouldn't show any lockscreen notifications
+        when(mLockscreenUserManager.shouldShowLockscreenNotifications()).thenReturn(false);
+
+        // THEN filter out the entry
+        assertTrue(mKeyguardNotificationVisibilityProvider.hideNotification(mEntry));
+    }
+
+    @Test
+    public void lockdown() {
+        // GIVEN an 'unfiltered-keyguard-showing' state
+        setupUnfilteredState(mEntry);
+
+        // WHEN the notification's user is in lockdown:
+        when(mKeyguardUpdateMonitor.isUserInLockdown(NOTIF_USER_ID)).thenReturn(true);
+
+        // THEN filter out the entry
+        assertTrue(mKeyguardNotificationVisibilityProvider.hideNotification(mEntry));
+    }
+
+    @Test
+    public void publicMode_settingsDisallow() {
+        // GIVEN an 'unfiltered-keyguard-showing' state
+        setupUnfilteredState(mEntry);
+
+        // WHEN the notification's user is in public mode and settings are configured to disallow
+        // notifications in public mode
+        when(mLockscreenUserManager.isLockscreenPublicMode(NOTIF_USER_ID)).thenReturn(true);
+        when(mLockscreenUserManager.userAllowsNotificationsInPublic(NOTIF_USER_ID))
+                .thenReturn(false);
+
+        // THEN filter out the entry
+        assertTrue(mKeyguardNotificationVisibilityProvider.hideNotification(mEntry));
+    }
+
+    @Test
+    public void publicMode_notifDisallowed() {
+        // GIVEN an 'unfiltered-keyguard-showing' state
+        setupUnfilteredState(mEntry);
+
+        // WHEN the notification's user is in public mode and settings are configured to disallow
+        // notifications in public mode
+        when(mLockscreenUserManager.isLockscreenPublicMode(CURR_USER_ID)).thenReturn(true);
+        mEntry.setRanking(new RankingBuilder()
+                .setKey(mEntry.getKey())
+                .setVisibilityOverride(VISIBILITY_SECRET).build());
+
+        // THEN filter out the entry
+        assertTrue(mKeyguardNotificationVisibilityProvider.hideNotification(mEntry));
+    }
+
+    @Test
+    public void doesNotExceedThresholdToShow() {
+        // GIVEN an 'unfiltered-keyguard-showing' state
+        setupUnfilteredState(mEntry);
+
+        // WHEN the notification doesn't exceed the threshold to show on the lockscreen
+        mEntry.setRanking(new RankingBuilder()
+                .setKey(mEntry.getKey())
+                .setImportance(IMPORTANCE_MIN)
+                .build());
+        when(mHighPriorityProvider.isHighPriority(mEntry)).thenReturn(false);
+
+        // THEN filter out the entry
+        assertTrue(mKeyguardNotificationVisibilityProvider.hideNotification(mEntry));
+    }
+
+    @Test
+    public void summaryExceedsThresholdToShow() {
+        // GIVEN the notification doesn't exceed the threshold to show on the lockscreen
+        // but it's part of a group (has a parent)
+        final NotificationEntry entryWithParent = new NotificationEntryBuilder()
+                .setUser(new UserHandle(NOTIF_USER_ID))
+                .build();
+
+        final GroupEntry parent = new GroupEntryBuilder()
+                .setKey("test_group_key")
+                .setSummary(new NotificationEntryBuilder()
+                        .setImportance(IMPORTANCE_HIGH)
+                        .build())
+                .addChild(entryWithParent)
+                .build();
+
+        setupUnfilteredState(entryWithParent);
+        entryWithParent.setRanking(new RankingBuilder()
+                .setKey(entryWithParent.getKey())
+                .setImportance(IMPORTANCE_MIN)
+                .build());
+
+        // WHEN its parent does exceed threshold tot show on the lockscreen
+        when(mHighPriorityProvider.isHighPriority(parent)).thenReturn(true);
+
+        // THEN don't filter out the entry
+        assertFalse(mKeyguardNotificationVisibilityProvider.hideNotification(entryWithParent));
+
+        // WHEN its parent doesn't exceed threshold to show on lockscreen
+        when(mHighPriorityProvider.isHighPriority(parent)).thenReturn(false);
+        modifyEntry(parent.getSummary(), builder -> builder
+                .setImportance(IMPORTANCE_MIN)
+                .done());
+
+        // THEN filter out the entry
+        assertTrue(mKeyguardNotificationVisibilityProvider.hideNotification(entryWithParent));
+    }
+
+    /**
+     * setup a state where the notification will not be filtered by the
+     * KeyguardNotificationCoordinator when the keyguard is showing.
+     */
+    private void setupUnfilteredState(NotificationEntry entry) {
+        // keyguard is showing
+        when(mKeyguardStateController.isShowing()).thenReturn(true);
+
+        // show notifications on the lockscreen
+        when(mLockscreenUserManager.shouldShowLockscreenNotifications()).thenReturn(true);
+
+        // neither the current user nor the notification's user is in lockdown
+        when(mLockscreenUserManager.getCurrentUserId()).thenReturn(CURR_USER_ID);
+        when(mKeyguardUpdateMonitor.isUserInLockdown(NOTIF_USER_ID)).thenReturn(false);
+        when(mKeyguardUpdateMonitor.isUserInLockdown(CURR_USER_ID)).thenReturn(false);
+
+        // not in public mode
+        when(mLockscreenUserManager.isLockscreenPublicMode(CURR_USER_ID)).thenReturn(false);
+        when(mLockscreenUserManager.isLockscreenPublicMode(NOTIF_USER_ID)).thenReturn(false);
+
+        // entry's ranking - should show on all lockscreens
+        // + priority of the notification exceeds the threshold to be shown on the lockscreen
+        entry.setRanking(new RankingBuilder()
+                .setKey(mEntry.getKey())
+                .setVisibilityOverride(VISIBILITY_PUBLIC)
+                .setImportance(IMPORTANCE_HIGH)
+                .build());
+
+        // settings allows notifications in public mode
+        when(mLockscreenUserManager.userAllowsNotificationsInPublic(CURR_USER_ID)).thenReturn(true);
+        when(mLockscreenUserManager.userAllowsNotificationsInPublic(NOTIF_USER_ID))
+                .thenReturn(true);
+
+        // notification doesn't have a summary
+
+        // notification is high priority, so it shouldn't be filtered
+        when(mHighPriorityProvider.isHighPriority(mEntry)).thenReturn(true);
+    }
+}
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/interruption/NotificationInterruptStateProviderImplTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/interruption/NotificationInterruptStateProviderImplTest.java
index 2e1297b..8a2dc26 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/interruption/NotificationInterruptStateProviderImplTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/interruption/NotificationInterruptStateProviderImplTest.java
@@ -30,6 +30,9 @@
 
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 import android.app.Notification;
@@ -48,6 +51,7 @@
 import com.android.systemui.R;
 import com.android.systemui.SysuiTestCase;
 import com.android.systemui.plugins.statusbar.StatusBarStateController;
+import com.android.systemui.statusbar.notification.NotifPipelineFlags;
 import com.android.systemui.statusbar.notification.NotificationFilter;
 import com.android.systemui.statusbar.notification.collection.NotificationEntry;
 import com.android.systemui.statusbar.notification.collection.NotificationEntryBuilder;
@@ -86,6 +90,10 @@
     BatteryController mBatteryController;
     @Mock
     Handler mMockHandler;
+    @Mock
+    NotifPipelineFlags mFlags;
+    @Mock
+    KeyguardNotificationVisibilityProvider mKeyguardNotificationVisibilityProvider;
 
     private NotificationInterruptStateProviderImpl mNotifInterruptionStateProvider;
 
@@ -104,8 +112,9 @@
                         mStatusBarStateController,
                         mHeadsUpManager,
                         mLogger,
-                        mMockHandler);
-
+                        mMockHandler,
+                        mFlags,
+                        mKeyguardNotificationVisibilityProvider);
         mNotifInterruptionStateProvider.mUseHeadsUp = true;
     }
 
@@ -194,6 +203,16 @@
     }
 
     @Test
+    public void testDoNotRunFilterOnNewPipeline() {
+        when(mFlags.isNewPipelineEnabled()).thenReturn(true);
+        // WHEN this entry should be filtered out
+        NotificationEntry entry  = createNotification(IMPORTANCE_DEFAULT);
+        mNotifInterruptionStateProvider.shouldHeadsUp(entry);
+        verify(mFlags, times(1)).isNewPipelineEnabled();
+        verify(mNotificationFilter, times(0)).shouldFilterOut(eq(entry));
+    }
+
+    @Test
     public void testShouldNotHeadsUp_suppressedForGroups() throws RemoteException {
         // GIVEN state for "heads up when awake" is true
         ensureStateForHeadsUpWhenAwake();
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/CentralSurfacesTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/CentralSurfacesTest.java
index 953a330..c72f895 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/CentralSurfacesTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/CentralSurfacesTest.java
@@ -131,6 +131,7 @@
 import com.android.systemui.statusbar.notification.collection.render.NotifShadeEventSource;
 import com.android.systemui.statusbar.notification.collection.render.NotificationVisibilityProvider;
 import com.android.systemui.statusbar.notification.init.NotificationsController;
+import com.android.systemui.statusbar.notification.interruption.KeyguardNotificationVisibilityProvider;
 import com.android.systemui.statusbar.notification.interruption.NotificationInterruptLogger;
 import com.android.systemui.statusbar.notification.interruption.NotificationInterruptStateProviderImpl;
 import com.android.systemui.statusbar.notification.logging.NotificationLogger;
@@ -309,7 +310,9 @@
                         mDreamManager, mAmbientDisplayConfiguration, mNotificationFilter,
                         mStatusBarStateController, mBatteryController, mHeadsUpManager,
                         mock(NotificationInterruptLogger.class),
-                        new Handler(TestableLooper.get(this).getLooper()));
+                        new Handler(TestableLooper.get(this).getLooper()),
+                        mock(NotifPipelineFlags.class),
+                        mock(KeyguardNotificationVisibilityProvider.class));
 
         mContext.addMockSystemService(TrustManager.class, mock(TrustManager.class));
         mContext.addMockSystemService(FingerprintManager.class, mock(FingerprintManager.class));
@@ -846,6 +849,7 @@
     @Test
     public void testTransitionLaunch_noPreview_doesntGoUnlocked() {
         mCentralSurfaces.setBarStateForTest(StatusBarState.KEYGUARD);
+        when(mKeyguardStateController.isShowing()).thenReturn(true);
         mCentralSurfaces.showKeyguardImpl();
 
         // Starting a pulse should change the scrim controller to the pulsing state
@@ -868,6 +872,7 @@
     @Test
     public void testPulseWhileDozing_updatesScrimController() {
         mCentralSurfaces.setBarStateForTest(StatusBarState.KEYGUARD);
+        when(mKeyguardStateController.isShowing()).thenReturn(true);
         mCentralSurfaces.showKeyguardImpl();
 
         // Starting a pulse should change the scrim controller to the pulsing state
@@ -992,9 +997,12 @@
                 BatteryController batteryController,
                 HeadsUpManager headsUpManager,
                 NotificationInterruptLogger logger,
-                Handler mainHandler) {
+                Handler mainHandler,
+                NotifPipelineFlags flags,
+                KeyguardNotificationVisibilityProvider keyguardNotificationVisibilityProvider) {
             super(contentResolver, powerManager, dreamManager, ambientDisplayConfiguration, filter,
-                    batteryController, controller, headsUpManager, logger, mainHandler);
+                    batteryController, controller, headsUpManager, logger, mainHandler,
+                    flags, keyguardNotificationVisibilityProvider);
             mUseHeadsUp = true;
         }
     }
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/NotificationPanelViewControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/NotificationPanelViewControllerTest.java
index 6842295..4bac08e 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/NotificationPanelViewControllerTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/NotificationPanelViewControllerTest.java
@@ -56,13 +56,11 @@
 import android.view.LayoutInflater;
 import android.view.MotionEvent;
 import android.view.View;
-import android.view.ViewGroup;
 import android.view.ViewPropertyAnimator;
 import android.view.ViewStub;
 import android.view.accessibility.AccessibilityManager;
 import android.view.accessibility.AccessibilityNodeInfo;
 
-import androidx.constraintlayout.widget.ConstraintLayout;
 import androidx.constraintlayout.widget.ConstraintSet;
 import androidx.test.filters.SmallTest;
 
@@ -389,8 +387,6 @@
         when(mView.findViewById(R.id.keyguard_status_view))
                 .thenReturn(mock(KeyguardStatusView.class));
         mNotificationContainerParent = new NotificationsQuickSettingsContainer(getContext(), null);
-        mNotificationContainerParent.addView(newViewWithId(R.id.qs_frame));
-        mNotificationContainerParent.addView(newViewWithId(R.id.notification_stack_scroller));
         mNotificationContainerParent.addView(mKeyguardStatusView);
         mNotificationContainerParent.onFinishInflate();
         when(mView.findViewById(R.id.notification_container_parent))
@@ -679,31 +675,6 @@
     }
 
     @Test
-    public void testAllChildrenOfNotificationContainer_haveIds() {
-        enableSplitShade(/* enabled= */ true);
-        mNotificationContainerParent.removeAllViews();
-        mNotificationContainerParent.addView(newViewWithId(1));
-        mNotificationContainerParent.addView(newViewWithId(View.NO_ID));
-
-        mNotificationPanelViewController.updateResources();
-
-        assertThat(mNotificationContainerParent.getChildAt(0).getId()).isEqualTo(1);
-        assertThat(mNotificationContainerParent.getChildAt(1).getId()).isNotEqualTo(View.NO_ID);
-    }
-
-    @Test
-    public void testSinglePaneShadeLayout_isAlignedToParent() {
-        enableSplitShade(/* enabled= */ false);
-
-        mNotificationPanelViewController.updateResources();
-
-        assertThat(getConstraintSetLayout(R.id.qs_frame).endToEnd)
-                .isEqualTo(ConstraintSet.PARENT_ID);
-        assertThat(getConstraintSetLayout(R.id.notification_stack_scroller).startToStart)
-                .isEqualTo(ConstraintSet.PARENT_ID);
-    }
-
-    @Test
     public void testKeyguardStatusViewInSplitShade_changesConstraintsDependingOnNotifications() {
         mStatusBarStateController.setState(KEYGUARD);
         enableSplitShade(/* enabled= */ true);
@@ -752,46 +723,6 @@
     }
 
     @Test
-    public void testSplitShadeLayout_isAlignedToGuideline() {
-        enableSplitShade(/* enabled= */ true);
-
-        mNotificationPanelViewController.updateResources();
-
-        assertThat(getConstraintSetLayout(R.id.qs_frame).endToEnd)
-                .isEqualTo(R.id.qs_edge_guideline);
-        assertThat(getConstraintSetLayout(R.id.notification_stack_scroller).startToStart)
-                .isEqualTo(R.id.qs_edge_guideline);
-    }
-
-    @Test
-    public void testSplitShadeLayout_childrenHaveInsideMarginsOfZero() {
-        enableSplitShade(/* enabled= */ true);
-
-        mNotificationPanelViewController.updateResources();
-
-        assertThat(getConstraintSetLayout(R.id.qs_frame).startMargin).isEqualTo(10);
-        assertThat(getConstraintSetLayout(R.id.qs_frame).endMargin).isEqualTo(0);
-        assertThat(getConstraintSetLayout(R.id.notification_stack_scroller).startMargin)
-                .isEqualTo(0);
-        assertThat(getConstraintSetLayout(R.id.notification_stack_scroller).endMargin)
-                .isEqualTo(10);
-    }
-
-    @Test
-    public void testSinglePaneLayout_childrenHaveEqualMargins() {
-        enableSplitShade(/* enabled= */ false);
-
-        mNotificationPanelViewController.updateResources();
-
-        assertThat(getConstraintSetLayout(R.id.qs_frame).startMargin).isEqualTo(10);
-        assertThat(getConstraintSetLayout(R.id.qs_frame).endMargin).isEqualTo(10);
-        assertThat(getConstraintSetLayout(R.id.notification_stack_scroller).startMargin)
-                .isEqualTo(10);
-        assertThat(getConstraintSetLayout(R.id.notification_stack_scroller).endMargin)
-                .isEqualTo(10);
-    }
-
-    @Test
     public void testCanCollapsePanelOnTouch_trueForKeyGuard() {
         mStatusBarStateController.setState(KEYGUARD);
 
@@ -1016,17 +947,6 @@
         }
     }
 
-
-    private View newViewWithId(int id) {
-        View view = new View(mContext);
-        view.setId(id);
-        ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(
-                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
-        // required as cloning ConstraintSet fails if view doesn't have layout params
-        view.setLayoutParams(layoutParams);
-        return view;
-    }
-
     private ConstraintSet.Layout getConstraintSetLayout(@IdRes int id) {
         ConstraintSet constraintSet = new ConstraintSet();
         constraintSet.clone(mNotificationContainerParent);
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/NotificationQSContainerControllerTest.kt b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/NotificationQSContainerControllerTest.kt
index 5fb4144..83eabb6 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/NotificationQSContainerControllerTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/NotificationQSContainerControllerTest.kt
@@ -2,8 +2,14 @@
 
 import android.testing.AndroidTestingRunner
 import android.testing.TestableLooper
+import android.view.View
+import android.view.ViewGroup
 import android.view.WindowInsets
 import android.view.WindowManagerPolicyConstants
+import androidx.annotation.AnyRes
+import androidx.annotation.IdRes
+import androidx.constraintlayout.widget.ConstraintLayout
+import androidx.constraintlayout.widget.ConstraintSet
 import androidx.test.filters.SmallTest
 import com.android.systemui.R
 import com.android.systemui.SysuiTestCase
@@ -13,6 +19,7 @@
 import com.android.systemui.navigationbar.NavigationModeController.ModeChangedListener
 import com.android.systemui.recents.OverviewProxyService
 import com.android.systemui.recents.OverviewProxyService.OverviewProxyListener
+import com.google.common.truth.Truth.assertThat
 import org.junit.Before
 import org.junit.Test
 import org.junit.runner.RunWith
@@ -58,8 +65,10 @@
     lateinit var taskbarVisibilityCaptor: ArgumentCaptor<OverviewProxyListener>
     @Captor
     lateinit var windowInsetsCallbackCaptor: ArgumentCaptor<Consumer<WindowInsets>>
+    @Captor
+    lateinit var constraintSetCaptor: ArgumentCaptor<ConstraintSet>
 
-    private lateinit var notificationsQSContainerController: NotificationsQSContainerController
+    private lateinit var controller: NotificationsQSContainerController
     private lateinit var navigationModeCallback: ModeChangedListener
     private lateinit var taskbarVisibilityCallback: OverviewProxyListener
     private lateinit var windowInsetsCallback: Consumer<WindowInsets>
@@ -68,36 +77,40 @@
     fun setup() {
         MockitoAnnotations.initMocks(this)
         mContext.ensureTestableResources()
+        whenever(notificationsQSContainer.context).thenReturn(mContext)
         whenever(notificationsQSContainer.resources).thenReturn(mContext.resources)
-        notificationsQSContainerController = NotificationsQSContainerController(
+        controller = NotificationsQSContainerController(
                 notificationsQSContainer,
                 navigationModeController,
                 overviewProxyService,
                 featureFlags
         )
 
-        mContext.orCreateTestableResources
-            .addOverride(R.dimen.split_shade_notifications_scrim_margin_bottom, SCRIM_MARGIN)
-
-        whenever(notificationsQSContainer.defaultNotificationsMarginBottom)
-                .thenReturn(NOTIFICATIONS_MARGIN)
+        overrideResource(R.dimen.split_shade_notifications_scrim_margin_bottom, SCRIM_MARGIN)
+        overrideResource(R.dimen.notification_panel_margin_bottom, NOTIFICATIONS_MARGIN)
+        overrideResource(R.bool.config_use_split_notification_shade, false)
         whenever(navigationModeController.addListener(navigationModeCaptor.capture()))
                 .thenReturn(GESTURES_NAVIGATION)
         doNothing().`when`(overviewProxyService).addCallback(taskbarVisibilityCaptor.capture())
         doNothing().`when`(notificationsQSContainer)
                 .setInsetsChangedListener(windowInsetsCallbackCaptor.capture())
+        doNothing().`when`(notificationsQSContainer).applyConstraints(constraintSetCaptor.capture())
 
-        notificationsQSContainerController.init()
-        notificationsQSContainerController.onViewAttached()
+        controller.init()
+        controller.onViewAttached()
 
         navigationModeCallback = navigationModeCaptor.value
         taskbarVisibilityCallback = taskbarVisibilityCaptor.value
         windowInsetsCallback = windowInsetsCallbackCaptor.value
     }
 
+    private fun overrideResource(@AnyRes id: Int, value: Any) {
+        mContext.orCreateTestableResources.addOverride(id, value)
+    }
+
     @Test
     fun testTaskbarVisibleInSplitShade() {
-        notificationsQSContainerController.splitShadeEnabled = true
+        enableSplitShade()
         useNewFooter(false)
 
         given(taskbarVisible = true,
@@ -115,7 +128,7 @@
 
     @Test
     fun testTaskbarVisibleInSplitShade_newFooter() {
-        notificationsQSContainerController.splitShadeEnabled = true
+        enableSplitShade()
         useNewFooter(true)
 
         given(taskbarVisible = true,
@@ -136,7 +149,7 @@
     @Test
     fun testTaskbarNotVisibleInSplitShade() {
         // when taskbar is not visible, it means we're on the home screen
-        notificationsQSContainerController.splitShadeEnabled = true
+        enableSplitShade()
         useNewFooter(false)
 
         given(taskbarVisible = false,
@@ -154,7 +167,7 @@
     @Test
     fun testTaskbarNotVisibleInSplitShade_newFooter() {
         // when taskbar is not visible, it means we're on the home screen
-        notificationsQSContainerController.splitShadeEnabled = true
+        enableSplitShade()
         useNewFooter(true)
 
         given(taskbarVisible = false,
@@ -173,7 +186,7 @@
 
     @Test
     fun testTaskbarNotVisibleInSplitShadeWithCutout() {
-        notificationsQSContainerController.splitShadeEnabled = true
+        enableSplitShade()
         useNewFooter(false)
 
         given(taskbarVisible = false,
@@ -190,7 +203,7 @@
 
     @Test
     fun testTaskbarNotVisibleInSplitShadeWithCutout_newFooter() {
-        notificationsQSContainerController.splitShadeEnabled = true
+        enableSplitShade()
         useNewFooter(true)
 
         given(taskbarVisible = false,
@@ -209,7 +222,7 @@
 
     @Test
     fun testTaskbarVisibleInSinglePaneShade() {
-        notificationsQSContainerController.splitShadeEnabled = false
+        disableSplitShade()
         useNewFooter(false)
 
         given(taskbarVisible = true,
@@ -225,7 +238,7 @@
 
     @Test
     fun testTaskbarVisibleInSinglePaneShade_newFooter() {
-        notificationsQSContainerController.splitShadeEnabled = false
+        disableSplitShade()
         useNewFooter(true)
 
         given(taskbarVisible = true,
@@ -243,7 +256,7 @@
 
     @Test
     fun testTaskbarNotVisibleInSinglePaneShade() {
-        notificationsQSContainerController.splitShadeEnabled = false
+        disableSplitShade()
         useNewFooter(false)
 
         given(taskbarVisible = false,
@@ -264,7 +277,7 @@
 
     @Test
     fun testTaskbarNotVisibleInSinglePaneShade_newFooter() {
-        notificationsQSContainerController.splitShadeEnabled = false
+        disableSplitShade()
         useNewFooter(true)
 
         given(taskbarVisible = false,
@@ -285,8 +298,8 @@
 
     @Test
     fun testCustomizingInSinglePaneShade() {
-        notificationsQSContainerController.splitShadeEnabled = false
-        notificationsQSContainerController.setCustomizerShowing(true)
+        disableSplitShade()
+        controller.setCustomizerShowing(true)
         useNewFooter(false)
 
         // always sets spacings to 0
@@ -305,8 +318,8 @@
 
     @Test
     fun testCustomizingInSinglePaneShade_newFooter() {
-        notificationsQSContainerController.splitShadeEnabled = false
-        notificationsQSContainerController.setCustomizerShowing(true)
+        disableSplitShade()
+        controller.setCustomizerShowing(true)
         useNewFooter(true)
 
         // always sets spacings to 0
@@ -325,8 +338,8 @@
 
     @Test
     fun testDetailShowingInSinglePaneShade() {
-        notificationsQSContainerController.splitShadeEnabled = false
-        notificationsQSContainerController.setDetailShowing(true)
+        disableSplitShade()
+        controller.setDetailShowing(true)
         useNewFooter(false)
 
         // always sets spacings to 0
@@ -345,8 +358,8 @@
 
     @Test
     fun testDetailShowingInSinglePaneShade_newFooter() {
-        notificationsQSContainerController.splitShadeEnabled = false
-        notificationsQSContainerController.setDetailShowing(true)
+        disableSplitShade()
+        controller.setDetailShowing(true)
         useNewFooter(true)
 
         // always sets spacings to 0
@@ -365,8 +378,8 @@
 
     @Test
     fun testDetailShowingInSplitShade() {
-        notificationsQSContainerController.splitShadeEnabled = true
-        notificationsQSContainerController.setDetailShowing(true)
+        enableSplitShade()
+        controller.setDetailShowing(true)
         useNewFooter(false)
 
         given(taskbarVisible = false,
@@ -383,8 +396,8 @@
 
     @Test
     fun testDetailShowingInSplitShade_newFooter() {
-        notificationsQSContainerController.splitShadeEnabled = true
-        notificationsQSContainerController.setDetailShowing(true)
+        enableSplitShade()
+        controller.setDetailShowing(true)
         useNewFooter(true)
 
         given(taskbarVisible = false,
@@ -402,16 +415,106 @@
     @Test
     fun testNotificationsMarginBottomIsUpdated() {
         Mockito.clearInvocations(notificationsQSContainer)
-        notificationsQSContainerController.splitShadeEnabled = true
+        enableSplitShade()
         verify(notificationsQSContainer).setNotificationsMarginBottom(NOTIFICATIONS_MARGIN)
 
-        whenever(notificationsQSContainer.defaultNotificationsMarginBottom).thenReturn(100)
-        notificationsQSContainerController.updateMargins()
-        notificationsQSContainerController.splitShadeEnabled = false
-
+        overrideResource(R.dimen.notification_panel_margin_bottom, 100)
+        disableSplitShade()
         verify(notificationsQSContainer).setNotificationsMarginBottom(100)
     }
 
+    @Test
+    fun testSplitShadeLayout_isAlignedToGuideline() {
+        enableSplitShade()
+        controller.updateResources()
+        assertThat(getConstraintSetLayout(R.id.qs_frame).endToEnd)
+                .isEqualTo(R.id.qs_edge_guideline)
+        assertThat(getConstraintSetLayout(R.id.notification_stack_scroller).startToStart)
+                .isEqualTo(R.id.qs_edge_guideline)
+    }
+
+    @Test
+    fun testSinglePaneLayout_childrenHaveEqualMargins() {
+        disableSplitShade()
+        controller.updateResources()
+        val qsStartMargin = getConstraintSetLayout(R.id.qs_frame).startMargin
+        val qsEndMargin = getConstraintSetLayout(R.id.qs_frame).endMargin
+        val notifStartMargin = getConstraintSetLayout(R.id.notification_stack_scroller).startMargin
+        val notifEndMargin = getConstraintSetLayout(R.id.notification_stack_scroller).endMargin
+        assertThat(qsStartMargin == qsEndMargin &&
+                notifStartMargin == notifEndMargin &&
+                qsStartMargin == notifStartMargin
+        ).isTrue()
+    }
+
+    @Test
+    fun testSplitShadeLayout_childrenHaveInsideMarginsOfZero() {
+        enableSplitShade()
+        controller.updateResources()
+        assertThat(getConstraintSetLayout(R.id.qs_frame).endMargin).isEqualTo(0)
+        assertThat(getConstraintSetLayout(R.id.notification_stack_scroller).startMargin)
+                .isEqualTo(0)
+    }
+
+    @Test
+    fun testSplitShadeLayout_qsFrameHasHorizontalMarginsOfZero() {
+        enableSplitShade()
+        controller.updateResources()
+        assertThat(getConstraintSetLayout(R.id.qs_frame).endMargin).isEqualTo(0)
+        assertThat(getConstraintSetLayout(R.id.qs_frame).startMargin).isEqualTo(0)
+    }
+
+    @Test
+    fun testSinglePaneShadeLayout_qsFrameHasHorizontalMarginsSetToCorrectValue() {
+        disableSplitShade()
+        controller.updateResources()
+        val notificationPanelMarginHorizontal = context.resources
+                .getDimensionPixelSize(R.dimen.notification_panel_margin_horizontal)
+        assertThat(getConstraintSetLayout(R.id.qs_frame).endMargin)
+                .isEqualTo(notificationPanelMarginHorizontal)
+        assertThat(getConstraintSetLayout(R.id.qs_frame).startMargin)
+                .isEqualTo(notificationPanelMarginHorizontal)
+    }
+
+    @Test
+    fun testSinglePaneShadeLayout_isAlignedToParent() {
+        disableSplitShade()
+        controller.updateResources()
+        assertThat(getConstraintSetLayout(R.id.qs_frame).endToEnd)
+                .isEqualTo(ConstraintSet.PARENT_ID)
+        assertThat(getConstraintSetLayout(R.id.notification_stack_scroller).startToStart)
+                .isEqualTo(ConstraintSet.PARENT_ID)
+    }
+
+    @Test
+    fun testAllChildrenOfNotificationContainer_haveIds() {
+        // set dimen to 0 to avoid triggering updating bottom spacing
+        overrideResource(R.dimen.split_shade_notifications_scrim_margin_bottom, 0)
+        val container = NotificationsQuickSettingsContainer(context, null)
+        container.removeAllViews()
+        container.addView(newViewWithId(1))
+        container.addView(newViewWithId(View.NO_ID))
+        val controller = NotificationsQSContainerController(container, navigationModeController,
+                overviewProxyService, featureFlags)
+        controller.updateResources()
+
+        assertThat(container.getChildAt(0).id).isEqualTo(1)
+        assertThat(container.getChildAt(1).id).isNotEqualTo(View.NO_ID)
+    }
+
+    private fun disableSplitShade() {
+        setSplitShadeEnabled(false)
+    }
+
+    private fun enableSplitShade() {
+        setSplitShadeEnabled(true)
+    }
+
+    private fun setSplitShadeEnabled(enabled: Boolean) {
+        overrideResource(R.bool.config_use_split_notification_shade, enabled)
+        controller.updateResources()
+    }
+
     private fun given(
         taskbarVisible: Boolean,
         navigationMode: Int,
@@ -458,4 +561,18 @@
     private fun useNewFooter(useNewFooter: Boolean) {
         whenever(featureFlags.isEnabled(Flags.NEW_FOOTER)).thenReturn(useNewFooter)
     }
-}
\ No newline at end of file
+
+    private fun getConstraintSetLayout(@IdRes id: Int): ConstraintSet.Layout {
+        return constraintSetCaptor.value.getConstraint(id).layout
+    }
+
+    private fun newViewWithId(id: Int): View {
+        val view = View(mContext)
+        view.id = id
+        val layoutParams = ConstraintLayout.LayoutParams(
+                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
+        // required as cloning ConstraintSet fails if view doesn't have layout params
+        view.layoutParams = layoutParams
+        return view
+    }
+}
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/ScrimControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/ScrimControllerTest.java
index 10f4435..0b25467 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/ScrimControllerTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/ScrimControllerTest.java
@@ -1234,10 +1234,8 @@
         mScrimController.transitionTo(ScrimState.KEYGUARD);
         mScrimController.setUnocclusionAnimationRunning(true);
 
-        assertAlphaAfterExpansion(mNotificationsScrim, /* alpha */ KEYGUARD_SCRIM_ALPHA,
-                /* expansion */ 0.0f);
-        assertAlphaAfterExpansion(mNotificationsScrim, /* alpha */ KEYGUARD_SCRIM_ALPHA,
-                /* expansion */ 1.0f);
+        assertAlphaAfterExpansion(mNotificationsScrim, /* alpha */ 0f, /* expansion */ 0f);
+        assertAlphaAfterExpansion(mNotificationsScrim, /* alpha */ 0f, /* expansion */ 1.0f);
 
         // Verify normal behavior after
         mScrimController.setUnocclusionAnimationRunning(false);
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/StatusBarNotificationActivityStarterTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/StatusBarNotificationActivityStarterTest.java
index 27179fd..d48ce8c 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/StatusBarNotificationActivityStarterTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/StatusBarNotificationActivityStarterTest.java
@@ -85,6 +85,7 @@
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
 import org.mockito.InOrder;
 import org.mockito.Mock;
 import org.mockito.Mockito;
@@ -188,10 +189,11 @@
         when(mNotifPipelineFlags.isNewPipelineEnabled()).thenReturn(false);
         when(mOnUserInteractionCallback.getGroupSummaryToDismiss(mNotificationRow.getEntry()))
                 .thenReturn(null);
-        when(mVisibilityProvider.obtain(anyString(), anyBoolean())).thenAnswer(
-                invocation-> NotificationVisibility.obtain(invocation.getArgument(0), 0, 1, false));
-        when(mVisibilityProvider.obtain(any(NotificationEntry.class), anyBoolean())).thenAnswer(
-                invocation-> NotificationVisibility.obtain(
+        when(mVisibilityProvider.obtain(anyString(), anyBoolean()))
+                .thenAnswer(invocation -> NotificationVisibility.obtain(
+                        invocation.getArgument(0), 0, 1, false));
+        when(mVisibilityProvider.obtain(any(NotificationEntry.class), anyBoolean()))
+                .thenAnswer(invocation -> NotificationVisibility.obtain(
                         invocation.<NotificationEntry>getArgument(0).getKey(), 0, 1, false));
 
         HeadsUpManagerPhone headsUpManager = mock(HeadsUpManagerPhone.class);
@@ -431,12 +433,18 @@
     }
 
     @Test
-    public void testNotifActivityStarterEventSourceFinishEvent_postPanelCollapse() {
+    public void testNotifActivityStarterEventSourceFinishEvent_postPanelCollapse()
+            throws Exception {
         NotifActivityLaunchEvents.Listener listener =
                 mock(NotifActivityLaunchEvents.Listener.class);
         mLaunchEventsEmitter.registerListener(listener);
         mNotificationActivityStarter
                 .onNotificationClicked(mNotificationRow.getEntry().getSbn(), mNotificationRow);
+        ArgumentCaptor<ActivityLaunchAnimator.Controller> controllerCaptor =
+                ArgumentCaptor.forClass(ActivityLaunchAnimator.Controller.class);
+        verify(mActivityLaunchAnimator).startPendingIntentWithAnimation(
+                controllerCaptor.capture(), anyBoolean(), any(), any());
+        controllerCaptor.getValue().onIntentStarted(false);
         verify(listener).onFinishLaunchNotifActivity(mNotificationRow.getEntry());
     }
 }
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/RemoteInputViewTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/RemoteInputViewTest.java
index 20a3fda..3a0a7c9 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/RemoteInputViewTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/RemoteInputViewTest.java
@@ -114,15 +114,13 @@
         mContext.unregisterReceiver(mReceiver);
     }
 
-    private void setTestPendingIntent(RemoteInputView view, RemoteInputViewController controller) {
+    private void setTestPendingIntent(RemoteInputViewController controller) {
         PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0,
                 new Intent(TEST_ACTION), PendingIntent.FLAG_MUTABLE);
         RemoteInput input = new RemoteInput.Builder(TEST_RESULT_KEY).build();
         RemoteInput[] inputs = {input};
 
-        view.setPendingIntent(pendingIntent);
         controller.setPendingIntent(pendingIntent);
-        view.setRemoteInput(inputs, input, null /* editedSuggestionInfo */);
         controller.setRemoteInput(input);
         controller.setRemoteInputs(inputs);
     }
@@ -137,7 +135,7 @@
         RemoteInputView view = RemoteInputView.inflate(mContext, null, row.getEntry(), mController);
         RemoteInputViewController controller = bindController(view, row.getEntry());
 
-        setTestPendingIntent(view, controller);
+        setTestPendingIntent(controller);
 
         view.focus();
 
@@ -177,7 +175,7 @@
         RemoteInputView view = RemoteInputView.inflate(mContext, null, row.getEntry(), mController);
         RemoteInputViewController controller = bindController(view, row.getEntry());
 
-        setTestPendingIntent(view, controller);
+        setTestPendingIntent(controller);
 
         view.focus();
 
@@ -235,7 +233,7 @@
         RemoteInputView view = RemoteInputView.inflate(mContext, null, row.getEntry(), mController);
         RemoteInputViewController controller = bindController(view, row.getEntry());
 
-        setTestPendingIntent(view, controller);
+        setTestPendingIntent(controller);
 
         // Open view, send a reply
         view.focus();
@@ -265,7 +263,7 @@
         RemoteInputView view = RemoteInputView.inflate(mContext, null, row.getEntry(), mController);
         RemoteInputViewController controller = bindController(view, row.getEntry());
 
-        setTestPendingIntent(view, controller);
+        setTestPendingIntent(controller);
 
         // Open view, attach an image
         view.focus();
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/UserSwitcherControllerTest.kt b/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/UserSwitcherControllerTest.kt
index 91c347f..1caacb8 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/UserSwitcherControllerTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/UserSwitcherControllerTest.kt
@@ -41,6 +41,7 @@
 import com.android.systemui.SysuiTestCase
 import com.android.systemui.animation.DialogLaunchAnimator
 import com.android.systemui.broadcast.BroadcastDispatcher
+import com.android.systemui.broadcast.BroadcastSender
 import com.android.systemui.dump.DumpManager
 import com.android.systemui.plugins.ActivityStarter
 import com.android.systemui.plugins.FalsingManager
@@ -83,6 +84,7 @@
     @Mock private lateinit var userManager: UserManager
     @Mock private lateinit var activityStarter: ActivityStarter
     @Mock private lateinit var broadcastDispatcher: BroadcastDispatcher
+    @Mock private lateinit var broadcastSender: BroadcastSender
     @Mock private lateinit var telephonyListenerManager: TelephonyListenerManager
     @Mock private lateinit var secureSettings: SecureSettings
     @Mock private lateinit var falsingManager: FalsingManager
@@ -159,6 +161,7 @@
                 handler,
                 activityStarter,
                 broadcastDispatcher,
+                broadcastSender,
                 uiEventLogger,
                 falsingManager,
                 telephonyListenerManager,
diff --git a/packages/SystemUI/tests/src/com/android/systemui/wmshell/BubblesTest.java b/packages/SystemUI/tests/src/com/android/systemui/wmshell/BubblesTest.java
index 82880fe..78ee9e8 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/wmshell/BubblesTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/wmshell/BubblesTest.java
@@ -98,6 +98,7 @@
 import com.android.systemui.statusbar.notification.collection.legacy.NotificationGroupManagerLegacy;
 import com.android.systemui.statusbar.notification.collection.notifcollection.CommonNotifCollection;
 import com.android.systemui.statusbar.notification.collection.render.NotificationVisibilityProvider;
+import com.android.systemui.statusbar.notification.interruption.KeyguardNotificationVisibilityProvider;
 import com.android.systemui.statusbar.notification.interruption.NotificationInterruptLogger;
 import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
 import com.android.systemui.statusbar.notification.row.NotificationTestHelper;
@@ -343,7 +344,9 @@
                         mock(BatteryController.class),
                         mock(HeadsUpManager.class),
                         mock(NotificationInterruptLogger.class),
-                        mock(Handler.class)
+                        mock(Handler.class),
+                        mock(NotifPipelineFlags.class),
+                        mock(KeyguardNotificationVisibilityProvider.class)
                 );
 
         when(mNotifPipelineFlags.isNewPipelineEnabled()).thenReturn(false);
diff --git a/packages/SystemUI/tests/src/com/android/systemui/wmshell/NewNotifPipelineBubblesTest.java b/packages/SystemUI/tests/src/com/android/systemui/wmshell/NewNotifPipelineBubblesTest.java
index cc848bc3..fafe4b3 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/wmshell/NewNotifPipelineBubblesTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/wmshell/NewNotifPipelineBubblesTest.java
@@ -85,6 +85,7 @@
 import com.android.systemui.statusbar.notification.collection.notifcollection.CommonNotifCollection;
 import com.android.systemui.statusbar.notification.collection.notifcollection.NotifCollectionListener;
 import com.android.systemui.statusbar.notification.collection.render.NotificationVisibilityProvider;
+import com.android.systemui.statusbar.notification.interruption.KeyguardNotificationVisibilityProvider;
 import com.android.systemui.statusbar.notification.interruption.NotificationInterruptLogger;
 import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
 import com.android.systemui.statusbar.notification.row.NotificationTestHelper;
@@ -309,7 +310,9 @@
                         mock(BatteryController.class),
                         mock(HeadsUpManager.class),
                         mock(NotificationInterruptLogger.class),
-                        mock(Handler.class)
+                        mock(Handler.class),
+                        mock(NotifPipelineFlags.class),
+                        mock(KeyguardNotificationVisibilityProvider.class)
                 );
         when(mNotifPipelineFlags.isNewPipelineEnabled()).thenReturn(true);
         when(mShellTaskOrganizer.getExecutor()).thenReturn(syncExecutor);
diff --git a/packages/SystemUI/tests/src/com/android/systemui/wmshell/TestableNotificationInterruptStateProviderImpl.java b/packages/SystemUI/tests/src/com/android/systemui/wmshell/TestableNotificationInterruptStateProviderImpl.java
index e698f1e..a7f0dc2 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/wmshell/TestableNotificationInterruptStateProviderImpl.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/wmshell/TestableNotificationInterruptStateProviderImpl.java
@@ -23,7 +23,9 @@
 import android.service.dreams.IDreamManager;
 
 import com.android.systemui.plugins.statusbar.StatusBarStateController;
+import com.android.systemui.statusbar.notification.NotifPipelineFlags;
 import com.android.systemui.statusbar.notification.NotificationFilter;
+import com.android.systemui.statusbar.notification.interruption.KeyguardNotificationVisibilityProvider;
 import com.android.systemui.statusbar.notification.interruption.NotificationInterruptLogger;
 import com.android.systemui.statusbar.notification.interruption.NotificationInterruptStateProviderImpl;
 import com.android.systemui.statusbar.policy.BatteryController;
@@ -42,7 +44,9 @@
             BatteryController batteryController,
             HeadsUpManager headsUpManager,
             NotificationInterruptLogger logger,
-            Handler mainHandler) {
+            Handler mainHandler,
+            NotifPipelineFlags flags,
+            KeyguardNotificationVisibilityProvider keyguardNotificationVisibilityProvider) {
         super(contentResolver,
                 powerManager,
                 dreamManager,
@@ -52,7 +56,9 @@
                 statusBarStateController,
                 headsUpManager,
                 logger,
-                mainHandler);
+                mainHandler,
+                flags,
+                keyguardNotificationVisibilityProvider);
         mUseHeadsUp = true;
     }
 }
diff --git a/proto/src/camera.proto b/proto/src/camera.proto
index 4082118..38d74e4 100644
--- a/proto/src/camera.proto
+++ b/proto/src/camera.proto
@@ -66,5 +66,5 @@
     // The dynamic range profile of the stream
     optional int64 dynamic_range_profile = 14;
     // The stream use case
-    optional int32 stream_use_case = 15;
+    optional int64 stream_use_case = 15;
 }
diff --git a/services/accessibility/java/com/android/server/accessibility/AbstractAccessibilityServiceConnection.java b/services/accessibility/java/com/android/server/accessibility/AbstractAccessibilityServiceConnection.java
index 7f10314..5ef1008 100644
--- a/services/accessibility/java/com/android/server/accessibility/AbstractAccessibilityServiceConnection.java
+++ b/services/accessibility/java/com/android/server/accessibility/AbstractAccessibilityServiceConnection.java
@@ -281,9 +281,9 @@
 
         void onDoubleTapAndHold(int displayId);
 
-        void requestImeLocked(AccessibilityServiceConnection connection);
+        void requestImeLocked(AbstractAccessibilityServiceConnection connection);
 
-        void unbindImeLocked(AccessibilityServiceConnection connection);
+        void unbindImeLocked(AbstractAccessibilityServiceConnection connection);
     }
 
     public AbstractAccessibilityServiceConnection(Context context, ComponentName componentName,
@@ -387,7 +387,6 @@
                 & AccessibilityServiceInfo.FLAG_REQUEST_FINGERPRINT_GESTURES) != 0;
         mRequestAccessibilityButton = (info.flags
                 & AccessibilityServiceInfo.FLAG_REQUEST_ACCESSIBILITY_BUTTON) != 0;
-        // TODO(b/218193835): request ime when ime flag is set and clean up when ime flag is unset
         mRequestImeApis = (info.flags
                 & AccessibilityServiceInfo.FLAG_INPUT_METHOD_EDITOR) != 0;
     }
@@ -439,6 +438,7 @@
                 // If the XML manifest had data to configure the service its info
                 // should be already set. In such a case update only the dynamically
                 // configurable properties.
+                boolean oldRequestIme = mRequestImeApis;
                 AccessibilityServiceInfo oldInfo = mAccessibilityServiceInfo;
                 if (oldInfo != null) {
                     oldInfo.updateDynamicallyConfigurableProperties(mIPlatformCompat, info);
@@ -447,6 +447,11 @@
                     setDynamicallyConfigurableProperties(info);
                 }
                 mSystemSupport.onClientChangeLocked(true);
+                if (!oldRequestIme && mRequestImeApis) {
+                    mSystemSupport.requestImeLocked(this);
+                } else if (oldRequestIme && !mRequestImeApis) {
+                    mSystemSupport.unbindImeLocked(this);
+                }
             }
         } finally {
             Binder.restoreCallingIdentity(identity);
diff --git a/services/accessibility/java/com/android/server/accessibility/AccessibilityManagerService.java b/services/accessibility/java/com/android/server/accessibility/AccessibilityManagerService.java
index 0ea087d..249ee16 100644
--- a/services/accessibility/java/com/android/server/accessibility/AccessibilityManagerService.java
+++ b/services/accessibility/java/com/android/server/accessibility/AccessibilityManagerService.java
@@ -4305,7 +4305,7 @@
     }
 
     @Override
-    public void requestImeLocked(AccessibilityServiceConnection connection) {
+    public void requestImeLocked(AbstractAccessibilityServiceConnection connection) {
         mMainHandler.sendMessage(obtainMessage(
                 AccessibilityManagerService::createSessionForConnection, this, connection));
         mMainHandler.sendMessage(obtainMessage(
@@ -4313,12 +4313,12 @@
     }
 
     @Override
-    public void unbindImeLocked(AccessibilityServiceConnection connection) {
+    public void unbindImeLocked(AbstractAccessibilityServiceConnection connection) {
         mMainHandler.sendMessage(obtainMessage(
                 AccessibilityManagerService::unbindInputForConnection, this, connection));
     }
 
-    private void createSessionForConnection(AccessibilityServiceConnection connection) {
+    private void createSessionForConnection(AbstractAccessibilityServiceConnection connection) {
         synchronized (mLock) {
             if (mInputSessionRequested) {
                 connection.createImeSessionLocked();
@@ -4326,7 +4326,7 @@
         }
     }
 
-    private void bindAndStartInputForConnection(AccessibilityServiceConnection connection) {
+    private void bindAndStartInputForConnection(AbstractAccessibilityServiceConnection connection) {
         synchronized (mLock) {
             if (mInputBinding != null) {
                 connection.bindInputLocked(mInputBinding);
@@ -4336,7 +4336,7 @@
         }
     }
 
-    private void unbindInputForConnection(AccessibilityServiceConnection connection) {
+    private void unbindInputForConnection(AbstractAccessibilityServiceConnection connection) {
         InputMethodManagerInternal.get().unbindAccessibilityFromCurrentClient(connection.mId);
         synchronized (mLock) {
             connection.unbindInputLocked();
diff --git a/services/api/current.txt b/services/api/current.txt
index 45c0059..5a28802 100644
--- a/services/api/current.txt
+++ b/services/api/current.txt
@@ -38,7 +38,7 @@
 package com.android.server.am {
 
   public interface ActivityManagerLocal {
-    method public boolean bindSdkSandboxService(@NonNull android.content.Intent, @NonNull android.content.ServiceConnection, int, @NonNull String, int) throws android.os.RemoteException;
+    method public boolean bindSdkSandboxService(@NonNull android.content.Intent, @NonNull android.content.ServiceConnection, int, @NonNull String, @NonNull String, int) throws android.os.RemoteException;
     method public boolean canStartForegroundService(int, int, @NonNull String);
   }
 
diff --git a/services/backup/java/com/android/server/backup/UserBackupManagerService.java b/services/backup/java/com/android/server/backup/UserBackupManagerService.java
index e10151d..1af35af 100644
--- a/services/backup/java/com/android/server/backup/UserBackupManagerService.java
+++ b/services/backup/java/com/android/server/backup/UserBackupManagerService.java
@@ -89,8 +89,6 @@
 import android.os.SystemClock;
 import android.os.UserHandle;
 import android.os.WorkSource;
-import android.os.storage.IStorageManager;
-import android.os.storage.StorageManager;
 import android.provider.Settings;
 import android.text.TextUtils;
 import android.util.ArraySet;
@@ -325,7 +323,6 @@
     private final ActivityManagerInternal mActivityManagerInternal;
     private PowerManager mPowerManager;
     private final AlarmManager mAlarmManager;
-    private final IStorageManager mStorageManager;
     private final BackupManagerConstants mConstants;
     private final BackupWakeLock mWakelock;
     private final BackupHandler mBackupHandler;
@@ -536,7 +533,6 @@
         mBackupPasswordManager = null;
         mPackageManagerBinder = null;
         mActivityManager = null;
-        mStorageManager = null;
         mBackupManagerBinder = null;
         mScheduledBackupEligibility = null;
     }
@@ -560,7 +556,6 @@
 
         mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
         mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
-        mStorageManager = IStorageManager.Stub.asInterface(ServiceManager.getService("mount"));
 
         Objects.requireNonNull(parent, "parent cannot be null");
         mBackupManagerBinder = BackupManagerService.asInterface(parent.asBinder());
@@ -2077,26 +2072,6 @@
         }
     }
 
-    /** For adb backup/restore. */
-    public boolean deviceIsEncrypted() {
-        try {
-            return mStorageManager.getEncryptionState()
-                    != StorageManager.ENCRYPTION_STATE_NONE
-                    && mStorageManager.getPasswordType()
-                    != StorageManager.CRYPT_TYPE_DEFAULT;
-        } catch (Exception e) {
-            // If we can't talk to the storagemanager service we have a serious problem; fail
-            // "secure" i.e. assuming that the device is encrypted.
-            Slog.e(
-                    TAG,
-                    addUserIdToLogMessage(
-                            mUserId,
-                            "Unable to communicate with storagemanager service: "
-                                    + e.getMessage()));
-            return true;
-        }
-    }
-
     // ----- Full-data backup scheduling -----
 
     /**
diff --git a/services/backup/java/com/android/server/backup/fullbackup/PerformAdbBackupTask.java b/services/backup/java/com/android/server/backup/fullbackup/PerformAdbBackupTask.java
index 7ee307e..ec58e17 100644
--- a/services/backup/java/com/android/server/backup/fullbackup/PerformAdbBackupTask.java
+++ b/services/backup/java/com/android/server/backup/fullbackup/PerformAdbBackupTask.java
@@ -320,12 +320,6 @@
         try {
             boolean encrypting = (mEncryptPassword != null && mEncryptPassword.length() > 0);
 
-            // Only allow encrypted backups of encrypted devices
-            if (mUserBackupManagerService.deviceIsEncrypted() && !encrypting) {
-                Slog.e(TAG, "Unencrypted backup of encrypted device; aborting");
-                return;
-            }
-
             OutputStream finalOutput = ofstream;
 
             // Verify that the given password matches the currently-active
diff --git a/services/cloudsearch/java/com/android/server/cloudsearch/CloudSearchManagerService.java b/services/cloudsearch/java/com/android/server/cloudsearch/CloudSearchManagerService.java
index b1f572d..ac2d1dd 100644
--- a/services/cloudsearch/java/com/android/server/cloudsearch/CloudSearchManagerService.java
+++ b/services/cloudsearch/java/com/android/server/cloudsearch/CloudSearchManagerService.java
@@ -129,7 +129,7 @@
         @Override
         public void search(@NonNull SearchRequest searchRequest,
                 @NonNull ICloudSearchManagerCallback callBack) {
-            searchRequest.setSource(
+            searchRequest.setCallerPackageName(
                     mContext.getPackageManager().getNameForUid(Binder.getCallingUid()));
             runForUser("search", (service) -> {
                 synchronized (service.mLock) {
diff --git a/services/companion/java/com/android/server/companion/presence/BluetoothCompanionDeviceConnectionListener.java b/services/companion/java/com/android/server/companion/presence/BluetoothCompanionDeviceConnectionListener.java
index 1ba198a..823743d 100644
--- a/services/companion/java/com/android/server/companion/presence/BluetoothCompanionDeviceConnectionListener.java
+++ b/services/companion/java/com/android/server/companion/presence/BluetoothCompanionDeviceConnectionListener.java
@@ -94,7 +94,7 @@
             int reason) {
         if (DEBUG) {
             Log.i(TAG, "onDevice_Disconnected() " + btDeviceToString(device));
-            Log.d(TAG, "  reason=" + disconnectReasonText(reason));
+            Log.d(TAG, "  reason=" + disconnectReasonToString(reason));
         }
 
         final MacAddress macAddress = MacAddress.fromString(device.getAddress());
diff --git a/services/companion/java/com/android/server/companion/virtual/GenericWindowPolicyController.java b/services/companion/java/com/android/server/companion/virtual/GenericWindowPolicyController.java
index b991ba8..3a26c46 100644
--- a/services/companion/java/com/android/server/companion/virtual/GenericWindowPolicyController.java
+++ b/services/companion/java/com/android/server/companion/virtual/GenericWindowPolicyController.java
@@ -22,6 +22,7 @@
 
 import android.annotation.NonNull;
 import android.annotation.Nullable;
+import android.app.WindowConfiguration;
 import android.app.compat.CompatChanges;
 import android.companion.virtual.VirtualDeviceManager.ActivityListener;
 import android.companion.virtual.VirtualDeviceParams;
@@ -119,6 +120,7 @@
             @ActivityPolicy int defaultActivityPolicy,
             @NonNull ActivityListener activityListener,
             @NonNull Consumer<ActivityInfo> activityBlockedCallback) {
+        super();
         mAllowedUsers = allowedUsers;
         mAllowedActivities = new ArraySet<>(allowedActivities);
         mBlockedActivities = new ArraySet<>(blockedActivities);
@@ -134,7 +136,11 @@
     }
 
     @Override
-    public boolean canContainActivities(@NonNull List<ActivityInfo> activities) {
+    public boolean canContainActivities(@NonNull List<ActivityInfo> activities,
+            @WindowConfiguration.WindowingMode int windowingMode) {
+        if (!isWindowingModeSupported(windowingMode)) {
+            return false;
+        }
         // Can't display all the activities if any of them don't want to be displayed.
         final int activityCount = activities.size();
         for (int i = 0; i < activityCount; i++) {
diff --git a/services/core/java/android/content/pm/PackageManagerInternal.java b/services/core/java/android/content/pm/PackageManagerInternal.java
index 2068e6d..68cd288 100644
--- a/services/core/java/android/content/pm/PackageManagerInternal.java
+++ b/services/core/java/android/content/pm/PackageManagerInternal.java
@@ -48,7 +48,6 @@
 import com.android.server.pm.dex.DynamicCodeLogger;
 import com.android.server.pm.parsing.pkg.AndroidPackage;
 import com.android.server.pm.pkg.AndroidPackageApi;
-import com.android.server.pm.pkg.PackageState;
 import com.android.server.pm.pkg.PackageStateInternal;
 import com.android.server.pm.pkg.SharedUserApi;
 import com.android.server.pm.pkg.component.ParsedMainComponent;
@@ -690,8 +689,6 @@
     @Nullable
     public abstract PackageStateInternal getPackageStateInternal(@NonNull String packageName);
 
-    public abstract @Nullable PackageState getPackageState(@NonNull String packageName);
-
     @NonNull
     public abstract ArrayMap<String, ? extends PackageStateInternal> getPackageStates();
 
diff --git a/services/core/java/com/android/server/MasterClearReceiver.java b/services/core/java/com/android/server/MasterClearReceiver.java
index be2b7f7..f1fa982 100644
--- a/services/core/java/com/android/server/MasterClearReceiver.java
+++ b/services/core/java/com/android/server/MasterClearReceiver.java
@@ -131,7 +131,7 @@
         final UserManager userManager = context.getSystemService(UserManager.class);
         final int result = userManager.removeUserWhenPossible(
                 UserHandle.of(userId), /* overrideDevicePolicy= */ false);
-        if (result == UserManager.REMOVE_RESULT_ERROR) {
+        if (!UserManager.isRemoveResultSuccessful(result)) {
             Slogf.e(TAG, "Can't remove user %d", userId);
             return false;
         }
diff --git a/services/core/java/com/android/server/NetworkManagementService.java b/services/core/java/com/android/server/NetworkManagementService.java
index b59cd4c..1a39ffa 100644
--- a/services/core/java/com/android/server/NetworkManagementService.java
+++ b/services/core/java/com/android/server/NetworkManagementService.java
@@ -1166,9 +1166,17 @@
             final ConnectivityManager cm = mContext.getSystemService(ConnectivityManager.class);
             try {
                 if (allowlist) {
-                    cm.updateMeteredNetworkAllowList(uid, enable);
+                    if (enable) {
+                        cm.addUidToMeteredNetworkAllowList(uid);
+                    } else {
+                        cm.removeUidFromMeteredNetworkAllowList(uid);
+                    }
                 } else {
-                    cm.updateMeteredNetworkDenyList(uid, enable);
+                    if (enable) {
+                        cm.addUidToMeteredNetworkDenyList(uid);
+                    } else {
+                        cm.removeUidFromMeteredNetworkDenyList(uid);
+                    }
                 }
                 synchronized (mRulesLock) {
                     if (enable) {
diff --git a/services/core/java/com/android/server/StorageManagerService.java b/services/core/java/com/android/server/StorageManagerService.java
index 4628a1f..677fc79 100644
--- a/services/core/java/com/android/server/StorageManagerService.java
+++ b/services/core/java/com/android/server/StorageManagerService.java
@@ -75,7 +75,6 @@
 import android.content.pm.PackageManagerInternal;
 import android.content.pm.ProviderInfo;
 import android.content.pm.UserInfo;
-import android.content.res.Configuration;
 import android.content.res.ObbInfo;
 import android.database.ContentObserver;
 import android.net.Uri;
@@ -124,7 +123,6 @@
 import android.provider.MediaStore;
 import android.provider.Settings;
 import android.service.storage.ExternalStorageService;
-import android.sysprop.VoldProperties;
 import android.text.TextUtils;
 import android.text.format.DateUtils;
 import android.util.ArrayMap;
@@ -1407,39 +1405,6 @@
     private void handleDaemonConnected() {
         initIfBootedAndConnected();
         resetIfBootedAndConnected();
-
-        // On an encrypted device we can't see system properties yet, so pull
-        // the system locale out of the mount service.
-        if ("".equals(VoldProperties.encrypt_progress().orElse(""))) {
-            copyLocaleFromMountService();
-        }
-    }
-
-    private void copyLocaleFromMountService() {
-        String systemLocale;
-        try {
-            systemLocale = getField(StorageManager.SYSTEM_LOCALE_KEY);
-        } catch (RemoteException e) {
-            return;
-        }
-        if (TextUtils.isEmpty(systemLocale)) {
-            return;
-        }
-
-        Slog.d(TAG, "Got locale " + systemLocale + " from mount service");
-        Locale locale = Locale.forLanguageTag(systemLocale);
-        Configuration config = new Configuration();
-        config.setLocale(locale);
-        try {
-            ActivityManager.getService().updatePersistentConfigurationWithAttribution(config,
-                    mContext.getOpPackageName(), mContext.getAttributionTag());
-        } catch (RemoteException e) {
-            Slog.e(TAG, "Error setting system locale from mount service", e);
-        }
-
-        // Temporary workaround for http://b/17945169.
-        Slog.d(TAG, "Setting system properties to " + systemLocale + " from mount service");
-        SystemProperties.set("persist.sys.locale", locale.toLanguageTag());
     }
 
     private final IVoldListener mListener = new IVoldListener.Stub() {
@@ -3164,203 +3129,6 @@
         }
     }
 
-    @Override
-    public int getEncryptionState() {
-        mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
-                "no permission to access the crypt keeper");
-
-        try {
-            return mVold.fdeComplete();
-        } catch (Exception e) {
-            Slog.wtf(TAG, e);
-            return StorageManager.ENCRYPTION_STATE_ERROR_UNKNOWN;
-        }
-    }
-
-    @Override
-    public int decryptStorage(String password) {
-        mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
-                "no permission to access the crypt keeper");
-
-        if (TextUtils.isEmpty(password)) {
-            throw new IllegalArgumentException("password cannot be empty");
-        }
-
-        if (DEBUG_EVENTS) {
-            Slog.i(TAG, "decrypting storage...");
-        }
-
-        try {
-            mVold.fdeCheckPassword(password);
-            mHandler.postDelayed(() -> {
-                try {
-                    mVold.fdeRestart();
-                } catch (Exception e) {
-                    Slog.wtf(TAG, e);
-                }
-            }, DateUtils.SECOND_IN_MILLIS);
-            return 0;
-        } catch (ServiceSpecificException e) {
-            Slog.e(TAG, "fdeCheckPassword failed", e);
-            return e.errorCode;
-        } catch (Exception e) {
-            Slog.wtf(TAG, e);
-            return StorageManager.ENCRYPTION_STATE_ERROR_UNKNOWN;
-        }
-    }
-
-    @Override
-    public int encryptStorage(int type, String password) {
-        mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
-            "no permission to access the crypt keeper");
-
-        if (type == StorageManager.CRYPT_TYPE_DEFAULT) {
-            password = "";
-        } else if (TextUtils.isEmpty(password)) {
-            throw new IllegalArgumentException("password cannot be empty");
-        }
-
-        if (DEBUG_EVENTS) {
-            Slog.i(TAG, "encrypting storage...");
-        }
-
-        try {
-            mVold.fdeEnable(type, password, 0);
-        } catch (Exception e) {
-            Slog.wtf(TAG, e);
-            return -1;
-        }
-
-        return 0;
-    }
-
-    /** Set the password for encrypting the main key.
-     *  @param type One of the CRYPTO_TYPE_XXX consts defined in StorageManager.
-     *  @param password The password to set.
-     */
-    @Override
-    public int changeEncryptionPassword(int type, String password) {
-        mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
-            "no permission to access the crypt keeper");
-
-        if (StorageManager.isFileEncryptedNativeOnly()) {
-            // Not supported on FBE devices
-            return -1;
-        }
-
-        if (type == StorageManager.CRYPT_TYPE_DEFAULT) {
-            password = "";
-        } else if (TextUtils.isEmpty(password)) {
-            throw new IllegalArgumentException("password cannot be empty");
-        }
-
-        if (DEBUG_EVENTS) {
-            Slog.i(TAG, "changing encryption password...");
-        }
-
-        try {
-            mVold.fdeChangePassword(type, password);
-            return 0;
-        } catch (Exception e) {
-            Slog.wtf(TAG, e);
-            return -1;
-        }
-    }
-
-    /**
-     * Validate a user-supplied password string with cryptfs
-     */
-    @Override
-    public int verifyEncryptionPassword(String password) throws RemoteException {
-        // Only the system process is permitted to validate passwords
-        if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) {
-            throw new SecurityException("no permission to access the crypt keeper");
-        }
-
-        mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
-            "no permission to access the crypt keeper");
-
-        if (TextUtils.isEmpty(password)) {
-            throw new IllegalArgumentException("password cannot be empty");
-        }
-
-        if (DEBUG_EVENTS) {
-            Slog.i(TAG, "validating encryption password...");
-        }
-
-        try {
-            mVold.fdeVerifyPassword(password);
-            return 0;
-        } catch (Exception e) {
-            Slog.wtf(TAG, e);
-            return -1;
-        }
-    }
-
-    /**
-     * Get the type of encryption used to encrypt the main key.
-     * @return The type, one of the CRYPT_TYPE_XXX consts from StorageManager.
-     */
-    @Override
-    public int getPasswordType() {
-        mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
-            "no permission to access the crypt keeper");
-
-        try {
-            return mVold.fdeGetPasswordType();
-        } catch (Exception e) {
-            Slog.wtf(TAG, e);
-            return -1;
-        }
-    }
-
-    /**
-     * Set a field in the crypto header.
-     * @param field field to set
-     * @param contents contents to set in field
-     */
-    @Override
-    public void setField(String field, String contents) throws RemoteException {
-        mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
-            "no permission to access the crypt keeper");
-
-        if (!StorageManager.isBlockEncrypted()) {
-            // Only supported on FDE devices
-            return;
-        }
-
-        try {
-            mVold.fdeSetField(field, contents);
-            return;
-        } catch (Exception e) {
-            Slog.wtf(TAG, e);
-            return;
-        }
-    }
-
-    /**
-     * Gets a field from the crypto header.
-     * @param field field to get
-     * @return contents of field
-     */
-    @Override
-    public String getField(String field) throws RemoteException {
-        mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
-            "no permission to access the crypt keeper");
-
-        if (!StorageManager.isBlockEncrypted()) {
-            // Only supported on FDE devices
-            return null;
-        }
-
-        try {
-            return mVold.fdeGetField(field);
-        } catch (Exception e) {
-            Slog.wtf(TAG, e);
-            return null;
-        }
-    }
-
     /**
      * Is userdata convertible to file based encryption?
      * @return non zero for convertible
@@ -3443,33 +3211,6 @@
     }
 
     @Override
-    public String getPassword() throws RemoteException {
-        mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
-                "only keyguard can retrieve password");
-
-        try {
-            return mVold.fdeGetPassword();
-        } catch (Exception e) {
-            Slog.wtf(TAG, e);
-            return null;
-        }
-    }
-
-    @Override
-    public void clearPassword() throws RemoteException {
-        mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
-                "only keyguard can clear password");
-
-        try {
-            mVold.fdeClearPassword();
-            return;
-        } catch (Exception e) {
-            Slog.wtf(TAG, e);
-            return;
-        }
-    }
-
-    @Override
     public void createUserKey(int userId, int serialNumber, boolean ephemeral) {
         enforcePermission(android.Manifest.permission.STORAGE_INTERNAL);
 
diff --git a/services/core/java/com/android/server/TelephonyRegistry.java b/services/core/java/com/android/server/TelephonyRegistry.java
index 40ab0c0..839cdc6 100644
--- a/services/core/java/com/android/server/TelephonyRegistry.java
+++ b/services/core/java/com/android/server/TelephonyRegistry.java
@@ -91,7 +91,7 @@
 
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.app.IBatteryStats;
-import com.android.internal.telephony.ICarrierPrivilegesListener;
+import com.android.internal.telephony.ICarrierPrivilegesCallback;
 import com.android.internal.telephony.IOnSubscriptionsChangedListener;
 import com.android.internal.telephony.IPhoneStateListener;
 import com.android.internal.telephony.ITelephonyRegistry;
@@ -153,7 +153,7 @@
         IPhoneStateListener callback;
         IOnSubscriptionsChangedListener onSubscriptionsChangedListenerCallback;
         IOnSubscriptionsChangedListener onOpportunisticSubscriptionsChangedListenerCallback;
-        ICarrierPrivilegesListener carrierPrivilegesListener;
+        ICarrierPrivilegesCallback carrierPrivilegesCallback;
 
         int callerUid;
         int callerPid;
@@ -178,8 +178,8 @@
             return (onOpportunisticSubscriptionsChangedListenerCallback != null);
         }
 
-        boolean matchCarrierPrivilegesListener() {
-            return carrierPrivilegesListener != null;
+        boolean matchCarrierPrivilegesCallback() {
+            return carrierPrivilegesCallback != null;
         }
 
         boolean canReadCallLog() {
@@ -199,7 +199,7 @@
                     + onSubscriptionsChangedListenerCallback
                     + " onOpportunisticSubscriptionsChangedListenererCallback="
                     + onOpportunisticSubscriptionsChangedListenerCallback
-                    + " carrierPrivilegesListener=" + carrierPrivilegesListener
+                    + " carrierPrivilegesCallback=" + carrierPrivilegesCallback
                     + " subId=" + subId + " phoneId=" + phoneId + " events=" + eventList + "}";
         }
     }
@@ -414,7 +414,9 @@
             mPreciseDataConnectionStates;
 
     /** Per-phoneId snapshot of privileged packages (names + UIDs). */
-    private List<Pair<List<String>, int[]>> mCarrierPrivilegeStates;
+    @NonNull private List<Pair<List<String>, int[]>> mCarrierPrivilegeStates;
+    /** Per-phoneId of CarrierService (PackageName, UID) pair. */
+    @NonNull private List<Pair<String, Integer>> mCarrierServiceStates;
 
     /**
      * Support backward compatibility for {@link android.telephony.TelephonyDisplayInfo}.
@@ -705,6 +707,7 @@
                 cutListToSize(mPhysicalChannelConfigs, mNumPhones);
                 cutListToSize(mLinkCapacityEstimateLists, mNumPhones);
                 cutListToSize(mCarrierPrivilegeStates, mNumPhones);
+                cutListToSize(mCarrierServiceStates, mNumPhones);
                 return;
             }
 
@@ -746,6 +749,7 @@
                 mAllowedNetworkTypeValue[i] = -1;
                 mLinkCapacityEstimateLists.add(i, INVALID_LCE_LIST);
                 mCarrierPrivilegeStates.add(i, new Pair<>(Collections.emptyList(), new int[0]));
+                mCarrierServiceStates.add(i, new Pair<>(null, Process.INVALID_UID));
             }
         }
     }
@@ -813,6 +817,7 @@
         mDataEnabledReason = new int[numPhones];
         mLinkCapacityEstimateLists = new ArrayList<>();
         mCarrierPrivilegeStates = new ArrayList<>();
+        mCarrierServiceStates = new ArrayList<>();
 
         for (int i = 0; i < numPhones; i++) {
             mCallState[i] =  TelephonyManager.CALL_STATE_IDLE;
@@ -851,6 +856,7 @@
             mAllowedNetworkTypeValue[i] = -1;
             mLinkCapacityEstimateLists.add(i, INVALID_LCE_LIST);
             mCarrierPrivilegeStates.add(i, new Pair<>(Collections.emptyList(), new int[0]));
+            mCarrierServiceStates.add(i, new Pair<>(null, Process.INVALID_UID));
         }
 
         mAppOps = mContext.getSystemService(AppOpsManager.class);
@@ -2013,10 +2019,8 @@
             return;
         }
 
-        ApnSetting apnSetting = preciseState.getApnSetting();
-
         synchronized (mRecords) {
-            if (validatePhoneId(phoneId)) {
+            if (validatePhoneId(phoneId) && preciseState.getApnSetting() != null) {
                 Pair<Integer, ApnSetting> key = Pair.create(preciseState.getTransportType(),
                         preciseState.getApnSetting());
                 PreciseDataConnectionState oldState = mPreciseDataConnectionStates.get(phoneId)
@@ -2786,16 +2790,16 @@
     }
 
     @Override
-    public void addCarrierPrivilegesListener(
+    public void addCarrierPrivilegesCallback(
             int phoneId,
-            ICarrierPrivilegesListener callback,
-            String callingPackage,
-            String callingFeatureId) {
+            @NonNull ICarrierPrivilegesCallback callback,
+            @NonNull String callingPackage,
+            @NonNull String callingFeatureId) {
         int callerUserId = UserHandle.getCallingUserId();
         mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
         mContext.enforceCallingOrSelfPermission(
                 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
-                "addCarrierPrivilegesListener");
+                "addCarrierPrivilegesCallback");
         if (VDBG) {
             log(
                     "listen carrier privs: E pkg=" + pii(callingPackage) + " phoneId=" + phoneId
@@ -2815,7 +2819,7 @@
             if (r == null) return;
 
             r.context = mContext;
-            r.carrierPrivilegesListener = callback;
+            r.carrierPrivilegesCallback = callback;
             r.callingPackage = callingPackage;
             r.callingFeatureId = callingFeatureId;
             r.callerUid = Binder.getCallingUid();
@@ -2827,10 +2831,18 @@
             }
 
             Pair<List<String>, int[]> state = mCarrierPrivilegeStates.get(phoneId);
+            Pair<String, Integer> carrierServiceState = mCarrierServiceStates.get(phoneId);
             try {
-                r.carrierPrivilegesListener.onCarrierPrivilegesChanged(
-                        Collections.unmodifiableList(state.first),
-                        Arrays.copyOf(state.second, state.second.length));
+                if (r.matchCarrierPrivilegesCallback()) {
+                    // Here, two callbacks are triggered in quick succession on the same binder.
+                    // In typical case, we expect the callers to care about only one or the other.
+                    r.carrierPrivilegesCallback.onCarrierPrivilegesChanged(
+                            Collections.unmodifiableList(state.first),
+                            Arrays.copyOf(state.second, state.second.length));
+
+                    r.carrierPrivilegesCallback.onCarrierServiceChanged(carrierServiceState.first,
+                            carrierServiceState.second);
+                }
             } catch (RemoteException ex) {
                 remove(r.binder);
             }
@@ -2838,12 +2850,12 @@
     }
 
     @Override
-    public void removeCarrierPrivilegesListener(
-            ICarrierPrivilegesListener callback, String callingPackage) {
+    public void removeCarrierPrivilegesCallback(
+            @NonNull ICarrierPrivilegesCallback callback, @NonNull String callingPackage) {
         mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
         mContext.enforceCallingOrSelfPermission(
                 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
-                "removeCarrierPrivilegesListener");
+                "removeCarrierPrivilegesCallback");
         remove(callback.asBinder());
     }
 
@@ -2868,13 +2880,13 @@
             for (Record r : mRecords) {
                 // Listeners are per-slot, not per-subscription. This is to provide a stable
                 // view across SIM profile switches.
-                if (!r.matchCarrierPrivilegesListener()
+                if (!r.matchCarrierPrivilegesCallback()
                         || !idMatch(r, SubscriptionManager.INVALID_SUBSCRIPTION_ID, phoneId)) {
                     continue;
                 }
                 try {
                     // Make sure even in-process listeners can't modify the values.
-                    r.carrierPrivilegesListener.onCarrierPrivilegesChanged(
+                    r.carrierPrivilegesCallback.onCarrierPrivilegesChanged(
                             Collections.unmodifiableList(privilegedPackageNames),
                             Arrays.copyOf(privilegedUids, privilegedUids.length));
                 } catch (RemoteException ex) {
@@ -2885,6 +2897,34 @@
         }
     }
 
+    @Override
+    public void notifyCarrierServiceChanged(int phoneId, @Nullable String packageName, int uid) {
+        if (!checkNotifyPermission("notifyCarrierServiceChanged")) return;
+        if (!validatePhoneId(phoneId)) return;
+        if (VDBG) {
+            log("notifyCarrierServiceChanged: phoneId=" + phoneId
+                    + ", package=" + pii(packageName) + ", uid=" + uid);
+        }
+
+        synchronized (mRecords) {
+            mCarrierServiceStates.set(
+                    phoneId, new Pair<>(packageName, uid));
+            for (Record r : mRecords) {
+                // Listeners are per-slot, not per-subscription.
+                if (!r.matchCarrierPrivilegesCallback()
+                        || !idMatch(r, SubscriptionManager.INVALID_SUBSCRIPTION_ID, phoneId)) {
+                    continue;
+                }
+                try {
+                    r.carrierPrivilegesCallback.onCarrierServiceChanged(packageName, uid);
+                } catch (RemoteException ex) {
+                    mRemoveList.add(r.binder);
+                }
+            }
+            handleRemoveListLocked();
+        }
+    }
+
     @NeverCompile // Avoid size overhead of debugging code.
     @Override
     public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
@@ -2940,6 +2980,9 @@
                 pw.println(
                         "mCarrierPrivilegeState=<packages=" + pii(carrierPrivilegeState.first)
                                 + ", uids=" + Arrays.toString(carrierPrivilegeState.second) + ">");
+                Pair<String, Integer> carrierServiceState = mCarrierServiceStates.get(i);
+                pw.println("mCarrierServiceState=<package=" + pii(carrierServiceState.first)
+                        + ", uid=" + carrierServiceState.second + ">");
                 pw.decreaseIndent();
             }
 
diff --git a/services/core/java/com/android/server/am/ActiveServices.java b/services/core/java/com/android/server/am/ActiveServices.java
index d4ad718..48e3264 100644
--- a/services/core/java/com/android/server/am/ActiveServices.java
+++ b/services/core/java/com/android/server/am/ActiveServices.java
@@ -2722,7 +2722,7 @@
     int bindServiceLocked(IApplicationThread caller, IBinder token, Intent service,
             String resolvedType, final IServiceConnection connection, int flags,
             String instanceName, boolean isSdkSandboxService, int sdkSandboxClientAppUid,
-            String callingPackage, final int userId)
+            String sdkSandboxClientAppPackage, String callingPackage, final int userId)
             throws TransactionTooLargeException {
         if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "bindService: " + service
                 + " type=" + resolvedType + " conn=" + connection.asBinder()
@@ -2807,8 +2807,9 @@
         final boolean allowInstant = (flags & Context.BIND_ALLOW_INSTANT) != 0;
 
         ServiceLookupResult res = retrieveServiceLocked(service, instanceName,
-                isSdkSandboxService, sdkSandboxClientAppUid, resolvedType, callingPackage,
-                callingPid, callingUid, userId, true, callerFg, isBindExternal, allowInstant);
+                isSdkSandboxService, sdkSandboxClientAppUid, sdkSandboxClientAppPackage,
+                resolvedType, callingPackage, callingPid, callingUid, userId, true, callerFg,
+                isBindExternal, allowInstant);
         if (res == null) {
             return 0;
         }
@@ -3228,14 +3229,14 @@
             int callingPid, int callingUid, int userId,
             boolean createIfNeeded, boolean callingFromFg, boolean isBindExternal,
             boolean allowInstant) {
-        return retrieveServiceLocked(service, instanceName, false, 0, resolvedType, callingPackage,
-                callingPid, callingUid, userId, createIfNeeded, callingFromFg, isBindExternal,
-                allowInstant);
+        return retrieveServiceLocked(service, instanceName, false, 0, null, resolvedType,
+                callingPackage, callingPid, callingUid, userId, createIfNeeded, callingFromFg,
+                isBindExternal, allowInstant);
     }
 
     private ServiceLookupResult retrieveServiceLocked(Intent service,
             String instanceName, boolean isSdkSandboxService, int sdkSandboxClientAppUid,
-            String resolvedType,
+            String sdkSandboxClientAppPackage, String resolvedType,
             String callingPackage, int callingPid, int callingUid, int userId,
             boolean createIfNeeded, boolean callingFromFg, boolean isBindExternal,
             boolean allowInstant) {
@@ -3416,7 +3417,8 @@
                                                                                   : null;
                     r = new ServiceRecord(mAm, className, name, definingPackageName,
                             definingUid, filter, sInfo, callingFromFg, res,
-                            sdkSandboxProcessName, sdkSandboxClientAppUid);
+                            sdkSandboxProcessName, sdkSandboxClientAppUid,
+                            sdkSandboxClientAppPackage);
                     res.setService(r);
                     smap.mServicesByInstanceName.put(name, r);
                     smap.mServicesByIntent.put(filter, r);
@@ -4195,7 +4197,7 @@
             if (r.isSdkSandbox) {
                 final int uid = Process.toSdkSandboxUid(r.sdkSandboxClientAppUid);
                 app = mAm.startSdkSandboxProcessLocked(procName, r.appInfo, true, intentFlags,
-                        hostingRecord, ZYGOTE_POLICY_FLAG_EMPTY, uid);
+                        hostingRecord, ZYGOTE_POLICY_FLAG_EMPTY, uid, r.sdkSandboxClientAppPackage);
                 r.isolationHostProc = app;
             } else {
                 app = mAm.startProcessLocked(procName, r.appInfo, true, intentFlags,
diff --git a/services/core/java/com/android/server/am/ActivityManagerConstants.java b/services/core/java/com/android/server/am/ActivityManagerConstants.java
index 18e0155..c426662 100644
--- a/services/core/java/com/android/server/am/ActivityManagerConstants.java
+++ b/services/core/java/com/android/server/am/ActivityManagerConstants.java
@@ -127,7 +127,15 @@
     static final String KEY_KILL_BG_RESTRICTED_CACHED_IDLE = "kill_bg_restricted_cached_idle";
     static final String KEY_KILL_BG_RESTRICTED_CACHED_IDLE_SETTLE_TIME =
             "kill_bg_restricted_cached_idle_settle_time";
+    /**
+     * Note this key is on {@link DeviceConfig#NAMESPACE_ACTIVITY_MANAGER_COMPONENT_ALIAS}.
+     * @see #mEnableComponentAlias
+     */
     static final String KEY_ENABLE_COMPONENT_ALIAS = "enable_experimental_component_alias";
+    /**
+     * Note this key is on {@link DeviceConfig#NAMESPACE_ACTIVITY_MANAGER_COMPONENT_ALIAS}.
+     * @see #mComponentAliasOverrides
+     */
     static final String KEY_COMPONENT_ALIAS_OVERRIDES = "component_alias_overrides";
 
     private static final int DEFAULT_MAX_CACHED_PROCESSES = 32;
@@ -899,10 +907,6 @@
                             case KEY_ENABLE_EXTRA_SERVICE_RESTART_DELAY_ON_MEM_PRESSURE:
                                 updateEnableExtraServiceRestartDelayOnMemPressure();
                                 break;
-                            case KEY_ENABLE_COMPONENT_ALIAS:
-                            case KEY_COMPONENT_ALIAS_OVERRIDES:
-                                updateComponentAliases();
-                                break;
                             case KEY_PROCESS_KILL_TIMEOUT:
                                 updateProcessKillTimeout();
                                 break;
@@ -925,6 +929,26 @@
                 }
             };
 
+    private final OnPropertiesChangedListener mOnDeviceConfigChangedForComponentAliasListener =
+            new OnPropertiesChangedListener() {
+                @Override
+                public void onPropertiesChanged(Properties properties) {
+                    for (String name : properties.getKeyset()) {
+                        if (name == null) {
+                            return;
+                        }
+                        switch (name) {
+                            case KEY_ENABLE_COMPONENT_ALIAS:
+                            case KEY_COMPONENT_ALIAS_OVERRIDES:
+                                updateComponentAliases();
+                                break;
+                            default:
+                                break;
+                        }
+                    }
+                }
+            };
+
     ActivityManagerConstants(Context context, ActivityManagerService service, Handler handler) {
         super(handler);
         mService = service;
@@ -991,6 +1015,10 @@
         DeviceConfig.addOnPropertiesChangedListener(DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
                 ActivityThread.currentApplication().getMainExecutor(),
                 mOnDeviceConfigChangedListener);
+        DeviceConfig.addOnPropertiesChangedListener(
+                DeviceConfig.NAMESPACE_ACTIVITY_MANAGER_COMPONENT_ALIAS,
+                ActivityThread.currentApplication().getMainExecutor(),
+                mOnDeviceConfigChangedForComponentAliasListener);
         loadDeviceConfigConstants();
         // The following read from Settings.
         updateActivityStartsLoggingEnabled();
@@ -1000,6 +1028,9 @@
     private void loadDeviceConfigConstants() {
         mOnDeviceConfigChangedListener.onPropertiesChanged(
                 DeviceConfig.getProperties(DeviceConfig.NAMESPACE_ACTIVITY_MANAGER));
+        mOnDeviceConfigChangedForComponentAliasListener.onPropertiesChanged(
+                DeviceConfig.getProperties(
+                        DeviceConfig.NAMESPACE_ACTIVITY_MANAGER_COMPONENT_ALIAS));
     }
 
     public void setOverrideMaxCachedProcesses(int value) {
@@ -1379,11 +1410,11 @@
 
     private void updateComponentAliases() {
         mEnableComponentAlias = DeviceConfig.getBoolean(
-                DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
+                DeviceConfig.NAMESPACE_ACTIVITY_MANAGER_COMPONENT_ALIAS,
                 KEY_ENABLE_COMPONENT_ALIAS,
                 DEFAULT_ENABLE_COMPONENT_ALIAS);
         mComponentAliasOverrides = DeviceConfig.getString(
-                DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
+                DeviceConfig.NAMESPACE_ACTIVITY_MANAGER_COMPONENT_ALIAS,
                 KEY_COMPONENT_ALIAS_OVERRIDES,
                 DEFAULT_COMPONENT_ALIAS_OVERRIDES);
         mService.mComponentAliasResolver.update(mEnableComponentAlias, mComponentAliasOverrides);
diff --git a/services/core/java/com/android/server/am/ActivityManagerLocal.java b/services/core/java/com/android/server/am/ActivityManagerLocal.java
index 3226a2b..1d2c36b 100644
--- a/services/core/java/com/android/server/am/ActivityManagerLocal.java
+++ b/services/core/java/com/android/server/am/ActivityManagerLocal.java
@@ -74,6 +74,8 @@
      * @param conn Receives information as the service is started and stopped.
      *        This must be a valid ServiceConnection object; it must not be null.
      * @param clientAppUid Uid of the app for which the sdk sandbox process needs to be spawned.
+     * @param clientAppPackage Package of the app for which the sdk sandbox process needs to
+     *        be spawned. This package must belong to the clientAppUid.
      * @param processName Unique identifier for the service instance. Each unique name here will
      *        result in a different service instance being created. Identifiers must only contain
      *        ASCII letters, digits, underscores, and periods.
@@ -87,6 +89,7 @@
      */
     @SuppressLint("RethrowRemoteException")
     boolean bindSdkSandboxService(@NonNull Intent service, @NonNull ServiceConnection conn,
-            int clientAppUid, @NonNull String processName, @Context.BindServiceFlags int flags)
+            int clientAppUid, @NonNull String clientAppPackage, @NonNull String processName,
+            @Context.BindServiceFlags int flags)
             throws RemoteException;
 }
diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java
index 9512e1d..0735648 100644
--- a/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -36,6 +36,7 @@
 import static android.app.ActivityManager.StopUserOnSwitch;
 import static android.app.ActivityManagerInternal.ALLOW_FULL_ONLY;
 import static android.app.ActivityManagerInternal.ALLOW_NON_FULL;
+import static android.app.AppOpsManager.MODE_ALLOWED;
 import static android.app.AppOpsManager.OP_NONE;
 import static android.content.pm.ApplicationInfo.HIDDEN_API_ENFORCEMENT_DEFAULT;
 import static android.content.pm.PackageManager.GET_SHARED_LIBRARY_FILES;
@@ -1895,6 +1896,7 @@
                         0,
                         false,
                         0,
+                        null,
                         new HostingRecord("system"));
                 app.setPersistent(true);
                 app.setPid(MY_PID);
@@ -2783,7 +2785,8 @@
                     false /* knownToBeDead */, 0 /* intentFlags */,
                     sNullHostingRecord  /* hostingRecord */, ZYGOTE_POLICY_FLAG_EMPTY,
                     true /* allowWhileBooting */, true /* isolated */,
-                    uid, false /* supplemental */, 0 /* supplementalUid */,
+                    uid, false /* isSdkSandbox */, 0 /* sdkSandboxUid */,
+                    null /* sdkSandboxClientAppPackage */,
                     abiOverride, entryPoint, entryPointArgs, crashHandler);
             return proc != null;
         }
@@ -2792,11 +2795,12 @@
     @GuardedBy("this")
     final ProcessRecord startSdkSandboxProcessLocked(String processName,
             ApplicationInfo info, boolean knownToBeDead, int intentFlags,
-            HostingRecord hostingRecord, int zygotePolicyFlags, int sdkSandboxUid) {
+            HostingRecord hostingRecord, int zygotePolicyFlags, int sdkSandboxUid,
+            String sdkSandboxClientAppPackage) {
         return mProcessList.startProcessLocked(processName, info, knownToBeDead, intentFlags,
                 hostingRecord, zygotePolicyFlags, false /* allowWhileBooting */,
                 false /* isolated */, 0 /* isolatedUid */,
-                true /* isSdkSandbox */, sdkSandboxUid,
+                true /* isSdkSandbox */, sdkSandboxUid, sdkSandboxClientAppPackage,
                 null /* ABI override */, null /* entryPoint */,
                 null /* entryPointArgs */, null /* crashHandler */);
     }
@@ -2808,7 +2812,8 @@
             boolean isolated) {
         return mProcessList.startProcessLocked(processName, info, knownToBeDead, intentFlags,
                 hostingRecord, zygotePolicyFlags, allowWhileBooting, isolated, 0 /* isolatedUid */,
-                false /* isSdkSandbox */, 0 /* sdkSandboxClientdAppUid */,
+                false /* isSdkSandbox */, 0 /* sdkSandboxClientAppUid */,
+                null /* sdkSandboxClientAppPackage */,
                 null /* ABI override */, null /* entryPoint */,
                 null /* entryPointArgs */, null /* crashHandler */);
     }
@@ -3780,7 +3785,7 @@
                     synchronized (mProcLock) {
                         mProcessList.killPackageProcessesLSP(packageName, appId, targetUserId,
                                 ProcessList.SERVICE_ADJ, ApplicationExitInfo.REASON_USER_REQUESTED,
-                                ApplicationExitInfo.SUBREASON_UNKNOWN, "kill background");
+                                ApplicationExitInfo.SUBREASON_KILL_BACKGROUND, "kill background");
                     }
                 }
             }
@@ -3810,7 +3815,7 @@
                     mProcessList.killPackageProcessesLSP(null /* packageName */, -1 /* appId */,
                             UserHandle.USER_ALL, ProcessList.CACHED_APP_MIN_ADJ,
                             ApplicationExitInfo.REASON_USER_REQUESTED,
-                            ApplicationExitInfo.SUBREASON_UNKNOWN,
+                            ApplicationExitInfo.SUBREASON_KILL_BACKGROUND,
                             "kill all background");
                 }
 
@@ -4352,7 +4357,7 @@
                         ProcessList.INVALID_ADJ, true, false, true,
                         false, true /* setRemoved */, false,
                         ApplicationExitInfo.REASON_USER_REQUESTED,
-                        ApplicationExitInfo.SUBREASON_UNKNOWN,
+                        ApplicationExitInfo.SUBREASON_STOP_APP,
                         "fully stop " + packageName + "/" + userId + " by user request");
             }
 
@@ -4404,7 +4409,7 @@
                     evenPersistent, true /* setRemoved */, uninstalling,
                     packageName == null ? ApplicationExitInfo.REASON_USER_STOPPED
                     : ApplicationExitInfo.REASON_USER_REQUESTED,
-                    ApplicationExitInfo.SUBREASON_UNKNOWN,
+                    ApplicationExitInfo.SUBREASON_FORCE_STOP,
                     (packageName == null ? ("stop user " + userId) : ("stop " + packageName))
                     + " due to " + reason);
         }
@@ -4773,7 +4778,8 @@
                 thread.runIsolatedEntryPoint(
                         app.getIsolatedEntryPoint(), app.getIsolatedEntryPointArgs());
             } else if (instr2 != null) {
-                thread.bindApplication(processName, appInfo, providerList,
+                thread.bindApplication(processName, appInfo, app.sdkSandboxClientAppPackage,
+                        providerList,
                         instr2.mClass,
                         profilerInfo, instr2.mArguments,
                         instr2.mWatcher,
@@ -4787,8 +4793,8 @@
                         app.getDisabledCompatChanges(), serializedSystemFontMap,
                         app.getStartElapsedTime(), app.getStartUptime());
             } else {
-                thread.bindApplication(processName, appInfo, providerList, null, profilerInfo,
-                        null, null, null, testMode,
+                thread.bindApplication(processName, appInfo, app.sdkSandboxClientAppPackage,
+                        providerList, null, profilerInfo, null, null, null, testMode,
                         mBinderTransactionTrackingEnabled, enableTrackAllocation,
                         isRestrictedBackupMode || !normalMode, app.isPersistent(),
                         new Configuration(app.getWindowProcessController().getConfiguration()),
@@ -5749,6 +5755,18 @@
                 owningUid, exported);
     }
 
+    private void enforceDebuggable(ProcessRecord proc) {
+        if (!Build.IS_DEBUGGABLE && !proc.isDebuggable()) {
+            throw new SecurityException("Process not debuggable: " + proc.info.packageName);
+        }
+    }
+
+    private void enforceDebuggable(ApplicationInfo info) {
+        if (!Build.IS_DEBUGGABLE && (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) == 0) {
+            throw new SecurityException("Process not debuggable: " + info.packageName);
+        }
+    }
+
     /**
      * As the only public entry point for permissions checking, this method
      * can enforce the semantic that requesting a check on a null global
@@ -6553,7 +6571,7 @@
 
         if (app == null) {
             app = mProcessList.newProcessRecordLocked(info, customProcess, isolated, 0,
-                    false, 0,
+                    false, 0, null,
                     new HostingRecord("added application",
                         customProcess != null ? customProcess : info.processName));
             updateLruProcessLocked(app, false, null);
@@ -6786,22 +6804,25 @@
     }
 
     void setTrackAllocationApp(ApplicationInfo app, String processName) {
-        if (!Build.IS_DEBUGGABLE) {
-            if ((app.flags & ApplicationInfo.FLAG_DEBUGGABLE) == 0) {
-                throw new SecurityException("Process not debuggable: " + app.packageName);
-            }
-        }
+        enforceDebuggable(app);
 
         synchronized (mProcLock) {
             mTrackAllocationApp = processName;
         }
     }
 
-    void setProfileApp(ApplicationInfo app, String processName, ProfilerInfo profilerInfo) {
+    void setProfileApp(ApplicationInfo app, String processName, ProfilerInfo profilerInfo,
+            ApplicationInfo sdkSandboxClientApp) {
         synchronized (mAppProfiler.mProfilerLock) {
             if (!Build.IS_DEBUGGABLE) {
                 boolean isAppDebuggable = (app.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
                 boolean isAppProfileable = app.isProfileableByShell();
+
+                if (sdkSandboxClientApp != null) {
+                    isAppDebuggable |=
+                            (sdkSandboxClientApp.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
+                    isAppProfileable |= sdkSandboxClientApp.isProfileableByShell();
+                }
                 if (!isAppDebuggable && !isAppProfileable) {
                     throw new SecurityException("Process not debuggable, "
                             + "and not profileable by shell: " + app.packageName);
@@ -6812,11 +6833,7 @@
     }
 
     void setNativeDebuggingAppLocked(ApplicationInfo app, String processName) {
-        if (!Build.IS_DEBUGGABLE) {
-            if ((app.flags & ApplicationInfo.FLAG_DEBUGGABLE) == 0) {
-                throw new SecurityException("Process not debuggable: " + app.packageName);
-            }
-        }
+        enforceDebuggable(app);
         mNativeDebuggingApp = processName;
     }
 
@@ -12395,13 +12412,13 @@
             String resolvedType, IServiceConnection connection, int flags, String instanceName,
             String callingPackage, int userId) throws TransactionTooLargeException {
         return bindServiceInstance(caller, token, service, resolvedType, connection, flags,
-                instanceName, false, 0, callingPackage, userId);
+                instanceName, false, 0, null, callingPackage, userId);
     }
 
     private int bindServiceInstance(IApplicationThread caller, IBinder token, Intent service,
             String resolvedType, IServiceConnection connection, int flags, String instanceName,
-            boolean isSdkSandboxService, int sdkSandboxClientdAppUid, String callingPackage,
-            int userId)
+            boolean isSdkSandboxService, int sdkSandboxClientAppUid,
+            String sdkSandboxClientAppPackage, String callingPackage, int userId)
             throws TransactionTooLargeException {
         enforceNotIsolatedCaller("bindService");
 
@@ -12438,8 +12455,8 @@
             }
             synchronized (this) {
                 return mServices.bindServiceLocked(caller, token, service, resolvedType, connection,
-                        flags, instanceName, isSdkSandboxService, sdkSandboxClientdAppUid,
-                        callingPackage, userId);
+                        flags, instanceName, isSdkSandboxService, sdkSandboxClientAppUid,
+                        sdkSandboxClientAppPackage, callingPackage, userId);
             }
         } finally {
             Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
@@ -13636,7 +13653,7 @@
                                                     UserHandle.getAppId(extraUid),
                                                     userId, ProcessList.INVALID_ADJ,
                                                     ApplicationExitInfo.REASON_USER_REQUESTED,
-                                                    ApplicationExitInfo.SUBREASON_UNKNOWN,
+                                                    ApplicationExitInfo.SUBREASON_PACKAGE_UPDATE,
                                                     "change " + ssp);
                                         }
                                     }
@@ -15562,12 +15579,7 @@
                     throw new IllegalArgumentException("Unknown process: " + process);
                 }
 
-                boolean isDebuggable = Build.IS_DEBUGGABLE;
-                if (!isDebuggable) {
-                    if ((proc.info.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) {
-                        throw new SecurityException("Process not debuggable: " + proc);
-                    }
-                }
+                enforceDebuggable(proc);
 
                 mOomAdjuster.mCachedAppOptimizer.enableFreezer(false);
 
@@ -15670,10 +15682,7 @@
                     throw new SecurityException("No process found for calling pid "
                             + Binder.getCallingPid());
                 }
-                if (!Build.IS_DEBUGGABLE
-                        && (proc.info.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) {
-                    throw new SecurityException("Not running a debuggable build");
-                }
+                enforceDebuggable(proc);
                 processName = proc.processName;
                 uid = proc.uid;
                 if (reportPackage != null && !proc.getPkgList().containsKey(reportPackage)) {
@@ -15884,13 +15893,7 @@
             return false;
         }
 
-        if (!Build.IS_DEBUGGABLE) {
-            if ((process.info.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) {
-                return false;
-            }
-        }
-
-        return true;
+        return Build.IS_DEBUGGABLE || process.isDebuggable();
     }
 
     public boolean startBinderTracking() throws RemoteException {
@@ -16022,22 +16025,29 @@
 
         @Override
         public boolean bindSdkSandboxService(Intent service, ServiceConnection conn,
-                int userAppUid, String processName, int flags) throws RemoteException {
+                int clientAppUid, String clientAppPackage, String processName, int flags)
+                throws RemoteException {
             if (service == null) {
                 throw new IllegalArgumentException("intent is null");
             }
             if (conn == null) {
                 throw new IllegalArgumentException("connection is null");
             }
+            if (clientAppPackage == null) {
+                throw new IllegalArgumentException("clientAppPackage is null");
+            }
             if (processName == null) {
                 throw new IllegalArgumentException("processName is null");
             }
             if (service.getComponent() == null) {
                 throw new IllegalArgumentException("service must specify explicit component");
             }
-            if (!UserHandle.isApp(userAppUid)) {
+            if (!UserHandle.isApp(clientAppUid)) {
                 throw new IllegalArgumentException("uid is not within application range");
             }
+            if (mAppOpsService.checkPackage(clientAppUid, clientAppPackage) != MODE_ALLOWED) {
+                throw new IllegalArgumentException("uid does not belong to provided package");
+            }
 
             Handler handler = mContext.getMainThreadHandler();
 
@@ -16046,8 +16056,8 @@
             return ActivityManagerService.this.bindServiceInstance(
                     mContext.getIApplicationThread(), mContext.getActivityToken(), service,
                     service.resolveTypeIfNeeded(mContext.getContentResolver()), sd, flags,
-                    processName, /*isSupplementalProcessService*/ true, userAppUid,
-                    mContext.getOpPackageName(), UserHandle.getUserId(userAppUid)) != 0;
+                    processName, /*isSdkSandboxService*/ true, clientAppUid, clientAppPackage,
+                    mContext.getOpPackageName(), UserHandle.getUserId(clientAppUid)) != 0;
         }
 
         @Override
@@ -16365,7 +16375,7 @@
                     if (pr.mState.getSetSchedGroup() == ProcessList.SCHED_GROUP_BACKGROUND
                             && pr.mReceivers.numberOfCurReceivers() == 0) {
                         pr.killLocked("remove task", ApplicationExitInfo.REASON_USER_REQUESTED,
-                                ApplicationExitInfo.SUBREASON_UNKNOWN, true);
+                                ApplicationExitInfo.SUBREASON_REMOVE_TASK, true);
                     } else {
                         // We delay killing processes that are not in the background or running a
                         // receiver.
@@ -16837,7 +16847,7 @@
                     }
 
                     if (profilerInfo != null) {
-                        setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
+                        setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo, null);
                     }
                     wmLock.notify();
                 }
@@ -17625,11 +17635,7 @@
                     throw new IllegalArgumentException("Unknown process: " + process);
                 }
 
-                if (!Build.IS_DEBUGGABLE) {
-                    if ((proc.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) == 0) {
-                        throw new SecurityException("Process not debuggable: " + proc);
-                    }
-                }
+                enforceDebuggable(proc);
 
                 thread.attachAgent(path);
             }
diff --git a/services/core/java/com/android/server/am/ActivityManagerShellCommand.java b/services/core/java/com/android/server/am/ActivityManagerShellCommand.java
index 28b807c..2b61e7f 100644
--- a/services/core/java/com/android/server/am/ActivityManagerShellCommand.java
+++ b/services/core/java/com/android/server/am/ActivityManagerShellCommand.java
@@ -343,6 +343,8 @@
                     return runSetStopUserOnSwitch(pw);
                 case "set-bg-abusive-uids":
                     return runSetBgAbusiveUids(pw);
+                case "list-bg-exemptions-config":
+                    return runListBgExemptionsConfig(pw);
                 default:
                     return handleDefaultCommands(cmd);
             }
@@ -3292,6 +3294,19 @@
         return 0;
     }
 
+    private int runListBgExemptionsConfig(PrintWriter pw) throws RemoteException {
+        final ArraySet<String> sysConfigs = mInternal.mAppRestrictionController
+                .mBgRestrictionExemptioFromSysConfig;
+        if (sysConfigs != null) {
+            for (int i = 0, size = sysConfigs.size(); i < size; i++) {
+                pw.print(sysConfigs.valueAt(i));
+                pw.print(' ');
+            }
+            pw.println();
+        }
+        return 0;
+    }
+
     private Resources getResources(PrintWriter pw) throws RemoteException {
         // system resources does not contain all the device configuration, construct it manually.
         Configuration config = mInterface.getConfiguration();
diff --git a/services/core/java/com/android/server/am/AppBatteryTracker.java b/services/core/java/com/android/server/am/AppBatteryTracker.java
index 7cd45fe..ea1e335 100644
--- a/services/core/java/com/android/server/am/AppBatteryTracker.java
+++ b/services/core/java/com/android/server/am/AppBatteryTracker.java
@@ -28,7 +28,6 @@
 import static android.content.pm.PackageManager.PERMISSION_GRANTED;
 import static android.os.BatteryConsumer.POWER_COMPONENT_ANY;
 import static android.os.BatteryConsumer.PROCESS_STATE_BACKGROUND;
-import static android.os.BatteryConsumer.PROCESS_STATE_COUNT;
 import static android.os.BatteryConsumer.PROCESS_STATE_FOREGROUND;
 import static android.os.BatteryConsumer.PROCESS_STATE_FOREGROUND_SERVICE;
 import static android.os.BatteryConsumer.PROCESS_STATE_UNSPECIFIED;
@@ -38,7 +37,6 @@
 import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
 import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
 import static com.android.server.am.AppRestrictionController.DEVICE_CONFIG_SUBNAMESPACE_PREFIX;
-import static com.android.server.am.BaseAppStateTracker.ONE_MINUTE;
 
 import android.annotation.NonNull;
 import android.annotation.Nullable;
@@ -71,7 +69,6 @@
 import com.android.internal.util.ArrayUtils;
 import com.android.server.am.AppBatteryTracker.AppBatteryPolicy;
 import com.android.server.am.AppRestrictionController.UidBatteryUsageProvider;
-import com.android.server.am.BaseAppStateTracker.Injector;
 import com.android.server.pm.UserManagerInternal;
 
 import java.io.PrintWriter;
@@ -684,7 +681,7 @@
         static final int BATTERY_USAGE_INDEX_FOREGROUND = PROCESS_STATE_FOREGROUND;
         static final int BATTERY_USAGE_INDEX_BACKGROUND = PROCESS_STATE_BACKGROUND;
         static final int BATTERY_USAGE_INDEX_FOREGROUND_SERVICE = PROCESS_STATE_FOREGROUND_SERVICE;
-        static final int BATTERY_USAGE_COUNT = PROCESS_STATE_COUNT;
+        static final int BATTERY_USAGE_COUNT = 4;
 
         static final Dimensions[] BATT_DIMENS = new Dimensions[] {
                 new Dimensions(AppBatteryPolicy.DEFAULT_BG_CURRENT_DRAIN_POWER_COMPONENTS,
diff --git a/services/core/java/com/android/server/am/AppProfiler.java b/services/core/java/com/android/server/am/AppProfiler.java
index d6a4cf6..16a7283 100644
--- a/services/core/java/com/android/server/am/AppProfiler.java
+++ b/services/core/java/com/android/server/am/AppProfiler.java
@@ -58,7 +58,6 @@
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.Intent;
-import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager.NameNotFoundException;
 import android.net.Uri;
 import android.os.Binder;
@@ -608,13 +607,7 @@
         if (check != null) {
             if ((pss * 1024) >= check && profile.getThread() != null
                     && mMemWatchDumpProcName == null) {
-                boolean isDebuggable = Build.IS_DEBUGGABLE;
-                if (!isDebuggable) {
-                    if ((proc.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
-                        isDebuggable = true;
-                    }
-                }
-                if (isDebuggable) {
+                if (Build.IS_DEBUGGABLE || proc.isDebuggable()) {
                     Slog.w(TAG, "Process " + proc + " exceeded pss limit " + check + "; reporting");
                     startHeapDumpLPf(profile, false);
                 } else {
@@ -1702,7 +1695,8 @@
         try {
             if (start) {
                 stopProfilerLPf(null, 0);
-                mService.setProfileApp(proc.info, proc.processName, profilerInfo);
+                mService.setProfileApp(proc.info, proc.processName, profilerInfo,
+                        proc.isSdkSandbox ? proc.getClientInfoForSdkSandbox() : null);
                 mProfileData.setProfileProc(proc);
                 mProfileType = profileType;
                 ParcelFileDescriptor fd = profilerInfo.profileFd;
@@ -1886,8 +1880,7 @@
                                     BatteryStatsImpl.Uid.Proc ps = st.batteryStats;
                                     if (ps == null || !ps.isActive()) {
                                         st.batteryStats = ps = bstats.getProcessStatsLocked(
-                                                bstats.mapUid(st.uid), st.name,
-                                                elapsedRealtime, uptime);
+                                                st.uid, st.name, elapsedRealtime, uptime);
                                     }
                                     ps.addCpuTimeLocked(st.rel_utime, st.rel_stime);
                                 }
@@ -2076,7 +2069,7 @@
             if (mAppAgentMap != null && mAppAgentMap.containsKey(processName)) {
                 // We need to do a debuggable check here. See setAgentApp for why the check is
                 // postponed to here.
-                if ((app.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
+                if (app.isDebuggable()) {
                     String agent = mAppAgentMap.get(processName);
                     // Do not overwrite already requested agent.
                     if (profilerInfo == null) {
@@ -2133,7 +2126,7 @@
         if (preBindAgent != null) {
             thread.attachAgent(preBindAgent);
         }
-        if ((app.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
+        if (app.isDebuggable()) {
             thread.attachStartupAgents(app.info.dataDir);
         }
         return profilerInfo;
diff --git a/services/core/java/com/android/server/am/AppRestrictionController.java b/services/core/java/com/android/server/am/AppRestrictionController.java
index 6e6cb87..dc8403a 100644
--- a/services/core/java/com/android/server/am/AppRestrictionController.java
+++ b/services/core/java/com/android/server/am/AppRestrictionController.java
@@ -182,7 +182,7 @@
     /**
      * Whether or not to show the foreground service manager on tapping notifications.
      */
-    private static final boolean ENABLE_SHOW_FOREGROUND_SERVICE_MANAGER = false;
+    private static final boolean ENABLE_SHOW_FOREGROUND_SERVICE_MANAGER = true;
 
     private final Context mContext;
     private final HandlerThread mBgHandlerThread;
@@ -241,7 +241,7 @@
     /**
      * The pre-config packages that are exempted from the background restrictions.
      */
-    private ArraySet<String> mBgRestrictionExemptioFromSysConfig;
+    ArraySet<String> mBgRestrictionExemptioFromSysConfig;
 
     /**
      * Lock specifically for bookkeeping around the carrier-privileged app set.
@@ -1595,8 +1595,8 @@
                         cancelRequestBgRestrictedIfNecessary(packageName, uid);
                         final Intent newIntent = new Intent(ACTION_SHOW_FOREGROUND_SERVICE_MANAGER);
                         newIntent.addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
-                        mContext.sendBroadcastAsUser(newIntent,
-                                UserHandle.of(UserHandle.getUserId(uid)));
+                        // Task manager runs in SystemUI, which is SYSTEM user only.
+                        mContext.sendBroadcastAsUser(newIntent, UserHandle.SYSTEM);
                         break;
                 }
             }
@@ -1670,9 +1670,10 @@
             if (ENABLE_SHOW_FOREGROUND_SERVICE_MANAGER) {
                 final Intent intent = new Intent(ACTION_SHOW_FOREGROUND_SERVICE_MANAGER);
                 intent.addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
+                // Task manager runs in SystemUI, which is SYSTEM user only.
                 pendingIntent = PendingIntent.getBroadcastAsUser(mContext, 0,
                         intent, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE,
-                        UserHandle.of(UserHandle.getUserId(uid)));
+                        UserHandle.SYSTEM);
             } else {
                 final Intent intent = new Intent(Settings.ACTION_VIEW_ADVANCED_POWER_USAGE_DETAIL);
                 intent.setData(Uri.fromParts(PACKAGE_SCHEME, packageName, null));
@@ -1750,7 +1751,7 @@
                     SYSTEM_UID, UserHandle.getUserId(uid));
             final String title = mContext.getString(titleRes);
             final String message = mContext.getString(messageRes,
-                    ai != null ? pm.getText(packageName, ai.labelRes, ai) : packageName);
+                    ai != null ? ai.loadLabel(pm) : packageName);
             final Icon icon = ai != null ? Icon.createWithResource(packageName, ai.icon) : null;
 
             postNotification(notificationId, packageName, uid, title, message, icon, pendingIntent,
diff --git a/services/core/java/com/android/server/am/BatteryStatsService.java b/services/core/java/com/android/server/am/BatteryStatsService.java
index 91822ac..eb7897b 100644
--- a/services/core/java/com/android/server/am/BatteryStatsService.java
+++ b/services/core/java/com/android/server/am/BatteryStatsService.java
@@ -799,6 +799,7 @@
                     final BatteryUsageStatsQuery querySinceReset =
                             new BatteryUsageStatsQuery.Builder()
                                     .includeProcessStateData()
+                                    .includeVirtualUids()
                                     .build();
                     bus = getBatteryUsageStats(List.of(querySinceReset)).get(0);
                     break;
@@ -806,6 +807,7 @@
                     final BatteryUsageStatsQuery queryPowerProfile =
                             new BatteryUsageStatsQuery.Builder()
                                     .includeProcessStateData()
+                                    .includeVirtualUids()
                                     .powerProfileModeledOnly()
                                     .build();
                     bus = getBatteryUsageStats(List.of(queryPowerProfile)).get(0);
@@ -821,6 +823,7 @@
                     final BatteryUsageStatsQuery queryBeforeReset =
                             new BatteryUsageStatsQuery.Builder()
                                     .includeProcessStateData()
+                                    .includeVirtualUids()
                                     .aggregateSnapshots(sessionStart, sessionEnd)
                                     .build();
                     bus = getBatteryUsageStats(List.of(queryBeforeReset)).get(0);
diff --git a/services/core/java/com/android/server/am/BroadcastQueue.java b/services/core/java/com/android/server/am/BroadcastQueue.java
index ea63c08..ade44ea 100644
--- a/services/core/java/com/android/server/am/BroadcastQueue.java
+++ b/services/core/java/com/android/server/am/BroadcastQueue.java
@@ -21,6 +21,9 @@
 import static android.os.Process.ZYGOTE_POLICY_FLAG_LATENCY_SENSITIVE;
 import static android.text.TextUtils.formatSimple;
 
+import static com.android.internal.util.FrameworkStatsLog.BOOT_COMPLETED_BROADCAST_COMPLETION_LATENCY_REPORTED;
+import static com.android.internal.util.FrameworkStatsLog.BOOT_COMPLETED_BROADCAST_COMPLETION_LATENCY_REPORTED__EVENT__BOOT_COMPLETED;
+import static com.android.internal.util.FrameworkStatsLog.BOOT_COMPLETED_BROADCAST_COMPLETION_LATENCY_REPORTED__EVENT__LOCKED_BOOT_COMPLETED;
 import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_BROADCAST;
 import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_BROADCAST_DEFERRAL;
 import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_BROADCAST_LIGHT;
@@ -29,6 +32,7 @@
 import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_BROADCAST;
 import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_MU;
 
+
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.app.ActivityManager;
@@ -51,6 +55,7 @@
 import android.content.pm.PackageManager;
 import android.content.pm.PermissionInfo;
 import android.content.pm.ResolveInfo;
+import android.content.pm.UserInfo;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.IBinder;
@@ -64,6 +69,7 @@
 import android.os.SystemClock;
 import android.os.Trace;
 import android.os.UserHandle;
+import android.os.UserManager;
 import android.permission.IPermissionManager;
 import android.text.TextUtils;
 import android.util.EventLog;
@@ -75,6 +81,7 @@
 import com.android.internal.util.ArrayUtils;
 import com.android.internal.util.FrameworkStatsLog;
 import com.android.server.LocalServices;
+import com.android.server.pm.UserManagerInternal;
 
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
@@ -250,12 +257,16 @@
 
     public void enqueueParallelBroadcastLocked(BroadcastRecord r) {
         r.enqueueClockTime = System.currentTimeMillis();
+        r.enqueueTime = SystemClock.uptimeMillis();
+        r.enqueueRealTime = SystemClock.elapsedRealtime();
         mParallelBroadcasts.add(r);
         enqueueBroadcastHelper(r);
     }
 
     public void enqueueOrderedBroadcastLocked(BroadcastRecord r) {
         r.enqueueClockTime = System.currentTimeMillis();
+        r.enqueueTime = SystemClock.uptimeMillis();
+        r.enqueueRealTime = SystemClock.elapsedRealtime();
         mDispatcher.enqueueOrderedBroadcastLocked(r);
         enqueueBroadcastHelper(r);
     }
@@ -1121,6 +1132,7 @@
         while (mParallelBroadcasts.size() > 0) {
             r = mParallelBroadcasts.remove(0);
             r.dispatchTime = SystemClock.uptimeMillis();
+            r.dispatchRealTime = SystemClock.elapsedRealtime();
             r.dispatchClockTime = System.currentTimeMillis();
 
             if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) {
@@ -1292,6 +1304,7 @@
                             performReceiveLocked(r.callerApp, r.resultTo,
                                     new Intent(r.intent), r.resultCode,
                                     r.resultData, r.resultExtras, false, false, r.userId);
+                            logBootCompletedBroadcastCompletionLatencyIfPossible(r);
                             // Set this to null so that the reference
                             // (local and remote) isn't kept in the mBroadcastHistory.
                             r.resultTo = null;
@@ -1408,6 +1421,7 @@
         r.receiverTime = SystemClock.uptimeMillis();
         if (recIdx == 0) {
             r.dispatchTime = r.receiverTime;
+            r.dispatchRealTime = SystemClock.elapsedRealtime();
             r.dispatchClockTime = System.currentTimeMillis();
 
             if (mLogLatencyMetrics) {
@@ -1866,6 +1880,59 @@
         return null;
     }
 
+    private void logBootCompletedBroadcastCompletionLatencyIfPossible(BroadcastRecord r) {
+        // Only log after last receiver.
+        // In case of split BOOT_COMPLETED broadcast, make sure only call this method on the
+        // last BroadcastRecord of the split broadcast which has non-null resultTo.
+        final int numReceivers = (r.receivers != null) ? r.receivers.size() : 0;
+        if (r.nextReceiver < numReceivers) {
+            return;
+        }
+        final String action = r.intent.getAction();
+        int event = 0;
+        if (Intent.ACTION_LOCKED_BOOT_COMPLETED.equals(action)) {
+            event = BOOT_COMPLETED_BROADCAST_COMPLETION_LATENCY_REPORTED__EVENT__LOCKED_BOOT_COMPLETED;
+        } else if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
+            event = BOOT_COMPLETED_BROADCAST_COMPLETION_LATENCY_REPORTED__EVENT__BOOT_COMPLETED;
+        }
+        if (event != 0) {
+            final int dispatchLatency = (int)(r.dispatchTime - r.enqueueTime);
+            final int completeLatency = (int)
+                    (SystemClock.uptimeMillis() - r.enqueueTime);
+            final int dispatchRealLatency = (int)(r.dispatchRealTime - r.enqueueRealTime);
+            final int completeRealLatency = (int)
+                    (SystemClock.elapsedRealtime() - r.enqueueRealTime);
+            int userType = FrameworkStatsLog.USER_LIFECYCLE_JOURNEY_REPORTED__USER_TYPE__TYPE_UNKNOWN;
+            // This method is called very infrequently, no performance issue we call
+            // LocalServices.getService() here.
+            final UserManagerInternal umInternal = LocalServices.getService(
+                    UserManagerInternal.class);
+            final UserInfo userInfo = umInternal.getUserInfo(r.userId);
+            if (userInfo != null) {
+                userType = UserManager.getUserTypeForStatsd(userInfo.userType);
+            }
+            Slog.i(TAG_BROADCAST,
+                    "BOOT_COMPLETED_BROADCAST_COMPLETION_LATENCY_REPORTED action:"
+                            + action
+                            + " dispatchLatency:" + dispatchLatency
+                            + " completeLatency:" + completeLatency
+                            + " dispatchRealLatency:" + dispatchRealLatency
+                            + " completeRealLatency:" + completeRealLatency
+                            + " receiversSize:" + r.receivers.size()
+                            + " userId:" + r.userId
+                            + " userType:" + (userInfo != null? userInfo.userType : null));
+            FrameworkStatsLog.write(
+                    BOOT_COMPLETED_BROADCAST_COMPLETION_LATENCY_REPORTED,
+                    event,
+                    dispatchLatency,
+                    completeLatency,
+                    dispatchRealLatency,
+                    completeRealLatency,
+                    r.userId,
+                    userType);
+        }
+    }
+
     private void maybeReportBroadcastDispatchedEventLocked(BroadcastRecord r, int targetUid) {
         // STOPSHIP (217251579): Temporarily use temp-allowlist reason to identify
         // push messages and record response events.
diff --git a/services/core/java/com/android/server/am/BroadcastRecord.java b/services/core/java/com/android/server/am/BroadcastRecord.java
index 8b1e829..2ee32b6 100644
--- a/services/core/java/com/android/server/am/BroadcastRecord.java
+++ b/services/core/java/com/android/server/am/BroadcastRecord.java
@@ -84,11 +84,14 @@
     boolean deferred;
     int splitCount;         // refcount for result callback, when split
     int splitToken;         // identifier for cross-BroadcastRecord refcount
+    long enqueueTime;       // uptimeMillis when the broadcast was enqueued
+    long enqueueRealTime;   // elapsedRealtime when the broadcast was enqueued
     long enqueueClockTime;  // the clock time the broadcast was enqueued
     long dispatchTime;      // when dispatch started on this set of receivers
+    long dispatchRealTime;  // elapsedRealtime when the broadcast was dispatched
     long dispatchClockTime; // the clock time the dispatch started
     long receiverTime;      // when current receiver started for timeouts.
-    long finishTime;        // when we finished the broadcast.
+    long finishTime;        // when we finished the current receiver.
     boolean timeoutExempt;  // true if this broadcast is not subject to receiver timeouts
     int resultCode;         // current result code value.
     String resultData;      // current result data value.
@@ -169,7 +172,7 @@
         pw.print(prefix); pw.print("dispatchTime=");
                 TimeUtils.formatDuration(dispatchTime, now, pw);
                 pw.print(" (");
-                TimeUtils.formatDuration(dispatchClockTime-enqueueClockTime, pw);
+                TimeUtils.formatDuration(dispatchTime - enqueueTime, pw);
                 pw.print(" since enq)");
         if (finishTime != 0) {
             pw.print(" finishTime="); TimeUtils.formatDuration(finishTime, now, pw);
@@ -324,8 +327,11 @@
         delivery = from.delivery;
         duration = from.duration;
         resultTo = from.resultTo;
+        enqueueTime = from.enqueueTime;
+        enqueueRealTime = from.enqueueRealTime;
         enqueueClockTime = from.enqueueClockTime;
         dispatchTime = from.dispatchTime;
+        dispatchRealTime = from.dispatchRealTime;
         dispatchClockTime = from.dispatchClockTime;
         receiverTime = from.receiverTime;
         finishTime = from.finishTime;
@@ -378,7 +384,9 @@
                 requiredPermissions, excludedPermissions, appOp, options, splitReceivers, resultTo,
                 resultCode, resultData, resultExtras, ordered, sticky, initialSticky, userId,
                 allowBackgroundActivityStarts, mBackgroundActivityStartsToken, timeoutExempt);
-
+        split.enqueueTime = this.enqueueTime;
+        split.enqueueRealTime = this.enqueueRealTime;
+        split.enqueueClockTime = this.enqueueClockTime;
         split.splitToken = this.splitToken;
         return split;
     }
@@ -448,9 +456,11 @@
             final BroadcastRecord br = new BroadcastRecord(queue, intent, callerApp, callerPackage,
                     callerFeatureId, callingPid, callingUid, callerInstantApp, resolvedType,
                     requiredPermissions, excludedPermissions, appOp, options,
-                    uid2receiverList.valueAt(i), resultTo,
+                    uid2receiverList.valueAt(i), null /* _resultTo */,
                     resultCode, resultData, resultExtras, ordered, sticky, initialSticky, userId,
                     allowBackgroundActivityStarts, mBackgroundActivityStartsToken, timeoutExempt);
+            br.enqueueTime = this.enqueueTime;
+            br.enqueueRealTime = this.enqueueRealTime;
             br.enqueueClockTime = this.enqueueClockTime;
             ret.put(uid2receiverList.keyAt(i), br);
         }
diff --git a/services/core/java/com/android/server/am/OomAdjuster.java b/services/core/java/com/android/server/am/OomAdjuster.java
index e0d333e..7ed3dcf 100644
--- a/services/core/java/com/android/server/am/OomAdjuster.java
+++ b/services/core/java/com/android/server/am/OomAdjuster.java
@@ -2561,7 +2561,7 @@
             if (app.getWaitingToKill() != null && app.mReceivers.numberOfCurReceivers() == 0
                     && state.getSetSchedGroup() == ProcessList.SCHED_GROUP_BACKGROUND) {
                 app.killLocked(app.getWaitingToKill(), ApplicationExitInfo.REASON_USER_REQUESTED,
-                        ApplicationExitInfo.SUBREASON_UNKNOWN, true);
+                        ApplicationExitInfo.SUBREASON_REMOVE_TASK, true);
                 success = false;
             } else {
                 int processGroup;
diff --git a/services/core/java/com/android/server/am/ProcessList.java b/services/core/java/com/android/server/am/ProcessList.java
index 48ca59d..253686c 100644
--- a/services/core/java/com/android/server/am/ProcessList.java
+++ b/services/core/java/com/android/server/am/ProcessList.java
@@ -1718,8 +1718,16 @@
             int runtimeFlags = 0;
 
             boolean debuggableFlag = (app.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
-            if (!debuggableFlag && app.isSdkSandbox) {
-                debuggableFlag = isAppForSdkSandboxDebuggable(app);
+            boolean isProfileableByShell = app.info.isProfileableByShell();
+            boolean isProfileable = app.info.isProfileable();
+
+            if (app.isSdkSandbox) {
+                ApplicationInfo clientInfo = app.getClientInfoForSdkSandbox();
+                if (clientInfo != null) {
+                    debuggableFlag |= (clientInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
+                    isProfileableByShell |= clientInfo.isProfileableByShell();
+                    isProfileable |= clientInfo.isProfileable();
+                }
             }
 
             if (debuggableFlag) {
@@ -1741,10 +1749,10 @@
             if ((app.info.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0 || mService.mSafeMode) {
                 runtimeFlags |= Zygote.DEBUG_ENABLE_SAFEMODE;
             }
-            if ((app.info.privateFlags & ApplicationInfo.PRIVATE_FLAG_PROFILEABLE_BY_SHELL) != 0) {
+            if (isProfileableByShell) {
                 runtimeFlags |= Zygote.PROFILE_FROM_SHELL;
             }
-            if (app.info.isProfileable()) {
+            if (isProfileable) {
                 runtimeFlags |= Zygote.PROFILEABLE;
             }
             if ("1".equals(SystemProperties.get("debug.checkjni"))) {
@@ -1812,7 +1820,7 @@
             }
 
             String invokeWith = null;
-            if ((app.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
+            if (debuggableFlag) {
                 // Debuggable apps may include a wrapper script with their library directory.
                 String wrapperFileName = app.info.nativeLibraryDir + "/wrap.sh";
                 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
@@ -1887,24 +1895,6 @@
         }
     }
 
-    /** Return true if the client app for the SDK sandbox process is debuggable. */
-    private boolean isAppForSdkSandboxDebuggable(ProcessRecord sandboxProcess) {
-        // TODO (b/221004701) use client app process name
-        final int appUid = Process.getAppUidForSdkSandboxUid(sandboxProcess.uid);
-        IPackageManager pm = mService.getPackageManager();
-        try {
-            String[] packages = pm.getPackagesForUid(appUid);
-            for (String aPackage : packages) {
-                ApplicationInfo i = pm.getApplicationInfo(aPackage, 0, sandboxProcess.userId);
-                if ((i.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
-                    return true;
-                }
-            }
-        } catch (RemoteException e) {
-            // shouldn't happen
-        }
-        return false;
-    }
 
     @GuardedBy("mService")
     boolean startProcessLocked(HostingRecord hostingRecord, String entryPoint, ProcessRecord app,
@@ -2365,7 +2355,7 @@
     ProcessRecord startProcessLocked(String processName, ApplicationInfo info,
             boolean knownToBeDead, int intentFlags, HostingRecord hostingRecord,
             int zygotePolicyFlags, boolean allowWhileBooting, boolean isolated, int isolatedUid,
-            boolean isSdkSandbox, int sdkSandboxUid,
+            boolean isSdkSandbox, int sdkSandboxUid, String sdkSandboxClientAppPackage,
             String abiOverride, String entryPoint, String[] entryPointArgs, Runnable crashHandler) {
         long startTime = SystemClock.uptimeMillis();
         ProcessRecord app;
@@ -2460,7 +2450,7 @@
         if (app == null) {
             checkSlow(startTime, "startProcess: creating new process record");
             app = newProcessRecordLocked(info, processName, isolated, isolatedUid, isSdkSandbox,
-                    sdkSandboxUid, hostingRecord);
+                    sdkSandboxUid, sdkSandboxClientAppPackage, hostingRecord);
             if (app == null) {
                 Slog.w(TAG, "Failed making new process record for "
                         + processName + "/" + info.uid + " isolated=" + isolated);
@@ -2956,7 +2946,7 @@
     @GuardedBy("mService")
     ProcessRecord newProcessRecordLocked(ApplicationInfo info, String customProcess,
             boolean isolated, int isolatedUid, boolean isSdkSandbox, int sdkSandboxUid,
-            HostingRecord hostingRecord) {
+            String sdkSandboxClientAppPackage, HostingRecord hostingRecord) {
         String proc = customProcess != null ? customProcess : info.processName;
         final int userId = UserHandle.getUserId(info.uid);
         int uid = info.uid;
@@ -2992,6 +2982,7 @@
                     FrameworkStatsLog.ISOLATED_UID_CHANGED__EVENT__CREATED);
         }
         final ProcessRecord r = new ProcessRecord(mService, info, proc, uid,
+                sdkSandboxClientAppPackage,
                 hostingRecord.getDefiningUid(), hostingRecord.getDefiningProcessName());
         final ProcessStateRecord state = r.mState;
 
diff --git a/services/core/java/com/android/server/am/ProcessRecord.java b/services/core/java/com/android/server/am/ProcessRecord.java
index f7cc3d7..b4ff870 100644
--- a/services/core/java/com/android/server/am/ProcessRecord.java
+++ b/services/core/java/com/android/server/am/ProcessRecord.java
@@ -27,6 +27,7 @@
 import android.app.ApplicationExitInfo.SubReason;
 import android.app.IApplicationThread;
 import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManagerInternal;
 import android.content.pm.ProcessInfo;
 import android.content.pm.VersionedPackage;
 import android.content.res.CompatibilityInfo;
@@ -84,6 +85,8 @@
     final int uid;              // uid of process; may be different from 'info' if isolated
     final int userId;           // user of process.
     final String processName;   // name of the process
+    final String sdkSandboxClientAppPackage; // if this is an sdk sandbox process, name of the
+                                             // app package for which it is running
 
     /**
      * Overall state of process's uid.
@@ -493,11 +496,12 @@
 
     ProcessRecord(ActivityManagerService _service, ApplicationInfo _info, String _processName,
             int _uid) {
-        this(_service, _info, _processName, _uid, -1, null);
+        this(_service, _info, _processName, _uid, null, -1, null);
     }
 
     ProcessRecord(ActivityManagerService _service, ApplicationInfo _info, String _processName,
-            int _uid, int _definingUid, String _definingProcessName) {
+            int _uid, String _sdkSandboxClientAppPackage, int _definingUid,
+            String _definingProcessName) {
         mService = _service;
         mProcLock = _service.mProcLock;
         info = _info;
@@ -530,6 +534,7 @@
         uid = _uid;
         userId = UserHandle.getUserId(_uid);
         processName = _processName;
+        sdkSandboxClientAppPackage = _sdkSandboxClientAppPackage;
         mPersistent = false;
         mRemoved = false;
         mProfile = new ProcessProfileRecord(this);
@@ -861,6 +866,29 @@
         return mDebugging;
     }
 
+    @Nullable
+    public ApplicationInfo getClientInfoForSdkSandbox() {
+        if (!isSdkSandbox || sdkSandboxClientAppPackage == null) {
+            throw new IllegalStateException(
+                    "getClientInfoForSdkSandbox called for non-sandbox process"
+            );
+        }
+        PackageManagerInternal pm = mService.getPackageManagerInternal();
+        return pm.getApplicationInfo(
+                sdkSandboxClientAppPackage, /* flags */0, Process.SYSTEM_UID, userId);
+    }
+
+    public boolean isDebuggable() {
+        if ((info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
+            return true;
+        }
+        if (isSdkSandbox) {
+            ApplicationInfo clientInfo = getClientInfoForSdkSandbox();
+            return clientInfo != null && (clientInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
+        }
+        return false;
+    }
+
     @GuardedBy("mService")
     void setDebugging(boolean debugging) {
         mDebugging = debugging;
diff --git a/services/core/java/com/android/server/am/ServiceRecord.java b/services/core/java/com/android/server/am/ServiceRecord.java
index c53d4d6..795311f 100644
--- a/services/core/java/com/android/server/am/ServiceRecord.java
+++ b/services/core/java/com/android/server/am/ServiceRecord.java
@@ -96,6 +96,8 @@
     final long createRealTime;  // when this service was created
     final boolean isSdkSandbox; // whether this is a sdk sandbox service
     final int sdkSandboxClientAppUid; // the app uid for which this sdk sandbox service is running
+    final String sdkSandboxClientAppPackage; // the app package for which this sdk sandbox service
+                                             // is running
     final ArrayMap<Intent.FilterComparison, IntentBindRecord> bindings
             = new ArrayMap<Intent.FilterComparison, IntentBindRecord>();
                             // All active bindings to the service.
@@ -573,13 +575,14 @@
             Intent.FilterComparison intent, ServiceInfo sInfo, boolean callerIsFg,
             Runnable restarter) {
         this(ams, name, instanceName, definingPackageName, definingUid, intent, sInfo, callerIsFg,
-                restarter, null, 0);
+                restarter, null, 0, null);
     }
 
     ServiceRecord(ActivityManagerService ams, ComponentName name,
             ComponentName instanceName, String definingPackageName, int definingUid,
             Intent.FilterComparison intent, ServiceInfo sInfo, boolean callerIsFg,
-            Runnable restarter, String sdkSandboxProcessName, int sdkSandboxClientAppUid) {
+            Runnable restarter, String sdkSandboxProcessName, int sdkSandboxClientAppUid,
+            String sdkSandboxClientAppPackage) {
         this.ams = ams;
         this.name = name;
         this.instanceName = instanceName;
@@ -590,8 +593,9 @@
         serviceInfo = sInfo;
         appInfo = sInfo.applicationInfo;
         packageName = sInfo.applicationInfo.packageName;
-        isSdkSandbox = sdkSandboxProcessName != null;
+        this.isSdkSandbox = sdkSandboxProcessName != null;
         this.sdkSandboxClientAppUid = sdkSandboxClientAppUid;
+        this.sdkSandboxClientAppPackage = sdkSandboxClientAppPackage;
         if ((sInfo.flags & ServiceInfo.FLAG_ISOLATED_PROCESS) != 0) {
             processName = sInfo.processName + ":" + instanceName.getClassName();
         } else if (sdkSandboxProcessName != null) {
diff --git a/services/core/java/com/android/server/am/UserController.java b/services/core/java/com/android/server/am/UserController.java
index ad17655..8795347 100644
--- a/services/core/java/com/android/server/am/UserController.java
+++ b/services/core/java/com/android/server/am/UserController.java
@@ -2618,7 +2618,7 @@
         if (getStartedUserState(userId) == null) {
             return false;
         }
-        if (!mInjector.getUserManager().isCredentialSharedWithParent(userId)) {
+        if (!mInjector.getUserManager().isCredentialSharableWithParent(userId)) {
             return false;
         }
         if (mLockPatternUtils.isSeparateProfileChallengeEnabled(userId)) {
diff --git a/services/core/java/com/android/server/ambientcontext/RemoteAmbientContextDetectionService.java b/services/core/java/com/android/server/ambientcontext/RemoteAmbientContextDetectionService.java
index f42080d..8aec752 100644
--- a/services/core/java/com/android/server/ambientcontext/RemoteAmbientContextDetectionService.java
+++ b/services/core/java/com/android/server/ambientcontext/RemoteAmbientContextDetectionService.java
@@ -49,6 +49,12 @@
         connect();
     }
 
+    @Override
+    protected long getAutoDisconnectTimeoutMs() {
+        // Disable automatic unbinding.
+        return -1;
+    }
+
     /**
      * Asks the implementation to start detection.
      *
diff --git a/services/core/java/com/android/server/app/GameServiceProviderInstanceFactoryImpl.java b/services/core/java/com/android/server/app/GameServiceProviderInstanceFactoryImpl.java
index b0a389d..a76eb8f 100644
--- a/services/core/java/com/android/server/app/GameServiceProviderInstanceFactoryImpl.java
+++ b/services/core/java/com/android/server/app/GameServiceProviderInstanceFactoryImpl.java
@@ -18,6 +18,7 @@
 
 import android.annotation.NonNull;
 import android.app.ActivityManager;
+import android.app.ActivityManagerInternal;
 import android.app.ActivityTaskManager;
 import android.content.Context;
 import android.content.Intent;
@@ -52,6 +53,7 @@
                 mContext,
                 new GameClassifierImpl(mContext.getPackageManager()),
                 ActivityManager.getService(),
+                LocalServices.getService(ActivityManagerInternal.class),
                 ActivityTaskManager.getService(),
                 (WindowManagerService) ServiceManager.getService(Context.WINDOW_SERVICE),
                 LocalServices.getService(WindowManagerInternal.class),
diff --git a/services/core/java/com/android/server/app/GameServiceProviderInstanceImpl.java b/services/core/java/com/android/server/app/GameServiceProviderInstanceImpl.java
index faf5c38..e920523 100644
--- a/services/core/java/com/android/server/app/GameServiceProviderInstanceImpl.java
+++ b/services/core/java/com/android/server/app/GameServiceProviderInstanceImpl.java
@@ -21,9 +21,11 @@
 import android.annotation.Nullable;
 import android.annotation.RequiresPermission;
 import android.app.ActivityManager.RunningTaskInfo;
+import android.app.ActivityManagerInternal;
 import android.app.ActivityTaskManager;
 import android.app.IActivityManager;
 import android.app.IActivityTaskManager;
+import android.app.IProcessObserver;
 import android.app.TaskStackListener;
 import android.content.ComponentName;
 import android.content.Context;
@@ -45,6 +47,7 @@
 import android.service.games.IGameSession;
 import android.service.games.IGameSessionController;
 import android.service.games.IGameSessionService;
+import android.text.TextUtils;
 import android.util.Slog;
 import android.view.SurfaceControl;
 import android.view.SurfaceControlViewHost.SurfacePackage;
@@ -143,12 +146,42 @@
                 GameServiceProviderInstanceImpl.this.onTaskFocusChanged(taskId, focused);
             });
         }
+    };
 
-        // TODO(b/204503192): Limit the lifespan of the game session in the Game Service provider
-        // to only when the associated task is running. Right now it is possible for a task to
-        // move into the background and for all associated processes to die and for the Game Session
-        // provider's GameSessionService to continue to be running. Ideally we could unbind the
-        // service when this happens.
+    /**
+     * The TaskStackListener declared above gives us good visibility into game task lifecycle.
+     * However, it is possible for the Android system to kill all the processes associated with a
+     * game task (e.g., when the system is under memory pressure or reaches a background process
+     * limit). When this happens, the game task remains (and no TaskStackListener callbacks are
+     * invoked), but we would nonetheless want to destroy a game session associated with the task
+     * if this were to happen.
+     *
+     * This process observer gives us visibility into process lifecycles and lets us track all the
+     * processes associated with each package so that any game sessions associated with the package
+     * are destroyed if the process count for a given package reaches zero (most packages will
+     * have at most one task). If processes for a given package are started up again, the destroyed
+     * game sessions will be re-created.
+     */
+    private final IProcessObserver mProcessObserver = new IProcessObserver.Stub() {
+        @Override
+        public void onForegroundActivitiesChanged(int pid, int uid, boolean fg) {
+            // This callback is used to track how many processes are running for a given package.
+            // Then, when a process dies, we will know if it was the only process running for that
+            // package and the associated game sessions should be destroyed.
+            mBackgroundExecutor.execute(() -> {
+                GameServiceProviderInstanceImpl.this.onForegroundActivitiesChanged(pid);
+            });
+        }
+
+        @Override
+        public void onProcessDied(int pid, int uid) {
+            mBackgroundExecutor.execute(() -> {
+                GameServiceProviderInstanceImpl.this.onProcessDied(pid);
+            });
+        }
+
+        @Override
+        public void onForegroundServicesChanged(int pid, int uid, int serviceTypes) {}
     };
 
     private final IGameServiceController mGameServiceController =
@@ -192,6 +225,7 @@
     private final Context mContext;
     private final GameClassifier mGameClassifier;
     private final IActivityManager mActivityManager;
+    private final ActivityManagerInternal mActivityManagerInternal;
     private final IActivityTaskManager mActivityTaskManager;
     private final WindowManagerService mWindowManagerService;
     private final WindowManagerInternal mWindowManagerInternal;
@@ -203,6 +237,12 @@
     private final ConcurrentHashMap<Integer, GameSessionRecord> mGameSessions =
             new ConcurrentHashMap<>();
     @GuardedBy("mLock")
+    private final ConcurrentHashMap<Integer, String> mPidToPackageMap = new ConcurrentHashMap<>();
+    @GuardedBy("mLock")
+    private final ConcurrentHashMap<String, Integer> mPackageNameToProcessCountMap =
+            new ConcurrentHashMap<>();
+
+    @GuardedBy("mLock")
     private volatile boolean mIsRunning;
 
     GameServiceProviderInstanceImpl(
@@ -211,6 +251,7 @@
             @NonNull Context context,
             @NonNull GameClassifier gameClassifier,
             @NonNull IActivityManager activityManager,
+            @NonNull ActivityManagerInternal activityManagerInternal,
             @NonNull IActivityTaskManager activityTaskManager,
             @NonNull WindowManagerService windowManagerService,
             @NonNull WindowManagerInternal windowManagerInternal,
@@ -222,6 +263,7 @@
         mContext = context;
         mGameClassifier = gameClassifier;
         mActivityManager = activityManager;
+        mActivityManagerInternal = activityManagerInternal;
         mActivityTaskManager = activityTaskManager;
         mWindowManagerService = windowManagerService;
         mWindowManagerInternal = windowManagerInternal;
@@ -263,6 +305,12 @@
             Slog.w(TAG, "Failed to register task stack listener", e);
         }
 
+        try {
+            mActivityManager.registerProcessObserver(mProcessObserver);
+        } catch (RemoteException e) {
+            Slog.w(TAG, "Failed to register process observer", e);
+        }
+
         mWindowManagerInternal.registerTaskSystemBarsListener(mTaskSystemBarsVisibilityListener);
     }
 
@@ -274,6 +322,12 @@
         mIsRunning = false;
 
         try {
+            mActivityManager.unregisterProcessObserver(mProcessObserver);
+        } catch (RemoteException e) {
+            Slog.w(TAG, "Failed to unregister process observer", e);
+        }
+
+        try {
             mActivityTaskManager.unregisterTaskStackListener(mTaskStackListener);
         } catch (RemoteException e) {
             Slog.w(TAG, "Failed to unregister task stack listener", e);
@@ -596,6 +650,126 @@
         }
     }
 
+    private void onForegroundActivitiesChanged(int pid) {
+        synchronized (mLock) {
+            onForegroundActivitiesChangedLocked(pid);
+        }
+    }
+
+    @GuardedBy("mLock")
+    private void onForegroundActivitiesChangedLocked(int pid) {
+        if (mPidToPackageMap.containsKey(pid)) {
+            // We are already tracking this pid, nothing to do.
+            return;
+        }
+
+        final String packageName = mActivityManagerInternal.getPackageNameByPid(pid);
+        if (TextUtils.isEmpty(packageName)) {
+            // Game processes should always have a package name.
+            return;
+        }
+
+        if (!gameSessionExistsForPackageNameLocked(packageName)) {
+            // We only need to track processes for tasks with game session records.
+            return;
+        }
+
+        mPidToPackageMap.put(pid, packageName);
+        final int processCountForPackage = mPackageNameToProcessCountMap.getOrDefault(packageName,
+                0) + 1;
+        mPackageNameToProcessCountMap.put(packageName, processCountForPackage);
+
+        if (DEBUG) {
+            Slog.d(TAG, "onForegroundActivitiesChangedLocked: tracking pid " + pid + ", for "
+                    + packageName + ". Process count for package: " + processCountForPackage);
+        }
+
+        // If there are processes for the package, we may need to re-create game sessions
+        // that are associated with the package
+        if (processCountForPackage > 0) {
+            recreateEndedGameSessionsLocked(packageName);
+        }
+    }
+
+    @GuardedBy("mLock")
+    private void recreateEndedGameSessionsLocked(String packageName) {
+        for (GameSessionRecord gameSessionRecord : mGameSessions.values()) {
+            if (gameSessionRecord.isGameSessionEndedForProcessDeath() && packageName.equals(
+                    gameSessionRecord.getComponentName().getPackageName())) {
+                if (DEBUG) {
+                    Slog.d(TAG,
+                            "recreateGameSessionsLocked(): re-creating game session for: "
+                                    + packageName + " with taskId: "
+                                    + gameSessionRecord.getTaskId());
+                }
+
+                final int taskId = gameSessionRecord.getTaskId();
+                mGameSessions.put(taskId, GameSessionRecord.awaitingGameSessionRequest(taskId,
+                        gameSessionRecord.getComponentName()));
+                createGameSessionLocked(gameSessionRecord.getTaskId());
+            }
+        }
+    }
+
+    private void onProcessDied(int pid) {
+        synchronized (mLock) {
+            onProcessDiedLocked(pid);
+        }
+    }
+
+    @GuardedBy("mLock")
+    private void onProcessDiedLocked(int pid) {
+        final String packageName = mPidToPackageMap.remove(pid);
+        if (packageName == null) {
+            // We weren't tracking this process.
+            return;
+        }
+
+        final Integer oldProcessCountForPackage = mPackageNameToProcessCountMap.get(packageName);
+        if (oldProcessCountForPackage == null) {
+            // This should never happen; we should have a process count for all tracked packages.
+            Slog.w(TAG, "onProcessDiedLocked(): Missing process count for package");
+            return;
+        }
+
+        final int processCountForPackage = oldProcessCountForPackage - 1;
+        mPackageNameToProcessCountMap.put(packageName, processCountForPackage);
+
+        // If there are no more processes for the game, then we will terminate any game sessions
+        // running for the package.
+        if (processCountForPackage <= 0) {
+            endGameSessionsForPackageLocked(packageName);
+        }
+    }
+
+    @GuardedBy("mLock")
+    private void endGameSessionsForPackageLocked(String packageName) {
+        for (GameSessionRecord gameSessionRecord : mGameSessions.values()) {
+            if (gameSessionRecord.getGameSession() != null && packageName.equals(
+                    gameSessionRecord.getComponentName().getPackageName())) {
+                if (DEBUG) {
+                    Slog.d(TAG, "endGameSessionsForPackageLocked(): No more processes for "
+                            + packageName + ", ending game session with taskId: "
+                            + gameSessionRecord.getTaskId());
+                }
+                mGameSessions.put(gameSessionRecord.getTaskId(),
+                        gameSessionRecord.withGameSessionEndedOnProcessDeath());
+                destroyGameSessionFromRecordLocked(gameSessionRecord);
+            }
+        }
+    }
+
+    @GuardedBy("mLock")
+    private boolean gameSessionExistsForPackageNameLocked(String packageName) {
+        for (GameSessionRecord gameSessionRecord : mGameSessions.values()) {
+            if (packageName.equals(gameSessionRecord.getComponentName().getPackageName())) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
     @Nullable
     private GameSessionViewHostConfiguration createViewHostConfigurationForTask(int taskId) {
         RunningTaskInfo runningTaskInfo = getRunningTaskInfoForTask(taskId);
diff --git a/services/core/java/com/android/server/app/GameSessionRecord.java b/services/core/java/com/android/server/app/GameSessionRecord.java
index a241812..74e538e 100644
--- a/services/core/java/com/android/server/app/GameSessionRecord.java
+++ b/services/core/java/com/android/server/app/GameSessionRecord.java
@@ -35,6 +35,10 @@
         // A Game Session is created and attached.
         // GameSessionRecord.getGameSession() != null.
         GAME_SESSION_ATTACHED,
+        // A Game Session did exist for a given game task but was destroyed because the last process
+        // for the game died.
+        // GameSessionRecord.getGameSession() == null.
+        GAME_SESSION_ENDED_PROCESS_DEATH,
     }
 
     private final int mTaskId;
@@ -99,6 +103,20 @@
     }
 
     @NonNull
+    public GameSessionRecord withGameSessionEndedOnProcessDeath() {
+        return new GameSessionRecord(
+                mTaskId,
+                State.GAME_SESSION_ENDED_PROCESS_DEATH,
+                mRootComponentName,
+                /* gameSession=*/ null,
+                /* surfacePackage=*/ null);
+    }
+
+    public boolean isGameSessionEndedForProcessDeath() {
+        return mState == State.GAME_SESSION_ENDED_PROCESS_DEATH;
+    }
+
+    @NonNull
     public int getTaskId() {
         return mTaskId;
     }
diff --git a/services/core/java/com/android/server/audio/SpatializerHelper.java b/services/core/java/com/android/server/audio/SpatializerHelper.java
index 193cc5f..5af73c9 100644
--- a/services/core/java/com/android/server/audio/SpatializerHelper.java
+++ b/services/core/java/com/android/server/audio/SpatializerHelper.java
@@ -275,6 +275,7 @@
      */
     synchronized void reset(boolean featureEnabled) {
         Log.i(TAG, "Resetting");
+        releaseSpat();
         mState = STATE_UNINITIALIZED;
         mSpatLevel = Spatializer.SPATIALIZER_IMMERSIVE_LEVEL_NONE;
         mCapableSpatLevel = Spatializer.SPATIALIZER_IMMERSIVE_LEVEL_NONE;
@@ -831,10 +832,10 @@
             try {
                 mSpat.registerHeadTrackingCallback(null);
                 mSpat.release();
-                mSpat = null;
             } catch (RemoteException e) {
                 Log.e(TAG, "Can't set release spatializer cleanly", e);
             }
+            mSpat = null;
         }
     }
 
diff --git a/services/core/java/com/android/server/biometrics/sensors/face/aidl/FaceAuthenticationClient.java b/services/core/java/com/android/server/biometrics/sensors/face/aidl/FaceAuthenticationClient.java
index 7765ab3..9ae6750 100644
--- a/services/core/java/com/android/server/biometrics/sensors/face/aidl/FaceAuthenticationClient.java
+++ b/services/core/java/com/android/server/biometrics/sensors/face/aidl/FaceAuthenticationClient.java
@@ -136,8 +136,8 @@
         try {
             if (mSensorPrivacyManager != null
                     && mSensorPrivacyManager
-                    .isSensorPrivacyEnabled(SensorPrivacyManager.Sensors.CAMERA,
-                    getTargetUserId())) {
+                    .isSensorPrivacyEnabled(SensorPrivacyManager.TOGGLE_TYPE_SOFTWARE,
+                    SensorPrivacyManager.Sensors.CAMERA)) {
                 onError(BiometricConstants.BIOMETRIC_ERROR_HW_UNAVAILABLE,
                         0 /* vendorCode */);
                 mCallback.onClientFinished(this, false /* success */);
diff --git a/services/core/java/com/android/server/biometrics/sensors/face/aidl/FaceDetectClient.java b/services/core/java/com/android/server/biometrics/sensors/face/aidl/FaceDetectClient.java
index efedcf8..ded1810 100644
--- a/services/core/java/com/android/server/biometrics/sensors/face/aidl/FaceDetectClient.java
+++ b/services/core/java/com/android/server/biometrics/sensors/face/aidl/FaceDetectClient.java
@@ -96,7 +96,8 @@
     protected void startHalOperation() {
         if (mSensorPrivacyManager != null
                 && mSensorPrivacyManager
-                .isSensorPrivacyEnabled(SensorPrivacyManager.Sensors.CAMERA, getTargetUserId())) {
+                .isSensorPrivacyEnabled(SensorPrivacyManager.TOGGLE_TYPE_SOFTWARE,
+                    SensorPrivacyManager.Sensors.CAMERA)) {
             onError(BiometricConstants.BIOMETRIC_ERROR_HW_UNAVAILABLE, 0 /* vendorCode */);
             mCallback.onClientFinished(this, false /* success */);
             return;
diff --git a/services/core/java/com/android/server/biometrics/sensors/face/hidl/FaceAuthenticationClient.java b/services/core/java/com/android/server/biometrics/sensors/face/hidl/FaceAuthenticationClient.java
index 8d76e9f..1935a5b 100644
--- a/services/core/java/com/android/server/biometrics/sensors/face/hidl/FaceAuthenticationClient.java
+++ b/services/core/java/com/android/server/biometrics/sensors/face/hidl/FaceAuthenticationClient.java
@@ -109,7 +109,8 @@
 
         if (mSensorPrivacyManager != null
                 && mSensorPrivacyManager
-                .isSensorPrivacyEnabled(SensorPrivacyManager.Sensors.CAMERA, getTargetUserId())) {
+                .isSensorPrivacyEnabled(SensorPrivacyManager.TOGGLE_TYPE_SOFTWARE,
+                SensorPrivacyManager.Sensors.CAMERA)) {
             onError(BiometricFaceConstants.FACE_ERROR_HW_UNAVAILABLE, 0 /* vendorCode */);
             mCallback.onClientFinished(this, false /* success */);
             return;
diff --git a/services/core/java/com/android/server/camera/CameraServiceProxy.java b/services/core/java/com/android/server/camera/CameraServiceProxy.java
index 01f0cb6..7db99f1 100644
--- a/services/core/java/com/android/server/camera/CameraServiceProxy.java
+++ b/services/core/java/com/android/server/camera/CameraServiceProxy.java
@@ -184,6 +184,9 @@
     // Must be equal to number of CameraStreamProto in CameraActionEvent
     private static final int MAX_STREAM_STATISTICS = 5;
 
+    private static final float MIN_PREVIEW_FPS = 30.0f;
+    private static final float MAX_PREVIEW_FPS = 60.0f;
+
     private final Context mContext;
     private final ServiceThread mHandlerThread;
     private final Handler mHandler;
@@ -821,6 +824,7 @@
                         Slog.v(TAG, "Stream " + i + ": width " + streamProtos[i].width
                                 + ", height " + streamProtos[i].height
                                 + ", format " + streamProtos[i].format
+                                + ", maxPreviewFps " + streamStats.getMaxPreviewFps()
                                 + ", dataSpace " + streamProtos[i].dataSpace
                                 + ", usage " + streamProtos[i].usage
                                 + ", requestCount " + streamProtos[i].requestCount
@@ -1015,6 +1019,11 @@
         return false;
     }
 
+    private float getMinFps(CameraSessionStats cameraState) {
+        float maxFps = cameraState.getMaxPreviewFps();
+        return Math.max(Math.min(maxFps, MAX_PREVIEW_FPS), MIN_PREVIEW_FPS);
+    }
+
     private void updateActivityCount(CameraSessionStats cameraState) {
         String cameraId = cameraState.getCameraId();
         int newCameraState = cameraState.getNewCameraState();
@@ -1068,9 +1077,9 @@
                     if (!alreadyActivePackage) {
                         WindowManagerInternal wmi =
                                 LocalServices.getService(WindowManagerInternal.class);
-                        // TODO(b/209669709): populate min.max refreshRate based on
-                        //  the camera capture speed
-                        wmi.addRefreshRateRangeForPackage(clientName, 60.0f, 60.0f);
+                        float minFps = getMinFps(cameraState);
+                        wmi.addRefreshRateRangeForPackage(clientName,
+                                minFps, MAX_PREVIEW_FPS);
                     }
 
                     // Update activity events
diff --git a/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java b/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java
index 1f9d080..77dcbd3 100644
--- a/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java
+++ b/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java
@@ -1757,6 +1757,15 @@
         }
     }
 
+    @NonNull
+    private static PackageManager getPackageManagerForUser(@NonNull Context context,
+            @UserIdInt int userId) {
+        return context.getUserId() == userId
+                ? context.getPackageManager()
+                : context.createContextAsUser(UserHandle.of(userId), 0 /* flags */)
+                        .getPackageManager();
+    }
+
     @GuardedBy("ImfLock.class")
     private void switchUserOnHandlerLocked(@UserIdInt int newUserId,
             IInputMethodClient clientToBeReset) {
@@ -1801,9 +1810,9 @@
         updateFromSettingsLocked(true);
 
         if (initialUserSwitch) {
-            InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
-                    mSettings.getEnabledInputMethodListLocked(), newUserId,
-                    mContext.getBasePackageName());
+            InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(
+                    getPackageManagerForUser(mContext, newUserId),
+                    mSettings.getEnabledInputMethodListLocked());
         }
 
         if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
@@ -1892,9 +1901,9 @@
                 final boolean imeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
                 buildInputMethodListLocked(!imeSelectedOnBoot /* resetDefaultEnabledIme */);
                 updateFromSettingsLocked(true);
-                InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
-                        mSettings.getEnabledInputMethodListLocked(), currentUserId,
-                        mContext.getBasePackageName());
+                InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(
+                        getPackageManagerForUser(mContext, currentUserId),
+                        mSettings.getEnabledInputMethodListLocked());
             }
         }
     }
@@ -6122,10 +6131,9 @@
                         setInputMethodEnabledLocked(imi.getId(), true);
                     }
                     updateInputMethodsFromSettingsLocked(true /* enabledMayChange */);
-                    InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
-                            mSettings.getEnabledInputMethodListLocked(),
-                            mSettings.getCurrentUserId(),
-                            mContext.getBasePackageName());
+                    InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(
+                            getPackageManagerForUser(mContext, mSettings.getCurrentUserId()),
+                            mSettings.getEnabledInputMethodListLocked());
                     nextIme = mSettings.getSelectedInputMethod();
                     nextEnabledImes = mSettings.getEnabledInputMethodListLocked();
                 } else {
diff --git a/services/core/java/com/android/server/inputmethod/InputMethodUtils.java b/services/core/java/com/android/server/inputmethod/InputMethodUtils.java
index e619fff..4633df2 100644
--- a/services/core/java/com/android/server/inputmethod/InputMethodUtils.java
+++ b/services/core/java/com/android/server/inputmethod/InputMethodUtils.java
@@ -18,17 +18,16 @@
 
 import android.annotation.NonNull;
 import android.annotation.Nullable;
+import android.annotation.UserHandleAware;
 import android.annotation.UserIdInt;
 import android.app.AppOpsManager;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.pm.ApplicationInfo;
-import android.content.pm.IPackageManager;
 import android.content.pm.PackageManager;
 import android.content.res.Resources;
 import android.os.Build;
 import android.os.LocaleList;
-import android.os.RemoteException;
 import android.os.UserHandle;
 import android.provider.Settings;
 import android.text.TextUtils;
@@ -663,8 +662,9 @@
         return !subtype.isAuxiliary();
     }
 
-    static void setNonSelectedSystemImesDisabledUntilUsed(IPackageManager packageManager,
-            List<InputMethodInfo> enabledImis, @UserIdInt int userId, String callingPackage) {
+    @UserHandleAware
+    static void setNonSelectedSystemImesDisabledUntilUsed(PackageManager packageManagerForUser,
+            List<InputMethodInfo> enabledImis) {
         if (DEBUG) {
             Slog.d(TAG, "setNonSelectedSystemImesDisabledUntilUsed");
         }
@@ -675,7 +675,8 @@
         }
         // Only the current spell checker should be treated as an enabled one.
         final SpellCheckerInfo currentSpellChecker =
-                TextServicesManagerInternal.get().getCurrentSpellCheckerForUser(userId);
+                TextServicesManagerInternal.get().getCurrentSpellCheckerForUser(
+                        packageManagerForUser.getUserId());
         for (final String packageName : systemImesDisabledUntilUsed) {
             if (DEBUG) {
                 Slog.d(TAG, "check " + packageName);
@@ -702,11 +703,12 @@
             }
             ApplicationInfo ai = null;
             try {
-                ai = packageManager.getApplicationInfo(packageName,
-                        PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS, userId);
-            } catch (RemoteException e) {
+                ai = packageManagerForUser.getApplicationInfo(packageName,
+                        PackageManager.ApplicationInfoFlags.of(
+                                PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS));
+            } catch (PackageManager.NameNotFoundException e) {
                 Slog.w(TAG, "getApplicationInfo failed. packageName=" + packageName
-                        + " userId=" + userId, e);
+                        + " userId=" + packageManagerForUser.getUserId(), e);
                 continue;
             }
             if (ai == null) {
@@ -717,18 +719,18 @@
             if (!isSystemPackage) {
                 continue;
             }
-            setDisabledUntilUsed(packageManager, packageName, userId, callingPackage);
+            setDisabledUntilUsed(packageManagerForUser, packageName);
         }
     }
 
-    private static void setDisabledUntilUsed(IPackageManager packageManager, String packageName,
-            int userId, String callingPackage) {
+    private static void setDisabledUntilUsed(PackageManager packageManagerForUser,
+            String packageName) {
         final int state;
         try {
-            state = packageManager.getApplicationEnabledSetting(packageName, userId);
-        } catch (RemoteException e) {
+            state = packageManagerForUser.getApplicationEnabledSetting(packageName);
+        } catch (IllegalArgumentException e) {
             Slog.w(TAG, "getApplicationEnabledSetting failed. packageName=" + packageName
-                    + " userId=" + userId, e);
+                    + " userId=" + packageManagerForUser.getUserId(), e);
             return;
         }
         if (state == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT
@@ -737,12 +739,12 @@
                 Slog.d(TAG, "Update state(" + packageName + "): DISABLED_UNTIL_USED");
             }
             try {
-                packageManager.setApplicationEnabledSetting(packageName,
+                packageManagerForUser.setApplicationEnabledSetting(packageName,
                         PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED,
-                        0 /* newState */, userId, callingPackage);
-            } catch (RemoteException e) {
+                        0 /* newState */);
+            } catch (IllegalArgumentException e) {
                 Slog.w(TAG, "setApplicationEnabledSetting failed. packageName=" + packageName
-                        + " userId=" + userId + " callingPackage=" + callingPackage, e);
+                        + " userId=" + packageManagerForUser.getUserId(), e);
                 return;
             }
         } else {
diff --git a/services/core/java/com/android/server/location/gnss/GnssLocationProvider.java b/services/core/java/com/android/server/location/gnss/GnssLocationProvider.java
index cd2ba39..8407a11 100644
--- a/services/core/java/com/android/server/location/gnss/GnssLocationProvider.java
+++ b/services/core/java/com/android/server/location/gnss/GnssLocationProvider.java
@@ -1184,6 +1184,7 @@
                 mAlarmManager.cancel(mBatchingAlarm);
                 mBatchingAlarm = null;
             }
+            mGnssNative.flushBatch();
             mGnssNative.stopBatch();
             mBatchingStarted = false;
         }
diff --git a/services/core/java/com/android/server/locksettings/LockSettingsService.java b/services/core/java/com/android/server/locksettings/LockSettingsService.java
index 0f4648a..436cc69 100644
--- a/services/core/java/com/android/server/locksettings/LockSettingsService.java
+++ b/services/core/java/com/android/server/locksettings/LockSettingsService.java
@@ -252,8 +252,6 @@
 
     private final RebootEscrowManager mRebootEscrowManager;
 
-    private boolean mFirstCallToVold;
-
     // Current password metric for all users on the device. Updated when user unlocks
     // the device or changes password. Removed when user is stopped.
     @GuardedBy("this")
@@ -373,7 +371,7 @@
             LockscreenCredential profileUserPassword) {
         if (DEBUG) Slog.v(TAG, "Check child profile lock for user: " + profileUserId);
         // Only for profiles that shares credential with parent
-        if (!isCredentialSharedWithParent(profileUserId)) {
+        if (!isCredentialSharableWithParent(profileUserId)) {
             return;
         }
         // Do not tie profile when work challenge is enabled
@@ -597,8 +595,6 @@
         mStrongAuth = injector.getStrongAuth();
         mActivityManager = injector.getActivityManager();
 
-        mFirstCallToVold = true;
-
         IntentFilter filter = new IntentFilter();
         filter.addAction(Intent.ACTION_USER_ADDED);
         filter.addAction(Intent.ACTION_USER_STARTING);
@@ -789,7 +785,7 @@
     private void ensureProfileKeystoreUnlocked(int userId) {
         final KeyStore ks = KeyStore.getInstance();
         if (ks.state(userId) == KeyStore.State.LOCKED
-                && isCredentialSharedWithParent(userId)
+                && isCredentialSharableWithParent(userId)
                 && hasUnifiedChallenge(userId)) {
             Slog.i(TAG, "Profile got unlocked, will unlock its keystore");
             // If boot took too long and the password in vold got expired, parent keystore will
@@ -810,7 +806,7 @@
                 // Hide notification first, as tie managed profile lock takes time
                 hideEncryptionNotification(new UserHandle(userId));
 
-                if (isCredentialSharedWithParent(userId)) {
+                if (isCredentialSharableWithParent(userId)) {
                     tieProfileLockIfNecessary(userId, LockscreenCredential.createNone());
                 }
 
@@ -1077,7 +1073,7 @@
         final int userCount = users.size();
         for (int i = 0; i < userCount; i++) {
             UserInfo user = users.get(i);
-            if (isCredentialSharedWithParent(user.id)
+            if (isCredentialSharableWithParent(user.id)
                     && !getSeparateProfileChallengeEnabledInternal(user.id)) {
                 success &= SyntheticPasswordCrypto.migrateLockSettingsKey(
                         PROFILE_KEY_NAME_ENCRYPT + user.id);
@@ -1471,7 +1467,7 @@
             Thread.currentThread().interrupt();
         }
 
-        if (isCredentialSharedWithParent(userId)) {
+        if (isCredentialSharableWithParent(userId)) {
             if (!hasUnifiedChallenge(userId)) {
                 mBiometricDeferredQueue.processPendingLockoutResets();
             }
@@ -1480,7 +1476,7 @@
 
         for (UserInfo profile : mUserManager.getProfiles(userId)) {
             if (profile.id == userId) continue;
-            if (!isCredentialSharedWithParent(profile.id)) continue;
+            if (!isCredentialSharableWithParent(profile.id)) continue;
 
             if (hasUnifiedChallenge(profile.id)) {
                 if (mUserManager.isUserRunning(profile.id)) {
@@ -1517,7 +1513,7 @@
     }
 
     private Map<Integer, LockscreenCredential> getDecryptedPasswordsForAllTiedProfiles(int userId) {
-        if (isCredentialSharedWithParent(userId)) {
+        if (isCredentialSharableWithParent(userId)) {
             return null;
         }
         Map<Integer, LockscreenCredential> result = new ArrayMap<>();
@@ -1525,7 +1521,7 @@
         final int size = profiles.size();
         for (int i = 0; i < size; i++) {
             final UserInfo profile = profiles.get(i);
-            if (!isCredentialSharedWithParent(profile.id)) {
+            if (!isCredentialSharableWithParent(profile.id)) {
                 continue;
             }
             final int profileUserId = profile.id;
@@ -1560,7 +1556,7 @@
      */
     private void synchronizeUnifiedWorkChallengeForProfiles(int userId,
             Map<Integer, LockscreenCredential> profilePasswordMap) {
-        if (isCredentialSharedWithParent(userId)) {
+        if (isCredentialSharableWithParent(userId)) {
             return;
         }
         final boolean isSecure = isUserSecure(userId);
@@ -1569,7 +1565,7 @@
         for (int i = 0; i < size; i++) {
             final UserInfo profile = profiles.get(i);
             final int profileUserId = profile.id;
-            if (isCredentialSharedWithParent(profileUserId)) {
+            if (isCredentialSharableWithParent(profileUserId)) {
                 if (getSeparateProfileChallengeEnabledInternal(profileUserId)) {
                     continue;
                 }
@@ -1596,12 +1592,12 @@
     }
 
     private boolean isProfileWithUnifiedLock(int userId) {
-        return isCredentialSharedWithParent(userId)
+        return isCredentialSharableWithParent(userId)
                 && !getSeparateProfileChallengeEnabledInternal(userId);
     }
 
     private boolean isProfileWithSeparatedLock(int userId) {
-        return isCredentialSharedWithParent(userId)
+        return isCredentialSharableWithParent(userId)
                 && getSeparateProfileChallengeEnabledInternal(userId);
     }
 
@@ -1719,7 +1715,7 @@
                 setSeparateProfileChallengeEnabledLocked(userId, true, /* unused */ null);
                 notifyPasswordChanged(credential, userId);
             }
-            if (isCredentialSharedWithParent(userId)) {
+            if (isCredentialSharableWithParent(userId)) {
                 // Make sure the profile doesn't get locked straight after setting work challenge.
                 setDeviceUnlockedForUser(userId);
             }
@@ -1734,7 +1730,7 @@
 
     /**
      * @param savedCredential if the user is a profile with
-     * {@link UserManager#isCredentialSharedWithParent()} with unified challenge and
+     * {@link UserManager#isCredentialSharableWithParent()} with unified challenge and
      *   savedCredential is empty, LSS will try to re-derive the profile password internally.
      *     TODO (b/80170828): Fix this so profile password is always passed in.
      * @param isLockTiedToParent is {@code true} if {@code userId} is a profile and its new
@@ -1800,7 +1796,10 @@
     }
 
     private void onPostPasswordChanged(LockscreenCredential newCredential, int userHandle) {
-        updateEncryptionPasswordIfNeeded(newCredential, userHandle);
+        if (userHandle == UserHandle.USER_SYSTEM && isDeviceEncryptionEnabled() &&
+            shouldEncryptWithCredentials() && newCredential.isNone()) {
+            setCredentialRequiredToDecrypt(false);
+        }
         if (newCredential.isPattern()) {
             setBoolean(LockPatternUtils.PATTERN_EVER_CHOSEN_KEY, true, userHandle);
         }
@@ -1809,26 +1808,6 @@
     }
 
     /**
-     * Update device encryption password if calling user is USER_SYSTEM and device supports
-     * encryption.
-     */
-    private void updateEncryptionPasswordIfNeeded(LockscreenCredential credential, int userHandle) {
-        // Update the device encryption password.
-        if (userHandle != UserHandle.USER_SYSTEM || !isDeviceEncryptionEnabled()) {
-            return;
-        }
-        if (!shouldEncryptWithCredentials()) {
-            updateEncryptionPassword(StorageManager.CRYPT_TYPE_DEFAULT, null);
-            return;
-        }
-        if (credential.isNone()) {
-            // Set the encryption password to default.
-            setCredentialRequiredToDecrypt(false);
-        }
-        updateEncryptionPassword(credential.getStorageCryptType(), credential.getCredential());
-    }
-
-    /**
      * Store the hash of the *current* password in the password history list, if device policy
      * enforces password history requirement.
      */
@@ -1927,8 +1906,8 @@
         }
     }
 
-    protected boolean isCredentialSharedWithParent(int userId) {
-        return getUserManagerFromCache(userId).isCredentialSharedWithParent();
+    protected boolean isCredentialSharableWithParent(int userId) {
+        return getUserManagerFromCache(userId).isCredentialSharableWithParent();
     }
 
     private VerifyCredentialResponse convertResponse(GateKeeperResponse gateKeeperResponse) {
@@ -1942,34 +1921,6 @@
         }
     }
 
-    /** Update the encryption password if it is enabled **/
-    @Override
-    public void updateEncryptionPassword(final int type, final byte[] password) {
-        if (!hasSecureLockScreen() && password != null && password.length != 0) {
-            throw new UnsupportedOperationException(
-                    "This operation requires the lock screen feature.");
-        }
-        if (!isDeviceEncryptionEnabled()) {
-            return;
-        }
-        final IBinder service = ServiceManager.getService("mount");
-        if (service == null) {
-            Slog.e(TAG, "Could not find the mount service to update the encryption password");
-            return;
-        }
-
-        // TODO(b/120484642): This is a location where we still use a String for vold
-        String passwordString = password != null ? new String(password) : null;
-        mHandler.post(() -> {
-            IStorageManager storageManager = mInjector.getStorageManager();
-            try {
-                storageManager.changeEncryptionPassword(type, passwordString);
-            } catch (RemoteException e) {
-                Slog.e(TAG, "Error changing encryption password", e);
-            }
-        });
-    }
-
     /** Register the given WeakEscrowTokenRemovedListener. */
     @Override
     public boolean registerWeakEscrowTokenRemovedListener(
@@ -2206,7 +2157,7 @@
         final List<UserInfo> profiles = mUserManager.getProfiles(userId);
         for (UserInfo pi : profiles) {
             // Unlock profile which shares credential with parent with unified lock
-            if (isCredentialSharedWithParent(pi.id)
+            if (isCredentialSharableWithParent(pi.id)
                     && !getSeparateProfileChallengeEnabledInternal(pi.id)
                     && mStorage.hasChildProfileLock(pi.id)) {
                 try {
@@ -2519,77 +2470,6 @@
         });
     }
 
-    private LockscreenCredential createPattern(String patternString) {
-        final byte[] patternBytes = patternString.getBytes();
-        LockscreenCredential pattern = LockscreenCredential.createPattern(
-                LockPatternUtils.byteArrayToPattern(patternBytes));
-        Arrays.fill(patternBytes, (byte) 0);
-        return pattern;
-    }
-
-    @Override
-    public boolean checkVoldPassword(int userId) {
-        if (!mFirstCallToVold) {
-            return false;
-        }
-        mFirstCallToVold = false;
-
-        checkPasswordReadPermission();
-
-        // There's no guarantee that this will safely connect, but if it fails
-        // we will simply show the lock screen when we shouldn't, so relatively
-        // benign. There is an outside chance something nasty would happen if
-        // this service restarted before vold stales out the password in this
-        // case. The nastiness is limited to not showing the lock screen when
-        // we should, within the first minute of decrypting the phone if this
-        // service can't connect to vold, it restarts, and then the new instance
-        // does successfully connect.
-        final IStorageManager service = mInjector.getStorageManager();
-        // TODO(b/120484642): Update vold to return a password as a byte array
-        String password;
-        final long identity = Binder.clearCallingIdentity();
-        try {
-            password = service.getPassword();
-            service.clearPassword();
-        } catch (RemoteException e) {
-            Slog.w(TAG, "vold getPassword() failed", e);
-            return false;
-        } finally {
-            Binder.restoreCallingIdentity(identity);
-        }
-        if (TextUtils.isEmpty(password)) {
-            return false;
-        }
-
-        try {
-            final LockscreenCredential credential;
-            switch (getCredentialTypeInternal(userId)) {
-                case CREDENTIAL_TYPE_PATTERN:
-                    credential = createPattern(password);
-                    break;
-                case CREDENTIAL_TYPE_PIN:
-                    credential = LockscreenCredential.createPin(password);
-                    break;
-                case CREDENTIAL_TYPE_PASSWORD:
-                    credential = LockscreenCredential.createPassword(password);
-                    break;
-                default:
-                    credential = null;
-                    Slog.e(TAG, "Unknown credential type");
-            }
-
-            if (credential != null
-                    && checkCredential(credential, userId, null /* progressCallback */)
-                                .getResponseCode() == GateKeeperResponse.RESPONSE_OK) {
-                return true;
-            }
-        } catch (Exception e) {
-            Slog.e(TAG, "checkVoldPassword failed: ", e);
-        }
-
-        return false;
-    }
-
     private void removeUser(int userId, boolean unknownUser) {
         Slog.i(TAG, "RemoveUser: " + userId);
         removeBiometricsForUser(userId);
@@ -2600,7 +2480,7 @@
         mManagedProfilePasswordCache.removePassword(userId);
 
         gateKeeperClearSecureUserId(userId);
-        if (unknownUser || isCredentialSharedWithParent(userId)) {
+        if (unknownUser || isCredentialSharableWithParent(userId)) {
             removeKeystoreProfileKey(userId);
         }
         // Clean up storage last, this is to ensure that cleanupDataForReusedUserIdIfNecessary()
@@ -3320,7 +3200,7 @@
      * Returns a fixed pseudorandom byte string derived from the user's synthetic password.
      * This is used to salt the password history hash to protect the hash against offline
      * bruteforcing, since rederiving this value requires a successful authentication.
-     * If user is a profile with {@link UserManager#isCredentialSharedWithParent()} true and with
+     * If user is a profile with {@link UserManager#isCredentialSharableWithParent()} true and with
      * unified challenge, currentCredential is ignored.
      */
     @Override
diff --git a/services/core/java/com/android/server/logcat/LogAccessDialogActivity.java b/services/core/java/com/android/server/logcat/LogAccessDialogActivity.java
index 7116ca3..8be90e0c 100644
--- a/services/core/java/com/android/server/logcat/LogAccessDialogActivity.java
+++ b/services/core/java/com/android/server/logcat/LogAccessDialogActivity.java
@@ -22,6 +22,7 @@
 import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.content.pm.PackageManager.NameNotFoundException;
+import android.os.Build;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.RemoteException;
@@ -56,7 +57,7 @@
     private AlertDialog.Builder mAlertDialog;
     private AlertDialog mAlert;
 
-    private static final int DIALOG_TIME_OUT = 300000;
+    private static final int DIALOG_TIME_OUT = Build.IS_DEBUGGABLE ? 60000 : 300000;
     private static final int MSG_DISMISS_DIALOG = 0;
 
 
diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java
index 265ad7d..1885b55 100755
--- a/services/core/java/com/android/server/notification/NotificationManagerService.java
+++ b/services/core/java/com/android/server/notification/NotificationManagerService.java
@@ -630,6 +630,7 @@
     private int mWarnRemoteViewsSizeBytes;
     private int mStripRemoteViewsSizeBytes;
     final boolean mEnableAppSettingMigration;
+    private boolean mForceUserSetOnUpgrade;
 
     private MetricsLogger mMetricsLogger;
     private TriPredicate<String, Integer, String> mAllowedManagedServicePackages;
@@ -2294,6 +2295,7 @@
 
         mMsgPkgsAllowedAsConvos = Set.of(getStringArrayResource(
                 com.android.internal.R.array.config_notificationMsgPkgsAllowedAsConvos));
+
         mStatsManager = statsManager;
 
         mToastRateLimiter = toastRateLimiter;
@@ -2386,6 +2388,9 @@
 
         WorkerHandler handler = new WorkerHandler(Looper.myLooper());
 
+        mForceUserSetOnUpgrade = getContext().getResources().getBoolean(
+                R.bool.config_notificationForceUserSetOnUpgrade);
+
         init(handler, new RankingHandlerWorker(mRankingThread.getLooper()),
                 AppGlobals.getPackageManager(), getContext().getPackageManager(),
                 getLocalService(LightsManager.class),
@@ -2414,7 +2419,8 @@
                 LocalServices.getService(ActivityManagerInternal.class),
                 createToastRateLimiter(), new PermissionHelper(LocalServices.getService(
                         PermissionManagerServiceInternal.class), AppGlobals.getPackageManager(),
-                        AppGlobals.getPermissionManager(), mEnableAppSettingMigration),
+                        AppGlobals.getPermissionManager(), mEnableAppSettingMigration,
+                        mForceUserSetOnUpgrade),
                 LocalServices.getService(UsageStatsManagerInternal.class));
 
         publishBinderService(Context.NOTIFICATION_SERVICE, mService, /* allowIsolated= */ false,
@@ -6069,6 +6075,7 @@
                     pw.println("  mMaxPackageEnqueueRate=" + mMaxPackageEnqueueRate);
                     pw.println("  hideSilentStatusBar="
                             + mPreferencesHelper.shouldHideSilentStatusIcons());
+                    pw.println("  mForceUserSetOnUpgrade=" + mForceUserSetOnUpgrade);
                 }
                 pw.println("  mArchive=" + mArchive.toString());
                 mArchive.dumpImpl(pw, filter);
diff --git a/services/core/java/com/android/server/notification/PermissionHelper.java b/services/core/java/com/android/server/notification/PermissionHelper.java
index e551f10..b4230c1 100644
--- a/services/core/java/com/android/server/notification/PermissionHelper.java
+++ b/services/core/java/com/android/server/notification/PermissionHelper.java
@@ -57,13 +57,16 @@
     private final IPermissionManager mPermManager;
     // TODO (b/194833441): Remove when the migration is enabled
     private final boolean mMigrationEnabled;
+    private final boolean mForceUserSetOnUpgrade;
 
     public PermissionHelper(PermissionManagerServiceInternal pmi, IPackageManager packageManager,
-            IPermissionManager permManager, boolean migrationEnabled) {
+            IPermissionManager permManager, boolean migrationEnabled,
+            boolean forceUserSetOnUpgrade) {
         mPmi = pmi;
         mPackageManager = packageManager;
         mPermManager = permManager;
         mMigrationEnabled = migrationEnabled;
+        mForceUserSetOnUpgrade = forceUserSetOnUpgrade;
     }
 
     public boolean isMigrationEnabled() {
@@ -223,8 +226,9 @@
             return;
         }
         if (!isPermissionFixed(pkgPerm.packageName, pkgPerm.userId)) {
+            boolean userSet = mForceUserSetOnUpgrade ? true : pkgPerm.userModifiedSettings;
             setNotificationPermission(pkgPerm.packageName, pkgPerm.userId, pkgPerm.granted,
-                    pkgPerm.userSet, !pkgPerm.userSet);
+                    userSet, !userSet);
         }
     }
 
@@ -305,13 +309,13 @@
         public final String packageName;
         public final @UserIdInt int userId;
         public final boolean granted;
-        public final boolean userSet;
+        public final boolean userModifiedSettings;
 
         public PackagePermission(String pkg, int userId, boolean granted, boolean userSet) {
             this.packageName = pkg;
             this.userId = userId;
             this.granted = granted;
-            this.userSet = userSet;
+            this.userModifiedSettings = userSet;
         }
 
         @Override
@@ -319,13 +323,14 @@
             if (this == o) return true;
             if (o == null || getClass() != o.getClass()) return false;
             PackagePermission that = (PackagePermission) o;
-            return userId == that.userId && granted == that.granted && userSet == that.userSet
+            return userId == that.userId && granted == that.granted && userModifiedSettings
+                    == that.userModifiedSettings
                     && Objects.equals(packageName, that.packageName);
         }
 
         @Override
         public int hashCode() {
-            return Objects.hash(packageName, userId, granted, userSet);
+            return Objects.hash(packageName, userId, granted, userModifiedSettings);
         }
 
         @Override
@@ -334,7 +339,7 @@
                     "packageName='" + packageName + '\'' +
                     ", userId=" + userId +
                     ", granted=" + granted +
-                    ", userSet=" + userSet +
+                    ", userSet=" + userModifiedSettings +
                     '}';
         }
     }
diff --git a/services/core/java/com/android/server/notification/ZenModeHelper.java b/services/core/java/com/android/server/notification/ZenModeHelper.java
index 93f1b47..9e0c975 100644
--- a/services/core/java/com/android/server/notification/ZenModeHelper.java
+++ b/services/core/java/com/android/server/notification/ZenModeHelper.java
@@ -104,6 +104,7 @@
 
     // The amount of time rules instances can exist without their owning app being installed.
     private static final int RULE_INSTANCE_GRACE_PERIOD = 1000 * 60 * 60 * 72;
+    static final int RULE_LIMIT_PER_PACKAGE = 100;
 
     // pkg|userId => uid
     protected final ArrayMap<String, Integer> mRulesUidCache = new ArrayMap<>();
@@ -329,10 +330,10 @@
             int newRuleInstanceCount = getCurrentInstanceCount(automaticZenRule.getOwner())
                     + getCurrentInstanceCount(automaticZenRule.getConfigurationActivity())
                     + 1;
-            if (ruleInstanceLimit > 0 && ruleInstanceLimit < newRuleInstanceCount) {
+            if (newRuleInstanceCount > RULE_LIMIT_PER_PACKAGE
+                    || (ruleInstanceLimit > 0 && ruleInstanceLimit < newRuleInstanceCount)) {
                 throw new IllegalArgumentException("Rule instance limit exceeded");
             }
-
         }
 
         ZenModeConfig newConfig;
diff --git a/services/core/java/com/android/server/pm/ApexManager.java b/services/core/java/com/android/server/pm/ApexManager.java
index 6a2b2d5..76d3d23 100644
--- a/services/core/java/com/android/server/pm/ApexManager.java
+++ b/services/core/java/com/android/server/pm/ApexManager.java
@@ -29,7 +29,6 @@
 import android.apex.IApexService;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageInfo;
-import android.content.pm.PackageInstaller;
 import android.content.pm.PackageManager;
 import android.content.pm.SigningDetails;
 import android.content.pm.parsing.result.ParseResult;
@@ -834,7 +833,7 @@
                 throw new RuntimeException(re);
             } catch (Exception e) {
                 throw new PackageManagerException(
-                        PackageInstaller.SessionInfo.SESSION_VERIFICATION_FAILED,
+                        PackageManager.INSTALL_FAILED_VERIFICATION_FAILURE,
                         "apexd verification failed : " + e.getMessage());
             }
         }
@@ -861,7 +860,7 @@
                 throw new RuntimeException(re);
             } catch (Exception e) {
                 throw new PackageManagerException(
-                        PackageInstaller.SessionInfo.SESSION_VERIFICATION_FAILED,
+                        PackageManager.INSTALL_FAILED_VERIFICATION_FAILURE,
                         "Failed to mark apexd session as ready : " + e.getMessage());
             }
         }
diff --git a/services/core/java/com/android/server/pm/ApkChecksums.java b/services/core/java/com/android/server/pm/ApkChecksums.java
index aa467e7..2824585 100644
--- a/services/core/java/com/android/server/pm/ApkChecksums.java
+++ b/services/core/java/com/android/server/pm/ApkChecksums.java
@@ -34,6 +34,7 @@
 import android.content.pm.ApkChecksum;
 import android.content.pm.Checksum;
 import android.content.pm.IOnChecksumsReadyListener;
+import android.content.pm.PackageManager;
 import android.content.pm.PackageManagerInternal;
 import android.content.pm.Signature;
 import android.content.pm.SigningDetails.SignatureSchemeVersion;
@@ -62,6 +63,7 @@
 
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.security.VerityUtils;
+import com.android.server.LocalServices;
 import com.android.server.pm.parsing.pkg.AndroidPackage;
 
 import java.io.ByteArrayOutputStream;
diff --git a/services/core/java/com/android/server/pm/AppIdSettingMap.java b/services/core/java/com/android/server/pm/AppIdSettingMap.java
new file mode 100644
index 0000000..b41a0b8
--- /dev/null
+++ b/services/core/java/com/android/server/pm/AppIdSettingMap.java
@@ -0,0 +1,147 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.pm;
+
+import android.os.Process;
+import android.util.Log;
+
+import com.android.server.utils.WatchedArrayList;
+import com.android.server.utils.WatchedSparseArray;
+import com.android.server.utils.Watcher;
+
+/**
+ * A wrapper over {@link WatchedArrayList} that tracks the current (app ID -> SettingBase) mapping
+ * for non-system apps. Also tracks system app settings in an {@link WatchedSparseArray}.
+ */
+final class AppIdSettingMap {
+    /**
+     * We use an ArrayList instead of an SparseArray for non system apps because the number of apps
+     * might be big, and only ArrayList gives us a constant lookup time. For a given app ID, the
+     * index to the corresponding SettingBase object is (appId - FIRST_APPLICATION_ID). If an app ID
+     * doesn't exist (i.e., app is not installed), we fill the corresponding entry with null.
+     */
+    private WatchedArrayList<SettingBase> mNonSystemSettings = new WatchedArrayList<>();
+    private WatchedSparseArray<SettingBase> mSystemSettings = new WatchedSparseArray<>();
+    private int mFirstAvailableAppId = Process.FIRST_APPLICATION_UID;
+
+    /** Returns true if the requested AppID was valid and not already registered. */
+    public boolean registerExistingAppId(int appId, SettingBase setting, Object name) {
+        if (appId >= Process.FIRST_APPLICATION_UID) {
+            int size = mNonSystemSettings.size();
+            final int index = appId - Process.FIRST_APPLICATION_UID;
+            // fill the array until our index becomes valid
+            while (index >= size) {
+                mNonSystemSettings.add(null);
+                size++;
+            }
+            if (mNonSystemSettings.get(index) != null) {
+                PackageManagerService.reportSettingsProblem(Log.WARN,
+                        "Adding duplicate app id: " + appId
+                                + " name=" + name);
+                return false;
+            }
+            mNonSystemSettings.set(index, setting);
+        } else {
+            if (mSystemSettings.get(appId) != null) {
+                PackageManagerService.reportSettingsProblem(Log.WARN,
+                        "Adding duplicate shared id: " + appId
+                                + " name=" + name);
+                return false;
+            }
+            mSystemSettings.put(appId, setting);
+        }
+        return true;
+    }
+
+    public SettingBase getSetting(int appId) {
+        if (appId >= Process.FIRST_APPLICATION_UID) {
+            final int size = mNonSystemSettings.size();
+            final int index = appId - Process.FIRST_APPLICATION_UID;
+            return index < size ? mNonSystemSettings.get(index) : null;
+        } else {
+            return mSystemSettings.get(appId);
+        }
+    }
+
+    public void removeSetting(int appId) {
+        if (appId >= Process.FIRST_APPLICATION_UID) {
+            final int size = mNonSystemSettings.size();
+            final int index = appId - Process.FIRST_APPLICATION_UID;
+            if (index < size) {
+                mNonSystemSettings.set(index, null);
+            }
+        } else {
+            mSystemSettings.remove(appId);
+        }
+        setFirstAvailableAppId(appId + 1);
+    }
+
+    // This should be called (at least) whenever an application is removed
+    private void setFirstAvailableAppId(int uid) {
+        if (uid > mFirstAvailableAppId) {
+            mFirstAvailableAppId = uid;
+        }
+    }
+
+    public void replaceSetting(int appId, SettingBase setting) {
+        if (appId >= Process.FIRST_APPLICATION_UID) {
+            final int size = mNonSystemSettings.size();
+            final int index = appId - Process.FIRST_APPLICATION_UID;
+            if (index < size) {
+                mNonSystemSettings.set(index, setting);
+            } else {
+                PackageManagerService.reportSettingsProblem(Log.WARN,
+                        "Error in package manager settings: calling replaceAppIdLpw to"
+                                + " replace SettingBase at appId=" + appId
+                                + " but nothing is replaced.");
+            }
+        } else {
+            mSystemSettings.put(appId, setting);
+        }
+    }
+
+    /** Returns a new AppID or -1 if we could not find an available AppID to assign */
+    public int acquireAndRegisterNewAppId(SettingBase obj) {
+        final int size = mNonSystemSettings.size();
+        for (int i = mFirstAvailableAppId - Process.FIRST_APPLICATION_UID; i < size; i++) {
+            if (mNonSystemSettings.get(i) == null) {
+                mNonSystemSettings.set(i, obj);
+                return Process.FIRST_APPLICATION_UID + i;
+            }
+        }
+
+        // None left?
+        if (size > (Process.LAST_APPLICATION_UID - Process.FIRST_APPLICATION_UID)) {
+            return -1;
+        }
+
+        mNonSystemSettings.add(obj);
+        return Process.FIRST_APPLICATION_UID + size;
+    }
+
+    public AppIdSettingMap snapshot() {
+        AppIdSettingMap l = new AppIdSettingMap();
+        mNonSystemSettings.snapshot(l.mNonSystemSettings, mNonSystemSettings);
+        mSystemSettings.snapshot(l.mSystemSettings, mSystemSettings);
+        return l;
+    }
+
+    public void registerObserver(Watcher observer) {
+        mNonSystemSettings.registerObserver(observer);
+        mSystemSettings.registerObserver(observer);
+    }
+}
diff --git a/services/core/java/com/android/server/pm/AppsFilter.java b/services/core/java/com/android/server/pm/AppsFilter.java
index d117967..7b2dc28 100644
--- a/services/core/java/com/android/server/pm/AppsFilter.java
+++ b/services/core/java/com/android/server/pm/AppsFilter.java
@@ -52,6 +52,7 @@
 import com.android.internal.util.ArrayUtils;
 import com.android.internal.util.function.QuadFunction;
 import com.android.server.FgThread;
+import com.android.server.LocalServices;
 import com.android.server.compat.CompatChange;
 import com.android.server.om.OverlayReferenceMapper;
 import com.android.server.pm.parsing.pkg.AndroidPackage;
@@ -486,12 +487,12 @@
     }
 
     /** Builder method for an AppsFilter */
-    public static AppsFilter create(
-            PackageManagerInternal pms, PackageManagerServiceInjector injector) {
+    public static AppsFilter create(@NonNull PackageManagerServiceInjector injector,
+            @NonNull PackageManagerInternal pmInt) {
         final boolean forceSystemAppsQueryable =
                 injector.getContext().getResources()
                         .getBoolean(R.bool.config_forceSystemPackagesQueryable);
-        final FeatureConfigImpl featureConfig = new FeatureConfigImpl(pms, injector);
+        final FeatureConfigImpl featureConfig = new FeatureConfigImpl(pmInt, injector);
         final String[] forcedQueryablePackageNames;
         if (forceSystemAppsQueryable) {
             // all system apps already queryable, no need to read and parse individual exceptions
@@ -512,7 +513,7 @@
         };
         AppsFilter appsFilter = new AppsFilter(stateProvider, featureConfig,
                 forcedQueryablePackageNames, forceSystemAppsQueryable, null,
-                injector.getBackgroundExecutor(), pms);
+                injector.getBackgroundExecutor(), pmInt);
         featureConfig.setAppsFilter(appsFilter);
         return appsFilter;
     }
diff --git a/services/core/java/com/android/server/pm/BackgroundDexOptService.java b/services/core/java/com/android/server/pm/BackgroundDexOptService.java
index ecbb4a9..9ff4aab 100644
--- a/services/core/java/com/android/server/pm/BackgroundDexOptService.java
+++ b/services/core/java/com/android/server/pm/BackgroundDexOptService.java
@@ -19,6 +19,7 @@
 import static com.android.server.pm.PackageManagerService.PLATFORM_PACKAGE_NAME;
 
 import android.annotation.IntDef;
+import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.app.job.JobInfo;
 import android.app.job.JobParameters;
@@ -269,7 +270,7 @@
             PackageManagerService pm = mInjector.getPackageManagerService();
             ArraySet<String> packagesToOptimize;
             if (packageNames == null) {
-                packagesToOptimize = mDexOptHelper.getOptimizablePackages();
+                packagesToOptimize = mDexOptHelper.getOptimizablePackages(pm.snapshotComputer());
             } else {
                 packagesToOptimize = new ArraySet<>(packageNames);
             }
@@ -334,7 +335,7 @@
             return false;
         }
 
-        ArraySet<String> pkgs = mDexOptHelper.getOptimizablePackages();
+        ArraySet<String> pkgs = mDexOptHelper.getOptimizablePackages(pm.snapshotComputer());
         if (pkgs.isEmpty()) {
             Slog.i(TAG, "No packages to optimize");
             markPostBootUpdateCompleted(params);
@@ -556,8 +557,8 @@
     }
 
     /** Gets the size of a package. */
-    private long getPackageSize(PackageManagerService pm, String pkg) {
-        PackageInfo info = pm.snapshotComputer().getPackageInfo(pkg, 0, UserHandle.USER_SYSTEM);
+    private long getPackageSize(@NonNull Computer snapshot, String pkg) {
+        PackageInfo info = snapshot.getPackageInfo(pkg, 0, UserHandle.USER_SYSTEM);
         long size = 0;
         if (info != null && info.applicationInfo != null) {
             File path = Paths.get(info.applicationInfo.sourceDir).toFile();
@@ -605,8 +606,9 @@
                 Slog.d(TAG, "Should Downgrade " + shouldDowngrade);
             }
             if (shouldDowngrade) {
+                final Computer snapshot = pm.snapshotComputer();
                 Set<String> unusedPackages =
-                        pm.getUnusedPackages(mDowngradeUnusedAppsThresholdInMillis);
+                        snapshot.getUnusedPackages(mDowngradeUnusedAppsThresholdInMillis);
                 if (DEBUG) {
                     Slog.d(TAG, "Unsused Packages " + String.join(",", unusedPackages));
                 }
@@ -618,7 +620,7 @@
                             // Should be aborted by the scheduler.
                             return abortCode;
                         }
-                        @DexOptResult int downgradeResult = downgradePackage(pm, pkg,
+                        @DexOptResult int downgradeResult = downgradePackage(snapshot, pm, pkg,
                                 /* isForPrimaryDex= */ true, isPostBootUpdate);
                         if (downgradeResult == PackageDexOptimizer.DEX_OPT_PERFORMED) {
                             updatedPackages.add(pkg);
@@ -629,7 +631,7 @@
                             return status;
                         }
                         if (supportSecondaryDex) {
-                            downgradeResult = downgradePackage(pm, pkg,
+                            downgradeResult = downgradePackage(snapshot, pm, pkg,
                                     /* isForPrimaryDex= */false, isPostBootUpdate);
                             status = convertPackageDexOptimizerStatusToInternal(downgradeResult);
                             if (status != STATUS_OK) {
@@ -696,8 +698,8 @@
      * @return PackageDexOptimizer.DEX_*
      */
     @DexOptResult
-    private int downgradePackage(PackageManagerService pm, String pkg, boolean isForPrimaryDex,
-            boolean isPostBootUpdate) {
+    private int downgradePackage(@NonNull Computer snapshot, PackageManagerService pm, String pkg,
+            boolean isForPrimaryDex, boolean isPostBootUpdate) {
         if (DEBUG) {
             Slog.d(TAG, "Downgrading " + pkg);
         }
@@ -709,15 +711,15 @@
         if (!isPostBootUpdate) {
             dexoptFlags |= DexoptOptions.DEXOPT_IDLE_BACKGROUND_JOB;
         }
-        long package_size_before = getPackageSize(pm, pkg);
+        long package_size_before = getPackageSize(snapshot, pkg);
         int result = PackageDexOptimizer.DEX_OPT_SKIPPED;
         if (isForPrimaryDex || PLATFORM_PACKAGE_NAME.equals(pkg)) {
             // This applies for system apps or if packages location is not a directory, i.e.
             // monolithic install.
-            if (!pm.canHaveOatDir(pkg)) {
+            if (!pm.canHaveOatDir(snapshot, pkg)) {
                 // For apps that don't have the oat directory, instead of downgrading,
                 // remove their compiler artifacts from dalvik cache.
-                pm.deleteOatArtifactsOfPackage(pkg);
+                pm.deleteOatArtifactsOfPackage(snapshot, pkg);
             } else {
                 result = performDexOptPrimary(pkg, reason, dexoptFlags);
             }
@@ -726,8 +728,9 @@
         }
 
         if (result == PackageDexOptimizer.DEX_OPT_PERFORMED) {
+            final Computer newSnapshot = pm.snapshotComputer();
             FrameworkStatsLog.write(FrameworkStatsLog.APP_DOWNGRADED, pkg, package_size_before,
-                    getPackageSize(pm, pkg), /*aggressive=*/ false);
+                    getPackageSize(newSnapshot, pkg), /*aggressive=*/ false);
         }
         return result;
     }
diff --git a/services/core/java/com/android/server/pm/BroadcastHelper.java b/services/core/java/com/android/server/pm/BroadcastHelper.java
index df7387d..f1394d4 100644
--- a/services/core/java/com/android/server/pm/BroadcastHelper.java
+++ b/services/core/java/com/android/server/pm/BroadcastHelper.java
@@ -119,14 +119,10 @@
                 intent.setPackage(targetPkg);
             }
             // Modify the UID when posting to other users
-            final String[] uidExtraNames =
-                    { Intent.EXTRA_UID, Intent.EXTRA_PREVIOUS_UID, Intent.EXTRA_NEW_UID };
-            for (String name : uidExtraNames) {
-                int uid = intent.getIntExtra(name, -1);
-                if (uid >= 0 && UserHandle.getUserId(uid) != userId) {
-                    uid = UserHandle.getUid(userId, UserHandle.getAppId(uid));
-                    intent.putExtra(name, uid);
-                }
+            int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
+            if (uid >= 0 && UserHandle.getUserId(uid) != userId) {
+                uid = UserHandle.getUid(userId, UserHandle.getAppId(uid));
+                intent.putExtra(Intent.EXTRA_UID, uid);
             }
             if (broadcastAllowList != null && PLATFORM_PACKAGE_NAME.equals(targetPkg)) {
                 intent.putExtra(Intent.EXTRA_VISIBILITY_ALLOW_LIST,
diff --git a/services/core/java/com/android/server/pm/Computer.java b/services/core/java/com/android/server/pm/Computer.java
index 6103d68..8e85301 100644
--- a/services/core/java/com/android/server/pm/Computer.java
+++ b/services/core/java/com/android/server/pm/Computer.java
@@ -58,10 +58,6 @@
 
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
 import java.util.List;
 import java.util.Set;
 
@@ -92,92 +88,69 @@
  * and other managers (like PermissionManager) mean deadlock is possible.  On the
  * other hand, not overriding in {@link ComputerLocked} may leave a function walking
  * unstable data.
- *
- * To coax developers to consider such issues carefully, all methods in
- * {@link Computer} must be annotated with <code>@LiveImplementation(override =
- * MANDATORY)</code> or <code>LiveImplementation(locked = NOT_ALLOWED)</code>.  A unit
- * test verifies the annotation and that the annotation corresponds to the code in
- * {@link ComputerEngine} and {@link ComputerLocked}.
  */
 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
 public interface Computer extends PackageDataSnapshot {
 
     /**
-     * Every method must be annotated.
-     */
-    @Target({ ElementType.METHOD })
-    @Retention(RetentionPolicy.RUNTIME)
-    @interface LiveImplementation {
-        // A Computer method must be annotated with one of the following values:
-        //   MANDATORY - the method must be overridden in ComputerEngineLive.  The
-        //     format of the override is a call to the super method, wrapped in a
-        //     synchronization block.
-        //   NOT_ALLOWED - the method may not appear in the live computer.  It must
-        //     be final in the ComputerEngine.
-        int MANDATORY = 1;
-        int NOT_ALLOWED = 2;
-        int override() default MANDATORY;
-        String rationale() default "";
-    }
-
-    /**
      * Administrative statistics: record that the snapshot has been used.  Every call
      * to use() increments the usage counter.
      */
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     default void use() {
     }
     /**
      * Fetch the snapshot usage counter.
      * @return The number of times this snapshot was used.
      */
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     default int getUsed() {
         return 0;
     }
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     @NonNull List<ResolveInfo> queryIntentActivitiesInternal(Intent intent, String resolvedType,
             @PackageManager.ResolveInfoFlagsBits long flags,
             @PackageManagerInternal.PrivateResolveFlags long privateResolveFlags,
             int filterCallingUid, int userId, boolean resolveForStart, boolean allowDynamicSplits);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     @NonNull List<ResolveInfo> queryIntentActivitiesInternal(Intent intent, String resolvedType,
             long flags, int userId);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     @NonNull List<ResolveInfo> queryIntentServicesInternal(Intent intent, String resolvedType,
             long flags, int userId, int callingUid, boolean includeInstantApps);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.MANDATORY)
     @NonNull QueryIntentActivitiesResult queryIntentActivitiesInternalBody(Intent intent,
             String resolvedType, long flags, int filterCallingUid, int userId,
             boolean resolveForStart, boolean allowDynamicSplits, String pkgName,
             String instantAppPkgName);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     ActivityInfo getActivityInfo(ComponentName component, long flags, int userId);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
+
+    /**
+     * Important: The provided filterCallingUid is used exclusively to filter out activities
+     * that can be seen based on user state. It's typically the original caller uid prior
+     * to clearing. Because it can only be provided by trusted code, its value can be
+     * trusted and will be used as-is; unlike userId which will be validated by this method.
+     */
     ActivityInfo getActivityInfoInternal(ComponentName component, long flags,
             int filterCallingUid, int userId);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.MANDATORY)
     AndroidPackage getPackage(String packageName);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.MANDATORY)
     AndroidPackage getPackage(int uid);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     ApplicationInfo generateApplicationInfoFromSettings(String packageName, long flags,
             int filterCallingUid, int userId);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     ApplicationInfo getApplicationInfo(String packageName, long flags, int userId);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
+
+    /**
+     * Important: The provided filterCallingUid is used exclusively to filter out applications
+     * that can be seen based on user state. It's typically the original caller uid prior
+     * to clearing. Because it can only be provided by trusted code, its value can be
+     * trusted and will be used as-is; unlike userId which will be validated by this method.
+     */
     ApplicationInfo getApplicationInfoInternal(String packageName, long flags,
             int filterCallingUid, int userId);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
+
+    /**
+     * Report the 'Home' activity which is currently set as "always use this one". If non is set
+     * then reports the most likely home activity or null if there are more than one.
+     */
     ComponentName getDefaultHomeActivity(int userId);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     ComponentName getHomeActivitiesAsUser(List<ResolveInfo> allHomeCandidates, int userId);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     CrossProfileDomainInfo getCrossProfileDomainPreferredLpr(Intent intent, String resolvedType,
             long flags, int sourceUserId, int parentUserId);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     Intent getHomeIntent();
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     List<CrossProfileIntentFilter> getMatchingCrossProfileIntentFilters(Intent intent,
             String resolvedType, int userId);
 
@@ -192,15 +165,11 @@
      * @param intent
      * @return A filtered list of resolved activities.
      */
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     List<ResolveInfo> applyPostResolutionFilter(@NonNull List<ResolveInfo> resolveInfos,
             String ephemeralPkgName, boolean allowDynamicSplits, int filterCallingUid,
             boolean resolveForStart, int userId, Intent intent);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     PackageInfo generatePackageInfo(PackageStateInternal ps, long flags, int userId);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     PackageInfo getPackageInfo(String packageName, long flags, int userId);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     PackageInfo getPackageInfoInternal(String packageName, long versionCode, long flags,
             int filterCallingUid, int userId);
 
@@ -209,79 +178,69 @@
      * known {@link PackageState} instances without a {@link PackageState#getAndroidPackage()}
      * will not be represented.
      */
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.MANDATORY)
     String[] getAllAvailablePackageNames();
 
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     PackageStateInternal getPackageStateInternal(String packageName);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.MANDATORY)
     PackageStateInternal getPackageStateInternal(String packageName, int callingUid);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.MANDATORY)
-    @Nullable PackageState getPackageStateCopied(@NonNull String packageName);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     ParceledListSlice<PackageInfo> getInstalledPackages(long flags, int userId);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     ResolveInfo createForwardingResolveInfoUnchecked(WatchedIntentFilter filter,
             int sourceUserId, int targetUserId);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     ServiceInfo getServiceInfo(ComponentName component, long flags, int userId);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     SharedLibraryInfo getSharedLibraryInfo(String name, long version);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.MANDATORY)
     String getInstantAppPackageName(int callingUid);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     String resolveExternalPackageName(AndroidPackage pkg);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     String resolveInternalPackageName(String packageName, long versionCode);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     String[] getPackagesForUid(int uid);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     UserInfo getProfileParent(int userId);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     boolean canViewInstantApps(int callingUid, int userId);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     boolean filterSharedLibPackage(@Nullable PackageStateInternal ps, int uid, int userId,
             long flags);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     boolean isCallerSameApp(String packageName, int uid);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     boolean isComponentVisibleToInstantApp(@Nullable ComponentName component);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     boolean isComponentVisibleToInstantApp(@Nullable ComponentName component,
             @PackageManager.ComponentType int type);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
-    boolean isImplicitImageCaptureIntentAndNotSetByDpcLocked(Intent intent, int userId,
+
+    /**
+     * From Android R, camera intents have to match system apps. The only exception to this is if
+     * the DPC has set the camera persistent preferred activity. This case was introduced
+     * because it is important that the DPC has the ability to set both system and non-system
+     * camera persistent preferred activities.
+     *
+     * @return {@code true} if the intent is a camera intent and the persistent preferred
+     * activity was not set by the DPC.
+     */
+    boolean isImplicitImageCaptureIntentAndNotSetByDpc(Intent intent, int userId,
             String resolvedType, long flags);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     boolean isInstantApp(String packageName, int userId);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     boolean isInstantAppInternal(String packageName, @UserIdInt int userId, int callingUid);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     boolean isSameProfileGroup(@UserIdInt int callerUserId, @UserIdInt int userId);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     boolean shouldFilterApplication(@Nullable PackageStateInternal ps, int callingUid,
             @Nullable ComponentName component, @PackageManager.ComponentType int componentType,
             int userId);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     boolean shouldFilterApplication(@Nullable PackageStateInternal ps, int callingUid,
             int userId);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     boolean shouldFilterApplication(@NonNull SharedUserSetting sus, int callingUid,
             int userId);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     int checkUidPermission(String permName, int uid);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.MANDATORY)
     int getPackageUidInternal(String packageName, long flags, int userId, int callingUid);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     long updateFlagsForApplication(long flags, int userId);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     long updateFlagsForComponent(long flags, int userId);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     long updateFlagsForPackage(long flags, int userId);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
+
+    /**
+     * Update given flags when being used to request {@link ResolveInfo}.
+     * <p>Instant apps are resolved specially, depending upon context. Minimally,
+     * {@code}flags{@code} must have the {@link PackageManager#MATCH_INSTANT}
+     * flag set. However, this flag is only honoured in three circumstances:
+     * <ul>
+     * <li>when called from a system process</li>
+     * <li>when the caller holds the permission {@code android.permission.ACCESS_INSTANT_APPS}</li>
+     * <li>when resolution occurs to start an activity with a {@code android.intent.action.VIEW}
+     * action and a {@code android.intent.category.BROWSABLE} category</li>
+     * </ul>
+     */
     long updateFlagsForResolve(long flags, int userId, int callingUid, boolean wantInstantApps,
             boolean isImplicitImageCaptureIntentAndNotSetByDpc);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     long updateFlagsForResolve(long flags, int userId, int callingUid, boolean wantInstantApps,
             boolean onlyExposedExplicitly, boolean isImplicitImageCaptureIntentAndNotSetByDpc);
 
@@ -299,117 +258,99 @@
      * @param checkShell whether to prevent shell from access if there's a debugging restriction
      * @param message the message to log on security exception
      */
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     void enforceCrossUserOrProfilePermission(int callingUid, @UserIdInt int userId,
             boolean requireFullPermission, boolean checkShell, String message);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
+
+    /**
+     * Enforces the request is from the system or an app that has INTERACT_ACROSS_USERS
+     * or INTERACT_ACROSS_USERS_FULL permissions, if the {@code userId} is not for the caller.
+     *
+     * @param checkShell whether to prevent shell from access if there's a debugging restriction
+     * @param message the message to log on security exception
+     */
     void enforceCrossUserPermission(int callingUid, @UserIdInt int userId,
             boolean requireFullPermission, boolean checkShell, String message);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     void enforceCrossUserPermission(int callingUid, @UserIdInt int userId,
             boolean requireFullPermission, boolean checkShell,
             boolean requirePermissionWhenSameUser, String message);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.MANDATORY)
     SigningDetails getSigningDetails(@NonNull String packageName);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.MANDATORY)
     SigningDetails getSigningDetails(int uid);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.MANDATORY)
     boolean filterAppAccess(AndroidPackage pkg, int callingUid, int userId);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.MANDATORY)
     boolean filterAppAccess(String packageName, int callingUid, int userId);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.MANDATORY)
     boolean filterAppAccess(int uid, int callingUid);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.MANDATORY)
     void dump(int type, FileDescriptor fd, PrintWriter pw, DumpState dumpState);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
     PackageManagerService.FindPreferredActivityBodyResult findPreferredActivityInternal(
             Intent intent, String resolvedType, long flags, List<ResolveInfo> query, boolean always,
             boolean removeMatches, boolean debug, int userId, boolean queryMayBeFiltered);
-    @Computer.LiveImplementation(override = Computer.LiveImplementation.NOT_ALLOWED)
-    ResolveInfo findPersistentPreferredActivityLP(Intent intent, String resolvedType, long flags,
+    ResolveInfo findPersistentPreferredActivity(Intent intent, String resolvedType, long flags,
             List<ResolveInfo> query, boolean debug, int userId);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     PreferredIntentResolver getPreferredActivities(@UserIdInt int userId);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @NonNull
     ArrayMap<String, ? extends PackageStateInternal> getPackageStates();
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @Nullable
     String getRenamedPackage(@NonNull String packageName);
 
     /**
      * @return set of packages to notify
      */
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @NonNull
     ArraySet<String> getNotifyPackagesForReplacedReceived(@NonNull String[] packages);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @PackageManagerService.PackageStartability
     int getPackageStartability(boolean safeMode, @NonNull String packageName, int callingUid,
             @UserIdInt int userId);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     boolean isPackageAvailable(String packageName, @UserIdInt int userId);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @NonNull
     String[] currentToCanonicalPackageNames(@NonNull String[] names);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @NonNull
     String[] canonicalToCurrentPackageNames(@NonNull String[] names);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @NonNull
     int[] getPackageGids(@NonNull String packageName,
             @PackageManager.PackageInfoFlagsBits long flags, @UserIdInt int userId);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     int getTargetSdkVersion(@NonNull String packageName);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     boolean activitySupportsIntent(@NonNull ComponentName resolveComponentName,
             @NonNull ComponentName component, @NonNull Intent intent, String resolvedType);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @Nullable
     ActivityInfo getReceiverInfo(@NonNull ComponentName component,
             @PackageManager.ComponentInfoFlagsBits long flags, @UserIdInt int userId);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @Nullable
     ParceledListSlice<SharedLibraryInfo> getSharedLibraries(@NonNull String packageName,
             @PackageManager.PackageInfoFlagsBits long flags, @UserIdInt int userId);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     boolean canRequestPackageInstalls(@NonNull String packageName, int callingUid,
             int userId, boolean throwIfPermNotDeclared);
 
-    @Computer.LiveImplementation(override = LiveImplementation.NOT_ALLOWED)
+    /**
+     * Returns true if the system or user is explicitly preventing an otherwise valid installer to
+     * complete an install. This includes checks like unknown sources and user restrictions.
+     */
     boolean isInstallDisabledForPackage(@NonNull String packageName, int uid,
             @UserIdInt int userId);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @Nullable
     List<VersionedPackage> getPackagesUsingSharedLibrary(@NonNull SharedLibraryInfo libInfo,
             @PackageManager.PackageInfoFlagsBits long flags, int callingUid, @UserIdInt int userId);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @Nullable
     ParceledListSlice<SharedLibraryInfo> getDeclaredSharedLibraries(
             @NonNull String packageName, @PackageManager.PackageInfoFlagsBits long flags,
             @UserIdInt int userId);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @Nullable
     ProviderInfo getProviderInfo(@NonNull ComponentName component,
             @PackageManager.ComponentInfoFlagsBits long flags, @UserIdInt int userId);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @Nullable
     String[] getSystemSharedLibraryNames();
 
@@ -417,136 +358,103 @@
      * @return the state if the given package has a state and isn't filtered by visibility.
      * Provides no guarantee that the package is in any usable state.
      */
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @Nullable
     PackageStateInternal getPackageStateFiltered(@NonNull String packageName, int callingUid,
             @UserIdInt int userId);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     int checkSignatures(@NonNull String pkg1, @NonNull String pkg2);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     int checkUidSignatures(int uid1, int uid2);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     boolean hasSigningCertificate(@NonNull String packageName, @NonNull byte[] certificate,
             @PackageManager.CertificateInputType int type);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     boolean hasUidSigningCertificate(int uid, @NonNull byte[] certificate,
             @PackageManager.CertificateInputType int type);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @NonNull
     List<String> getAllPackages();
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @Nullable
     String getNameForUid(int uid);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @Nullable
     String[] getNamesForUids(@NonNull int[] uids);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     int getUidForSharedUser(@NonNull String sharedUserName);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     int getFlagsForUid(int uid);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     int getPrivateFlagsForUid(int uid);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     boolean isUidPrivileged(int uid);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @NonNull
     String[] getAppOpPermissionPackages(@NonNull String permissionName);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @NonNull
     ParceledListSlice<PackageInfo> getPackagesHoldingPermissions(@NonNull String[] permissions,
             @PackageManager.PackageInfoFlagsBits long flags, @UserIdInt int userId);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @NonNull
     List<ApplicationInfo> getInstalledApplications(
             @PackageManager.ApplicationInfoFlagsBits long flags, @UserIdInt int userId,
             int callingUid);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @Nullable
     ProviderInfo resolveContentProvider(@NonNull String name,
             @PackageManager.ResolveInfoFlagsBits long flags, @UserIdInt int userId, int callingUid);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @Nullable
     ProviderInfo getGrantImplicitAccessProviderInfo(int recipientUid,
             @NonNull String visibleAuthority);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     void querySyncProviders(boolean safeMode, @NonNull List<String> outNames,
             @NonNull List<ProviderInfo> outInfo);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @NonNull
     ParceledListSlice<ProviderInfo> queryContentProviders(@Nullable String processName, int uid,
             @PackageManager.ComponentInfoFlagsBits long flags, @Nullable String metaDataKey);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @Nullable
     InstrumentationInfo getInstrumentationInfo(@NonNull ComponentName component, int flags);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @NonNull
     ParceledListSlice<InstrumentationInfo> queryInstrumentation(
             @NonNull String targetPackage, int flags);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @NonNull
     List<PackageStateInternal> findSharedNonSystemLibraries(
             @NonNull PackageStateInternal pkgSetting);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     boolean getApplicationHiddenSettingAsUser(@NonNull String packageName, @UserIdInt int userId);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     boolean isPackageSuspendedForUser(@NonNull String packageName, @UserIdInt int userId);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     boolean isSuspendingAnyPackages(@NonNull String suspendingPackage, @UserIdInt int userId);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @NonNull
     ParceledListSlice<IntentFilter> getAllIntentFilters(@NonNull String packageName);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     boolean getBlockUninstallForUser(@NonNull String packageName, @UserIdInt int userId);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @Nullable
     SparseArray<int[]> getBroadcastAllowList(@NonNull String packageName, @UserIdInt int[] userIds,
             boolean isInstantApp);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @Nullable
     String getInstallerPackageName(@NonNull String packageName);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @Nullable
     InstallSourceInfo getInstallSourceInfo(@NonNull String packageName);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @PackageManager.EnabledState
     int getApplicationEnabledSetting(@NonNull String packageName, @UserIdInt int userId);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @PackageManager.EnabledState
     int getComponentEnabledSetting(@NonNull ComponentName component, int callingUid,
             @UserIdInt int userId);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @PackageManager.EnabledState
     int getComponentEnabledSettingInternal(@NonNull ComponentName component, int callingUid,
             @UserIdInt int userId);
@@ -557,25 +465,19 @@
      * are all effectively enabled for the given component. Or if the component cannot be found,
      * returns false.
      */
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     boolean isComponentEffectivelyEnabled(@NonNull ComponentInfo componentInfo,
             @UserIdInt int userId);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @Nullable
     KeySet getKeySetByAlias(@NonNull String packageName, @NonNull String alias);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @Nullable
     KeySet getSigningKeySet(@NonNull String packageName);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     boolean isPackageSignedByKeySet(@NonNull String packageName, @NonNull KeySet ks);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     boolean isPackageSignedByKeySetExactly(@NonNull String packageName, @NonNull KeySet ks);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @Nullable
     int[] getVisibilityAllowList(@NonNull String packageName, @UserIdInt int userId);
 
@@ -585,49 +487,37 @@
      * package visibility filtering is enabled on it. If the UID is part of a shared user ID,
      * return {@code true} if any one application belongs to the shared user ID meets the criteria.
      */
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     boolean canQueryPackage(int callingUid, @Nullable String targetPackageName);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     int getPackageUid(@NonNull String packageName, @PackageManager.PackageInfoFlagsBits long flags,
             @UserIdInt int userId);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     boolean canAccessComponent(int callingUid, @NonNull ComponentName component,
             @UserIdInt int userId);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     boolean isCallerInstallerOfRecord(@NonNull AndroidPackage pkg, int callingUid);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @PackageManager.InstallReason
     int getInstallReason(@NonNull String packageName, @UserIdInt int userId);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     boolean canPackageQuery(@NonNull String sourcePackageName, @NonNull String targetPackageName,
             @UserIdInt int userId);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     boolean canForwardTo(@NonNull Intent intent, @Nullable String resolvedType,
             @UserIdInt int sourceUserId, @UserIdInt int targetUserId);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @NonNull
     List<ApplicationInfo> getPersistentApplications(boolean safeMode, int flags);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @NonNull
     SparseArray<String> getAppsWithSharedUserIds();
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @NonNull
     String[] getSharedUserPackagesForPackage(@NonNull String packageName, @UserIdInt int userId);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @NonNull
     Set<String> getUnusedPackages(long downgradeTimeThresholdMillis);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @Nullable
     CharSequence getHarmfulAppWarning(@NonNull String packageName, @UserIdInt int userId);
 
@@ -638,55 +528,49 @@
      *
      * @return The filtered packages
      */
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @NonNull
     String[] filterOnlySystemPackages(@Nullable String... pkgNames);
 
     // The methods in this block should be removed once SettingBase is interface snapshotted
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @NonNull
     List<AndroidPackage> getPackagesForAppId(int appId);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     int getUidTargetSdkVersion(int uid);
 
     /**
      * @see PackageManagerInternal#getProcessesForUid(int)
      */
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @Nullable
     ArrayMap<String, ProcessInfo> getProcessesForUid(int uid);
     // End block
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     boolean getBlockUninstall(@UserIdInt int userId, @NonNull String packageName);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @NonNull
     WatchedArrayMap<String, WatchedLongSparseArray<SharedLibraryInfo>> getSharedLibraries();
 
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @Nullable
     Pair<PackageStateInternal, SharedUserApi> getPackageOrSharedUser(int appId);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @Nullable
     SharedUserApi getSharedUser(int sharedUserAppIde);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @NonNull
     ArraySet<PackageStateInternal> getSharedUserPackages(int sharedUserAppId);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @NonNull
     ComponentResolverApi getComponentResolver();
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @Nullable
     PackageStateInternal getDisabledSystemPackage(@NonNull String packageName);
 
-    @Computer.LiveImplementation(override = LiveImplementation.MANDATORY)
     @Nullable
     ResolveInfo getInstantAppInstallerInfo();
+
+    @NonNull
+    WatchedArrayMap<String, Integer> getFrozenPackages();
+
+    @Nullable
+    ComponentName getInstantAppInstallerComponent();
 }
diff --git a/services/core/java/com/android/server/pm/ComputerEngine.java b/services/core/java/com/android/server/pm/ComputerEngine.java
index 0c9855b..06e827a 100644
--- a/services/core/java/com/android/server/pm/ComputerEngine.java
+++ b/services/core/java/com/android/server/pm/ComputerEngine.java
@@ -466,7 +466,7 @@
 
         flags = updateFlagsForResolve(flags, userId, filterCallingUid, resolveForStart,
                 comp != null || pkgName != null /*onlyExposedExplicitly*/,
-                isImplicitImageCaptureIntentAndNotSetByDpcLocked(intent, userId, resolvedType,
+                isImplicitImageCaptureIntentAndNotSetByDpc(intent, userId, resolvedType,
                         flags));
         List<ResolveInfo> list = Collections.emptyList();
         boolean skipPostResolution = false;
@@ -1722,15 +1722,6 @@
         return mSettings.getPackage(packageName);
     }
 
-    @Nullable
-    public PackageState getPackageStateCopied(@NonNull String packageName) {
-        int callingUid = Binder.getCallingUid();
-        packageName = resolveInternalPackageNameInternalLocked(
-                packageName, PackageManager.VERSION_CODE_HIGHEST, callingUid);
-        PackageStateInternal pkgSetting = mSettings.getPackage(packageName);
-        return pkgSetting == null ? null : PackageStateImpl.copy(pkgSetting);
-    }
-
     public final ParceledListSlice<PackageInfo> getInstalledPackages(long flags, int userId) {
         final int callingUid = Binder.getCallingUid();
         if (getInstantAppPackageName(callingUid) != null) {
@@ -2468,7 +2459,7 @@
      * @return {@code true} if the intent is a camera intent and the persistent preferred
      * activity was not set by the DPC.
      */
-    public final boolean isImplicitImageCaptureIntentAndNotSetByDpcLocked(Intent intent,
+    public final boolean isImplicitImageCaptureIntentAndNotSetByDpc(Intent intent,
             int userId, String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags) {
         return intent.isImplicitImageCaptureIntent() && !isPersistentPreferredActivitySetByDpm(
                 intent, userId, resolvedType, flags);
@@ -3228,12 +3219,12 @@
 
         flags = updateFlagsForResolve(
                 flags, userId, callingUid, false /*includeInstantApps*/,
-                isImplicitImageCaptureIntentAndNotSetByDpcLocked(intent, userId,
+                isImplicitImageCaptureIntentAndNotSetByDpc(intent, userId,
                         resolvedType, flags));
         intent = PackageManagerServiceUtils.updateIntentForResolve(intent);
 
         // Try to find a matching persistent preferred activity.
-        result.mPreferredResolveInfo = findPersistentPreferredActivityLP(intent,
+        result.mPreferredResolveInfo = findPersistentPreferredActivity(intent,
                 resolvedType, flags, query, debug, userId);
 
         // If a persistent preferred activity matched, use it.
@@ -3444,7 +3435,7 @@
                 userId, queryMayBeFiltered, callingUid, isDeviceProvisioned);
     }
 
-    public final ResolveInfo findPersistentPreferredActivityLP(Intent intent,
+    public final ResolveInfo findPersistentPreferredActivity(Intent intent,
             String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags,
             List<ResolveInfo> query, boolean debug, int userId) {
         final int n = query.size();
@@ -5418,7 +5409,7 @@
             }
             long flags = updateFlagsForResolve(0, parent.id, callingUid,
                     false /*includeInstantApps*/,
-                    isImplicitImageCaptureIntentAndNotSetByDpcLocked(intent, parent.id,
+                    isImplicitImageCaptureIntentAndNotSetByDpc(intent, parent.id,
                             resolvedType, 0));
             flags |= PackageManager.MATCH_DEFAULT_ONLY;
             CrossProfileDomainInfo xpDomainInfo = getCrossProfileDomainPreferredLpr(
@@ -5694,4 +5685,17 @@
     public ResolveInfo getInstantAppInstallerInfo() {
         return mInstantAppInstallerInfo;
     }
+
+    @NonNull
+    @Override
+    public WatchedArrayMap<String, Integer> getFrozenPackages() {
+        return mFrozenPackages;
+    }
+
+    @Nullable
+    @Override
+    public ComponentName getInstantAppInstallerComponent() {
+        return mLocalInstantAppInstallerActivity == null
+                ? null : mLocalInstantAppInstallerActivity.getComponentName();
+    }
 }
diff --git a/services/core/java/com/android/server/pm/ComputerLocked.java b/services/core/java/com/android/server/pm/ComputerLocked.java
index 5d89c7d..af196d5 100644
--- a/services/core/java/com/android/server/pm/ComputerLocked.java
+++ b/services/core/java/com/android/server/pm/ComputerLocked.java
@@ -16,62 +16,21 @@
 
 package com.android.server.pm;
 
-import android.annotation.NonNull;
-import android.annotation.Nullable;
-import android.annotation.UserIdInt;
 import android.content.ComponentName;
-import android.content.Intent;
-import android.content.IntentFilter;
 import android.content.pm.ActivityInfo;
 import android.content.pm.ApplicationInfo;
-import android.content.pm.ComponentInfo;
-import android.content.pm.InstallSourceInfo;
-import android.content.pm.InstrumentationInfo;
-import android.content.pm.KeySet;
-import android.content.pm.PackageInfo;
-import android.content.pm.PackageManager;
-import android.content.pm.ParceledListSlice;
-import android.content.pm.ProcessInfo;
-import android.content.pm.ProviderInfo;
-import android.content.pm.ResolveInfo;
-import android.content.pm.ServiceInfo;
-import android.content.pm.SharedLibraryInfo;
-import android.content.pm.SigningDetails;
-import android.content.pm.VersionedPackage;
-import android.util.ArrayMap;
-import android.util.ArraySet;
-import android.util.Pair;
-import android.util.SparseArray;
 
 import com.android.internal.annotations.VisibleForTesting;
-import com.android.server.pm.parsing.pkg.AndroidPackage;
-import com.android.server.pm.pkg.PackageState;
-import com.android.server.pm.pkg.PackageStateInternal;
-import com.android.server.pm.pkg.SharedUserApi;
-import com.android.server.pm.resolution.ComponentResolverApi;
-import com.android.server.utils.WatchedArrayMap;
-import com.android.server.utils.WatchedLongSparseArray;
-
-import java.io.FileDescriptor;
-import java.io.PrintWriter;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Set;
 
 /**
  * This subclass is the external interface to the live computer.  Some internal helper
- * methods are overridden to fetch live data instead of snapshot data.  For each
- * Computer interface that is overridden in this class, the override takes the PM lock
- * and then delegates to the live computer engine.  This is required because there are
- * no locks taken in the engine itself.
+ * methods are overridden to fetch live data instead of snapshot data.
  */
 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
 public final class ComputerLocked extends ComputerEngine {
-    private final Object mLock;
 
     ComputerLocked(PackageManagerService.Snapshot args) {
         super(args);
-        mLock = mService.mLock;
     }
 
     protected ComponentName resolveComponentName() {
@@ -83,814 +42,4 @@
     protected ApplicationInfo androidApplication() {
         return mService.getCoreAndroidApplication();
     }
-
-    public @NonNull List<ResolveInfo> queryIntentServicesInternalBody(Intent intent,
-            String resolvedType, int flags, int userId, int callingUid,
-            String instantAppPkgName) {
-        synchronized (mLock) {
-            return super.queryIntentServicesInternalBody(intent, resolvedType, flags, userId,
-                    callingUid, instantAppPkgName);
-        }
-    }
-    public @NonNull QueryIntentActivitiesResult queryIntentActivitiesInternalBody(
-            Intent intent, String resolvedType, long flags, int filterCallingUid, int userId,
-            boolean resolveForStart, boolean allowDynamicSplits, String pkgName,
-            String instantAppPkgName) {
-        synchronized (mLock) {
-            return super.queryIntentActivitiesInternalBody(intent, resolvedType, flags,
-                    filterCallingUid, userId, resolveForStart, allowDynamicSplits, pkgName,
-                    instantAppPkgName);
-        }
-    }
-    public ActivityInfo getActivityInfoInternalBody(ComponentName component, int flags,
-            int filterCallingUid, int userId) {
-        synchronized (mLock) {
-            return super.getActivityInfoInternalBody(component, flags, filterCallingUid,
-                    userId);
-        }
-    }
-    public AndroidPackage getPackage(String packageName) {
-        synchronized (mLock) {
-            return super.getPackage(packageName);
-        }
-    }
-    public AndroidPackage getPackage(int uid) {
-        synchronized (mLock) {
-            return super.getPackage(uid);
-        }
-    }
-    public ApplicationInfo getApplicationInfoInternalBody(String packageName, int flags,
-            int filterCallingUid, int userId) {
-        synchronized (mLock) {
-            return super.getApplicationInfoInternalBody(packageName, flags, filterCallingUid,
-                    userId);
-        }
-    }
-    public ArrayList<ResolveInfo> filterCandidatesWithDomainPreferredActivitiesLPrBody(
-            Intent intent, int matchFlags, List<ResolveInfo> candidates,
-            CrossProfileDomainInfo xpDomainInfo, int userId, boolean debug) {
-        synchronized (mLock) {
-            return super.filterCandidatesWithDomainPreferredActivitiesLPrBody(intent,
-                    matchFlags, candidates, xpDomainInfo, userId, debug);
-        }
-    }
-    public PackageInfo getPackageInfoInternalBody(String packageName, long versionCode,
-            int flags, int filterCallingUid, int userId) {
-        synchronized (mLock) {
-            return super.getPackageInfoInternalBody(packageName, versionCode, flags,
-                    filterCallingUid, userId);
-        }
-    }
-
-    @Override
-    public String[] getAllAvailablePackageNames() {
-        synchronized (mLock) {
-            return super.getAllAvailablePackageNames();
-        }
-    }
-
-    public PackageStateInternal getPackageStateInternal(String packageName, int callingUid) {
-        synchronized (mLock) {
-            return super.getPackageStateInternal(packageName, callingUid);
-        }
-    }
-
-    @Nullable
-    public PackageState getPackageStateCopied(@NonNull String packageName) {
-        synchronized (mLock) {
-            return super.getPackageStateCopied(packageName);
-        }
-    }
-
-    public ParceledListSlice<PackageInfo> getInstalledPackagesBody(int flags, int userId,
-            int callingUid) {
-        synchronized (mLock) {
-            return super.getInstalledPackagesBody(flags, userId, callingUid);
-        }
-    }
-    public ServiceInfo getServiceInfoBody(ComponentName component, int flags, int userId,
-            int callingUid) {
-        synchronized (mLock) {
-            return super.getServiceInfoBody(component, flags, userId, callingUid);
-        }
-    }
-    public String getInstantAppPackageName(int callingUid) {
-        synchronized (mLock) {
-            return super.getInstantAppPackageName(callingUid);
-        }
-    }
-    public String[] getPackagesForUidInternalBody(int callingUid, int userId, int appId,
-            boolean isCallerInstantApp) {
-        synchronized (mLock) {
-            return super.getPackagesForUidInternalBody(callingUid, userId, appId,
-                    isCallerInstantApp);
-        }
-    }
-    public boolean isInstantAppInternalBody(String packageName, @UserIdInt int userId,
-            int callingUid) {
-        synchronized (mLock) {
-            return super.isInstantAppInternalBody(packageName, userId, callingUid);
-        }
-    }
-    public boolean isInstantAppResolutionAllowedBody(Intent intent,
-            List<ResolveInfo> resolvedActivities, int userId, boolean skipPackageCheck,
-            int flags) {
-        synchronized (mLock) {
-            return super.isInstantAppResolutionAllowedBody(intent, resolvedActivities, userId,
-                    skipPackageCheck, flags);
-        }
-    }
-    public int getPackageUidInternal(String packageName, int flags, int userId,
-            int callingUid) {
-        synchronized (mLock) {
-            return super.getPackageUidInternal(packageName, flags, userId, callingUid);
-        }
-    }
-    public SigningDetails getSigningDetails(@NonNull String packageName) {
-        synchronized (mLock) {
-            return super.getSigningDetails(packageName);
-        }
-    }
-    public SigningDetails getSigningDetails(int uid) {
-        synchronized (mLock) {
-            return super.getSigningDetails(uid);
-        }
-    }
-    public boolean filterAppAccess(AndroidPackage pkg, int callingUid, int userId) {
-        synchronized (mLock) {
-            return super.filterAppAccess(pkg, callingUid, userId);
-        }
-    }
-    public boolean filterAppAccess(String packageName, int callingUid, int userId) {
-        synchronized (mLock) {
-            return super.filterAppAccess(packageName, callingUid, userId);
-        }
-    }
-    public boolean filterAppAccess(int uid, int callingUid) {
-        synchronized (mLock) {
-            return super.filterAppAccess(uid, callingUid);
-        }
-    }
-    public void dump(int type, FileDescriptor fd, PrintWriter pw, DumpState dumpState) {
-        synchronized (mLock) {
-            super.dump(type, fd, pw, dumpState);
-        }
-    }
-    public PackageManagerService.FindPreferredActivityBodyResult findPreferredActivityBody(
-            Intent intent, String resolvedType, int flags, List<ResolveInfo> query, boolean always,
-            boolean removeMatches, boolean debug, int userId, boolean queryMayBeFiltered,
-            int callingUid, boolean isDeviceProvisioned) {
-        synchronized (mLock) {
-            return super.findPreferredActivityBody(intent, resolvedType, flags, query, always,
-                    removeMatches, debug, userId, queryMayBeFiltered, callingUid,
-                    isDeviceProvisioned);
-        }
-    }
-
-    @Override
-    public PreferredIntentResolver getPreferredActivities(int userId) {
-        synchronized (mLock) {
-            return super.getPreferredActivities(userId);
-        }
-    }
-
-    @NonNull
-    @Override
-    public ArrayMap<String, ? extends PackageStateInternal> getPackageStates() {
-        synchronized (mLock) {
-            return super.getPackageStates();
-        }
-    }
-
-    @Nullable
-    @Override
-    public String getRenamedPackage(@NonNull String packageName) {
-        synchronized (mLock) {
-            return super.getRenamedPackage(packageName);
-        }
-    }
-
-    @NonNull
-    @Override
-    public ArraySet<String> getNotifyPackagesForReplacedReceived(@NonNull String[] packages) {
-        synchronized (mLock) {
-            return super.getNotifyPackagesForReplacedReceived(packages);
-        }
-    }
-
-    @Override
-    public int getPackageStartability(boolean safeMode, @NonNull String packageName, int callingUid,
-            @UserIdInt int userId) {
-        synchronized (mLock) {
-            return super.getPackageStartability(safeMode, packageName, callingUid, userId);
-        }
-    }
-
-    @Override
-    public boolean isPackageAvailable(String packageName, @UserIdInt int userId) {
-        synchronized (mLock) {
-            return super.isPackageAvailable(packageName, userId);
-        }
-    }
-
-    @Override
-    public String[] currentToCanonicalPackageNames(String[] names) {
-        synchronized (mLock) {
-            return super.currentToCanonicalPackageNames(names);
-        }
-    }
-
-    @Override
-    public String[] canonicalToCurrentPackageNames(String[] names) {
-        synchronized (mLock) {
-            return super.canonicalToCurrentPackageNames(names);
-        }
-    }
-
-    @Override
-    public int[] getPackageGids(@NonNull String packageName,
-            @PackageManager.PackageInfoFlagsBits long flags, @UserIdInt int userId) {
-        synchronized (mLock) {
-            return super.getPackageGids(packageName, flags, userId);
-        }
-    }
-
-    @Override
-    public int getTargetSdkVersion(@NonNull String packageName) {
-        synchronized (mLock) {
-            return super.getTargetSdkVersion(packageName);
-        }
-    }
-
-    @Override
-    public boolean activitySupportsIntent(@NonNull ComponentName resolveComponentName,
-            @NonNull ComponentName component, @NonNull Intent intent, String resolvedType) {
-        synchronized (mLock) {
-            return super.activitySupportsIntent(resolveComponentName, component, intent,
-                    resolvedType);
-        }
-    }
-
-    @Nullable
-    @Override
-    public ActivityInfo getReceiverInfo(@NonNull ComponentName component,
-            @PackageManager.ComponentInfoFlagsBits long flags, @UserIdInt int userId) {
-        synchronized (mLock) {
-            return super.getReceiverInfo(component, flags, userId);
-        }
-    }
-
-    @Nullable
-    @Override
-    public ParceledListSlice<SharedLibraryInfo> getSharedLibraries(@NonNull String packageName,
-            @PackageManager.PackageInfoFlagsBits long flags, @UserIdInt int userId) {
-        synchronized (mLock) {
-            return super.getSharedLibraries(packageName, flags, userId);
-        }
-    }
-
-    @Override
-    public boolean canRequestPackageInstalls(@NonNull String packageName, int callingUid,
-            @UserIdInt int userId, boolean throwIfPermNotDeclared) {
-        synchronized (mLock) {
-            return super.canRequestPackageInstalls(packageName, callingUid, userId,
-                    throwIfPermNotDeclared);
-        }
-    }
-
-    @Override
-    public List<VersionedPackage> getPackagesUsingSharedLibrary(@NonNull SharedLibraryInfo libInfo,
-            @PackageManager.PackageInfoFlagsBits long flags, int callingUid,
-            @UserIdInt int userId) {
-        synchronized (mLock) {
-            return super.getPackagesUsingSharedLibrary(libInfo, flags, callingUid, userId);
-        }
-    }
-
-    @Nullable
-    @Override
-    public ParceledListSlice<SharedLibraryInfo> getDeclaredSharedLibraries(
-            @NonNull String packageName, @PackageManager.PackageInfoFlagsBits long flags,
-            @UserIdInt int userId) {
-        synchronized (mLock) {
-            return super.getDeclaredSharedLibraries(packageName, flags, userId);
-        }
-    }
-
-    @Nullable
-    @Override
-    public ProviderInfo getProviderInfo(@NonNull ComponentName component,
-            @PackageManager.ComponentInfoFlagsBits long flags, @UserIdInt int userId) {
-        synchronized (mLock) {
-            return super.getProviderInfo(component, flags, userId);
-        }
-    }
-
-    @Nullable
-    @Override
-    public String[] getSystemSharedLibraryNames() {
-        synchronized (mLock) {
-            return super.getSystemSharedLibraryNames();
-        }
-    }
-
-    @Override
-    public int checkSignatures(@NonNull String pkg1,
-            @NonNull String pkg2) {
-        synchronized (mLock) {
-            return super.checkSignatures(pkg1, pkg2);
-        }
-    }
-
-    @Override
-    public int checkUidSignatures(int uid1, int uid2) {
-        synchronized (mLock) {
-            return super.checkUidSignatures(uid1, uid2);
-        }
-    }
-
-    @Override
-    public boolean hasSigningCertificate(@NonNull String packageName, @NonNull byte[] certificate,
-            int type) {
-        synchronized (mLock) {
-            return super.hasSigningCertificate(packageName, certificate, type);
-        }
-    }
-
-    @Override
-    public boolean hasUidSigningCertificate(int uid, @NonNull byte[] certificate, int type) {
-        synchronized (mLock) {
-            return super.hasUidSigningCertificate(uid, certificate, type);
-        }
-    }
-
-    @Override
-    public List<String> getAllPackages() {
-        synchronized (mLock) {
-            return super.getAllPackages();
-        }
-    }
-
-    @Nullable
-    @Override
-    public String getNameForUid(int uid) {
-        synchronized (mLock) {
-            return super.getNameForUid(uid);
-        }
-    }
-
-    @Nullable
-    @Override
-    public String[] getNamesForUids(int[] uids) {
-        synchronized (mLock) {
-            return super.getNamesForUids(uids);
-        }
-    }
-
-    @Override
-    public int getUidForSharedUser(@NonNull String sharedUserName) {
-        synchronized (mLock) {
-            return super.getUidForSharedUser(sharedUserName);
-        }
-    }
-
-    @Override
-    public int getFlagsForUid(int uid) {
-        synchronized (mLock) {
-            return super.getFlagsForUid(uid);
-        }
-    }
-
-    @Override
-    public int getPrivateFlagsForUid(int uid) {
-        synchronized (mLock) {
-            return super.getPrivateFlagsForUid(uid);
-        }
-    }
-
-    @Override
-    public boolean isUidPrivileged(int uid) {
-        synchronized (mLock) {
-            return super.isUidPrivileged(uid);
-        }
-    }
-
-    @NonNull
-    @Override
-    public String[] getAppOpPermissionPackages(@NonNull String permissionName) {
-        synchronized (mLock) {
-            return super.getAppOpPermissionPackages(permissionName);
-        }
-    }
-
-    @NonNull
-    @Override
-    public ParceledListSlice<PackageInfo> getPackagesHoldingPermissions(
-            @NonNull String[] permissions, @PackageManager.PackageInfoFlagsBits long flags,
-            @UserIdInt int userId) {
-        synchronized (mLock) {
-            return super.getPackagesHoldingPermissions(permissions, flags, userId);
-        }
-    }
-
-    @NonNull
-    @Override
-    public List<ApplicationInfo> getInstalledApplications(
-            @PackageManager.ApplicationInfoFlagsBits long flags, @UserIdInt int userId,
-            int callingUid) {
-        synchronized (mLock) {
-            return super.getInstalledApplications(flags, userId, callingUid);
-        }
-    }
-
-    @Nullable
-    @Override
-    public ProviderInfo resolveContentProvider(@NonNull String name,
-            @PackageManager.ResolveInfoFlagsBits long flags, @UserIdInt int userId,
-            int callingUid) {
-        synchronized (mLock) {
-            return super.resolveContentProvider(name, flags, userId, callingUid);
-        }
-    }
-
-    @Nullable
-    @Override
-    public ProviderInfo getGrantImplicitAccessProviderInfo(int recipientUid,
-            @NonNull String visibleAuthority) {
-        synchronized (mLock) {
-            return super.getGrantImplicitAccessProviderInfo(recipientUid, visibleAuthority);
-        }
-    }
-
-    @Override
-    public void querySyncProviders(boolean safeMode, @NonNull List<String> outNames,
-            @NonNull List<ProviderInfo> outInfo) {
-        synchronized (mLock) {
-            super.querySyncProviders(safeMode, outNames, outInfo);
-        }
-    }
-
-    @NonNull
-    @Override
-    public ParceledListSlice<ProviderInfo> queryContentProviders(@Nullable String processName,
-            int uid, @PackageManager.ComponentInfoFlagsBits long flags,
-            @Nullable String metaDataKey) {
-        synchronized (mLock) {
-            return super.queryContentProviders(processName, uid, flags, metaDataKey);
-        }
-    }
-
-    @Nullable
-    @Override
-    public InstrumentationInfo getInstrumentationInfo(@NonNull ComponentName component, int flags) {
-        synchronized (mLock) {
-            return super.getInstrumentationInfo(component, flags);
-        }
-    }
-
-    @NonNull
-    @Override
-    public ParceledListSlice<InstrumentationInfo> queryInstrumentation(
-            @NonNull String targetPackage, int flags) {
-        synchronized (mLock) {
-            return super.queryInstrumentation(targetPackage, flags);
-        }
-    }
-
-    @NonNull
-    @Override
-    public List<PackageStateInternal> findSharedNonSystemLibraries(
-            @NonNull PackageStateInternal pkgSetting) {
-        synchronized (mLock) {
-            return super.findSharedNonSystemLibraries(pkgSetting);
-        }
-    }
-
-    @Override
-    public boolean getApplicationHiddenSettingAsUser(@NonNull String packageName,
-            @UserIdInt int userId) {
-        synchronized (mLock) {
-            return super.getApplicationHiddenSettingAsUser(packageName, userId);
-        }
-    }
-
-    @Override
-    public boolean isPackageSuspendedForUser(@NonNull String packageName, @UserIdInt int userId) {
-        synchronized (mLock) {
-            return super.isPackageSuspendedForUser(packageName, userId);
-        }
-    }
-
-    @Override
-    public boolean isSuspendingAnyPackages(@NonNull String suspendingPackage,
-            @UserIdInt int userId) {
-        synchronized (mLock) {
-            return super.isSuspendingAnyPackages(suspendingPackage, userId);
-        }
-    }
-
-    @NonNull
-    @Override
-    public ParceledListSlice<IntentFilter> getAllIntentFilters(@NonNull String packageName) {
-        synchronized (mLock) {
-            return super.getAllIntentFilters(packageName);
-        }
-    }
-
-    @Override
-    public boolean getBlockUninstallForUser(@NonNull String packageName, @UserIdInt int userId) {
-        synchronized (mLock) {
-            return super.getBlockUninstallForUser(packageName, userId);
-        }
-    }
-
-    @Nullable
-    @Override
-    public SparseArray<int[]> getBroadcastAllowList(@NonNull String packageName,
-            @UserIdInt int[] userIds, boolean isInstantApp) {
-        synchronized (mLock) {
-            return super.getBroadcastAllowList(packageName, userIds, isInstantApp);
-        }
-    }
-
-    @Nullable
-    @Override
-    public String getInstallerPackageName(@NonNull String packageName) {
-        synchronized (mLock) {
-            return super.getInstallerPackageName(packageName);
-        }
-    }
-
-    @Nullable
-    @Override
-    public InstallSourceInfo getInstallSourceInfo(@NonNull String packageName) {
-        synchronized (mLock) {
-            return super.getInstallSourceInfo(packageName);
-        }
-    }
-
-    @Override
-    public int getApplicationEnabledSetting(@NonNull String packageName, @UserIdInt int userId) {
-        synchronized (mLock) {
-            return super.getApplicationEnabledSetting(packageName, userId);
-        }
-    }
-
-    @Override
-    public int getComponentEnabledSetting(@NonNull ComponentName component, int callingUid,
-            @UserIdInt int userId) {
-        synchronized (mLock) {
-            return super.getComponentEnabledSetting(component, callingUid, userId);
-        }
-    }
-
-    @Override
-    public int getComponentEnabledSettingInternal(@NonNull ComponentName component, int callingUid,
-            @UserIdInt int userId) {
-        synchronized (mLock) {
-            return super.getComponentEnabledSettingInternal(component, callingUid, userId);
-        }
-    }
-
-    @Override
-    public boolean isComponentEffectivelyEnabled(@NonNull ComponentInfo componentInfo,
-            @UserIdInt int userId) {
-        synchronized (mLock) {
-            return super.isComponentEffectivelyEnabled(componentInfo, userId);
-        }
-    }
-
-    @Nullable
-    @Override
-    public KeySet getKeySetByAlias(@NonNull String packageName, @NonNull String alias) {
-        synchronized (mLock) {
-            return super.getKeySetByAlias(packageName, alias);
-        }
-    }
-
-    @Nullable
-    @Override
-    public KeySet getSigningKeySet(@NonNull String packageName) {
-        synchronized (mLock) {
-            return super.getSigningKeySet(packageName);
-        }
-    }
-
-    @Override
-    public boolean isPackageSignedByKeySet(@NonNull String packageName, @NonNull KeySet ks) {
-        synchronized (mLock) {
-            return super.isPackageSignedByKeySet(packageName, ks);
-        }
-    }
-
-    @Override
-    public boolean isPackageSignedByKeySetExactly(@NonNull String packageName, @NonNull KeySet ks) {
-        synchronized (mLock) {
-            return super.isPackageSignedByKeySetExactly(packageName, ks);
-        }
-    }
-
-    @Nullable
-    @Override
-    public int[] getVisibilityAllowList(@NonNull String packageName, @UserIdInt int userId) {
-        synchronized (mLock) {
-            return super.getVisibilityAllowList(packageName, userId);
-        }
-    }
-
-    @Override
-    public boolean canQueryPackage(int callingUid, @Nullable String targetPackageName) {
-        synchronized (mLock) {
-            return super.canQueryPackage(callingUid, targetPackageName);
-        }
-    }
-
-    @Override
-    public int getPackageUid(@NonNull String packageName,
-            @PackageManager.PackageInfoFlagsBits long flags, @UserIdInt int userId) {
-        synchronized (mLock) {
-            return super.getPackageUid(packageName, flags, userId);
-        }
-    }
-
-    @Override
-    public boolean canAccessComponent(int callingUid, @NonNull ComponentName component,
-            @UserIdInt int userId) {
-        synchronized (mLock) {
-            return super.canAccessComponent(callingUid, component, userId);
-        }
-    }
-
-    @Override
-    public boolean isCallerInstallerOfRecord(@NonNull AndroidPackage pkg, int callingUid) {
-        synchronized (mLock) {
-            return super.isCallerInstallerOfRecord(pkg, callingUid);
-        }
-    }
-
-    @Override
-    public int getInstallReason(@NonNull String packageName, @UserIdInt int userId) {
-        synchronized (mLock) {
-            return super.getInstallReason(packageName, userId);
-        }
-    }
-
-    @Override
-    public boolean canPackageQuery(@NonNull String sourcePackageName,
-            @NonNull String targetPackageName, @UserIdInt int userId) {
-        synchronized (mLock) {
-            return super.canPackageQuery(sourcePackageName, targetPackageName, userId);
-        }
-    }
-
-    @Override
-    public boolean canForwardTo(@NonNull Intent intent, @Nullable String resolvedType,
-            @UserIdInt int sourceUserId, @UserIdInt int targetUserId) {
-        synchronized (mLock) {
-            return super.canForwardTo(intent, resolvedType, sourceUserId, targetUserId);
-        }
-    }
-
-    @NonNull
-    @Override
-    public List<ApplicationInfo> getPersistentApplications(boolean safeMode, int flags) {
-        synchronized (mLock) {
-            return super.getPersistentApplications(safeMode, flags);
-        }
-    }
-
-    @NonNull
-    @Override
-    public SparseArray<String> getAppsWithSharedUserIds() {
-        synchronized (mLock) {
-            return super.getAppsWithSharedUserIds();
-        }
-    }
-
-    @NonNull
-    @Override
-    public String[] getSharedUserPackagesForPackage(@NonNull String packageName,
-            @UserIdInt int userId) {
-        synchronized (mLock) {
-            return super.getSharedUserPackagesForPackage(packageName, userId);
-        }
-    }
-
-    @NonNull
-    @Override
-    public Set<String> getUnusedPackages(long downgradeTimeThresholdMillis) {
-        synchronized (mLock) {
-            return super.getUnusedPackages(downgradeTimeThresholdMillis);
-        }
-    }
-
-    @Nullable
-    @Override
-    public CharSequence getHarmfulAppWarning(@NonNull String packageName, @UserIdInt int userId) {
-        synchronized (mLock) {
-            return super.getHarmfulAppWarning(packageName, userId);
-        }
-    }
-
-    @NonNull
-    @Override
-    public String[] filterOnlySystemPackages(@Nullable String... pkgNames) {
-        synchronized (mLock) {
-            return super.filterOnlySystemPackages(pkgNames);
-        }
-    }
-
-    @NonNull
-    @Override
-    public List<AndroidPackage> getPackagesForAppId(int appId) {
-        synchronized (mLock) {
-            return super.getPackagesForAppId(appId);
-        }
-    }
-
-    @Override
-    public int getUidTargetSdkVersion(int uid) {
-        synchronized (mLock) {
-            return super.getUidTargetSdkVersion(uid);
-        }
-    }
-
-    @Nullable
-    @Override
-    public ArrayMap<String, ProcessInfo> getProcessesForUid(int uid) {
-        synchronized (mLock) {
-            return super.getProcessesForUid(uid);
-        }
-    }
-
-    @Override
-    public PackageStateInternal getPackageStateFiltered(@NonNull String packageName, int callingUid,
-            @UserIdInt int userId) {
-        synchronized (mLock) {
-            return super.getPackageStateFiltered(packageName, callingUid, userId);
-        }
-    }
-
-    @Override
-    public boolean getBlockUninstall(@UserIdInt int userId, @NonNull String packageName) {
-        synchronized (mLock) {
-            return super.getBlockUninstall(userId, packageName);
-        }
-    }
-
-    @NonNull
-    @Override
-    public WatchedArrayMap<String, WatchedLongSparseArray<SharedLibraryInfo>> getSharedLibraries() {
-        synchronized (mLock) {
-            return super.getSharedLibraries();
-        }
-    }
-
-    @Nullable
-    @Override
-    public Pair<PackageStateInternal, SharedUserApi> getPackageOrSharedUser(int appId) {
-        synchronized (mLock) {
-            return super.getPackageOrSharedUser(appId);
-        }
-    }
-
-    @Nullable
-    @Override
-    public SharedUserApi getSharedUser(int sharedUserAppId) {
-        synchronized (mLock) {
-            return super.getSharedUser(sharedUserAppId);
-        }
-    }
-
-    @NonNull
-    @Override
-    public ArraySet<PackageStateInternal> getSharedUserPackages(int sharedUserAppId) {
-        synchronized (mLock) {
-            return super.getSharedUserPackages(sharedUserAppId);
-        }
-    }
-
-    @NonNull
-    @Override
-    public ComponentResolverApi getComponentResolver() {
-        synchronized (mLock) {
-            return super.getComponentResolver();
-        }
-    }
-
-    @Nullable
-    @Override
-    public PackageStateInternal getDisabledSystemPackage(@NonNull String packageName) {
-        synchronized (mLock) {
-            return super.getDisabledSystemPackage(packageName);
-        }
-    }
-
-    @Nullable
-    @Override
-    public ResolveInfo getInstantAppInstallerInfo() {
-        synchronized (mLock) {
-            return super.getInstantAppInstallerInfo();
-        }
-    }
 }
diff --git a/services/core/java/com/android/server/pm/ComputerTracker.java b/services/core/java/com/android/server/pm/ComputerTracker.java
deleted file mode 100644
index 216ad71..0000000
--- a/services/core/java/com/android/server/pm/ComputerTracker.java
+++ /dev/null
@@ -1,1327 +0,0 @@
-/*
- * Copyright (C) 2021 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.server.pm;
-
-import android.annotation.NonNull;
-import android.annotation.Nullable;
-import android.annotation.UserIdInt;
-import android.content.ComponentName;
-import android.content.Intent;
-import android.content.IntentFilter;
-import android.content.pm.ActivityInfo;
-import android.content.pm.ApplicationInfo;
-import android.content.pm.ComponentInfo;
-import android.content.pm.InstallSourceInfo;
-import android.content.pm.InstrumentationInfo;
-import android.content.pm.KeySet;
-import android.content.pm.PackageInfo;
-import android.content.pm.PackageManager;
-import android.content.pm.PackageManagerInternal;
-import android.content.pm.ParceledListSlice;
-import android.content.pm.ProcessInfo;
-import android.content.pm.ProviderInfo;
-import android.content.pm.ResolveInfo;
-import android.content.pm.ServiceInfo;
-import android.content.pm.SharedLibraryInfo;
-import android.content.pm.SigningDetails;
-import android.content.pm.UserInfo;
-import android.content.pm.VersionedPackage;
-import android.util.ArrayMap;
-import android.util.ArraySet;
-import android.util.Pair;
-import android.util.SparseArray;
-
-import com.android.server.pm.parsing.pkg.AndroidPackage;
-import com.android.server.pm.pkg.PackageState;
-import com.android.server.pm.pkg.PackageStateInternal;
-import com.android.server.pm.pkg.SharedUserApi;
-import com.android.server.pm.resolution.ComponentResolverApi;
-import com.android.server.utils.WatchedArrayMap;
-import com.android.server.utils.WatchedLongSparseArray;
-
-import java.io.FileDescriptor;
-import java.io.PrintWriter;
-import java.util.List;
-import java.util.Set;
-import java.util.concurrent.atomic.AtomicInteger;
-
-/**
- * This subclass delegates to methods in a Computer after reference-counting the computer.
- */
-public final class ComputerTracker implements Computer {
-
-    // The number of times a thread reused a computer in its stack instead of fetching
-    // a snapshot computer.
-    private final AtomicInteger mReusedSnapshot = new AtomicInteger(0);
-
-    private final PackageManagerService mService;
-    ComputerTracker(PackageManagerService s) {
-        mService = s;
-    }
-
-    private ThreadComputer snapshot() {
-        ThreadComputer current = PackageManagerService.sThreadComputer.get();
-        if (current.mRefCount > 0) {
-            current.acquire();
-            mReusedSnapshot.incrementAndGet();
-        } else {
-            current.acquire(mService.snapshotComputer());
-        }
-        return current;
-    }
-
-    public @NonNull List<ResolveInfo> queryIntentActivitiesInternal(Intent intent,
-            String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags,
-            @PackageManagerInternal.PrivateResolveFlags long privateResolveFlags,
-            int filterCallingUid, int userId, boolean resolveForStart,
-            boolean allowDynamicSplits) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.queryIntentActivitiesInternal(intent, resolvedType, flags,
-                    privateResolveFlags, filterCallingUid, userId, resolveForStart,
-                    allowDynamicSplits);
-        } finally {
-            current.release();
-        }
-    }
-    public @NonNull List<ResolveInfo> queryIntentActivitiesInternal(Intent intent,
-            String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags, int userId) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.queryIntentActivitiesInternal(intent, resolvedType, flags,
-                    userId);
-        } finally {
-            current.release();
-        }
-    }
-    public @NonNull List<ResolveInfo> queryIntentServicesInternal(Intent intent,
-            String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags, int userId,
-            int callingUid, boolean includeInstantApps) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.queryIntentServicesInternal(intent, resolvedType, flags,
-                    userId, callingUid, includeInstantApps);
-        } finally {
-            current.release();
-        }
-    }
-    public @NonNull QueryIntentActivitiesResult queryIntentActivitiesInternalBody(
-            Intent intent, String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags,
-            int filterCallingUid, int userId, boolean resolveForStart, boolean allowDynamicSplits,
-            String pkgName, String instantAppPkgName) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.queryIntentActivitiesInternalBody(intent, resolvedType,
-                    flags, filterCallingUid, userId, resolveForStart, allowDynamicSplits,
-                    pkgName, instantAppPkgName);
-        } finally {
-            current.release();
-        }
-    }
-    public ActivityInfo getActivityInfo(ComponentName component,
-            @PackageManager.ComponentInfoFlagsBits long flags, int userId) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.getActivityInfo(component, flags, userId);
-        } finally {
-            current.release();
-        }
-    }
-    public ActivityInfo getActivityInfoInternal(ComponentName component,
-            @PackageManager.ComponentInfoFlagsBits long flags,
-            int filterCallingUid, int userId) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.getActivityInfoInternal(component, flags, filterCallingUid,
-                    userId);
-        } finally {
-            current.release();
-        }
-    }
-    public AndroidPackage getPackage(String packageName) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.getPackage(packageName);
-        } finally {
-            current.release();
-        }
-    }
-    public AndroidPackage getPackage(int uid) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.getPackage(uid);
-        } finally {
-            current.release();
-        }
-    }
-    public ApplicationInfo generateApplicationInfoFromSettings(String packageName,
-            long flags, int filterCallingUid, int userId) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.generateApplicationInfoFromSettings(packageName, flags,
-                    filterCallingUid, userId);
-        } finally {
-            current.release();
-        }
-    }
-    public ApplicationInfo getApplicationInfo(String packageName,
-            @PackageManager.ApplicationInfoFlagsBits long flags, int userId) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.getApplicationInfo(packageName, flags, userId);
-        } finally {
-            current.release();
-        }
-    }
-    public ApplicationInfo getApplicationInfoInternal(String packageName,
-            @PackageManager.ApplicationInfoFlagsBits long flags, int filterCallingUid, int userId) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.getApplicationInfoInternal(packageName, flags,
-                    filterCallingUid, userId);
-        } finally {
-            current.release();
-        }
-    }
-    public ComponentName getDefaultHomeActivity(int userId) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.getDefaultHomeActivity(userId);
-        } finally {
-            current.release();
-        }
-    }
-    public ComponentName getHomeActivitiesAsUser(List<ResolveInfo> allHomeCandidates,
-            int userId) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.getHomeActivitiesAsUser(allHomeCandidates, userId);
-        } finally {
-            current.release();
-        }
-    }
-    public CrossProfileDomainInfo getCrossProfileDomainPreferredLpr(Intent intent,
-            String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags, int sourceUserId,
-            int parentUserId) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.getCrossProfileDomainPreferredLpr(intent, resolvedType,
-                    flags, sourceUserId, parentUserId);
-        } finally {
-            current.release();
-        }
-    }
-    public Intent getHomeIntent() {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.getHomeIntent();
-        } finally {
-            current.release();
-        }
-    }
-    public List<CrossProfileIntentFilter> getMatchingCrossProfileIntentFilters(
-            Intent intent, String resolvedType, int userId) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.getMatchingCrossProfileIntentFilters(intent, resolvedType,
-                    userId);
-        } finally {
-            current.release();
-        }
-    }
-    public List<ResolveInfo> applyPostResolutionFilter(
-            @NonNull List<ResolveInfo> resolveInfos,
-            String ephemeralPkgName, boolean allowDynamicSplits, int filterCallingUid,
-            boolean resolveForStart, int userId, Intent intent) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.applyPostResolutionFilter(resolveInfos, ephemeralPkgName,
-                    allowDynamicSplits, filterCallingUid, resolveForStart, userId, intent);
-        } finally {
-            current.release();
-        }
-    }
-    public PackageInfo generatePackageInfo(PackageStateInternal ps,
-            @PackageManager.PackageInfoFlagsBits long flags, int userId) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.generatePackageInfo(ps, flags, userId);
-        } finally {
-            current.release();
-        }
-    }
-    public PackageInfo getPackageInfo(String packageName,
-            @PackageManager.PackageInfoFlagsBits long flags, int userId) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.getPackageInfo(packageName, flags, userId);
-        } finally {
-            current.release();
-        }
-    }
-    public PackageInfo getPackageInfoInternal(String packageName, long versionCode,
-            long flags, int filterCallingUid, int userId) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.getPackageInfoInternal(packageName, versionCode, flags,
-                    filterCallingUid, userId);
-        } finally {
-            current.release();
-        }
-    }
-    public PackageStateInternal getPackageStateInternal(String packageName) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.getPackageStateInternal(packageName);
-        } finally {
-            current.release();
-        }
-    }
-    public PackageStateInternal getPackageStateInternal(String packageName, int callingUid) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.getPackageStateInternal(packageName, callingUid);
-        } finally {
-            current.release();
-        }
-    }
-
-    @Nullable
-    public PackageState getPackageStateCopied(@NonNull String packageName) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.getPackageStateCopied(packageName);
-        } finally {
-            current.release();
-        }
-    }
-
-    public ParceledListSlice<PackageInfo> getInstalledPackages(long flags, int userId) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.getInstalledPackages(flags, userId);
-        } finally {
-            current.release();
-        }
-    }
-    public ResolveInfo createForwardingResolveInfoUnchecked(WatchedIntentFilter filter,
-            int sourceUserId, int targetUserId) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.createForwardingResolveInfoUnchecked(filter, sourceUserId,
-                    targetUserId);
-        } finally {
-            current.release();
-        }
-    }
-    public ServiceInfo getServiceInfo(ComponentName component,
-            @PackageManager.ComponentInfoFlagsBits long flags, int userId) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.getServiceInfo(component, flags, userId);
-        } finally {
-            current.release();
-        }
-    }
-    public SharedLibraryInfo getSharedLibraryInfo(String name, long version) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.getSharedLibraryInfo(name, version);
-        } finally {
-            current.release();
-        }
-    }
-    public SigningDetails getSigningDetails(@NonNull String packageName) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.getSigningDetails(packageName);
-        } finally {
-            current.release();
-        }
-    }
-    public SigningDetails getSigningDetails(int uid) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.getSigningDetails(uid);
-        } finally {
-            current.release();
-        }
-    }
-    public String getInstantAppPackageName(int callingUid) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.getInstantAppPackageName(callingUid);
-        } finally {
-            current.release();
-        }
-    }
-    public String resolveExternalPackageName(AndroidPackage pkg) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.resolveExternalPackageName(pkg);
-        } finally {
-            current.release();
-        }
-    }
-    public String resolveInternalPackageName(String packageName, long versionCode) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.resolveInternalPackageName(packageName, versionCode);
-        } finally {
-            current.release();
-        }
-    }
-    public String[] getPackagesForUid(int uid) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.getPackagesForUid(uid);
-        } finally {
-            current.release();
-        }
-    }
-    public UserInfo getProfileParent(int userId) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.getProfileParent(userId);
-        } finally {
-            current.release();
-        }
-    }
-    public boolean canViewInstantApps(int callingUid, int userId) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.canViewInstantApps(callingUid, userId);
-        } finally {
-            current.release();
-        }
-    }
-    public boolean filterAppAccess(AndroidPackage pkg, int callingUid, int userId) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.filterAppAccess(pkg, callingUid, userId);
-        } finally {
-            current.release();
-        }
-    }
-    public boolean filterAppAccess(String packageName, int callingUid, int userId) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.filterAppAccess(packageName, callingUid, userId);
-        } finally {
-            current.release();
-        }
-    }
-    public boolean filterAppAccess(int uid, int callingUid) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.filterAppAccess(uid, callingUid);
-        } finally {
-            current.release();
-        }
-    }
-    public boolean filterSharedLibPackage(@Nullable PackageStateInternal ps, int uid,
-            int userId, @PackageManager.ComponentInfoFlagsBits long flags) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.filterSharedLibPackage(ps, uid, userId, flags);
-        } finally {
-            current.release();
-        }
-    }
-    public boolean isCallerSameApp(String packageName, int uid) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.isCallerSameApp(packageName, uid);
-        } finally {
-            current.release();
-        }
-    }
-    public boolean isComponentVisibleToInstantApp(@Nullable ComponentName component) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.isComponentVisibleToInstantApp(component);
-        } finally {
-            current.release();
-        }
-    }
-    public boolean isComponentVisibleToInstantApp(@Nullable ComponentName component,
-            @PackageManager.ComponentType int type) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.isComponentVisibleToInstantApp(component, type);
-        } finally {
-            current.release();
-        }
-    }
-    public boolean isImplicitImageCaptureIntentAndNotSetByDpcLocked(Intent intent,
-            int userId, String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.isImplicitImageCaptureIntentAndNotSetByDpcLocked(intent,
-                    userId, resolvedType, flags);
-        } finally {
-            current.release();
-        }
-    }
-    public boolean isInstantApp(String packageName, int userId) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.isInstantApp(packageName, userId);
-        } finally {
-            current.release();
-        }
-    }
-    public boolean isInstantAppInternal(String packageName, @UserIdInt int userId,
-            int callingUid) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.isInstantAppInternal(packageName, userId, callingUid);
-        } finally {
-            current.release();
-        }
-    }
-    public boolean isSameProfileGroup(@UserIdInt int callerUserId,
-            @UserIdInt int userId) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.isSameProfileGroup(callerUserId, userId);
-        } finally {
-            current.release();
-        }
-    }
-    public boolean shouldFilterApplication(@NonNull SharedUserSetting sus,
-            int callingUid, int userId) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.shouldFilterApplication(sus, callingUid, userId);
-        } finally {
-            current.release();
-        }
-    }
-    public boolean shouldFilterApplication(@Nullable PackageStateInternal ps,
-            int callingUid, @Nullable ComponentName component,
-            @PackageManager.ComponentType int componentType, int userId) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.shouldFilterApplication(ps, callingUid, component,
-                    componentType, userId);
-        } finally {
-            current.release();
-        }
-    }
-    public boolean shouldFilterApplication(@Nullable PackageStateInternal ps,
-            int callingUid, int userId) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.shouldFilterApplication(ps, callingUid, userId);
-        } finally {
-            current.release();
-        }
-    }
-    public int checkUidPermission(String permName, int uid) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.checkUidPermission(permName, uid);
-        } finally {
-            current.release();
-        }
-    }
-    public int getPackageUidInternal(String packageName,
-            @PackageManager.PackageInfoFlagsBits long flags, int userId, int callingUid) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.getPackageUidInternal(packageName, flags, userId,
-                    callingUid);
-        } finally {
-            current.release();
-        }
-    }
-    public long updateFlagsForApplication(long flags, int userId) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.updateFlagsForApplication(flags, userId);
-        } finally {
-            current.release();
-        }
-    }
-    public long updateFlagsForComponent(long flags, int userId) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.updateFlagsForComponent(flags, userId);
-        } finally {
-            current.release();
-        }
-    }
-    public long updateFlagsForPackage(long flags, int userId) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.updateFlagsForPackage(flags, userId);
-        } finally {
-            current.release();
-        }
-    }
-    public long updateFlagsForResolve(long flags, int userId, int callingUid,
-            boolean wantInstantApps, boolean isImplicitImageCaptureIntentAndNotSetByDpc) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.updateFlagsForResolve(flags, userId, callingUid,
-                    wantInstantApps, isImplicitImageCaptureIntentAndNotSetByDpc);
-        } finally {
-            current.release();
-        }
-    }
-    public long updateFlagsForResolve(long flags, int userId, int callingUid,
-            boolean wantInstantApps, boolean onlyExposedExplicitly,
-            boolean isImplicitImageCaptureIntentAndNotSetByDpc) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.updateFlagsForResolve(flags, userId, callingUid,
-                    wantInstantApps, onlyExposedExplicitly,
-                    isImplicitImageCaptureIntentAndNotSetByDpc);
-        } finally {
-            current.release();
-        }
-    }
-    public void dump(int type, FileDescriptor fd, PrintWriter pw, DumpState dumpState) {
-        ThreadComputer current = snapshot();
-        try {
-            current.mComputer.dump(type, fd, pw, dumpState);
-        } finally {
-            current.release();
-        }
-    }
-    public void enforceCrossUserOrProfilePermission(int callingUid, @UserIdInt int userId,
-            boolean requireFullPermission, boolean checkShell, String message) {
-        ThreadComputer current = snapshot();
-        try {
-            current.mComputer.enforceCrossUserOrProfilePermission(callingUid, userId,
-                    requireFullPermission, checkShell, message);
-        } finally {
-            current.release();
-        }
-    }
-    public void enforceCrossUserPermission(int callingUid, @UserIdInt int userId,
-            boolean requireFullPermission, boolean checkShell, String message) {
-        ThreadComputer current = snapshot();
-        try {
-            current.mComputer.enforceCrossUserPermission(callingUid, userId,
-                    requireFullPermission, checkShell, message);
-        } finally {
-            current.release();
-        }
-    }
-    public void enforceCrossUserPermission(int callingUid, @UserIdInt int userId,
-            boolean requireFullPermission, boolean checkShell,
-            boolean requirePermissionWhenSameUser, String message) {
-        ThreadComputer current = snapshot();
-        try {
-            current.mComputer.enforceCrossUserPermission(callingUid, userId,
-                    requireFullPermission, checkShell, requirePermissionWhenSameUser, message);
-        } finally {
-            current.release();
-        }
-    }
-    public PackageManagerService.FindPreferredActivityBodyResult findPreferredActivityInternal(
-            Intent intent, String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags,
-            List<ResolveInfo> query, boolean always, boolean removeMatches, boolean debug,
-            int userId, boolean queryMayBeFiltered) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.findPreferredActivityInternal(intent, resolvedType, flags,
-                    query, always, removeMatches, debug, userId, queryMayBeFiltered);
-        } finally {
-            current.release();
-        }
-    }
-    public ResolveInfo findPersistentPreferredActivityLP(Intent intent,
-            String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags,
-            List<ResolveInfo> query, boolean debug, int userId) {
-        ThreadComputer current = snapshot();
-        try {
-            return current.mComputer.findPersistentPreferredActivityLP(intent, resolvedType,
-                    flags, query, debug, userId);
-        } finally {
-            current.release();
-        }
-    }
-
-    @Override
-    public String[] getAllAvailablePackageNames() {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getAllAvailablePackageNames();
-        }
-    }
-
-    @Override
-    public PreferredIntentResolver getPreferredActivities(int userId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getPreferredActivities(userId);
-        }
-    }
-
-    @NonNull
-    @Override
-    public ArrayMap<String, ? extends PackageStateInternal> getPackageStates() {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getPackageStates();
-        }
-    }
-
-    @Nullable
-    @Override
-    public String getRenamedPackage(@NonNull String packageName) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getRenamedPackage(packageName);
-        }
-    }
-
-    @NonNull
-    @Override
-    public ArraySet<String> getNotifyPackagesForReplacedReceived(@NonNull String[] packages) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getNotifyPackagesForReplacedReceived(packages);
-        }
-    }
-
-    @Override
-    public int getPackageStartability(boolean safeMode, @NonNull String packageName, int callingUid,
-            @UserIdInt int userId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getPackageStartability(safeMode, packageName, callingUid,
-                    userId);
-        }
-    }
-
-    @Override
-    public boolean isPackageAvailable(String packageName, @UserIdInt int userId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.isPackageAvailable(packageName, userId);
-        }
-    }
-
-    @Override
-    public String[] currentToCanonicalPackageNames(String[] names) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.currentToCanonicalPackageNames(names);
-        }
-    }
-
-    @Override
-    public String[] canonicalToCurrentPackageNames(String[] names) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.canonicalToCurrentPackageNames(names);
-        }
-    }
-
-    @Override
-    public int[] getPackageGids(@NonNull String packageName,
-            @PackageManager.PackageInfoFlagsBits long flags, @UserIdInt int userId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getPackageGids(packageName, flags, userId);
-        }
-    }
-
-    @Override
-    public int getTargetSdkVersion(@NonNull String packageName) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getTargetSdkVersion(packageName);
-        }
-    }
-
-    @Override
-    public boolean activitySupportsIntent(@NonNull ComponentName resolveComponentName,
-            @NonNull ComponentName component, @NonNull Intent intent, String resolvedType) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.activitySupportsIntent(resolveComponentName, component, intent,
-                    resolvedType);
-        }
-    }
-
-    @Nullable
-    @Override
-    public ActivityInfo getReceiverInfo(@NonNull ComponentName component,
-            @PackageManager.ComponentInfoFlagsBits long flags, @UserIdInt int userId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getReceiverInfo(component, flags, userId);
-        }
-    }
-
-    @Nullable
-    @Override
-    public ParceledListSlice<SharedLibraryInfo> getSharedLibraries(@NonNull String packageName,
-            @PackageManager.PackageInfoFlagsBits long flags, @UserIdInt int userId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getSharedLibraries(packageName, flags, userId);
-        }
-    }
-
-    @Override
-    public boolean canRequestPackageInstalls(@NonNull String packageName, int callingUid,
-            @UserIdInt int userId, boolean throwIfPermNotDeclared) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.canRequestPackageInstalls(packageName, callingUid, userId,
-                    throwIfPermNotDeclared);
-        }
-    }
-
-    @Override
-    public boolean isInstallDisabledForPackage(@NonNull String packageName, int uid,
-            @UserIdInt int userId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.isInstallDisabledForPackage(packageName, uid, userId);
-        }
-    }
-
-    @Override
-    public List<VersionedPackage> getPackagesUsingSharedLibrary(@NonNull SharedLibraryInfo libInfo,
-            @PackageManager.PackageInfoFlagsBits long flags, int callingUid,
-            @UserIdInt int userId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getPackagesUsingSharedLibrary(libInfo, flags, callingUid,
-                    userId);
-        }
-    }
-
-    @Nullable
-    @Override
-    public ParceledListSlice<SharedLibraryInfo> getDeclaredSharedLibraries(
-            @NonNull String packageName, @PackageManager.PackageInfoFlagsBits long flags,
-            @UserIdInt int userId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getDeclaredSharedLibraries(packageName, flags, userId);
-        }
-    }
-
-    @Nullable
-    @Override
-    public ProviderInfo getProviderInfo(@NonNull ComponentName component,
-            @PackageManager.ComponentInfoFlagsBits long flags, @UserIdInt int userId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getProviderInfo(component, flags, userId);
-        }
-    }
-
-    @Nullable
-    @Override
-    public String[] getSystemSharedLibraryNames() {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getSystemSharedLibraryNames();
-        }
-    }
-
-    @Override
-    public int checkSignatures(@NonNull String pkg1,
-            @NonNull String pkg2) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.checkSignatures(pkg1, pkg2);
-        }
-    }
-
-    @Override
-    public int checkUidSignatures(int uid1, int uid2) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.checkUidSignatures(uid1, uid2);
-        }
-    }
-
-    @Override
-    public boolean hasSigningCertificate(@NonNull String packageName, @NonNull byte[] certificate,
-            int type) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.hasSigningCertificate(packageName, certificate, type);
-        }
-    }
-
-    @Override
-    public boolean hasUidSigningCertificate(int uid, @NonNull byte[] certificate, int type) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.hasUidSigningCertificate(uid, certificate, type);
-        }
-    }
-
-    @Override
-    public List<String> getAllPackages() {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getAllPackages();
-        }
-    }
-
-    @Nullable
-    @Override
-    public String getNameForUid(int uid) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getNameForUid(uid);
-        }
-    }
-
-    @Nullable
-    @Override
-    public String[] getNamesForUids(int[] uids) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getNamesForUids(uids);
-        }
-    }
-
-    @Override
-    public int getUidForSharedUser(@NonNull String sharedUserName) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getUidForSharedUser(sharedUserName);
-        }
-    }
-
-    @Override
-    public int getFlagsForUid(int uid) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getFlagsForUid(uid);
-        }
-    }
-
-    @Override
-    public int getPrivateFlagsForUid(int uid) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getPrivateFlagsForUid(uid);
-        }
-    }
-
-    @Override
-    public boolean isUidPrivileged(int uid) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.isUidPrivileged(uid);
-        }
-    }
-
-    @NonNull
-    @Override
-    public String[] getAppOpPermissionPackages(@NonNull String permissionName) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getAppOpPermissionPackages(permissionName);
-        }
-    }
-
-    @NonNull
-    @Override
-    public ParceledListSlice<PackageInfo> getPackagesHoldingPermissions(
-            @NonNull String[] permissions, @PackageManager.PackageInfoFlagsBits long flags,
-            @UserIdInt int userId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getPackagesHoldingPermissions(permissions, flags, userId);
-        }
-    }
-
-    @NonNull
-    @Override
-    public List<ApplicationInfo> getInstalledApplications(
-            @PackageManager.ApplicationInfoFlagsBits long flags, @UserIdInt int userId,
-            int callingUid) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getInstalledApplications(flags, userId, callingUid);
-        }
-    }
-
-    @Nullable
-    @Override
-    public ProviderInfo resolveContentProvider(@NonNull String name,
-            @PackageManager.ResolveInfoFlagsBits long flags, @UserIdInt int userId,
-            int callingUid) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.resolveContentProvider(name, flags, userId, callingUid);
-        }
-    }
-
-    @Nullable
-    @Override
-    public ProviderInfo getGrantImplicitAccessProviderInfo(int recipientUid,
-            @NonNull String visibleAuthority) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getGrantImplicitAccessProviderInfo(recipientUid,
-                    visibleAuthority);
-        }
-    }
-
-    @Override
-    public void querySyncProviders(boolean safeMode, @NonNull List<String> outNames,
-            @NonNull List<ProviderInfo> outInfo) {
-        try (ThreadComputer current = snapshot()) {
-            current.mComputer.querySyncProviders(safeMode, outNames, outInfo);
-        }
-    }
-
-    @NonNull
-    @Override
-    public ParceledListSlice<ProviderInfo> queryContentProviders(@Nullable String processName,
-            int uid, @PackageManager.ComponentInfoFlagsBits long flags,
-            @Nullable String metaDataKey) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.queryContentProviders(processName, uid, flags, metaDataKey);
-        }
-    }
-
-    @Nullable
-    @Override
-    public InstrumentationInfo getInstrumentationInfo(@NonNull ComponentName component, int flags) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getInstrumentationInfo(component, flags);
-        }
-    }
-
-    @NonNull
-    @Override
-    public ParceledListSlice<InstrumentationInfo> queryInstrumentation(
-            @NonNull String targetPackage, int flags) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.queryInstrumentation(targetPackage, flags);
-        }
-    }
-
-    @NonNull
-    @Override
-    public List<PackageStateInternal> findSharedNonSystemLibraries(
-            @NonNull PackageStateInternal pkgSetting) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.findSharedNonSystemLibraries(pkgSetting);
-        }
-    }
-
-    @Override
-    public boolean getApplicationHiddenSettingAsUser(@NonNull String packageName,
-            @UserIdInt int userId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getApplicationHiddenSettingAsUser(packageName, userId);
-        }
-    }
-
-    @Override
-    public boolean isPackageSuspendedForUser(@NonNull String packageName, @UserIdInt int userId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.isPackageSuspendedForUser(packageName, userId);
-        }
-    }
-
-    @Override
-    public boolean isSuspendingAnyPackages(@NonNull String suspendingPackage,
-            @UserIdInt int userId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.isSuspendingAnyPackages(suspendingPackage, userId);
-        }
-    }
-
-    @NonNull
-    @Override
-    public ParceledListSlice<IntentFilter> getAllIntentFilters(@NonNull String packageName) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getAllIntentFilters(packageName);
-        }
-    }
-
-    @Override
-    public boolean getBlockUninstallForUser(@NonNull String packageName, @UserIdInt int userId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getBlockUninstallForUser(packageName, userId);
-        }
-    }
-
-    @Nullable
-    @Override
-    public SparseArray<int[]> getBroadcastAllowList(@NonNull String packageName,
-            @UserIdInt int[] userIds, boolean isInstantApp) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getBroadcastAllowList(packageName, userIds, isInstantApp);
-        }
-    }
-
-    @Nullable
-    @Override
-    public String getInstallerPackageName(@NonNull String packageName) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getInstallerPackageName(packageName);
-        }
-    }
-
-    @Nullable
-    @Override
-    public InstallSourceInfo getInstallSourceInfo(@NonNull String packageName) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getInstallSourceInfo(packageName);
-        }
-    }
-
-    @Override
-    public int getApplicationEnabledSetting(@NonNull String packageName, @UserIdInt int userId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getApplicationEnabledSetting(packageName, userId);
-        }
-    }
-
-    @Override
-    public int getComponentEnabledSetting(@NonNull ComponentName component, int callingUid,
-            @UserIdInt int userId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getComponentEnabledSetting(component, callingUid, userId);
-        }
-    }
-
-    @Override
-    public int getComponentEnabledSettingInternal(@NonNull ComponentName component, int callingUid,
-            @UserIdInt int userId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getComponentEnabledSettingInternal(
-                    component, callingUid, userId);
-        }
-    }
-
-    @Override
-    public boolean isComponentEffectivelyEnabled(@NonNull ComponentInfo componentInfo,
-            @UserIdInt int userId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.isComponentEffectivelyEnabled(componentInfo, userId);
-        }
-    }
-
-    @Nullable
-    @Override
-    public KeySet getKeySetByAlias(@NonNull String packageName, @NonNull String alias) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getKeySetByAlias(packageName, alias);
-        }
-    }
-
-    @Nullable
-    @Override
-    public KeySet getSigningKeySet(@NonNull String packageName) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getSigningKeySet(packageName);
-        }
-    }
-
-    @Override
-    public boolean isPackageSignedByKeySet(@NonNull String packageName, @NonNull KeySet ks) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.isPackageSignedByKeySet(packageName, ks);
-        }
-    }
-
-    @Override
-    public boolean isPackageSignedByKeySetExactly(@NonNull String packageName, @NonNull KeySet ks) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.isPackageSignedByKeySetExactly(packageName, ks);
-        }
-    }
-
-    @Nullable
-    @Override
-    public int[] getVisibilityAllowList(@NonNull String packageName, @UserIdInt int userId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getVisibilityAllowList(packageName, userId);
-        }
-    }
-
-    @Override
-    public boolean canQueryPackage(int callingUid, @Nullable String targetPackageName) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.canQueryPackage(callingUid, targetPackageName);
-        }
-    }
-
-    @Override
-    public int getPackageUid(@NonNull String packageName,
-            @PackageManager.PackageInfoFlagsBits long flags, @UserIdInt int userId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getPackageUid(packageName, flags, userId);
-        }
-    }
-
-    @Override
-    public boolean canAccessComponent(int callingUid, @NonNull ComponentName component,
-            @UserIdInt int userId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.canAccessComponent(callingUid, component, userId);
-        }
-    }
-
-    @Override
-    public boolean isCallerInstallerOfRecord(@NonNull AndroidPackage pkg, int callingUid) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.isCallerInstallerOfRecord(pkg, callingUid);
-        }
-    }
-
-    @Override
-    public int getInstallReason(@NonNull String packageName, @UserIdInt int userId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getInstallReason(packageName, userId);
-        }
-    }
-
-    @Override
-    public boolean canPackageQuery(@NonNull String sourcePackageName,
-            @NonNull String targetPackageName, @UserIdInt int userId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.canPackageQuery(sourcePackageName, targetPackageName, userId);
-        }
-    }
-
-    @Override
-    public boolean canForwardTo(@NonNull Intent intent, @Nullable String resolvedType,
-            @UserIdInt int sourceUserId, @UserIdInt int targetUserId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.canForwardTo(intent, resolvedType, sourceUserId, targetUserId);
-        }
-    }
-
-    @NonNull
-    @Override
-    public List<ApplicationInfo> getPersistentApplications(boolean safeMode, int flags) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getPersistentApplications(safeMode, flags);
-        }
-    }
-
-    @NonNull
-    @Override
-    public SparseArray<String> getAppsWithSharedUserIds() {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getAppsWithSharedUserIds();
-        }
-    }
-
-    @NonNull
-    @Override
-    public String[] getSharedUserPackagesForPackage(@NonNull String packageName,
-            @UserIdInt int userId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getSharedUserPackagesForPackage(packageName, userId);
-        }
-    }
-
-    @NonNull
-    @Override
-    public Set<String> getUnusedPackages(long downgradeTimeThresholdMillis) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getUnusedPackages(downgradeTimeThresholdMillis);
-        }
-    }
-
-    @Nullable
-    @Override
-    public CharSequence getHarmfulAppWarning(@NonNull String packageName, @UserIdInt int userId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getHarmfulAppWarning(packageName, userId);
-        }
-    }
-
-    @NonNull
-    @Override
-    public String[] filterOnlySystemPackages(@Nullable String... pkgNames) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.filterOnlySystemPackages(pkgNames);
-        }
-    }
-
-    @NonNull
-    @Override
-    public List<AndroidPackage> getPackagesForAppId(int appId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getPackagesForAppId(appId);
-        }
-    }
-
-    @Override
-    public int getUidTargetSdkVersion(int uid) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getUidTargetSdkVersion(uid);
-        }
-    }
-
-    @Nullable
-    @Override
-    public ArrayMap<String, ProcessInfo> getProcessesForUid(int uid) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getProcessesForUid(uid);
-        }
-    }
-
-    @Override
-    public PackageStateInternal getPackageStateFiltered(@NonNull String packageName, int callingUid,
-            @UserIdInt int userId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getPackageStateFiltered(packageName, callingUid, userId);
-        }
-    }
-
-    @Override
-    public boolean getBlockUninstall(@UserIdInt int userId, @NonNull String packageName) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getBlockUninstall(userId, packageName);
-        }
-    }
-
-    @NonNull
-    @Override
-    public WatchedArrayMap<String, WatchedLongSparseArray<SharedLibraryInfo>> getSharedLibraries() {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getSharedLibraries();
-        }
-    }
-
-    @Nullable
-    @Override
-    public Pair<PackageStateInternal, SharedUserApi> getPackageOrSharedUser(int appId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getPackageOrSharedUser(appId);
-        }
-    }
-
-    @Nullable
-    @Override
-    public SharedUserApi getSharedUser(int sharedUserAppId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getSharedUser(sharedUserAppId);
-        }
-    }
-
-    @NonNull
-    @Override
-    public ArraySet<PackageStateInternal> getSharedUserPackages(int sharedUserAppId) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getSharedUserPackages(sharedUserAppId);
-        }
-    }
-
-    @NonNull
-    @Override
-    public ComponentResolverApi getComponentResolver() {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getComponentResolver();
-        }
-    }
-
-    @Nullable
-    @Override
-    public PackageStateInternal getDisabledSystemPackage(@NonNull String packageName) {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getDisabledSystemPackage(packageName);
-        }
-    }
-
-    @Nullable
-    @Override
-    public ResolveInfo getInstantAppInstallerInfo() {
-        try (ThreadComputer current = snapshot()) {
-            return current.mComputer.getInstantAppInstallerInfo();
-        }
-    }
-}
diff --git a/services/core/java/com/android/server/pm/CrossProfileAppsServiceImpl.java b/services/core/java/com/android/server/pm/CrossProfileAppsServiceImpl.java
index b307984..89f8be2 100644
--- a/services/core/java/com/android/server/pm/CrossProfileAppsServiceImpl.java
+++ b/services/core/java/com/android/server/pm/CrossProfileAppsServiceImpl.java
@@ -112,7 +112,9 @@
             String callingFeatureId,
             ComponentName component,
             @UserIdInt int userId,
-            boolean launchMainActivity) throws RemoteException {
+            boolean launchMainActivity,
+            IBinder targetTask,
+            Bundle options) throws RemoteException {
         Objects.requireNonNull(callingPackage);
         Objects.requireNonNull(component);
 
@@ -145,8 +147,12 @@
         if (launchMainActivity) {
             launchIntent.setAction(Intent.ACTION_MAIN);
             launchIntent.addCategory(Intent.CATEGORY_LAUNCHER);
-            launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
-                    | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
+            if (targetTask == null) {
+                launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
+                        | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
+            } else {
+                launchIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
+            }
             // Only package name is set here, as opposed to component name, because intent action
             // and category are ignored if component name is present while we are resolving intent.
             launchIntent.setPackage(component.getPackageName());
@@ -170,15 +176,20 @@
         }
         verifyActivityCanHandleIntentAndExported(launchIntent, component, callingUid, userId);
 
+        // Always show the cross profile animation
+        if (options == null) {
+            options = ActivityOptions.makeOpenCrossProfileAppsAnimation().toBundle();
+        } else {
+            options.putAll(ActivityOptions.makeOpenCrossProfileAppsAnimation().toBundle());
+        }
+
         launchIntent.setPackage(null);
         launchIntent.setComponent(component);
         mInjector.getActivityTaskManagerInternal().startActivityAsUser(
                 caller, callingPackage, callingFeatureId, launchIntent,
-                /* resultTo= */ null,
-                Intent.FLAG_ACTIVITY_NEW_TASK,
-                launchMainActivity
-                        ? ActivityOptions.makeOpenCrossProfileAppsAnimation().toBundle()
-                        : null,
+                targetTask,
+                /* startFlags= */ 0,
+                options,
                 userId);
     }
 
@@ -225,6 +236,13 @@
 
         verifyActivityCanHandleIntent(launchIntent, callingUid, userId);
 
+        // Always show the cross profile animation
+        if (options == null) {
+            options = ActivityOptions.makeOpenCrossProfileAppsAnimation().toBundle();
+        } else {
+            options.putAll(ActivityOptions.makeOpenCrossProfileAppsAnimation().toBundle());
+        }
+
         mInjector.getActivityTaskManagerInternal()
                 .startActivityAsUser(
                         caller,
diff --git a/services/core/java/com/android/server/pm/DeletePackageHelper.java b/services/core/java/com/android/server/pm/DeletePackageHelper.java
index a9dc0f3..0e1c1ad 100644
--- a/services/core/java/com/android/server/pm/DeletePackageHelper.java
+++ b/services/core/java/com/android/server/pm/DeletePackageHelper.java
@@ -147,6 +147,7 @@
         final SparseArray<TempUserState> priorUserStates;
         /** enabled state of the uninstalled application */
         synchronized (mPm.mLock) {
+            final Computer computer = mPm.snapshotComputer();
             uninstalledPs = mPm.mSettings.getPackageLPr(packageName);
             if (uninstalledPs == null) {
                 Slog.w(TAG, "Not removing non-existent package " + packageName);
@@ -170,10 +171,10 @@
             if (pkg != null) {
                 SharedLibraryInfo libraryInfo = null;
                 if (pkg.getStaticSharedLibName() != null) {
-                    libraryInfo = mPm.getSharedLibraryInfo(pkg.getStaticSharedLibName(),
+                    libraryInfo = computer.getSharedLibraryInfo(pkg.getStaticSharedLibName(),
                             pkg.getStaticSharedLibVersion());
                 } else if (pkg.getSdkLibName() != null) {
-                    libraryInfo = mPm.getSharedLibraryInfo(pkg.getSdkLibName(),
+                    libraryInfo = computer.getSharedLibraryInfo(pkg.getSdkLibName(),
                             pkg.getSdkLibVersionMajor());
                 }
 
@@ -183,7 +184,7 @@
                             continue;
                         }
                         List<VersionedPackage> libClientPackages =
-                                mPm.getPackagesUsingSharedLibrary(libraryInfo,
+                                computer.getPackagesUsingSharedLibrary(libraryInfo,
                                         MATCH_KNOWN_PACKAGES, Process.SYSTEM_UID, currUserId);
                         if (!ArrayUtils.isEmpty(libClientPackages)) {
                             Slog.w(TAG, "Not removing package " + pkg.getManifestPackageName()
@@ -243,9 +244,7 @@
         if (res) {
             final boolean killApp = (deleteFlags & PackageManager.DELETE_DONT_KILL_APP) == 0;
             info.sendPackageRemovedBroadcasts(killApp, removedBySystem);
-            if (disabledSystemPs != null) {
-                info.sendSystemPackageUpdatedBroadcasts(disabledSystemPs.getAppId());
-            }
+            info.sendSystemPackageUpdatedBroadcasts();
         }
 
         // Force a gc to clear up things.
@@ -456,11 +455,11 @@
         if (affectedUserIds == null) {
             affectedUserIds = mPm.resolveUserIds(userId);
         }
+        final Computer snapshot = mPm.snapshotComputer();
         for (final int affectedUserId : affectedUserIds) {
             if (hadSuspendAppsPermission.get(affectedUserId)) {
-                mPm.unsuspendForSuspendingPackage(mPm.snapshotComputer(), packageName,
-                        affectedUserId);
-                mPm.removeAllDistractingPackageRestrictions(affectedUserId);
+                mPm.unsuspendForSuspendingPackage(snapshot, packageName, affectedUserId);
+                mPm.removeAllDistractingPackageRestrictions(snapshot, affectedUserId);
             }
         }
 
@@ -603,9 +602,6 @@
         if (outInfo != null) {
             // Delete the updated package
             outInfo.mIsRemovedPackageSystemUpdate = true;
-            if (disabledPs.getAppId() != deletedPs.getAppId()) {
-                outInfo.mNewAppId = disabledPs.getAppId();
-            }
         }
 
         if (disabledPs.getVersionCode() < deletedPs.getVersionCode()
@@ -626,7 +622,8 @@
         final int callingUid = Binder.getCallingUid();
         mPm.mContext.enforceCallingOrSelfPermission(
                 android.Manifest.permission.DELETE_PACKAGES, null);
-        final boolean canViewInstantApps = mPm.canViewInstantApps(callingUid, userId);
+        final Computer snapshot = mPm.snapshotComputer();
+        final boolean canViewInstantApps = snapshot.canViewInstantApps(callingUid, userId);
         Preconditions.checkNotNull(versionedPackage);
         Preconditions.checkNotNull(observer);
         Preconditions.checkArgumentInRange(versionedPackage.getLongVersionCode(),
@@ -649,12 +646,13 @@
         }
 
         // Normalize package name to handle renamed packages and static libs
-        final String internalPackageName = mPm.resolveInternalPackageName(packageName, versionCode);
+        final String internalPackageName =
+                snapshot.resolveInternalPackageName(packageName, versionCode);
 
         final int uid = Binder.getCallingUid();
-        if (!isOrphaned(internalPackageName)
+        if (!isOrphaned(snapshot, internalPackageName)
                 && !allowSilentUninstall
-                && !isCallerAllowedToSilentlyUninstall(uid, internalPackageName)) {
+                && !isCallerAllowedToSilentlyUninstall(snapshot, uid, internalPackageName)) {
             mPm.mHandler.post(() -> {
                 try {
                     final Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);
@@ -685,8 +683,7 @@
             return;
         }
 
-        if (!deleteAllUsers && mPm.mIPackageManager
-                .getBlockUninstallForUser(internalPackageName, userId)) {
+        if (!deleteAllUsers && snapshot.getBlockUninstallForUser(internalPackageName, userId)) {
             mPm.mHandler.post(() -> {
                 try {
                     observer.onPackageDeleted(packageName,
@@ -761,44 +758,45 @@
         });
     }
 
-    private boolean isOrphaned(String packageName) {
-        final PackageStateInternal packageState = mPm.getPackageStateInternal(packageName);
+    private boolean isOrphaned(@NonNull Computer snapshot, String packageName) {
+        final PackageStateInternal packageState = snapshot.getPackageStateInternal(packageName);
         return packageState != null && packageState.getInstallSource().isOrphaned;
     }
 
-    private boolean isCallerAllowedToSilentlyUninstall(int callingUid, String pkgName) {
+    private boolean isCallerAllowedToSilentlyUninstall(@NonNull Computer snapshot, int callingUid,
+            String pkgName) {
         if (callingUid == Process.SHELL_UID || callingUid == Process.ROOT_UID
                 || UserHandle.getAppId(callingUid) == Process.SYSTEM_UID) {
             return true;
         }
         final int callingUserId = UserHandle.getUserId(callingUid);
         // If the caller installed the pkgName, then allow it to silently uninstall.
-        if (callingUid == mPm.mIPackageManager.getPackageUid(
-                mPm.mIPackageManager.getInstallerPackageName(pkgName), 0, callingUserId)) {
+        if (callingUid == snapshot.getPackageUid(snapshot.getInstallerPackageName(pkgName), 0,
+                callingUserId)) {
             return true;
         }
 
         // Allow package verifier to silently uninstall.
-        if (mPm.mRequiredVerifierPackage != null && callingUid == mPm.mIPackageManager
+        if (mPm.mRequiredVerifierPackage != null && callingUid == snapshot
                 .getPackageUid(mPm.mRequiredVerifierPackage, 0, callingUserId)) {
             return true;
         }
 
         // Allow package uninstaller to silently uninstall.
-        if (mPm.mRequiredUninstallerPackage != null && callingUid == mPm.mIPackageManager
+        if (mPm.mRequiredUninstallerPackage != null && callingUid == snapshot
                 .getPackageUid(mPm.mRequiredUninstallerPackage, 0, callingUserId)) {
             return true;
         }
 
         // Allow storage manager to silently uninstall.
-        if (mPm.mStorageManagerPackage != null && callingUid == mPm.mIPackageManager.getPackageUid(
+        if (mPm.mStorageManagerPackage != null && callingUid == snapshot.getPackageUid(
                 mPm.mStorageManagerPackage, 0, callingUserId)) {
             return true;
         }
 
         // Allow caller having MANAGE_PROFILE_AND_DEVICE_OWNERS permission to silently
         // uninstall for device owner provisioning.
-        return mPm.mIPackageManager.checkUidPermission(MANAGE_PROFILE_AND_DEVICE_OWNERS, callingUid)
+        return snapshot.checkUidPermission(MANAGE_PROFILE_AND_DEVICE_OWNERS, callingUid)
                 == PERMISSION_GRANTED;
     }
 
@@ -904,8 +902,8 @@
         int installedForUsersCount = 0;
         synchronized (mPm.mLock) {
             // Normalize package name to handle renamed packages and static libs
-            final String internalPkgName = mPm.resolveInternalPackageName(packageName,
-                    versionCode);
+            final String internalPkgName = mPm.snapshotComputer()
+                    .resolveInternalPackageName(packageName, versionCode);
             final PackageSetting ps = mPm.mSettings.getPackageLPr(internalPkgName);
             if (ps != null) {
                 int[] installedUsers = ps.queryInstalledUsers(mUserManagerInternal.getUserIds(),
diff --git a/services/core/java/com/android/server/pm/DexOptHelper.java b/services/core/java/com/android/server/pm/DexOptHelper.java
index 74ea7cc..50b2e23 100644
--- a/services/core/java/com/android/server/pm/DexOptHelper.java
+++ b/services/core/java/com/android/server/pm/DexOptHelper.java
@@ -266,8 +266,9 @@
             return;
         }
 
+        final Computer snapshot = mPm.snapshotComputer();
         List<PackageStateInternal> pkgSettings =
-                getPackagesForDexopt(mPm.getPackageStates().values(), mPm);
+                getPackagesForDexopt(snapshot.getPackageStates().values(), mPm);
 
         List<AndroidPackage> pkgs = new ArrayList<>(pkgSettings.size());
         for (int index = 0; index < pkgSettings.size(); index++) {
@@ -282,17 +283,19 @@
         final int elapsedTimeSeconds =
                 (int) TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - startTime);
 
+        final Computer newSnapshot = mPm.snapshotComputer();
+
         MetricsLogger.histogram(mPm.mContext, "opt_dialog_num_dexopted", stats[0]);
         MetricsLogger.histogram(mPm.mContext, "opt_dialog_num_skipped", stats[1]);
         MetricsLogger.histogram(mPm.mContext, "opt_dialog_num_failed", stats[2]);
-        MetricsLogger.histogram(
-                mPm.mContext, "opt_dialog_num_total", getOptimizablePackages().size());
+        MetricsLogger.histogram(mPm.mContext, "opt_dialog_num_total",
+                getOptimizablePackages(newSnapshot).size());
         MetricsLogger.histogram(mPm.mContext, "opt_dialog_time_s", elapsedTimeSeconds);
     }
 
-    public ArraySet<String> getOptimizablePackages() {
+    public ArraySet<String> getOptimizablePackages(@NonNull Computer snapshot) {
         ArraySet<String> pkgs = new ArraySet<>();
-        mPm.forEachPackageState(packageState -> {
+        mPm.forEachPackageState(snapshot, packageState -> {
             final AndroidPackage pkg = packageState.getPkg();
             if (pkg != null && mPm.mPackageDexOptimizer.canOptimizePackage(pkg)) {
                 pkgs.add(packageState.getPackageName());
@@ -302,10 +305,10 @@
     }
 
     /*package*/ boolean performDexOpt(DexoptOptions options) {
-        if (mPm.getInstantAppPackageName(Binder.getCallingUid()) != null) {
+        final Computer snapshot = mPm.snapshotComputer();
+        if (snapshot.getInstantAppPackageName(Binder.getCallingUid()) != null) {
             return false;
-        } else if (mPm.mIPackageManager.isInstantApp(options.getPackageName(),
-                UserHandle.getCallingUserId())) {
+        } else if (snapshot.isInstantApp(options.getPackageName(), UserHandle.getCallingUserId())) {
             return false;
         }
 
@@ -417,10 +420,10 @@
                 mPm.getDexManager().getPackageUseInfoOrDefault(p.getPackageName()), options);
     }
 
-    public void forceDexOpt(String packageName) {
+    public void forceDexOpt(@NonNull Computer snapshot, String packageName) {
         PackageManagerServiceUtils.enforceSystemOrRoot("forceDexOpt");
 
-        final PackageStateInternal packageState = mPm.getPackageStateInternal(packageName);
+        final PackageStateInternal packageState = snapshot.getPackageStateInternal(packageName);
         final AndroidPackage pkg = packageState == null ? null : packageState.getPkg();
         if (packageState == null || pkg == null) {
             throw new IllegalArgumentException("Unknown package: " + packageName);
@@ -485,19 +488,21 @@
 
         ArrayList<PackageStateInternal> sortTemp = new ArrayList<>(remainingPkgSettings.size());
 
+        final Computer snapshot = packageManagerService.snapshotComputer();
+
         // Give priority to core apps.
-        applyPackageFilter(pkgSetting -> pkgSetting.getPkg().isCoreApp(), result,
+        applyPackageFilter(snapshot, pkgSetting -> pkgSetting.getPkg().isCoreApp(), result,
                 remainingPkgSettings, sortTemp, packageManagerService);
 
         // Give priority to system apps that listen for pre boot complete.
         Intent intent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED);
         final ArraySet<String> pkgNames = getPackageNamesForIntent(intent, UserHandle.USER_SYSTEM);
-        applyPackageFilter(pkgSetting -> pkgNames.contains(pkgSetting.getPackageName()), result,
+        applyPackageFilter(snapshot, pkgSetting -> pkgNames.contains(pkgSetting.getPackageName()), result,
                 remainingPkgSettings, sortTemp, packageManagerService);
 
         // Give priority to apps used by other apps.
         DexManager dexManager = packageManagerService.getDexManager();
-        applyPackageFilter(pkgSetting ->
+        applyPackageFilter(snapshot, pkgSetting ->
                         dexManager.getPackageUseInfoOrDefault(pkgSetting.getPackageName())
                                 .isAnyCodePathUsedByOtherApps(),
                 result, remainingPkgSettings, sortTemp, packageManagerService);
@@ -535,7 +540,7 @@
             // No historical info. Take all.
             remainingPredicate = pkgSetting -> true;
         }
-        applyPackageFilter(remainingPredicate, result, remainingPkgSettings, sortTemp,
+        applyPackageFilter(snapshot, remainingPredicate, result, remainingPkgSettings, sortTemp,
                 packageManagerService);
 
         if (debug) {
@@ -550,7 +555,7 @@
     // package will be removed from {@code packages} and added to {@code result} with its
     // dependencies. If usage data is available, the positive packages will be sorted by usage
     // data (with {@code sortTemp} as temporary storage).
-    private static void applyPackageFilter(
+    private static void applyPackageFilter(@NonNull Computer snapshot,
             Predicate<PackageStateInternal> filter,
             Collection<PackageStateInternal> result,
             Collection<PackageStateInternal> packages,
@@ -568,8 +573,7 @@
         for (PackageStateInternal pkgSetting : sortTemp) {
             result.add(pkgSetting);
 
-            List<PackageStateInternal> deps =
-                    packageManagerService.findSharedNonSystemLibraries(pkgSetting);
+            List<PackageStateInternal> deps = snapshot.findSharedNonSystemLibraries(pkgSetting);
             if (!deps.isEmpty()) {
                 deps.removeAll(result);
                 result.addAll(deps);
diff --git a/services/core/java/com/android/server/pm/DomainVerificationConnection.java b/services/core/java/com/android/server/pm/DomainVerificationConnection.java
index db8c6dc..20e4dd8 100644
--- a/services/core/java/com/android/server/pm/DomainVerificationConnection.java
+++ b/services/core/java/com/android/server/pm/DomainVerificationConnection.java
@@ -21,7 +21,6 @@
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.annotation.UserIdInt;
-import android.content.pm.PackageManagerInternal;
 import android.os.Binder;
 import android.os.Message;
 import android.os.UserHandle;
@@ -35,13 +34,10 @@
 public final class DomainVerificationConnection implements DomainVerificationService.Connection,
         DomainVerificationProxyV1.Connection, DomainVerificationProxyV2.Connection {
     final PackageManagerService mPm;
-    final PackageManagerInternal mPmInternal;
     final UserManagerInternal mUmInternal;
 
-    // TODO(b/198166813): remove PMS dependency
     DomainVerificationConnection(PackageManagerService pm) {
         mPm = pm;
-        mPmInternal = mPm.mInjector.getLocalService(PackageManagerInternal.class);
         mUmInternal = mPm.mInjector.getLocalService(UserManagerInternal.class);
     }
 
@@ -82,18 +78,18 @@
     @Override
     public boolean isCallerPackage(int callingUid, @NonNull String packageName) {
         final int callingUserId = UserHandle.getUserId(callingUid);
-        return callingUid == mPmInternal.getPackageUid(packageName, 0, callingUserId);
+        return callingUid == mPm.snapshotComputer().getPackageUid(packageName, 0, callingUserId);
     }
 
     @Nullable
     @Override
     public AndroidPackage getPackage(@NonNull String packageName) {
-        return mPmInternal.getPackage(packageName);
+        return mPm.snapshotComputer().getPackage(packageName);
     }
 
     @Override
     public boolean filterAppAccess(String packageName, int callingUid, int userId) {
-        return mPmInternal.filterAppAccess(packageName, callingUid, userId);
+        return mPm.snapshotComputer().filterAppAccess(packageName, callingUid, userId);
     }
 
     @Override
@@ -108,6 +104,6 @@
 
     @NonNull
     public Computer snapshot() {
-        return (Computer) mPmInternal.snapshot();
+        return mPm.snapshotComputer();
     }
 }
diff --git a/services/core/java/com/android/server/pm/DumpHelper.java b/services/core/java/com/android/server/pm/DumpHelper.java
index 05ef3c4..f83ef5a 100644
--- a/services/core/java/com/android/server/pm/DumpHelper.java
+++ b/services/core/java/com/android/server/pm/DumpHelper.java
@@ -55,6 +55,7 @@
 
     @NeverCompile // Avoid size overhead of debugging code.
     public void doDump(FileDescriptor fd, PrintWriter pw, String[] args) {
+        final Computer snapshot = mPm.snapshotComputer();
         DumpState dumpState = new DumpState();
         ArraySet<String> permissionNames = null;
 
@@ -121,7 +122,7 @@
                 }
 
                 // Normalize package name to handle renamed packages and static libs
-                pkg = mPm.resolveInternalPackageName(pkg, PackageManager.VERSION_CODE_HIGHEST);
+                pkg = snapshot.resolveInternalPackageName(pkg, PackageManager.VERSION_CODE_HIGHEST);
 
                 pw.println(mPm.checkPermission(perm, pkg, user));
                 return;
@@ -243,7 +244,7 @@
 
         // Return if the package doesn't exist.
         if (packageName != null
-                && mPm.getPackageStateInternal(packageName) == null
+                && snapshot.getPackageStateInternal(packageName) == null
                 && !mPm.mApexManager.isApexPackage(packageName)) {
             pw.println("Unable to find package: " + packageName);
             return;
@@ -257,7 +258,7 @@
         if (!checkin
                 && dumpState.isDumping(DumpState.DUMP_VERSION)
                 && packageName == null) {
-            mPm.dumpComputer(DumpState.DUMP_VERSION, fd, pw, dumpState);
+            snapshot.dump(DumpState.DUMP_VERSION, fd, pw, dumpState);
         }
 
         if (!checkin
@@ -273,7 +274,7 @@
                 final String knownPackage = PackageManagerInternal.knownPackageToString(i);
                 ipw.print(knownPackage);
                 ipw.println(":");
-                final String[] pkgNames = mPm.getKnownPackageNamesInternal(i,
+                final String[] pkgNames = mPm.getKnownPackageNamesInternal(snapshot, i,
                         UserHandle.USER_SYSTEM);
                 ipw.increaseIndent();
                 if (ArrayUtils.isEmpty(pkgNames)) {
@@ -288,8 +289,6 @@
             ipw.decreaseIndent();
         }
 
-        final Computer snapshot = mPm.snapshotComputer();
-
         if (dumpState.isDumping(DumpState.DUMP_VERIFIERS)
                 && packageName == null) {
             final String requiredVerifierPackage = mPm.mRequiredVerifierPackage;
@@ -343,7 +342,7 @@
 
         if (dumpState.isDumping(DumpState.DUMP_LIBS)
                 && packageName == null) {
-            mPm.dumpComputer(DumpState.DUMP_LIBS, fd, pw, dumpState);
+            snapshot.dump(DumpState.DUMP_LIBS, fd, pw, dumpState);
         }
 
         if (dumpState.isDumping(DumpState.DUMP_FEATURES)
@@ -389,17 +388,17 @@
         }
 
         if (!checkin && dumpState.isDumping(DumpState.DUMP_PREFERRED)) {
-            mPm.dumpComputer(DumpState.DUMP_PREFERRED, fd, pw, dumpState);
+            snapshot.dump(DumpState.DUMP_PREFERRED, fd, pw, dumpState);
         }
 
         if (!checkin
                 && dumpState.isDumping(DumpState.DUMP_PREFERRED_XML)
                 && packageName == null) {
-            mPm.dumpComputer(DumpState.DUMP_PREFERRED_XML, fd, pw, dumpState);
+            snapshot.dump(DumpState.DUMP_PREFERRED_XML, fd, pw, dumpState);
         }
 
         if (!checkin && dumpState.isDumping(DumpState.DUMP_DOMAIN_PREFERRED)) {
-            mPm.dumpComputer(DumpState.DUMP_DOMAIN_PREFERRED, fd, pw, dumpState);
+            snapshot.dump(DumpState.DUMP_DOMAIN_PREFERRED, fd, pw, dumpState);
         }
 
         if (!checkin && dumpState.isDumping(DumpState.DUMP_PERMISSIONS)) {
@@ -429,7 +428,7 @@
 
         if (!checkin
                 && dumpState.isDumping(DumpState.DUMP_QUERIES)) {
-            mPm.dumpComputer(DumpState.DUMP_QUERIES, fd, pw, dumpState);
+            snapshot.dump(DumpState.DUMP_QUERIES, fd, pw, dumpState);
         }
 
         if (dumpState.isDumping(DumpState.DUMP_SHARED_USERS)) {
@@ -529,12 +528,12 @@
 
         if (!checkin
                 && dumpState.isDumping(DumpState.DUMP_DEXOPT)) {
-            mPm.dumpComputer(DumpState.DUMP_DEXOPT, fd, pw, dumpState);
+            snapshot.dump(DumpState.DUMP_DEXOPT, fd, pw, dumpState);
         }
 
         if (!checkin
                 && dumpState.isDumping(DumpState.DUMP_COMPILER_STATS)) {
-            mPm.dumpComputer(DumpState.DUMP_COMPILER_STATS, fd, pw, dumpState);
+            snapshot.dump(DumpState.DUMP_COMPILER_STATS, fd, pw, dumpState);
         }
 
         if (dumpState.isDumping(DumpState.DUMP_MESSAGES)
@@ -581,7 +580,7 @@
             pw.println("    Known digesters list flag: "
                     + PackageManagerService.getKnownDigestersList());
 
-            PerUidReadTimeouts[] items = mPm.getPerUidReadTimeouts();
+            PerUidReadTimeouts[] items = mPm.getPerUidReadTimeouts(snapshot);
             pw.println("    Timeouts (" + items.length + "):");
             for (PerUidReadTimeouts item : items) {
                 pw.print("        (");
diff --git a/services/core/java/com/android/server/pm/IPackageManagerBase.java b/services/core/java/com/android/server/pm/IPackageManagerBase.java
new file mode 100644
index 0000000..e1aee6d
--- /dev/null
+++ b/services/core/java/com/android/server/pm/IPackageManagerBase.java
@@ -0,0 +1,1189 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.pm;
+
+
+import static android.os.Trace.TRACE_TAG_PACKAGE_MANAGER;
+
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.annotation.SuppressLint;
+import android.annotation.UserIdInt;
+import android.app.role.RoleManager;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.pm.ActivityInfo;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.IPackageDeleteObserver;
+import android.content.pm.IPackageDeleteObserver2;
+import android.content.pm.IPackageInstaller;
+import android.content.pm.IPackageManager;
+import android.content.pm.IPackageStatsObserver;
+import android.content.pm.InstallSourceInfo;
+import android.content.pm.InstrumentationInfo;
+import android.content.pm.IntentFilterVerificationInfo;
+import android.content.pm.KeySet;
+import android.content.pm.ModuleInfo;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+import android.content.pm.ParceledListSlice;
+import android.content.pm.PermissionGroupInfo;
+import android.content.pm.PermissionInfo;
+import android.content.pm.ProviderInfo;
+import android.content.pm.ResolveInfo;
+import android.content.pm.ServiceInfo;
+import android.content.pm.SharedLibraryInfo;
+import android.content.pm.VersionedPackage;
+import android.content.pm.dex.IArtManager;
+import android.os.Binder;
+import android.os.Process;
+import android.os.RemoteException;
+import android.os.Trace;
+import android.os.UserHandle;
+import android.permission.PermissionManager;
+
+import com.android.internal.R;
+import com.android.internal.content.InstallLocationUtils;
+import com.android.internal.util.CollectionUtils;
+import com.android.server.pm.pkg.PackageStateInternal;
+import com.android.server.pm.verify.domain.DomainVerificationManagerInternal;
+import com.android.server.pm.verify.domain.proxy.DomainVerificationProxyV1;
+
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Contains all simply proxy methods which need a snapshot instance and just calls a method on it,
+ * with no additional logic. Separated with all methods marked final and deprecated to prevent their
+ * use from other methods which may need a snapshot for non-trivial reasons.
+ */
+public abstract class IPackageManagerBase extends IPackageManager.Stub {
+
+    @NonNull
+    private final PackageManagerService mService;
+
+    @NonNull
+    private final Context mContext;
+
+    @NonNull private final DexOptHelper mDexOptHelper;
+    @NonNull private final ModuleInfoProvider mModuleInfoProvider;
+    @NonNull private final PreferredActivityHelper mPreferredActivityHelper;
+    @NonNull private final ResolveIntentHelper mResolveIntentHelper;
+
+    @NonNull
+    private final DomainVerificationManagerInternal mDomainVerificationManager;
+
+    @NonNull
+    private final DomainVerificationConnection mDomainVerificationConnection;
+
+    @NonNull
+    private final PackageInstallerService mInstallerService;
+
+    @NonNull
+    private final PackageProperty mPackageProperty;
+
+    @NonNull
+    private final ComponentName mResolveComponentName;
+
+    @Nullable
+    private final ComponentName mInstantAppResolverSettingsComponent;
+
+    @NonNull
+    private final String mRequiredSupplementalProcessPackage;
+
+    @Nullable
+    private final String mServicesExtensionPackageName;
+
+    @Nullable
+    private final String mSharedSystemSharedLibraryPackageName;
+
+    public IPackageManagerBase(@NonNull PackageManagerService service, @NonNull Context context,
+            @NonNull DexOptHelper dexOptHelper, @NonNull ModuleInfoProvider moduleInfoProvider,
+            @NonNull PreferredActivityHelper preferredActivityHelper,
+            @NonNull ResolveIntentHelper resolveIntentHelper,
+            @NonNull DomainVerificationManagerInternal domainVerificationManager,
+            @NonNull DomainVerificationConnection domainVerificationConnection,
+            @NonNull PackageInstallerService installerService,
+            @NonNull PackageProperty packageProperty, @NonNull ComponentName resolveComponentName,
+            @Nullable ComponentName instantAppResolverSettingsComponent,
+            @NonNull String requiredSupplementalProcessPackage,
+            @Nullable String servicesExtensionPackageName,
+            @Nullable String sharedSystemSharedLibraryPackageName) {
+        mService = service;
+        mContext = context;
+        mDexOptHelper = dexOptHelper;
+        mModuleInfoProvider = moduleInfoProvider;
+        mPreferredActivityHelper = preferredActivityHelper;
+        mResolveIntentHelper = resolveIntentHelper;
+        mDomainVerificationManager = domainVerificationManager;
+        mDomainVerificationConnection = domainVerificationConnection;
+        mInstallerService = installerService;
+        mPackageProperty = packageProperty;
+        mResolveComponentName = resolveComponentName;
+        mInstantAppResolverSettingsComponent = instantAppResolverSettingsComponent;
+        mRequiredSupplementalProcessPackage = requiredSupplementalProcessPackage;
+        mServicesExtensionPackageName = servicesExtensionPackageName;
+        mSharedSystemSharedLibraryPackageName = sharedSystemSharedLibraryPackageName;
+    }
+
+    protected Computer snapshot() {
+        return mService.snapshotComputer();
+    }
+
+    @Override
+    @Deprecated
+    public final boolean activitySupportsIntent(ComponentName component, Intent intent,
+            String resolvedType) {
+        return snapshot().activitySupportsIntent(mResolveComponentName, component, intent,
+                resolvedType);
+    }
+
+    @Override
+    @Deprecated
+    public final void addCrossProfileIntentFilter(IntentFilter intentFilter, String ownerPackage,
+            int sourceUserId, int targetUserId, int flags) {
+        mService.addCrossProfileIntentFilter(snapshot(),
+                new WatchedIntentFilter(intentFilter), ownerPackage, sourceUserId, targetUserId,
+                flags);
+    }
+
+    // NOTE: Can't remove due to unsupported app usage
+    @Override
+    @Deprecated
+    public final boolean addPermission(PermissionInfo info) {
+        // Because this is accessed via the package manager service AIDL,
+        // go through the permission manager service AIDL
+        return mContext.getSystemService(PermissionManager.class).addPermission(info, false);
+    }
+
+    // NOTE: Can't remove due to unsupported app usage
+    @Override
+    @Deprecated
+    public final boolean addPermissionAsync(PermissionInfo info) {
+        // Because this is accessed via the package manager service AIDL,
+        // go through the permission manager service AIDL
+        return mContext.getSystemService(PermissionManager.class).addPermission(info, true);
+    }
+
+    @Override
+    @Deprecated
+    public final void addPersistentPreferredActivity(IntentFilter filter, ComponentName activity,
+            int userId) {
+        mPreferredActivityHelper.addPersistentPreferredActivity(new WatchedIntentFilter(filter),
+                activity, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final void addPreferredActivity(IntentFilter filter, int match,
+            ComponentName[] set, ComponentName activity, int userId, boolean removeExisting) {
+        mPreferredActivityHelper.addPreferredActivity(snapshot(),
+                new WatchedIntentFilter(filter), match, set, activity, true, userId,
+                "Adding preferred", removeExisting);
+    }
+
+    /*
+     * Returns if intent can be forwarded from the sourceUserId to the targetUserId
+     */
+    @Override
+    @Deprecated
+    public final boolean canForwardTo(@NonNull Intent intent, @Nullable String resolvedType,
+            @UserIdInt int sourceUserId, @UserIdInt int targetUserId) {
+        return snapshot().canForwardTo(intent, resolvedType, sourceUserId, targetUserId);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean canRequestPackageInstalls(String packageName, int userId) {
+        return snapshot().canRequestPackageInstalls(packageName, Binder.getCallingUid(), userId,
+                true /* throwIfPermNotDeclared*/);
+    }
+
+    @Override
+    @Deprecated
+    public final String[] canonicalToCurrentPackageNames(String[] names) {
+        return snapshot().canonicalToCurrentPackageNames(names);
+    }
+
+    // NOTE: Can't remove due to unsupported app usage
+    @Override
+    @Deprecated
+    public final int checkPermission(String permName, String pkgName, int userId) {
+        return mService.checkPermission(permName, pkgName, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final int checkSignatures(@NonNull String pkg1, @NonNull String pkg2) {
+        return snapshot().checkSignatures(pkg1, pkg2);
+    }
+
+    @Override
+    @Deprecated
+    public final int checkUidPermission(String permName, int uid) {
+        return snapshot().checkUidPermission(permName, uid);
+    }
+
+    @Override
+    @Deprecated
+    public final int checkUidSignatures(int uid1, int uid2) {
+        return snapshot().checkUidSignatures(uid1, uid2);
+    }
+
+    @Override
+    @Deprecated
+    public final void clearPackagePersistentPreferredActivities(String packageName, int userId) {
+        mPreferredActivityHelper.clearPackagePersistentPreferredActivities(packageName, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final void clearPackagePreferredActivities(String packageName) {
+        mPreferredActivityHelper.clearPackagePreferredActivities(snapshot(),
+                packageName);
+    }
+
+    @Override
+    @Deprecated
+    public final String[] currentToCanonicalPackageNames(String[] names) {
+        return snapshot().currentToCanonicalPackageNames(names);
+    }
+
+    @Override
+    @Deprecated
+    public final void deleteExistingPackageAsUser(VersionedPackage versionedPackage,
+            final IPackageDeleteObserver2 observer, final int userId) {
+        mService.deleteExistingPackageAsUser(versionedPackage, observer, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final void deletePackageAsUser(String packageName, int versionCode,
+            IPackageDeleteObserver observer, int userId, int flags) {
+        deletePackageVersioned(new VersionedPackage(packageName, versionCode),
+                new PackageManager.LegacyPackageDeleteObserver(observer).getBinder(), userId,
+                flags);
+    }
+
+    @Override
+    @Deprecated
+    public final void deletePackageVersioned(VersionedPackage versionedPackage,
+            final IPackageDeleteObserver2 observer, final int userId, final int deleteFlags) {
+        mService.deletePackageVersioned(versionedPackage, observer, userId, deleteFlags);
+    }
+
+    @Override
+    @Deprecated
+    public final ResolveInfo findPersistentPreferredActivity(Intent intent, int userId) {
+        return mPreferredActivityHelper.findPersistentPreferredActivity(snapshot(), intent, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final void forceDexOpt(String packageName) {
+        mDexOptHelper.forceDexOpt(snapshot(), packageName);
+    }
+
+    @Override
+    @Deprecated
+    public final ActivityInfo getActivityInfo(ComponentName component,
+            @PackageManager.ComponentInfoFlagsBits long flags, int userId) {
+        return snapshot().getActivityInfo(component, flags, userId);
+    }
+
+    @NonNull
+    @Override
+    @Deprecated
+    public final ParceledListSlice<IntentFilter> getAllIntentFilters(@NonNull String packageName) {
+        return snapshot().getAllIntentFilters(packageName);
+    }
+
+    @Override
+    @Deprecated
+    public final List<String> getAllPackages() {
+        return snapshot().getAllPackages();
+    }
+
+    // NOTE: Can't remove due to unsupported app usage
+    @NonNull
+    @Override
+    @Deprecated
+    public final String[] getAppOpPermissionPackages(@NonNull String permissionName) {
+        return snapshot().getAppOpPermissionPackages(permissionName);
+    }
+
+    @Override
+    @Deprecated
+    public final String getAppPredictionServicePackageName() {
+        return mService.mAppPredictionServicePackage;
+    }
+
+    @PackageManager.EnabledState
+    @Override
+    @Deprecated
+    public final int getApplicationEnabledSetting(@NonNull String packageName,
+            @UserIdInt int userId) {
+        return snapshot().getApplicationEnabledSetting(packageName, userId);
+    }
+
+    /**
+     * Returns true if application is not found or there was an error. Otherwise it returns the
+     * hidden state of the package for the given user.
+     */
+    @Override
+    @Deprecated
+    public final boolean getApplicationHiddenSettingAsUser(@NonNull String packageName,
+            @UserIdInt int userId) {
+        return snapshot().getApplicationHiddenSettingAsUser(packageName, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final ApplicationInfo getApplicationInfo(String packageName,
+            @PackageManager.ApplicationInfoFlagsBits long flags, int userId) {
+        return snapshot().getApplicationInfo(packageName, flags, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final IArtManager getArtManager() {
+        return mService.mArtManagerService;
+    }
+
+    @Override
+    @Deprecated
+    public final @Nullable
+    String getAttentionServicePackageName() {
+        return mService.ensureSystemPackageName(snapshot(),
+                mService.getPackageFromComponentString(R.string.config_defaultAttentionService));
+    }
+
+    @Override
+    @Deprecated
+    public final boolean getBlockUninstallForUser(@NonNull String packageName,
+            @UserIdInt int userId) {
+        return snapshot().getBlockUninstallForUser(packageName, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final int getComponentEnabledSetting(@NonNull ComponentName component, int userId) {
+        return snapshot().getComponentEnabledSetting(component, Binder.getCallingUid(), userId);
+    }
+
+    @Override
+    @Deprecated
+    public final String getContentCaptureServicePackageName() {
+        return mService.ensureSystemPackageName(snapshot(),
+                mService.getPackageFromComponentString(
+                        R.string.config_defaultContentCaptureService));
+    }
+
+    @Nullable
+    @Override
+    @Deprecated
+    public final ParceledListSlice<SharedLibraryInfo> getDeclaredSharedLibraries(
+            @NonNull String packageName, @PackageManager.PackageInfoFlagsBits long flags,
+            @NonNull int userId) {
+        return snapshot().getDeclaredSharedLibraries(packageName, flags, userId);
+    }
+
+    /**
+     * Non-Binder method, support for the backup/restore mechanism: write the default browser (etc)
+     * settings in its canonical XML format.  Returns the default browser XML representation as a
+     * byte array, or null if there is none.
+     */
+    @Override
+    @Deprecated
+    public final byte[] getDefaultAppsBackup(int userId) {
+        return mPreferredActivityHelper.getDefaultAppsBackup(userId);
+    }
+
+    @Override
+    @Deprecated
+    public final String getDefaultTextClassifierPackageName() {
+        return mService.mDefaultTextClassifierPackage;
+    }
+
+    @Override
+    @Deprecated
+    public final int getFlagsForUid(int uid) {
+        return snapshot().getFlagsForUid(uid);
+    }
+
+    @Nullable
+    @Override
+    @Deprecated
+    public final CharSequence getHarmfulAppWarning(@NonNull String packageName,
+            @UserIdInt int userId) {
+        return snapshot().getHarmfulAppWarning(packageName, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final ComponentName getHomeActivities(List<ResolveInfo> allHomeCandidates) {
+        final Computer snapshot = snapshot();
+        if (snapshot.getInstantAppPackageName(Binder.getCallingUid()) != null) {
+            return null;
+        }
+        return snapshot.getHomeActivitiesAsUser(allHomeCandidates,
+                UserHandle.getCallingUserId());
+    }
+
+    @Deprecated
+    public final String getIncidentReportApproverPackageName() {
+        return mService.mIncidentReportApproverPackage;
+    }
+
+    @Override
+    @Deprecated
+    public final int getInstallLocation() {
+        // allow instant app access
+        return android.provider.Settings.Global.getInt(mContext.getContentResolver(),
+                android.provider.Settings.Global.DEFAULT_INSTALL_LOCATION,
+                InstallLocationUtils.APP_INSTALL_AUTO);
+    }
+
+    @PackageManager.InstallReason
+    @Override
+    @Deprecated
+    public final int getInstallReason(@NonNull String packageName, @UserIdInt int userId) {
+        return snapshot().getInstallReason(packageName, userId);
+    }
+
+    @Override
+    @Nullable
+    @Deprecated
+    public final InstallSourceInfo getInstallSourceInfo(@NonNull String packageName) {
+        return snapshot().getInstallSourceInfo(packageName);
+    }
+
+    @Override
+    @Deprecated
+    public final ParceledListSlice<ApplicationInfo> getInstalledApplications(
+            @PackageManager.ApplicationInfoFlagsBits long flags, int userId) {
+        final int callingUid = Binder.getCallingUid();
+        return new ParceledListSlice<>(
+                snapshot().getInstalledApplications(flags, userId, callingUid));
+    }
+
+    @Override
+    @Deprecated
+    public final List<ModuleInfo> getInstalledModules(int flags) {
+        return mModuleInfoProvider.getInstalledModules(flags);
+    }
+
+    @Override
+    @Deprecated
+    public final ParceledListSlice<PackageInfo> getInstalledPackages(
+            @PackageManager.PackageInfoFlagsBits long flags, int userId) {
+        return snapshot().getInstalledPackages(flags, userId);
+    }
+
+    @Nullable
+    @Override
+    @Deprecated
+    public final String getInstallerPackageName(@NonNull String packageName) {
+        return snapshot().getInstallerPackageName(packageName);
+    }
+
+    @Override
+    @Deprecated
+    public final ComponentName getInstantAppInstallerComponent() {
+        final Computer snapshot = snapshot();
+        if (snapshot.getInstantAppPackageName(Binder.getCallingUid()) != null) {
+            return null;
+        }
+        return snapshot.getInstantAppInstallerComponent();
+    }
+
+    @Override
+    @Deprecated
+    public final @Nullable
+    ComponentName getInstantAppResolverComponent() {
+        final Computer snapshot = snapshot();
+        if (snapshot.getInstantAppPackageName(Binder.getCallingUid()) != null) {
+            return null;
+        }
+        return mService.getInstantAppResolver(snapshot);
+    }
+
+    @Override
+    @Deprecated
+    public final ComponentName getInstantAppResolverSettingsComponent() {
+        return mInstantAppResolverSettingsComponent;
+    }
+
+    @Nullable
+    @Override
+    @Deprecated
+    public final InstrumentationInfo getInstrumentationInfo(@NonNull ComponentName component,
+            int flags) {
+        return snapshot().getInstrumentationInfo(component, flags);
+    }
+
+    @Override
+    @Deprecated
+    public final @NonNull
+    ParceledListSlice<IntentFilterVerificationInfo>
+    getIntentFilterVerifications(String packageName) {
+        return ParceledListSlice.emptyList();
+    }
+
+    @Override
+    @Deprecated
+    public final int getIntentVerificationStatus(String packageName, int userId) {
+        return mDomainVerificationManager.getLegacyState(packageName, userId);
+    }
+
+    @Nullable
+    @Override
+    @Deprecated
+    public final KeySet getKeySetByAlias(@NonNull String packageName, @NonNull String alias) {
+        return snapshot().getKeySetByAlias(packageName, alias);
+    }
+
+    @Override
+    @Deprecated
+    public final ModuleInfo getModuleInfo(String packageName,
+            @PackageManager.ModuleInfoFlags int flags) {
+        return mModuleInfoProvider.getModuleInfo(packageName, flags);
+    }
+
+    @Nullable
+    @Override
+    @Deprecated
+    public final String getNameForUid(int uid) {
+        return snapshot().getNameForUid(uid);
+    }
+
+    @Nullable
+    @Override
+    @Deprecated
+    public final String[] getNamesForUids(@NonNull int[] uids) {
+        return snapshot().getNamesForUids(uids);
+    }
+
+    @Override
+    @Deprecated
+    public final int[] getPackageGids(String packageName,
+            @PackageManager.PackageInfoFlagsBits long flags, int userId) {
+        return snapshot().getPackageGids(packageName, flags, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final PackageInfo getPackageInfo(String packageName,
+            @PackageManager.PackageInfoFlagsBits long flags, int userId) {
+        return snapshot().getPackageInfo(packageName, flags, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final PackageInfo getPackageInfoVersioned(VersionedPackage versionedPackage,
+            @PackageManager.PackageInfoFlagsBits long flags, int userId) {
+        return snapshot().getPackageInfoInternal(versionedPackage.getPackageName(),
+                versionedPackage.getLongVersionCode(), flags, Binder.getCallingUid(), userId);
+    }
+
+    @Override
+    @Deprecated
+    public final IPackageInstaller getPackageInstaller() {
+        // Return installer service for internal calls.
+        if (PackageManagerServiceUtils.isSystemOrRoot()) {
+            return mInstallerService;
+        }
+        final Computer snapshot = snapshot();
+        // Return null for InstantApps.
+        if (snapshot.getInstantAppPackageName(Binder.getCallingUid()) != null) {
+            return null;
+        }
+        return mInstallerService;
+    }
+
+    @Override
+    @Deprecated
+    public final void getPackageSizeInfo(final String packageName, int userId,
+            final IPackageStatsObserver observer) {
+        throw new UnsupportedOperationException(
+                "Shame on you for calling the hidden API getPackageSizeInfo(). Shame!");
+    }
+
+    @Override
+    @Deprecated
+    public final int getPackageUid(@NonNull String packageName,
+            @PackageManager.PackageInfoFlagsBits long flags, @UserIdInt int userId) {
+        return snapshot().getPackageUid(packageName, flags, userId);
+    }
+
+    /**
+     * <em>IMPORTANT:</em> Not all packages returned by this method may be known
+     * to the system. There are two conditions in which this may occur:
+     * <ol>
+     *   <li>The package is on adoptable storage and the device has been removed</li>
+     *   <li>The package is being removed and the internal structures are partially updated</li>
+     * </ol>
+     * The second is an artifact of the current data structures and should be fixed. See
+     * b/111075456 for one such instance.
+     * This binder API is cached.  If the algorithm in this method changes,
+     * or if the underlying objecs (as returned by getSettingLPr()) change
+     * then the logic that invalidates the cache must be revisited.  See
+     * calls to invalidateGetPackagesForUidCache() to locate the points at
+     * which the cache is invalidated.
+     */
+    @Override
+    @Deprecated
+    public final String[] getPackagesForUid(int uid) {
+        final int callingUid = Binder.getCallingUid();
+        final int userId = UserHandle.getUserId(uid);
+        snapshot().enforceCrossUserOrProfilePermission(callingUid, userId,
+                /* requireFullPermission */ false,
+                /* checkShell */ false, "getPackagesForUid");
+        return snapshot().getPackagesForUid(uid);
+    }
+
+    @Override
+    @Deprecated
+    public final ParceledListSlice<PackageInfo> getPackagesHoldingPermissions(
+            @NonNull String[] permissions, @PackageManager.PackageInfoFlagsBits long flags,
+            @UserIdInt int userId) {
+        return snapshot().getPackagesHoldingPermissions(permissions, flags, userId);
+    }
+
+    // NOTE: Can't remove due to unsupported app usage
+    @Override
+    @Deprecated
+    public final PermissionGroupInfo getPermissionGroupInfo(String groupName, int flags) {
+        return mService.getPermissionGroupInfo(groupName, flags);
+    }
+
+    @Override
+    @Deprecated
+    public final @NonNull
+    ParceledListSlice<ApplicationInfo> getPersistentApplications(int flags) {
+        final Computer snapshot = snapshot();
+        if (snapshot.getInstantAppPackageName(Binder.getCallingUid()) != null) {
+            return ParceledListSlice.emptyList();
+        }
+        return new ParceledListSlice<>(snapshot.getPersistentApplications(isSafeMode(), flags));
+    }
+
+    @Override
+    @Deprecated
+    public final int getPreferredActivities(List<IntentFilter> outFilters,
+            List<ComponentName> outActivities, String packageName) {
+        return mPreferredActivityHelper.getPreferredActivities(snapshot(), outFilters,
+                outActivities, packageName);
+    }
+
+    /**
+     * Non-Binder method, support for the backup/restore mechanism: write the full set of preferred
+     * activities in its canonical XML format.  Returns the XML output as a byte array, or null if
+     * there is none.
+     */
+    @Override
+    @Deprecated
+    public final byte[] getPreferredActivityBackup(int userId) {
+        return mPreferredActivityHelper.getPreferredActivityBackup(userId);
+    }
+
+    @Override
+    @Deprecated
+    public final int getPrivateFlagsForUid(int uid) {
+        return snapshot().getPrivateFlagsForUid(uid);
+    }
+
+    @Override
+    @Deprecated
+    public final PackageManager.Property getProperty(String propertyName, String packageName,
+            String className) {
+        Objects.requireNonNull(propertyName);
+        Objects.requireNonNull(packageName);
+        PackageStateInternal packageState = snapshot().getPackageStateFiltered(packageName,
+                Binder.getCallingUid(), UserHandle.getCallingUserId());
+        if (packageState == null) {
+            return null;
+        }
+        return mPackageProperty.getProperty(propertyName, packageName, className);
+    }
+
+    @Nullable
+    @Override
+    @Deprecated
+    public final ProviderInfo getProviderInfo(@NonNull ComponentName component,
+            @PackageManager.ComponentInfoFlagsBits long flags, @UserIdInt int userId) {
+        return snapshot().getProviderInfo(component, flags, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final ActivityInfo getReceiverInfo(ComponentName component,
+            @PackageManager.ComponentInfoFlagsBits long flags, int userId) {
+        return snapshot().getReceiverInfo(component, flags, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final @Nullable
+    String getRotationResolverPackageName() {
+        return mService.ensureSystemPackageName(snapshot(),
+                mService.getPackageFromComponentString(
+                        R.string.config_defaultRotationResolverService));
+    }
+
+    @Nullable
+    @Override
+    @Deprecated
+    public final ServiceInfo getServiceInfo(@NonNull ComponentName component,
+            @PackageManager.ComponentInfoFlagsBits long flags, @UserIdInt int userId) {
+        return snapshot().getServiceInfo(component, flags, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final @NonNull
+    String getServicesSystemSharedLibraryPackageName() {
+        return mServicesExtensionPackageName;
+    }
+
+    @Override
+    @Deprecated
+    public final String getSetupWizardPackageName() {
+        if (Binder.getCallingUid() != Process.SYSTEM_UID) {
+            throw new SecurityException("Non-system caller");
+        }
+        return mService.mSetupWizardPackage;
+    }
+
+    @Override
+    @Deprecated
+    public final ParceledListSlice<SharedLibraryInfo> getSharedLibraries(String packageName,
+            @PackageManager.PackageInfoFlagsBits long flags, int userId) {
+        return snapshot().getSharedLibraries(packageName, flags, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final @NonNull
+    String getSharedSystemSharedLibraryPackageName() {
+        return mSharedSystemSharedLibraryPackageName;
+    }
+
+    @Nullable
+    @Override
+    @Deprecated
+    public final KeySet getSigningKeySet(@NonNull String packageName) {
+        return snapshot().getSigningKeySet(packageName);
+    }
+
+    @Override
+    @Deprecated
+    public final String getSdkSandboxPackageName() {
+        return mService.getSdkSandboxPackageName();
+    }
+
+    @Override
+    @Deprecated
+    public final String getSystemCaptionsServicePackageName() {
+        return mService.ensureSystemPackageName(snapshot(),
+                mService.getPackageFromComponentString(
+                        R.string.config_defaultSystemCaptionsService));
+    }
+
+    @Nullable
+    @Override
+    @Deprecated
+    public final String[] getSystemSharedLibraryNames() {
+        return snapshot().getSystemSharedLibraryNames();
+    }
+
+    @Override
+    @Deprecated
+    public final String getSystemTextClassifierPackageName() {
+        return mService.mSystemTextClassifierPackageName;
+    }
+
+    @Override
+    @Deprecated
+    public final int getTargetSdkVersion(@NonNull String packageName) {
+        return snapshot().getTargetSdkVersion(packageName);
+    }
+
+    @Override
+    @Deprecated
+    public final int getUidForSharedUser(@NonNull String sharedUserName) {
+        return snapshot().getUidForSharedUser(sharedUserName);
+    }
+
+    @SuppressLint("MissingPermission")
+    @Override
+    @Deprecated
+    public final String getWellbeingPackageName() {
+        final long identity = Binder.clearCallingIdentity();
+        try {
+            return CollectionUtils.firstOrNull(
+                    mContext.getSystemService(RoleManager.class).getRoleHolders(
+                            RoleManager.ROLE_SYSTEM_WELLBEING));
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+    }
+
+    // NOTE: Can't remove due to unsupported app usage
+    @SuppressLint("MissingPermission")
+    @Override
+    @Deprecated
+    public final void grantRuntimePermission(String packageName, String permName,
+            final int userId) {
+        // Because this is accessed via the package manager service AIDL,
+        // go through the permission manager service AIDL
+        mContext.getSystemService(PermissionManager.class)
+                .grantRuntimePermission(packageName, permName, UserHandle.of(userId));
+    }
+
+    @Override
+    @Deprecated
+    public final boolean hasSigningCertificate(@NonNull String packageName,
+            @NonNull byte[] certificate,
+            @PackageManager.CertificateInputType int type) {
+        return snapshot().hasSigningCertificate(packageName, certificate, type);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean hasSystemFeature(String name, int version) {
+        return mService.hasSystemFeature(name, version);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean hasSystemUidErrors() {
+        // allow instant applications
+        return false;
+    }
+
+    @Override
+    @Deprecated
+    public final boolean hasUidSigningCertificate(int uid, @NonNull byte[] certificate,
+            @PackageManager.CertificateInputType int type) {
+        return snapshot().hasUidSigningCertificate(uid, certificate, type);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean isDeviceUpgrading() {
+        return mService.isDeviceUpgrading();
+    }
+
+    @Override
+    @Deprecated
+    public final boolean isFirstBoot() {
+        return mService.isFirstBoot();
+    }
+
+    @Override
+    @Deprecated
+    public final boolean isInstantApp(String packageName, int userId) {
+        return snapshot().isInstantApp(packageName, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean isOnlyCoreApps() {
+        return mService.isOnlyCoreApps();
+    }
+
+    @Override
+    @Deprecated
+    public final boolean isPackageAvailable(String packageName, int userId) {
+        return snapshot().isPackageAvailable(packageName, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean isPackageDeviceAdminOnAnyUser(String packageName) {
+        return mService.isPackageDeviceAdminOnAnyUser(snapshot(),
+                packageName);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean isPackageSignedByKeySet(@NonNull String packageName, @NonNull KeySet ks) {
+        return snapshot().isPackageSignedByKeySet(packageName, ks);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean isPackageSignedByKeySetExactly(@NonNull String packageName,
+            @NonNull KeySet ks) {
+        return snapshot().isPackageSignedByKeySetExactly(packageName, ks);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean isPackageSuspendedForUser(@NonNull String packageName,
+            @UserIdInt int userId) {
+        return snapshot().isPackageSuspendedForUser(packageName, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean isSafeMode() {
+        // allow instant applications
+        return mService.getSafeMode();
+    }
+
+    @Override
+    @Deprecated
+    public final boolean isStorageLow() {
+        return mService.isStorageLow();
+    }
+
+    @Override
+    @Deprecated
+    public final boolean isUidPrivileged(int uid) {
+        return snapshot().isUidPrivileged(uid);
+    }
+
+    /**
+     * Ask the package manager to perform a dex-opt with the given compiler filter.
+     * <p>
+     * Note: exposed only for the shell command to allow moving packages explicitly to a definite
+     * state.
+     */
+    @Override
+    @Deprecated
+    public final boolean performDexOptMode(String packageName,
+            boolean checkProfiles, String targetCompilerFilter, boolean force,
+            boolean bootComplete, String splitName) {
+        return mDexOptHelper.performDexOptMode(packageName, checkProfiles, targetCompilerFilter,
+                force, bootComplete, splitName);
+    }
+
+    /**
+     * Ask the package manager to perform a dex-opt with the given compiler filter on the secondary
+     * dex files belonging to the given package.
+     * <p>
+     * Note: exposed only for the shell command to allow moving packages explicitly to a definite
+     * state.
+     */
+    @Override
+    @Deprecated
+    public final boolean performDexOptSecondary(String packageName, String compilerFilter,
+            boolean force) {
+        return mDexOptHelper.performDexOptSecondary(packageName, compilerFilter, force);
+    }
+
+    @Override
+    @Deprecated
+    public final @NonNull
+    ParceledListSlice<ResolveInfo> queryIntentActivities(Intent intent,
+            String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags, int userId) {
+        try {
+            Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "queryIntentActivities");
+
+            return new ParceledListSlice<>(snapshot().queryIntentActivitiesInternal(intent,
+                    resolvedType, flags, userId));
+        } finally {
+            Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER);
+        }
+    }
+
+    @NonNull
+    @Override
+    @Deprecated
+    public final ParceledListSlice<ProviderInfo> queryContentProviders(@Nullable String processName,
+            int uid, @PackageManager.ComponentInfoFlagsBits long flags,
+            @Nullable String metaDataKey) {
+        return snapshot().queryContentProviders(processName, uid, flags, metaDataKey);
+    }
+
+    @NonNull
+    @Override
+    @Deprecated
+    public final ParceledListSlice<InstrumentationInfo> queryInstrumentation(
+            @NonNull String targetPackage, int flags) {
+        return snapshot().queryInstrumentation(targetPackage, flags);
+    }
+
+    @Override
+    @Deprecated
+    public final @NonNull
+    ParceledListSlice<ResolveInfo> queryIntentActivityOptions(
+            ComponentName caller, Intent[] specifics, String[] specificTypes, Intent intent,
+            String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags, int userId) {
+        return new ParceledListSlice<>(mResolveIntentHelper.queryIntentActivityOptionsInternal(
+                snapshot(), caller, specifics, specificTypes, intent, resolvedType, flags,
+                userId));
+    }
+
+    @Override
+    @Deprecated
+    public final @NonNull
+    ParceledListSlice<ResolveInfo> queryIntentContentProviders(Intent intent,
+            String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags, int userId) {
+        return new ParceledListSlice<>(mResolveIntentHelper.queryIntentContentProvidersInternal(
+                snapshot(), intent, resolvedType, flags, userId));
+    }
+
+    @Override
+    @Deprecated
+    public final @NonNull
+    ParceledListSlice<ResolveInfo> queryIntentReceivers(Intent intent,
+            String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags, int userId) {
+        return new ParceledListSlice<>(mResolveIntentHelper.queryIntentReceiversInternal(
+                snapshot(), intent, resolvedType, flags, userId, Binder.getCallingUid()));
+    }
+
+    @Override
+    @Deprecated
+    public final @NonNull
+    ParceledListSlice<ResolveInfo> queryIntentServices(Intent intent,
+            String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags, int userId) {
+        final int callingUid = Binder.getCallingUid();
+        return new ParceledListSlice<>(snapshot().queryIntentServicesInternal(
+                intent, resolvedType, flags, userId, callingUid, false /*includeInstantApps*/));
+    }
+
+    @Override
+    @Deprecated
+    public final void querySyncProviders(List<String> outNames, List<ProviderInfo> outInfo) {
+        snapshot().querySyncProviders(isSafeMode(), outNames, outInfo);
+    }
+
+    @Override
+    @Deprecated
+    public final void removePermission(String permName) {
+        // Because this is accessed via the package manager service AIDL,
+        // go through the permission manager service AIDL
+        mContext.getSystemService(PermissionManager.class).removePermission(permName);
+    }
+
+    @Override
+    @Deprecated
+    public final void replacePreferredActivity(IntentFilter filter, int match,
+            ComponentName[] set, ComponentName activity, int userId) {
+        mPreferredActivityHelper.replacePreferredActivity(snapshot(),
+                new WatchedIntentFilter(filter), match, set, activity, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final ProviderInfo resolveContentProvider(String name,
+            @PackageManager.ResolveInfoFlagsBits long flags, int userId) {
+        return snapshot().resolveContentProvider(name, flags, userId, Binder.getCallingUid());
+    }
+
+    @Override
+    @Deprecated
+    public final void resetApplicationPreferences(int userId) {
+        mPreferredActivityHelper.resetApplicationPreferences(userId);
+    }
+
+    @Override
+    @Deprecated
+    public final ResolveInfo resolveIntent(Intent intent, String resolvedType,
+            @PackageManager.ResolveInfoFlagsBits long flags, int userId) {
+        return mResolveIntentHelper.resolveIntentInternal(snapshot(), intent,
+                resolvedType, flags, 0 /*privateResolveFlags*/, userId, false,
+                Binder.getCallingUid());
+    }
+
+    @Override
+    @Deprecated
+    public final ResolveInfo resolveService(Intent intent, String resolvedType,
+            @PackageManager.ResolveInfoFlagsBits long flags, int userId) {
+        final int callingUid = Binder.getCallingUid();
+        return mResolveIntentHelper.resolveServiceInternal(snapshot(), intent,
+                resolvedType, flags, userId, callingUid);
+    }
+
+    @Override
+    @Deprecated
+    public final void restoreDefaultApps(byte[] backup, int userId) {
+        mPreferredActivityHelper.restoreDefaultApps(backup, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final void restorePreferredActivities(byte[] backup, int userId) {
+        mPreferredActivityHelper.restorePreferredActivities(backup, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final void setHomeActivity(ComponentName comp, int userId) {
+        mPreferredActivityHelper.setHomeActivity(snapshot(), comp, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final void setLastChosenActivity(Intent intent, String resolvedType, int flags,
+            IntentFilter filter, int match, ComponentName activity) {
+        mPreferredActivityHelper.setLastChosenActivity(snapshot(), intent, resolvedType,
+                flags, new WatchedIntentFilter(filter), match, activity);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean updateIntentVerificationStatus(String packageName, int status,
+            int userId) {
+        return mDomainVerificationManager.setLegacyUserState(packageName, userId, status);
+    }
+
+    @Override
+    @Deprecated
+    public final void verifyIntentFilter(int id, int verificationCode, List<String> failedDomains) {
+        DomainVerificationProxyV1.queueLegacyVerifyResult(mContext, mDomainVerificationConnection,
+                id, verificationCode, failedDomains, Binder.getCallingUid());
+    }
+
+    @Override
+    @Deprecated
+    public final boolean canPackageQuery(@NonNull String sourcePackageName,
+            @NonNull String targetPackageName, @UserIdInt int userId) {
+        return snapshot().canPackageQuery(sourcePackageName, targetPackageName, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final void deletePreloadsFileCache() throws RemoteException {
+        mService.deletePreloadsFileCache();
+    }
+
+    @Override
+    @Deprecated
+    public final void setSystemAppHiddenUntilInstalled(String packageName, boolean hidden)
+            throws RemoteException {
+        mService.setSystemAppHiddenUntilInstalled(snapshot(), packageName, hidden);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean setSystemAppInstallState(String packageName,
+            boolean installed, int userId) throws RemoteException {
+        return mService.setSystemAppInstallState(snapshot(), packageName, installed, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final void finishPackageInstall(int token, boolean didLaunch) throws RemoteException {
+        mService.finishPackageInstall(token, didLaunch);
+    }
+}
diff --git a/services/core/java/com/android/server/pm/IncrementalProgressListener.java b/services/core/java/com/android/server/pm/IncrementalProgressListener.java
index fa11924..703bbda 100644
--- a/services/core/java/com/android/server/pm/IncrementalProgressListener.java
+++ b/services/core/java/com/android/server/pm/IncrementalProgressListener.java
@@ -33,7 +33,8 @@
 
     @Override
     public void onPackageLoadingProgressChanged(float progress) {
-        PackageStateInternal packageState = mPm.getPackageStateInternal(mPackageName);
+        PackageStateInternal packageState = mPm.snapshotComputer()
+                .getPackageStateInternal(mPackageName);
         if (packageState == null) {
             return;
         }
diff --git a/services/core/java/com/android/server/pm/InitAndSystemPackageHelper.java b/services/core/java/com/android/server/pm/InitAndSystemPackageHelper.java
index 6dbe9b6..91750de 100644
--- a/services/core/java/com/android/server/pm/InitAndSystemPackageHelper.java
+++ b/services/core/java/com/android/server/pm/InitAndSystemPackageHelper.java
@@ -30,6 +30,7 @@
 import static com.android.server.pm.PackageManagerService.SCAN_REQUIRE_KNOWN;
 import static com.android.server.pm.PackageManagerService.SYSTEM_PARTITIONS;
 import static com.android.server.pm.PackageManagerService.TAG;
+import static com.android.server.pm.pkg.parsing.ParsingPackageUtils.PARSE_CHECK_MAX_SDK_VERSION;
 
 import android.annotation.Nullable;
 import android.content.pm.parsing.ApkLiteParseUtils;
@@ -170,7 +171,7 @@
             }
         }
         OverlayConfig overlayConfig = OverlayConfig.initializeSystemInstance(
-                consumer -> mPm.forEachPackage(
+                consumer -> mPm.forEachPackage(mPm.snapshotComputer(),
                         pkg -> consumer.accept(pkg, pkg.isSystem(),
                           apkInApexPreInstalledPaths.get(pkg.getPackageName()))));
         // Prune any system packages that no longer exist.
@@ -313,10 +314,14 @@
 
     @GuardedBy({"mPm.mInstallLock", "mPm.mLock"})
     private void scanDirTracedLI(File scanDir, List<File> frameworkSplits,
-            final int parseFlags, int scanFlags,
+            int parseFlags, int scanFlags,
             long currentTime, PackageParser2 packageParser, ExecutorService executorService) {
         Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "scanDir [" + scanDir.getAbsolutePath() + "]");
         try {
+            if ((scanFlags & SCAN_AS_APK_IN_APEX) != 0) {
+                // when scanning apk in apexes, we want to check the maxSdkVersion
+                parseFlags |= PARSE_CHECK_MAX_SDK_VERSION;
+            }
             mInstallPackageHelper.installPackagesFromDir(scanDir, frameworkSplits, parseFlags,
                     scanFlags, currentTime, packageParser, executorService);
         } finally {
diff --git a/services/core/java/com/android/server/pm/InstallPackageHelper.java b/services/core/java/com/android/server/pm/InstallPackageHelper.java
index b5d63f3..b39b24f 100644
--- a/services/core/java/com/android/server/pm/InstallPackageHelper.java
+++ b/services/core/java/com/android/server/pm/InstallPackageHelper.java
@@ -527,8 +527,10 @@
                     + android.Manifest.permission.INSTALL_PACKAGES + ".");
         }
         PackageSetting pkgSetting;
-        mPm.enforceCrossUserPermission(callingUid, userId, true /* requireFullPermission */,
-                true /* checkShell */, "installExistingPackage for user " + userId);
+        final Computer preLockSnapshot = mPm.snapshotComputer();
+        preLockSnapshot.enforceCrossUserPermission(callingUid, userId,
+                true /* requireFullPermission */, true /* checkShell */,
+                "installExistingPackage for user " + userId);
         if (mPm.isUserRestricted(userId, UserManager.DISALLOW_INSTALL_APPS)) {
             return PackageManager.INSTALL_FAILED_USER_RESTRICTED;
         }
@@ -543,11 +545,12 @@
 
             // writer
             synchronized (mPm.mLock) {
+                final Computer snapshot = mPm.snapshotComputer();
                 pkgSetting = mPm.mSettings.getPackageLPr(packageName);
                 if (pkgSetting == null) {
                     return PackageManager.INSTALL_FAILED_INVALID_URI;
                 }
-                if (!mPm.canViewInstantApps(callingUid, UserHandle.getUserId(callingUid))) {
+                if (!snapshot.canViewInstantApps(callingUid, UserHandle.getUserId(callingUid))) {
                     // only allow the existing package to be used if it's installed as a full
                     // application for at least one user
                     boolean installAllowed = false;
@@ -597,7 +600,8 @@
                         mAppDataHelper.prepareAppDataAfterInstallLIF(pkgSetting.getPkg());
                     }
                 }
-                mPm.sendPackageAddedForUser(packageName, pkgSetting, userId, DataLoaderType.NONE);
+                mPm.sendPackageAddedForUser(mPm.snapshotComputer(), packageName, pkgSetting, userId,
+                        DataLoaderType.NONE);
                 synchronized (mPm.mLock) {
                     mPm.updateSequenceNumberLP(pkgSetting, new int[]{ userId });
                 }
@@ -958,10 +962,6 @@
                     createdAppId.put(packageName, optimisticallyRegisterAppId(result));
                     versionInfos.put(result.mPkgSetting.getPkg().getPackageName(),
                             mPm.getSettingsVersionForPackage(result.mPkgSetting.getPkg()));
-                    if (result.needsNewAppId()) {
-                        request.mInstallResult.mRemovedInfo.mNewAppId =
-                                result.mPkgSetting.getAppId();
-                    }
                 } catch (PackageManagerException e) {
                     request.mInstallResult.setError("Scanning Failed.", e);
                     return;
@@ -1906,8 +1906,8 @@
                 AndroidPackage oldPackage = mPm.mPackages.get(packageName);
 
                 // Set the update and install times
-                PackageStateInternal deletedPkgSetting = mPm.getPackageStateInternal(
-                        oldPackage.getPackageName());
+                PackageStateInternal deletedPkgSetting = mPm.snapshotComputer()
+                        .getPackageStateInternal(oldPackage.getPackageName());
                 reconciledPkg.mPkgSetting
                         .setFirstInstallTimeFromReplaced(deletedPkgSetting, request.mAllUsers)
                         .setLastUpdateTime(System.currentTimeMillis());
@@ -2575,9 +2575,10 @@
             size = i;
             mPm.mPendingBroadcasts.clear();
         }
+        final Computer snapshot = mPm.snapshotComputer();
         // Send broadcasts
         for (int i = 0; i < size; i++) {
-            mPm.sendPackageChangedBroadcast(packages[i], true /* dontKillApp */,
+            mPm.sendPackageChangedBroadcast(snapshot, packages[i], true /* dontKillApp */,
                     components[i], uids[i], null /* reason */);
         }
         Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
@@ -2594,11 +2595,9 @@
         final int dataLoaderType = installArgs.mDataLoaderType;
         final boolean succeeded = res.mReturnCode == PackageManager.INSTALL_SUCCEEDED;
         final boolean update = res.mRemovedInfo != null && res.mRemovedInfo.mRemovedPackage != null;
-        final int previousAppId = (res.mRemovedInfo != null && res.mRemovedInfo.mNewAppId >= 0)
-                ? res.mRemovedInfo.mUid : Process.INVALID_UID;
         final String packageName = res.mName;
         final PackageStateInternal pkgSetting =
-                succeeded ? mPm.getPackageStateInternal(packageName) : null;
+                succeeded ? mPm.snapshotComputer().getPackageStateInternal(packageName) : null;
         final boolean removedBeforeUpdate = (pkgSetting == null)
                 || (pkgSetting.isSystem() && !pkgSetting.getPath().getPath().equals(
                 res.mPkg.getPath()));
@@ -2696,17 +2695,14 @@
                 // sendPackageAddedForNewUsers also deals with system apps
                 int appId = UserHandle.getAppId(res.mUid);
                 boolean isSystem = res.mPkg.isSystem();
-                mPm.sendPackageAddedForNewUsers(packageName, isSystem || virtualPreload,
-                        virtualPreload /*startReceiver*/, appId, firstUserIds, firstInstantUserIds,
-                        dataLoaderType);
+                mPm.sendPackageAddedForNewUsers(mPm.snapshotComputer(), packageName,
+                        isSystem || virtualPreload, virtualPreload /*startReceiver*/, appId,
+                        firstUserIds, firstInstantUserIds, dataLoaderType);
 
                 // Send added for users that don't see the package for the first time
                 Bundle extras = new Bundle();
                 extras.putInt(Intent.EXTRA_UID, res.mUid);
-                if (previousAppId != Process.INVALID_UID) {
-                    extras.putBoolean(Intent.EXTRA_UID_CHANGING, true);
-                    extras.putInt(Intent.EXTRA_PREVIOUS_UID, previousAppId);
-                } else if (update) {
+                if (update) {
                     extras.putBoolean(Intent.EXTRA_REPLACING, true);
                 }
                 extras.putInt(PackageInstaller.EXTRA_DATA_LOADER_TYPE, dataLoaderType);
@@ -2714,7 +2710,8 @@
                 final SparseArray<int[]> newBroadcastAllowList;
                 synchronized (mPm.mLock) {
                     newBroadcastAllowList = mPm.mAppsFilter.getVisibilityAllowList(
-                            mPm.getPackageStateInternal(packageName, Process.SYSTEM_UID),
+                            mPm.snapshotComputer()
+                                    .getPackageStateInternal(packageName, Process.SYSTEM_UID),
                             updateUserIds, mPm.mSettings.getPackagesLocked());
                 }
                 mPm.sendPackageBroadcast(Intent.ACTION_PACKAGE_ADDED, packageName,
@@ -2749,27 +2746,24 @@
 
                 // Send replaced for users that don't see the package for the first time
                 if (update) {
-                    // Only send PACKAGE_REPLACED if appId has not changed
-                    if (previousAppId == Process.INVALID_UID) {
-                        mPm.sendPackageBroadcast(Intent.ACTION_PACKAGE_REPLACED,
-                                packageName, extras, 0 /*flags*/,
-                                null /*targetPackage*/, null /*finishedReceiver*/,
-                                updateUserIds, instantUserIds, res.mRemovedInfo.mBroadcastAllowList,
+                    mPm.sendPackageBroadcast(Intent.ACTION_PACKAGE_REPLACED,
+                            packageName, extras, 0 /*flags*/,
+                            null /*targetPackage*/, null /*finishedReceiver*/,
+                            updateUserIds, instantUserIds, res.mRemovedInfo.mBroadcastAllowList,
+                            null);
+                    if (installerPackageName != null) {
+                        mPm.sendPackageBroadcast(Intent.ACTION_PACKAGE_REPLACED, packageName,
+                                extras, 0 /*flags*/,
+                                installerPackageName, null /*finishedReceiver*/,
+                                updateUserIds, instantUserIds, null /*broadcastAllowList*/,
                                 null);
-                        if (installerPackageName != null) {
-                            mPm.sendPackageBroadcast(Intent.ACTION_PACKAGE_REPLACED, packageName,
-                                    extras, 0 /*flags*/,
-                                    installerPackageName, null /*finishedReceiver*/,
-                                    updateUserIds, instantUserIds, null /*broadcastAllowList*/,
-                                    null);
-                        }
-                        if (notifyVerifier) {
-                            mPm.sendPackageBroadcast(Intent.ACTION_PACKAGE_REPLACED, packageName,
-                                    extras, 0 /*flags*/,
-                                    mPm.mRequiredVerifierPackage, null /*finishedReceiver*/,
-                                    updateUserIds, instantUserIds, null /*broadcastAllowList*/,
-                                    null);
-                        }
+                    }
+                    if (notifyVerifier) {
+                        mPm.sendPackageBroadcast(Intent.ACTION_PACKAGE_REPLACED, packageName,
+                                extras, 0 /*flags*/,
+                                mPm.mRequiredVerifierPackage, null /*finishedReceiver*/,
+                                updateUserIds, instantUserIds, null /*broadcastAllowList*/,
+                                null);
                     }
                     mPm.sendPackageBroadcast(Intent.ACTION_MY_PACKAGE_REPLACED,
                             null /*package*/, null /*extras*/, 0 /*flags*/,
@@ -2819,11 +2813,12 @@
                 }
             } else if (!ArrayUtils.isEmpty(res.mLibraryConsumers)) { // if static shared lib
                 // No need to kill consumers if it's installation of new version static shared lib.
+                final Computer snapshot = mPm.snapshotComputer();
                 final boolean dontKillApp = !update && res.mPkg.getStaticSharedLibName() != null;
                 for (int i = 0; i < res.mLibraryConsumers.size(); i++) {
                     AndroidPackage pkg = res.mLibraryConsumers.get(i);
                     // send broadcast that all consumers of the static shared library have changed
-                    mPm.sendPackageChangedBroadcast(pkg.getPackageName(), dontKillApp,
+                    mPm.sendPackageChangedBroadcast(snapshot, pkg.getPackageName(), dontKillApp,
                             new ArrayList<>(Collections.singletonList(pkg.getPackageName())),
                             pkg.getUid(), null);
                 }
diff --git a/services/core/java/com/android/server/pm/ModuleInfoProvider.java b/services/core/java/com/android/server/pm/ModuleInfoProvider.java
index 0ffc1ed..230f555 100644
--- a/services/core/java/com/android/server/pm/ModuleInfoProvider.java
+++ b/services/core/java/com/android/server/pm/ModuleInfoProvider.java
@@ -16,6 +16,7 @@
 
 package com.android.server.pm;
 
+import android.annotation.NonNull;
 import android.content.Context;
 import android.content.pm.IPackageManager;
 import android.content.pm.ModuleInfo;
@@ -25,6 +26,7 @@
 import android.content.res.Resources;
 import android.content.res.XmlResourceParser;
 import android.os.RemoteException;
+import android.os.ServiceManager;
 import android.os.UserHandle;
 import android.text.TextUtils;
 import android.util.ArrayMap;
@@ -58,7 +60,7 @@
     private static final String MODULE_METADATA_KEY = "android.content.pm.MODULE_METADATA";
 
     private final Context mContext;
-    private final IPackageManager mPackageManager;
+    private IPackageManager mPackageManager;
     private final ApexManager mApexManager;
     private final Map<String, ModuleInfo> mModuleInfo;
 
@@ -66,9 +68,8 @@
     private volatile boolean mMetadataLoaded;
     private volatile String mPackageName;
 
-    ModuleInfoProvider(Context context, IPackageManager packageManager) {
+    ModuleInfoProvider(Context context) {
         mContext = context;
-        mPackageManager = packageManager;
         mApexManager = ApexManager.getInstance();
         mModuleInfo = new ArrayMap<>();
     }
@@ -77,12 +78,20 @@
     public ModuleInfoProvider(
             XmlResourceParser metadata, Resources resources, ApexManager apexManager) {
         mContext = null;
-        mPackageManager = null;
         mApexManager = apexManager;
         mModuleInfo = new ArrayMap<>();
         loadModuleMetadata(metadata, resources);
     }
 
+    @NonNull
+    private IPackageManager getPackageManager() {
+        if (mPackageManager == null) {
+            mPackageManager = IPackageManager.Stub.asInterface(
+                    ServiceManager.getService("package"));
+        }
+        return mPackageManager;
+    }
+
     /** Called by the {@code PackageManager} when it has completed its boot sequence */
     public void systemReady() {
         mPackageName = mContext.getResources().getString(
@@ -95,7 +104,7 @@
         final Resources packageResources;
         final PackageInfo pi;
         try {
-            pi = mPackageManager.getPackageInfo(mPackageName,
+            pi =  getPackageManager().getPackageInfo(mPackageName,
                 PackageManager.GET_META_DATA, UserHandle.USER_SYSTEM);
 
             Context packageContext = mContext.createPackageContext(mPackageName, 0);
@@ -183,7 +192,7 @@
 
         List<PackageInfo> allPackages;
         try {
-            allPackages = mPackageManager.getInstalledPackages(
+            allPackages =  getPackageManager().getInstalledPackages(
                     flags | PackageManager.MATCH_APEX, UserHandle.getCallingUserId()).getList();
         } catch (RemoteException e) {
             Slog.w(TAG, "Unable to retrieve all package names", e);
diff --git a/services/core/java/com/android/server/pm/MovePackageHelper.java b/services/core/java/com/android/server/pm/MovePackageHelper.java
index 5fc90b1..c5ca06c 100644
--- a/services/core/java/com/android/server/pm/MovePackageHelper.java
+++ b/services/core/java/com/android/server/pm/MovePackageHelper.java
@@ -57,6 +57,8 @@
 import com.android.internal.util.FrameworkStatsLog;
 import com.android.server.pm.parsing.pkg.AndroidPackage;
 import com.android.server.pm.parsing.pkg.AndroidPackageUtils;
+import com.android.server.pm.pkg.PackageStateInternal;
+import com.android.server.pm.pkg.PackageStateUtils;
 
 import java.io.File;
 import java.util.Objects;
@@ -77,81 +79,74 @@
         final StorageManager storage = mPm.mInjector.getSystemService(StorageManager.class);
         final PackageManager pm = mPm.mContext.getPackageManager();
 
-        final String currentVolumeUuid;
-        final File codeFile;
-        final InstallSource installSource;
-        final String packageAbiOverride;
-        final int appId;
-        final String seinfo;
-        final String label;
-        final int targetSdkVersion;
-        final PackageFreezer freezer;
-        final int[] installedUserIds;
-        final boolean isCurrentLocationExternal;
+        Computer snapshot = mPm.snapshotComputer();
+        final PackageStateInternal packageState = snapshot.getPackageStateInternal(packageName);
+        if (packageState == null
+                || packageState.getPkg() == null
+                || snapshot.shouldFilterApplication(packageState, callingUid, user.getIdentifier())) {
+            throw new PackageManagerException(MOVE_FAILED_DOESNT_EXIST, "Missing package");
+        }
+        final AndroidPackage pkg = packageState.getPkg();
+        if (pkg.isSystem()) {
+            throw new PackageManagerException(MOVE_FAILED_SYSTEM_PACKAGE,
+                    "Cannot move system application");
+        }
+
+        final boolean isInternalStorage = VolumeInfo.ID_PRIVATE_INTERNAL.equals(volumeUuid);
+        final boolean allow3rdPartyOnInternal = mPm.mContext.getResources().getBoolean(
+                com.android.internal.R.bool.config_allow3rdPartyAppOnInternal);
+        if (isInternalStorage && !allow3rdPartyOnInternal) {
+            throw new PackageManagerException(MOVE_FAILED_3RD_PARTY_NOT_ALLOWED_ON_INTERNAL,
+                    "3rd party apps are not allowed on internal storage");
+        }
+
+
+        final String currentVolumeUuid = packageState.getVolumeUuid();
+
+        final File probe = new File(pkg.getPath());
+        final File probeOat = new File(probe, "oat");
+        if (!probe.isDirectory() || !probeOat.isDirectory()) {
+            throw new PackageManagerException(MOVE_FAILED_INTERNAL_ERROR,
+                    "Move only supported for modern cluster style installs");
+        }
+
+        if (Objects.equals(currentVolumeUuid, volumeUuid)) {
+            throw new PackageManagerException(MOVE_FAILED_INTERNAL_ERROR,
+                    "Package already moved to " + volumeUuid);
+        }
+        if (!pkg.isExternalStorage()
+                && mPm.isPackageDeviceAdminOnAnyUser(snapshot, packageName)) {
+            throw new PackageManagerException(MOVE_FAILED_DEVICE_ADMIN,
+                    "Device admin cannot be moved");
+        }
+
+        if (snapshot.getFrozenPackages().containsKey(packageName)) {
+            throw new PackageManagerException(MOVE_FAILED_OPERATION_PENDING,
+                    "Failed to move already frozen package");
+        }
+
+        final boolean isCurrentLocationExternal = pkg.isExternalStorage();
+        final File codeFile = new File(pkg.getPath());
+        final InstallSource installSource = packageState.getInstallSource();
+        final String packageAbiOverride = packageState.getCpuAbiOverride();
+        final int appId = UserHandle.getAppId(pkg.getUid());
+        final String seinfo = AndroidPackageUtils.getSeInfo(pkg, packageState);
+        final String label = String.valueOf(pm.getApplicationLabel(
+                AndroidPackageUtils.generateAppInfoWithoutState(pkg)));
+        final int targetSdkVersion = pkg.getTargetSdkVersion();
+        final int[] installedUserIds = PackageStateUtils.queryInstalledUsers(packageState,
+                mPm.mUserManager.getUserIds(), true);
         final String fromCodePath;
+        if (codeFile.getParentFile().getName().startsWith(
+                PackageManagerService.RANDOM_DIR_PREFIX)) {
+            fromCodePath = codeFile.getParentFile().getAbsolutePath();
+        } else {
+            fromCodePath = codeFile.getAbsolutePath();
+        }
 
-        // reader
+        final PackageFreezer freezer;
         synchronized (mPm.mLock) {
-            final AndroidPackage pkg = mPm.mPackages.get(packageName);
-            final PackageSetting ps = mPm.mSettings.getPackageLPr(packageName);
-            if (pkg == null
-                    || ps == null
-                    || mPm.shouldFilterApplication(ps, callingUid, user.getIdentifier())) {
-                throw new PackageManagerException(MOVE_FAILED_DOESNT_EXIST, "Missing package");
-            }
-            if (pkg.isSystem()) {
-                throw new PackageManagerException(MOVE_FAILED_SYSTEM_PACKAGE,
-                        "Cannot move system application");
-            }
-
-            final boolean isInternalStorage = VolumeInfo.ID_PRIVATE_INTERNAL.equals(volumeUuid);
-            final boolean allow3rdPartyOnInternal = mPm.mContext.getResources().getBoolean(
-                    com.android.internal.R.bool.config_allow3rdPartyAppOnInternal);
-            if (isInternalStorage && !allow3rdPartyOnInternal) {
-                throw new PackageManagerException(MOVE_FAILED_3RD_PARTY_NOT_ALLOWED_ON_INTERNAL,
-                        "3rd party apps are not allowed on internal storage");
-            }
-
-            currentVolumeUuid = ps.getVolumeUuid();
-
-            final File probe = new File(pkg.getPath());
-            final File probeOat = new File(probe, "oat");
-            if (!probe.isDirectory() || !probeOat.isDirectory()) {
-                throw new PackageManagerException(MOVE_FAILED_INTERNAL_ERROR,
-                        "Move only supported for modern cluster style installs");
-            }
-
-            if (Objects.equals(currentVolumeUuid, volumeUuid)) {
-                throw new PackageManagerException(MOVE_FAILED_INTERNAL_ERROR,
-                        "Package already moved to " + volumeUuid);
-            }
-            if (!pkg.isExternalStorage() && mPm.isPackageDeviceAdminOnAnyUser(packageName)) {
-                throw new PackageManagerException(MOVE_FAILED_DEVICE_ADMIN,
-                        "Device admin cannot be moved");
-            }
-
-            if (mPm.mFrozenPackages.containsKey(packageName)) {
-                throw new PackageManagerException(MOVE_FAILED_OPERATION_PENDING,
-                        "Failed to move already frozen package");
-            }
-
-            isCurrentLocationExternal = pkg.isExternalStorage();
-            codeFile = new File(pkg.getPath());
-            installSource = ps.getInstallSource();
-            packageAbiOverride = ps.getCpuAbiOverride();
-            appId = UserHandle.getAppId(pkg.getUid());
-            seinfo = AndroidPackageUtils.getSeInfo(pkg, ps);
-            label = String.valueOf(pm.getApplicationLabel(
-                    AndroidPackageUtils.generateAppInfoWithoutState(pkg)));
-            targetSdkVersion = pkg.getTargetSdkVersion();
             freezer = mPm.freezePackage(packageName, "movePackageInternal");
-            installedUserIds = ps.queryInstalledUsers(mPm.mUserManager.getUserIds(), true);
-            if (codeFile.getParentFile().getName().startsWith(
-                    PackageManagerService.RANDOM_DIR_PREFIX)) {
-                fromCodePath = codeFile.getParentFile().getAbsolutePath();
-            } else {
-                fromCodePath = codeFile.getAbsolutePath();
-            }
         }
 
         final Bundle extras = new Bundle();
diff --git a/services/core/java/com/android/server/pm/OWNERS b/services/core/java/com/android/server/pm/OWNERS
index c219f80..8534fab 100644
--- a/services/core/java/com/android/server/pm/OWNERS
+++ b/services/core/java/com/android/server/pm/OWNERS
@@ -15,7 +15,9 @@
 per-file AbstractStatsBase.java = file:dex/OWNERS
 per-file BackgroundDexOptService.java = file:dex/OWNERS
 per-file CompilerStats.java = file:dex/OWNERS
+per-file DexOptHelper.java = file:dex/OWNERS
 per-file DynamicCodeLoggingService.java = file:dex/OWNERS
+per-file Installer.java = file:dex/OWNERS
 per-file InstructionSets.java = file:dex/OWNERS
 per-file OtaDexoptService.java = file:dex/OWNERS
 per-file OtaDexoptShellCommand.java = file:dex/OWNERS
diff --git a/services/core/java/com/android/server/pm/OtaDexoptService.java b/services/core/java/com/android/server/pm/OtaDexoptService.java
index bd00914..cc4a760 100644
--- a/services/core/java/com/android/server/pm/OtaDexoptService.java
+++ b/services/core/java/com/android/server/pm/OtaDexoptService.java
@@ -131,8 +131,9 @@
         Predicate<PackageStateInternal> isPlatformPackage = pkgSetting ->
                 PLATFORM_PACKAGE_NAME.equals(pkgSetting.getPkg().getPackageName());
         // Important: the packages we need to run with ab-ota compiler-reason.
+        final Computer snapshot = mPackageManagerService.snapshotComputer();
         final Collection<? extends PackageStateInternal> allPackageStates =
-                mPackageManagerService.getPackageStates().values();
+                snapshot.getPackageStates().values();
         important = DexOptHelper.getPackagesForDexopt(allPackageStates,mPackageManagerService,
                 DEBUG_DEXOPT);
         // Remove Platform Package from A/B OTA b/160735835.
@@ -165,7 +166,7 @@
             Log.i(TAG, "Low on space, deleting oat files in an attempt to free up space: "
                     + DexOptHelper.packagesToString(others));
             for (PackageStateInternal pkg : others) {
-                mPackageManagerService.deleteOatArtifactsOfPackage(pkg.getPackageName());
+                mPackageManagerService.deleteOatArtifactsOfPackage(snapshot, pkg.getPackageName());
             }
         }
         long spaceAvailableNow = getAvailableSpace();
diff --git a/services/core/java/com/android/server/pm/PackageInstallerService.java b/services/core/java/com/android/server/pm/PackageInstallerService.java
index 0a19357..4e702e2 100644
--- a/services/core/java/com/android/server/pm/PackageInstallerService.java
+++ b/services/core/java/com/android/server/pm/PackageInstallerService.java
@@ -334,7 +334,7 @@
                 StagingManager.StagedSession stagedSession = session.mStagedSession;
                 if (!stagedSession.isInTerminalState() && stagedSession.hasParentSessionId()
                         && getSession(stagedSession.getParentSessionId()) == null) {
-                    stagedSession.setSessionFailed(SessionInfo.SESSION_ACTIVATION_FAILED,
+                    stagedSession.setSessionFailed(PackageManager.INSTALL_ACTIVATION_FAILED,
                             "An orphan staged session " + stagedSession.sessionId() + " is found, "
                                 + "parent " + stagedSession.getParentSessionId() + " is missing");
                     continue;
@@ -597,8 +597,8 @@
             String installerAttributionTag, int userId)
             throws IOException {
         final int callingUid = Binder.getCallingUid();
-        mPm.enforceCrossUserPermission(
-                callingUid, userId, true, true, "createSession");
+        final Computer snapshot = mPm.snapshotComputer();
+        snapshot.enforceCrossUserPermission(callingUid, userId, true, true, "createSession");
 
         if (mPm.isUserRestricted(userId, UserManager.DISALLOW_INSTALL_APPS)) {
             throw new SecurityException("User restriction prevents installing");
@@ -663,7 +663,7 @@
             params.installFlags &= ~PackageManager.INSTALL_ALL_USERS;
             params.installFlags |= PackageManager.INSTALL_REPLACE_EXISTING;
             if ((params.installFlags & PackageManager.INSTALL_VIRTUAL_PRELOAD) != 0
-                    && !mPm.isCallerVerifier(callingUid)) {
+                    && !mPm.isCallerVerifier(snapshot, callingUid)) {
                 params.installFlags &= ~PackageManager.INSTALL_VIRTUAL_PRELOAD;
             }
             if (mContext.checkCallingOrSelfPermission(
@@ -676,7 +676,7 @@
         String originatingPackageName = null;
         if (params.originatingUid != SessionParams.UID_UNKNOWN
                 && params.originatingUid != callingUid) {
-            String[] packages = mPm.mIPackageManager.getPackagesForUid(params.originatingUid);
+            String[] packages = snapshot.getPackagesForUid(params.originatingUid);
             if (packages != null && packages.length > 0) {
                 // Choose an arbitrary representative package in the case of a shared UID.
                 originatingPackageName = packages[0];
@@ -727,7 +727,7 @@
 
         if ((params.installFlags & PackageManager.INSTALL_INSTANT_APP) != 0
                 && !isCalledBySystemOrShell(callingUid)
-                && (mPm.mIPackageManager.getFlagsForUid(callingUid) & ApplicationInfo.FLAG_SYSTEM)
+                && (snapshot.getFlagsForUid(callingUid) & ApplicationInfo.FLAG_SYSTEM)
                 == 0) {
             throw new SecurityException(
                     "Only system apps could use the PackageManager.INSTALL_INSTANT_APP flag.");
@@ -853,7 +853,7 @@
                 mSilentUpdatePolicy, mInstallThread.getLooper(), mStagingManager, sessionId,
                 userId, callingUid, installSource, params, createdMillis, 0L, stageDir, stageCid,
                 null, null, false, false, false, false, null, SessionInfo.INVALID_ID,
-                false, false, false, SessionInfo.SESSION_NO_ERROR, "");
+                false, false, false, PackageManager.INSTALL_UNKNOWN, "");
 
         synchronized (mSessions) {
             mSessions.put(sessionId, session);
@@ -1038,11 +1038,12 @@
         return "smdl" + sessionId + ".tmp";
     }
 
-    private boolean shouldFilterSession(int uid, SessionInfo info) {
+    private boolean shouldFilterSession(@NonNull Computer snapshot, int uid, SessionInfo info) {
         if (info == null) {
             return false;
         }
-        return uid != info.getInstallerUid() && !mPm.canQueryPackage(uid, info.getAppPackageName());
+        return uid != info.getInstallerUid()
+                && !snapshot.canQueryPackage(uid, info.getAppPackageName());
     }
 
     @Override
@@ -1055,7 +1056,7 @@
                     ? session.generateInfoForCaller(true /* includeIcon */, callingUid)
                     : null;
         }
-        return shouldFilterSession(callingUid, result) ? null : result;
+        return shouldFilterSession(mPm.snapshotComputer(), callingUid, result) ? null : result;
     }
 
     @Override
@@ -1070,15 +1071,16 @@
                 }
             }
         }
-        result.removeIf(info -> shouldFilterSession(callingUid, info));
+        final Computer snapshot = mPm.snapshotComputer();
+        result.removeIf(info -> shouldFilterSession(snapshot, callingUid, info));
         return new ParceledListSlice<>(result);
     }
 
     @Override
     public ParceledListSlice<SessionInfo> getAllSessions(int userId) {
         final int callingUid = Binder.getCallingUid();
-        mPm.enforceCrossUserPermission(
-                callingUid, userId, true, false, "getAllSessions");
+        final Computer snapshot = mPm.snapshotComputer();
+        snapshot.enforceCrossUserPermission(callingUid, userId, true, false, "getAllSessions");
 
         final List<SessionInfo> result = new ArrayList<>();
         synchronized (mSessions) {
@@ -1090,15 +1092,16 @@
                 }
             }
         }
-        result.removeIf(info -> shouldFilterSession(callingUid, info));
+        result.removeIf(info -> shouldFilterSession(snapshot, callingUid, info));
         return new ParceledListSlice<>(result);
     }
 
     @Override
     public ParceledListSlice<SessionInfo> getMySessions(String installerPackageName, int userId) {
-        mPm.enforceCrossUserPermission(
-                Binder.getCallingUid(), userId, true, false, "getMySessions");
-        mAppOps.checkPackage(Binder.getCallingUid(), installerPackageName);
+        final Computer snapshot = mPm.snapshotComputer();
+        final int callingUid = Binder.getCallingUid();
+        snapshot.enforceCrossUserPermission(callingUid, userId, true, false, "getMySessions");
+        mAppOps.checkPackage(callingUid, installerPackageName);
 
         final List<SessionInfo> result = new ArrayList<>();
         synchronized (mSessions) {
@@ -1120,8 +1123,9 @@
     @Override
     public void uninstall(VersionedPackage versionedPackage, String callerPackageName, int flags,
                 IntentSender statusReceiver, int userId) {
+        final Computer snapshot = mPm.snapshotComputer();
         final int callingUid = Binder.getCallingUid();
-        mPm.enforceCrossUserPermission(callingUid, userId, true, true, "uninstall");
+        snapshot.enforceCrossUserPermission(callingUid, userId, true, true, "uninstall");
         if ((callingUid != Process.SHELL_UID) && (callingUid != Process.ROOT_UID)) {
             mAppOps.checkPackage(callingUid, callerPackageName);
         }
@@ -1154,8 +1158,7 @@
                     .setAdmin(callerPackageName)
                     .write();
         } else {
-            ApplicationInfo appInfo = mPm.mIPackageManager
-                    .getApplicationInfo(callerPackageName, 0, userId);
+            ApplicationInfo appInfo = snapshot.getApplicationInfo(callerPackageName, 0, userId);
             if (appInfo.targetSdkVersion >= Build.VERSION_CODES.P) {
                 mContext.enforceCallingOrSelfPermission(Manifest.permission.REQUEST_DELETE_PACKAGES,
                         null);
@@ -1174,7 +1177,8 @@
             String callerPackageName, IntentSender statusReceiver, int userId) {
         final int callingUid = Binder.getCallingUid();
         mContext.enforceCallingOrSelfPermission(Manifest.permission.DELETE_PACKAGES, null);
-        mPm.enforceCrossUserPermission(callingUid, userId, true, true, "uninstall");
+        final Computer snapshot = mPm.snapshotComputer();
+        snapshot.enforceCrossUserPermission(callingUid, userId, true, true, "uninstall");
         if ((callingUid != Process.SHELL_UID) && (callingUid != Process.ROOT_UID)) {
             mAppOps.checkPackage(callingUid, callerPackageName);
         }
@@ -1206,8 +1210,9 @@
 
     @Override
     public void registerCallback(IPackageInstallerCallback callback, int userId) {
-        mPm.enforceCrossUserPermission(
-                Binder.getCallingUid(), userId, true, false, "registerCallback");
+        final Computer snapshot = mPm.snapshotComputer();
+        snapshot.enforceCrossUserPermission(Binder.getCallingUid(), userId, true, false,
+                "registerCallback");
         registerCallback(callback, eventUserId -> userId == eventUserId);
     }
 
@@ -1295,13 +1300,13 @@
         }
     }
 
-    private boolean shouldFilterSession(int uid, int sessionId) {
+    private boolean shouldFilterSession(@NonNull Computer snapshot, int uid, int sessionId) {
         final PackageInstallerSession session = getSession(sessionId);
         if (session == null) {
             return false;
         }
         return uid != session.getInstallerUid()
-                && !mPm.canQueryPackage(uid, session.getPackageName());
+                && !snapshot.canQueryPackage(uid, session.getPackageName());
     }
 
     static class PackageDeleteObserverAdapter extends PackageDeleteObserver {
@@ -1454,11 +1459,12 @@
             final int sessionId = msg.arg1;
             final int userId = msg.arg2;
             final int n = mCallbacks.beginBroadcast();
+            final Computer snapshot = mPm.snapshotComputer();
             for (int i = 0; i < n; i++) {
                 final IPackageInstallerCallback callback = mCallbacks.getBroadcastItem(i);
                 final BroadcastCookie cookie = (BroadcastCookie) mCallbacks.getBroadcastCookie(i);
                 if (cookie.userCheck.test(userId)
-                        && !shouldFilterSession(cookie.callingUid, sessionId)) {
+                        && !shouldFilterSession(snapshot, cookie.callingUid, sessionId)) {
                     try {
                         invokeCallback(callback, msg);
                     } catch (RemoteException ignored) {
diff --git a/services/core/java/com/android/server/pm/PackageInstallerSession.java b/services/core/java/com/android/server/pm/PackageInstallerSession.java
index ef7158a..68be64f 100644
--- a/services/core/java/com/android/server/pm/PackageInstallerSession.java
+++ b/services/core/java/com/android/server/pm/PackageInstallerSession.java
@@ -82,7 +82,6 @@
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageInstaller;
 import android.content.pm.PackageInstaller.SessionInfo;
-import android.content.pm.PackageInstaller.SessionInfo.SessionErrorCode;
 import android.content.pm.PackageInstaller.SessionParams;
 import android.content.pm.PackageManager;
 import android.content.pm.PackageManagerInternal;
@@ -127,6 +126,7 @@
 import android.text.TextUtils;
 import android.util.ArrayMap;
 import android.util.ArraySet;
+import android.util.EventLog;
 import android.util.ExceptionUtils;
 import android.util.IntArray;
 import android.util.MathUtils;
@@ -462,7 +462,7 @@
     @GuardedBy("mLock")
     private boolean mSessionFailed;
     @GuardedBy("mLock")
-    private int mSessionErrorCode = SessionInfo.SESSION_NO_ERROR;
+    private int mSessionErrorCode = PackageManager.INSTALL_UNKNOWN;
     @GuardedBy("mLock")
     private String mSessionErrorMessage;
 
@@ -860,7 +860,8 @@
             return USER_ACTION_NOT_NEEDED;
         }
 
-        if (mPm.isInstallDisabledForPackage(getInstallerPackageName(), mInstallerUid, userId)) {
+        if (snapshot.isInstallDisabledForPackage(getInstallerPackageName(), mInstallerUid,
+                userId)) {
             // show the installer to account for device poslicy or unknown sources use cases
             return USER_ACTION_REQUIRED;
         }
@@ -2331,7 +2332,7 @@
                 }
             } else {
                 PackageManagerException e = (PackageManagerException) t.getCause();
-                setSessionFailed(SessionInfo.SESSION_ACTIVATION_FAILED,
+                setSessionFailed(e.error,
                         PackageManager.installStatusToString(e.error, e.getMessage()));
                 dispatchSessionFinished(e.error, e.getMessage(), null);
                 maybeFinishChildSessions(e.error, e.getMessage());
@@ -2862,7 +2863,9 @@
                 inheritFileLocked(mResolvedBaseFile);
                 // Collect the requiredSplitTypes from base
                 CollectionUtils.addAll(requiredSplitTypes, existing.getBaseRequiredSplitTypes());
-            } else {
+            } else if ((params.installFlags & PackageManager.INSTALL_DONT_KILL_APP) != 0) {
+                EventLog.writeEvent(0x534e4554, "219044664");
+
                 // Installing base.apk. Make sure the app is restarted.
                 params.setDontKillApp(false);
             }
@@ -3753,7 +3756,8 @@
         };
 
         if (!manualStartAndDestroy) {
-            final PerUidReadTimeouts[] perUidReadTimeouts = mPm.getPerUidReadTimeouts();
+            final PerUidReadTimeouts[] perUidReadTimeouts =
+                    mPm.getPerUidReadTimeouts(mPm.snapshotComputer());
 
             final StorageHealthCheckParams healthCheckParams = new StorageHealthCheckParams();
             healthCheckParams.blockedTimeoutMs = INCREMENTAL_STORAGE_BLOCKED_TIMEOUT_MS;
@@ -4045,7 +4049,7 @@
             mSessionReady = true;
             mSessionApplied = false;
             mSessionFailed = false;
-            mSessionErrorCode = SessionInfo.SESSION_NO_ERROR;
+            mSessionErrorCode = PackageManager.INSTALL_UNKNOWN;
             mSessionErrorMessage = "";
         }
         mCallback.onSessionChanged(this);
@@ -4073,7 +4077,7 @@
             mSessionReady = false;
             mSessionApplied = true;
             mSessionFailed = false;
-            mSessionErrorCode = SessionInfo.SESSION_NO_ERROR;
+            mSessionErrorCode = INSTALL_SUCCEEDED;
             mSessionErrorMessage = "";
             Slog.d(TAG, "Marking session " + sessionId + " as applied");
         }
@@ -4103,7 +4107,6 @@
     }
 
     /** {@hide} */
-    @SessionErrorCode
     int getSessionErrorCode() {
         synchronized (mLock) {
             return mSessionErrorCode;
@@ -4592,7 +4595,7 @@
         final boolean isFailed = in.getAttributeBoolean(null, ATTR_IS_FAILED, false);
         final boolean isApplied = in.getAttributeBoolean(null, ATTR_IS_APPLIED, false);
         final int sessionErrorCode = in.getAttributeInt(null, ATTR_SESSION_ERROR_CODE,
-                SessionInfo.SESSION_NO_ERROR);
+                PackageManager.INSTALL_UNKNOWN);
         final String sessionErrorMessage = readStringAttribute(in, ATTR_SESSION_ERROR_MESSAGE);
 
         if (!isStagedSessionStateValid(isReady, isApplied, isFailed)) {
diff --git a/services/core/java/com/android/server/pm/PackageManagerInternalBase.java b/services/core/java/com/android/server/pm/PackageManagerInternalBase.java
new file mode 100644
index 0000000..2b73375
--- /dev/null
+++ b/services/core/java/com/android/server/pm/PackageManagerInternalBase.java
@@ -0,0 +1,751 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.pm;
+
+import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
+import static android.content.pm.PackageManager.RESTRICTION_NONE;
+
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.annotation.UserIdInt;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.ActivityInfo;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.AuxiliaryResolveInfo;
+import android.content.pm.Checksum;
+import android.content.pm.IOnChecksumsReadyListener;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+import android.content.pm.PackageManagerInternal;
+import android.content.pm.ProcessInfo;
+import android.content.pm.ProviderInfo;
+import android.content.pm.ResolveInfo;
+import android.content.pm.SuspendDialogInfo;
+import android.os.Binder;
+import android.os.Build;
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.Process;
+import android.os.storage.StorageManager;
+import android.util.ArrayMap;
+import android.util.ArraySet;
+import android.util.SparseArray;
+
+import com.android.server.pm.dex.DexManager;
+import com.android.server.pm.dex.DynamicCodeLogger;
+import com.android.server.pm.parsing.pkg.AndroidPackage;
+import com.android.server.pm.permission.PermissionManagerServiceInternal;
+import com.android.server.pm.pkg.AndroidPackageApi;
+import com.android.server.pm.pkg.PackageStateInternal;
+import com.android.server.pm.pkg.PackageStateUtils;
+import com.android.server.pm.pkg.SharedUserApi;
+import com.android.server.pm.pkg.component.ParsedMainComponent;
+import com.android.server.pm.pkg.mutate.PackageStateMutator;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.concurrent.Executor;
+import java.util.function.Consumer;
+
+/**
+ * Internal manager variant of {@link IPackageManagerBase}. See that class for info.
+ * {@link PackageManagerInternal} should eventually passing in a snapshot instance, deprecating
+ * this class, but that requires much larger refactor.
+ */
+abstract class PackageManagerInternalBase extends PackageManagerInternal {
+
+    @NonNull
+    private final PackageManagerService mService;
+
+    public PackageManagerInternalBase(@NonNull PackageManagerService service) {
+        mService = service;
+    }
+
+    @NonNull protected abstract Context getContext();
+    @NonNull protected abstract PermissionManagerServiceInternal getPermissionManager();
+    @NonNull protected abstract AppDataHelper getAppDataHelper();
+    @NonNull protected abstract PackageObserverHelper getPackageObserverHelper();
+    @NonNull protected abstract ResolveIntentHelper getResolveIntentHelper();
+    @NonNull protected abstract SuspendPackageHelper getSuspendPackageHelper();
+    @NonNull protected abstract ProtectedPackages getProtectedPackages();
+    @NonNull protected abstract UserNeedsBadgingCache getUserNeedsBadging();
+    @NonNull protected abstract InstantAppRegistry getInstantAppRegistry();
+    @NonNull protected abstract ApexManager getApexManager();
+    @NonNull protected abstract DexManager getDexManager();
+
+    @Override
+    public final Computer snapshot() {
+        return mService.snapshotComputer();
+    }
+
+    @Override
+    @Deprecated
+    public final List<ApplicationInfo> getInstalledApplications(
+            @PackageManager.ApplicationInfoFlagsBits long flags, int userId, int callingUid) {
+        return snapshot().getInstalledApplications(flags, userId, callingUid);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean isInstantApp(String packageName, int userId) {
+        return snapshot().isInstantApp(packageName, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final String getInstantAppPackageName(int uid) {
+        return snapshot().getInstantAppPackageName(uid);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean filterAppAccess(AndroidPackage pkg, int callingUid, int userId) {
+        return snapshot().filterAppAccess(pkg, callingUid, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean filterAppAccess(String packageName, int callingUid, int userId) {
+        return snapshot().filterAppAccess(packageName, callingUid, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean filterAppAccess(int uid, int callingUid) {
+        return snapshot().filterAppAccess(uid, callingUid);
+    }
+
+    @Nullable
+    @Override
+    @Deprecated
+    public final int[] getVisibilityAllowList(@NonNull String packageName, int userId) {
+        return snapshot().getVisibilityAllowList(packageName, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean canQueryPackage(int callingUid, @Nullable String packageName) {
+        return snapshot().canQueryPackage(callingUid, packageName);
+    }
+
+    @Override
+    @Deprecated
+    public final AndroidPackage getPackage(String packageName) {
+        return snapshot().getPackage(packageName);
+    }
+
+    @Nullable
+    @Override
+    @Deprecated
+    public final AndroidPackageApi getAndroidPackage(@NonNull String packageName) {
+        return snapshot().getPackage(packageName);
+    }
+
+    @Override
+    @Deprecated
+    public final AndroidPackage getPackage(int uid) {
+        return snapshot().getPackage(uid);
+    }
+
+    @Override
+    @Deprecated
+    public final List<AndroidPackage> getPackagesForAppId(int appId) {
+        return snapshot().getPackagesForAppId(appId);
+    }
+
+    @Nullable
+    @Override
+    @Deprecated
+    public final PackageStateInternal getPackageStateInternal(String packageName) {
+        return snapshot().getPackageStateInternal(packageName);
+    }
+
+    @NonNull
+    @Override
+    @Deprecated
+    public final ArrayMap<String, ? extends PackageStateInternal> getPackageStates() {
+        return snapshot().getPackageStates();
+    }
+
+    @Override
+    @Deprecated
+    public final void removePackageListObserver(PackageListObserver observer) {
+        getPackageObserverHelper().removeObserver(observer);
+    }
+
+    @Override
+    @Deprecated
+    public final PackageStateInternal getDisabledSystemPackage(@NonNull String packageName) {
+        return snapshot().getDisabledSystemPackage(packageName);
+    }
+
+    @Override
+    @Deprecated
+    public final @NonNull String[] getKnownPackageNames(int knownPackage, int userId) {
+        return mService.getKnownPackageNamesInternal(snapshot(), knownPackage, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final void setKeepUninstalledPackages(final List<String> packageList) {
+        mService.setKeepUninstalledPackagesInternal(snapshot(), packageList);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean isPermissionsReviewRequired(String packageName, int userId) {
+        return getPermissionManager().isPermissionsReviewRequired(packageName, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final PackageInfo getPackageInfo(String packageName,
+            @PackageManager.PackageInfoFlagsBits long flags, int filterCallingUid, int userId) {
+        return snapshot().getPackageInfoInternal(packageName,
+                PackageManager.VERSION_CODE_HIGHEST, flags, filterCallingUid, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final Bundle getSuspendedPackageLauncherExtras(String packageName, int userId) {
+        return getSuspendPackageHelper().getSuspendedPackageLauncherExtras(snapshot(), packageName,
+                userId, Binder.getCallingUid());
+    }
+
+    @Override
+    @Deprecated
+    public final boolean isPackageSuspended(String packageName, int userId) {
+        return getSuspendPackageHelper().isPackageSuspended(snapshot(), packageName, userId,
+                Binder.getCallingUid());
+    }
+
+    @Override
+    @Deprecated
+    public final void removeNonSystemPackageSuspensions(String packageName, int userId) {
+        getSuspendPackageHelper().removeSuspensionsBySuspendingPackage(snapshot(),
+                new String[]{packageName},
+                (suspendingPackage) -> !PackageManagerService.PLATFORM_PACKAGE_NAME.equals(
+                        suspendingPackage),
+                userId);
+    }
+
+    @Override
+    @Deprecated
+    public final void removeDistractingPackageRestrictions(String packageName, int userId) {
+        mService.removeDistractingPackageRestrictions(snapshot(), new String[]{packageName},
+                userId);
+    }
+
+    @Override
+    @Deprecated
+    public final void removeAllDistractingPackageRestrictions(int userId) {
+        mService.removeAllDistractingPackageRestrictions(snapshot(), userId);
+    }
+
+    @Override
+    @Deprecated
+    public final String getSuspendingPackage(String suspendedPackage, int userId) {
+        return getSuspendPackageHelper().getSuspendingPackage(snapshot(), suspendedPackage, userId,
+                Binder.getCallingUid());
+    }
+
+    @Override
+    @Deprecated
+    public final SuspendDialogInfo getSuspendedDialogInfo(String suspendedPackage,
+            String suspendingPackage, int userId) {
+        return getSuspendPackageHelper().getSuspendedDialogInfo(snapshot(), suspendedPackage,
+                suspendingPackage, userId, Binder.getCallingUid());
+    }
+
+    @Override
+    @Deprecated
+    public final int getDistractingPackageRestrictions(String packageName, int userId) {
+        final PackageStateInternal packageState = getPackageStateInternal(packageName);
+        return (packageState == null) ? RESTRICTION_NONE
+                : packageState.getUserStateOrDefault(userId).getDistractionFlags();
+    }
+
+    @Override
+    @Deprecated
+    public final int getPackageUid(String packageName,
+            @PackageManager.PackageInfoFlagsBits long flags, int userId) {
+        return snapshot().getPackageUidInternal(packageName, flags, userId, Process.SYSTEM_UID);
+    }
+
+    @Override
+    @Deprecated
+    public final ApplicationInfo getApplicationInfo(String packageName,
+            @PackageManager.ApplicationInfoFlagsBits long flags, int filterCallingUid, int userId) {
+        return snapshot().getApplicationInfoInternal(packageName, flags, filterCallingUid, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final ActivityInfo getActivityInfo(ComponentName component,
+            @PackageManager.ComponentInfoFlagsBits long flags, int filterCallingUid, int userId) {
+        return snapshot().getActivityInfoInternal(component, flags, filterCallingUid, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final List<ResolveInfo> queryIntentActivities(
+            Intent intent, String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags,
+            int filterCallingUid, int userId) {
+        return snapshot().queryIntentActivitiesInternal(intent, resolvedType, flags, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final List<ResolveInfo> queryIntentReceivers(Intent intent,
+            String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags,
+            int filterCallingUid, int userId) {
+        return getResolveIntentHelper().queryIntentReceiversInternal(
+                snapshot(), intent, resolvedType, flags, userId, filterCallingUid);
+    }
+
+    @Override
+    @Deprecated
+    public final List<ResolveInfo> queryIntentServices(
+            Intent intent, @PackageManager.ResolveInfoFlagsBits long flags, int callingUid,
+            int userId) {
+        final String resolvedType = intent.resolveTypeIfNeeded(getContext().getContentResolver());
+        return snapshot().queryIntentServicesInternal(intent, resolvedType, flags, userId,
+                callingUid, false);
+    }
+
+    @Override
+    @Deprecated
+    public final ComponentName getHomeActivitiesAsUser(List<ResolveInfo> allHomeCandidates,
+            int userId) {
+        return snapshot().getHomeActivitiesAsUser(allHomeCandidates, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final ComponentName getDefaultHomeActivity(int userId) {
+        return snapshot().getDefaultHomeActivity(userId);
+    }
+
+    @Override
+    @Deprecated
+    public final ComponentName getSystemUiServiceComponent() {
+        return ComponentName.unflattenFromString(getContext().getResources().getString(
+                com.android.internal.R.string.config_systemUIServiceComponent));
+    }
+
+    @Override
+    @Deprecated
+    public final void setDeviceOwnerProtectedPackages(
+            String deviceOwnerPackageName, List<String> packageNames) {
+        getProtectedPackages().setDeviceOwnerProtectedPackages(
+                deviceOwnerPackageName, packageNames);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean isPackageDataProtected(int userId, String packageName) {
+        return getProtectedPackages().isPackageDataProtected(userId, packageName);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean isPackageStateProtected(String packageName, int userId) {
+        return getProtectedPackages().isPackageStateProtected(userId, packageName);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean isPackageEphemeral(int userId, String packageName) {
+        final PackageStateInternal packageState = getPackageStateInternal(packageName);
+        return packageState != null
+                && packageState.getUserStateOrDefault(userId).isInstantApp();
+    }
+
+    @Override
+    @Deprecated
+    public final boolean wasPackageEverLaunched(String packageName, int userId) {
+        final PackageStateInternal packageState = getPackageStateInternal(packageName);
+        if (packageState == null) {
+            throw new IllegalArgumentException("Unknown package: " + packageName);
+        }
+        return !packageState.getUserStateOrDefault(userId).isNotLaunched();
+    }
+
+    @Override
+    @Deprecated
+    public final boolean isEnabledAndMatches(ParsedMainComponent component, long flags, int userId) {
+        return PackageStateUtils.isEnabledAndMatches(
+                getPackageStateInternal(component.getPackageName()), component, flags, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean userNeedsBadging(int userId) {
+        return getUserNeedsBadging().get(userId);
+    }
+
+    @Override
+    @Deprecated
+    public final String getNameForUid(int uid) {
+        return snapshot().getNameForUid(uid);
+    }
+
+    @Override
+    @Deprecated
+    public final void requestInstantAppResolutionPhaseTwo(AuxiliaryResolveInfo responseObj,
+            Intent origIntent, String resolvedType, String callingPackage,
+            @Nullable String callingFeatureId, boolean isRequesterInstantApp,
+            Bundle verificationBundle, int userId) {
+        mService.requestInstantAppResolutionPhaseTwo(responseObj, origIntent,
+                resolvedType, callingPackage, callingFeatureId, isRequesterInstantApp,
+                verificationBundle, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final void grantImplicitAccess(int userId, Intent intent,
+            int recipientAppId, int visibleUid, boolean direct) {
+        grantImplicitAccess(userId, intent, recipientAppId, visibleUid, direct,
+                false /* retainOnUpdate */);
+    }
+
+    @Override
+    @Deprecated
+    public final void grantImplicitAccess(int userId, Intent intent,
+            int recipientAppId, int visibleUid, boolean direct, boolean retainOnUpdate) {
+        mService.grantImplicitAccess(snapshot(), userId, intent,
+                recipientAppId, visibleUid, direct, retainOnUpdate);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean isInstantAppInstallerComponent(ComponentName component) {
+        final ActivityInfo instantAppInstallerActivity = mService.mInstantAppInstallerActivity;
+        return instantAppInstallerActivity != null
+                && instantAppInstallerActivity.getComponentName().equals(component);
+    }
+
+    @Override
+    @Deprecated
+    public final void pruneInstantApps() {
+        getInstantAppRegistry().pruneInstantApps(snapshot());
+    }
+
+    @Override
+    @Deprecated
+    public final String getSetupWizardPackageName() {
+        return mService.mSetupWizardPackage;
+    }
+
+    @Override
+    @Deprecated
+    public final ResolveInfo resolveIntent(Intent intent, String resolvedType,
+            @PackageManager.ResolveInfoFlagsBits long flags,
+            @PackageManagerInternal.PrivateResolveFlags long privateResolveFlags, int userId,
+            boolean resolveForStart, int filterCallingUid) {
+        return getResolveIntentHelper().resolveIntentInternal(snapshot(),
+                intent, resolvedType, flags, privateResolveFlags, userId, resolveForStart,
+                filterCallingUid);
+    }
+
+    @Override
+    @Deprecated
+    public final ResolveInfo resolveService(Intent intent, String resolvedType,
+            @PackageManager.ResolveInfoFlagsBits long flags, int userId, int callingUid) {
+        return getResolveIntentHelper().resolveServiceInternal(snapshot(), intent,
+                resolvedType, flags, userId, callingUid);
+    }
+
+    @Override
+    @Deprecated
+    public final ProviderInfo resolveContentProvider(String name,
+            @PackageManager.ResolveInfoFlagsBits long flags, int userId, int callingUid) {
+        return snapshot().resolveContentProvider(name, flags, userId,callingUid);
+    }
+
+    @Override
+    @Deprecated
+    public final int getUidTargetSdkVersion(int uid) {
+        return snapshot().getUidTargetSdkVersion(uid);
+    }
+
+    @Override
+    @Deprecated
+    public final int getPackageTargetSdkVersion(String packageName) {
+        final PackageStateInternal packageState = getPackageStateInternal(packageName);
+        if (packageState != null && packageState.getPkg() != null) {
+            return packageState.getPkg().getTargetSdkVersion();
+        }
+        return Build.VERSION_CODES.CUR_DEVELOPMENT;
+    }
+
+    @Override
+    @Deprecated
+    public final boolean canAccessInstantApps(int callingUid, @UserIdInt int userId) {
+        return snapshot().canViewInstantApps(callingUid, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean canAccessComponent(int callingUid, @NonNull ComponentName component,
+            @UserIdInt int userId) {
+        return snapshot().canAccessComponent(callingUid, component, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean hasInstantApplicationMetadata(String packageName, int userId) {
+        return getInstantAppRegistry().hasInstantApplicationMetadata(packageName, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final SparseArray<String> getAppsWithSharedUserIds() {
+        return snapshot().getAppsWithSharedUserIds();
+    }
+
+    @Override
+    @NonNull
+    @Deprecated
+    public final String[] getSharedUserPackagesForPackage(String packageName, int userId) {
+        return snapshot().getSharedUserPackagesForPackage(packageName, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final ArrayMap<String, ProcessInfo> getProcessesForUid(int uid) {
+        return snapshot().getProcessesForUid(uid);
+    }
+
+    @Override
+    @Deprecated
+    public final int[] getPermissionGids(String permissionName, int userId) {
+        return getPermissionManager().getPermissionGids(permissionName, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean isOnlyCoreApps() {
+        return mService.isOnlyCoreApps();
+    }
+
+    @Override
+    @Deprecated
+    public final void freeStorage(String volumeUuid, long bytes,
+            @StorageManager.AllocateFlags int flags) throws IOException {
+        mService.freeStorage(volumeUuid, bytes, flags);
+    }
+
+    @Override
+    @Deprecated
+    public final void freeAllAppCacheAboveQuota(@NonNull String volumeUuid) throws IOException {
+        mService.freeAllAppCacheAboveQuota(volumeUuid);
+    }
+
+    @Override
+    @Deprecated
+    public final void forEachPackageSetting(Consumer<PackageSetting> actionLocked) {
+        mService.forEachPackageSetting(actionLocked);
+    }
+
+    @Override
+    @Deprecated
+    public final void forEachPackageState(Consumer<PackageStateInternal> action) {
+        mService.forEachPackageState(snapshot(), action);
+    }
+
+    @Override
+    @Deprecated
+    public final void forEachPackage(Consumer<AndroidPackage> action) {
+        mService.forEachPackage(snapshot(), action);
+    }
+
+    @Override
+    @Deprecated
+    public final void forEachInstalledPackage(@NonNull Consumer<AndroidPackage> action,
+            @UserIdInt int userId) {
+        mService.forEachInstalledPackage(snapshot(), action, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final ArraySet<String> getEnabledComponents(String packageName, int userId) {
+        final PackageStateInternal packageState = getPackageStateInternal(packageName);
+        if (packageState == null) {
+            return new ArraySet<>();
+        }
+        return packageState.getUserStateOrDefault(userId).getEnabledComponents();
+    }
+
+    @Override
+    @Deprecated
+    public final ArraySet<String> getDisabledComponents(String packageName, int userId) {
+        final PackageStateInternal packageState = getPackageStateInternal(packageName);
+        if (packageState == null) {
+            return new ArraySet<>();
+        }
+        return packageState.getUserStateOrDefault(userId).getDisabledComponents();
+    }
+
+    @Override
+    @Deprecated
+    public final @PackageManager.EnabledState int getApplicationEnabledState(
+            String packageName, int userId) {
+        final PackageStateInternal packageState = getPackageStateInternal(packageName);
+        if (packageState == null) {
+            return COMPONENT_ENABLED_STATE_DEFAULT;
+        }
+        return packageState.getUserStateOrDefault(userId).getEnabledState();
+    }
+
+    @Override
+    @Deprecated
+    public final @PackageManager.EnabledState int getComponentEnabledSetting(
+            @NonNull ComponentName componentName, int callingUid, int userId) {
+        return snapshot().getComponentEnabledSettingInternal(
+                componentName, callingUid, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final void setEnableRollbackCode(int token, int enableRollbackCode) {
+        mService.setEnableRollbackCode(token, enableRollbackCode);
+    }
+
+    @Override
+    @Deprecated
+    public final void finishPackageInstall(int token, boolean didLaunch) {
+        mService.finishPackageInstall(token, didLaunch);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean isApexPackage(String packageName) {
+        return getApexManager().isApexPackage(packageName);
+    }
+
+    @Override
+    @Deprecated
+    public final List<String> getApksInApex(String apexPackageName) {
+        return getApexManager().getApksInApex(apexPackageName);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean isCallerInstallerOfRecord(@NonNull AndroidPackage pkg, int callingUid) {
+        return snapshot().isCallerInstallerOfRecord(pkg, callingUid);
+    }
+
+    @Override
+    @Deprecated
+    public final List<String> getMimeGroup(String packageName, String mimeGroup) {
+        return mService.getMimeGroupInternal(snapshot(), packageName, mimeGroup);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean isSystemPackage(@NonNull String packageName) {
+        return packageName.equals(mService.ensureSystemPackageName(snapshot(), packageName));
+    }
+
+    @Override
+    @Deprecated
+    public final void unsuspendForSuspendingPackage(final String packageName, int affectedUser) {
+        mService.unsuspendForSuspendingPackage(snapshot(), packageName, affectedUser);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean isSuspendingAnyPackages(String suspendingPackage, int userId) {
+        return snapshot().isSuspendingAnyPackages(suspendingPackage, userId);
+    }
+
+    @Override
+    @Deprecated
+    public final void requestChecksums(@NonNull String packageName, boolean includeSplits,
+            @Checksum.TypeMask int optional, @Checksum.TypeMask int required,
+            @Nullable List trustedInstallers,
+            @NonNull IOnChecksumsReadyListener onChecksumsReadyListener, int userId,
+            @NonNull Executor executor, @NonNull Handler handler) {
+        mService.requestChecksumsInternal(snapshot(), packageName, includeSplits, optional,
+                required, trustedInstallers, onChecksumsReadyListener, userId, executor,
+                handler);
+    }
+
+    @Override
+    @Deprecated
+    public final boolean isPackageFrozen(@NonNull String packageName,
+            int callingUid, int userId) {
+        return snapshot().getPackageStartability(mService.getSafeMode(), packageName, callingUid, userId)
+                == PackageManagerService.PACKAGE_STARTABILITY_FROZEN;
+    }
+
+    @Override
+    @Deprecated
+    public final long deleteOatArtifactsOfPackage(String packageName) {
+        return mService.deleteOatArtifactsOfPackage(snapshot(), packageName);
+    }
+
+    @Override
+    @Deprecated
+    public final void reconcileAppsData(int userId, @StorageManager.StorageFlags int flags,
+            boolean migrateAppsData) {
+        getAppDataHelper().reconcileAppsData(userId, flags, migrateAppsData);
+    }
+
+    @Override
+    @NonNull
+    public ArraySet<PackageStateInternal> getSharedUserPackages(int sharedUserAppId) {
+        return snapshot().getSharedUserPackages(sharedUserAppId);
+    }
+
+    @Override
+    @Nullable
+    public SharedUserApi getSharedUserApi(int sharedUserAppId) {
+        return snapshot().getSharedUser(sharedUserAppId);
+    }
+
+    @NonNull
+    @Override
+    @Deprecated
+    public final PackageStateMutator.InitialState recordInitialState() {
+        return mService.recordInitialState();
+    }
+
+    @Nullable
+    @Override
+    @Deprecated
+    public final PackageStateMutator.Result commitPackageStateMutation(
+            @Nullable PackageStateMutator.InitialState state,
+            @NonNull Consumer<PackageStateMutator> consumer) {
+        return mService.commitPackageStateMutation(state, consumer);
+    }
+
+    @Override
+    @Deprecated
+    public final void shutdown() {
+        mService.shutdown();
+    }
+
+    @Override
+    @Deprecated
+    public final DynamicCodeLogger getDynamicCodeLogger() {
+        return getDexManager().getDynamicCodeLogger();
+    }
+}
diff --git a/services/core/java/com/android/server/pm/PackageManagerService.java b/services/core/java/com/android/server/pm/PackageManagerService.java
index e20a861..67056ea 100644
--- a/services/core/java/com/android/server/pm/PackageManagerService.java
+++ b/services/core/java/com/android/server/pm/PackageManagerService.java
@@ -29,7 +29,6 @@
 import static android.content.pm.PackageManager.MATCH_FACTORY_ONLY;
 import static android.content.pm.PackageManager.MATCH_SYSTEM_ONLY;
 import static android.content.pm.PackageManager.PERMISSION_GRANTED;
-import static android.content.pm.PackageManager.RESTRICTION_NONE;
 import static android.os.Trace.TRACE_TAG_PACKAGE_MANAGER;
 import static android.os.storage.StorageManager.FLAG_STORAGE_CE;
 import static android.os.storage.StorageManager.FLAG_STORAGE_DE;
@@ -83,21 +82,15 @@
 import android.content.pm.IOnChecksumsReadyListener;
 import android.content.pm.IPackageChangeObserver;
 import android.content.pm.IPackageDataObserver;
-import android.content.pm.IPackageDeleteObserver;
 import android.content.pm.IPackageDeleteObserver2;
 import android.content.pm.IPackageInstallObserver2;
-import android.content.pm.IPackageInstaller;
 import android.content.pm.IPackageLoadingProgressCallback;
 import android.content.pm.IPackageManager;
 import android.content.pm.IPackageMoveObserver;
-import android.content.pm.IPackageStatsObserver;
 import android.content.pm.IncrementalStatesInfo;
 import android.content.pm.InstallSourceInfo;
 import android.content.pm.InstantAppInfo;
 import android.content.pm.InstantAppRequest;
-import android.content.pm.InstrumentationInfo;
-import android.content.pm.IntentFilterVerificationInfo;
-import android.content.pm.KeySet;
 import android.content.pm.ModuleInfo;
 import android.content.pm.PackageChangeEvent;
 import android.content.pm.PackageInfo;
@@ -109,11 +102,8 @@
 import android.content.pm.PackagePartitions;
 import android.content.pm.ParceledListSlice;
 import android.content.pm.PermissionGroupInfo;
-import android.content.pm.PermissionInfo;
-import android.content.pm.ProcessInfo;
 import android.content.pm.ProviderInfo;
 import android.content.pm.ResolveInfo;
-import android.content.pm.ServiceInfo;
 import android.content.pm.SharedLibraryInfo;
 import android.content.pm.Signature;
 import android.content.pm.SigningDetails;
@@ -122,7 +112,6 @@
 import android.content.pm.UserInfo;
 import android.content.pm.VerifierDeviceIdentity;
 import android.content.pm.VersionedPackage;
-import android.content.pm.dex.IArtManager;
 import android.content.pm.overlay.OverlayPaths;
 import android.content.pm.parsing.PackageLite;
 import android.content.res.Resources;
@@ -213,7 +202,6 @@
 import com.android.server.pm.dex.ArtManagerService;
 import com.android.server.pm.dex.ArtUtils;
 import com.android.server.pm.dex.DexManager;
-import com.android.server.pm.dex.DynamicCodeLogger;
 import com.android.server.pm.dex.ViewCompiler;
 import com.android.server.pm.parsing.PackageCacher;
 import com.android.server.pm.parsing.PackageInfoUtils;
@@ -225,10 +213,7 @@
 import com.android.server.pm.permission.LegacyPermissionManagerService;
 import com.android.server.pm.permission.PermissionManagerService;
 import com.android.server.pm.permission.PermissionManagerServiceInternal;
-import com.android.server.pm.pkg.AndroidPackageApi;
-import com.android.server.pm.pkg.PackageState;
 import com.android.server.pm.pkg.PackageStateInternal;
-import com.android.server.pm.pkg.PackageStateUtils;
 import com.android.server.pm.pkg.PackageUserState;
 import com.android.server.pm.pkg.PackageUserStateInternal;
 import com.android.server.pm.pkg.SharedUserApi;
@@ -413,7 +398,8 @@
     public @interface ScanFlags {}
 
     /**
-     * Used as the result code of the {@link #getPackageStartability}.
+     * Used as the result code of the {@link Computer#getPackageStartability(boolean, String, int,
+     * int)}.
      */
     @IntDef(value = {
         PACKAGE_STARTABILITY_OK,
@@ -426,40 +412,43 @@
     public @interface PackageStartability {}
 
     /**
-     * Used as the result code of the {@link #getPackageStartability} to indicate
-     * the given package is allowed to start.
+     * Used as the result code of the {@link Computer#getPackageStartability(boolean, String, int,
+     * int)} to indicate the given package is allowed to start.
      */
     public static final int PACKAGE_STARTABILITY_OK = 0;
 
     /**
-     * Used as the result code of the {@link #getPackageStartability} to indicate
-     * the given package is <b>not</b> allowed to start because it's not found
+     * Used as the result code of the {@link Computer#getPackageStartability(boolean, String, int,
+     * int)} to indicate the given package is <b>not</b> allowed to start because it's not found
      * (could be due to that package is invisible to the given user).
      */
     public static final int PACKAGE_STARTABILITY_NOT_FOUND = 1;
 
     /**
-     * Used as the result code of the {@link #getPackageStartability} to indicate
-     * the given package is <b>not</b> allowed to start because it's not a system app
-     * and the system is running in safe mode.
+     * Used as the result code of the {@link Computer#getPackageStartability(boolean, String, int,
+     * int)} to indicate the given package is <b>not</b> allowed to start because it's not a system
+     * app and the system is running in safe mode.
      */
     public static final int PACKAGE_STARTABILITY_NOT_SYSTEM = 2;
 
     /**
-     * Used as the result code of the {@link #getPackageStartability} to indicate
-     * the given package is <b>not</b> allowed to start because it's currently frozen.
+     * Used as the result code of the {@link Computer#getPackageStartability(boolean, String, int,
+     * int)} to indicate the given package is <b>not</b> allowed to start because it's currently
+     * frozen.
      */
     public static final int PACKAGE_STARTABILITY_FROZEN = 3;
 
     /**
-     * Used as the result code of the {@link #getPackageStartability} to indicate
-     * the given package is <b>not</b> allowed to start because it doesn't support
+     * Used as the result code of the {@link Computer#getPackageStartability(boolean, String, int,
+     * int)} to indicate the given package is <b>not</b> allowed to start because it doesn't support
      * direct boot.
      */
     public static final int PACKAGE_STARTABILITY_DIRECT_BOOT_UNSUPPORTED = 4;
 
     private static final String STATIC_SHARED_LIB_DELIMITER = "_";
-    /** Extension of the compressed packages */
+    /**
+     * Extension of the compressed packages
+     */
     public final static String COMPRESSED_EXTENSION = ".gz";
     /** Suffix of stub packages on the system partition */
     public final static String STUB_SUFFIX = "-Stub";
@@ -644,9 +633,6 @@
      */
     boolean mPromoteSystemApps;
 
-    // TODO: Make IPackageManager reference private to hide discouraged APIs
-    final IPackageManagerImpl mIPackageManager;
-    private final PackageManagerInternal mPmInternal;
     private final TestUtilityService mTestUtilityService;
 
     @Watched
@@ -1057,9 +1043,6 @@
     // A lock-free cache for frequently called functions.
     private volatile Computer mSnapshotComputer;
 
-    // A trampoline that directs callers to either the live or snapshot computer.
-    final ComputerTracker mComputer = new ComputerTracker(this);
-
     // If true, the snapshot is invalid (stale).  The attribute is static since it may be
     // set from outside classes.  The attribute may be set to true anywhere, although it
     // should only be set true while holding mLock.  However, the attribute id guaranteed
@@ -1088,6 +1071,8 @@
      * Return the cached computer.  The method will rebuild the cached computer if necessary.
      * The live computer will be returned if snapshots are disabled.
      */
+    @VisibleForTesting(visibility = Visibility.PACKAGE)
+    @NonNull
     public Computer snapshotComputer() {
         if (Thread.holdsLock(mLock)) {
             // If the current thread holds mLock then it may have modified state but not
@@ -1247,15 +1232,15 @@
             ApkChecksums.Injector injector = new ApkChecksums.Injector(
                     () -> mContext,
                     () -> handler,
-                    () -> mInjector.getIncrementalManager(),
-                    () -> mPmInternal);
+                    mInjector::getIncrementalManager,
+                    () -> mInjector.getLocalService(PackageManagerInternal.class));
             ApkChecksums.getChecksums(filesToChecksum, optional, required, installerPackageName,
                     trustedCerts, onChecksumsReadyListener, injector);
         });
     }
 
-    private void requestChecksumsInternal(@NonNull String packageName, boolean includeSplits,
-            @Checksum.TypeMask int optional, @Checksum.TypeMask int required,
+    void requestChecksumsInternal(@NonNull Computer snapshot, @NonNull String packageName,
+            boolean includeSplits, @Checksum.TypeMask int optional, @Checksum.TypeMask int required,
             @Nullable List trustedInstallers,
             @NonNull IOnChecksumsReadyListener onChecksumsReadyListener, int userId,
             @NonNull Executor executor, @NonNull Handler handler) {
@@ -1264,13 +1249,12 @@
         Objects.requireNonNull(executor);
         Objects.requireNonNull(handler);
 
-        final ApplicationInfo applicationInfo = getApplicationInfoInternal(packageName, 0,
+        final ApplicationInfo applicationInfo = snapshot.getApplicationInfoInternal(packageName, 0,
                 Binder.getCallingUid(), userId);
         if (applicationInfo == null) {
             throw new ParcelableException(new PackageManager.NameNotFoundException(packageName));
         }
-        final InstallSourceInfo installSourceInfo =
-                mIPackageManager.getInstallSourceInfo(packageName);
+        final InstallSourceInfo installSourceInfo = snapshot.getInstallSourceInfo(packageName);
         final String installerPackageName =
                 installSourceInfo != null ? installSourceInfo.getInitiatingPackageName() : null;
 
@@ -1294,8 +1278,8 @@
             ApkChecksums.Injector injector = new ApkChecksums.Injector(
                     () -> mContext,
                     () -> handler,
-                    () -> mInjector.getIncrementalManager(),
-                    () -> mPmInternal);
+                    mInjector::getIncrementalManager,
+                    () -> mInjector.getLocalService(PackageManagerInternal.class));
             ApkChecksums.getChecksums(filesToChecksum, optional, required, installerPackageName,
                     trustedCerts, onChecksumsReadyListener, injector);
         });
@@ -1440,15 +1424,15 @@
                         RuntimePermissionsPersistence.createInstance(),
                         i.getPermissionManagerServiceInternal(),
                         domainVerificationService, lock),
-                (i, pm) -> AppsFilter.create(pm.mPmInternal, i),
+                (i, pm) -> AppsFilter.create(i, i.getLocalService(PackageManagerInternal.class)),
                 (i, pm) -> (PlatformCompat) ServiceManager.getService("platform_compat"),
                 (i, pm) -> SystemConfig.getInstance(),
                 (i, pm) -> new PackageDexOptimizer(i.getInstaller(), i.getInstallLock(),
                         i.getContext(), "*dexopt*"),
-                (i, pm) -> new DexManager(i.getContext(), pm.mIPackageManager,
-                        i.getPackageDexOptimizer(), i.getInstaller(), i.getInstallLock()),
-                (i, pm) -> new ArtManagerService(i.getContext(), pm.mIPackageManager,
+                (i, pm) -> new DexManager(i.getContext(), i.getPackageDexOptimizer(),
                         i.getInstaller(), i.getInstallLock()),
+                (i, pm) -> new ArtManagerService(i.getContext(), i.getInstaller(),
+                        i.getInstallLock()),
                 (i, pm) -> ApexManager.getInstance(),
                 (i, pm) -> new ViewCompiler(i.getInstallLock(), i.getInstaller()),
                 (i, pm) -> (IncrementalManager)
@@ -1470,7 +1454,7 @@
                         i.getContext(), pm, i::getScanningPackageParser),
                 (i, pm, cn) -> new InstantAppResolverConnection(
                         i.getContext(), cn, Intent.ACTION_RESOLVE_INSTANT_APP_PACKAGE),
-                (i, pm) -> new ModuleInfoProvider(i.getContext(), pm.mIPackageManager),
+                (i, pm) -> new ModuleInfoProvider(i.getContext()),
                 (i, pm) -> LegacyPermissionManagerService.create(i.getContext()),
                 (i, pm) -> domainVerificationService,
                 (i, pm) -> {
@@ -1498,13 +1482,15 @@
 
         final CompatChange.ChangeListener selinuxChangeListener = packageName -> {
             synchronized (m.mInstallLock) {
-                final PackageStateInternal packageState = m.getPackageStateInternal(packageName);
+                final Computer snapshot = m.snapshotComputer();
+                final PackageStateInternal packageState =
+                        snapshot.getPackageStateInternal(packageName);
                 if (packageState == null) {
                     Slog.e(TAG, "Failed to find package setting " + packageName);
                     return;
                 }
                 AndroidPackage pkg = packageState.getPkg();
-                SharedUserApi sharedUser = m.mComputer.getSharedUser(
+                SharedUserApi sharedUser = snapshot.getSharedUser(
                         packageState.getSharedUserAppId());
                 String oldSeInfo = AndroidPackageUtils.getSeInfo(pkg, packageState);
 
@@ -1531,11 +1517,12 @@
                 selinuxChangeListener);
 
         m.installAllowlistedSystemPackages();
-        ServiceManager.addService("package", m.mIPackageManager);
+        IPackageManagerImpl iPackageManager = m.new IPackageManagerImpl();
+        ServiceManager.addService("package", iPackageManager);
         final PackageManagerNative pmn = new PackageManagerNative(m);
         ServiceManager.addService("package_native", pmn);
         LocalManagerRegistry.addManager(PackageManagerLocal.class, m.new PackageManagerLocalImpl());
-        return Pair.create(m, m.mIPackageManager);
+        return Pair.create(m, iPackageManager);
     }
 
     /** Install/uninstall system packages for all users based on their user-type, as applicable. */
@@ -1641,8 +1628,6 @@
         mPackageDexOptimizer = testParams.packageDexOptimizer;
         mPackageParserCallback = testParams.packageParserCallback;
         mPendingBroadcasts = testParams.pendingPackageBroadcasts;
-        mIPackageManager = new IPackageManagerImpl();
-        mPmInternal = testParams.pmInternal;
         mTestUtilityService = testParams.testUtilityService;
         mProcessLoggingHandler = testParams.processLoggingHandler;
         mProtectedPackages = testParams.protectedPackages;
@@ -1703,7 +1688,6 @@
     public PackageManagerService(PackageManagerServiceInjector injector, boolean onlyCore,
             boolean factoryTest, final String buildFingerprint, final boolean isEngBuild,
             final boolean isUserDebugBuild, final int sdkVersion, final String incrementalVersion) {
-        mIPackageManager = new IPackageManagerImpl();
         mIsEngBuild = isEngBuild;
         mIsUserDebugBuild = isUserDebugBuild;
         mSdkVersion = sdkVersion;
@@ -1734,10 +1718,9 @@
         t.traceBegin("createSubComponents");
 
         // Expose private service for system components to use.
-        mPmInternal = new PackageManagerInternalImpl();
+        LocalServices.addService(PackageManagerInternal.class, new PackageManagerInternalImpl());
         LocalServices.addService(TestUtilityService.class, this);
         mTestUtilityService = LocalServices.getService(TestUtilityService.class);
-        LocalServices.addService(PackageManagerInternal.class, mPmInternal);
         mUserManager = injector.getUserManagerService();
         mUserNeedsBadging = new UserNeedsBadgingCache(mUserManager);
         mComponentResolver = injector.getComponentResolver();
@@ -1755,7 +1738,7 @@
 
             @Override
             public boolean hasFeature(String feature) {
-                return PackageManagerService.this.mIPackageManager.hasSystemFeature(feature, 0);
+                return PackageManagerService.this.hasSystemFeature(feature, 0);
             }
         };
 
@@ -1885,9 +1868,10 @@
                 final int dependencyCount = entry.dependencies.length;
                 for (int j = 0; j < dependencyCount; j++) {
                     final SharedLibraryInfo dependency =
-                        getSharedLibraryInfo(entry.dependencies[j], undefinedVersion);
+                        computer.getSharedLibraryInfo(entry.dependencies[j], undefinedVersion);
                     if (dependency != null) {
-                        getSharedLibraryInfo(name, undefinedVersion).addDependency(dependency);
+                        computer.getSharedLibraryInfo(name, undefinedVersion)
+                                .addDependency(dependency);
                     }
                 }
             }
@@ -1899,7 +1883,7 @@
             t.traceEnd();
 
             t.traceBegin("read user settings");
-            mFirstBoot = !mSettings.readLPw(mLiveComputer,
+            mFirstBoot = !mSettings.readLPw(computer,
                     mInjector.getUserManagerInternal().getUsers(
                     /* excludePartial= */ true,
                     /* excludeDying= */ false,
@@ -1980,19 +1964,26 @@
             // Resolve protected action filters. Only the setup wizard is allowed to
             // have a high priority filter for these actions.
             mSetupWizardPackage = getSetupWizardPackageNameImpl(computer);
-            mComponentResolver.fixProtectedFilterPriorities(mPmInternal.getSetupWizardPackageName());
+            mComponentResolver.fixProtectedFilterPriorities(mSetupWizardPackage);
 
-            mDefaultTextClassifierPackage = mIPackageManager.getDefaultTextClassifierPackageName();
-            mSystemTextClassifierPackageName =
-                    mIPackageManager.getSystemTextClassifierPackageName();
-            mConfiguratorPackage = getDeviceConfiguratorPackageName();
-            mAppPredictionServicePackage = mIPackageManager.getAppPredictionServicePackageName();
-            mIncidentReportApproverPackage =
-                    mIPackageManager.getIncidentReportApproverPackageName();
+            mDefaultTextClassifierPackage = ensureSystemPackageName(computer,
+                    mContext.getString(R.string.config_servicesExtensionPackage));
+            mSystemTextClassifierPackageName = ensureSystemPackageName(computer,
+                    mContext.getString(R.string.config_defaultTextClassifierPackage));
+            mConfiguratorPackage = ensureSystemPackageName(computer,
+                    mContext.getString(R.string.config_deviceConfiguratorPackageName));
+            mAppPredictionServicePackage = ensureSystemPackageName(computer,
+                    getPackageFromComponentString(R.string.config_defaultAppPredictionService));
+            mIncidentReportApproverPackage = ensureSystemPackageName(computer,
+                    mContext.getString(R.string.config_incidentReportApproverPackage));
             mRetailDemoPackage = getRetailDemoPackageName();
-            mOverlayConfigSignaturePackage = getOverlayConfigSignaturePackageName();
-            mRecentsPackage = getRecentsPackageName();
-            mAmbientContextDetectionPackage = getAmbientContextDetectionPackageName();
+            mOverlayConfigSignaturePackage = ensureSystemPackageName(computer,
+                    mInjector.getSystemConfig().getOverlayConfigSignaturePackage());
+            mRecentsPackage = ensureSystemPackageName(computer,
+                    getPackageFromComponentString(R.string.config_recentsComponentName));
+            mAmbientContextDetectionPackage = ensureSystemPackageName(computer,
+                    getPackageFromComponentString(
+                            R.string.config_defaultAmbientContextDetectionService));
 
             // Now that we know all of the shared libraries, update all clients to have
             // the correct library paths.
@@ -2126,8 +2117,8 @@
 
                 mDomainVerificationManager.setProxy(domainVerificationProxy);
 
-                mServicesExtensionPackageName = getRequiredServicesExtensionPackageLPr();
-                mSharedSystemSharedLibraryPackageName = getRequiredSharedLibrary(
+                mServicesExtensionPackageName = getRequiredServicesExtensionPackageLPr(computer);
+                mSharedSystemSharedLibraryPackageName = getRequiredSharedLibrary(computer,
                         PackageManager.SYSTEM_SHARED_LIBRARY_SHARED,
                         SharedLibraryInfo.VERSION_UNDEFINED);
             } else {
@@ -2143,11 +2134,11 @@
             mRequiredPermissionControllerPackage = getRequiredPermissionControllerLPr(computer);
 
             mSettings.setPermissionControllerVersion(
-                    mIPackageManager.getPackageInfo(mRequiredPermissionControllerPackage, 0,
+                    computer.getPackageInfo(mRequiredPermissionControllerPackage, 0,
                             UserHandle.USER_SYSTEM).getLongVersionCode());
 
             // Resolve the sdk sandbox package
-            mRequiredSdkSandboxPackage = getRequiredSdkSandboxPackageName();
+            mRequiredSdkSandboxPackage = getRequiredSdkSandboxPackageName(computer);
 
             // Initialize InstantAppRegistry's Instant App list for all users.
             for (AndroidPackage pkg : mPackages.values()) {
@@ -2155,7 +2146,8 @@
                     continue;
                 }
                 for (int userId : userIds) {
-                    final PackageStateInternal ps = getPackageStateInternal(pkg.getPackageName());
+                    final PackageStateInternal ps =
+                            computer.getPackageStateInternal(pkg.getPackageName());
                     if (ps == null || !ps.getUserStateOrDefault(userId).isInstantApp()
                             || !ps.getUserStateOrDefault(userId).isInstalled()) {
                         continue;
@@ -2165,7 +2157,7 @@
             }
 
             mInstallerService = mInjector.getPackageInstallerService();
-            final ComponentName instantAppResolverComponent = getInstantAppResolver();
+            final ComponentName instantAppResolverComponent = getInstantAppResolver(computer);
             if (instantAppResolverComponent != null) {
                 if (DEBUG_INSTANT) {
                     Slog.d(TAG, "Set ephemeral resolver: " + instantAppResolverComponent);
@@ -2191,7 +2183,7 @@
             // scanning).
             final Map<Integer, List<PackageInfo>> userPackages = new HashMap<>();
             for (int userId : userIds) {
-                userPackages.put(userId, mIPackageManager.getInstalledPackages(/*flags*/ 0, userId)
+                userPackages.put(userId, computer.getInstalledPackages(/*flags*/ 0, userId)
                         .getList());
             }
             mDexManager.load(userPackages);
@@ -2202,7 +2194,7 @@
                         SystemClock.uptimeMillis() - startTime);
             }
 
-            // Rebild the live computer since some attributes have been rebuilt.
+            // Rebuild the live computer since some attributes have been rebuilt.
             mLiveComputer = createLiveComputer();
 
         } // synchronized (mLock)
@@ -2210,6 +2202,7 @@
         // CHECKSTYLE:ON IndentationCheck
 
         mModuleInfoProvider = mInjector.getModuleInfoProvider();
+
         mInjector.getSystemWrapper().enablePackageCaches();
 
         // Now after opening every single application zip, make sure they
@@ -2281,8 +2274,9 @@
     }
 
     @NonNull
-    private String getRequiredSharedLibrary(@NonNull String name, int version) {
-        SharedLibraryInfo libraryInfo = getSharedLibraryInfo(name, version);
+    private String getRequiredSharedLibrary(@NonNull Computer snapshot, @NonNull String name,
+            int version) {
+        SharedLibraryInfo libraryInfo = snapshot.getSharedLibraryInfo(name, version);
         if (libraryInfo == null) {
             throw new IllegalStateException("Missing required shared library:" + name);
         }
@@ -2294,9 +2288,9 @@
     }
 
     @NonNull
-    private String getRequiredServicesExtensionPackageLPr() {
+    private String getRequiredServicesExtensionPackageLPr(@NonNull Computer computer) {
         String servicesExtensionPackage =
-                ensureSystemPackageName(
+                ensureSystemPackageName(computer,
                         mContext.getString(R.string.config_servicesExtensionPackage));
         if (TextUtils.isEmpty(servicesExtensionPackage)) {
             throw new RuntimeException(
@@ -2375,7 +2369,7 @@
         for (int i = 0; i < N; i++) {
             final ResolveInfo cur = matches.get(i);
             final String packageName = cur.getComponentInfo().packageName;
-            if (mIPackageManager.checkPermission(
+            if (checkPermission(
                     android.Manifest.permission.INTENT_FILTER_VERIFICATION_AGENT, packageName,
                     UserHandle.USER_SYSTEM) != PackageManager.PERMISSION_GRANTED) {
                 continue;
@@ -2405,7 +2399,7 @@
         for (int i = 0; i < N; i++) {
             final ResolveInfo cur = matches.get(i);
             final String packageName = cur.getComponentInfo().packageName;
-            if (mIPackageManager.checkPermission(
+            if (checkPermission(
                     android.Manifest.permission.DOMAIN_VERIFICATION_AGENT, packageName,
                     UserHandle.USER_SYSTEM) != PackageManager.PERMISSION_GRANTED) {
                 Slog.w(TAG, "Domain verification agent found but does not hold permission: "
@@ -2414,7 +2408,7 @@
             }
 
             if (best == null || cur.priority > best.priority) {
-                if (mComputer.isComponentEffectivelyEnabled(cur.getComponentInfo(),
+                if (computer.isComponentEffectivelyEnabled(cur.getComponentInfo(),
                         UserHandle.USER_SYSTEM)) {
                     best = cur;
                 } else {
@@ -2430,7 +2424,7 @@
         return null;
     }
 
-    private @Nullable ComponentName getInstantAppResolver() {
+    @Nullable ComponentName getInstantAppResolver(@NonNull Computer snapshot) {
         final String[] packageArray =
                 mContext.getResources().getStringArray(R.array.config_ephemeralResolverPackage);
         if (packageArray.length == 0 && !Build.IS_DEBUGGABLE) {
@@ -2446,7 +2440,7 @@
                 | MATCH_DIRECT_BOOT_UNAWARE
                 | (!Build.IS_DEBUGGABLE ? MATCH_SYSTEM_ONLY : 0);
         final Intent resolverIntent = new Intent(Intent.ACTION_RESOLVE_INSTANT_APP_PACKAGE);
-        List<ResolveInfo> resolvers = queryIntentServicesInternal(resolverIntent, null,
+        List<ResolveInfo> resolvers = snapshot.queryIntentServicesInternal(resolverIntent, null,
                 resolveFlags, UserHandle.USER_SYSTEM, callingUid, false /*includeInstantApps*/);
         final int N = resolvers.size();
         if (N == 0) {
@@ -2519,7 +2513,7 @@
         Iterator<ResolveInfo> iter = matches.iterator();
         while (iter.hasNext()) {
             final ResolveInfo rInfo = iter.next();
-            if (mIPackageManager.checkPermission(
+            if (checkPermission(
                     Manifest.permission.INSTALL_PACKAGES,
                     rInfo.activityInfo.packageName, 0) == PERMISSION_GRANTED || mIsEngBuild) {
                 continue;
@@ -2550,48 +2544,6 @@
         return matches.get(0).getComponentInfo().getComponentName();
     }
 
-    /**
-     * @see #shouldFilterApplication(PackageStateInternal, int, ComponentName, int, int)
-     */
-    boolean shouldFilterApplication(
-            @Nullable PackageStateInternal ps, int callingUid, int userId) {
-        return mComputer.shouldFilterApplication(
-            ps, callingUid, userId);
-    }
-
-    private @PackageStartability int getPackageStartability(String packageName,
-            int callingUid, int userId) {
-        return mComputer.getPackageStartability(mSafeMode, packageName, callingUid, userId);
-    }
-
-    /**
-     * Returns whether or not a full application can see an instant application.
-     * <p>
-     * Currently, there are four cases in which this can occur:
-     * <ol>
-     * <li>The calling application is a "special" process. Special processes
-     *     are those with a UID < {@link Process#FIRST_APPLICATION_UID}.</li>
-     * <li>The calling application has the permission
-     *     {@link android.Manifest.permission#ACCESS_INSTANT_APPS}.</li>
-     * <li>The calling application is the default launcher on the
-     *     system partition.</li>
-     * <li>The calling application is the default app prediction service.</li>
-     * </ol>
-     */
-    boolean canViewInstantApps(int callingUid, int userId) {
-        return mComputer.canViewInstantApps(callingUid, userId);
-    }
-
-    private PackageInfo generatePackageInfo(@NonNull PackageStateInternal ps,
-            @PackageManager.PackageInfoFlagsBits long flags, int userId) {
-        return mComputer.generatePackageInfo(ps, flags, userId);
-    }
-
-    int getPackageUidInternal(String packageName,
-            @PackageManager.PackageInfoFlagsBits long flags, int userId, int callingUid) {
-        return mComputer.getPackageUidInternal(packageName, flags, userId, callingUid);
-    }
-
     public PermissionGroupInfo getPermissionGroupInfo(String groupName, int flags) {
         // Because this is accessed via the package manager service AIDL,
         // go through the permission manager service AIDL
@@ -2600,19 +2552,6 @@
     }
 
     /**
-     * Important: The provided filterCallingUid is used exclusively to filter out applications
-     * that can be seen based on user state. It's typically the original caller uid prior
-     * to clearing. Because it can only be provided by trusted code, its value can be
-     * trusted and will be used as-is; unlike userId which will be validated by this method.
-     */
-    private ApplicationInfo getApplicationInfoInternal(String packageName,
-            @PackageManager.ApplicationInfoFlagsBits long flags,
-            int filterCallingUid, int userId) {
-        return mComputer.getApplicationInfoInternal(packageName, flags,
-                filterCallingUid, userId);
-    }
-
-    /**
      * Blocking call to clear all cached app data above quota.
      */
     public void freeAllAppCacheAboveQuota(String volumeUuid) throws IOException {
@@ -2648,7 +2587,7 @@
             // 2. Consider preloaded data (after 1w honeymoon, unless aggressive)
             if (internalVolume && (aggressive || SystemProperties
                     .getBoolean("persist.sys.preloads.file_cache_expired", false))) {
-                mIPackageManager.deletePreloadsFileCache();
+                deletePreloadsFileCache();
                 if (file.getUsableSpace() >= bytes) return;
             }
 
@@ -2769,43 +2708,6 @@
         return recommendedInstallLocation;
     }
 
-    /**
-     * Update given flags when being used to request {@link ResolveInfo}.
-     * <p>Instant apps are resolved specially, depending upon context. Minimally,
-     * {@code}flags{@code} must have the {@link PackageManager#MATCH_INSTANT}
-     * flag set. However, this flag is only honoured in three circumstances:
-     * <ul>
-     * <li>when called from a system process</li>
-     * <li>when the caller holds the permission {@code android.permission.ACCESS_INSTANT_APPS}</li>
-     * <li>when resolution occurs to start an activity with a {@code android.intent.action.VIEW}
-     * action and a {@code android.intent.category.BROWSABLE} category</li>
-     * </ul>
-     */
-    long updateFlagsForResolve(long flags, int userId, int callingUid,
-            boolean wantInstantApps, boolean isImplicitImageCaptureIntentAndNotSetByDpc) {
-        return mComputer.updateFlagsForResolve(flags, userId, callingUid,
-                wantInstantApps, isImplicitImageCaptureIntentAndNotSetByDpc);
-    }
-
-    /**
-     * Important: The provided filterCallingUid is used exclusively to filter out activities
-     * that can be seen based on user state. It's typically the original caller uid prior
-     * to clearing. Because it can only be provided by trusted code, its value can be
-     * trusted and will be used as-is; unlike userId which will be validated by this method.
-     */
-    private ActivityInfo getActivityInfoInternal(ComponentName component,
-            @PackageManager.ComponentInfoFlagsBits long flags, int filterCallingUid, int userId) {
-        return mComputer.getActivityInfoInternal(component, flags,
-                filterCallingUid, userId);
-    }
-
-    @Nullable
-    List<VersionedPackage> getPackagesUsingSharedLibrary(
-            SharedLibraryInfo libInfo, @PackageManager.PackageInfoFlagsBits long flags,
-            int callingUid, int userId) {
-        return mComputer.getPackagesUsingSharedLibrary(libInfo, flags, callingUid, userId);
-    }
-
     public ModuleInfo getModuleInfo(String packageName, @PackageManager.ModuleInfoFlags int flags) {
         return mModuleInfoProvider.getModuleInfo(packageName, flags);
     }
@@ -2840,7 +2742,7 @@
         return mRequiredInstallerPackage;
     }
 
-    private void requestInstantAppResolutionPhaseTwo(AuxiliaryResolveInfo responseObj,
+    void requestInstantAppResolutionPhaseTwo(AuxiliaryResolveInfo responseObj,
             Intent origIntent, String resolvedType, String callingPackage,
             @Nullable String callingFeatureId, boolean isRequesterInstantApp,
             Bundle verificationBundle, int userId) {
@@ -2852,31 +2754,6 @@
         mHandler.sendMessage(msg);
     }
 
-    /**
-     * From Android R, camera intents have to match system apps. The only exception to this is if
-     * the DPC has set the camera persistent preferred activity. This case was introduced
-     * because it is important that the DPC has the ability to set both system and non-system
-     * camera persistent preferred activities.
-     *
-     * @return {@code true} if the intent is a camera intent and the persistent preferred
-     * activity was not set by the DPC.
-     */
-    @GuardedBy("mLock")
-    boolean isImplicitImageCaptureIntentAndNotSetByDpcLocked(Intent intent, int userId,
-            String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags) {
-        return mComputer.isImplicitImageCaptureIntentAndNotSetByDpcLocked(intent, userId,
-                resolvedType, flags);
-    }
-
-    @GuardedBy("mLock")
-    ResolveInfo findPersistentPreferredActivityLP(Intent intent,
-            String resolvedType,
-            @PackageManager.ResolveInfoFlagsBits long flags, List<ResolveInfo> query, boolean debug,
-            int userId) {
-        return mComputer.findPersistentPreferredActivityLP(intent,
-                resolvedType, flags, query, debug, userId);
-    }
-
     // findPreferredActivityBody returns two items: a "things changed" flag and a
     // ResolveInfo, which is the preferred activity itself.
     static class FindPreferredActivityBodyResult {
@@ -2884,24 +2761,6 @@
         ResolveInfo mPreferredResolveInfo;
     }
 
-    FindPreferredActivityBodyResult findPreferredActivityInternal(
-            Intent intent, String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags,
-            List<ResolveInfo> query, boolean always,
-            boolean removeMatches, boolean debug, int userId, boolean queryMayBeFiltered) {
-        return mComputer.findPreferredActivityInternal(
-            intent, resolvedType, flags,
-            query, always,
-            removeMatches, debug, userId, queryMayBeFiltered);
-    }
-
-    /**
-     * Returns the package name of the calling Uid if it's an instant app. If it isn't
-     * instant, returns {@code null}.
-     */
-    String getInstantAppPackageName(int callingUid) {
-        return mComputer.getInstantAppPackageName(callingUid);
-    }
-
     public @NonNull ParceledListSlice<ResolveInfo> queryIntentReceivers(@NonNull Computer snapshot,
             Intent intent, String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags,
             @UserIdInt int userId) {
@@ -2909,24 +2768,6 @@
                 snapshot, intent, resolvedType, flags, userId, Binder.getCallingUid()));
     }
 
-    @NonNull List<ResolveInfo> queryIntentServicesInternal(Intent intent,
-            String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags, int userId,
-            int callingUid, boolean includeInstantApps) {
-        return mComputer.queryIntentServicesInternal(intent,
-                resolvedType, flags, userId, callingUid,
-                includeInstantApps);
-    }
-
-    private boolean isInstantAppInternal(String packageName, @UserIdInt int userId,
-            int callingUid) {
-        return mComputer.isInstantAppInternal(packageName, userId,
-                callingUid);
-    }
-
-    boolean isCallerSameApp(String packageName, int uid) {
-        return mComputer.isCallerSameApp(packageName, uid);
-    }
-
     public static void reportSettingsProblem(int priority, String msg) {
         logCriticalInfo(priority, msg);
     }
@@ -2943,39 +2784,6 @@
         return packageName + STATIC_SHARED_LIB_DELIMITER + libraryVersion;
     }
 
-    /**
-     * Enforces the request is from the system or an app that has INTERACT_ACROSS_USERS
-     * or INTERACT_ACROSS_USERS_FULL permissions, if the {@code userId} is not for the caller.
-     *
-     * @param checkShell whether to prevent shell from access if there's a debugging restriction
-     * @param message the message to log on security exception
-     */
-    void enforceCrossUserPermission(int callingUid, @UserIdInt int userId,
-            boolean requireFullPermission, boolean checkShell, String message) {
-        mComputer.enforceCrossUserPermission(callingUid, userId,
-                requireFullPermission, checkShell, message);
-    }
-
-    /**
-     * Checks if the request is from the system or an app that has the appropriate cross-user
-     * permissions defined as follows:
-     * <ul>
-     * <li>INTERACT_ACROSS_USERS_FULL if {@code requireFullPermission} is true.</li>
-     * <li>INTERACT_ACROSS_USERS if the given {@code userId} is in a different profile group
-     * to the caller.</li>
-     * <li>Otherwise, INTERACT_ACROSS_PROFILES if the given {@code userId} is in the same profile
-     * group as the caller.</li>
-     * </ul>
-     *
-     * @param checkShell whether to prevent shell from access if there's a debugging restriction
-     * @param message the message to log on security exception
-     */
-    private void enforceCrossUserOrProfilePermission(int callingUid, @UserIdInt int userId,
-            boolean requireFullPermission, boolean checkShell, String message) {
-        mComputer.enforceCrossUserOrProfilePermission(callingUid, userId,
-                requireFullPermission, checkShell, message);
-    }
-
     public void performFstrimIfNeeded() {
         PackageManagerServiceUtils.enforceSystemOrRoot("Only the system can request fstrim");
 
@@ -3033,17 +2841,6 @@
         return mDexManager;
     }
 
-    @NonNull
-    List<PackageStateInternal> findSharedNonSystemLibraries(
-            @NonNull PackageStateInternal pkgSetting) {
-        return mComputer.findSharedNonSystemLibraries(pkgSetting);
-    }
-
-    @Nullable
-    SharedLibraryInfo getSharedLibraryInfo(String name, long version) {
-        return mComputer.getSharedLibraryInfo(name, version);
-    }
-
     public void shutdown() {
         mCompilerStats.writeNow();
         mDexManager.writePackageDexUsageNow();
@@ -3141,14 +2938,14 @@
         mPackageObserverHelper.notifyRemoved(packageName, uid);
     }
 
-    void sendPackageAddedForUser(String packageName, @NonNull PackageStateInternal packageState,
-            int userId, int dataLoaderType) {
+    void sendPackageAddedForUser(@NonNull Computer snapshot, String packageName,
+            @NonNull PackageStateInternal packageState, int userId, int dataLoaderType) {
         final PackageUserStateInternal userState = packageState.getUserStateOrDefault(userId);
         final boolean isSystem = packageState.isSystem();
         final boolean isInstantApp = userState.isInstantApp();
         final int[] userIds = isInstantApp ? EMPTY_INT_ARRAY : new int[] { userId };
         final int[] instantUserIds = isInstantApp ? new int[] { userId } : EMPTY_INT_ARRAY;
-        sendPackageAddedForNewUsers(packageName, isSystem /*sendBootCompleted*/,
+        sendPackageAddedForNewUsers(snapshot, packageName, isSystem /*sendBootCompleted*/,
                 false /*startReceiver*/, packageState.getAppId(), userIds, instantUserIds,
                 dataLoaderType);
 
@@ -3160,15 +2957,15 @@
     }
 
     @Override
-    public void sendPackageAddedForNewUsers(String packageName, boolean sendBootCompleted,
-            boolean includeStopped, @AppIdInt int appId, int[] userIds, int[] instantUserIds,
-            int dataLoaderType) {
+    public void sendPackageAddedForNewUsers(@NonNull Computer snapshot, String packageName,
+            boolean sendBootCompleted, boolean includeStopped, @AppIdInt int appId, int[] userIds,
+            int[] instantUserIds, int dataLoaderType) {
         if (ArrayUtils.isEmpty(userIds) && ArrayUtils.isEmpty(instantUserIds)) {
             return;
         }
         SparseArray<int[]> broadcastAllowList = mAppsFilter.getVisibilityAllowList(
-                getPackageStateInternal(packageName, Process.SYSTEM_UID),
-                userIds, getPackageStates());
+                snapshot.getPackageStateInternal(packageName, Process.SYSTEM_UID),
+                userIds, snapshot.getPackageStates());
         mHandler.post(() -> mBroadcastHelper.sendPackageAddedForNewUsers(
                 packageName, appId, userIds, instantUserIds, dataLoaderType, broadcastAllowList));
         if (sendBootCompleted && !ArrayUtils.isEmpty(userIds)) {
@@ -3202,8 +2999,8 @@
         return false;
     }
 
-    private void enforceCanSetPackagesSuspendedAsUser(String callingPackage, int callingUid,
-            int userId, String callingMethod) {
+    private void enforceCanSetPackagesSuspendedAsUser(@NonNull Computer snapshot,
+            String callingPackage, int callingUid, int userId, String callingMethod) {
         if (callingUid == Process.ROOT_UID
                 // Need to compare app-id to allow system dialogs access on secondary users
                 || UserHandle.getAppId(callingUid) == Process.SYSTEM_UID) {
@@ -3212,7 +3009,7 @@
 
         final String ownerPackage = mProtectedPackages.getDeviceOwnerOrProfileOwnerPackage(userId);
         if (ownerPackage != null) {
-            final int ownerUid = mIPackageManager.getPackageUid(ownerPackage, 0, userId);
+            final int ownerUid = snapshot.getPackageUid(ownerPackage, 0, userId);
             if (ownerUid == callingUid) {
                 return;
             }
@@ -3221,7 +3018,7 @@
         mContext.enforceCallingOrSelfPermission(android.Manifest.permission.SUSPEND_APPS,
                 callingMethod);
 
-        final int packageUid = mIPackageManager.getPackageUid(callingPackage, 0, userId);
+        final int packageUid = snapshot.getPackageUid(callingPackage, 0, userId);
         final boolean allowedPackageUid = packageUid == callingUid;
         // TODO(b/139383163): remove special casing for shell and enforce INTERACT_ACROSS_USERS_FULL
         final boolean allowedShell = callingUid == SHELL_UID
@@ -3242,13 +3039,9 @@
                 allPackages, suspendingPackage::equals, userId);
     }
 
-    private boolean isSuspendingAnyPackages(String suspendingPackage, int userId) {
-        return mComputer.isSuspendingAnyPackages(suspendingPackage, userId);
-    }
-
-    void removeAllDistractingPackageRestrictions(int userId) {
-        final String[] allPackages = mComputer.getAllAvailablePackageNames();
-        removeDistractingPackageRestrictions(allPackages, userId);
+    void removeAllDistractingPackageRestrictions(@NonNull Computer snapshot, int userId) {
+        final String[] allPackages = snapshot.getAllAvailablePackageNames();
+        removeDistractingPackageRestrictions(snapshot, allPackages, userId);
     }
 
     /**
@@ -3260,11 +3053,12 @@
      * @param packagesToChange The packages on which restrictions are to be removed.
      * @param userId the user for which changes are taking place.
      */
-    private void removeDistractingPackageRestrictions(String[] packagesToChange, int userId) {
+    void removeDistractingPackageRestrictions(@NonNull Computer snapshot,
+            String[] packagesToChange, int userId) {
         final List<String> changedPackages = new ArrayList<>();
         final IntArray changedUids = new IntArray();
         for (String packageName : packagesToChange) {
-            final PackageStateInternal ps = getPackageStateInternal(packageName);
+            final PackageStateInternal ps = snapshot.getPackageStateInternal(packageName);
             if (ps != null && ps.getUserStateOrDefault(userId).getDistractionFlags() != 0) {
                 changedPackages.add(ps.getPackageName());
                 changedUids.add(UserHandle.getUid(userId, ps.getAppId()));
@@ -3287,7 +3081,7 @@
         }
     }
 
-    private void setEnableRollbackCode(int token, int enableRollbackCode) {
+    void setEnableRollbackCode(int token, int enableRollbackCode) {
         final Message msg = mHandler.obtainMessage(ENABLE_ROLLBACK_STATUS);
         msg.arg1 = token;
         msg.arg2 = enableRollbackCode;
@@ -3334,7 +3128,7 @@
             if (DEBUG_BACKUP) {
                 Slog.i(TAG, "Package " + packageName + " sending normal FIRST_LAUNCH");
             }
-            final boolean isInstantApp = isInstantAppInternal(
+            final boolean isInstantApp = snapshotComputer().isInstantAppInternal(
                     packageName, userId, Process.SYSTEM_UID);
             final int[] userIds = isInstantApp ? EMPTY_INT_ARRAY : new int[] { userId };
             final int[] instantUserIds = isInstantApp ? new int[] { userId } : EMPTY_INT_ARRAY;
@@ -3384,30 +3178,22 @@
                 versionedPackage, observer, userId, deleteFlags, false);
     }
 
-    private String resolveExternalPackageName(AndroidPackage pkg) {
-        return mComputer.resolveExternalPackageName(pkg);
-    }
-
-    String resolveInternalPackageName(String packageName, long versionCode) {
-        return mComputer.resolveInternalPackageName(packageName, versionCode);
-    }
-
-    boolean isCallerVerifier(int callingUid) {
+    boolean isCallerVerifier(@NonNull Computer snapshot, int callingUid) {
         final int callingUserId = UserHandle.getUserId(callingUid);
-        return mRequiredVerifierPackage != null && callingUid == mIPackageManager.getPackageUid(
+        return mRequiredVerifierPackage != null && callingUid == snapshot.getPackageUid(
                 mRequiredVerifierPackage, 0, callingUserId);
     }
 
-    public boolean isPackageDeviceAdminOnAnyUser(String packageName) {
+    public boolean isPackageDeviceAdminOnAnyUser(@NonNull Computer snapshot, String packageName) {
         final int callingUid = Binder.getCallingUid();
-        if (mIPackageManager.checkUidPermission(android.Manifest.permission.MANAGE_USERS,
-                callingUid) != PERMISSION_GRANTED) {
+        if (snapshot.checkUidPermission(android.Manifest.permission.MANAGE_USERS, callingUid)
+                != PERMISSION_GRANTED) {
             EventLog.writeEvent(0x534e4554, "128599183", -1, "");
             throw new SecurityException(android.Manifest.permission.MANAGE_USERS
                     + " permission is required to call this API");
         }
-        if (getInstantAppPackageName(callingUid) != null
-                && !isCallerSameApp(packageName, callingUid)) {
+        if (snapshot.getInstantAppPackageName(callingUid) != null
+                && !snapshot.isCallerSameApp(packageName, callingUid)) {
             return false;
         }
         return isPackageDeviceAdmin(packageName, UserHandle.USER_ALL);
@@ -3456,14 +3242,15 @@
         return mDevicePolicyManager;
     }
 
-    private boolean clearApplicationUserDataLIF(String packageName, int userId) {
+    private boolean clearApplicationUserDataLIF(@NonNull Computer snapshot, String packageName,
+            int userId) {
         if (packageName == null) {
             Slog.w(TAG, "Attempt to delete null packageName.");
             return false;
         }
 
         // Try finding details about the requested package
-        AndroidPackage pkg = getPackage(packageName);
+        AndroidPackage pkg = snapshot.getPackage(packageName);
         if (pkg == null) {
             Slog.w(TAG, "Package named '" + packageName + "' doesn't exist.");
             return false;
@@ -3486,8 +3273,8 @@
         } else {
             flags = 0;
         }
-        mAppDataHelper.prepareAppDataContentsLIF(pkg, getPackageStateInternal(packageName), userId,
-                flags);
+        mAppDataHelper.prepareAppDataContentsLIF(pkg, snapshot.getPackageStateInternal(packageName),
+                userId, flags);
 
         return true;
     }
@@ -3538,10 +3325,6 @@
         }
     }
 
-    int getUidTargetSdkVersion(int uid) {
-        return mComputer.getUidTargetSdkVersion(uid);
-    }
-
     void postPreferredActivityChangedBroadcast(int userId) {
         mHandler.post(() -> mBroadcastHelper.sendPreferredActivityChangedBroadcast(userId));
     }
@@ -3562,18 +3345,19 @@
 
         // Persistent preferred activity might have came into effect due to this
         // install.
-        mPreferredActivityHelper.updateDefaultHomeNotLocked(userId);
+        mPreferredActivityHelper.updateDefaultHomeNotLocked(snapshotComputer(), userId);
     }
 
     /**
      * Variant that takes a {@link WatchedIntentFilter}
      */
-    public void addCrossProfileIntentFilter(WatchedIntentFilter intentFilter, String ownerPackage,
-            int sourceUserId, int targetUserId, int flags) {
+    public void addCrossProfileIntentFilter(@NonNull Computer snapshot,
+            WatchedIntentFilter intentFilter, String ownerPackage, int sourceUserId,
+            int targetUserId, int flags) {
         mContext.enforceCallingOrSelfPermission(
                         android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, null);
         int callingUid = Binder.getCallingUid();
-        enforceOwnerRights(ownerPackage, callingUid);
+        enforceOwnerRights(snapshot, ownerPackage, callingUid);
         PackageManagerServiceUtils.enforceShellRestriction(mInjector.getUserManagerInternal(),
                 UserManager.DISALLOW_DEBUGGING_FEATURES, callingUid, sourceUserId);
         if (intentFilter.countActions() == 0) {
@@ -3601,18 +3385,18 @@
     }
 
     // Enforcing that callingUid is owning pkg on userId
-    private void enforceOwnerRights(String pkg, int callingUid) {
+    private void enforceOwnerRights(@NonNull Computer snapshot, String pkg, int callingUid) {
         // The system owns everything.
         if (UserHandle.getAppId(callingUid) == Process.SYSTEM_UID) {
             return;
         }
-        final String[] callerPackageNames = mIPackageManager.getPackagesForUid(callingUid);
+        final String[] callerPackageNames = snapshot.getPackagesForUid(callingUid);
         if (!ArrayUtils.contains(callerPackageNames, pkg)) {
             throw new SecurityException("Calling uid " + callingUid
                     + " does not own package " + pkg);
         }
         final int callingUserId = UserHandle.getUserId(callingUid);
-        PackageInfo pi = mIPackageManager.getPackageInfo(pkg, 0, callingUserId);
+        PackageInfo pi = snapshot.getPackageInfo(pkg, 0, callingUserId);
         if (pi == null) {
             throw new IllegalArgumentException("Unknown package " + pkg + " on user "
                     + callingUserId);
@@ -3626,29 +3410,13 @@
         }
         final UserInfo parent = ums.getProfileParent(userId);
         final int launcherUid = (parent != null) ? parent.id : userId;
-        final ComponentName launcherComponent = getDefaultHomeActivity(launcherUid);
+        // TODO: Should this snapshot be moved further up?
+        final ComponentName launcherComponent = snapshotComputer()
+                .getDefaultHomeActivity(launcherUid);
         mBroadcastHelper.sendSessionCommitBroadcast(sessionInfo, userId, launcherUid,
                 launcherComponent, mAppPredictionServicePackage);
     }
 
-    /**
-     * Report the 'Home' activity which is currently set as "always use this one". If non is set
-     * then reports the most likely home activity or null if there are more than one.
-     */
-    private ComponentName getDefaultHomeActivity(int userId) {
-        return mComputer.getDefaultHomeActivity(userId);
-    }
-
-    Intent getHomeIntent() {
-        return mComputer.getHomeIntent();
-    }
-
-    ComponentName getHomeActivitiesAsUser(List<ResolveInfo> allHomeCandidates,
-            int userId) {
-        return mComputer.getHomeActivitiesAsUser(allHomeCandidates,
-                userId);
-    }
-
     private @Nullable String getSetupWizardPackageNameImpl(@NonNull Computer computer) {
         final Intent intent = new Intent(Intent.ACTION_MAIN);
         intent.addCategory(Intent.CATEGORY_SETUP_WIZARD);
@@ -3682,10 +3450,11 @@
         }
     }
 
-    private @NonNull String getRequiredSdkSandboxPackageName() {
+    @NonNull
+    private static String getRequiredSdkSandboxPackageName(@NonNull Computer computer) {
         final Intent intent = new Intent(SdkSandboxManagerLocal.SERVICE_INTERFACE);
 
-        final List<ResolveInfo> matches = queryIntentServicesInternal(
+        final List<ResolveInfo> matches = computer.queryIntentServicesInternal(
                 intent,
                 /* resolvedType= */ null,
                 MATCH_SYSTEM_ONLY | MATCH_DIRECT_BOOT_AWARE | MATCH_DIRECT_BOOT_UNAWARE,
@@ -3701,22 +3470,6 @@
     }
 
     @Nullable
-    private String getDeviceConfiguratorPackageName() {
-        return ensureSystemPackageName(mContext.getString(
-                R.string.config_deviceConfiguratorPackageName));
-    }
-
-    public @Nullable String getAmbientContextDetectionPackageName() {
-        return ensureSystemPackageName(getPackageFromComponentString(
-                        R.string.config_defaultAmbientContextDetectionService));
-    }
-
-    public String getOverlayConfigSignaturePackageName() {
-        return ensureSystemPackageName(mInjector.getSystemConfig()
-                .getOverlayConfigSignaturePackage());
-    }
-
-    @Nullable
     private String getRetailDemoPackageName() {
         final String predefinedPkgName = mContext.getString(R.string.config_retailDemoPackage);
         final String predefinedSignature = mContext.getString(
@@ -3752,14 +3505,7 @@
     }
 
     @Nullable
-    private String getRecentsPackageName() {
-        return ensureSystemPackageName(
-                getPackageFromComponentString(R.string.config_recentsComponentName));
-
-    }
-
-    @Nullable
-    private String getPackageFromComponentString(@StringRes int stringResId) {
+    String getPackageFromComponentString(@StringRes int stringResId) {
         final String componentString = mContext.getString(stringResId);
         if (TextUtils.isEmpty(componentString)) {
             return null;
@@ -3772,16 +3518,17 @@
     }
 
     @Nullable
-    private String ensureSystemPackageName(@Nullable String packageName) {
+    String ensureSystemPackageName(@NonNull Computer snapshot,
+            @Nullable String packageName) {
         if (packageName == null) {
             return null;
         }
         final long token = Binder.clearCallingIdentity();
         try {
-            if (mIPackageManager.getPackageInfo(packageName, MATCH_FACTORY_ONLY,
+            if (snapshot.getPackageInfo(packageName, MATCH_FACTORY_ONLY,
                     UserHandle.USER_SYSTEM) == null) {
                 PackageInfo packageInfo =
-                        mIPackageManager.getPackageInfo(packageName, 0, UserHandle.USER_SYSTEM);
+                        snapshot.getPackageInfo(packageName, 0, UserHandle.USER_SYSTEM);
                 if (packageInfo != null) {
                     EventLog.writeEvent(0x534e4554, "145981139", packageInfo.applicationInfo.uid,
                             "");
@@ -3863,8 +3610,10 @@
     private void setEnabledSettings(List<ComponentEnabledSetting> settings, int userId,
             String callingPackage) {
         final int callingUid = Binder.getCallingUid();
-        enforceCrossUserPermission(callingUid, userId, false /* requireFullPermission */,
-                true /* checkShell */, "set enabled");
+        // TODO: This method is not properly snapshotified beyond this call
+        final Computer preLockSnapshot = snapshotComputer();
+        preLockSnapshot.enforceCrossUserPermission(callingUid, userId,
+                false /* requireFullPermission */, true /* checkShell */, "set enabled");
 
         final int targetSize = settings.size();
         for (int i = 0; i < targetSize; i++) {
@@ -3920,6 +3669,7 @@
         final Map<String, PackageSetting> pkgSettings = new ArrayMap<>(targetSize);
         // reader
         synchronized (mLock) {
+            final Computer snapshot = snapshotComputer();
             // Checks for target packages
             for (int i = 0; i < targetSize; i++) {
                 final ComponentEnabledSetting setting = settings.get(i);
@@ -3929,13 +3679,13 @@
                     continue;
                 }
                 final boolean isCallerTargetApp = ArrayUtils.contains(
-                        mIPackageManager.getPackagesForUid(callingUid), packageName);
+                        snapshot.getPackagesForUid(callingUid), packageName);
                 final PackageSetting pkgSetting = mSettings.getPackageLPr(packageName);
                 // Limit who can change which apps
                 if (!isCallerTargetApp) {
                     // Don't allow apps that don't have permission to modify other apps
                     if (!allowedByPermission
-                            || shouldFilterApplication(pkgSetting, callingUid, userId)) {
+                            || snapshot.shouldFilterApplication(pkgSetting, callingUid, userId)) {
                         throw new SecurityException("Attempt to change component state; "
                                 + "pid=" + Binder.getCallingPid()
                                 + ", uid=" + callingUid
@@ -4108,12 +3858,13 @@
 
         final long callingId = Binder.clearCallingIdentity();
         try {
+            final Computer newSnapshot = snapshotComputer();
             for (int i = 0; i < sendNowBroadcasts.size(); i++) {
                 final String packageName = sendNowBroadcasts.keyAt(i);
                 final ArrayList<String> components = sendNowBroadcasts.valueAt(i);
                 final int packageUid = UserHandle.getUid(
                         userId, pkgSettings.get(packageName).getAppId());
-                sendPackageChangedBroadcast(packageName, false /* dontKillApp */,
+                sendPackageChangedBroadcast(newSnapshot, packageName, false /* dontKillApp */,
                         components, packageUid, null /* reason */);
             }
         } finally {
@@ -4137,13 +3888,13 @@
             pkgSetting.setEnabled(newState, userId, callingPackage);
             if ((newState == COMPONENT_ENABLED_STATE_DISABLED_USER
                     || newState == COMPONENT_ENABLED_STATE_DISABLED)
-                    && mIPackageManager.checkPermission(Manifest.permission.SUSPEND_APPS,
-                    packageName, userId) == PERMISSION_GRANTED) {
+                    && checkPermission(Manifest.permission.SUSPEND_APPS, packageName, userId)
+                    == PERMISSION_GRANTED) {
                 // This app should not generally be allowed to get disabled by the UI, but
                 // if it ever does, we don't want to end up with some of the user's apps
                 // permanently suspended.
                 unsuspendForSuspendingPackage(computer, packageName, userId);
-                removeAllDistractingPackageRestrictions(userId);
+                removeAllDistractingPackageRestrictions(computer, userId);
             }
             success = true;
         } else {
@@ -4194,25 +3945,20 @@
         }
     }
 
-    void sendPackageChangedBroadcast(String packageName,
+    void sendPackageChangedBroadcast(@NonNull Computer snapshot, String packageName,
             boolean dontKillApp, ArrayList<String> componentNames, int packageUid, String reason) {
         final int userId = UserHandle.getUserId(packageUid);
-        final boolean isInstantApp = isInstantAppInternal(packageName, userId, Process.SYSTEM_UID);
+        final boolean isInstantApp =
+                snapshot.isInstantAppInternal(packageName, userId, Process.SYSTEM_UID);
         final int[] userIds = isInstantApp ? EMPTY_INT_ARRAY : new int[] { userId };
         final int[] instantUserIds = isInstantApp ? new int[] { userId } : EMPTY_INT_ARRAY;
-        final SparseArray<int[]> broadcastAllowList = getBroadcastAllowList(
+        final SparseArray<int[]> broadcastAllowList = snapshot.getBroadcastAllowList(
                 packageName, userIds, isInstantApp);
         mHandler.post(() -> mBroadcastHelper.sendPackageChangedBroadcast(
                 packageName, dontKillApp, componentNames, packageUid, reason, userIds,
                 instantUserIds, broadcastAllowList));
     }
 
-    @Nullable
-    private SparseArray<int[]> getBroadcastAllowList(@NonNull String packageName,
-            @UserIdInt int[] userIds, boolean isInstantApp) {
-        return mComputer.getBroadcastAllowList(packageName, userIds, isInstantApp);
-    }
-
     /**
      * Used by SystemServer
      */
@@ -4296,7 +4042,7 @@
 
         // Now that we're mostly running, clean up stale users and apps
         mUserManager.reconcileUsers(StorageManager.UUID_PRIVATE_INTERNAL);
-        storageEventHelper.reconcileApps(StorageManager.UUID_PRIVATE_INTERNAL);
+        storageEventHelper.reconcileApps(snapshotComputer(), StorageManager.UUID_PRIVATE_INTERNAL);
 
         mPermissionManager.onSystemReady();
 
@@ -4308,7 +4054,7 @@
         final int livingUserCount = livingUsers.size();
         for (int i = 0; i < livingUserCount; i++) {
             final int userId = livingUsers.get(i).id;
-            if (mPmInternal.isPermissionUpgradeNeeded(userId)) {
+            if (mSettings.isPermissionUpgradeNeeded(userId)) {
                 grantPermissionsUserIds = ArrayUtils.appendInt(
                         grantPermissionsUserIds, userId);
             }
@@ -4350,11 +4096,12 @@
                 if (packageName == null) {
                     return;
                 }
-                AndroidPackage pkg = mPackages.get(packageName);
+                final Computer snapshot = snapshotComputer();
+                AndroidPackage pkg = snapshot.getPackage(packageName);
                 if (pkg == null) {
                     return;
                 }
-                sendPackageChangedBroadcast(pkg.getPackageName(),
+                sendPackageChangedBroadcast(snapshot, pkg.getPackageName(),
                         true /* dontKillApp */,
                         new ArrayList<>(Collections.singletonList(pkg.getPackageName())),
                         pkg.getUid(),
@@ -4407,14 +4154,6 @@
         mSnapshotStatistics.dump(pw, "  ", now, hits, -1, isBrief);
     }
 
-    /**
-     * Dump package manager states to the file according to a given dumping type of
-     * {@link DumpState}.
-     */
-    void dumpComputer(int type, FileDescriptor fd, PrintWriter pw, DumpState dumpState) {
-        mComputer.dump(type, fd, pw, dumpState);
-    }
-
     //TODO: b/111402650
     private void disableSkuSpecificApps() {
         String[] apkList = mContext.getResources().getStringArray(
@@ -4428,10 +4167,11 @@
         if (!TextUtils.isEmpty(sku) && ArrayUtils.contains(skuArray, sku)) {
             return;
         }
+        final Computer snapshot = snapshotComputer();
         for (String packageName : apkList) {
-            mIPackageManager.setSystemAppHiddenUntilInstalled(packageName, true);
+            setSystemAppHiddenUntilInstalled(snapshot, packageName, true);
             for (UserInfo user : mInjector.getUserManagerInternal().getUsers(false)) {
-                mIPackageManager.setSystemAppInstallState(packageName, false, user.id);
+                setSystemAppInstallState(snapshot, packageName, false, user.id);
             }
         }
     }
@@ -4524,7 +4264,7 @@
             mPermissionManager.writeLegacyPermissionStateTEMP();
             mSettings.readPermissionStateForUserSyncLPr(userId);
             mPermissionManager.readLegacyPermissionStateTEMP();
-            return mPmInternal.isPermissionUpgradeNeeded(userId);
+            return mSettings.isPermissionUpgradeNeeded(userId);
         }
     }
 
@@ -4544,12 +4284,8 @@
         }
     }
 
-    boolean userNeedsBadging(int userId) {
-        return mUserNeedsBadging.get(userId);
-    }
-
-    private void deletePackageIfUnused(final String packageName) {
-        PackageStateInternal ps = getPackageStateInternal(packageName);
+    private void deletePackageIfUnused(@NonNull Computer snapshot, final String packageName) {
+        PackageStateInternal ps = snapshot.getPackageStateInternal(packageName);
         if (ps == null) {
             return;
         }
@@ -4567,41 +4303,101 @@
                 0, PackageManager.DELETE_ALL_USERS, true /*removedBySystem*/));
     }
 
-    private AndroidPackage getPackage(String packageName) {
-        return mComputer.getPackage(packageName);
+    void deletePreloadsFileCache() {
+        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CLEAR_APP_CACHE,
+                "deletePreloadsFileCache");
+        File dir = Environment.getDataPreloadsFileCacheDirectory();
+        Slog.i(PackageManagerService.TAG, "Deleting preloaded file cache " + dir);
+        FileUtils.deleteContents(dir);
     }
 
-    private AndroidPackage getPackage(int uid) {
-        return mComputer.getPackage(uid);
+    void setSystemAppHiddenUntilInstalled(@NonNull Computer snapshot, String packageName,
+            boolean hidden) {
+        final int callingUid = Binder.getCallingUid();
+        final boolean calledFromSystemOrPhone = callingUid == Process.PHONE_UID
+                || callingUid == Process.SYSTEM_UID;
+        if (!calledFromSystemOrPhone) {
+            mContext.enforceCallingOrSelfPermission(Manifest.permission.SUSPEND_APPS,
+                    "setSystemAppHiddenUntilInstalled");
+        }
+
+        final PackageStateInternal stateRead = snapshot.getPackageStateInternal(packageName);
+        if (stateRead == null || !stateRead.isSystem() || stateRead.getPkg() == null) {
+            return;
+        }
+        if (stateRead.getPkg().isCoreApp() && !calledFromSystemOrPhone) {
+            throw new SecurityException("Only system or phone callers can modify core apps");
+        }
+
+        commitPackageStateMutation(null, mutator -> {
+            mutator.forPackage(packageName)
+                    .setHiddenUntilInstalled(hidden);
+            mutator.forDisabledSystemPackage(packageName)
+                    .setHiddenUntilInstalled(hidden);
+        });
     }
 
-    private SigningDetails getSigningDetails(@NonNull String packageName) {
-        return mComputer.getSigningDetails(packageName);
+    boolean setSystemAppInstallState(@NonNull Computer snapshot, String packageName,
+            boolean installed, int userId) {
+        final int callingUid = Binder.getCallingUid();
+        final boolean calledFromSystemOrPhone = callingUid == Process.PHONE_UID
+                || callingUid == Process.SYSTEM_UID;
+        if (!calledFromSystemOrPhone) {
+            mContext.enforceCallingOrSelfPermission(Manifest.permission.SUSPEND_APPS,
+                    "setSystemAppHiddenUntilInstalled");
+        }
+
+        final PackageStateInternal packageState = snapshot.getPackageStateInternal(packageName);
+        // The target app should always be in system
+        if (packageState == null || !packageState.isSystem() || packageState.getPkg() == null) {
+            return false;
+        }
+        if (packageState.getPkg().isCoreApp() && !calledFromSystemOrPhone) {
+            throw new SecurityException("Only system or phone callers can modify core apps");
+        }
+        // Check if the install state is the same
+        if (packageState.getUserStateOrDefault(userId).isInstalled() == installed) {
+            return false;
+        }
+
+        final long callingId = Binder.clearCallingIdentity();
+        try {
+            if (installed) {
+                // install the app from uninstalled state
+                mInstallPackageHelper.installExistingPackageAsUser(
+                        packageName,
+                        userId,
+                        PackageManager.INSTALL_ALL_WHITELIST_RESTRICTED_PERMISSIONS,
+                        PackageManager.INSTALL_REASON_DEVICE_SETUP,
+                        null,
+                        null);
+                return true;
+            }
+
+            // uninstall the app from installed state
+            deletePackageVersioned(
+                    new VersionedPackage(packageName, PackageManager.VERSION_CODE_HIGHEST),
+                    new PackageManager.LegacyPackageDeleteObserver(null).getBinder(),
+                    userId,
+                    PackageManager.DELETE_SYSTEM_APP);
+            return true;
+        } finally {
+            Binder.restoreCallingIdentity(callingId);
+        }
     }
 
-    private SigningDetails getSigningDetails(int uid) {
-        return mComputer.getSigningDetails(uid);
-    }
+    void finishPackageInstall(int token, boolean didLaunch) {
+        PackageManagerServiceUtils.enforceSystemOrRoot(
+                "Only the system is allowed to finish installs");
 
-    private boolean filterAppAccess(AndroidPackage pkg, int callingUid, int userId) {
-        return mComputer.filterAppAccess(pkg, callingUid, userId);
-    }
+        if (PackageManagerService.DEBUG_INSTALL) {
+            Slog.v(PackageManagerService.TAG, "BM finishing package install for " + token);
+        }
+        Trace.asyncTraceEnd(TRACE_TAG_PACKAGE_MANAGER, "restore", token);
 
-    private boolean filterAppAccess(String packageName, int callingUid, int userId) {
-        return mComputer.filterAppAccess(packageName, callingUid, userId);
-    }
-
-    private boolean filterAppAccess(int uid, int callingUid) {
-        return mComputer.filterAppAccess(uid, callingUid);
-    }
-
-    @Nullable
-    private int[] getVisibilityAllowList(@NonNull String packageName, @UserIdInt int userId) {
-        return mComputer.getVisibilityAllowList(packageName, userId);
-    }
-
-    boolean canQueryPackage(int callingUid, @Nullable String targetPackageName) {
-        return mComputer.canQueryPackage(callingUid, targetPackageName);
+        final Message msg = mHandler.obtainMessage(PackageManagerService.POST_INSTALL, token,
+                didLaunch ? 1 : 0);
+        mHandler.sendMessage(msg);
     }
 
     void checkPackageStartable(@NonNull Computer snapshot, @NonNull String packageName,
@@ -4689,71 +4485,15 @@
         }
     }
 
-    public class IPackageManagerImpl extends IPackageManager.Stub {
+    public class IPackageManagerImpl extends IPackageManagerBase {
 
-        @Override
-        public boolean activitySupportsIntent(ComponentName component, Intent intent,
-                String resolvedType) {
-            return mComputer.activitySupportsIntent(mResolveComponentName, component, intent,
-                    resolvedType);
-        }
-
-        @Override
-        public void addCrossProfileIntentFilter(IntentFilter intentFilter, String ownerPackage,
-                int sourceUserId, int targetUserId, int flags) {
-            PackageManagerService.this.addCrossProfileIntentFilter(
-                    new WatchedIntentFilter(intentFilter), ownerPackage, sourceUserId, targetUserId,
-                    flags);
-        }
-
-        // NOTE: Can't remove due to unsupported app usage
-        @Override
-        public boolean addPermission(PermissionInfo info) {
-            // Because this is accessed via the package manager service AIDL,
-            // go through the permission manager service AIDL
-            return mContext.getSystemService(PermissionManager.class).addPermission(info, false);
-        }
-
-        // NOTE: Can't remove due to unsupported app usage
-        @Override
-        public boolean addPermissionAsync(PermissionInfo info) {
-            // Because this is accessed via the package manager service AIDL,
-            // go through the permission manager service AIDL
-            return mContext.getSystemService(PermissionManager.class).addPermission(info, true);
-        }
-
-        @Override
-        public void addPersistentPreferredActivity(IntentFilter filter, ComponentName activity,
-                int userId) {
-            mPreferredActivityHelper.addPersistentPreferredActivity(new WatchedIntentFilter(filter),
-                    activity, userId);
-        }
-
-        @Override
-        public void addPreferredActivity(IntentFilter filter, int match,
-                ComponentName[] set, ComponentName activity, int userId, boolean removeExisting) {
-            mPreferredActivityHelper.addPreferredActivity(new WatchedIntentFilter(filter), match,
-                    set, activity, true, userId, "Adding preferred", removeExisting);
-        }
-
-        /*
-         * Returns if intent can be forwarded from the sourceUserId to the targetUserId
-         */
-        @Override
-        public boolean canForwardTo(@NonNull Intent intent, @Nullable String resolvedType,
-                @UserIdInt int sourceUserId, @UserIdInt int targetUserId) {
-            return mComputer.canForwardTo(intent, resolvedType, sourceUserId, targetUserId);
-        }
-
-        @Override
-        public boolean canRequestPackageInstalls(String packageName, int userId) {
-            return mComputer.canRequestPackageInstalls(packageName, Binder.getCallingUid(), userId,
-                    true /* throwIfPermNotDeclared*/);
-        }
-
-        @Override
-        public String[] canonicalToCurrentPackageNames(String[] names) {
-            return mComputer.canonicalToCurrentPackageNames(names);
+        public IPackageManagerImpl() {
+            super(PackageManagerService.this, mContext, mDexOptHelper, mModuleInfoProvider,
+                    mPreferredActivityHelper, mResolveIntentHelper, mDomainVerificationManager,
+                    mDomainVerificationConnection, mInstallerService, mPackageProperty,
+                    mResolveComponentName, mInstantAppResolverSettingsComponent,
+                    mRequiredSdkSandboxPackage, mServicesExtensionPackageName,
+                    mSharedSystemSharedLibraryPackageName);
         }
 
         @Override
@@ -4762,33 +4502,13 @@
                     .checkPackageStartable(snapshotComputer(), packageName, userId);
         }
 
-        // NOTE: Can't remove due to unsupported app usage
-        @Override
-        public int checkPermission(String permName, String pkgName, int userId) {
-            return PackageManagerService.this.checkPermission(permName, pkgName, userId);
-        }
-
-        @Override
-        public int checkSignatures(@NonNull String pkg1, @NonNull String pkg2) {
-            return mComputer.checkSignatures(pkg1, pkg2);
-        }
-
-        @Override
-        public int checkUidPermission(String permName, int uid) {
-            return mComputer.checkUidPermission(permName, uid);
-        }
-
-        @Override
-        public int checkUidSignatures(int uid1, int uid2) {
-            return mComputer.checkUidSignatures(uid1, uid2);
-        }
-
         @Override
         public void clearApplicationProfileData(String packageName) {
             PackageManagerServiceUtils.enforceSystemOrRoot(
                     "Only the system can clear all profile data");
 
-            final AndroidPackage pkg = getPackage(packageName);
+            final Computer snapshot = snapshotComputer();
+            final AndroidPackage pkg = snapshot.getPackage(packageName);
             try (PackageFreezer ignored = freezePackage(packageName, "clearApplicationProfileData")) {
                 synchronized (mInstallLock) {
                     mAppDataHelper.clearAppProfilesLIF(pkg);
@@ -4803,10 +4523,11 @@
                     android.Manifest.permission.CLEAR_APP_USER_DATA, null);
 
             final int callingUid = Binder.getCallingUid();
-            enforceCrossUserPermission(callingUid, userId, true /* requireFullPermission */,
+            final Computer snapshot = snapshotComputer();
+            snapshot.enforceCrossUserPermission(callingUid, userId, true /* requireFullPermission */,
                     false /* checkShell */, "clear application data");
 
-            if (mComputer.getPackageStateFiltered(packageName, callingUid, userId) == null) {
+            if (snapshot.getPackageStateFiltered(packageName, callingUid, userId) == null) {
                 if (observer != null) {
                     mHandler.post(() -> {
                         try {
@@ -4831,7 +4552,8 @@
                     try (PackageFreezer freezer = freezePackage(packageName,
                             "clearApplicationUserData")) {
                         synchronized (mInstallLock) {
-                            succeeded = clearApplicationUserDataLIF(packageName, userId);
+                            succeeded = clearApplicationUserDataLIF(snapshotComputer(), packageName,
+                                    userId);
                         }
                         mInstantAppRegistry.deleteInstantApplicationMetadata(packageName, userId);
                         synchronized (mLock) {
@@ -4849,8 +4571,9 @@
                         }
                         if (checkPermission(Manifest.permission.SUSPEND_APPS, packageName, userId)
                                 == PERMISSION_GRANTED) {
-                            unsuspendForSuspendingPackage(snapshotComputer(), packageName, userId);
-                            removeAllDistractingPackageRestrictions(userId);
+                            final Computer snapshot = snapshotComputer();
+                            unsuspendForSuspendingPackage(snapshot, packageName, userId);
+                            removeAllDistractingPackageRestrictions(snapshot, userId);
                             flushPackageRestrictionsAsUserInternalLocked(userId);
                         }
                     }
@@ -4870,7 +4593,8 @@
             mContext.enforceCallingOrSelfPermission(
                     android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, null);
             final int callingUid = Binder.getCallingUid();
-            enforceOwnerRights(ownerPackage, callingUid);
+            final Computer snapshot = snapshotComputer();
+            enforceOwnerRights(snapshot, ownerPackage, callingUid);
             PackageManagerServiceUtils.enforceShellRestriction(mInjector.getUserManagerInternal(),
                     UserManager.DISALLOW_DEBUGGING_FEATURES, callingUid, sourceUserId);
             synchronized (mLock) {
@@ -4888,22 +4612,7 @@
         }
 
         @Override
-        public void clearPackagePersistentPreferredActivities(String packageName, int userId) {
-            mPreferredActivityHelper.clearPackagePersistentPreferredActivities(packageName, userId);
-        }
-
-        @Override
-        public void clearPackagePreferredActivities(String packageName) {
-            mPreferredActivityHelper.clearPackagePreferredActivities(packageName);
-        }
-
-        @Override
-        public String[] currentToCanonicalPackageNames(String[] names) {
-            return mComputer.currentToCanonicalPackageNames(names);
-        }
-
-        @Override
-        public void deleteApplicationCacheFiles(final String packageName,
+        public final void deleteApplicationCacheFiles(final String packageName,
                 final IPackageDataObserver observer) {
             final int userId = UserHandle.getCallingUserId();
             deleteApplicationCacheFilesAsUser(packageName, userId, observer);
@@ -4928,17 +4637,18 @@
                 mContext.enforceCallingOrSelfPermission(
                         android.Manifest.permission.INTERNAL_DELETE_CACHE_FILES, null);
             }
-            enforceCrossUserPermission(callingUid, userId, /* requireFullPermission= */ true,
+            final Computer snapshot = snapshotComputer();
+            snapshot.enforceCrossUserPermission(callingUid, userId, /* requireFullPermission= */ true,
                     /* checkShell= */ false, "delete application cache files");
             final int hasAccessInstantApps = mContext.checkCallingOrSelfPermission(
                     android.Manifest.permission.ACCESS_INSTANT_APPS);
 
-            final AndroidPackage pkg = getPackage(packageName);
-
             // Queue up an async operation since the package deletion may take a little while.
             mHandler.post(() -> {
-                final PackageStateInternal ps =
-                        pkg == null ? null : getPackageStateInternal(pkg.getPackageName());
+                // Snapshot in the Handler Runnable since this may be deferred quite a bit
+                // TODO: Is this and the later mInstallLock re-snapshot necessary?
+                final Computer newSnapshot = snapshotComputer();
+                final PackageStateInternal ps = newSnapshot.getPackageStateInternal(packageName);
                 boolean doClearData = true;
                 if (ps != null) {
                     final boolean targetIsInstantApp =
@@ -4949,6 +4659,8 @@
                 if (doClearData) {
                     synchronized (mInstallLock) {
                         final int flags = FLAG_STORAGE_DE | FLAG_STORAGE_CE | FLAG_STORAGE_EXTERNAL;
+                        // Snapshot again after mInstallLock?
+                        final AndroidPackage pkg = snapshotComputer().getPackage(packageName);
                         // We're only clearing cache files, so we don't care if the
                         // app is unfrozen and still able to run
                         mAppDataHelper.clearAppDataLIF(pkg, userId,
@@ -4968,47 +4680,18 @@
         }
 
         @Override
-        public void deleteExistingPackageAsUser(VersionedPackage versionedPackage,
-                final IPackageDeleteObserver2 observer, final int userId) {
-            PackageManagerService.this.deleteExistingPackageAsUser(versionedPackage, observer,
-                    userId);
-        }
-
-        @Override
-        public void deletePackageAsUser(String packageName, int versionCode,
-                IPackageDeleteObserver observer, int userId, int flags) {
-            deletePackageVersioned(new VersionedPackage(packageName, versionCode),
-                    new PackageManager.LegacyPackageDeleteObserver(observer).getBinder(), userId, flags);
-        }
-
-        @Override
-        public void deletePackageVersioned(VersionedPackage versionedPackage,
-                final IPackageDeleteObserver2 observer, final int userId, final int deleteFlags) {
-            PackageManagerService.this.deletePackageVersioned(versionedPackage, observer,
-                    userId, deleteFlags);
-        }
-
-        @Override
-        public void deletePreloadsFileCache() {
-            mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CLEAR_APP_CACHE,
-                    "deletePreloadsFileCache");
-            File dir = Environment.getDataPreloadsFileCacheDirectory();
-            Slog.i(PackageManagerService.TAG, "Deleting preloaded file cache " + dir);
-            FileUtils.deleteContents(dir);
-        }
-
-        @Override
         public void dumpProfiles(String packageName) {
             /* Only the shell, root, or the app user should be able to dump profiles. */
             final int callingUid = Binder.getCallingUid();
-            final String[] callerPackageNames = getPackagesForUid(callingUid);
+            final Computer snapshot = snapshotComputer();
+            final String[] callerPackageNames = snapshot.getPackagesForUid(callingUid);
             if (callingUid != Process.SHELL_UID
                     && callingUid != Process.ROOT_UID
                     && !ArrayUtils.contains(callerPackageNames, packageName)) {
                 throw new SecurityException("dumpProfiles");
             }
 
-            AndroidPackage pkg = mComputer.getPackage(packageName);
+            AndroidPackage pkg = snapshot.getPackage(packageName);
             if (pkg == null) {
                 throw new IllegalArgumentException("Unknown package: " + packageName);
             }
@@ -5062,46 +4745,25 @@
             });
         }
 
-        @Override
-        public ResolveInfo findPersistentPreferredActivity(Intent intent, int userId) {
-            return mPreferredActivityHelper.findPersistentPreferredActivity(intent, userId);
-        }
-
-        @Override
-        public void finishPackageInstall(int token, boolean didLaunch) {
-            PackageManagerServiceUtils.enforceSystemOrRoot(
-                    "Only the system is allowed to finish installs");
-
-            if (PackageManagerService.DEBUG_INSTALL) {
-                Slog.v(PackageManagerService.TAG, "BM finishing package install for " + token);
-            }
-            Trace.asyncTraceEnd(TRACE_TAG_PACKAGE_MANAGER, "restore", token);
-
-            final Message msg = mHandler.obtainMessage(PackageManagerService.POST_INSTALL, token, didLaunch ? 1 : 0);
-            mHandler.sendMessage(msg);
-        }
-
         @WorkerThread
         @Override
         public void flushPackageRestrictionsAsUser(int userId) {
-            if (getInstantAppPackageName(Binder.getCallingUid()) != null) {
+            final Computer snapshot = snapshotComputer();
+            final int callingUid = Binder.getCallingUid();
+            if (snapshot.getInstantAppPackageName(callingUid) != null) {
                 return;
             }
             if (!mUserManager.exists(userId)) {
                 return;
             }
-            enforceCrossUserPermission(Binder.getCallingUid(), userId, false /* requireFullPermission*/,
-                    false /* checkShell */, "flushPackageRestrictions");
+            snapshot.enforceCrossUserPermission(callingUid, userId,
+                    false /* requireFullPermission*/, false /* checkShell */,
+                    "flushPackageRestrictions");
             synchronized (mLock) {
                 flushPackageRestrictionsAsUserInternalLocked(userId);
             }
         }
 
-        @Override
-        public void forceDexOpt(String packageName) {
-            mDexOptHelper.forceDexOpt(packageName);
-        }
-
 
         @Override
         public void freeStorage(final String volumeUuid, final long freeStorageSize,
@@ -5150,83 +4812,17 @@
         }
 
         @Override
-        public ActivityInfo getActivityInfo(ComponentName component,
-                @PackageManager.ComponentInfoFlagsBits long flags, int userId) {
-            return mComputer.getActivityInfo(component, flags, userId);
-        }
-
-        @NonNull
-        @Override
-        public ParceledListSlice<IntentFilter> getAllIntentFilters(@NonNull String packageName) {
-            return mComputer.getAllIntentFilters(packageName);
-        }
-
-        @Override
-        public List<String> getAllPackages() {
-            return mComputer.getAllPackages();
-        }
-
-        // NOTE: Can't remove due to unsupported app usage
-        @NonNull
-        @Override
-        public String[] getAppOpPermissionPackages(@NonNull String permissionName) {
-            return mComputer.getAppOpPermissionPackages(permissionName);
-        }
-
-        @Override
-        public String getAppPredictionServicePackageName() {
-            return ensureSystemPackageName(
-                    getPackageFromComponentString(R.string.config_defaultAppPredictionService));
-        }
-
-        @PackageManager.EnabledState
-        @Override
-        public int getApplicationEnabledSetting(@NonNull String packageName, @UserIdInt int userId) {
-            return mComputer.getApplicationEnabledSetting(packageName, userId);
-        }
-
-        /**
-         * Returns true if application is not found or there was an error. Otherwise it returns
-         * the hidden state of the package for the given user.
-         */
-        @Override
-        public boolean getApplicationHiddenSettingAsUser(@NonNull String packageName,
-                @UserIdInt int userId) {
-            return mComputer.getApplicationHiddenSettingAsUser(packageName, userId);
-        }
-
-        @Override
-        public ApplicationInfo getApplicationInfo(String packageName,
-                @PackageManager.ApplicationInfoFlagsBits long flags, int userId) {
-            return mComputer.getApplicationInfo(packageName, flags, userId);
-        }
-
-        @Override
-        public IArtManager getArtManager() {
-            return mArtManagerService;
-        }
-
-        @Override
-        public @Nullable String getAttentionServicePackageName() {
-            return ensureSystemPackageName(
-                    getPackageFromComponentString(R.string.config_defaultAttentionService));
-        }
-
-        @Override
-        public boolean getBlockUninstallForUser(@NonNull String packageName, @UserIdInt int userId) {
-            return mComputer.getBlockUninstallForUser(packageName, userId);
-        }
-
-        @Override
         public ChangedPackages getChangedPackages(int sequenceNumber, int userId) {
             final int callingUid = Binder.getCallingUid();
-            if (getInstantAppPackageName(callingUid) != null) {
+            final Computer snapshot = snapshotComputer();
+            if (snapshot.getInstantAppPackageName(callingUid) != null) {
                 return null;
             }
             if (!mUserManager.exists(userId)) {
                 return null;
             }
-            enforceCrossUserPermission(callingUid, userId, false, false, "getChangedPackages");
+            snapshot.enforceCrossUserPermission(callingUid, userId, false, false,
+                    "getChangedPackages");
             final ChangedPackages changedPackages = mChangedPackagesTracker.getChangedPackages(
                     sequenceNumber, userId);
 
@@ -5234,8 +4830,9 @@
                 final List<String> packageNames = changedPackages.getPackageNames();
                 for (int index = packageNames.size() - 1; index >= 0; index--) {
                     // Filter out the changes if the calling package should not be able to see it.
-                    final PackageSetting ps = mSettings.getPackageLPr(packageNames.get(index));
-                    if (shouldFilterApplication(ps, callingUid, userId)) {
+                    final PackageStateInternal packageState =
+                            snapshot.getPackageStateInternal(packageNames.get(index));
+                    if (snapshot.shouldFilterApplication(packageState, callingUid, userId)) {
                         packageNames.remove(index);
                     }
                 }
@@ -5245,41 +4842,6 @@
         }
 
         @Override
-        public int getComponentEnabledSetting(@NonNull ComponentName component, int userId) {
-            return mComputer.getComponentEnabledSetting(component, Binder.getCallingUid(), userId);
-        }
-
-        @Override
-        public String getContentCaptureServicePackageName() {
-            return ensureSystemPackageName(
-                    getPackageFromComponentString(R.string.config_defaultContentCaptureService));
-        }
-
-        @Nullable
-        @Override
-        public ParceledListSlice<SharedLibraryInfo> getDeclaredSharedLibraries(
-                @NonNull String packageName, @PackageManager.PackageInfoFlagsBits long flags,
-                @NonNull int userId) {
-            return mComputer.getDeclaredSharedLibraries(packageName, flags, userId);
-        }
-
-        /**
-         * Non-Binder method, support for the backup/restore mechanism: write the
-         * default browser (etc) settings in its canonical XML format.  Returns the default
-         * browser XML representation as a byte array, or null if there is none.
-         */
-        @Override
-        public byte[] getDefaultAppsBackup(int userId) {
-            return mPreferredActivityHelper.getDefaultAppsBackup(userId);
-        }
-
-        @Override
-        public String getDefaultTextClassifierPackageName() {
-            return ensureSystemPackageName(
-                    mContext.getString(R.string.config_servicesExtensionPackage));
-        }
-
-        @Override
         public byte[] getDomainVerificationBackup(int userId) {
             if (Binder.getCallingUid() != Process.SYSTEM_UID) {
                 throw new SecurityException("Only the system may call getDomainVerificationBackup()");
@@ -5301,17 +4863,6 @@
         }
 
         @Override
-        public int getFlagsForUid(int uid) {
-            return mComputer.getFlagsForUid(uid);
-        }
-
-        @Nullable
-        @Override
-        public CharSequence getHarmfulAppWarning(@NonNull String packageName, @UserIdInt int userId) {
-            return mComputer.getHarmfulAppWarning(packageName, userId);
-        }
-
-        @Override
         public IBinder getHoldLockToken() {
             if (!Build.IS_DEBUGGABLE) {
                 throw new SecurityException("getHoldLockToken requires a debuggable build");
@@ -5327,72 +4878,15 @@
         }
 
         @Override
-        public ComponentName getHomeActivities(List<ResolveInfo> allHomeCandidates) {
-            if (getInstantAppPackageName(Binder.getCallingUid()) != null) {
-                return null;
-            }
-            return getHomeActivitiesAsUser(allHomeCandidates, UserHandle.getCallingUserId());
-        }
-
-        public String getIncidentReportApproverPackageName() {
-            return ensureSystemPackageName(mContext.getString(
-                    R.string.config_incidentReportApproverPackage));
-        }
-
-        @Override
-        public int getInstallLocation() {
-            // allow instant app access
-            return android.provider.Settings.Global.getInt(mContext.getContentResolver(),
-                    android.provider.Settings.Global.DEFAULT_INSTALL_LOCATION,
-                    InstallLocationUtils.APP_INSTALL_AUTO);
-        }
-
-        @PackageManager.InstallReason
-        @Override
-        public int getInstallReason(@NonNull String packageName, @UserIdInt int userId) {
-            return mComputer.getInstallReason(packageName, userId);
-        }
-
-        @Override
-        @Nullable
-        public InstallSourceInfo getInstallSourceInfo(@NonNull String packageName) {
-            return mComputer.getInstallSourceInfo(packageName);
-        }
-
-        @Override
-        public ParceledListSlice<ApplicationInfo> getInstalledApplications(
-                @PackageManager.ApplicationInfoFlagsBits long flags, int userId) {
-            final int callingUid = Binder.getCallingUid();
-            return new ParceledListSlice<>(
-                    mComputer.getInstalledApplications(flags, userId, callingUid));
-        }
-
-        @Override
-        public List<ModuleInfo> getInstalledModules(int flags) {
-            return mModuleInfoProvider.getInstalledModules(flags);
-        }
-
-        @Override
-        public ParceledListSlice<PackageInfo> getInstalledPackages(
-                @PackageManager.PackageInfoFlagsBits long flags, int userId) {
-            return mComputer.getInstalledPackages(flags, userId);
-        }
-
-        @Nullable
-        @Override
-        public String getInstallerPackageName(@NonNull String packageName) {
-            return mComputer.getInstallerPackageName(packageName);
-        }
-
-        @Override
         public String getInstantAppAndroidId(String packageName, int userId) {
-            mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_INSTANT_APPS,
-                    "getInstantAppAndroidId");
-            enforceCrossUserPermission(Binder.getCallingUid(), userId,
+            mContext.enforceCallingOrSelfPermission(
+                    android.Manifest.permission.ACCESS_INSTANT_APPS, "getInstantAppAndroidId");
+            final Computer snapshot = snapshotComputer();
+            snapshot.enforceCrossUserPermission(Binder.getCallingUid(), userId,
                     true /* requireFullPermission */, false /* checkShell */,
                     "getInstantAppAndroidId");
             // Make sure the target is an Instant App.
-            if (!isInstantApp(packageName, userId)) {
+            if (!snapshot.isInstantApp(packageName, userId)) {
                 return null;
             }
             return mInstantAppRegistry.getInstantAppAndroidId(packageName, userId);
@@ -5404,13 +4898,14 @@
                 return null;
             }
 
-            enforceCrossUserPermission(Binder.getCallingUid(), userId,
+            final Computer snapshot = snapshotComputer();
+            snapshot.enforceCrossUserPermission(Binder.getCallingUid(), userId,
                     true /* requireFullPermission */, false /* checkShell */,
                     "getInstantAppCookie");
-            if (!isCallerSameApp(packageName, Binder.getCallingUid())) {
+            if (!snapshot.isCallerSameApp(packageName, Binder.getCallingUid())) {
                 return null;
             }
-            PackageStateInternal packageState = getPackageStateInternal(packageName);
+            PackageStateInternal packageState = snapshot.getPackageStateInternal(packageName);
             if (packageState == null || packageState.getPkg() == null) {
                 return null;
             }
@@ -5423,11 +4918,12 @@
                 return null;
             }
 
-            if (!canViewInstantApps(Binder.getCallingUid(), userId)) {
+            final Computer snapshot = snapshotComputer();
+            if (!snapshot.canViewInstantApps(Binder.getCallingUid(), userId)) {
                 mContext.enforceCallingOrSelfPermission(Manifest.permission.ACCESS_INSTANT_APPS,
                         "getInstantAppIcon");
             }
-            enforceCrossUserPermission(Binder.getCallingUid(), userId,
+            snapshot.enforceCrossUserPermission(Binder.getCallingUid(), userId,
                     true /* requireFullPermission */, false /* checkShell */,
                     "getInstantAppIcon");
 
@@ -5435,76 +4931,31 @@
         }
 
         @Override
-        public ComponentName getInstantAppInstallerComponent() {
-            if (getInstantAppPackageName(Binder.getCallingUid()) != null) {
-                return null;
-            }
-            return mInstantAppInstallerActivity == null
-                    ? null : mInstantAppInstallerActivity.getComponentName();
-        }
-
-        @Override
-        public @Nullable ComponentName getInstantAppResolverComponent() {
-            if (getInstantAppPackageName(Binder.getCallingUid()) != null) {
-                return null;
-            }
-            return getInstantAppResolver();
-        }
-
-        @Override
-        public ComponentName getInstantAppResolverSettingsComponent() {
-            return mInstantAppResolverSettingsComponent;
-        }
-
-        @Override
         public ParceledListSlice<InstantAppInfo> getInstantApps(int userId) {
-            if (PackageManagerService.HIDE_EPHEMERAL_APIS) {
+            if (HIDE_EPHEMERAL_APIS) {
                 return null;
             }
-            if (!canViewInstantApps(Binder.getCallingUid(), userId)) {
+
+            final Computer snapshot = snapshotComputer();
+            if (!snapshot.canViewInstantApps(Binder.getCallingUid(), userId)) {
                 mContext.enforceCallingOrSelfPermission(Manifest.permission.ACCESS_INSTANT_APPS,
                         "getEphemeralApplications");
             }
-            enforceCrossUserPermission(Binder.getCallingUid(), userId,
+            snapshot.enforceCrossUserPermission(Binder.getCallingUid(), userId,
                     true /* requireFullPermission */, false /* checkShell */,
                     "getEphemeralApplications");
 
-            Computer computer = snapshotComputer();
-            List<InstantAppInfo> instantApps = mInstantAppRegistry.getInstantApps(computer, userId);
+            List<InstantAppInfo> instantApps = mInstantAppRegistry.getInstantApps(snapshot, userId);
             if (instantApps != null) {
                 return new ParceledListSlice<>(instantApps);
             }
             return null;
         }
 
-        @Nullable
-        @Override
-        public InstrumentationInfo getInstrumentationInfo(@NonNull ComponentName component, int flags) {
-            return mComputer.getInstrumentationInfo(component, flags);
-        }
-
-        @Deprecated
-        @Override
-        public @NonNull ParceledListSlice<IntentFilterVerificationInfo> getIntentFilterVerifications(
-                String packageName) {
-            return ParceledListSlice.emptyList();
-        }
-
-        @Deprecated
-        @Override
-        public int getIntentVerificationStatus(String packageName, int userId) {
-            return mDomainVerificationManager.getLegacyState(packageName, userId);
-        }
-
-        @Nullable
-        @Override
-        public KeySet getKeySetByAlias(@NonNull String packageName, @NonNull String alias) {
-            return mComputer.getKeySetByAlias(packageName, alias);
-        }
-
         @Override
         public ResolveInfo getLastChosenActivity(Intent intent, String resolvedType, int flags) {
-            return mPreferredActivityHelper.getLastChosenActivity(intent, resolvedType, flags);
+            return mPreferredActivityHelper.getLastChosenActivity(snapshotComputer(), intent,
+                    resolvedType, flags);
         }
 
         @Override
@@ -5516,13 +4967,9 @@
 
         @Override
         public List<String> getMimeGroup(String packageName, String mimeGroup) {
-            enforceOwnerRights(packageName, Binder.getCallingUid());
-            return getMimeGroupInternal(packageName, mimeGroup);
-        }
-
-        @Override
-        public ModuleInfo getModuleInfo(String packageName, @PackageManager.ModuleInfoFlags int flags) {
-            return PackageManagerService.this.getModuleInfo(packageName, flags);
+            final Computer snapshot = snapshotComputer();
+            enforceOwnerRights(snapshot, packageName, Binder.getCallingUid());
+            return getMimeGroupInternal(snapshot, packageName, mimeGroup);
         }
 
         @Override
@@ -5532,99 +4979,11 @@
             return mMoveCallbacks.mLastStatus.get(moveId);
         }
 
-        @Nullable
-        @Override
-        public String getNameForUid(int uid) {
-            return mComputer.getNameForUid(uid);
-        }
-
-        @Nullable
-        @Override
-        public String[] getNamesForUids(@NonNull int[] uids) {
-            return mComputer.getNamesForUids(uids);
-        }
-
-        @Override
-        public int[] getPackageGids(String packageName, @PackageManager.PackageInfoFlagsBits long flags,
-                int userId) {
-            return mComputer.getPackageGids(packageName, flags, userId);
-        }
-
-        @Override
-        public PackageInfo getPackageInfo(String packageName,
-                @PackageManager.PackageInfoFlagsBits long flags, int userId) {
-            return mComputer.getPackageInfo(packageName, flags, userId);
-        }
-
-        @Override
-        public PackageInfo getPackageInfoVersioned(VersionedPackage versionedPackage,
-                @PackageManager.PackageInfoFlagsBits long flags, int userId) {
-            return mComputer.getPackageInfoInternal(versionedPackage.getPackageName(),
-                    versionedPackage.getLongVersionCode(), flags, Binder.getCallingUid(), userId);
-        }
-
-        @Override
-        public IPackageInstaller getPackageInstaller() {
-            // Return installer service for internal calls.
-            if (PackageManagerServiceUtils.isSystemOrRoot()) {
-                return mInstallerService;
-            }
-            // Return null for InstantApps.
-            if (snapshotComputer().getInstantAppPackageName(Binder.getCallingUid()) != null) {
-                return null;
-            }
-            return mInstallerService;
-        }
-
-        @Override
-        public void getPackageSizeInfo(final String packageName, int userId,
-                final IPackageStatsObserver observer) {
-            throw new UnsupportedOperationException(
-                    "Shame on you for calling the hidden API getPackageSizeInfo(). Shame!");
-        }
-
-        @Override
-        public int getPackageUid(@NonNull String packageName,
-                @PackageManager.PackageInfoFlagsBits long flags, @UserIdInt int userId) {
-            return mComputer.getPackageUid(packageName, flags, userId);
-        }
-
-        /**
-         * <em>IMPORTANT:</em> Not all packages returned by this method may be known
-         * to the system. There are two conditions in which this may occur:
-         * <ol>
-         *   <li>The package is on adoptable storage and the device has been removed</li>
-         *   <li>The package is being removed and the internal structures are partially updated</li>
-         * </ol>
-         * The second is an artifact of the current data structures and should be fixed. See
-         * b/111075456 for one such instance.
-         * This binder API is cached.  If the algorithm in this method changes,
-         * or if the underlying objecs (as returned by getSettingLPr()) change
-         * then the logic that invalidates the cache must be revisited.  See
-         * calls to invalidateGetPackagesForUidCache() to locate the points at
-         * which the cache is invalidated.
-         */
-        @Override
-        public String[] getPackagesForUid(int uid) {
-            final int callingUid = Binder.getCallingUid();
-            final int userId = UserHandle.getUserId(uid);
-            mComputer.enforceCrossUserOrProfilePermission(callingUid, userId,
-                    /* requireFullPermission */ false,
-                    /* checkShell */ false, "getPackagesForUid");
-            return mComputer.getPackagesForUid(uid);
-        }
-
-        @Override
-        public ParceledListSlice<PackageInfo> getPackagesHoldingPermissions(
-                @NonNull String[] permissions, @PackageManager.PackageInfoFlagsBits long flags,
-                @UserIdInt int userId) {
-            return mComputer.getPackagesHoldingPermissions(permissions, flags, userId);
-        }
-
         @Override
         public String getPermissionControllerPackageName() {
             final int callingUid = Binder.getCallingUid();
-            if (mComputer.getPackageStateFiltered(mRequiredPermissionControllerPackage,
+            final Computer snapshot = snapshotComputer();
+            if (snapshot.getPackageStateFiltered(mRequiredPermissionControllerPackage,
                     callingUid, UserHandle.getUserId(callingUid)) != null) {
                 return mRequiredPermissionControllerPackage;
             }
@@ -5632,73 +4991,6 @@
             throw new IllegalStateException("PermissionController is not found");
         }
 
-        // NOTE: Can't remove due to unsupported app usage
-        @Override
-        public PermissionGroupInfo getPermissionGroupInfo(String groupName, int flags) {
-            return PackageManagerService.this.getPermissionGroupInfo(groupName, flags);
-        }
-
-        @Override
-        public @NonNull ParceledListSlice<ApplicationInfo> getPersistentApplications(int flags) {
-            if (getInstantAppPackageName(Binder.getCallingUid()) != null) {
-                return ParceledListSlice.emptyList();
-            }
-            return new ParceledListSlice<>(mComputer.getPersistentApplications(mSafeMode, flags));
-        }
-
-        @Override
-        public int getPreferredActivities(List<IntentFilter> outFilters,
-                List<ComponentName> outActivities, String packageName) {
-            return mPreferredActivityHelper.getPreferredActivities(outFilters, outActivities,
-                    packageName, snapshotComputer());
-        }
-
-        /**
-         * Non-Binder method, support for the backup/restore mechanism: write the
-         * full set of preferred activities in its canonical XML format.  Returns the
-         * XML output as a byte array, or null if there is none.
-         */
-        @Override
-        public byte[] getPreferredActivityBackup(int userId) {
-            return mPreferredActivityHelper.getPreferredActivityBackup(userId);
-        }
-
-        @Override
-        public int getPrivateFlagsForUid(int uid) {
-            return mComputer.getPrivateFlagsForUid(uid);
-        }
-
-        @Override
-        public PackageManager.Property getProperty(String propertyName, String packageName, String className) {
-            Objects.requireNonNull(propertyName);
-            Objects.requireNonNull(packageName);
-            PackageStateInternal packageState = mComputer.getPackageStateFiltered(packageName,
-                    Binder.getCallingUid(), UserHandle.getCallingUserId());
-            if (packageState == null) {
-                return null;
-            }
-            return mPackageProperty.getProperty(propertyName, packageName, className);
-        }
-
-        @Nullable
-        @Override
-        public ProviderInfo getProviderInfo(@NonNull ComponentName component,
-                @PackageManager.ComponentInfoFlagsBits long flags, @UserIdInt int userId) {
-            return mComputer.getProviderInfo(component, flags, userId);
-        }
-
-        @Override
-        public ActivityInfo getReceiverInfo(ComponentName component,
-                @PackageManager.ComponentInfoFlagsBits long flags, int userId) {
-            return mComputer.getReceiverInfo(component, flags, userId);
-        }
-
-        @Override
-        public @Nullable String getRotationResolverPackageName() {
-            return ensureSystemPackageName(
-                    getPackageFromComponentString(R.string.config_defaultRotationResolverService));
-        }
-
         @Override
         public int getRuntimePermissionsVersion(@UserIdInt int userId) {
             Preconditions.checkArgumentNonnegative(userId);
@@ -5707,65 +4999,25 @@
             return mSettings.getDefaultRuntimePermissionsVersion(userId);
         }
 
-        @Nullable
-        @Override
-        public ServiceInfo getServiceInfo(@NonNull ComponentName component,
-                @PackageManager.ComponentInfoFlagsBits long flags, @UserIdInt int userId) {
-            return mComputer.getServiceInfo(component, flags, userId);
-        }
-
-        @Override
-        public @NonNull String getServicesSystemSharedLibraryPackageName() {
-            return mServicesExtensionPackageName;
-        }
-
-        @Override
-        public String getSetupWizardPackageName() {
-            if (Binder.getCallingUid() != Process.SYSTEM_UID) {
-                throw new SecurityException("Non-system caller");
-            }
-            return mPmInternal.getSetupWizardPackageName();
-        }
-
-        @Override
-        public ParceledListSlice<SharedLibraryInfo> getSharedLibraries(String packageName,
-                @PackageManager.PackageInfoFlagsBits long flags, int userId) {
-            return mComputer.getSharedLibraries(packageName, flags, userId);
-        }
-
-        @Override
-        public @NonNull String getSharedSystemSharedLibraryPackageName() {
-            return mSharedSystemSharedLibraryPackageName;
-        }
-
-        @Nullable
-        @Override
-        public KeySet getSigningKeySet(@NonNull String packageName) {
-            return mComputer.getSigningKeySet(packageName);
-        }
-
         @Override
         public String getSplashScreenTheme(@NonNull String packageName, int userId) {
-            PackageStateInternal packageState =
-                    getPackageStateInstalledFiltered(packageName, Binder.getCallingUid(), userId);
+            final Computer snapshot = snapshotComputer();
+            PackageStateInternal packageState = filterPackageStateForInstalledAndFiltered(snapshot,
+                    packageName, Binder.getCallingUid(), userId);
             return packageState == null ? null
                     : packageState.getUserStateOrDefault(userId).getSplashScreenTheme();
         }
 
         @Override
-        public String getSdkSandboxPackageName() {
-            return mRequiredSdkSandboxPackage;
-        }
-
-        @Override
         public Bundle getSuspendedPackageAppExtras(String packageName, int userId) {
             final int callingUid = Binder.getCallingUid();
-            if (getPackageUid(packageName, 0, userId) != callingUid) {
+            final Computer snapshot = snapshot();
+            if (snapshot.getPackageUid(packageName, 0, userId) != callingUid) {
                 throw new SecurityException("Calling package " + packageName
                         + " does not belong to calling uid " + callingUid);
             }
-            return mSuspendPackageHelper.getSuspendedPackageAppExtras(
-                    packageName, userId, callingUid);
+            return mSuspendPackageHelper
+                    .getSuspendedPackageAppExtras(snapshot, packageName, userId, callingUid);
         }
 
         @Override
@@ -5785,34 +5037,6 @@
         }
 
         @Override
-        public String getSystemCaptionsServicePackageName() {
-            return ensureSystemPackageName(
-                    getPackageFromComponentString(R.string.config_defaultSystemCaptionsService));
-        }
-
-        @Nullable
-        @Override
-        public String[] getSystemSharedLibraryNames() {
-            return mComputer.getSystemSharedLibraryNames();
-        }
-
-        @Override
-        public String getSystemTextClassifierPackageName() {
-            return ensureSystemPackageName(
-                    mContext.getString(R.string.config_defaultTextClassifierPackage));
-        }
-
-        @Override
-        public int getTargetSdkVersion(@NonNull String packageName)  {
-            return mComputer.getTargetSdkVersion(packageName);
-        }
-
-        @Override
-        public int getUidForSharedUser(@NonNull String sharedUserName) {
-            return mComputer.getUidForSharedUser(sharedUserName);
-        }
-
-        @Override
         public String[] getUnsuspendablePackagesForUser(String[] packageNames, int userId) {
             Objects.requireNonNull(packageNames, "packageNames cannot be null");
             mContext.enforceCallingOrSelfPermission(Manifest.permission.SUSPEND_APPS,
@@ -5838,19 +5062,7 @@
         }
 
         @Override
-        public String getWellbeingPackageName() {
-            final long identity = Binder.clearCallingIdentity();
-            try {
-                return CollectionUtils.firstOrNull(
-                        mContext.getSystemService(RoleManager.class).getRoleHolders(
-                                RoleManager.ROLE_SYSTEM_WELLBEING));
-            } finally {
-                Binder.restoreCallingIdentity(identity);
-            }
-        }
-
-        @Override
-        public void grantImplicitAccess(int recipientUid, @NonNull String visibleAuthority) {
+        public void makeProviderVisible(int recipientUid, @NonNull String visibleAuthority) {
             final Computer snapshot = snapshotComputer();
             final int recipientUserId = UserHandle.getUserId(recipientUid);
             final ProviderInfo providerInfo =
@@ -5864,36 +5076,24 @@
                     false /*direct*/, false /* retainOnUpdate */);
         }
 
-        // NOTE: Can't remove due to unsupported app usage
         @Override
-        public void grantRuntimePermission(String packageName, String permName, final int userId) {
-            // Because this is accessed via the package manager service AIDL,
-            // go through the permission manager service AIDL
-            mContext.getSystemService(PermissionManager.class)
-                    .grantRuntimePermission(packageName, permName, UserHandle.of(userId));
-        }
+        public void makeUidVisible(int recipientUid, int visibleUid) {
+            mContext.enforceCallingOrSelfPermission(
+                    android.Manifest.permission.MAKE_UID_VISIBLE, "makeUidVisible");
+            final int callingUid = Binder.getCallingUid();
+            final int recipientUserId = UserHandle.getUserId(recipientUid);
+            final int visibleUserId = UserHandle.getUserId(visibleUid);
+            final Computer snapshot = snapshotComputer();
+            snapshot.enforceCrossUserPermission(callingUid, recipientUserId,
+                    false /* requireFullPermission */, false /* checkShell */, "makeUidVisible");
+            snapshot.enforceCrossUserPermission(callingUid, visibleUserId,
+                    false /* requireFullPermission */, false /* checkShell */, "makeUidVisible");
+            snapshot.enforceCrossUserPermission(recipientUid, visibleUserId,
+                    false /* requireFullPermission */, false /* checkShell */, "makeUidVisible");
 
-        @Override
-        public boolean hasSigningCertificate(@NonNull String packageName, @NonNull byte[] certificate,
-                @PackageManager.CertificateInputType int type) {
-            return mComputer.hasSigningCertificate(packageName, certificate, type);
-        }
-
-        @Override
-        public boolean hasSystemFeature(String name, int version) {
-            return PackageManagerService.this.hasSystemFeature(name, version);
-        }
-
-        @Override
-        public boolean hasSystemUidErrors() {
-            // allow instant applications
-            return false;
-        }
-
-        @Override
-        public boolean hasUidSigningCertificate(int uid, @NonNull byte[] certificate,
-                @PackageManager.CertificateInputType int type) {
-            return mComputer.hasUidSigningCertificate(uid, certificate, type);
+            PackageManagerService.this.grantImplicitAccess(snapshot, recipientUserId,
+                    null /*Intent*/, UserHandle.getAppId(recipientUid), visibleUid,
+                    false /*direct*/, false /* retainOnUpdate */);
         }
 
         @Override
@@ -5924,55 +5124,17 @@
         }
 
         @Override
-        public boolean isDeviceUpgrading() {
-            return PackageManagerService.this.isDeviceUpgrading();
-        }
-
-        @Override
-        public boolean isFirstBoot() {
-            return PackageManagerService.this.isFirstBoot();
-        }
-
-        @Override
-        public boolean isInstantApp(String packageName, int userId) {
-            return mComputer.isInstantApp(packageName, userId);
-        }
-
-        @Override
-        public boolean isOnlyCoreApps() {
-            return PackageManagerService.this.isOnlyCoreApps();
-        }
-
-        @Override
-        public boolean isPackageAvailable(String packageName, int userId) {
-            return mComputer.isPackageAvailable(packageName, userId);
-        }
-
-        @Override
-        public boolean isPackageDeviceAdminOnAnyUser(String packageName) {
-            return PackageManagerService.this.isPackageDeviceAdminOnAnyUser(packageName);
-        }
-
-        @Override
-        public boolean isPackageSignedByKeySet(@NonNull String packageName, @NonNull KeySet ks) {
-            return mComputer.isPackageSignedByKeySet(packageName, ks);
-        }
-
-        @Override
-        public boolean isPackageSignedByKeySetExactly(@NonNull String packageName, @NonNull KeySet ks) {
-            return mComputer.isPackageSignedByKeySetExactly(packageName, ks);
-        }
-
-        @Override
         public boolean isPackageStateProtected(@NonNull String packageName, @UserIdInt int userId) {
             final int callingUid = Binder.getCallingUid();
             final int callingAppId = UserHandle.getAppId(callingUid);
 
-            enforceCrossUserPermission(callingUid, userId, false /*requireFullPermission*/,
+            final Computer snapshot = snapshotComputer();
+            snapshot.enforceCrossUserPermission(callingUid, userId, false /*requireFullPermission*/,
                     true /*checkShell*/, "isPackageStateProtected");
 
             if (callingAppId != Process.SYSTEM_UID && callingAppId != Process.ROOT_UID
-                    && checkUidPermission(MANAGE_DEVICE_ADMINS, callingUid) != PERMISSION_GRANTED) {
+                    && snapshot.checkUidPermission(MANAGE_DEVICE_ADMINS, callingUid)
+                    != PERMISSION_GRANTED) {
                 throw new SecurityException("Caller must have the "
                         + MANAGE_DEVICE_ADMINS + " permission.");
             }
@@ -5981,11 +5143,6 @@
         }
 
         @Override
-        public boolean isPackageSuspendedForUser(@NonNull String packageName, @UserIdInt int userId) {
-            return mComputer.isPackageSuspendedForUser(packageName, userId);
-        }
-
-        @Override
         public boolean isProtectedBroadcast(String actionName) {
             if (actionName != null) {
                 // TODO: remove these terrible hacks
@@ -6002,22 +5159,6 @@
             }
         }
 
-        @Override
-        public boolean isSafeMode() {
-            // allow instant applications
-            return mSafeMode;
-        }
-
-        @Override
-        public boolean isStorageLow() {
-            return PackageManagerService.this.isStorageLow();
-        }
-
-        @Override
-        public boolean isUidPrivileged(int uid) {
-            return mComputer.isUidPrivileged(uid);
-        }
-
         /**
          * Logs process start information (including base APK hash) to the security log.
          * @hide
@@ -6025,13 +5166,15 @@
         @Override
         public void logAppProcessStartIfNeeded(String packageName, String processName, int uid,
                 String seinfo, String apkFile, int pid) {
-            if (getInstantAppPackageName(Binder.getCallingUid()) != null) {
+            final Computer snapshot = snapshotComputer();
+            if (snapshot.getInstantAppPackageName(Binder.getCallingUid()) != null) {
                 return;
             }
             if (!SecurityLog.isLoggingEnabled()) {
                 return;
             }
-            mProcessLoggingHandler.logAppProcessStart(mContext, mPmInternal, apkFile, packageName,
+            mProcessLoggingHandler.logAppProcessStart(mContext,
+                    LocalServices.getService(PackageManagerInternal.class), apkFile, packageName,
                     processName, uid, seinfo, pid);
         }
 
@@ -6083,25 +5226,29 @@
         }
 
         @Override
-        public void notifyDexLoad(String loadingPackageName, Map<String, String> classLoaderContextMap,
+        public void notifyDexLoad(String loadingPackageName,
+                Map<String, String> classLoaderContextMap,
                 String loaderIsa) {
             int callingUid = Binder.getCallingUid();
-            if (PackageManagerService.PLATFORM_PACKAGE_NAME.equals(loadingPackageName) && callingUid != Process.SYSTEM_UID) {
-                Slog.w(PackageManagerService.TAG, "Non System Server process reporting dex loads as system server. uid="
-                        + callingUid);
+            if (PackageManagerService.PLATFORM_PACKAGE_NAME.equals(loadingPackageName)
+                    && callingUid != Process.SYSTEM_UID) {
+                Slog.w(PackageManagerService.TAG,
+                        "Non System Server process reporting dex loads as system server. uid="
+                                + callingUid);
                 // Do not record dex loads from processes pretending to be system server.
                 // Only the system server should be assigned the package "android", so reject calls
                 // that don't satisfy the constraint.
                 //
                 // notifyDexLoad is a PM API callable from the app process. So in theory, apps could
-                // craft calls to this API and pretend to be system server. Doing so poses no particular
-                // danger for dex load reporting or later dexopt, however it is a sensible check to do
-                // in order to verify the expectations.
+                // craft calls to this API and pretend to be system server. Doing so poses no
+                // particular danger for dex load reporting or later dexopt, however it is a
+                // sensible check to do in order to verify the expectations.
                 return;
             }
 
             int userId = UserHandle.getCallingUserId();
-            ApplicationInfo ai = getApplicationInfo(loadingPackageName, /*flags*/ 0, userId);
+            ApplicationInfo ai = snapshot().getApplicationInfo(loadingPackageName, /*flags*/ 0,
+                    userId);
             if (ai == null) {
                 Slog.w(PackageManagerService.TAG, "Loading a package that does not exist for the calling user. package="
                         + loadingPackageName + ", user=" + userId);
@@ -6115,11 +5262,13 @@
         public void notifyPackageUse(String packageName, int reason) {
             final int callingUid = Binder.getCallingUid();
             final int callingUserId = UserHandle.getUserId(callingUid);
+            Computer snapshot = snapshotComputer();
             final boolean notify;
-            if (getInstantAppPackageName(callingUid) != null) {
-                notify = isCallerSameApp(packageName, callingUid);
+            if (snapshot.getInstantAppPackageName(callingUid) != null) {
+                notify = snapshot.isCallerSameApp(packageName, callingUid);
             } else {
-                notify = !isInstantAppInternal(packageName, callingUserId, Process.SYSTEM_UID);
+                notify = !snapshot.isInstantAppInternal(packageName, callingUserId,
+                        Process.SYSTEM_UID);
             }
             if (!notify) {
                 return;
@@ -6137,102 +5286,18 @@
             updateComponentLabelIcon(componentName, nonLocalizedLabel, icon, userId);
         }
 
-        /**
-         * Ask the package manager to perform a dex-opt with the given compiler filter.
-         *
-         * Note: exposed only for the shell command to allow moving packages explicitly to a
-         *       definite state.
-         */
-        @Override
-        public boolean performDexOptMode(String packageName,
-                boolean checkProfiles, String targetCompilerFilter, boolean force,
-                boolean bootComplete, String splitName) {
-            return mDexOptHelper.performDexOptMode(packageName, checkProfiles, targetCompilerFilter,
-                    force, bootComplete, splitName);
-        }
-
-        /**
-         * Ask the package manager to perform a dex-opt with the given compiler filter on the
-         * secondary dex files belonging to the given package.
-         *
-         * Note: exposed only for the shell command to allow moving packages explicitly to a
-         *       definite state.
-         */
-        @Override
-        public boolean performDexOptSecondary(String packageName, String compilerFilter,
-                boolean force) {
-            return mDexOptHelper.performDexOptSecondary(packageName, compilerFilter, force);
-        }
-
-        @NonNull
-        @Override
-        public ParceledListSlice<ProviderInfo> queryContentProviders(@Nullable  String processName,
-                int uid, @PackageManager.ComponentInfoFlagsBits long flags,
-                @Nullable String metaDataKey) {
-            return mComputer.queryContentProviders(processName, uid, flags, metaDataKey);
-        }
-
-        @NonNull
-        @Override
-        public ParceledListSlice<InstrumentationInfo> queryInstrumentation(
-                @NonNull String targetPackage, int flags) {
-            return mComputer.queryInstrumentation(targetPackage, flags);
-        }
-
-        @Override
-        public @NonNull ParceledListSlice<ResolveInfo> queryIntentActivities(Intent intent,
-                String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags, int userId) {
-            try {
-                Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "queryIntentActivities");
-
-                return new ParceledListSlice<>(snapshotComputer().queryIntentActivitiesInternal(intent,
-                        resolvedType, flags, userId));
-            } finally {
-                Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER);
-            }
-        }
-
-        @Override
-        public @NonNull ParceledListSlice<ResolveInfo> queryIntentActivityOptions(ComponentName caller,
-                Intent[] specifics, String[] specificTypes, Intent intent,
-                String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags, int userId) {
-            return new ParceledListSlice<>(mResolveIntentHelper.queryIntentActivityOptionsInternal(
-                    snapshotComputer(), caller, specifics, specificTypes, intent, resolvedType, flags,
-                    userId));
-        }
-
-        @Override
-        public @NonNull ParceledListSlice<ResolveInfo> queryIntentContentProviders(Intent intent,
-                String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags, int userId) {
-            return new ParceledListSlice<>(mResolveIntentHelper.queryIntentContentProvidersInternal(
-                    snapshotComputer(), intent, resolvedType, flags, userId));
-        }
-
-        @Override
-        public @NonNull ParceledListSlice<ResolveInfo> queryIntentReceivers(Intent intent,
-                String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags, int userId) {
-            return new ParceledListSlice<>(mResolveIntentHelper.queryIntentReceiversInternal(
-                    snapshotComputer(), intent, resolvedType, flags, userId, Binder.getCallingUid()));
-        }
-
-        @Override
-        public @NonNull ParceledListSlice<ResolveInfo> queryIntentServices(Intent intent,
-                String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags, int userId) {
-            final int callingUid = Binder.getCallingUid();
-            return new ParceledListSlice<>(snapshotComputer().queryIntentServicesInternal(
-                    intent, resolvedType, flags, userId, callingUid, false /*includeInstantApps*/));
-        }
-
         @Override
         public ParceledListSlice<PackageManager.Property> queryProperty(
                 String propertyName, @PackageManager.PropertyLocation int componentType) {
             Objects.requireNonNull(propertyName);
             final int callingUid = Binder.getCallingUid();
             final int callingUserId = UserHandle.getCallingUserId();
+            final Computer snapshot = snapshotComputer();
             final List<PackageManager.Property> result =
                     mPackageProperty.queryProperty(propertyName, componentType, packageName -> {
-                        final PackageStateInternal ps = getPackageStateInternal(packageName);
-                        return shouldFilterApplication(ps, callingUid, callingUserId);
+                        final PackageStateInternal ps =
+                                snapshot.getPackageStateInternal(packageName);
+                        return snapshot.shouldFilterApplication(ps, callingUid, callingUserId);
                     });
             if (result == null) {
                 return ParceledListSlice.emptyList();
@@ -6240,11 +5305,6 @@
             return new ParceledListSlice<>(result);
         }
 
-        @Deprecated
-        public void querySyncProviders(List<String> outNames, List<ProviderInfo> outInfo) {
-            mComputer.querySyncProviders(mSafeMode, outNames, outInfo);
-        }
-
         /**
          * Reconcile the information we have about the secondary dex files belonging to
          * {@code packageName} and the actual dex files. For all dex files that were
@@ -6252,9 +5312,10 @@
          */
         @Override
         public void reconcileSecondaryDexFiles(String packageName) {
-            if (getInstantAppPackageName(Binder.getCallingUid()) != null) {
+            final Computer snapshot = snapshotComputer();
+            if (snapshot.getInstantAppPackageName(Binder.getCallingUid()) != null) {
                 return;
-            } else if (isInstantAppInternal(
+            } else if (snapshot.isInstantAppInternal(
                     packageName, UserHandle.getCallingUserId(), Process.SYSTEM_UID)) {
                 return;
             }
@@ -6262,14 +5323,16 @@
         }
 
         @Override
-        public void registerDexModule(String packageName, String dexModulePath, boolean isSharedModule,
+        public void registerDexModule(String packageName, String dexModulePath,
+                boolean isSharedModule,
                 IDexModuleRegisterCallback callback) {
             int userId = UserHandle.getCallingUserId();
-            ApplicationInfo ai = getApplicationInfo(packageName, /*flags*/ 0, userId);
+            ApplicationInfo ai = snapshot().getApplicationInfo(packageName, /*flags*/ 0, userId);
             DexManager.RegisterDexModuleResult result;
             if (ai == null) {
-                Slog.w(PackageManagerService.TAG, "Registering a dex module for a package that does not exist for the" +
-                        " calling user. package=" + packageName + ", user=" + userId);
+                Slog.w(PackageManagerService.TAG,
+                        "Registering a dex module for a package that does not exist for the" +
+                                " calling user. package=" + packageName + ", user=" + userId);
                 result = new DexManager.RegisterDexModuleResult(false, "Package not installed");
             } else {
                 result = mDexManager.registerDexModule(ai, dexModulePath, isSharedModule, userId);
@@ -6278,9 +5341,11 @@
             if (callback != null) {
                 mHandler.post(() -> {
                     try {
-                        callback.onDexModuleRegistered(dexModulePath, result.success, result.message);
+                        callback.onDexModuleRegistered(dexModulePath, result.success,
+                                result.message);
                     } catch (RemoteException e) {
-                        Slog.w(PackageManagerService.TAG, "Failed to callback after module registration " + dexModulePath, e);
+                        Slog.w(PackageManagerService.TAG,
+                                "Failed to callback after module registration " + dexModulePath, e);
                     }
                 });
             }
@@ -6293,52 +5358,6 @@
             mMoveCallbacks.register(callback);
         }
 
-        // NOTE: Can't remove due to unsupported app usage
-        @Override
-        public void removePermission(String permName) {
-            // Because this is accessed via the package manager service AIDL,
-            // go through the permission manager service AIDL
-            mContext.getSystemService(PermissionManager.class).removePermission(permName);
-        }
-
-        @Override
-        public void replacePreferredActivity(IntentFilter filter, int match,
-                ComponentName[] set, ComponentName activity, int userId) {
-            mPreferredActivityHelper.replacePreferredActivity(new WatchedIntentFilter(filter),
-                    match, set, activity, userId);
-        }
-
-        @Override
-        public void resetApplicationPreferences(int userId) {
-            mPreferredActivityHelper.resetApplicationPreferences(userId);
-        }
-
-        @Override
-        public ProviderInfo resolveContentProvider(String name,
-                @PackageManager.ResolveInfoFlagsBits long flags, int userId) {
-            return mComputer.resolveContentProvider(name, flags, userId, Binder.getCallingUid());
-        }
-
-        @Override
-        public ResolveInfo resolveIntent(Intent intent, String resolvedType,
-                @PackageManager.ResolveInfoFlagsBits long flags, int userId) {
-            return mResolveIntentHelper.resolveIntentInternal(snapshotComputer(), intent, resolvedType,
-                    flags, 0 /*privateResolveFlags*/, userId, false, Binder.getCallingUid());
-        }
-
-        @Override
-        public ResolveInfo resolveService(Intent intent, String resolvedType,
-                @PackageManager.ResolveInfoFlagsBits long flags, int userId) {
-            final int callingUid = Binder.getCallingUid();
-            return mResolveIntentHelper.resolveServiceInternal(snapshotComputer(), intent, resolvedType,
-                    flags, userId, callingUid);
-        }
-
-        @Override
-        public void restoreDefaultApps(byte[] backup, int userId) {
-            mPreferredActivityHelper.restoreDefaultApps(backup, userId);
-        }
-
         @Override
         public void restoreDomainVerification(byte[] backup, int userId) {
             if (Binder.getCallingUid() != Process.SYSTEM_UID) {
@@ -6366,11 +5385,6 @@
         }
 
         @Override
-        public void restorePreferredActivities(byte[] backup, int userId) {
-            mPreferredActivityHelper.restorePreferredActivities(backup, userId);
-        }
-
-        @Override
         public void sendDeviceCustomizationReadyBroadcast() {
             mContext.enforceCallingPermission(Manifest.permission.SEND_DEVICE_CUSTOMIZATION_READY,
                     "sendDeviceCustomizationReadyBroadcast");
@@ -6386,16 +5400,17 @@
         @Override
         public void setApplicationCategoryHint(String packageName, int categoryHint,
                 String callerPackageName) {
-            if (getInstantAppPackageName(Binder.getCallingUid()) != null) {
-                throw new SecurityException("Instant applications don't have access to this method");
-            }
-            mInjector.getSystemService(AppOpsManager.class).checkPackage(Binder.getCallingUid(),
-                    callerPackageName);
-
             final PackageStateMutator.InitialState initialState = recordInitialState();
 
             final FunctionalUtils.ThrowingFunction<Computer, PackageStateMutator.Result>
                     implementation = computer -> {
+                if (computer.getInstantAppPackageName(Binder.getCallingUid()) != null) {
+                    throw new SecurityException(
+                            "Instant applications don't have access to this method");
+                }
+                mInjector.getSystemService(AppOpsManager.class)
+                        .checkPackage(Binder.getCallingUid(), callerPackageName);
+
                 PackageStateInternal packageState = computer.getPackageStateFiltered(packageName,
                         Binder.getCallingUid(), UserHandle.getCallingUserId());
                 if (packageState == null) {
@@ -6447,7 +5462,8 @@
                 int userId) {
             mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USERS, null);
             final int callingUid = Binder.getCallingUid();
-            enforceCrossUserPermission(callingUid, userId, true /* requireFullPermission */,
+            final Computer snapshot = snapshotComputer();
+            snapshot.enforceCrossUserPermission(callingUid, userId, true /* requireFullPermission */,
                     true /* checkShell */, "setApplicationHiddenSetting for user " + userId);
 
             if (hidden && isPackageDeviceAdmin(packageName, userId)) {
@@ -6464,7 +5480,7 @@
             final long callingId = Binder.clearCallingIdentity();
             try {
                 final PackageStateInternal packageState =
-                        mComputer.getPackageStateFiltered(packageName, callingUid, userId);
+                        snapshot.getPackageStateFiltered(packageName, callingUid, userId);
                 if (packageState == null) {
                     return false;
                 }
@@ -6505,13 +5521,16 @@
                 commitPackageStateMutation(null, packageName, packageState1 ->
                         packageState1.userState(userId).setHidden(hidden));
 
-                final PackageStateInternal newPackageState = getPackageStateInternal(packageName);
+                final Computer newSnapshot = snapshotComputer();
+                final PackageStateInternal newPackageState =
+                        newSnapshot.getPackageStateInternal(packageName);
 
                 if (hidden) {
                     killApplication(packageName, newPackageState.getAppId(), userId, "hiding pkg");
                     sendApplicationHiddenForUser(packageName, newPackageState, userId);
                 } else {
-                    sendPackageAddedForUser(packageName, newPackageState, userId, DataLoaderType.NONE);
+                    sendPackageAddedForUser(newSnapshot, packageName, newPackageState, userId,
+                            DataLoaderType.NONE);
                 }
 
                 scheduleWritePackageRestrictions(userId);
@@ -6526,7 +5545,8 @@
                 int userId) {
             mContext.enforceCallingOrSelfPermission(
                     Manifest.permission.DELETE_PACKAGES, null);
-            PackageStateInternal packageState = getPackageStateInternal(packageName);
+            final Computer snapshot = snapshotComputer();
+            PackageStateInternal packageState = snapshot.getPackageStateInternal(packageName);
             if (packageState != null && packageState.getPkg() != null) {
                 AndroidPackage pkg = packageState.getPkg();
                 // Cannot block uninstall SDK libs as they are controlled by SDK manager.
@@ -6584,8 +5604,10 @@
                         + userId);
             }
             Objects.requireNonNull(packageNames, "packageNames cannot be null");
+            final Computer snapshot = snapshotComputer();
             if (restrictionFlags != 0
-                    && !mSuspendPackageHelper.isSuspendAllowedForUser(userId, callingUid)) {
+                    && !mSuspendPackageHelper.isSuspendAllowedForUser(snapshot, userId,
+                    callingUid)) {
                 Slog.w(PackageManagerService.TAG, "Cannot restrict packages due to restrictions on user " + userId);
                 return packageNames;
             }
@@ -6595,16 +5617,15 @@
             final List<String> unactionedPackages = new ArrayList<>(packageNames.length);
 
             ArraySet<String> changesToCommit = new ArraySet<>();
-            Computer computer = snapshotComputer();
             final boolean[] canRestrict = (restrictionFlags != 0)
-                    ? mSuspendPackageHelper.canSuspendPackageForUser(computer, packageNames, userId,
+                    ? mSuspendPackageHelper.canSuspendPackageForUser(snapshot, packageNames, userId,
                     callingUid) : null;
             for (int i = 0; i < packageNames.length; i++) {
                 final String packageName = packageNames[i];
                 final PackageStateInternal packageState =
-                        computer.getPackageStateInternal(packageName);
+                        snapshot.getPackageStateInternal(packageName);
                 if (packageState == null
-                        || computer.shouldFilterApplication(packageState, callingUid, userId)) {
+                        || snapshot.shouldFilterApplication(packageState, callingUid, userId)) {
                     Slog.w(PackageManagerService.TAG, "Could not find package setting for package: " + packageName
                             + ". Skipping...");
                     unactionedPackages.add(packageName);
@@ -6648,11 +5669,13 @@
             final int callingUid = Binder.getCallingUid();
             final int callingAppId = UserHandle.getAppId(callingUid);
 
-            enforceCrossUserPermission(callingUid, userId, true /*requireFullPermission*/,
+            final Computer snapshot = snapshotComputer();
+            snapshot.enforceCrossUserPermission(callingUid, userId, true /*requireFullPermission*/,
                     true /*checkShell*/, "setHarmfulAppInfo");
 
             if (callingAppId != Process.SYSTEM_UID && callingAppId != Process.ROOT_UID &&
-                    checkUidPermission(SET_HARMFUL_APP_WARNINGS, callingUid) != PERMISSION_GRANTED) {
+                    snapshot.checkUidPermission(SET_HARMFUL_APP_WARNINGS, callingUid)
+                            != PERMISSION_GRANTED) {
                 throw new SecurityException("Caller must have the "
                         + SET_HARMFUL_APP_WARNINGS + " permission.");
             }
@@ -6667,11 +5690,6 @@
         }
 
         @Override
-        public void setHomeActivity(ComponentName comp, int userId) {
-            mPreferredActivityHelper.setHomeActivity(comp, userId);
-        }
-
-        @Override
         public boolean setInstallLocation(int loc) {
             mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SECURE_SETTINGS,
                     null);
@@ -6693,24 +5711,24 @@
             final int callingUid = Binder.getCallingUid();
             final int callingUserId = UserHandle.getUserId(callingUid);
             final FunctionalUtils.ThrowingCheckedFunction<Computer, Boolean, RuntimeException>
-                    implementation = computer -> {
-                if (computer.getInstantAppPackageName(callingUid) != null) {
+                    implementation = snapshot -> {
+                if (snapshot.getInstantAppPackageName(callingUid) != null) {
                     return false;
                 }
 
                 PackageStateInternal targetPackageState =
-                        computer.getPackageStateInternal(targetPackage);
+                        snapshot.getPackageStateInternal(targetPackage);
                 if (targetPackageState == null
-                        || computer.shouldFilterApplication(targetPackageState, callingUid,
+                        || snapshot.shouldFilterApplication(targetPackageState, callingUid,
                         callingUserId)) {
                     throw new IllegalArgumentException("Unknown target package: " + targetPackage);
                 }
 
                 PackageStateInternal installerPackageState = null;
                 if (installerPackageName != null) {
-                    installerPackageState = computer.getPackageStateInternal(installerPackageName);
+                    installerPackageState = snapshot.getPackageStateInternal(installerPackageName);
                     if (installerPackageState == null
-                            || shouldFilterApplication(
+                            || snapshot.shouldFilterApplication(
                             installerPackageState, callingUid, callingUserId)) {
                         throw new IllegalArgumentException("Unknown installer package: "
                                 + installerPackageName);
@@ -6720,7 +5738,7 @@
                 Signature[] callerSignature;
                 final int appId = UserHandle.getAppId(callingUid);
                 Pair<PackageStateInternal, SharedUserApi> either =
-                        computer.getPackageOrSharedUser(appId);
+                        snapshot.getPackageOrSharedUser(appId);
                 if (either != null) {
                     if (either.first != null) {
                         callerSignature = either.first.getSigningDetails().getSignatures();
@@ -6748,7 +5766,7 @@
                 String targetInstallerPackageName =
                         targetPackageState.getInstallSource().installerPackageName;
                 PackageStateInternal targetInstallerPkgSetting = targetInstallerPackageName == null
-                        ? null : computer.getPackageStateInternal(targetInstallerPackageName);
+                        ? null : snapshot.getPackageStateInternal(targetInstallerPackageName);
 
                 if (targetInstallerPkgSetting != null) {
                     if (compareSignatures(callerSignature,
@@ -6804,7 +5822,7 @@
                             }
                         }
                     }
-                    targetPackageState = getPackageStateInternal(targetPackage);
+                    targetPackageState = snapshotComputer().getPackageStateInternal(targetPackage);
                     mSettings.addInstallerPackageNames(targetPackageState.getInstallSource());
                 }
                 mAppsFilter.addPackage(targetPackageState);
@@ -6818,14 +5836,15 @@
                 return true;
             }
 
-            enforceCrossUserPermission(Binder.getCallingUid(), userId,
+            final Computer snapshot = snapshotComputer();
+            snapshot.enforceCrossUserPermission(Binder.getCallingUid(), userId,
                     true /* requireFullPermission */, true /* checkShell */,
                     "setInstantAppCookie");
-            if (!isCallerSameApp(packageName, Binder.getCallingUid())) {
+            if (!snapshot.isCallerSameApp(packageName, Binder.getCallingUid())) {
                 return false;
             }
 
-            PackageStateInternal packageState = getPackageStateInternal(packageName);
+            PackageStateInternal packageState = snapshot.getPackageStateInternal(packageName);
             if (packageState == null || packageState.getPkg() == null) {
                 return false;
             }
@@ -6840,21 +5859,15 @@
                     "setKeepUninstalledPackages requires KEEP_UNINSTALLED_PACKAGES permission");
             Objects.requireNonNull(packageList);
 
-            setKeepUninstalledPackagesInternal(packageList);
-        }
-
-        @Override
-        public void setLastChosenActivity(Intent intent, String resolvedType, int flags,
-                IntentFilter filter, int match, ComponentName activity) {
-            mPreferredActivityHelper.setLastChosenActivity(intent, resolvedType, flags,
-                    new WatchedIntentFilter(filter), match, activity);
+            setKeepUninstalledPackagesInternal(snapshot(), packageList);
         }
 
         @Override
         public void setMimeGroup(String packageName, String mimeGroup, List<String> mimeTypes) {
-            enforceOwnerRights(packageName, Binder.getCallingUid());
+            final Computer snapshot = snapshotComputer();
+            enforceOwnerRights(snapshot, packageName, Binder.getCallingUid());
             mimeTypes = CollectionUtils.emptyIfNull(mimeTypes);
-            final PackageStateInternal packageState = getPackageStateInternal(packageName);
+            final PackageStateInternal packageState = snapshot.getPackageStateInternal(packageName);
             Set<String> existingMimeTypes = packageState.getMimeGroups().get(mimeGroup);
             if (existingMimeTypes == null) {
                 throw new IllegalArgumentException("Unknown MIME group " + mimeGroup
@@ -6889,11 +5902,11 @@
                 PersistableBundle appExtras, PersistableBundle launcherExtras,
                 SuspendDialogInfo dialogInfo, String callingPackage, int userId) {
             final int callingUid = Binder.getCallingUid();
-            enforceCanSetPackagesSuspendedAsUser(callingPackage, callingUid, userId,
+            final Computer snapshot = snapshotComputer();
+            enforceCanSetPackagesSuspendedAsUser(snapshot, callingPackage, callingUid, userId,
                     "setPackagesSuspendedAsUser");
-            return mSuspendPackageHelper.setPackagesSuspended(snapshotComputer(), packageNames,
-                    suspended, appExtras, launcherExtras, dialogInfo, callingPackage, userId,
-                    callingUid);
+            return mSuspendPackageHelper.setPackagesSuspended(snapshot, packageNames, suspended,
+                    appExtras, launcherExtras, dialogInfo, callingPackage, userId, callingUid);
         }
 
         @Override
@@ -6924,12 +5937,13 @@
         public void setSplashScreenTheme(@NonNull String packageName, @Nullable String themeId,
                 int userId) {
             final int callingUid = Binder.getCallingUid();
-            enforceCrossUserPermission(callingUid, userId, false /* requireFullPermission */,
+            final Computer snapshot = snapshotComputer();
+            snapshot.enforceCrossUserPermission(callingUid, userId, false /* requireFullPermission */,
                     false /* checkShell */, "setSplashScreenTheme");
-            enforceOwnerRights(packageName, callingUid);
+            enforceOwnerRights(snapshot, packageName, callingUid);
 
-            PackageStateInternal packageState = getPackageStateInstalledFiltered(packageName,
-                    callingUid, userId);
+            PackageStateInternal packageState = filterPackageStateForInstalledAndFiltered(snapshot,
+                    packageName, callingUid, userId);
             if (packageState == null) {
                 return;
             }
@@ -6939,80 +5953,6 @@
         }
 
         @Override
-        public void setSystemAppHiddenUntilInstalled(String packageName, boolean hidden) {
-            final int callingUid = Binder.getCallingUid();
-            final boolean calledFromSystemOrPhone = callingUid == Process.PHONE_UID
-                    || callingUid == Process.SYSTEM_UID;
-            if (!calledFromSystemOrPhone) {
-                mContext.enforceCallingOrSelfPermission(Manifest.permission.SUSPEND_APPS,
-                        "setSystemAppHiddenUntilInstalled");
-            }
-
-            final PackageStateInternal stateRead = getPackageStateInternal(packageName);
-            if (stateRead == null || !stateRead.isSystem() || stateRead.getPkg() == null) {
-                return;
-            }
-            if (stateRead.getPkg().isCoreApp() && !calledFromSystemOrPhone) {
-                throw new SecurityException("Only system or phone callers can modify core apps");
-            }
-
-            commitPackageStateMutation(null, mutator -> {
-                mutator.forPackage(packageName)
-                        .setHiddenUntilInstalled(hidden);
-                mutator.forDisabledSystemPackage(packageName)
-                        .setHiddenUntilInstalled(hidden);
-            });
-        }
-
-        @Override
-        public boolean setSystemAppInstallState(String packageName, boolean installed, int userId) {
-            final int callingUid = Binder.getCallingUid();
-            final boolean calledFromSystemOrPhone = callingUid == Process.PHONE_UID
-                    || callingUid == Process.SYSTEM_UID;
-            if (!calledFromSystemOrPhone) {
-                mContext.enforceCallingOrSelfPermission(Manifest.permission.SUSPEND_APPS,
-                        "setSystemAppHiddenUntilInstalled");
-            }
-
-            final PackageStateInternal packageState = getPackageStateInternal(packageName);
-            // The target app should always be in system
-            if (packageState == null || !packageState.isSystem() || packageState.getPkg() == null) {
-                return false;
-            }
-            if (packageState.getPkg().isCoreApp() && !calledFromSystemOrPhone) {
-                throw new SecurityException("Only system or phone callers can modify core apps");
-            }
-            // Check if the install state is the same
-            if (packageState.getUserStateOrDefault(userId).isInstalled() == installed) {
-                return false;
-            }
-
-            final long callingId = Binder.clearCallingIdentity();
-            try {
-                if (installed) {
-                    // install the app from uninstalled state
-                    installExistingPackageAsUser(
-                            packageName,
-                            userId,
-                            PackageManager.INSTALL_ALL_WHITELIST_RESTRICTED_PERMISSIONS,
-                            PackageManager.INSTALL_REASON_DEVICE_SETUP,
-                            null);
-                    return true;
-                }
-
-                // uninstall the app from installed state
-                deletePackageVersioned(
-                        new VersionedPackage(packageName, PackageManager.VERSION_CODE_HIGHEST),
-                        new PackageManager.LegacyPackageDeleteObserver(null).getBinder(),
-                        userId,
-                        PackageManager.DELETE_SYSTEM_APP);
-                return true;
-            } finally {
-                Binder.restoreCallingIdentity(callingId);
-            }
-        }
-
-        @Override
         public void setUpdateAvailable(String packageName, boolean updateAvailable) {
             mContext.enforceCallingOrSelfPermission(Manifest.permission.INSTALL_PACKAGES, null);
             commitPackageStateMutation(null, packageName, state ->
@@ -7026,19 +5966,6 @@
             mMoveCallbacks.unregister(callback);
         }
 
-        @Deprecated
-        @Override
-        public boolean updateIntentVerificationStatus(String packageName, int status, int userId) {
-            return mDomainVerificationManager.setLegacyUserState(packageName, userId, status);
-        }
-
-        @Deprecated
-        @Override
-        public void verifyIntentFilter(int id, int verificationCode, List<String> failedDomains) {
-            DomainVerificationProxyV1.queueLegacyVerifyResult(mContext, mDomainVerificationConnection,
-                    id, verificationCode, failedDomains, Binder.getCallingUid());
-        }
-
         @Override
         public void verifyPendingInstall(int id, int verificationCode) throws RemoteException {
             mContext.enforceCallingOrSelfPermission(
@@ -7059,9 +5986,9 @@
                 @Checksum.TypeMask int optional, @Checksum.TypeMask int required,
                 @Nullable List trustedInstallers,
                 @NonNull IOnChecksumsReadyListener onChecksumsReadyListener, int userId) {
-            requestChecksumsInternal(packageName, includeSplits, optional, required, trustedInstallers,
-                    onChecksumsReadyListener, userId, mInjector.getBackgroundExecutor(),
-                    mInjector.getBackgroundHandler());
+            requestChecksumsInternal(snapshotComputer(), packageName, includeSplits, optional,
+                    required, trustedInstallers, onChecksumsReadyListener, userId,
+                    mInjector.getBackgroundExecutor(), mInjector.getBackgroundHandler());
         }
 
         @Override
@@ -7074,12 +6001,6 @@
         }
 
         @Override
-        public boolean canPackageQuery(@NonNull String sourcePackageName,
-                @NonNull String targetPackageName, @UserIdInt int userId) {
-            return mComputer.canPackageQuery(sourcePackageName, targetPackageName, userId);
-        }
-
-        @Override
         public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
                 throws RemoteException {
             try {
@@ -7097,8 +6018,8 @@
         public void onShellCommand(FileDescriptor in, FileDescriptor out,
                 FileDescriptor err, String[] args, ShellCallback callback,
                 ResultReceiver resultReceiver) {
-            (new PackageManagerShellCommand(mIPackageManager,
-                    mContext,mDomainVerificationManager.getShell()))
+            (new PackageManagerShellCommand(this, mContext,
+                    mDomainVerificationManager.getShell()))
                     .exec(this, in, out, err, args, callback, resultReceiver);
         }
 
@@ -7113,17 +6034,81 @@
     private class PackageManagerLocalImpl implements PackageManagerLocal {
     }
 
-    private class PackageManagerInternalImpl extends PackageManagerInternal {
+    private class PackageManagerInternalImpl extends PackageManagerInternalBase {
+
+        public PackageManagerInternalImpl() {
+            super(PackageManagerService.this);
+        }
+
+        @NonNull
         @Override
-        public List<ApplicationInfo> getInstalledApplications(
-                @PackageManager.ApplicationInfoFlagsBits long flags, int userId, int callingUid) {
-            return PackageManagerService.this.mComputer.getInstalledApplications(flags, userId,
-                    callingUid);
+        protected Context getContext() {
+            return mContext;
+        }
+
+        @NonNull
+        @Override
+        protected PermissionManagerServiceInternal getPermissionManager() {
+            return mPermissionManager;
+        }
+
+        @NonNull
+        @Override
+        protected AppDataHelper getAppDataHelper() {
+            return mAppDataHelper;
+        }
+
+        @NonNull
+        @Override
+        protected PackageObserverHelper getPackageObserverHelper() {
+            return mPackageObserverHelper;
+        }
+
+        @NonNull
+        @Override
+        protected ResolveIntentHelper getResolveIntentHelper() {
+            return mResolveIntentHelper;
+        }
+
+        @NonNull
+        @Override
+        protected SuspendPackageHelper getSuspendPackageHelper() {
+            return mSuspendPackageHelper;
+        }
+
+        @NonNull
+        @Override
+        protected ProtectedPackages getProtectedPackages() {
+            return mProtectedPackages;
+        }
+
+        @NonNull
+        @Override
+        protected UserNeedsBadgingCache getUserNeedsBadging() {
+            return mUserNeedsBadging;
+        }
+
+        @NonNull
+        @Override
+        protected InstantAppRegistry getInstantAppRegistry() {
+            return mInstantAppRegistry;
+        }
+
+        @NonNull
+        @Override
+        protected ApexManager getApexManager() {
+            return mApexManager;
+        }
+
+        @NonNull
+        @Override
+        protected DexManager getDexManager() {
+            return mDexManager;
         }
 
         @Override
         public boolean isPlatformSigned(String packageName) {
-            PackageStateInternal packageState = getPackageStateInternal(packageName);
+            PackageStateInternal packageState = snapshot().getPackageStateInternal(packageName);
             if (packageState == null) {
                 return false;
             }
@@ -7135,7 +6120,8 @@
 
         @Override
         public boolean isDataRestoreSafe(byte[] restoringFromSigHash, String packageName) {
-            SigningDetails sd = getSigningDetails(packageName);
+            final Computer snapshot = snapshot();
+            SigningDetails sd = snapshot.getSigningDetails(packageName);
             if (sd == null) {
                 return false;
             }
@@ -7145,7 +6131,8 @@
 
         @Override
         public boolean isDataRestoreSafe(Signature restoringFromSig, String packageName) {
-            SigningDetails sd = getSigningDetails(packageName);
+            final Computer snapshot = snapshot();
+            SigningDetails sd = snapshot.getSigningDetails(packageName);
             if (sd == null) {
                 return false;
             }
@@ -7156,100 +6143,17 @@
         @Override
         public boolean hasSignatureCapability(int serverUid, int clientUid,
                 @SigningDetails.CertCapabilities int capability) {
-            SigningDetails serverSigningDetails = getSigningDetails(serverUid);
-            SigningDetails clientSigningDetails = getSigningDetails(clientUid);
+            final Computer snapshot = snapshot();
+            SigningDetails serverSigningDetails = snapshot.getSigningDetails(serverUid);
+            SigningDetails clientSigningDetails = snapshot.getSigningDetails(clientUid);
             return serverSigningDetails.checkCapability(clientSigningDetails, capability)
                     || clientSigningDetails.hasAncestorOrSelf(serverSigningDetails);
-
-        }
-
-        private SigningDetails getSigningDetails(@NonNull String packageName) {
-            return PackageManagerService.this.getSigningDetails(packageName);
-        }
-
-        private SigningDetails getSigningDetails(int uid) {
-            return PackageManagerService.this.getSigningDetails(uid);
-        }
-
-        @Override
-        public boolean isInstantApp(String packageName, int userId) {
-            return PackageManagerService.this.mIPackageManager.isInstantApp(packageName, userId);
-        }
-
-        @Override
-        public String getInstantAppPackageName(int uid) {
-            return PackageManagerService.this.getInstantAppPackageName(uid);
-        }
-
-        @Override
-        public boolean filterAppAccess(AndroidPackage pkg, int callingUid, int userId) {
-            return PackageManagerService.this.filterAppAccess(pkg, callingUid, userId);
-        }
-
-        @Override
-        public boolean filterAppAccess(String packageName, int callingUid, int userId) {
-            return PackageManagerService.this.filterAppAccess(packageName, callingUid, userId);
-        }
-
-        @Override
-        public boolean filterAppAccess(int uid, int callingUid) {
-            return PackageManagerService.this.filterAppAccess(uid, callingUid);
-        }
-
-        @Nullable
-        @Override
-        public int[] getVisibilityAllowList(@NonNull String packageName, int userId) {
-            return PackageManagerService.this.getVisibilityAllowList(packageName, userId);
-        }
-
-        @Override
-        public boolean canQueryPackage(int callingUid, @Nullable String packageName) {
-            return PackageManagerService.this.canQueryPackage(callingUid, packageName);
-        }
-
-        @Override
-        public AndroidPackage getPackage(String packageName) {
-            return PackageManagerService.this.getPackage(packageName);
-        }
-
-        @Nullable
-        @Override
-        public AndroidPackageApi getAndroidPackage(@NonNull String packageName) {
-            return PackageManagerService.this.getPackage(packageName);
-        }
-
-        @Override
-        public AndroidPackage getPackage(int uid) {
-            return PackageManagerService.this.getPackage(uid);
-        }
-
-        @Override
-        public List<AndroidPackage> getPackagesForAppId(int appId) {
-            return mComputer.getPackagesForAppId(appId);
-        }
-
-        @Nullable
-        @Override
-        public PackageStateInternal getPackageStateInternal(String packageName) {
-            return PackageManagerService.this.getPackageStateInternal(packageName);
-        }
-
-        @Nullable
-        @Override
-        public PackageState getPackageState(@NonNull String packageName) {
-            return PackageManagerService.this.getPackageState(packageName);
-        }
-
-        @NonNull
-        @Override
-        public ArrayMap<String, ? extends PackageStateInternal> getPackageStates() {
-            return PackageManagerService.this.getPackageStates();
         }
 
         @Override
         public PackageList getPackageList(@Nullable PackageListObserver observer) {
             final ArrayList<String> list = new ArrayList<>();
-            forEachPackageState(packageState -> {
+            PackageManagerService.this.forEachPackageState(snapshot(), packageState -> {
                 AndroidPackage pkg = packageState.getPkg();
                 if (pkg != null) {
                     list.add(pkg.getPackageName());
@@ -7263,19 +6167,9 @@
         }
 
         @Override
-        public void removePackageListObserver(PackageListObserver observer) {
-            mPackageObserverHelper.removeObserver(observer);
-        }
-
-        @Override
-        public PackageStateInternal getDisabledSystemPackage(@NonNull String packageName) {
-            return snapshotComputer().getDisabledSystemPackage(packageName);
-        }
-
-        @Override
         public @Nullable
         String getDisabledSystemPackageName(@NonNull String packageName) {
-            PackageStateInternal disabledPkgSetting = getDisabledSystemPackage(
+            PackageStateInternal disabledPkgSetting = snapshot().getDisabledSystemPackage(
                     packageName);
             AndroidPackage disabledPkg = disabledPkgSetting == null
                     ? null : disabledPkgSetting.getPkg();
@@ -7283,51 +6177,15 @@
         }
 
         @Override
-        public @NonNull String[] getKnownPackageNames(int knownPackage, int userId) {
-            return PackageManagerService.this.getKnownPackageNamesInternal(knownPackage, userId);
-        }
-
-        @Override
-        public boolean isSameApp(@Nullable String packageName, int callingUid, int userId) {
-            if (packageName == null) {
-                return false;
-            }
-
-            if (Process.isSdkSandboxUid(callingUid)) {
-                return packageName.equals(getSdkSandboxPackageName());
-            }
-            int uid = getPackageUid(packageName, 0, userId);
-            return UserHandle.isSameApp(uid, callingUid);
-        }
-
-        @Override
         public boolean isResolveActivityComponent(ComponentInfo component) {
             return mResolveActivity.packageName.equals(component.packageName)
                     && mResolveActivity.name.equals(component.name);
         }
 
         @Override
-        public void setKeepUninstalledPackages(final List<String> packageList) {
-            PackageManagerService.this.setKeepUninstalledPackagesInternal(packageList);
-        }
-
-        @Override
-        public boolean isPermissionsReviewRequired(String packageName, int userId) {
-            return mPermissionManager.isPermissionsReviewRequired(packageName, userId);
-        }
-
-        @Override
-        public PackageInfo getPackageInfo(
-                String packageName, @PackageManager.PackageInfoFlagsBits long flags,
-                int filterCallingUid, int userId) {
-            return PackageManagerService.this.mComputer
-                    .getPackageInfoInternal(packageName, PackageManager.VERSION_CODE_HIGHEST,
-                            flags, filterCallingUid, userId);
-        }
-
-        @Override
         public long getCeDataInode(String packageName, int userId) {
-            final PackageStateInternal packageState = getPackageStateInternal(packageName);
+            final PackageStateInternal packageState =
+                    snapshot().getPackageStateInternal(packageName);
             if (packageState == null) {
                 return 0;
             } else {
@@ -7336,18 +6194,6 @@
         }
 
         @Override
-        public Bundle getSuspendedPackageLauncherExtras(String packageName, int userId) {
-            return mSuspendPackageHelper.getSuspendedPackageLauncherExtras(
-                    packageName, userId, Binder.getCallingUid());
-        }
-
-        @Override
-        public boolean isPackageSuspended(String packageName, int userId) {
-            return mSuspendPackageHelper.isPackageSuspended(
-                    packageName, userId, Binder.getCallingUid());
-        }
-
-        @Override
         public void removeAllNonSystemPackageSuspensions(int userId) {
             final Computer computer = snapshotComputer();
             final String[] allPackages = computer.getAllAvailablePackageNames();
@@ -7357,14 +6203,6 @@
         }
 
         @Override
-        public void removeNonSystemPackageSuspensions(String packageName, int userId) {
-            mSuspendPackageHelper.removeSuspensionsBySuspendingPackage(snapshotComputer(),
-                    new String[]{packageName},
-                    (suspendingPackage) -> !PLATFORM_PACKAGE_NAME.equals(suspendingPackage),
-                    userId);
-        }
-
-        @Override
         public void flushPackageRestrictions(int userId) {
             synchronized (mLock) {
                 PackageManagerService.this.flushPackageRestrictionsAsUserInternalLocked(userId);
@@ -7372,103 +6210,6 @@
         }
 
         @Override
-        public void removeDistractingPackageRestrictions(String packageName, int userId) {
-            PackageManagerService.this.removeDistractingPackageRestrictions(
-                    new String[]{packageName}, userId);
-        }
-
-        @Override
-        public void removeAllDistractingPackageRestrictions(int userId) {
-            PackageManagerService.this.removeAllDistractingPackageRestrictions(userId);
-        }
-
-        @Override
-        public String getSuspendingPackage(String suspendedPackage, int userId) {
-            return mSuspendPackageHelper.getSuspendingPackage(
-                    suspendedPackage, userId, Binder.getCallingUid());
-        }
-
-        @Override
-        public SuspendDialogInfo getSuspendedDialogInfo(String suspendedPackage,
-                String suspendingPackage, int userId) {
-            return mSuspendPackageHelper.getSuspendedDialogInfo(
-                    suspendedPackage, suspendingPackage, userId, Binder.getCallingUid());
-        }
-
-        @Override
-        public int getDistractingPackageRestrictions(String packageName, int userId) {
-            final PackageStateInternal packageState = getPackageStateInternal(packageName);
-            return (packageState == null) ? RESTRICTION_NONE
-                    : packageState.getUserStateOrDefault(userId).getDistractionFlags();
-        }
-
-        @Override
-        public int getPackageUid(String packageName,
-                @PackageManager.PackageInfoFlagsBits long flags, int userId) {
-            return PackageManagerService.this
-                    .getPackageUidInternal(packageName, flags, userId, Process.SYSTEM_UID);
-        }
-
-        @Override
-        public ApplicationInfo getApplicationInfo(
-                String packageName, @PackageManager.ApplicationInfoFlagsBits long flags,
-                int filterCallingUid, int userId) {
-            return PackageManagerService.this
-                    .getApplicationInfoInternal(packageName, flags, filterCallingUid, userId);
-        }
-
-        @Override
-        public ActivityInfo getActivityInfo(
-                ComponentName component, @PackageManager.ComponentInfoFlagsBits long flags,
-                int filterCallingUid, int userId) {
-            return PackageManagerService.this
-                    .getActivityInfoInternal(component, flags, filterCallingUid, userId);
-        }
-
-        @Override
-        public List<ResolveInfo> queryIntentActivities(
-                Intent intent, String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags,
-                int filterCallingUid, int userId) {
-            return snapshotComputer().queryIntentActivitiesInternal(intent, resolvedType, flags,
-                    userId);
-        }
-
-        @Override
-        public List<ResolveInfo> queryIntentReceivers(Intent intent,
-                String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags,
-                int filterCallingUid, int userId) {
-            return PackageManagerService.this.mResolveIntentHelper.queryIntentReceiversInternal(
-                    snapshotComputer(), intent, resolvedType, flags, userId, filterCallingUid);
-        }
-
-        @Override
-        public List<ResolveInfo> queryIntentServices(
-                Intent intent, @PackageManager.ResolveInfoFlagsBits long flags, int callingUid,
-                int userId) {
-            final String resolvedType = intent.resolveTypeIfNeeded(mContext.getContentResolver());
-            return PackageManagerService.this
-                    .queryIntentServicesInternal(intent, resolvedType, flags, userId, callingUid,
-                            false);
-        }
-
-        @Override
-        public ComponentName getHomeActivitiesAsUser(List<ResolveInfo> allHomeCandidates,
-                int userId) {
-            return PackageManagerService.this.getHomeActivitiesAsUser(allHomeCandidates, userId);
-        }
-
-        @Override
-        public ComponentName getDefaultHomeActivity(int userId) {
-            return PackageManagerService.this.getDefaultHomeActivity(userId);
-        }
-
-        @Override
-        public ComponentName getSystemUiServiceComponent() {
-            return ComponentName.unflattenFromString(mContext.getResources().getString(
-                    com.android.internal.R.string.config_systemUIServiceComponent));
-        }
-
-        @Override
         public void setDeviceAndProfileOwnerPackages(
                 int deviceOwnerUserId, String deviceOwnerPackage,
                 SparseArray<String> profileOwnerPackages) {
@@ -7487,118 +6228,6 @@
         }
 
         @Override
-        public void setDeviceOwnerProtectedPackages(
-                String deviceOwnerPackageName, List<String> packageNames) {
-            mProtectedPackages.setDeviceOwnerProtectedPackages(
-                    deviceOwnerPackageName, packageNames);
-        }
-
-        @Override
-        public boolean isPackageDataProtected(int userId, String packageName) {
-            return mProtectedPackages.isPackageDataProtected(userId, packageName);
-        }
-
-        @Override
-        public boolean isPackageStateProtected(String packageName, int userId) {
-            return mProtectedPackages.isPackageStateProtected(userId, packageName);
-        }
-
-        @Override
-        public boolean isPackageEphemeral(int userId, String packageName) {
-            final PackageStateInternal packageState = getPackageStateInternal(packageName);
-            return packageState != null
-                    && packageState.getUserStateOrDefault(userId).isInstantApp();
-        }
-
-        @Override
-        public boolean wasPackageEverLaunched(String packageName, int userId) {
-            final PackageStateInternal packageState = getPackageStateInternal(packageName);
-            if (packageState == null) {
-                throw new IllegalArgumentException("Unknown package: " + packageName);
-            }
-            return !packageState.getUserStateOrDefault(userId).isNotLaunched();
-        }
-
-        @Override
-        public boolean isEnabledAndMatches(ParsedMainComponent component, long flags, int userId) {
-            return PackageStateUtils.isEnabledAndMatches(
-                    getPackageStateInternal(component.getPackageName()), component, flags, userId);
-        }
-
-        @Override
-        public boolean userNeedsBadging(int userId) {
-            synchronized (mLock) {
-                return PackageManagerService.this.userNeedsBadging(userId);
-            }
-        }
-
-        @Override
-        public String getNameForUid(int uid) {
-            return mIPackageManager.getNameForUid(uid);
-        }
-
-        @Override
-        public void requestInstantAppResolutionPhaseTwo(AuxiliaryResolveInfo responseObj,
-                Intent origIntent, String resolvedType, String callingPackage,
-                @Nullable String callingFeatureId, boolean isRequesterInstantApp,
-                Bundle verificationBundle, int userId) {
-            PackageManagerService.this.requestInstantAppResolutionPhaseTwo(responseObj, origIntent,
-                    resolvedType, callingPackage, callingFeatureId, isRequesterInstantApp,
-                    verificationBundle, userId);
-        }
-
-        @Override
-        public void grantImplicitAccess(int userId, Intent intent,
-                int recipientAppId, int visibleUid, boolean direct) {
-            grantImplicitAccess(userId, intent, recipientAppId, visibleUid, direct,
-                    false /* retainOnUpdate */);
-        }
-
-        @Override
-        public void grantImplicitAccess(int userId, Intent intent,
-                int recipientAppId, int visibleUid, boolean direct, boolean retainOnUpdate) {
-            Computer computer = snapshotComputer();
-            final AndroidPackage visiblePackage = computer.getPackage(visibleUid);
-            final int recipientUid = UserHandle.getUid(userId, recipientAppId);
-            if (visiblePackage == null || computer.getPackage(recipientUid) == null) {
-                return;
-            }
-
-            final boolean instantApp = computer.isInstantAppInternal(
-                    visiblePackage.getPackageName(), userId, visibleUid);
-            final boolean accessGranted;
-            if (instantApp) {
-                if (!direct) {
-                    // if the interaction that lead to this granting access to an instant app
-                    // was indirect (i.e.: URI permission grant), do not actually execute the
-                    // grant.
-                    return;
-                }
-                accessGranted = mInstantAppRegistry.grantInstantAccess(userId, intent,
-                        recipientAppId, UserHandle.getAppId(visibleUid) /*instantAppId*/);
-            } else {
-                accessGranted = mAppsFilter.grantImplicitAccess(recipientUid, visibleUid,
-                        retainOnUpdate);
-            }
-
-            if (accessGranted) {
-                ApplicationPackageManager.invalidateGetPackagesForUidCache();
-            }
-        }
-
-        @Override
-        public boolean isInstantAppInstallerComponent(ComponentName component) {
-            final ActivityInfo instantAppInstallerActivity = mInstantAppInstallerActivity;
-            return instantAppInstallerActivity != null
-                    && instantAppInstallerActivity.getComponentName().equals(component);
-        }
-
-        @Override
-        public void pruneInstantApps() {
-            mInstantAppRegistry.pruneInstantApps(snapshotComputer());
-        }
-
-        @Override
         public void pruneCachedApksInApex(@NonNull List<PackageInfo> apexPackages) {
             if (mCacheDir == null) {
                 return;
@@ -7606,11 +6235,12 @@
 
             final PackageCacher cacher = new PackageCacher(mCacheDir);
             synchronized (mLock) {
+                final Computer snapshot = snapshot();
                 for (int i = 0, size = apexPackages.size(); i < size; i++) {
                     final List<String> apkNames =
                             mApexManager.getApksInApex(apexPackages.get(i).packageName);
                     for (int j = 0, apksInApex = apkNames.size(); j < apksInApex; j++) {
-                        final AndroidPackage pkg = getPackage(apkNames.get(j));
+                        final AndroidPackage pkg = snapshot.getPackage(apkNames.get(j));
                         cacher.cleanCachedResult(new File(pkg.getPath()));
                     }
                 }
@@ -7618,10 +6248,6 @@
         }
 
         @Override
-        public String getSetupWizardPackageName() {
-            return mSetupWizardPackage;
-        }
-
         public void setExternalSourcesPolicy(ExternalSourcesPolicy policy) {
             if (policy != null) {
                 mExternalSourcesPolicy = policy;
@@ -7630,7 +6256,8 @@
 
         @Override
         public boolean isPackagePersistent(String packageName) {
-            final PackageStateInternal packageState = getPackageStateInternal(packageName);
+            final PackageStateInternal packageState =
+                    snapshot().getPackageStateInternal(packageName);
             if (packageState == null) {
                 return false;
             }
@@ -7641,16 +6268,20 @@
 
         @Override
         public List<PackageInfo> getOverlayPackages(int userId) {
+            final Computer snapshot = snapshotComputer();
             final ArrayList<PackageInfo> overlayPackages = new ArrayList<>();
-            forEachPackageState(packageState -> {
+            final ArrayMap<String, ? extends PackageStateInternal> packageStates =
+                    snapshot.getPackageStates();
+            for (int index = 0; index < packageStates.size(); index++) {
+                final PackageStateInternal packageState = packageStates.valueAt(index);
                 final AndroidPackage pkg = packageState.getPkg();
                 if (pkg != null && pkg.getOverlayTarget() != null) {
-                    PackageInfo pkgInfo = generatePackageInfo(packageState, 0, userId);
+                    PackageInfo pkgInfo = snapshot.generatePackageInfo(packageState, 0, userId);
                     if (pkgInfo != null) {
                         overlayPackages.add(pkgInfo);
                     }
                 }
-            });
+            }
 
             return overlayPackages;
         }
@@ -7658,7 +6289,7 @@
         @Override
         public List<String> getTargetPackageNames(int userId) {
             List<String> targetPackages = new ArrayList<>();
-            forEachPackageState(packageState -> {
+            PackageManagerService.this.forEachPackageState(snapshot(), packageState -> {
                 final AndroidPackage pkg = packageState.getPkg();
                 if (pkg != null && !pkg.isOverlay()) {
                     targetPackages.add(pkg.getPackageName());
@@ -7676,30 +6307,6 @@
         }
 
         @Override
-        public ResolveInfo resolveIntent(Intent intent, String resolvedType,
-                @PackageManager.ResolveInfoFlagsBits long flags,
-                @PackageManagerInternal.PrivateResolveFlags long privateResolveFlags, int userId,
-                boolean resolveForStart, int filterCallingUid) {
-            return mResolveIntentHelper.resolveIntentInternal(snapshotComputer(),
-                    intent, resolvedType, flags, privateResolveFlags, userId, resolveForStart,
-                    filterCallingUid);
-        }
-
-        @Override
-        public ResolveInfo resolveService(Intent intent, String resolvedType,
-                @PackageManager.ResolveInfoFlagsBits long flags, int userId, int callingUid) {
-            return mResolveIntentHelper.resolveServiceInternal(snapshotComputer(), intent,
-                    resolvedType, flags, userId, callingUid);
-        }
-
-        @Override
-        public ProviderInfo resolveContentProvider(String name,
-                @PackageManager.ResolveInfoFlagsBits long flags, int userId, int callingUid) {
-            return PackageManagerService.this.mComputer
-                    .resolveContentProvider(name, flags, userId,callingUid);
-        }
-
-        @Override
         public void addIsolatedUid(int isolatedUid, int ownerUid) {
             synchronized (mLock) {
                 mIsolatedOwners.put(isolatedUid, ownerUid);
@@ -7714,146 +6321,12 @@
         }
 
         @Override
-        public int getUidTargetSdkVersion(int uid) {
-            return PackageManagerService.this.getUidTargetSdkVersion(uid);
-        }
-
-        @Override
-        public int getPackageTargetSdkVersion(String packageName) {
-            final PackageStateInternal packageState = getPackageStateInternal(packageName);
-            if (packageState != null && packageState.getPkg() != null) {
-                return packageState.getPkg().getTargetSdkVersion();
-            }
-            return Build.VERSION_CODES.CUR_DEVELOPMENT;
-        }
-
-        @Override
-        public boolean canAccessInstantApps(int callingUid, int userId) {
-            return PackageManagerService.this.canViewInstantApps(callingUid, userId);
-        }
-
-        @Override
-        public boolean canAccessComponent(int callingUid, @NonNull ComponentName component,
-                @UserIdInt int userId) {
-            return mComputer.canAccessComponent(callingUid, component, userId);
-        }
-
-        @Override
-        public boolean hasInstantApplicationMetadata(String packageName, int userId) {
-            return mInstantAppRegistry.hasInstantApplicationMetadata(packageName, userId);
-        }
-
-        @Override
         public void notifyPackageUse(String packageName, int reason) {
             synchronized (mLock) {
                 PackageManagerService.this.notifyPackageUseInternal(packageName, reason);
             }
         }
 
-        @Override
-        public void onPackageProcessKilledForUninstall(String packageName) {
-            mHandler.post(() -> PackageManagerService.this.notifyInstallObserver(packageName,
-                    true /* killApp */));
-        }
-
-        @Override
-        public SparseArray<String> getAppsWithSharedUserIds() {
-            return mComputer.getAppsWithSharedUserIds();
-        }
-
-        @Override
-        @NonNull
-        public String[] getSharedUserPackagesForPackage(String packageName, int userId) {
-            return mComputer.getSharedUserPackagesForPackage(packageName, userId);
-        }
-
-        @Override
-        public ArrayMap<String, ProcessInfo> getProcessesForUid(int uid) {
-            return mComputer.getProcessesForUid(uid);
-        }
-
-        @Override
-        public int[] getPermissionGids(String permissionName, int userId) {
-            return mPermissionManager.getPermissionGids(permissionName, userId);
-        }
-
-        @Override
-        public boolean isOnlyCoreApps() {
-            return mIPackageManager.isOnlyCoreApps();
-        }
-
-        @Override
-        public void freeStorage(String volumeUuid, long bytes,
-                @StorageManager.AllocateFlags int flags) throws IOException {
-            PackageManagerService.this.freeStorage(volumeUuid, bytes, flags);
-        }
-
-        @Override
-        public void freeAllAppCacheAboveQuota(@NonNull String volumeUuid) throws IOException {
-            PackageManagerService.this.freeAllAppCacheAboveQuota(volumeUuid);
-        }
-
-        @Override
-        public void forEachPackageSetting(Consumer<PackageSetting> actionLocked) {
-            PackageManagerService.this.forEachPackageSetting(actionLocked);
-        }
-
-        @Override
-        public void forEachPackageState(Consumer<PackageStateInternal> action) {
-            PackageManagerService.this.forEachPackageState(action);
-        }
-
-        @Override
-        public void forEachPackage(Consumer<AndroidPackage> action) {
-            PackageManagerService.this.forEachPackage(action);
-        }
-
-        @Override
-        public void forEachInstalledPackage(@NonNull Consumer<AndroidPackage> action,
-                @UserIdInt int userId) {
-            PackageManagerService.this.forEachInstalledPackage(action, userId);
-        }
-
-        @Override
-        public ArraySet<String> getEnabledComponents(String packageName, int userId) {
-            final PackageStateInternal packageState = getPackageStateInternal(packageName);
-            if (packageState == null) {
-                return new ArraySet<>();
-            }
-            return packageState.getUserStateOrDefault(userId).getEnabledComponents();
-        }
-
-        @Override
-        public ArraySet<String> getDisabledComponents(String packageName, int userId) {
-            final PackageStateInternal packageState = getPackageStateInternal(packageName);
-            if (packageState == null) {
-                return new ArraySet<>();
-            }
-            return packageState.getUserStateOrDefault(userId).getDisabledComponents();
-        }
-
-        @Override
-        public @PackageManager.EnabledState int getApplicationEnabledState(
-                String packageName, int userId) {
-            final PackageStateInternal packageState = getPackageStateInternal(packageName);
-            if (packageState == null) {
-                return COMPONENT_ENABLED_STATE_DEFAULT;
-            }
-            return packageState.getUserStateOrDefault(userId).getEnabledState();
-        }
-
-        @Override
-        public @PackageManager.EnabledState int getComponentEnabledSetting(
-                @NonNull ComponentName componentName, int callingUid, int userId) {
-            return PackageManagerService.this.mComputer.getComponentEnabledSettingInternal(
-                    componentName, callingUid, userId);
-        }
-
-        @Override
-        public void setEnableRollbackCode(int token, int enableRollbackCode) {
-            PackageManagerService.this.setEnableRollbackCode(token, enableRollbackCode);
-        }
-
         /**
          * Ask the package manager to compile layouts in the given package.
          */
@@ -7869,11 +6342,6 @@
             return mArtManagerService.compileLayouts(pkg);
         }
 
-        @Override
-        public void finishPackageInstall(int token, boolean didLaunch) {
-            mIPackageManager.finishPackageInstall(token, didLaunch);
-        }
-
         @Nullable
         @Override
         public String removeLegacyDefaultBrowserPackageName(int userId) {
@@ -7883,16 +6351,6 @@
         }
 
         @Override
-        public boolean isApexPackage(String packageName) {
-            return PackageManagerService.this.mApexManager.isApexPackage(packageName);
-        }
-
-        @Override
-        public List<String> getApksInApex(String apexPackageName) {
-            return PackageManagerService.this.mApexManager.getApksInApex(apexPackageName);
-        }
-
-        @Override
         public void uninstallApex(String packageName, long versionCode, int userId,
                 IntentSender intentSender, int flags) {
             final int callerUid = Binder.getCallingUid();
@@ -7967,11 +6425,6 @@
         }
 
         @Override
-        public boolean isCallerInstallerOfRecord(@NonNull AndroidPackage pkg, int callingUid) {
-            return mComputer.isCallerInstallerOfRecord(pkg, callingUid);
-        }
-
-        @Override
         public boolean isPermissionUpgradeNeeded(int userId) {
             return mSettings.isPermissionUpgradeNeeded(userId);
         }
@@ -7985,13 +6438,9 @@
         }
 
         @Override
-        public List<String> getMimeGroup(String packageName, String mimeGroup) {
-            return PackageManagerService.this.getMimeGroupInternal(packageName, mimeGroup);
-        }
-
-        @Override
         public void setVisibilityLogging(String packageName, boolean enable) {
-            final PackageStateInternal packageState = getPackageStateInternal(packageName);
+            final PackageStateInternal packageState =
+                    snapshot().getPackageStateInternal(packageName);
             if (packageState == null) {
                 throw new IllegalStateException("No package found for " + packageName);
             }
@@ -7999,12 +6448,6 @@
         }
 
         @Override
-        public boolean isSystemPackage(@NonNull String packageName) {
-            return packageName.equals(
-                    PackageManagerService.this.ensureSystemPackageName(packageName));
-        }
-
-        @Override
         public void clearBlockUninstallForUser(@UserIdInt int userId) {
             synchronized (mLock) {
                 mSettings.clearBlockUninstallLPw(userId);
@@ -8013,21 +6456,11 @@
         }
 
         @Override
-        public void unsuspendForSuspendingPackage(final String packageName, int affectedUser) {
-            PackageManagerService.this.unsuspendForSuspendingPackage(snapshotComputer(),
-                    packageName, affectedUser);
-        }
-
-        @Override
-        public boolean isSuspendingAnyPackages(String suspendingPackage, int userId) {
-            return PackageManagerService.this.isSuspendingAnyPackages(suspendingPackage, userId);
-        }
-
-        @Override
         public boolean registerInstalledLoadingProgressCallback(String packageName,
                 PackageManagerInternal.InstalledLoadingProgressCallback callback, int userId) {
-            final PackageStateInternal ps =
-                    getPackageStateInstalledFiltered(packageName, Binder.getCallingUid(), userId);
+            final Computer snapshot = snapshotComputer();
+            final PackageStateInternal ps = filterPackageStateForInstalledAndFiltered(snapshot,
+                    packageName, Binder.getCallingUid(), userId);
             if (ps == null) {
                 return false;
             }
@@ -8048,8 +6481,9 @@
         @Override
         public IncrementalStatesInfo getIncrementalStatesInfo(
                 @NonNull String packageName, int filterCallingUid, int userId) {
-            final PackageStateInternal ps =
-                    getPackageStateInstalledFiltered(packageName, filterCallingUid, userId);
+            final Computer snapshot = snapshotComputer();
+            final PackageStateInternal ps = filterPackageStateForInstalledAndFiltered(snapshot,
+                    packageName, filterCallingUid, userId);
             if (ps == null) {
                 return null;
             }
@@ -8057,74 +6491,23 @@
         }
 
         @Override
-        public void requestChecksums(@NonNull String packageName, boolean includeSplits,
-                @Checksum.TypeMask int optional, @Checksum.TypeMask int required,
-                @Nullable List trustedInstallers,
-                @NonNull IOnChecksumsReadyListener onChecksumsReadyListener, int userId,
-                @NonNull Executor executor, @NonNull Handler handler) {
-            requestChecksumsInternal(packageName, includeSplits, optional, required,
-                    trustedInstallers, onChecksumsReadyListener, userId, executor, handler);
+        public boolean isSameApp(@Nullable String packageName, int callingUid, int userId) {
+            if (packageName == null) {
+                return false;
+            }
+
+            if (Process.isSdkSandboxUid(callingUid)) {
+                return packageName.equals(mRequiredSdkSandboxPackage);
+            }
+            Computer snapshot = snapshot();
+            int uid = snapshot.getPackageUid(packageName, 0, userId);
+            return UserHandle.isSameApp(uid, callingUid);
         }
 
         @Override
-        public boolean isPackageFrozen(@NonNull String packageName,
-                int callingUid, int userId) {
-            return PackageManagerService.this.getPackageStartability(
-                    packageName, callingUid, userId) == PACKAGE_STARTABILITY_FROZEN;
-        }
-
-        @Override
-        public long deleteOatArtifactsOfPackage(String packageName) {
-            return PackageManagerService.this.deleteOatArtifactsOfPackage(packageName);
-        }
-
-        @Override
-        public void reconcileAppsData(int userId, @StorageManager.StorageFlags int flags,
-                boolean migrateAppsData) {
-            PackageManagerService.this.mAppDataHelper.reconcileAppsData(userId, flags,
-                    migrateAppsData);
-        }
-
-        @Override
-        @NonNull
-        public ArraySet<PackageStateInternal> getSharedUserPackages(int sharedUserAppId) {
-            return PackageManagerService.this.mComputer.getSharedUserPackages(sharedUserAppId);
-        }
-
-        @Override
-        @Nullable
-        public SharedUserApi getSharedUserApi(int sharedUserAppId) {
-            return mComputer.getSharedUser(sharedUserAppId);
-        }
-
-        @NonNull
-        @Override
-        public PackageStateMutator.InitialState recordInitialState() {
-            return PackageManagerService.this.recordInitialState();
-        }
-
-        @Nullable
-        @Override
-        public PackageStateMutator.Result commitPackageStateMutation(
-                @Nullable PackageStateMutator.InitialState state,
-                @NonNull Consumer<PackageStateMutator> consumer) {
-            return PackageManagerService.this.commitPackageStateMutation(state, consumer);
-        }
-
-        @NonNull
-        @Override
-        public Computer snapshot() {
-            return snapshotComputer();
-        }
-
-        @Override
-        public void shutdown() {
-            PackageManagerService.this.shutdown();
-        }
-
-        @Override
-        public DynamicCodeLogger getDynamicCodeLogger() {
-            return PackageManagerService.this.getDexManager().getDynamicCodeLogger();
+        public void onPackageProcessKilledForUninstall(String packageName) {
+            mHandler.post(() -> PackageManagerService.this.notifyInstallObserver(packageName,
+                    true /* killApp */));
         }
     }
 
@@ -8239,24 +6622,6 @@
         return mSettings.getDisabledSystemPkgLPr(packageName);
     }
 
-    @VisibleForTesting(visibility = Visibility.PRIVATE)
-    @Nullable
-    PackageStateInternal getPackageStateInternal(String packageName) {
-        return mComputer.getPackageStateInternal(packageName);
-    }
-
-    @Nullable
-    PackageStateInternal getPackageStateInternal(String packageName, int callingUid) {
-        return mComputer.getPackageStateInternal(packageName, callingUid);
-    }
-
-    @Nullable
-    PackageStateInternal getPackageStateInstalledFiltered(@NonNull String packageName,
-            int callingUid, @UserIdInt int userId) {
-        return filterPackageStateForInstalledAndFiltered(mComputer, packageName, callingUid,
-                userId);
-    }
-
     @Nullable
     private PackageStateInternal filterPackageStateForInstalledAndFiltered(
             @NonNull Computer computer, @NonNull String packageName, int callingUid,
@@ -8272,22 +6637,8 @@
         }
     }
 
-    @Nullable
-    private PackageState getPackageState(String packageName) {
-        return mComputer.getPackageStateCopied(packageName);
-    }
-
-    @NonNull
-    ArrayMap<String, ? extends PackageStateInternal> getPackageStates() {
-        Computer computer = snapshotComputer();
-        if (computer == mLiveComputer) {
-            return new ArrayMap<>(computer.getPackageStates());
-        } else {
-            return computer.getPackageStates();
-        }
-    }
-
-    private void forEachPackageSetting(Consumer<PackageSetting> actionLocked) {
+    @Deprecated
+    void forEachPackageSetting(Consumer<PackageSetting> actionLocked) {
         synchronized (mLock) {
             int size = mSettings.getPackagesLocked().size();
             for (int index = 0; index < size; index++) {
@@ -8296,13 +6647,13 @@
         }
     }
 
-    void forEachPackageState(Consumer<PackageStateInternal> consumer) {
-        forEachPackageState(mComputer.getPackageStates(), consumer);
+    void forEachPackageState(@NonNull Computer snapshot, Consumer<PackageStateInternal> consumer) {
+        forEachPackageState(snapshot.getPackageStates(), consumer);
     }
 
-    void forEachPackage(Consumer<AndroidPackage> consumer) {
+    void forEachPackage(@NonNull Computer snapshot, Consumer<AndroidPackage> consumer) {
         final ArrayMap<String, ? extends PackageStateInternal> packageStates =
-                mComputer.getPackageStates();
+                snapshot.getPackageStates();
         int size = packageStates.size();
         for (int index = 0; index < size; index++) {
             PackageStateInternal packageState = packageStates.valueAt(index);
@@ -8322,7 +6673,7 @@
         }
     }
 
-    void forEachInstalledPackage(@NonNull Consumer<AndroidPackage> action,
+    void forEachInstalledPackage(@NonNull Computer snapshot, @NonNull Consumer<AndroidPackage> action,
             @UserIdInt int userId) {
         Consumer<PackageStateInternal> actionWrapped = packageState -> {
             if (packageState.getPkg() != null
@@ -8330,7 +6681,7 @@
                 action.accept(packageState.getPkg());
             }
         };
-        forEachPackageState(mComputer.getPackageStates(), actionWrapped);
+        forEachPackageState(snapshot.getPackageStates(), actionWrapped);
     }
 
     boolean isHistoricalPackageUsageAvailable() {
@@ -8345,15 +6696,7 @@
         return mCompilerStats.getOrCreatePackageStats(pkgName);
     }
 
-    /**
-     * Returns true if the system or user is explicitly preventing an otherwise valid installer to
-     * complete an install. This includes checks like unknown sources and user restrictions.
-     */
-    public boolean isInstallDisabledForPackage(String packageName, int uid, int userId) {
-        return mComputer.isInstallDisabledForPackage(packageName, uid, userId);
-    }
-
-    private void grantImplicitAccess(@NonNull Computer snapshot, @UserIdInt int userId,
+    void grantImplicitAccess(@NonNull Computer snapshot, @UserIdInt int userId,
             Intent intent, @AppIdInt int recipientAppId, int visibleUid, boolean direct,
             boolean retainOnUpdate) {
         final AndroidPackage visiblePackage = snapshot.getPackage(visibleUid);
@@ -8384,8 +6727,8 @@
         }
     }
 
-    boolean canHaveOatDir(String packageName) {
-        final PackageStateInternal packageState = getPackageStateInternal(packageName);
+    boolean canHaveOatDir(@NonNull Computer snapshot, String packageName) {
+        final PackageStateInternal packageState = snapshot.getPackageStateInternal(packageName);
         if (packageState == null || packageState.getPkg() == null) {
             return false;
         }
@@ -8393,8 +6736,8 @@
                 packageState.getTransientState().isUpdatedSystemApp());
     }
 
-    long deleteOatArtifactsOfPackage(String packageName) {
-        PackageStateInternal packageState = getPackageStateInternal(packageName);
+    long deleteOatArtifactsOfPackage(@NonNull Computer snapshot, String packageName) {
+        PackageStateInternal packageState = snapshot.getPackageStateInternal(packageName);
         if (packageState == null || packageState.getPkg() == null) {
             return -1; // error code of deleteOptimizedFiles
         }
@@ -8402,13 +6745,9 @@
                 ArtUtils.createArtPackageInfo(packageState.getPkg(), packageState));
     }
 
-    @NonNull
-    Set<String> getUnusedPackages(long downgradeTimeThresholdMillis) {
-        return mComputer.getUnusedPackages(downgradeTimeThresholdMillis);
-    }
-
-    private List<String> getMimeGroupInternal(String packageName, String mimeGroup) {
-        final PackageStateInternal packageState = getPackageStateInternal(packageName);
+    List<String> getMimeGroupInternal(@NonNull Computer snapshot, String packageName,
+            String mimeGroup) {
+        final PackageStateInternal packageState = snapshot.getPackageStateInternal(packageName);
         if (packageState == null) {
             return Collections.emptyList();
         }
@@ -8473,16 +6812,16 @@
      * Returns the array containing per-uid timeout configuration.
      * This is derived from DeviceConfig flags.
      */
-    public @NonNull PerUidReadTimeouts[] getPerUidReadTimeouts() {
+    public @NonNull PerUidReadTimeouts[] getPerUidReadTimeouts(@NonNull Computer snapshot) {
         PerUidReadTimeouts[] result = mPerUidReadTimeoutsCache;
         if (result == null) {
-            result = parsePerUidReadTimeouts();
+            result = parsePerUidReadTimeouts(snapshot);
             mPerUidReadTimeoutsCache = result;
         }
         return result;
     }
 
-    private @NonNull PerUidReadTimeouts[] parsePerUidReadTimeouts() {
+    private @NonNull PerUidReadTimeouts[] parsePerUidReadTimeouts(@NonNull Computer snapshot) {
         final String defaultTimeouts = getDefaultTimeouts();
         final String knownDigestersList = getKnownDigestersList();
         final List<PerPackageReadTimeouts> perPackageReadTimeouts =
@@ -8496,7 +6835,8 @@
         final List<PerUidReadTimeouts> result = new ArrayList<>(perPackageReadTimeouts.size());
         for (int i = 0, size = perPackageReadTimeouts.size(); i < size; ++i) {
             final PerPackageReadTimeouts perPackage = perPackageReadTimeouts.get(i);
-            final PackageStateInternal ps = getPackageStateInternal(perPackage.packageName);
+            final PackageStateInternal ps =
+                    snapshot.getPackageStateInternal(perPackage.packageName);
             if (ps == null) {
                 if (DEBUG_PER_UID_READ_TIMEOUTS) {
                     Slog.i(TAG, "PerUidReadTimeouts: package not found = "
@@ -8546,7 +6886,7 @@
         return result.toArray(new PerUidReadTimeouts[result.size()]);
     }
 
-    private void setKeepUninstalledPackagesInternal(List<String> packageList) {
+    void setKeepUninstalledPackagesInternal(@NonNull Computer snapshot, List<String> packageList) {
         Preconditions.checkNotNull(packageList);
         synchronized (mKeepUninstalledPackages) {
             List<String> toRemove = new ArrayList<>(mKeepUninstalledPackages);
@@ -8556,7 +6896,7 @@
             mKeepUninstalledPackages.addAll(packageList);
 
             for (int i = 0; i < toRemove.size(); i++) {
-                deletePackageIfUnused(toRemove.get(i));
+                deletePackageIfUnused(snapshot, toRemove.get(i));
             }
         }
     }
@@ -8603,43 +6943,44 @@
         mInstrumentation.put(name, instrumentation);
     }
 
-    String[] getKnownPackageNamesInternal(int knownPackage, int userId) {
+    String[] getKnownPackageNamesInternal(@NonNull Computer snapshot, int knownPackage,
+            int userId) {
         switch (knownPackage) {
             case PackageManagerInternal.PACKAGE_BROWSER:
                 return new String[] { mDefaultAppProvider.getDefaultBrowser(userId) };
             case PackageManagerInternal.PACKAGE_INSTALLER:
-                return mComputer.filterOnlySystemPackages(mRequiredInstallerPackage);
+                return snapshot.filterOnlySystemPackages(mRequiredInstallerPackage);
             case PackageManagerInternal.PACKAGE_UNINSTALLER:
-                return mComputer.filterOnlySystemPackages(mRequiredUninstallerPackage);
+                return snapshot.filterOnlySystemPackages(mRequiredUninstallerPackage);
             case PackageManagerInternal.PACKAGE_SETUP_WIZARD:
-                return mComputer.filterOnlySystemPackages(mSetupWizardPackage);
+                return snapshot.filterOnlySystemPackages(mSetupWizardPackage);
             case PackageManagerInternal.PACKAGE_SYSTEM:
                 return new String[]{"android"};
             case PackageManagerInternal.PACKAGE_VERIFIER:
-                return mComputer.filterOnlySystemPackages(mRequiredVerifierPackage);
+                return snapshot.filterOnlySystemPackages(mRequiredVerifierPackage);
             case PackageManagerInternal.PACKAGE_SYSTEM_TEXT_CLASSIFIER:
-                return mComputer.filterOnlySystemPackages(
+                return snapshot.filterOnlySystemPackages(
                         mDefaultTextClassifierPackage, mSystemTextClassifierPackageName);
             case PackageManagerInternal.PACKAGE_PERMISSION_CONTROLLER:
-                return mComputer.filterOnlySystemPackages(mRequiredPermissionControllerPackage);
+                return snapshot.filterOnlySystemPackages(mRequiredPermissionControllerPackage);
             case PackageManagerInternal.PACKAGE_CONFIGURATOR:
-                return mComputer.filterOnlySystemPackages(mConfiguratorPackage);
+                return snapshot.filterOnlySystemPackages(mConfiguratorPackage);
             case PackageManagerInternal.PACKAGE_INCIDENT_REPORT_APPROVER:
-                return mComputer.filterOnlySystemPackages(mIncidentReportApproverPackage);
+                return snapshot.filterOnlySystemPackages(mIncidentReportApproverPackage);
             case PackageManagerInternal.PACKAGE_AMBIENT_CONTEXT_DETECTION:
-                return mComputer.filterOnlySystemPackages(mAmbientContextDetectionPackage);
+                return snapshot.filterOnlySystemPackages(mAmbientContextDetectionPackage);
             case PackageManagerInternal.PACKAGE_APP_PREDICTOR:
-                return mComputer.filterOnlySystemPackages(mAppPredictionServicePackage);
+                return snapshot.filterOnlySystemPackages(mAppPredictionServicePackage);
             case PackageManagerInternal.PACKAGE_COMPANION:
-                return mComputer.filterOnlySystemPackages(COMPANION_PACKAGE_NAME);
+                return snapshot.filterOnlySystemPackages(COMPANION_PACKAGE_NAME);
             case PackageManagerInternal.PACKAGE_RETAIL_DEMO:
                 return TextUtils.isEmpty(mRetailDemoPackage)
                         ? ArrayUtils.emptyArray(String.class)
                         : new String[] {mRetailDemoPackage};
             case PackageManagerInternal.PACKAGE_OVERLAY_CONFIG_SIGNATURE:
-                return mComputer.filterOnlySystemPackages(getOverlayConfigSignaturePackageName());
+                return snapshot.filterOnlySystemPackages(mOverlayConfigSignaturePackage);
             case PackageManagerInternal.PACKAGE_RECENTS:
-                return mComputer.filterOnlySystemPackages(mRecentsPackage);
+                return snapshot.filterOnlySystemPackages(mRecentsPackage);
             default:
                 return ArrayUtils.emptyArray(String.class);
         }
@@ -8659,10 +7000,6 @@
         mDefaultAppProvider.setDefaultBrowser(packageName, async, userId);
     }
 
-    ResolveInfo getInstantAppInstallerInfo() {
-        return mInstantAppInstallerInfo;
-    }
-
     PackageUsage getPackageUsage() {
         return mPackageUsage;
     }
@@ -8754,10 +7091,6 @@
         }
     }
 
-    ResolveInfo getResolveInfo() {
-        return mResolveInfo;
-    }
-
     ApplicationInfo getCoreAndroidApplication() {
         return mAndroidApplication;
     }
diff --git a/services/core/java/com/android/server/pm/PackageManagerServiceUtils.java b/services/core/java/com/android/server/pm/PackageManagerServiceUtils.java
index 8d3fbf7..2a1a990 100644
--- a/services/core/java/com/android/server/pm/PackageManagerServiceUtils.java
+++ b/services/core/java/com/android/server/pm/PackageManagerServiceUtils.java
@@ -850,6 +850,8 @@
         ret.recommendedInstallLocation = recommendedInstallLocation;
         ret.multiArch = pkg.isMultiArch();
         ret.debuggable = pkg.isDebuggable();
+        ret.isSdkLibrary = pkg.isIsSdkLibrary();
+
         return ret;
     }
 
diff --git a/services/core/java/com/android/server/pm/PackageManagerShellCommand.java b/services/core/java/com/android/server/pm/PackageManagerShellCommand.java
index b92f51b..15753cd2 100644
--- a/services/core/java/com/android/server/pm/PackageManagerShellCommand.java
+++ b/services/core/java/com/android/server/pm/PackageManagerShellCommand.java
@@ -131,6 +131,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import java.util.Set;
 import java.util.WeakHashMap;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CountDownLatch;
@@ -144,6 +145,10 @@
     private final static String ART_PROFILE_SNAPSHOT_DEBUG_LOCATION = "/data/misc/profman/";
     private static final int DEFAULT_STAGED_READY_TIMEOUT_MS = 60 * 1000;
     private static final String TAG = "PackageManagerShellCommand";
+    private static final Set<String> UNSUPPORTED_INSTALL_CMD_OPTS = Set.of(
+            "--multi-package"
+    );
+    private static final Set<String> UNSUPPORTED_SESSION_CREATE_OPTS = Collections.emptySet();
 
     final IPackageManager mInterface;
     final LegacyPermissionManagerInternal mLegacyPermissionManager;
@@ -1330,7 +1335,7 @@
     }
 
     private int runStreamingInstall() throws RemoteException {
-        final InstallParams params = makeInstallParams();
+        final InstallParams params = makeInstallParams(UNSUPPORTED_INSTALL_CMD_OPTS);
         if (params.sessionParams.dataLoaderParams == null) {
             params.sessionParams.setDataLoaderParams(
                     PackageManagerShellCommandDataLoader.getStreamingDataLoaderParams(this));
@@ -1339,7 +1344,7 @@
     }
 
     private int runIncrementalInstall() throws RemoteException {
-        final InstallParams params = makeInstallParams();
+        final InstallParams params = makeInstallParams(UNSUPPORTED_INSTALL_CMD_OPTS);
         if (params.sessionParams.dataLoaderParams == null) {
             params.sessionParams.setDataLoaderParams(
                     PackageManagerShellCommandDataLoader.getIncrementalDataLoaderParams(this));
@@ -1348,7 +1353,7 @@
     }
 
     private int runInstall() throws RemoteException {
-        return doRunInstall(makeInstallParams());
+        return doRunInstall(makeInstallParams(UNSUPPORTED_INSTALL_CMD_OPTS));
     }
 
     private int doRunInstall(final InstallParams params) throws RemoteException {
@@ -1500,7 +1505,7 @@
 
     private int runInstallCreate() throws RemoteException {
         final PrintWriter pw = getOutPrintWriter();
-        final InstallParams installParams = makeInstallParams();
+        final InstallParams installParams = makeInstallParams(UNSUPPORTED_SESSION_CREATE_OPTS);
         final int sessionId = doCreateSession(installParams.sessionParams,
                 installParams.installerPackageName, installParams.userId);
 
@@ -2535,8 +2540,10 @@
             privAppPermissions = SystemConfig.getInstance()
                     .getSystemExtPrivAppPermissions(pkg);
         } else if (isApexApp(pkg)) {
+            final String apexName = ApexManager.getInstance().getApexModuleNameForPackageName(
+                    getApexPackageNameContainingPackage(pkg));
             privAppPermissions = SystemConfig.getInstance()
-                    .getApexPrivAppPermissions(getApexPackageNameContainingPackage(pkg), pkg);
+                    .getApexPrivAppPermissions(apexName, pkg);
         } else {
             privAppPermissions = SystemConfig.getInstance().getPrivAppPermissions(pkg);
         }
@@ -2562,8 +2569,10 @@
             privAppPermissions = SystemConfig.getInstance()
                     .getSystemExtPrivAppDenyPermissions(pkg);
         } else if (isApexApp(pkg)) {
+            final String apexName = ApexManager.getInstance().getApexModuleNameForPackageName(
+                    getApexPackageNameContainingPackage(pkg));
             privAppPermissions = SystemConfig.getInstance()
-                    .getApexPrivAppDenyPermissions(getApexPackageNameContainingPackage(pkg), pkg);
+                    .getApexPrivAppDenyPermissions(apexName, pkg);
         } else {
             privAppPermissions = SystemConfig.getInstance().getPrivAppDenyPermissions(pkg);
         }
@@ -2896,7 +2905,7 @@
         long stagedReadyTimeoutMs = DEFAULT_STAGED_READY_TIMEOUT_MS;
     }
 
-    private InstallParams makeInstallParams() {
+    private InstallParams makeInstallParams(Set<String> unsupportedOptions) {
         final SessionParams sessionParams = new SessionParams(SessionParams.MODE_FULL_INSTALL);
         final InstallParams params = new InstallParams();
 
@@ -2910,6 +2919,9 @@
         boolean replaceExisting = true;
         boolean forceNonStaged = false;
         while ((opt = getNextOption()) != null) {
+            if (unsupportedOptions.contains(opt)) {
+                throw new IllegalArgumentException("Unsupported option " + opt);
+            }
             switch (opt) {
                 case "-r": // ignore
                     break;
@@ -3817,7 +3829,7 @@
         pw.println("       [--user USER_ID] INTENT");
         pw.println("    Prints all broadcast receivers that can handle the given INTENT.");
         pw.println("");
-        pw.println("  install [-rtfdgw] [-i PACKAGE] [--user USER_ID|all|current]");
+        pw.println("  install [-rtfdg] [-i PACKAGE] [--user USER_ID|all|current]");
         pw.println("       [-p INHERIT_PACKAGE] [--install-location 0/1/2]");
         pw.println("       [--install-reason 0/1/2/3/4] [--originating-uri URI]");
         pw.println("       [--referrer URI] [--abi ABI_NAME] [--force-sdk]");
diff --git a/services/core/java/com/android/server/pm/PackageRemovedInfo.java b/services/core/java/com/android/server/pm/PackageRemovedInfo.java
index fdad833..28ad4b6 100644
--- a/services/core/java/com/android/server/pm/PackageRemovedInfo.java
+++ b/services/core/java/com/android/server/pm/PackageRemovedInfo.java
@@ -38,8 +38,6 @@
     String mInstallerPackageName;
     int mUid = -1;
     int mRemovedAppId = -1;
-    // If not -1, the app is going through an appId change
-    int mNewAppId = -1;
     int[] mOrigUsers;
     int[] mRemovedUsers = null;
     int[] mBroadcastUsers = null;
@@ -67,22 +65,16 @@
         sendPackageRemovedBroadcastInternal(killApp, removedBySystem);
     }
 
-    void sendSystemPackageUpdatedBroadcasts(int newAppId) {
+    void sendSystemPackageUpdatedBroadcasts() {
         if (mIsRemovedPackageSystemUpdate) {
-            sendSystemPackageUpdatedBroadcastsInternal(newAppId);
+            sendSystemPackageUpdatedBroadcastsInternal();
         }
     }
 
-    private void sendSystemPackageUpdatedBroadcastsInternal(int newAppId) {
+    private void sendSystemPackageUpdatedBroadcastsInternal() {
         Bundle extras = new Bundle(2);
-        extras.putInt(Intent.EXTRA_UID, newAppId);
-        // When appId changes, do not set the replacing extra
-        if (mNewAppId >= 0) {
-            extras.putBoolean(Intent.EXTRA_UID_CHANGING, true);
-            extras.putInt(Intent.EXTRA_PREVIOUS_UID, mRemovedAppId >= 0 ? mRemovedAppId : mUid);
-        } else {
-            extras.putBoolean(Intent.EXTRA_REPLACING, true);
-        }
+        extras.putInt(Intent.EXTRA_UID, mRemovedAppId >= 0 ? mRemovedAppId : mUid);
+        extras.putBoolean(Intent.EXTRA_REPLACING, true);
         mPackageSender.sendPackageBroadcast(Intent.ACTION_PACKAGE_ADDED, mRemovedPackage, extras,
                 0, null /*targetPackage*/, null, null, null, mBroadcastAllowList, null);
         if (mInstallerPackageName != null) {
@@ -90,17 +82,13 @@
                     mRemovedPackage, extras, 0 /*flags*/,
                     mInstallerPackageName, null, null, null, null /* broadcastAllowList */,
                     null);
+            mPackageSender.sendPackageBroadcast(Intent.ACTION_PACKAGE_REPLACED,
+                    mRemovedPackage, extras, 0 /*flags*/,
+                    mInstallerPackageName, null, null, null, null /* broadcastAllowList */,
+                    null);
         }
-        if (mNewAppId < 0) {
-            mPackageSender.sendPackageBroadcast(Intent.ACTION_PACKAGE_REPLACED, mRemovedPackage,
-                    extras, 0, null /*targetPackage*/, null, null, null, mBroadcastAllowList, null);
-            if (mInstallerPackageName != null) {
-                mPackageSender.sendPackageBroadcast(Intent.ACTION_PACKAGE_REPLACED,
-                        mRemovedPackage, extras, 0 /*flags*/,
-                        mInstallerPackageName, null, null, null, null /* broadcastAllowList */,
-                        null);
-            }
-        }
+        mPackageSender.sendPackageBroadcast(Intent.ACTION_PACKAGE_REPLACED, mRemovedPackage,
+                extras, 0, null /*targetPackage*/, null, null, null, mBroadcastAllowList, null);
         mPackageSender.sendPackageBroadcast(Intent.ACTION_MY_PACKAGE_REPLACED, null, null, 0,
                 mRemovedPackage, null, null, null, null /* broadcastAllowList */,
                 getTemporaryAppAllowlistBroadcastOptions(REASON_PACKAGE_REPLACED).toBundle());
@@ -134,15 +122,10 @@
         extras.putBoolean(Intent.EXTRA_DATA_REMOVED, mDataRemoved);
         extras.putBoolean(Intent.EXTRA_DONT_KILL_APP, !killApp);
         extras.putBoolean(Intent.EXTRA_USER_INITIATED, !removedBySystem);
-
-        // When appId changes, do not set the replacing extra
-        if (mNewAppId >= 0) {
-            extras.putBoolean(Intent.EXTRA_UID_CHANGING, true);
-            extras.putInt(Intent.EXTRA_NEW_UID, mNewAppId);
-        } else if (mIsUpdate || mIsRemovedPackageSystemUpdate) {
+        final boolean isReplace = mIsUpdate || mIsRemovedPackageSystemUpdate;
+        if (isReplace) {
             extras.putBoolean(Intent.EXTRA_REPLACING, true);
         }
-
         extras.putBoolean(Intent.EXTRA_REMOVED_FOR_ALL_USERS, mRemovedForAllUsers);
         if (mRemovedPackage != null) {
             mPackageSender.sendPackageBroadcast(Intent.ACTION_PACKAGE_REMOVED,
@@ -165,9 +148,9 @@
             }
         }
         if (mRemovedAppId >= 0) {
-            // If the package is not actually removed, some services need to know the
-            // package name affected.
-            if (mNewAppId >= 0 || mIsUpdate || mIsRemovedPackageSystemUpdate) {
+            // If a system app's updates are uninstalled the UID is not actually removed. Some
+            // services need to know the package name affected.
+            if (isReplace) {
                 extras.putString(Intent.EXTRA_PACKAGE_NAME, mRemovedPackage);
             }
 
diff --git a/services/core/java/com/android/server/pm/PackageSender.java b/services/core/java/com/android/server/pm/PackageSender.java
index d380098..656d596 100644
--- a/services/core/java/com/android/server/pm/PackageSender.java
+++ b/services/core/java/com/android/server/pm/PackageSender.java
@@ -16,6 +16,7 @@
 
 package com.android.server.pm;
 
+import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.content.IIntentReceiver;
 import android.os.Bundle;
@@ -30,9 +31,9 @@
             Bundle extras, int flags, String targetPkg,
             IIntentReceiver finishedReceiver, int[] userIds, int[] instantUserIds,
             @Nullable SparseArray<int[]> broadcastAllowList, @Nullable Bundle bOptions);
-    void sendPackageAddedForNewUsers(String packageName, boolean sendBootCompleted,
-            boolean includeStopped, int appId, int[] userIds, int[] instantUserIds,
-            int dataLoaderType);
+    void sendPackageAddedForNewUsers(@NonNull Computer snapshot, String packageName,
+            boolean sendBootCompleted, boolean includeStopped, int appId, int[] userIds,
+            int[] instantUserIds, int dataLoaderType);
     void notifyPackageAdded(String packageName, int uid);
     void notifyPackageChanged(String packageName, int uid);
     void notifyPackageRemoved(String packageName, int uid);
diff --git a/services/core/java/com/android/server/pm/PackageSessionVerifier.java b/services/core/java/com/android/server/pm/PackageSessionVerifier.java
index 6b57deb..2016fc3 100644
--- a/services/core/java/com/android/server/pm/PackageSessionVerifier.java
+++ b/services/core/java/com/android/server/pm/PackageSessionVerifier.java
@@ -24,7 +24,6 @@
 import android.content.Intent;
 import android.content.pm.IPackageInstallObserver2;
 import android.content.pm.PackageInfo;
-import android.content.pm.PackageInstaller.SessionInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.PackageManagerInternal;
 import android.content.pm.SigningDetails;
@@ -110,7 +109,7 @@
                 verifyAPK(session, callback);
             } catch (PackageManagerException e) {
                 String errorMessage = PackageManager.installStatusToString(e.error, e.getMessage());
-                session.setSessionFailed(SessionInfo.SESSION_VERIFICATION_FAILED, errorMessage);
+                session.setSessionFailed(e.error, errorMessage);
                 callback.onResult(e.error, e.getMessage());
             }
         });
@@ -137,7 +136,7 @@
                 }
                 if (returnCode != PackageManager.INSTALL_SUCCEEDED) {
                     String errorMessage = PackageManager.installStatusToString(returnCode, msg);
-                    session.setSessionFailed(SessionInfo.SESSION_VERIFICATION_FAILED, errorMessage);
+                    session.setSessionFailed(returnCode, errorMessage);
                     callback.onResult(returnCode, msg);
                 } else {
                     session.setSessionReady();
@@ -220,7 +219,7 @@
     }
 
     private void onVerificationFailure(StagingManager.StagedSession session, Callback callback,
-            @SessionInfo.SessionErrorCode int errorCode, String errorMessage) {
+            int errorCode, String errorMessage) {
         if (!ensureActiveApexSessionIsAborted(session)) {
             Slog.e(TAG, "Failed to abort apex session " + session.sessionId());
             // Safe to ignore active apex session abortion failure since session will be marked
@@ -312,7 +311,7 @@
             // Failed to get hold of StorageManager
             Slog.e(TAG, "Failed to get hold of StorageManager", e);
             throw new PackageManagerException(
-                    SessionInfo.SESSION_UNKNOWN_ERROR,
+                    PackageManager.INSTALL_FAILED_INTERNAL_ERROR,
                     "Failed to get hold of StorageManager");
         }
         // Proactively mark session as ready before calling apexd. Although this call order
@@ -350,7 +349,7 @@
         final ParseResult<SigningDetails> newResult = ApkSignatureVerifier.verify(
                 input.reset(), apexPath, minSignatureScheme);
         if (newResult.isError()) {
-            throw new PackageManagerException(SessionInfo.SESSION_VERIFICATION_FAILED,
+            throw new PackageManagerException(PackageManager.INSTALL_FAILED_VERIFICATION_FAILURE,
                     "Failed to parse APEX package " + apexPath + " : "
                             + newResult.getException(), newResult.getException());
         }
@@ -369,7 +368,7 @@
                 input.reset(), existingApexPkg.applicationInfo.sourceDir,
                 SigningDetails.SignatureSchemeVersion.JAR);
         if (existingResult.isError()) {
-            throw new PackageManagerException(SessionInfo.SESSION_VERIFICATION_FAILED,
+            throw new PackageManagerException(PackageManager.INSTALL_FAILED_VERIFICATION_FAILURE,
                     "Failed to parse APEX package " + existingApexPkg.applicationInfo.sourceDir
                             + " : " + existingResult.getException(), existingResult.getException());
         }
@@ -383,7 +382,7 @@
             return;
         }
 
-        throw new PackageManagerException(SessionInfo.SESSION_VERIFICATION_FAILED,
+        throw new PackageManagerException(PackageManager.INSTALL_FAILED_VERIFICATION_FAILURE,
                 "APK-container signature of APEX package " + packageName + " with version "
                         + newApexPkg.versionCodeMajor + " and path " + apexPath + " is not"
                         + " compatible with the one currently installed on device");
@@ -426,11 +425,12 @@
                 packageInfo = PackageInfoWithoutStateUtils.generate(parsedPackage, apexInfo, flags);
                 if (packageInfo == null) {
                     throw new PackageManagerException(
-                            SessionInfo.SESSION_VERIFICATION_FAILED,
+                            PackageManager.INSTALL_FAILED_VERIFICATION_FAILURE,
                             "Unable to generate package info: " + apexInfo.modulePath);
                 }
             } catch (PackageManagerException e) {
-                throw new PackageManagerException(SessionInfo.SESSION_VERIFICATION_FAILED,
+                throw new PackageManagerException(
+                        PackageManager.INSTALL_FAILED_VERIFICATION_FAILURE,
                         "Failed to parse APEX package " + apexInfo.modulePath + " : " + e, e);
             }
             result.add(packageInfo);
@@ -452,7 +452,7 @@
             }
         }
         throw new PackageManagerException(
-                SessionInfo.SESSION_VERIFICATION_FAILED,
+                PackageManager.INSTALL_FAILED_VERIFICATION_FAILURE,
                 "Could not find rollback id for commit session: " + sessionId);
     }
 
@@ -560,7 +560,7 @@
         try {
             checkActiveSessions(InstallLocationUtils.getStorageManager().supportsCheckpoint());
         } catch (RemoteException e) {
-            throw new PackageManagerException(SessionInfo.SESSION_VERIFICATION_FAILED,
+            throw new PackageManagerException(PackageManager.INSTALL_FAILED_INTERNAL_ERROR,
                     "Can't query fs-checkpoint status : " + e);
         }
     }
@@ -576,7 +576,7 @@
         }
         if (!supportsCheckpoint && activeSessions > 1) {
             throw new PackageManagerException(
-                    SessionInfo.SESSION_VERIFICATION_FAILED,
+                    PackageManager.INSTALL_FAILED_OTHER_STAGED_SESSION_IN_PROGRESS,
                     "Cannot stage multiple sessions without checkpoint support");
         }
     }
@@ -607,13 +607,13 @@
                     // will be deleted.
                 }
                 stagedSession.setSessionFailed(
-                        SessionInfo.SESSION_CONFLICT,
+                        PackageManager.INSTALL_FAILED_OTHER_STAGED_SESSION_IN_PROGRESS,
                         "Session was failed by rollback session: " + session.sessionId());
                 Slog.i(TAG, "Session " + stagedSession.sessionId() + " is marked failed due to "
                         + "rollback session: " + session.sessionId());
             } else if (!isRollback(session) && isRollback(stagedSession)) {
                 throw new PackageManagerException(
-                        SessionInfo.SESSION_CONFLICT,
+                        PackageManager.INSTALL_FAILED_OTHER_STAGED_SESSION_IN_PROGRESS,
                         "Session was failed by rollback session: " + stagedSession.sessionId());
 
             }
@@ -636,7 +636,7 @@
         final String packageName = child.getPackageName();
         if (packageName == null) {
             throw new PackageManagerException(
-                    SessionInfo.SESSION_VERIFICATION_FAILED,
+                    PackageManager.INSTALL_FAILED_VERIFICATION_FAILURE,
                     "Cannot stage session " + child.sessionId() + " with package name null");
         }
         for (StagingManager.StagedSession stagedSession : mStagedSessions) {
@@ -648,14 +648,14 @@
                 if (stagedSession.getCommittedMillis() < parent.getCommittedMillis()) {
                     // Fail the session committed later when there are overlapping packages
                     throw new PackageManagerException(
-                            SessionInfo.SESSION_VERIFICATION_FAILED,
+                            PackageManager.INSTALL_FAILED_OTHER_STAGED_SESSION_IN_PROGRESS,
                             "Package: " + packageName + " in session: "
                                     + child.sessionId()
                                     + " has been staged already by session: "
                                     + stagedSession.sessionId());
                 } else {
                     stagedSession.setSessionFailed(
-                            SessionInfo.SESSION_VERIFICATION_FAILED,
+                            PackageManager.INSTALL_FAILED_OTHER_STAGED_SESSION_IN_PROGRESS,
                             "Package: " + packageName + " in session: "
                                     + stagedSession.sessionId()
                                     + " has been staged already by session: "
diff --git a/services/core/java/com/android/server/pm/PreferredActivityHelper.java b/services/core/java/com/android/server/pm/PreferredActivityHelper.java
index 7253ae4..9befd6e 100644
--- a/services/core/java/com/android/server/pm/PreferredActivityHelper.java
+++ b/services/core/java/com/android/server/pm/PreferredActivityHelper.java
@@ -25,6 +25,7 @@
 import static com.android.server.pm.PackageManagerService.TAG;
 
 import android.annotation.NonNull;
+import android.annotation.UserIdInt;
 import android.content.ComponentName;
 import android.content.Intent;
 import android.content.IntentFilter;
@@ -74,17 +75,18 @@
         mPm = pm;
     }
 
-    private ResolveInfo findPreferredActivityNotLocked(Intent intent, String resolvedType,
-            @PackageManager.ResolveInfoFlagsBits long flags, List<ResolveInfo> query,
-            boolean always, boolean removeMatches, boolean debug, int userId) {
-        return findPreferredActivityNotLocked(
-                intent, resolvedType, flags, query, always, removeMatches, debug, userId,
+    private ResolveInfo findPreferredActivityNotLocked(@NonNull Computer snapshot, Intent intent,
+            String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags,
+            List<ResolveInfo> query, boolean always, boolean removeMatches, boolean debug,
+            @UserIdInt int userId) {
+        return findPreferredActivityNotLocked(snapshot, intent, resolvedType, flags, query, always,
+                removeMatches, debug, userId,
                 UserHandle.getAppId(Binder.getCallingUid()) >= Process.FIRST_APPLICATION_UID);
     }
 
     // TODO: handle preferred activities missing while user has amnesia
     /** <b>must not hold {@link PackageManagerService.mLock}</b> */
-    public ResolveInfo findPreferredActivityNotLocked(
+    public ResolveInfo findPreferredActivityNotLocked(@NonNull Computer snapshot,
             Intent intent, String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags,
             List<ResolveInfo> query, boolean always, boolean removeMatches, boolean debug,
             int userId, boolean queryMayBeFiltered) {
@@ -95,7 +97,7 @@
         if (!mPm.mUserManager.exists(userId)) return null;
 
         PackageManagerService.FindPreferredActivityBodyResult body =
-                mPm.findPreferredActivityInternal(
+                snapshot.findPreferredActivityInternal(
                 intent, resolvedType, flags, query, always,
                 removeMatches, debug, userId, queryMayBeFiltered);
         if (body.mChanged) {
@@ -117,7 +119,7 @@
             mPm.clearPackagePreferredActivitiesLPw(packageName, changedUsers, userId);
         }
         if (changedUsers.size() > 0) {
-            updateDefaultHomeNotLocked(changedUsers);
+            updateDefaultHomeNotLocked(mPm.snapshotComputer(), changedUsers);
             mPm.postPreferredActivityChangedBroadcast(userId);
             mPm.scheduleWritePackageRestrictions(userId);
         }
@@ -128,7 +130,7 @@
      *
      * @return Whether the ACTION_PREFERRED_ACTIVITY_CHANGED broadcast has been scheduled.
      */
-    public boolean updateDefaultHomeNotLocked(int userId) {
+    public boolean updateDefaultHomeNotLocked(@NonNull Computer snapshot, @UserIdInt int userId) {
         if (Thread.holdsLock(mPm.mLock)) {
             Slog.wtf(TAG, "Calling thread " + Thread.currentThread().getName()
                     + " is holding mLock", new Throwable());
@@ -139,10 +141,10 @@
             // before that.
             return false;
         }
-        final Intent intent = mPm.getHomeIntent();
-        final List<ResolveInfo> resolveInfos = mPm.snapshotComputer().queryIntentActivitiesInternal(
+        final Intent intent = snapshot.getHomeIntent();
+        final List<ResolveInfo> resolveInfos = snapshot.queryIntentActivitiesInternal(
                 intent, null, MATCH_DIRECT_BOOT_AWARE | MATCH_DIRECT_BOOT_UNAWARE, userId);
-        final ResolveInfo preferredResolveInfo = findPreferredActivityNotLocked(
+        final ResolveInfo preferredResolveInfo = findPreferredActivityNotLocked(snapshot,
                 intent, null, 0, resolveInfos, true, false, false, userId);
         final String packageName = preferredResolveInfo != null
                 && preferredResolveInfo.activityInfo != null
@@ -151,8 +153,7 @@
         if (TextUtils.equals(currentPackageName, packageName)) {
             return false;
         }
-        final String[] callingPackages = mPm.mIPackageManager
-                .getPackagesForUid(Binder.getCallingUid());
+        final String[] callingPackages = snapshot.getPackagesForUid(Binder.getCallingUid());
         if (callingPackages != null && ArrayUtils.contains(callingPackages,
                 mPm.mRequiredPermissionControllerPackage)) {
             // PermissionController manages default home directly.
@@ -174,23 +175,21 @@
     /**
      * Variant that takes a {@link WatchedIntentFilter}
      */
-    public void addPreferredActivity(WatchedIntentFilter filter, int match,
-            ComponentName[] set, ComponentName activity, boolean always, int userId,
+    public void addPreferredActivity(@NonNull Computer snapshot, WatchedIntentFilter filter,
+            int match, ComponentName[] set, ComponentName activity, boolean always, int userId,
             String opname, boolean removeExisting) {
         // writer
         int callingUid = Binder.getCallingUid();
-        mPm.enforceCrossUserPermission(callingUid, userId, true /* requireFullPermission */,
+        snapshot.enforceCrossUserPermission(callingUid, userId, true /* requireFullPermission */,
                 false /* checkShell */, "add preferred activity");
         if (mPm.mContext.checkCallingOrSelfPermission(
                 android.Manifest.permission.SET_PREFERRED_APPLICATIONS)
                 != PackageManager.PERMISSION_GRANTED) {
-            synchronized (mPm.mLock) {
-                if (mPm.getUidTargetSdkVersion(callingUid)
-                        < Build.VERSION_CODES.FROYO) {
-                    Slog.w(TAG, "Ignoring addPreferredActivity() from uid "
-                            + callingUid);
-                    return;
-                }
+            if (snapshot.getUidTargetSdkVersion(callingUid)
+                    < Build.VERSION_CODES.FROYO) {
+                Slog.w(TAG, "Ignoring addPreferredActivity() from uid "
+                        + callingUid);
+                return;
             }
             mPm.mContext.enforceCallingOrSelfPermission(
                     android.Manifest.permission.SET_PREFERRED_APPLICATIONS, null);
@@ -214,7 +213,8 @@
                     new PreferredActivity(filter, match, set, activity, always));
             mPm.scheduleWritePackageRestrictions(userId);
         }
-        if (!(isHomeFilter(filter) && updateDefaultHomeNotLocked(userId))) {
+        // Re-snapshot after mLock
+        if (!(isHomeFilter(filter) && updateDefaultHomeNotLocked(mPm.snapshotComputer(), userId))) {
             mPm.postPreferredActivityChangedBroadcast(userId);
         }
     }
@@ -222,8 +222,8 @@
     /**
      * Variant that takes a {@link WatchedIntentFilter}
      */
-    public void replacePreferredActivity(WatchedIntentFilter filter, int match,
-            ComponentName[] set, ComponentName activity, int userId) {
+    public void replacePreferredActivity(@NonNull Computer snapshot, WatchedIntentFilter filter,
+            int match, ComponentName[] set, ComponentName activity, int userId) {
         if (filter.countActions() != 1) {
             throw new IllegalArgumentException(
                     "replacePreferredActivity expects filter to have only 1 action.");
@@ -238,13 +238,14 @@
         }
 
         final int callingUid = Binder.getCallingUid();
-        mPm.enforceCrossUserPermission(callingUid, userId, true /* requireFullPermission */,
+        snapshot.enforceCrossUserPermission(callingUid, userId, true /* requireFullPermission */,
                 false /* checkShell */, "replace preferred activity");
         if (mPm.mContext.checkCallingOrSelfPermission(
                 android.Manifest.permission.SET_PREFERRED_APPLICATIONS)
                 != PackageManager.PERMISSION_GRANTED) {
             synchronized (mPm.mLock) {
-                if (mPm.getUidTargetSdkVersion(callingUid)
+                // TODO: Remove lock?
+                if (mPm.snapshotComputer().getUidTargetSdkVersion(callingUid)
                         < Build.VERSION_CODES.FROYO) {
                     Slog.w(TAG, "Ignoring replacePreferredActivity() from uid "
                             + Binder.getCallingUid());
@@ -296,21 +297,23 @@
                 }
             }
         }
-        addPreferredActivity(filter, match, set, activity, true, userId,
+
+        // Retake a snapshot after editing with lock held
+        addPreferredActivity(mPm.snapshotComputer(), filter, match, set, activity, true, userId,
                 "Replacing preferred", false);
     }
 
-    public void clearPackagePreferredActivities(String packageName) {
+    public void clearPackagePreferredActivities(@NonNull Computer snapshot, String packageName) {
         final int callingUid = Binder.getCallingUid();
-        if (mPm.getInstantAppPackageName(callingUid) != null) {
+        if (snapshot.getInstantAppPackageName(callingUid) != null) {
             return;
         }
-        final PackageStateInternal packageState = mPm.getPackageStateInternal(packageName);
-        if (packageState == null || !mPm.isCallerSameApp(packageName, callingUid)) {
+        final PackageStateInternal packageState = snapshot.getPackageStateInternal(packageName);
+        if (packageState == null || !snapshot.isCallerSameApp(packageName, callingUid)) {
             if (mPm.mContext.checkCallingOrSelfPermission(
                     android.Manifest.permission.SET_PREFERRED_APPLICATIONS)
                     != PackageManager.PERMISSION_GRANTED) {
-                if (mPm.getUidTargetSdkVersion(callingUid)
+                if (snapshot.getUidTargetSdkVersion(callingUid)
                         < Build.VERSION_CODES.FROYO) {
                     Slog.w(TAG, "Ignoring clearPackagePreferredActivities() from uid "
                             + callingUid);
@@ -320,7 +323,7 @@
                         android.Manifest.permission.SET_PREFERRED_APPLICATIONS, null);
             }
         }
-        if (packageState != null && mPm.shouldFilterApplication(packageState, callingUid,
+        if (packageState != null && snapshot.shouldFilterApplication(packageState, callingUid,
                 UserHandle.getUserId(callingUid))) {
             return;
         }
@@ -329,23 +332,23 @@
     }
 
     /** <b>must not hold {@link #PackageManagerService.mLock}</b> */
-    void updateDefaultHomeNotLocked(SparseBooleanArray userIds) {
+    void updateDefaultHomeNotLocked(@NonNull Computer snapshot, SparseBooleanArray userIds) {
         if (Thread.holdsLock(mPm.mLock)) {
             Slog.wtf(TAG, "Calling thread " + Thread.currentThread().getName()
                     + " is holding mLock", new Throwable());
         }
         for (int i = userIds.size() - 1; i >= 0; --i) {
             final int userId = userIds.keyAt(i);
-            updateDefaultHomeNotLocked(userId);
+            updateDefaultHomeNotLocked(snapshot, userId);
         }
     }
 
-    public void setHomeActivity(ComponentName comp, int userId) {
-        if (mPm.getInstantAppPackageName(Binder.getCallingUid()) != null) {
+    public void setHomeActivity(@NonNull Computer snapshot, ComponentName comp, int userId) {
+        if (snapshot.getInstantAppPackageName(Binder.getCallingUid()) != null) {
             return;
         }
         ArrayList<ResolveInfo> homeActivities = new ArrayList<>();
-        mPm.getHomeActivitiesAsUser(homeActivities, userId);
+        snapshot.getHomeActivitiesAsUser(homeActivities, userId);
 
         boolean found = false;
 
@@ -364,7 +367,7 @@
             throw new IllegalArgumentException("Component " + comp + " cannot be home on user "
                     + userId);
         }
-        replacePreferredActivity(getHomeFilter(), IntentFilter.MATCH_CATEGORY_EMPTY,
+        replacePreferredActivity(snapshot, getHomeFilter(), IntentFilter.MATCH_CATEGORY_EMPTY,
                 set, comp, userId);
     }
 
@@ -401,7 +404,7 @@
             mPm.scheduleWritePackageRestrictions(userId);
         }
         if (isHomeFilter(filter)) {
-            updateDefaultHomeNotLocked(userId);
+            updateDefaultHomeNotLocked(mPm.snapshotComputer(), userId);
         }
         mPm.postPreferredActivityChangedBroadcast(userId);
     }
@@ -417,7 +420,7 @@
             changed = mPm.mSettings.clearPackagePersistentPreferredActivities(packageName, userId);
         }
         if (changed) {
-            updateDefaultHomeNotLocked(userId);
+            updateDefaultHomeNotLocked(mPm.snapshotComputer(), userId);
             mPm.postPreferredActivityChangedBroadcast(userId);
             mPm.scheduleWritePackageRestrictions(userId);
         }
@@ -506,7 +509,7 @@
                         synchronized (mPm.mLock) {
                             mPm.mSettings.readPreferredActivitiesLPw(readParser, readUserId);
                         }
-                        updateDefaultHomeNotLocked(readUserId);
+                        updateDefaultHomeNotLocked(mPm.snapshotComputer(), readUserId);
                     });
         } catch (Exception e) {
             if (DEBUG_BACKUP) {
@@ -598,7 +601,7 @@
                     mPm.mPermissionManager.resetRuntimePermissions(pkg, userId);
                 }
             }
-            updateDefaultHomeNotLocked(userId);
+            updateDefaultHomeNotLocked(mPm.snapshotComputer(), userId);
             resetNetworkPolicies(userId);
             mPm.scheduleWritePackageRestrictions(userId);
         } finally {
@@ -610,12 +613,11 @@
         mPm.mInjector.getLocalService(NetworkPolicyManagerInternal.class).resetUserState(userId);
     }
 
-    // TODO: This method should not touch the Computer directly
-    public int getPreferredActivities(List<IntentFilter> outFilters,
-            List<ComponentName> outActivities, String packageName, Computer computer) {
+    public int getPreferredActivities(@NonNull Computer snapshot, List<IntentFilter> outFilters,
+            List<ComponentName> outActivities, String packageName) {
         List<WatchedIntentFilter> temp =
                 WatchedIntentFilter.toWatchedIntentFilterList(outFilters);
-        int result = getPreferredActivitiesInternal(temp, outActivities, packageName, computer);
+        int result = getPreferredActivitiesInternal(snapshot, temp, outActivities, packageName);
         outFilters.clear();
         for (int i = 0; i < temp.size(); i++) {
             outFilters.add(temp.get(i).getIntentFilter());
@@ -626,16 +628,17 @@
     /**
      * Variant that takes a {@link WatchedIntentFilter}
      */
-    private int getPreferredActivitiesInternal(List<WatchedIntentFilter> outFilters,
-            List<ComponentName> outActivities, String packageName, Computer computer) {
+    private int getPreferredActivitiesInternal(@NonNull Computer snapshot,
+            List<WatchedIntentFilter> outFilters, List<ComponentName> outActivities,
+            String packageName) {
         final int callingUid = Binder.getCallingUid();
-        if (mPm.getInstantAppPackageName(callingUid) != null) {
+        if (snapshot.getInstantAppPackageName(callingUid) != null) {
             return 0;
         }
         int num = 0;
         final int userId = UserHandle.getCallingUserId();
 
-        PreferredIntentResolver pir = computer.getPreferredActivities(userId);
+        PreferredIntentResolver pir = snapshot.getPreferredActivities(userId);
         if (pir != null) {
             final Iterator<PreferredActivity> it = pir.filterIterator();
             while (it.hasNext()) {
@@ -643,8 +646,9 @@
                 final String prefPackageName = pa.mPref.mComponent.getPackageName();
                 if (packageName == null
                         || (prefPackageName.equals(packageName) && pa.mPref.mAlways)) {
-                    if (mPm.shouldFilterApplication(
-                            mPm.getPackageStateInternal(prefPackageName), callingUid, userId)) {
+                    if (snapshot.shouldFilterApplication(
+                            snapshot.getPackageStateInternal(prefPackageName), callingUid,
+                            userId)) {
                         continue;
                     }
                     if (outFilters != null) {
@@ -660,7 +664,8 @@
         return num;
     }
 
-    public ResolveInfo findPersistentPreferredActivity(Intent intent, int userId) {
+    public ResolveInfo findPersistentPreferredActivity(@NonNull Computer snapshot, Intent intent,
+            int userId) {
         if (!UserHandle.isSameApp(Binder.getCallingUid(), Process.SYSTEM_UID)) {
             throw new SecurityException(
                     "findPersistentPreferredActivity can only be run by the system");
@@ -671,24 +676,23 @@
         final int callingUid = Binder.getCallingUid();
         intent = PackageManagerServiceUtils.updateIntentForResolve(intent);
         final String resolvedType = intent.resolveTypeIfNeeded(mPm.mContext.getContentResolver());
-        final long flags = mPm.updateFlagsForResolve(
+        final long flags = snapshot.updateFlagsForResolve(
                 0, userId, callingUid, false /*includeInstantApps*/,
-                mPm.isImplicitImageCaptureIntentAndNotSetByDpcLocked(intent, userId, resolvedType,
+                snapshot.isImplicitImageCaptureIntentAndNotSetByDpc(intent, userId, resolvedType,
                         0));
-        final List<ResolveInfo> query = mPm.snapshotComputer().queryIntentActivitiesInternal(intent,
+        final List<ResolveInfo> query = snapshot.queryIntentActivitiesInternal(intent,
                 resolvedType, flags, userId);
-        synchronized (mPm.mLock) {
-            return mPm.findPersistentPreferredActivityLP(intent, resolvedType, flags, query, false,
-                    userId);
-        }
+        return snapshot.findPersistentPreferredActivity(intent, resolvedType, flags, query, false,
+                userId);
     }
 
     /**
      * Variant that takes a {@link WatchedIntentFilter}
      */
-    public void setLastChosenActivity(Intent intent, String resolvedType, int flags,
-            WatchedIntentFilter filter, int match, ComponentName activity) {
-        if (mPm.getInstantAppPackageName(Binder.getCallingUid()) != null) {
+    public void setLastChosenActivity(@NonNull Computer snapshot, Intent intent,
+            String resolvedType, int flags, WatchedIntentFilter filter, int match,
+            ComponentName activity) {
+        if (snapshot.getInstantAppPackageName(Binder.getCallingUid()) != null) {
             return;
         }
         final int userId = UserHandle.getCallingUserId();
@@ -702,25 +706,26 @@
             filter.dump(new PrintStreamPrinter(System.out), "    ");
         }
         intent.setComponent(null);
-        final List<ResolveInfo> query = mPm.snapshotComputer().queryIntentActivitiesInternal(intent,
+        final List<ResolveInfo> query = snapshot.queryIntentActivitiesInternal(intent,
                 resolvedType, flags, userId);
         // Find any earlier preferred or last chosen entries and nuke them
-        findPreferredActivityNotLocked(
-                intent, resolvedType, flags, query, false, true, false, userId);
+        findPreferredActivityNotLocked(snapshot, intent, resolvedType, flags, query, false, true,
+                false, userId);
         // Add the new activity as the last chosen for this filter
-        addPreferredActivity(filter, match, null, activity, false, userId,
+        addPreferredActivity(snapshot, filter, match, null, activity, false, userId,
                 "Setting last chosen", false);
     }
 
-    public ResolveInfo getLastChosenActivity(Intent intent, String resolvedType, int flags) {
-        if (mPm.getInstantAppPackageName(Binder.getCallingUid()) != null) {
+    public ResolveInfo getLastChosenActivity(@NonNull Computer snapshot, Intent intent,
+            String resolvedType, int flags) {
+        if (snapshot.getInstantAppPackageName(Binder.getCallingUid()) != null) {
             return null;
         }
         final int userId = UserHandle.getCallingUserId();
         if (DEBUG_PREFERRED) Log.v(TAG, "Querying last chosen activity for " + intent);
-        final List<ResolveInfo> query = mPm.snapshotComputer().queryIntentActivitiesInternal(intent,
+        final List<ResolveInfo> query = snapshot.queryIntentActivitiesInternal(intent,
                 resolvedType, flags, userId);
-        return findPreferredActivityNotLocked(
-                intent, resolvedType, flags, query, false, false, false, userId);
+        return findPreferredActivityNotLocked(snapshot, intent, resolvedType, flags, query, false,
+                false, false, userId);
     }
 }
diff --git a/services/core/java/com/android/server/pm/PreferredComponent.java b/services/core/java/com/android/server/pm/PreferredComponent.java
index 4ec042f..2a1ca2c 100644
--- a/services/core/java/com/android/server/pm/PreferredComponent.java
+++ b/services/core/java/com/android/server/pm/PreferredComponent.java
@@ -57,7 +57,6 @@
     private String mParseError;
 
     private final Callbacks mCallbacks;
-    private final String mSetupWizardPackageName;
 
     public interface Callbacks {
         public boolean onReadTag(String tagName, TypedXmlPullParser parser)
@@ -72,7 +71,6 @@
         mAlways = always;
         mShortComponent = component.flattenToShortString();
         mParseError = null;
-        mSetupWizardPackageName = null;
         if (set != null) {
             final int N = set.length;
             String[] myPackages = new String[N];
@@ -174,8 +172,6 @@
         mSetPackages = myPackages;
         mSetClasses = myClasses;
         mSetComponents = myComponents;
-        final PackageManagerInternal packageManagerInternal = LocalServices.getService(PackageManagerInternal.class);
-        mSetupWizardPackageName = packageManagerInternal.getSetupWizardPackageName();
     }
 
     public String getParseError() {
@@ -209,6 +205,7 @@
         final int NQ = query.size();
         final int NS = mSetPackages.length;
         final PackageManagerInternal pmi = LocalServices.getService(PackageManagerInternal.class);
+        String setupWizardPackageName = pmi.getSetupWizardPackageName();
         int numMatch = 0;
         for (int i=0; i<NQ; i++) {
             ResolveInfo ri = query.get(i);
@@ -217,7 +214,7 @@
 
             // ignore SetupWizard package's launcher capability because it is only existed
             // during SetupWizard is running
-            if (excludeSetupWizardPackage && ai.packageName.equals(mSetupWizardPackageName)) {
+            if (excludeSetupWizardPackage && ai.packageName.equals(setupWizardPackageName)) {
                 continue;
             }
 
@@ -307,6 +304,8 @@
         if (!excludeSetupWizardPackage && NS < NQ) {
             return false;
         }
+        final PackageManagerInternal pmi = LocalServices.getService(PackageManagerInternal.class);
+        String setupWizardPackageName = pmi.getSetupWizardPackageName();
         for (int i=0; i<NQ; i++) {
             ResolveInfo ri = query.get(i);
             ActivityInfo ai = ri.activityInfo;
@@ -314,7 +313,7 @@
 
             // ignore SetupWizard package's launcher capability because it is only existed
             // during SetupWizard is running
-            if (excludeSetupWizardPackage && ai.packageName.equals(mSetupWizardPackageName)) {
+            if (excludeSetupWizardPackage && ai.packageName.equals(setupWizardPackageName)) {
                 continue;
             }
 
diff --git a/services/core/java/com/android/server/pm/RemovePackageHelper.java b/services/core/java/com/android/server/pm/RemovePackageHelper.java
index 88df843..b181cdd 100644
--- a/services/core/java/com/android/server/pm/RemovePackageHelper.java
+++ b/services/core/java/com/android/server/pm/RemovePackageHelper.java
@@ -118,7 +118,8 @@
 
     public void removePackageLI(AndroidPackage pkg, boolean chatty) {
         // Remove the parent package setting
-        PackageStateInternal ps = mPm.getPackageStateInternal(pkg.getPackageName());
+        PackageStateInternal ps = mPm.snapshotComputer()
+                .getPackageStateInternal(pkg.getPackageName());
         if (ps != null) {
             removePackageLI(ps.getPackageName(), chatty);
         } else if (DEBUG_REMOVE && chatty) {
@@ -271,8 +272,8 @@
             synchronized (mPm.mLock) {
                 mPm.mDomainVerificationManager.clearPackage(deletedPs.getPackageName());
                 mPm.mSettings.getKeySetManagerService().removeAppKeySetDataLPw(packageName);
-                mPm.mAppsFilter.removePackage(mPm.getPackageStateInternal(packageName),
-                        false /* isReplace */);
+                mPm.mAppsFilter.removePackage(mPm.snapshotComputer()
+                                .getPackageStateInternal(packageName), false /* isReplace */);
                 removedAppId = mPm.mSettings.removePackageLPw(packageName);
                 if (outInfo != null) {
                     outInfo.mRemovedAppId = removedAppId;
@@ -298,7 +299,8 @@
             if (changedUsers.size() > 0) {
                 final PreferredActivityHelper preferredActivityHelper =
                         new PreferredActivityHelper(mPm);
-                preferredActivityHelper.updateDefaultHomeNotLocked(changedUsers);
+                preferredActivityHelper.updateDefaultHomeNotLocked(mPm.snapshotComputer(),
+                        changedUsers);
                 mPm.postPreferredActivityChangedBroadcast(UserHandle.USER_ALL);
             }
         }
diff --git a/services/core/java/com/android/server/pm/ResolveIntentHelper.java b/services/core/java/com/android/server/pm/ResolveIntentHelper.java
index 25356a4..b74670b 100644
--- a/services/core/java/com/android/server/pm/ResolveIntentHelper.java
+++ b/services/core/java/com/android/server/pm/ResolveIntentHelper.java
@@ -115,7 +115,7 @@
             if (!mUserManager.exists(userId)) return null;
             final int callingUid = Binder.getCallingUid();
             flags = computer.updateFlagsForResolve(flags, userId, filterCallingUid, resolveForStart,
-                    computer.isImplicitImageCaptureIntentAndNotSetByDpcLocked(intent, userId,
+                    computer.isImplicitImageCaptureIntentAndNotSetByDpc(intent, userId,
                             resolvedType, flags));
             computer.enforceCrossUserPermission(callingUid, userId, false /*requireFullPermission*/,
                     false /*checkShell*/, "resolve intent");
@@ -170,9 +170,9 @@
                 }
                 // If we have saved a preference for a preferred activity for
                 // this Intent, use that.
-                ResolveInfo ri = mPreferredActivityHelper.findPreferredActivityNotLocked(intent,
-                        resolvedType, flags, query, true, false, debug, userId,
-                        queryMayBeFiltered);
+                ResolveInfo ri = mPreferredActivityHelper.findPreferredActivityNotLocked(computer,
+                        intent, resolvedType, flags, query, true, false, debug,
+                        userId, queryMayBeFiltered);
                 if (ri != null) {
                     return ri;
                 }
@@ -317,7 +317,7 @@
         final String instantAppPkgName = computer.getInstantAppPackageName(filterCallingUid);
         flags = computer.updateFlagsForResolve(flags, userId, filterCallingUid,
                 false /*includeInstantApps*/,
-                computer.isImplicitImageCaptureIntentAndNotSetByDpcLocked(intent, userId,
+                computer.isImplicitImageCaptureIntentAndNotSetByDpc(intent, userId,
                         resolvedType, flags));
         Intent originalIntent = null;
         ComponentName comp = intent.getComponent();
@@ -562,7 +562,7 @@
         final int callingUid = Binder.getCallingUid();
         flags = computer.updateFlagsForResolve(flags, userId, callingUid,
                 false /*includeInstantApps*/,
-                computer.isImplicitImageCaptureIntentAndNotSetByDpcLocked(intent, userId,
+                computer.isImplicitImageCaptureIntentAndNotSetByDpc(intent, userId,
                         resolvedType, flags));
         computer.enforceCrossUserPermission(callingUid, userId, false /*requireFullPermission*/,
                 false /*checkShell*/, "query intent activity options");
diff --git a/services/core/java/com/android/server/pm/Settings.java b/services/core/java/com/android/server/pm/Settings.java
index 021c3db..698dbe9 100644
--- a/services/core/java/com/android/server/pm/Settings.java
+++ b/services/core/java/com/android/server/pm/Settings.java
@@ -40,7 +40,6 @@
 import android.content.IntentFilter;
 import android.content.pm.ActivityInfo;
 import android.content.pm.ApplicationInfo;
-import android.content.pm.ComponentInfo;
 import android.content.pm.IntentFilterVerificationInfo;
 import android.content.pm.PackageInstaller;
 import android.content.pm.PackageManager;
@@ -63,7 +62,6 @@
 import android.os.Message;
 import android.os.PatternMatcher;
 import android.os.PersistableBundle;
-import android.os.Process;
 import android.os.SELinux;
 import android.os.SystemClock;
 import android.os.Trace;
@@ -113,11 +111,9 @@
 import com.android.server.pm.pkg.PackageStateInternal;
 import com.android.server.pm.pkg.PackageUserState;
 import com.android.server.pm.pkg.PackageUserStateInternal;
-import com.android.server.pm.pkg.PackageUserStateUtils;
 import com.android.server.pm.pkg.SuspendParams;
 import com.android.server.pm.pkg.component.ParsedComponent;
 import com.android.server.pm.pkg.component.ParsedIntentInfo;
-import com.android.server.pm.pkg.component.ParsedMainComponent;
 import com.android.server.pm.pkg.component.ParsedPermission;
 import com.android.server.pm.pkg.component.ParsedProcess;
 import com.android.server.pm.pkg.parsing.PackageInfoWithoutStateUtils;
@@ -411,8 +407,6 @@
         int[] excludedUserIds;
     }
 
-    private static int mFirstAvailableUid = Process.FIRST_APPLICATION_UID;
-
     /** Map from volume UUID to {@link VersionInfo} */
     @Watched
     private final WatchedArrayMap<String, VersionInfo> mVersion = new WatchedArrayMap<>();
@@ -475,10 +469,8 @@
 
     @Watched
     final WatchedArrayMap<String, SharedUserSetting> mSharedUsers = new WatchedArrayMap<>();
-    @Watched
-    private final WatchedArrayList<SettingBase> mAppIds;
-    @Watched
-    private final WatchedSparseArray<SettingBase> mOtherAppIds;
+    @Watched(manual = true)
+    private final AppIdSettingMap mAppIds;
 
     // For reading/writing settings file.
     @Watched
@@ -568,7 +560,6 @@
         mCrossProfileIntentResolvers.registerObserver(mObserver);
         mSharedUsers.registerObserver(mObserver);
         mAppIds.registerObserver(mObserver);
-        mOtherAppIds.registerObserver(mObserver);
         mRenamedPackages.registerObserver(mObserver);
         mNextAppLinkGeneration.registerObserver(mObserver);
         mDefaultBrowserApp.registerObserver(mObserver);
@@ -594,8 +585,7 @@
 
         mLock = new PackageManagerTracedLock();
         mPackages.putAll(pkgSettings);
-        mAppIds = new WatchedArrayList<>();
-        mOtherAppIds = new WatchedSparseArray<>();
+        mAppIds = new AppIdSettingMap();
         mSystemDir = null;
         mPermissions = null;
         mRuntimePermissionsPersistence = null;
@@ -631,8 +621,7 @@
         mKeySetManagerService = new KeySetManagerService(mPackages);
 
         mLock = lock;
-        mAppIds = new WatchedArrayList<>();
-        mOtherAppIds = new WatchedSparseArray<>();
+        mAppIds = new AppIdSettingMap();
         mPermissions = new LegacyPermissionSettings(lock);
         mRuntimePermissionsPersistence = new RuntimePermissionPersistence(
                 runtimePermissionsPersistence, new Consumer<Integer>() {
@@ -713,7 +702,6 @@
                 mCrossProfileIntentResolvers, r.mCrossProfileIntentResolvers);
         mSharedUsers.snapshot(r.mSharedUsers);
         mAppIds = r.mAppIds.snapshot();
-        mOtherAppIds = r.mOtherAppIds.snapshot();
         WatchedArrayList.snapshot(
                 mPastSignatures, r.mPastSignatures);
         WatchedArrayMap.snapshot(
@@ -785,7 +773,7 @@
         SharedUserSetting s = mSharedUsers.get(name);
         if (s == null && create) {
             s = new SharedUserSetting(name, pkgFlags, pkgPrivateFlags);
-            s.mAppId = acquireAndRegisterNewAppIdLPw(s);
+            s.mAppId = mAppIds.acquireAndRegisterNewAppId(s);
             if (s.mAppId < 0) {
                 // < 0 means we couldn't assign a userid; throw exception
                 throw new PackageManagerException(INSTALL_FAILED_INSUFFICIENT_STORAGE,
@@ -893,7 +881,7 @@
                 pkgPrivateFlags, 0 /*userId*/, usesSdkLibraries, usesSdkLibrariesVersions,
                 usesStaticLibraries, usesStaticLibrariesVersions, mimeGroups, domainSetId);
         p.setAppId(uid);
-        if (registerExistingAppIdLPw(uid, p, name)) {
+        if (mAppIds.registerExistingAppId(uid, p, name)) {
             mPackages.put(name, p);
             return p;
         }
@@ -912,7 +900,7 @@
         }
         s = new SharedUserSetting(name, pkgFlags, pkgPrivateFlags);
         s.mAppId = uid;
-        if (registerExistingAppIdLPw(uid, s, name)) {
+        if (mAppIds.registerExistingAppId(uid, s, name)) {
             mSharedUsers.put(name, s);
             return s;
         }
@@ -1213,11 +1201,11 @@
         final boolean createdNew;
         if (p.getAppId() == 0 || forceNew) {
             // Assign new user ID
-            p.setAppId(acquireAndRegisterNewAppIdLPw(p));
+            p.setAppId(mAppIds.acquireAndRegisterNewAppId(p));
             createdNew = true;
         } else {
             // Add new setting to list of user IDs
-            createdNew = registerExistingAppIdLPw(p.getAppId(), p, p.getPackageName());
+            createdNew = mAppIds.registerExistingAppId(p.getAppId(), p, p.getPackageName());
         }
         if (p.getAppId() < 0) {
             PackageManagerService.reportSettingsProblem(Log.WARN,
@@ -1278,7 +1266,8 @@
     // Utility method that adds a PackageSetting to mPackages and
     // completes updating the shared user attributes and any restored
     // app link verification state
-    private void addPackageSettingLPw(PackageSetting p, SharedUserSetting sharedUser) {
+    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
+    void addPackageSettingLPw(PackageSetting p, SharedUserSetting sharedUser) {
         mPackages.put(p.getPackageName(), p);
         if (sharedUser != null) {
             SharedUserSetting existingSharedUserSetting = getSharedUserSettingLPr(p);
@@ -1301,16 +1290,16 @@
             p.setAppId(sharedUser.mAppId);
         }
 
-        // If the we know about this user id, we have to update it as it
+        // If we know about this user id, we have to update it as it
         // has to point to the same PackageSetting instance as the package.
         Object userIdPs = getSettingLPr(p.getAppId());
         if (sharedUser == null) {
             if (userIdPs != null && userIdPs != p) {
-                replaceAppIdLPw(p.getAppId(), p);
+                mAppIds.replaceSetting(p.getAppId(), p);
             }
         } else {
             if (userIdPs != null && userIdPs != sharedUser) {
-                replaceAppIdLPw(p.getAppId(), sharedUser);
+                mAppIds.replaceSetting(p.getAppId(), sharedUser);
             }
         }
     }
@@ -1359,79 +1348,22 @@
         mInstallerPackages.remove(packageName);
     }
 
-    /** Returns true if the requested AppID was valid and not already registered. */
-    private boolean registerExistingAppIdLPw(int appId, SettingBase obj, Object name) {
-        if (appId > Process.LAST_APPLICATION_UID) {
-            return false;
-        }
-
-        if (appId >= Process.FIRST_APPLICATION_UID) {
-            int size = mAppIds.size();
-            final int index = appId - Process.FIRST_APPLICATION_UID;
-            // fill the array until our index becomes valid
-            while (index >= size) {
-                mAppIds.add(null);
-                size++;
-            }
-            if (mAppIds.get(index) != null) {
-                PackageManagerService.reportSettingsProblem(Log.WARN,
-                        "Adding duplicate app id: " + appId
-                        + " name=" + name);
-                return false;
-            }
-            mAppIds.set(index, obj);
-        } else {
-            if (mOtherAppIds.get(appId) != null) {
-                PackageManagerService.reportSettingsProblem(Log.WARN,
-                        "Adding duplicate shared id: " + appId
-                                + " name=" + name);
-                return false;
-            }
-            mOtherAppIds.put(appId, obj);
-        }
-        return true;
-    }
-
     /** Gets the setting associated with the provided App ID */
     public SettingBase getSettingLPr(int appId) {
-        if (appId >= Process.FIRST_APPLICATION_UID) {
-            final int size = mAppIds.size();
-            final int index = appId - Process.FIRST_APPLICATION_UID;
-            return index < size ? mAppIds.get(index) : null;
-        } else {
-            return mOtherAppIds.get(appId);
-        }
+        return mAppIds.getSetting(appId);
     }
 
     /** Unregisters the provided app ID. */
     void removeAppIdLPw(int appId) {
-        if (appId >= Process.FIRST_APPLICATION_UID) {
-            final int size = mAppIds.size();
-            final int index = appId - Process.FIRST_APPLICATION_UID;
-            if (index < size) mAppIds.set(index, null);
-        } else {
-            mOtherAppIds.remove(appId);
-        }
-        setFirstAvailableUid(appId + 1);
+        mAppIds.removeSetting(appId);
     }
-
-    private void replaceAppIdLPw(int appId, SettingBase obj) {
-        if (appId >= Process.FIRST_APPLICATION_UID) {
-            final int size = mAppIds.size();
-            final int index = appId - Process.FIRST_APPLICATION_UID;
-            if (index < size) mAppIds.set(index, obj);
-        } else {
-            mOtherAppIds.put(appId, obj);
-        }
-    }
-
     /**
      * Transparently convert a SharedUserSetting into PackageSettings without changing appId.
      * The sharedUser passed to this method has to be {@link SharedUserSetting#isSingleUser()}.
      */
     void convertSharedUserSettingsLPw(SharedUserSetting sharedUser) {
         final PackageSetting ps = sharedUser.getPackageSettings().valueAt(0);
-        replaceAppIdLPw(sharedUser.getAppId(), ps);
+        mAppIds.replaceSetting(sharedUser.getAppId(), ps);
 
         // Unlink the SharedUserSetting
         ps.setSharedUserAppId(INVALID_UID);
@@ -4295,33 +4227,6 @@
         }
     }
 
-    // This should be called (at least) whenever an application is removed
-    private void setFirstAvailableUid(int uid) {
-        if (uid > mFirstAvailableUid) {
-            mFirstAvailableUid = uid;
-        }
-    }
-
-    /** Returns a new AppID or -1 if we could not find an available AppID to assign */
-    private int acquireAndRegisterNewAppIdLPw(SettingBase obj) {
-        // Let's be stupidly inefficient for now...
-        final int size = mAppIds.size();
-        for (int i = mFirstAvailableUid - Process.FIRST_APPLICATION_UID; i < size; i++) {
-            if (mAppIds.get(i) == null) {
-                mAppIds.set(i, obj);
-                return Process.FIRST_APPLICATION_UID + i;
-            }
-        }
-
-        // None left?
-        if (size > (Process.LAST_APPLICATION_UID - Process.FIRST_APPLICATION_UID)) {
-            return -1;
-        }
-
-        mAppIds.add(obj);
-        return Process.FIRST_APPLICATION_UID + size;
-    }
-
     public VerifierDeviceIdentity getVerifierDeviceIdentityLPw(@NonNull Computer computer) {
         if (mVerifierDeviceIdentity == null) {
             mVerifierDeviceIdentity = VerifierDeviceIdentity.generate();
@@ -4354,33 +4259,6 @@
         return getDisabledSystemPkgLPr(enabledPackageSetting.getPackageName());
     }
 
-    boolean isEnabledAndMatchLPr(ComponentInfo componentInfo, long flags, int userId) {
-        final PackageSetting ps = mPackages.get(componentInfo.packageName);
-        if (ps == null) return false;
-
-        final PackageUserStateInternal userState = ps.readUserState(userId);
-        return PackageUserStateUtils.isMatch(userState, componentInfo, flags);
-    }
-
-    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
-    public boolean isEnabledAndMatchLPr(AndroidPackage pkg, ParsedMainComponent component,
-            long flags, int userId) {
-        final PackageSetting ps = mPackages.get(component.getPackageName());
-        if (ps == null) return false;
-
-        final PackageUserStateInternal userState = ps.readUserState(userId);
-        return PackageUserStateUtils.isMatch(userState, pkg.isSystem(), pkg.isEnabled(), component,
-                flags);
-    }
-
-    boolean isOrphaned(String packageName) {
-        final PackageSetting pkg = mPackages.get(packageName);
-        if (pkg == null) {
-            throw new IllegalArgumentException("Unknown package: " + packageName);
-        }
-        return pkg.getInstallSource().isOrphaned;
-    }
-
     int getApplicationEnabledSettingLPr(String packageName, int userId)
             throws PackageManager.NameNotFoundException {
         final PackageSetting pkg = mPackages.get(packageName);
diff --git a/services/core/java/com/android/server/pm/SharedLibrariesImpl.java b/services/core/java/com/android/server/pm/SharedLibrariesImpl.java
index 3fe0790..479a404 100644
--- a/services/core/java/com/android/server/pm/SharedLibrariesImpl.java
+++ b/services/core/java/com/android/server/pm/SharedLibrariesImpl.java
@@ -741,9 +741,11 @@
         }
         SharedLibraryInfo libraryInfo = versionedLib.valueAt(libIdx);
 
+        final Computer snapshot = mPm.snapshotComputer();
+
         // Remove the shared library overlays from its dependent packages.
         for (int currentUserId : mPm.mUserManager.getUserIds()) {
-            final List<VersionedPackage> dependents = mPm.getPackagesUsingSharedLibrary(
+            final List<VersionedPackage> dependents = snapshot.getPackagesUsingSharedLibrary(
                     libraryInfo, 0, Process.SYSTEM_UID, currentUserId);
             if (dependents == null) {
                 continue;
diff --git a/services/core/java/com/android/server/pm/SharedUidMigration.java b/services/core/java/com/android/server/pm/SharedUidMigration.java
index a7d5e955..e44ef66 100644
--- a/services/core/java/com/android/server/pm/SharedUidMigration.java
+++ b/services/core/java/com/android/server/pm/SharedUidMigration.java
@@ -17,6 +17,7 @@
 package com.android.server.pm;
 
 import android.annotation.IntDef;
+import android.content.pm.PackageManager;
 import android.os.Build;
 import android.os.SystemProperties;
 
@@ -59,22 +60,15 @@
     @Retention(RetentionPolicy.SOURCE)
     public @interface Strategy {}
 
+    @Strategy
     private static final int DEFAULT = BEST_EFFORT;
 
     /**
-     * All shared UID migration is disabled.
-     * This is not a strategy that can be set with system properties.
-     * To disable shared UID migration, change {@link #DEFAULT} to this value.
-     */
-    private static final int DISABLED = 0;
-
-    /**
      * Whether shared UID migration is fully disabled. Disabled means the sharedUserMaxSdkVersion
      * attribute will be directly ignored in the parsing phase.
      */
-    @SuppressWarnings("ConstantConditions")
     public static boolean isDisabled() {
-        return DEFAULT == DISABLED;
+        return !PackageManager.ENABLE_SHARED_UID_MIGRATION;
     }
 
     /**
@@ -88,7 +82,7 @@
 
         final int s = SystemProperties.getInt(PROPERTY_KEY, DEFAULT);
         // No transition strategies can be used (http://b/221088088)
-        if (s > BEST_EFFORT || s <= DISABLED) {
+        if (s > BEST_EFFORT || s < NEW_INSTALL_ONLY) {
             return DEFAULT;
         }
         return s;
diff --git a/services/core/java/com/android/server/pm/ShortcutService.java b/services/core/java/com/android/server/pm/ShortcutService.java
index 1cf2dc5..25fe000 100644
--- a/services/core/java/com/android/server/pm/ShortcutService.java
+++ b/services/core/java/com/android/server/pm/ShortcutService.java
@@ -488,7 +488,8 @@
         mShortcutBitmapSaver = new ShortcutBitmapSaver(this);
         mShortcutDumpFiles = new ShortcutDumpFiles(this);
         mIsAppSearchEnabled = DeviceConfig.getBoolean(NAMESPACE_SYSTEMUI,
-                SystemUiDeviceConfigFlags.SHORTCUT_APPSEARCH_INTEGRATION, true);
+                SystemUiDeviceConfigFlags.SHORTCUT_APPSEARCH_INTEGRATION, true)
+                && !injectIsLowRamDevice();
 
         if (onlyForPackageManagerApis) {
             return; // Don't do anything further.  For unit tests only.
diff --git a/services/core/java/com/android/server/pm/StagingManager.java b/services/core/java/com/android/server/pm/StagingManager.java
index 52a7bed..43dde5c 100644
--- a/services/core/java/com/android/server/pm/StagingManager.java
+++ b/services/core/java/com/android/server/pm/StagingManager.java
@@ -28,8 +28,6 @@
 import android.content.pm.ApexStagedEvent;
 import android.content.pm.IStagedApexObserver;
 import android.content.pm.PackageInstaller;
-import android.content.pm.PackageInstaller.SessionInfo;
-import android.content.pm.PackageInstaller.SessionInfo.SessionErrorCode;
 import android.content.pm.PackageManager;
 import android.content.pm.PackageManagerInternal;
 import android.content.pm.StagedApexInfo;
@@ -124,7 +122,7 @@
         boolean containsApkSession();
         boolean containsApexSession();
         void setSessionReady();
-        void setSessionFailed(@SessionErrorCode int errorCode, String errorMessage);
+        void setSessionFailed(int errorCode, String errorMessage);
         void setSessionApplied();
         CompletableFuture<Void> installSession();
         boolean hasParentSessionId();
@@ -279,7 +277,7 @@
             String packageName = apexSession.getPackageName();
             String errorMsg = mApexManager.getApkInApexInstallError(packageName);
             if (errorMsg != null) {
-                throw new PackageManagerException(SessionInfo.SESSION_ACTIVATION_FAILED,
+                throw new PackageManagerException(PackageManager.INSTALL_ACTIVATION_FAILED,
                         "Failed to install apk-in-apex of " + packageName + " : " + errorMsg);
             }
         }
@@ -392,7 +390,7 @@
                 revertMsg += " Reason for revert: " + reasonForRevert;
             }
             Slog.d(TAG, revertMsg);
-            session.setSessionFailed(SessionInfo.SESSION_UNKNOWN_ERROR, revertMsg);
+            session.setSessionFailed(PackageManager.INSTALL_FAILED_INTERNAL_ERROR, revertMsg);
             return;
         }
 
@@ -477,7 +475,7 @@
             for (String apkInApex : mApexManager.getApksInApex(packageName)) {
                 if (!apkNames.add(apkInApex)) {
                     throw new PackageManagerException(
-                            SessionInfo.SESSION_ACTIVATION_FAILED,
+                            PackageManager.INSTALL_ACTIVATION_FAILED,
                             "Package: " + packageName + " in session: "
                                     + apexSession.sessionId() + " has duplicate apk-in-apex: "
                                     + apkInApex, null);
@@ -495,9 +493,7 @@
             // Should be impossible
             throw new RuntimeException(e);
         } catch (ExecutionException ee) {
-            PackageManagerException e = (PackageManagerException) ee.getCause();
-            final String errorMsg = PackageManager.installStatusToString(e.error, e.getMessage());
-            throw new PackageManagerException(SessionInfo.SESSION_ACTIVATION_FAILED, errorMsg);
+            throw (PackageManagerException) ee.getCause();
         }
     }
 
@@ -651,7 +647,7 @@
             // is upgrading. Fail all the sessions and exit early.
             for (int i = 0; i < sessions.size(); i++) {
                 StagedSession session = sessions.get(i);
-                session.setSessionFailed(SessionInfo.SESSION_ACTIVATION_FAILED,
+                session.setSessionFailed(PackageManager.INSTALL_ACTIVATION_FAILED,
                         "Build fingerprint has changed");
             }
             return;
@@ -691,7 +687,7 @@
             final ApexSessionInfo apexSession = apexSessions.get(session.sessionId());
             if (apexSession == null || apexSession.isUnknown) {
                 hasFailedApexSession = true;
-                session.setSessionFailed(SessionInfo.SESSION_ACTIVATION_FAILED, "apexd did "
+                session.setSessionFailed(PackageManager.INSTALL_ACTIVATION_FAILED, "apexd did "
                         + "not know anything about a staged session supposed to be activated");
                 continue;
             } else if (isApexSessionFailed(apexSession)) {
@@ -707,7 +703,7 @@
                     errorMsg += " Error: " + apexSession.errorMessage;
                 }
                 Slog.d(TAG, errorMsg);
-                session.setSessionFailed(SessionInfo.SESSION_ACTIVATION_FAILED, errorMsg);
+                session.setSessionFailed(PackageManager.INSTALL_ACTIVATION_FAILED, errorMsg);
                 continue;
             } else if (apexSession.isActivated || apexSession.isSuccess) {
                 hasAppliedApexSession = true;
@@ -716,13 +712,13 @@
                 // Apexd did not apply the session for some unknown reason. There is no guarantee
                 // that apexd will install it next time. Safer to proactively mark it as failed.
                 hasFailedApexSession = true;
-                session.setSessionFailed(SessionInfo.SESSION_ACTIVATION_FAILED,
+                session.setSessionFailed(PackageManager.INSTALL_ACTIVATION_FAILED,
                         "Staged session " + session.sessionId() + " at boot didn't activate nor "
                         + "fail. Marking it as failed anyway.");
             } else {
                 Slog.w(TAG, "Apex session " + session.sessionId() + " is in impossible state");
                 hasFailedApexSession = true;
-                session.setSessionFailed(SessionInfo.SESSION_ACTIVATION_FAILED,
+                session.setSessionFailed(PackageManager.INSTALL_ACTIVATION_FAILED,
                         "Impossible state");
             }
         }
@@ -742,7 +738,7 @@
                     // Session has been already failed in the loop above.
                     continue;
                 }
-                session.setSessionFailed(SessionInfo.SESSION_ACTIVATION_FAILED,
+                session.setSessionFailed(PackageManager.INSTALL_ACTIVATION_FAILED,
                         "Another apex session failed");
             }
             return;
@@ -758,7 +754,7 @@
             } catch (Exception e) {
                 Slog.e(TAG, "Staged install failed due to unhandled exception", e);
                 onInstallationFailure(session, new PackageManagerException(
-                        SessionInfo.SESSION_ACTIVATION_FAILED,
+                        PackageManager.INSTALL_FAILED_INTERNAL_ERROR,
                         "Staged install failed due to unhandled exception: " + e),
                         supportsCheckpoint, needsCheckpoint);
             }
diff --git a/services/core/java/com/android/server/pm/StorageEventHelper.java b/services/core/java/com/android/server/pm/StorageEventHelper.java
index bb7e55a..df19d3e 100644
--- a/services/core/java/com/android/server/pm/StorageEventHelper.java
+++ b/services/core/java/com/android/server/pm/StorageEventHelper.java
@@ -26,13 +26,13 @@
 import static com.android.server.pm.PackageManagerService.TAG;
 import static com.android.server.pm.PackageManagerServiceUtils.logCriticalInfo;
 
+import android.annotation.NonNull;
 import android.app.ResourcesManager;
 import android.content.IIntentReceiver;
 import android.content.pm.PackageManager;
 import android.content.pm.PackagePartitions;
 import android.content.pm.UserInfo;
 import android.content.pm.VersionedPackage;
-import com.android.server.pm.pkg.parsing.ParsingPackageUtils;
 import android.os.Environment;
 import android.os.FileUtils;
 import android.os.UserHandle;
@@ -48,6 +48,7 @@
 import com.android.internal.policy.AttributeCache;
 import com.android.server.pm.parsing.pkg.AndroidPackage;
 import com.android.server.pm.pkg.PackageStateInternal;
+import com.android.server.pm.pkg.parsing.ParsingPackageUtils;
 
 import java.io.File;
 import java.util.ArrayList;
@@ -78,7 +79,7 @@
                 // Clean up any users or apps that were removed or recreated
                 // while this volume was missing
                 mPm.mUserManager.reconcileUsers(volumeUuid);
-                reconcileApps(volumeUuid);
+                reconcileApps(mPm.snapshotComputer(), volumeUuid);
 
                 // Clean up any install sessions that expired or were
                 // cancelled while this volume was missing
@@ -299,8 +300,8 @@
      * aren't expected, either due to uninstallation or reinstallation on
      * another volume.
      */
-    public void reconcileApps(String volumeUuid) {
-        List<String> absoluteCodePaths = collectAbsoluteCodePaths();
+    public void reconcileApps(@NonNull Computer snapshot, String volumeUuid) {
+        List<String> absoluteCodePaths = collectAbsoluteCodePaths(snapshot);
         List<File> filesToDelete = null;
 
         final File[] files = FileUtils.listFilesOrEmpty(
@@ -345,10 +346,10 @@
         }
     }
 
-    private List<String> collectAbsoluteCodePaths() {
+    private List<String> collectAbsoluteCodePaths(@NonNull Computer snapshot) {
         List<String> codePaths = new ArrayList<>();
         final ArrayMap<String, ? extends PackageStateInternal> packageStates =
-                mPm.getPackageStates();
+                snapshot.getPackageStates();
         final int packageCount = packageStates.size();
         for (int i = 0; i < packageCount; i++) {
             final PackageStateInternal ps = packageStates.valueAt(i);
diff --git a/services/core/java/com/android/server/pm/SuspendPackageHelper.java b/services/core/java/com/android/server/pm/SuspendPackageHelper.java
index 3ef5599..588dfaf 100644
--- a/services/core/java/com/android/server/pm/SuspendPackageHelper.java
+++ b/services/core/java/com/android/server/pm/SuspendPackageHelper.java
@@ -100,14 +100,14 @@
      * @return The names of failed packages.
      */
     @Nullable
-    String[] setPackagesSuspended(@NonNull Computer computer, @Nullable String[] packageNames,
+    String[] setPackagesSuspended(@NonNull Computer snapshot, @Nullable String[] packageNames,
             boolean suspended, @Nullable PersistableBundle appExtras,
             @Nullable PersistableBundle launcherExtras, @Nullable SuspendDialogInfo dialogInfo,
             @NonNull String callingPackage, @UserIdInt int userId, int callingUid) {
         if (ArrayUtils.isEmpty(packageNames)) {
             return packageNames;
         }
-        if (suspended && !isSuspendAllowedForUser(userId, callingUid)) {
+        if (suspended && !isSuspendAllowedForUser(snapshot, userId, callingUid)) {
             Slog.w(TAG, "Cannot suspend due to restrictions on user " + userId);
             return packageNames;
         }
@@ -123,7 +123,7 @@
         ArraySet<String> modifiedPackages = new ArraySet<>();
 
         final boolean[] canSuspend = suspended
-                ? canSuspendPackageForUser(computer, packageNames, userId, callingUid) : null;
+                ? canSuspendPackageForUser(snapshot, packageNames, userId, callingUid) : null;
         for (int i = 0; i < packageNames.length; i++) {
             final String packageName = packageNames[i];
             if (callingPackage.equals(packageName)) {
@@ -133,9 +133,9 @@
                 continue;
             }
             final PackageStateInternal packageState =
-                    computer.getPackageStateInternal(packageName);
+                    snapshot.getPackageStateInternal(packageName);
             if (packageState == null
-                    || computer.shouldFilterApplication(packageState, callingUid, userId)) {
+                    || snapshot.shouldFilterApplication(packageState, callingUid, userId)) {
                 Slog.w(TAG, "Could not find package setting for package: " + packageName
                         + ". Skipping suspending/un-suspending.");
                 unmodifiablePackages.add(packageName);
@@ -191,9 +191,11 @@
             }
         });
 
+        final Computer newSnapshot = mPm.snapshotComputer();
+
         if (!changedPackagesList.isEmpty()) {
             final String[] changedPackages = changedPackagesList.toArray(new String[0]);
-            sendPackagesSuspendedForUser(
+            sendPackagesSuspendedForUser(newSnapshot,
                     suspended ? Intent.ACTION_PACKAGES_SUSPENDED
                             : Intent.ACTION_PACKAGES_UNSUSPENDED,
                     changedPackages, changedUids.toArray(), userId);
@@ -202,7 +204,7 @@
         }
         // Send the suspension changed broadcast to ensure suspension state is not stale.
         if (!modifiedPackages.isEmpty()) {
-            sendPackagesSuspendedForUser(Intent.ACTION_PACKAGES_SUSPENSION_CHANGED,
+            sendPackagesSuspendedForUser(newSnapshot, Intent.ACTION_PACKAGES_SUSPENSION_CHANGED,
                     modifiedPackages.toArray(new String[0]), modifiedUids.toArray(), userId);
         }
         return unmodifiablePackages.toArray(new String[0]);
@@ -217,14 +219,14 @@
      * @return The names of packages which are Unsuspendable.
      */
     @NonNull
-    String[] getUnsuspendablePackagesForUser(@NonNull Computer computer,
+    String[] getUnsuspendablePackagesForUser(@NonNull Computer snapshot,
             @NonNull String[] packageNames, @UserIdInt int userId, int callingUid) {
-        if (!isSuspendAllowedForUser(userId, callingUid)) {
+        if (!isSuspendAllowedForUser(snapshot, userId, callingUid)) {
             Slog.w(TAG, "Cannot suspend due to restrictions on user " + userId);
             return packageNames;
         }
         final ArraySet<String> unactionablePackages = new ArraySet<>();
-        final boolean[] canSuspend = canSuspendPackageForUser(computer, packageNames, userId,
+        final boolean[] canSuspend = canSuspendPackageForUser(snapshot, packageNames, userId,
                 callingUid);
         for (int i = 0; i < packageNames.length; i++) {
             if (!canSuspend[i]) {
@@ -232,7 +234,7 @@
                 continue;
             }
             final PackageStateInternal packageState =
-                    computer.getPackageStateFiltered(packageNames[i], callingUid, userId);
+                    snapshot.getPackageStateFiltered(packageNames[i], callingUid, userId);
             if (packageState == null) {
                 Slog.w(TAG, "Could not find package setting for package: " + packageNames[i]);
                 unactionablePackages.add(packageNames[i]);
@@ -250,8 +252,9 @@
      * @return The app extras of the suspended package.
      */
     @Nullable
-    Bundle getSuspendedPackageAppExtras(@NonNull String packageName, int userId, int callingUid) {
-        final PackageStateInternal ps = mPm.getPackageStateInternal(packageName, callingUid);
+    Bundle getSuspendedPackageAppExtras(@NonNull Computer snapshot, @NonNull String packageName,
+            int userId, int callingUid) {
+        final PackageStateInternal ps = snapshot.getPackageStateInternal(packageName, callingUid);
         if (ps == null) {
             return null;
         }
@@ -329,12 +332,14 @@
             }
         });
 
+        final Computer newSnapshot = mPm.snapshotComputer();
+
         mPm.scheduleWritePackageRestrictions(userId);
         if (!unsuspendedPackages.isEmpty()) {
             final String[] packageArray = unsuspendedPackages.toArray(
                     new String[unsuspendedPackages.size()]);
             sendMyPackageSuspendedOrUnsuspended(packageArray, false, userId);
-            sendPackagesSuspendedForUser(Intent.ACTION_PACKAGES_UNSUSPENDED,
+            sendPackagesSuspendedForUser(newSnapshot, Intent.ACTION_PACKAGES_UNSUSPENDED,
                     packageArray, unsuspendedUids.toArray(), userId);
         }
     }
@@ -348,10 +353,10 @@
      * @return The launcher extras.
      */
     @Nullable
-    Bundle getSuspendedPackageLauncherExtras(@NonNull String packageName, int userId,
-            int callingUid) {
-        final PackageStateInternal packageState = mPm.getPackageStateInternal(
-                packageName, callingUid);
+    Bundle getSuspendedPackageLauncherExtras(@NonNull Computer snapshot,
+            @NonNull String packageName, int userId, int callingUid) {
+        final PackageStateInternal packageState =
+                snapshot.getPackageStateInternal(packageName, callingUid);
         if (packageState == null) {
             return null;
         }
@@ -376,9 +381,10 @@
      * @param callingUid The caller's uid.
      * @return {@code true}, if the given package is suspended.
      */
-    boolean isPackageSuspended(@NonNull String packageName, int userId, int callingUid) {
-        final PackageStateInternal packageState = mPm.getPackageStateInternal(
-                packageName, callingUid);
+    boolean isPackageSuspended(@NonNull Computer snapshot, @NonNull String packageName, int userId,
+            int callingUid) {
+        final PackageStateInternal packageState =
+                snapshot.getPackageStateInternal(packageName, callingUid);
         return packageState != null && packageState.getUserStateOrDefault(userId)
                 .isSuspended();
     }
@@ -392,8 +398,9 @@
      * @return The name of suspending package.
      */
     @Nullable
-    String getSuspendingPackage(@NonNull String suspendedPackage, int userId, int callingUid) {
-        final PackageStateInternal packageState = mPm.getPackageStateInternal(
+    String getSuspendingPackage(@NonNull Computer snapshot, @NonNull String suspendedPackage,
+            int userId, int callingUid) {
+        final PackageStateInternal packageState = snapshot.getPackageStateInternal(
                 suspendedPackage, callingUid);
         if (packageState == null) {
             return  null;
@@ -424,9 +431,10 @@
      * @return The dialog info.
      */
     @Nullable
-    SuspendDialogInfo getSuspendedDialogInfo(@NonNull String suspendedPackage,
-            @NonNull String suspendingPackage, int userId, int callingUid) {
-        final PackageStateInternal packageState = mPm.getPackageStateInternal(
+    SuspendDialogInfo getSuspendedDialogInfo(@NonNull Computer snapshot,
+            @NonNull String suspendedPackage, @NonNull String suspendingPackage, int userId,
+            int callingUid) {
+        final PackageStateInternal packageState = snapshot.getPackageStateInternal(
                 suspendedPackage, callingUid);
         if (packageState == null) {
             return  null;
@@ -454,9 +462,9 @@
      * @param callingUid The caller's uid.
      * @return {@code true} if the user is allowed to suspend packages by the caller.
      */
-    boolean isSuspendAllowedForUser(int userId, int callingUid) {
+    boolean isSuspendAllowedForUser(@NonNull Computer snapshot, int userId, int callingUid) {
         final UserManagerService userManager = mInjector.getUserManagerService();
-        return isCallerDeviceOrProfileOwner(userId, callingUid)
+        return isCallerDeviceOrProfileOwner(snapshot, userId, callingUid)
                 || (!userManager.hasUserRestriction(UserManager.DISALLOW_APPS_CONTROL, userId)
                 && !userManager.hasUserRestriction(UserManager.DISALLOW_UNINSTALL_APPS, userId));
     }
@@ -471,21 +479,23 @@
      * @return An array containing results of the checks
      */
     @NonNull
-    boolean[] canSuspendPackageForUser(@NonNull Computer computer, @NonNull String[] packageNames,
+    boolean[] canSuspendPackageForUser(@NonNull Computer snapshot, @NonNull String[] packageNames,
             int userId, int callingUid) {
         final boolean[] canSuspend = new boolean[packageNames.length];
-        final boolean isCallerOwner = isCallerDeviceOrProfileOwner(userId, callingUid);
+        final boolean isCallerOwner = isCallerDeviceOrProfileOwner(snapshot, userId, callingUid);
         final long token = Binder.clearCallingIdentity();
         try {
             final DefaultAppProvider defaultAppProvider = mInjector.getDefaultAppProvider();
             final String activeLauncherPackageName = defaultAppProvider.getDefaultHome(userId);
             final String dialerPackageName = defaultAppProvider.getDefaultDialer(userId);
-            final String requiredInstallerPackage = getKnownPackageName(PACKAGE_INSTALLER, userId);
+            final String requiredInstallerPackage =
+                    getKnownPackageName(snapshot, PACKAGE_INSTALLER, userId);
             final String requiredUninstallerPackage =
-                    getKnownPackageName(PACKAGE_UNINSTALLER, userId);
-            final String requiredVerifierPackage = getKnownPackageName(PACKAGE_VERIFIER, userId);
+                    getKnownPackageName(snapshot, PACKAGE_UNINSTALLER, userId);
+            final String requiredVerifierPackage =
+                    getKnownPackageName(snapshot, PACKAGE_VERIFIER, userId);
             final String requiredPermissionControllerPackage =
-                    getKnownPackageName(PACKAGE_PERMISSION_CONTROLLER, userId);
+                    getKnownPackageName(snapshot, PACKAGE_PERMISSION_CONTROLLER, userId);
             for (int i = 0; i < packageNames.length; i++) {
                 canSuspend[i] = false;
                 final String packageName = packageNames[i];
@@ -530,7 +540,7 @@
                             + "\": protected package");
                     continue;
                 }
-                if (!isCallerOwner && computer.getBlockUninstall(userId, packageName)) {
+                if (!isCallerOwner && snapshot.getBlockUninstall(userId, packageName)) {
                     Slog.w(TAG, "Cannot suspend package \"" + packageName
                             + "\": blocked by admin");
                     continue;
@@ -539,7 +549,7 @@
                 // Cannot suspend static shared libs as they are considered
                 // a part of the using app (emulating static linking). Also
                 // static libs are installed always on internal storage.
-                PackageStateInternal packageState = computer.getPackageStateInternal(packageName);
+                PackageStateInternal packageState = snapshot.getPackageStateInternal(packageName);
                 AndroidPackage pkg = packageState == null ? null : packageState.getPkg();
                 if (pkg != null) {
                     // Cannot suspend SDK libs as they are controlled by SDK manager.
@@ -580,8 +590,8 @@
      * @param userId The user where packages reside.
      */
     @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
-    void sendPackagesSuspendedForUser(@NonNull String intent, @NonNull String[] pkgList,
-            @NonNull int[] uidList, int userId) {
+    void sendPackagesSuspendedForUser(@NonNull Computer snapshot, @NonNull String intent,
+            @NonNull String[] pkgList, @NonNull int[] uidList, int userId) {
         final List<List<String>> pkgsToSend = new ArrayList(pkgList.length);
         final List<IntArray> uidsToSend = new ArrayList(pkgList.length);
         final List<SparseArray<int[]>> allowListsToSend = new ArrayList(pkgList.length);
@@ -592,8 +602,8 @@
             final String pkgName = pkgList[i];
             final int uid = uidList[i];
             SparseArray<int[]> allowList = mInjector.getAppsFilter().getVisibilityAllowList(
-                    mPm.getPackageStateInternal(pkgName, SYSTEM_UID),
-                    userIds, mPm.getPackageStates());
+                    snapshot.getPackageStateInternal(pkgName, SYSTEM_UID),
+                    userIds, snapshot.getPackageStates());
             if (allowList == null) {
                 allowList = new SparseArray<>(0);
             }
@@ -628,19 +638,22 @@
         }
     }
 
-    private String getKnownPackageName(@KnownPackage int knownPackage, int userId) {
-        final String[] knownPackages = mPm.getKnownPackageNamesInternal(knownPackage, userId);
+    private String getKnownPackageName(@NonNull Computer snapshot, @KnownPackage int knownPackage,
+            int userId) {
+        final String[] knownPackages =
+                mPm.getKnownPackageNamesInternal(snapshot, knownPackage, userId);
         return knownPackages.length > 0 ? knownPackages[0] : null;
     }
 
-    private boolean isCallerDeviceOrProfileOwner(int userId, int callingUid) {
+    private boolean isCallerDeviceOrProfileOwner(@NonNull Computer snapshot, int userId,
+            int callingUid) {
         if (callingUid == SYSTEM_UID) {
             return true;
         }
         final String ownerPackage = mProtectedPackages.getDeviceOwnerOrProfileOwnerPackage(userId);
         if (ownerPackage != null) {
-            return callingUid == mPm.getPackageUidInternal(
-                    ownerPackage, 0, userId, callingUid);
+            return callingUid == snapshot.getPackageUidInternal(ownerPackage, 0, userId,
+                    callingUid);
         }
         return false;
     }
@@ -659,9 +672,10 @@
                 return;
             }
             final int[] targetUserIds = new int[] {userId};
+            final Computer snapshot = mPm.snapshotComputer();
             for (String packageName : affectedPackages) {
                 final Bundle appExtras = suspended
-                        ? getSuspendedPackageAppExtras(packageName, userId, SYSTEM_UID)
+                        ? getSuspendedPackageAppExtras(snapshot, packageName, userId, SYSTEM_UID)
                         : null;
                 final Bundle intentExtras;
                 if (appExtras != null) {
diff --git a/services/core/java/com/android/server/pm/TEST_MAPPING b/services/core/java/com/android/server/pm/TEST_MAPPING
index 88a298a..9c74dd7 100644
--- a/services/core/java/com/android/server/pm/TEST_MAPPING
+++ b/services/core/java/com/android/server/pm/TEST_MAPPING
@@ -52,9 +52,6 @@
       "options": [
         {
           "include-filter": "com.google.android.security.gts.PackageVerifierTest"
-        },
-        {
-          "exclude-filter": "com.google.android.security.gts.PackageVerifierTest#testAdbInstall_timeout_allowed"
         }
       ]
     },
diff --git a/services/core/java/com/android/server/pm/UserManagerService.java b/services/core/java/com/android/server/pm/UserManagerService.java
index a8d24fa..34b7ad4 100644
--- a/services/core/java/com/android/server/pm/UserManagerService.java
+++ b/services/core/java/com/android/server/pm/UserManagerService.java
@@ -1593,13 +1593,13 @@
     }
 
     @Override
-    public boolean isCredentialSharedWithParent(@UserIdInt int userId) {
+    public boolean isCredentialSharableWithParent(@UserIdInt int userId) {
         checkManageOrInteractPermissionIfCallerInOtherProfileGroup(userId,
-                "isCredentialSharedWithParent");
+                "isCredentialSharableWithParent");
         synchronized (mUsersLock) {
             UserTypeDetails userTypeDetails = getUserTypeDetailsNoChecks(userId);
             return userTypeDetails != null && userTypeDetails.isProfile()
-                    && userTypeDetails.isCredentialSharedWithParent();
+                    && userTypeDetails.isCredentialSharableWithParent();
         }
     }
 
@@ -4148,11 +4148,11 @@
                 continue;
             }
             if (filter.direction == DefaultCrossProfileIntentFilter.Direction.TO_PARENT) {
-                mPm.addCrossProfileIntentFilter(
+                mPm.addCrossProfileIntentFilter(mPm.snapshotComputer(),
                         filter.filter, mContext.getOpPackageName(), profileUserId, parentUserId,
                         filter.flags);
             } else {
-                mPm.addCrossProfileIntentFilter(
+                mPm.addCrossProfileIntentFilter(mPm.snapshotComputer(),
                         filter.filter, mContext.getOpPackageName(), parentUserId, profileUserId,
                         filter.flags);
             }
@@ -4638,12 +4638,12 @@
             final String restriction = getUserRemovalRestriction(userId);
             if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean(restriction, false)) {
                 Slog.w(LOG_TAG, "Cannot remove user. " + restriction + " is enabled.");
-                return UserManager.REMOVE_RESULT_ERROR;
+                return UserManager.REMOVE_RESULT_ERROR_USER_RESTRICTION;
             }
         }
         if (userId == UserHandle.USER_SYSTEM) {
             Slog.e(LOG_TAG, "System user cannot be removed.");
-            return UserManager.REMOVE_RESULT_ERROR;
+            return UserManager.REMOVE_RESULT_ERROR_SYSTEM_USER;
         }
 
         final long ident = Binder.clearCallingIdentity();
@@ -4655,7 +4655,7 @@
                     if (userData == null) {
                         Slog.e(LOG_TAG,
                                 "Cannot remove user " + userId + ", invalid user id provided.");
-                        return UserManager.REMOVE_RESULT_ERROR;
+                        return UserManager.REMOVE_RESULT_ERROR_USER_NOT_FOUND;
                     }
 
                     if (mRemovingUserIds.get(userId)) {
diff --git a/services/core/java/com/android/server/pm/UserTypeDetails.java b/services/core/java/com/android/server/pm/UserTypeDetails.java
index 2f5e238..4aad1a7 100644
--- a/services/core/java/com/android/server/pm/UserTypeDetails.java
+++ b/services/core/java/com/android/server/pm/UserTypeDetails.java
@@ -161,7 +161,7 @@
      *
      * <p> Default value is false
      */
-    private final boolean mIsCredentialSharedWithParent;
+    private final boolean mIsCredentialSharableWithParent;
 
     private UserTypeDetails(@NonNull String name, boolean enabled, int maxAllowed,
             @UserInfoFlag int baseType, @UserInfoFlag int defaultUserInfoPropertyFlags, int label,
@@ -174,7 +174,7 @@
             @Nullable Bundle defaultSecureSettings,
             @Nullable List<DefaultCrossProfileIntentFilter> defaultCrossProfileIntentFilters,
             boolean isMediaSharedWithParent,
-            boolean isCredentialSharedWithParent) {
+            boolean isCredentialSharableWithParent) {
         this.mName = name;
         this.mEnabled = enabled;
         this.mMaxAllowed = maxAllowed;
@@ -194,7 +194,7 @@
         this.mBadgeColors = badgeColors;
         this.mDarkThemeBadgeColors = darkThemeBadgeColors;
         this.mIsMediaSharedWithParent = isMediaSharedWithParent;
-        this.mIsCredentialSharedWithParent = isCredentialSharedWithParent;
+        this.mIsCredentialSharableWithParent = isCredentialSharableWithParent;
     }
 
     /**
@@ -323,8 +323,8 @@
      * Returns true if the user has shared encryption credential with parent user or
      * false otherwise.
      */
-    public boolean isCredentialSharedWithParent() {
-        return mIsCredentialSharedWithParent;
+    public boolean isCredentialSharableWithParent() {
+        return mIsCredentialSharableWithParent;
     }
 
     /** Returns a {@link Bundle} representing the default user restrictions. */
@@ -419,7 +419,7 @@
         private @DrawableRes int mBadgePlain = Resources.ID_NULL;
         private @DrawableRes int mBadgeNoBackground = Resources.ID_NULL;
         private boolean mIsMediaSharedWithParent = false;
-        private boolean mIsCredentialSharedWithParent = false;
+        private boolean mIsCredentialSharableWithParent = false;
 
         public Builder setName(String name) {
             mName = name;
@@ -521,10 +521,10 @@
 
         /**
          * Sets shared media property for the user.
-         * @param isCredentialSharedWithParent  the value to be set, true or false
+         * @param isCredentialSharableWithParent  the value to be set, true or false
          */
-        public Builder setIsCredentialSharedWithParent(boolean isCredentialSharedWithParent) {
-            mIsCredentialSharedWithParent = isCredentialSharedWithParent;
+        public Builder setIsCredentialSharableWithParent(boolean isCredentialSharableWithParent) {
+            mIsCredentialSharableWithParent = isCredentialSharableWithParent;
             return this;
         }
 
@@ -571,7 +571,7 @@
                     mDefaultSecureSettings,
                     mDefaultCrossProfileIntentFilters,
                     mIsMediaSharedWithParent,
-                    mIsCredentialSharedWithParent);
+                    mIsCredentialSharableWithParent);
         }
 
         private boolean hasBadge() {
diff --git a/services/core/java/com/android/server/pm/UserTypeFactory.java b/services/core/java/com/android/server/pm/UserTypeFactory.java
index 1e3b67c..cb18c6d 100644
--- a/services/core/java/com/android/server/pm/UserTypeFactory.java
+++ b/services/core/java/com/android/server/pm/UserTypeFactory.java
@@ -122,7 +122,7 @@
                 .setLabel(0)
                 .setDefaultRestrictions(null)
                 .setIsMediaSharedWithParent(true)
-                .setIsCredentialSharedWithParent(true);
+                .setIsCredentialSharableWithParent(true);
     }
 
     /**
@@ -154,7 +154,7 @@
                 .setDefaultRestrictions(getDefaultManagedProfileRestrictions())
                 .setDefaultSecureSettings(getDefaultManagedProfileSecureSettings())
                 .setDefaultCrossProfileIntentFilters(getDefaultManagedCrossProfileIntentFilter())
-                .setIsCredentialSharedWithParent(true);
+                .setIsCredentialSharableWithParent(true);
     }
 
     /**
diff --git a/services/core/java/com/android/server/pm/VerificationParams.java b/services/core/java/com/android/server/pm/VerificationParams.java
index bc93611..7423bf6 100644
--- a/services/core/java/com/android/server/pm/VerificationParams.java
+++ b/services/core/java/com/android/server/pm/VerificationParams.java
@@ -72,6 +72,7 @@
 import android.util.Slog;
 
 import com.android.server.DeviceIdleInternal;
+import com.android.server.sdksandbox.SdkSandboxManagerLocal;
 
 import java.io.File;
 import java.util.ArrayList;
@@ -441,9 +442,22 @@
         final long verificationTimeout = VerificationUtils.getVerificationTimeout(mPm.mContext,
                 streaming);
 
-        final List<ComponentName> sufficientVerifiers = matchVerifiers(pkgLite,
+        List<ComponentName> sufficientVerifiers = matchVerifiers(pkgLite,
                 receivers.getList(), verificationState);
 
+        // Add broadcastReceiver Component to verify Sdk before run in Sdk sandbox.
+        if (pkgLite.isSdkLibrary) {
+            if (sufficientVerifiers == null) {
+                sufficientVerifiers = new ArrayList<>();
+            }
+            ComponentName sdkSandboxComponentName = new ComponentName("android",
+                    SdkSandboxManagerLocal.VERIFIER_RECEIVER);
+            sufficientVerifiers.add(sdkSandboxComponentName);
+
+            // Add uid of system_server the same uid for SdkSandboxManagerService
+            verificationState.addSufficientVerifier(Process.myUid());
+        }
+
         DeviceIdleInternal idleController =
                 mPm.mInjector.getLocalService(DeviceIdleInternal.class);
         final BroadcastOptions options = BroadcastOptions.makeBasic();
diff --git a/services/core/java/com/android/server/pm/WatchedIntentFilter.java b/services/core/java/com/android/server/pm/WatchedIntentFilter.java
index 30f276e..5d7a2a3 100644
--- a/services/core/java/com/android/server/pm/WatchedIntentFilter.java
+++ b/services/core/java/com/android/server/pm/WatchedIntentFilter.java
@@ -84,7 +84,7 @@
     }
 
     // Convert an {@link IntentFilter} to a {@link WatchedIntentFilter}
-    protected WatchedIntentFilter(IntentFilter f) {
+    public WatchedIntentFilter(IntentFilter f) {
         mFilter = new IntentFilter(f);
     }
 
diff --git a/services/core/java/com/android/server/pm/dex/ArtManagerService.java b/services/core/java/com/android/server/pm/dex/ArtManagerService.java
index e28a6ea..7e4da94 100644
--- a/services/core/java/com/android/server/pm/dex/ArtManagerService.java
+++ b/services/core/java/com/android/server/pm/dex/ArtManagerService.java
@@ -31,13 +31,13 @@
 import android.content.pm.dex.DexMetadataHelper;
 import android.content.pm.dex.ISnapshotRuntimeProfileCallback;
 import android.content.pm.dex.PackageOptimizationInfo;
-import com.android.server.pm.pkg.parsing.PackageInfoWithoutStateUtils;
 import android.os.Binder;
 import android.os.Build;
 import android.os.Handler;
 import android.os.ParcelFileDescriptor;
 import android.os.Process;
 import android.os.RemoteException;
+import android.os.ServiceManager;
 import android.os.SystemProperties;
 import android.os.UserHandle;
 import android.system.Os;
@@ -55,6 +55,7 @@
 import com.android.server.pm.Installer.InstallerException;
 import com.android.server.pm.PackageManagerServiceCompilerMapping;
 import com.android.server.pm.parsing.pkg.AndroidPackage;
+import com.android.server.pm.pkg.parsing.PackageInfoWithoutStateUtils;
 
 import dalvik.system.DexFile;
 import dalvik.system.VMRuntime;
@@ -92,7 +93,7 @@
     private static final String BOOT_IMAGE_PROFILE_NAME = "android.prof";
 
     private final Context mContext;
-    private final IPackageManager mPackageManager;
+    private IPackageManager mPackageManager;
     private final Object mInstallLock;
     @GuardedBy("mInstallLock")
     private final Installer mInstaller;
@@ -103,10 +104,9 @@
         verifyTronLoggingConstants();
     }
 
-    public ArtManagerService(Context context, IPackageManager pm, Installer installer,
+    public ArtManagerService(Context context, Installer installer,
             Object installLock) {
         mContext = context;
-        mPackageManager = pm;
         mInstaller = installer;
         mInstallLock = installLock;
         mHandler = new Handler(BackgroundThread.getHandler().getLooper());
@@ -114,6 +114,15 @@
         LocalServices.addService(ArtManagerInternal.class, new ArtManagerInternalImpl());
     }
 
+    @NonNull
+    private IPackageManager getPackageManager() {
+        if (mPackageManager == null) {
+            mPackageManager = IPackageManager.Stub.asInterface(
+                    ServiceManager.getService("package"));
+        }
+        return mPackageManager;
+    }
+
     private boolean checkAndroidPermissions(int callingUid, String callingPackage) {
         // Callers always need this permission
         mContext.enforceCallingOrSelfPermission(
@@ -157,7 +166,7 @@
         }
         PackageInfo info = null;
         try {
-            info = mPackageManager.getPackageInfo(packageName, /*flags*/ 0, /*userId*/ 0);
+            info =  getPackageManager().getPackageInfo(packageName, /*flags*/ 0, /*userId*/ 0);
         } catch (RemoteException ignored) {
             // Should not happen.
         }
@@ -221,7 +230,7 @@
 
             // TODO(calin): consider adding an API to PMS which can retrieve the
             // PackageParser.Package.
-            info = mPackageManager.getPackageInfo(packageName, /*flags*/ 0, /*userId*/ 0);
+            info =  getPackageManager().getPackageInfo(packageName, /*flags*/ 0, /*userId*/ 0);
         } catch (RemoteException ignored) {
             // Should not happen.
         }
diff --git a/services/core/java/com/android/server/pm/dex/DexManager.java b/services/core/java/com/android/server/pm/dex/DexManager.java
index 5371454..17109e9 100644
--- a/services/core/java/com/android/server/pm/dex/DexManager.java
+++ b/services/core/java/com/android/server/pm/dex/DexManager.java
@@ -23,6 +23,8 @@
 
 import static java.util.function.Function.identity;
 
+import android.annotation.NonNull;
+import android.annotation.Nullable;
 import android.content.Context;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.IPackageManager;
@@ -33,6 +35,7 @@
 import android.os.FileUtils;
 import android.os.PowerManager;
 import android.os.RemoteException;
+import android.os.ServiceManager;
 import android.os.SystemProperties;
 import android.os.UserHandle;
 import android.os.storage.StorageManager;
@@ -109,7 +112,7 @@
     // record class loaders or ISAs.)
     private final DynamicCodeLogger mDynamicCodeLogger;
 
-    private final IPackageManager mPackageManager;
+    private IPackageManager mPackageManager;
     private final PackageDexOptimizer mPackageDexOptimizer;
     private final Object mInstallLock;
     @GuardedBy("mInstallLock")
@@ -128,16 +131,22 @@
     private static int DEX_SEARCH_FOUND_SPLIT = 2;  // dex file is a split apk
     private static int DEX_SEARCH_FOUND_SECONDARY = 3;  // dex file is a secondary dex
 
-    public DexManager(Context context, IPackageManager pms, PackageDexOptimizer pdo,
-            Installer installer, Object installLock) {
+    public DexManager(Context context, PackageDexOptimizer pdo, Installer installer,
+            Object installLock) {
+        this(context, pdo, installer, installLock, null);
+    }
+
+    @VisibleForTesting
+    public DexManager(Context context, PackageDexOptimizer pdo, Installer installer,
+            Object installLock, @Nullable IPackageManager packageManager) {
         mContext = context;
         mPackageCodeLocationsCache = new HashMap<>();
         mPackageDexUsage = new PackageDexUsage();
-        mPackageManager = pms;
         mPackageDexOptimizer = pdo;
         mInstaller = installer;
         mInstallLock = installLock;
-        mDynamicCodeLogger = new DynamicCodeLogger(pms, installer);
+        mDynamicCodeLogger = new DynamicCodeLogger(installer);
+        mPackageManager = packageManager;
 
         // This is currently checked to handle tests that pass in a null context.
         // TODO(b/174783329): Modify the tests to pass in a mocked Context, PowerManager,
@@ -157,6 +166,15 @@
         }
     }
 
+    @NonNull
+    private IPackageManager getPackageManager() {
+        if (mPackageManager == null) {
+            mPackageManager = IPackageManager.Stub.asInterface(
+                    ServiceManager.getService("package"));
+        }
+        return mPackageManager;
+    }
+
     public DynamicCodeLogger getDynamicCodeLogger() {
         return mDynamicCodeLogger;
     }
@@ -529,7 +547,7 @@
 
             PackageInfo pkg;
             try {
-                pkg = mPackageManager.getPackageInfo(packageName, /*flags*/0,
+                pkg = getPackageManager().getPackageInfo(packageName, /*flags*/0,
                     dexUseInfo.getOwnerUserId());
             } catch (RemoteException e) {
                 throw new AssertionError(e);
@@ -673,7 +691,7 @@
                 // to get back the real app uid and its storage kind. These are only used
                 // to perform extra validation in installd.
                 // TODO(calin): maybe a bit overkill.
-                pkg = mPackageManager.getPackageInfo(packageName, /*flags*/0,
+                pkg = getPackageManager().getPackageInfo(packageName, /*flags*/0,
                     dexUseInfo.getOwnerUserId());
             } catch (RemoteException ignore) {
                 // Can't happen, DexManager is local.
diff --git a/services/core/java/com/android/server/pm/dex/DynamicCodeLogger.java b/services/core/java/com/android/server/pm/dex/DynamicCodeLogger.java
index 75b4e38..9b94e99 100644
--- a/services/core/java/com/android/server/pm/dex/DynamicCodeLogger.java
+++ b/services/core/java/com/android/server/pm/dex/DynamicCodeLogger.java
@@ -19,11 +19,13 @@
 import static com.android.server.pm.dex.PackageDynamicCodeLoading.FILE_TYPE_DEX;
 import static com.android.server.pm.dex.PackageDynamicCodeLoading.FILE_TYPE_NATIVE;
 
+import android.annotation.NonNull;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.IPackageManager;
 import android.content.pm.PackageInfo;
 import android.os.FileUtils;
 import android.os.RemoteException;
+import android.os.ServiceManager;
 import android.os.UserHandle;
 import android.os.storage.StorageManager;
 import android.util.EventLog;
@@ -58,20 +60,30 @@
     private static final String DCL_DEX_SUBTAG = "dcl";
     private static final String DCL_NATIVE_SUBTAG = "dcln";
 
-    private final IPackageManager mPackageManager;
+    private IPackageManager mPackageManager;
     private final PackageDynamicCodeLoading mPackageDynamicCodeLoading;
     private final Installer mInstaller;
 
-    DynamicCodeLogger(IPackageManager pms, Installer installer) {
-        this(pms, installer, new PackageDynamicCodeLoading());
+    DynamicCodeLogger(Installer installer) {
+        mInstaller = installer;
+        mPackageDynamicCodeLoading = new PackageDynamicCodeLoading();
     }
 
     @VisibleForTesting
-    DynamicCodeLogger(IPackageManager pms, Installer installer,
-            PackageDynamicCodeLoading packageDynamicCodeLoading) {
-        mPackageManager = pms;
-        mPackageDynamicCodeLoading = packageDynamicCodeLoading;
+    DynamicCodeLogger(@NonNull IPackageManager packageManager, @NonNull Installer installer,
+            @NonNull PackageDynamicCodeLoading packageDynamicCodeLoading) {
+        mPackageManager = packageManager;
         mInstaller = installer;
+        mPackageDynamicCodeLoading = packageDynamicCodeLoading;
+    }
+
+    @NonNull
+    private IPackageManager getPackageManager() {
+        if (mPackageManager == null) {
+            mPackageManager = IPackageManager.Stub.asInterface(
+                    ServiceManager.getService("package"));
+        }
+        return mPackageManager;
     }
 
     public Set<String> getAllPackagesWithDynamicCodeLoading() {
@@ -104,7 +116,7 @@
 
                 try {
                     PackageInfo ownerInfo =
-                            mPackageManager.getPackageInfo(packageName, /*flags*/ 0, userId);
+                            getPackageManager().getPackageInfo(packageName, /*flags*/ 0, userId);
                     appInfo = ownerInfo == null ? null : ownerInfo.applicationInfo;
                 } catch (RemoteException ignored) {
                     // Can't happen, we're local.
@@ -167,7 +179,7 @@
                     loadingUid = appInfo.uid;
                 } else {
                     try {
-                        loadingUid = mPackageManager.getPackageUid(loadingPackageName, /*flags*/ 0,
+                        loadingUid =  getPackageManager().getPackageUid(loadingPackageName, /*flags*/ 0,
                                 userId);
                     } catch (RemoteException ignored) {
                         // Can't happen, we're local.
@@ -223,7 +235,7 @@
     public void recordNative(int loadingUid, String path) {
         String[] packages;
         try {
-            packages = mPackageManager.getPackagesForUid(loadingUid);
+            packages =  getPackageManager().getPackagesForUid(loadingUid);
             if (packages == null || packages.length == 0) {
                 return;
             }
diff --git a/services/core/java/com/android/server/pm/dex/OWNERS b/services/core/java/com/android/server/pm/dex/OWNERS
index 052a4ca..5ca8ddd 100644
--- a/services/core/java/com/android/server/pm/dex/OWNERS
+++ b/services/core/java/com/android/server/pm/dex/OWNERS
@@ -1,3 +1,4 @@
 alanstokes@google.com
 jiakaiz@google.com
 ngeoffray@google.com
+mast@google.com
diff --git a/services/core/java/com/android/server/pm/parsing/PackageInfoUtils.java b/services/core/java/com/android/server/pm/parsing/PackageInfoUtils.java
index 63469cb..8d1bcfc 100644
--- a/services/core/java/com/android/server/pm/parsing/PackageInfoUtils.java
+++ b/services/core/java/com/android/server/pm/parsing/PackageInfoUtils.java
@@ -122,6 +122,12 @@
         info.isStub = pkg.isStub();
         info.coreApp = pkg.isCoreApp();
 
+        if (pkgSetting != null && !pkgSetting.hasSharedUser()) {
+            // It is possible that this shared UID app has left
+            info.sharedUserId = null;
+            info.sharedUserLabel = 0;
+        }
+
         if ((flags & PackageManager.GET_ACTIVITIES) != 0) {
             final int N = pkg.getActivities().size();
             if (N > 0) {
diff --git a/services/core/java/com/android/server/pm/permission/PermissionManagerServiceImpl.java b/services/core/java/com/android/server/pm/permission/PermissionManagerServiceImpl.java
index 9084261..d060930 100644
--- a/services/core/java/com/android/server/pm/permission/PermissionManagerServiceImpl.java
+++ b/services/core/java/com/android/server/pm/permission/PermissionManagerServiceImpl.java
@@ -3291,10 +3291,13 @@
         } else if (pkg.isSystemExt()) {
             permissions = systemConfig.getSystemExtPrivAppPermissions(pkg.getPackageName());
         } else if (containingApexPackageName != null) {
+            final ApexManager apexManager = ApexManager.getInstance();
+            final String apexName = apexManager.getApexModuleNameForPackageName(
+                    containingApexPackageName);
             final Set<String> privAppPermissions = systemConfig.getPrivAppPermissions(
                     pkg.getPackageName());
             final Set<String> apexPermissions = systemConfig.getApexPrivAppPermissions(
-                    containingApexPackageName, pkg.getPackageName());
+                    apexName, pkg.getPackageName());
             if (privAppPermissions != null) {
                 // TODO(andreionea): Remove check as soon as all apk-in-apex
                 // permission allowlists are migrated.
diff --git a/services/core/java/com/android/server/pm/pkg/parsing/ParsingPackage.java b/services/core/java/com/android/server/pm/pkg/parsing/ParsingPackage.java
index cbba346..40f859c 100644
--- a/services/core/java/com/android/server/pm/pkg/parsing/ParsingPackage.java
+++ b/services/core/java/com/android/server/pm/pkg/parsing/ParsingPackage.java
@@ -302,6 +302,8 @@
 
     ParsingPackage setMinSdkVersion(int minSdkVersion);
 
+    ParsingPackage setMaxSdkVersion(int maxSdkVersion);
+
     ParsingPackage setNetworkSecurityConfigRes(int networkSecurityConfigRes);
 
     ParsingPackage setNonLocalizedLabel(CharSequence nonLocalizedLabel);
diff --git a/services/core/java/com/android/server/pm/pkg/parsing/ParsingPackageImpl.java b/services/core/java/com/android/server/pm/pkg/parsing/ParsingPackageImpl.java
index 1484df8..67d9aec 100644
--- a/services/core/java/com/android/server/pm/pkg/parsing/ParsingPackageImpl.java
+++ b/services/core/java/com/android/server/pm/pkg/parsing/ParsingPackageImpl.java
@@ -383,6 +383,7 @@
     @Nullable
     private SparseIntArray minExtensionVersions;
     private int minSdkVersion = ParsingUtils.DEFAULT_MIN_SDK_VERSION;
+    private int maxSdkVersion = ParsingUtils.DEFAULT_MAX_SDK_VERSION;
     private int networkSecurityConfigRes;
     @Nullable
     private CharSequence nonLocalizedLabel;
@@ -1306,6 +1307,7 @@
         dest.writeFloat(this.maxAspectRatio);
         dest.writeFloat(this.minAspectRatio);
         dest.writeInt(this.minSdkVersion);
+        dest.writeInt(this.maxSdkVersion);
         dest.writeInt(this.networkSecurityConfigRes);
         dest.writeCharSequence(this.nonLocalizedLabel);
         dest.writeString(this.permission);
@@ -1454,6 +1456,7 @@
         this.maxAspectRatio = in.readFloat();
         this.minAspectRatio = in.readFloat();
         this.minSdkVersion = in.readInt();
+        this.maxSdkVersion = in.readInt();
         this.networkSecurityConfigRes = in.readInt();
         this.nonLocalizedLabel = in.readCharSequence();
         this.permission = in.readString();
@@ -2068,6 +2071,11 @@
     }
 
     @Override
+    public int getMaxSdkVersion() {
+        return maxSdkVersion;
+    }
+
+    @Override
     public int getNetworkSecurityConfigRes() {
         return networkSecurityConfigRes;
     }
@@ -2592,6 +2600,12 @@
     }
 
     @Override
+    public ParsingPackageImpl setMaxSdkVersion(int value) {
+        maxSdkVersion = value;
+        return this;
+    }
+
+    @Override
     public ParsingPackageImpl setNetworkSecurityConfigRes(int value) {
         networkSecurityConfigRes = value;
         return this;
diff --git a/services/core/java/com/android/server/pm/pkg/parsing/ParsingPackageUtils.java b/services/core/java/com/android/server/pm/pkg/parsing/ParsingPackageUtils.java
index 112b9e0..b323948 100644
--- a/services/core/java/com/android/server/pm/pkg/parsing/ParsingPackageUtils.java
+++ b/services/core/java/com/android/server/pm/pkg/parsing/ParsingPackageUtils.java
@@ -209,6 +209,8 @@
 
     public static final int SDK_VERSION = Build.VERSION.SDK_INT;
     public static final String[] SDK_CODENAMES = Build.VERSION.ACTIVE_CODENAMES;
+    public static final String[] PREVIOUS_CODENAMES =
+            Build.VERSION.KNOWN_CODENAMES.toArray(new String[]{});
 
     public static boolean sCompatibilityModeEnabled = true;
     public static boolean sUseRoundIcon = false;
@@ -236,6 +238,7 @@
      */
     public static final int PARSE_IGNORE_OVERLAY_REQUIRED_SYSTEM_PROPERTY = 1 << 7;
     public static final int PARSE_FRAMEWORK_RES_SPLITS = 1 << 8;
+    public static final int PARSE_CHECK_MAX_SDK_VERSION = 1 << 9;
 
     public static final int PARSE_CHATTY = 1 << 31;
 
@@ -1002,7 +1005,7 @@
             case TAG_FEATURE_GROUP:
                 return parseFeatureGroup(input, pkg, res, parser);
             case TAG_USES_SDK:
-                return parseUsesSdk(input, pkg, res, parser);
+                return parseUsesSdk(input, pkg, res, parser, flags);
             case TAG_SUPPORT_SCREENS:
                 return parseSupportScreens(input, pkg, res, parser);
             case TAG_PROTECTED_BROADCAST:
@@ -1514,15 +1517,17 @@
     }
 
     private static ParseResult<ParsingPackage> parseUsesSdk(ParseInput input,
-            ParsingPackage pkg, Resources res, XmlResourceParser parser)
+            ParsingPackage pkg, Resources res, XmlResourceParser parser, int flags)
             throws IOException, XmlPullParserException {
         if (SDK_VERSION > 0) {
+            final boolean checkMaxSdkVersion = (flags & PARSE_CHECK_MAX_SDK_VERSION) != 0;
             TypedArray sa = res.obtainAttributes(parser, R.styleable.AndroidManifestUsesSdk);
             try {
                 int minVers = ParsingUtils.DEFAULT_MIN_SDK_VERSION;
                 String minCode = null;
                 int targetVers = ParsingUtils.DEFAULT_TARGET_SDK_VERSION;
                 String targetCode = null;
+                int maxVers = Integer.MAX_VALUE;
 
                 TypedValue val = sa.peekValue(R.styleable.AndroidManifestUsesSdk_minSdkVersion);
                 if (val != null) {
@@ -1550,6 +1555,14 @@
                     targetCode = minCode;
                 }
 
+                if (checkMaxSdkVersion) {
+                    val = sa.peekValue(R.styleable.AndroidManifestUsesSdk_maxSdkVersion);
+                    if (val != null) {
+                        // maxSdkVersion only supports integer
+                        maxVers = val.data;
+                    }
+                }
+
                 ParseResult<Integer> targetSdkVersionResult = FrameworkParsingPackageUtils
                         .computeTargetSdkVersion(targetVers, targetCode, SDK_CODENAMES, input);
                 if (targetSdkVersionResult.isError()) {
@@ -1574,6 +1587,15 @@
 
                 pkg.setMinSdkVersion(minSdkVersion)
                         .setTargetSdkVersion(targetSdkVersion);
+                if (checkMaxSdkVersion) {
+                    ParseResult<Integer> maxSdkVersionResult = FrameworkParsingPackageUtils
+                            .computeMaxSdkVersion(maxVers, SDK_VERSION, input);
+                    if (maxSdkVersionResult.isError()) {
+                        return input.error(maxSdkVersionResult);
+                    }
+                    int maxSdkVersion = maxSdkVersionResult.getResult();
+                    pkg.setMaxSdkVersion(maxSdkVersion);
+                }
 
                 int type;
                 final int innerDepth = parser.getDepth();
diff --git a/services/core/java/com/android/server/pm/pkg/parsing/ParsingUtils.java b/services/core/java/com/android/server/pm/pkg/parsing/ParsingUtils.java
index cb474df..0751285 100644
--- a/services/core/java/com/android/server/pm/pkg/parsing/ParsingUtils.java
+++ b/services/core/java/com/android/server/pm/pkg/parsing/ParsingUtils.java
@@ -50,6 +50,7 @@
     public static final String ANDROID_RES_NAMESPACE = "http://schemas.android.com/apk/res/android";
 
     public static final int DEFAULT_MIN_SDK_VERSION = 1;
+    public static final int DEFAULT_MAX_SDK_VERSION = Integer.MAX_VALUE;
     public static final int DEFAULT_TARGET_SDK_VERSION = 0;
 
     public static final int NOT_SET = -1;
diff --git a/services/core/java/com/android/server/pm/pkg/parsing/PkgWithoutStateAppInfo.java b/services/core/java/com/android/server/pm/pkg/parsing/PkgWithoutStateAppInfo.java
index 3205b76..99bcdb96 100644
--- a/services/core/java/com/android/server/pm/pkg/parsing/PkgWithoutStateAppInfo.java
+++ b/services/core/java/com/android/server/pm/pkg/parsing/PkgWithoutStateAppInfo.java
@@ -250,6 +250,11 @@
     int getMinSdkVersion();
 
     /**
+     * @see R.styleable#AndroidManifestUsesSdk_maxSdkVersion
+     */
+    int getMaxSdkVersion();
+
+    /**
      * @see ApplicationInfo#getNativeHeapZeroInitialized()
      * @see R.styleable#AndroidManifestApplication_nativeHeapZeroInitialized
      */
diff --git a/services/core/java/com/android/server/policy/PhoneWindowManager.java b/services/core/java/com/android/server/policy/PhoneWindowManager.java
index e79148d..dc5b325 100644
--- a/services/core/java/com/android/server/policy/PhoneWindowManager.java
+++ b/services/core/java/com/android/server/policy/PhoneWindowManager.java
@@ -792,7 +792,7 @@
         public void onWakeUp() {
             synchronized (mLock) {
                 if (shouldEnableWakeGestureLp()
-                        && mBatteryManagerInternal.getPlugType() != BATTERY_PLUGGED_WIRELESS) {
+                        && getBatteryManagerInternal().getPlugType() != BATTERY_PLUGGED_WIRELESS) {
                     performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, false,
                             "Wake Up");
                     wakeUp(SystemClock.uptimeMillis(), mAllowTheaterModeWakeFromWakeGesture,
diff --git a/services/core/java/com/android/server/slice/SliceManagerService.java b/services/core/java/com/android/server/slice/SliceManagerService.java
index ee0e5ba..e3dcfd0 100644
--- a/services/core/java/com/android/server/slice/SliceManagerService.java
+++ b/services/core/java/com/android/server/slice/SliceManagerService.java
@@ -247,6 +247,8 @@
         if (autoGrantPermissions != null && callingPkg != null) {
             // Need to own the Uri to call in with permissions to grant.
             enforceOwner(callingPkg, uri, userId);
+            // b/208232850: Needs to verify caller before granting slice access
+            verifyCaller(callingPkg);
             for (String perm : autoGrantPermissions) {
                 if (mContext.checkPermission(perm, pid, uid) == PERMISSION_GRANTED) {
                     int providerUser = ContentProvider.getUserIdFromUri(uri, userId);
diff --git a/services/core/java/com/android/server/soundtrigger_middleware/SoundTriggerHalConcurrentCaptureHandler.java b/services/core/java/com/android/server/soundtrigger_middleware/SoundTriggerHalConcurrentCaptureHandler.java
index 990b21c..1cc0539 100644
--- a/services/core/java/com/android/server/soundtrigger_middleware/SoundTriggerHalConcurrentCaptureHandler.java
+++ b/services/core/java/com/android/server/soundtrigger_middleware/SoundTriggerHalConcurrentCaptureHandler.java
@@ -20,6 +20,7 @@
 import android.media.permission.SafeCloseable;
 import android.media.soundtrigger.ModelParameterRange;
 import android.media.soundtrigger.PhraseRecognitionEvent;
+import android.media.soundtrigger.PhraseRecognitionExtra;
 import android.media.soundtrigger.PhraseSoundModel;
 import android.media.soundtrigger.Properties;
 import android.media.soundtrigger.RecognitionConfig;
@@ -392,7 +393,7 @@
     private static void notifyAbort(int modelHandle, LoadedModel model) {
         switch (model.type) {
             case SoundModelType.GENERIC: {
-                RecognitionEvent event = new RecognitionEvent();
+                RecognitionEvent event = newEmptyRecognitionEvent();
                 event.status = RecognitionStatus.ABORTED;
                 event.type = SoundModelType.GENERIC;
                 model.callback.recognitionCallback(modelHandle, event);
@@ -400,7 +401,7 @@
             break;
 
             case SoundModelType.KEYPHRASE: {
-                PhraseRecognitionEvent event = new PhraseRecognitionEvent();
+                PhraseRecognitionEvent event = newEmptyPhraseRecognitionEvent();
                 event.common.status = RecognitionStatus.ABORTED;
                 event.common.type = SoundModelType.KEYPHRASE;
                 model.callback.phraseRecognitionCallback(modelHandle, event);
@@ -415,6 +416,19 @@
         mNotifier.unregisterListener(this);
     }
 
+    private static PhraseRecognitionEvent newEmptyPhraseRecognitionEvent() {
+        PhraseRecognitionEvent result = new PhraseRecognitionEvent();
+        result.common = newEmptyRecognitionEvent();
+        result.phraseExtras = new PhraseRecognitionExtra[0];
+        return result;
+    }
+
+    private static RecognitionEvent newEmptyRecognitionEvent() {
+        RecognitionEvent result = new RecognitionEvent();
+        result.data = new byte[0];
+        return result;
+    }
+
     ////////////////////////////////////////////////////////////////////////////////////////////////
     // All methods below do trivial delegation - no interesting logic.
     @Override
diff --git a/services/core/java/com/android/server/statusbar/StatusBarManagerService.java b/services/core/java/com/android/server/statusbar/StatusBarManagerService.java
index 59b9daf..8087738 100644
--- a/services/core/java/com/android/server/statusbar/StatusBarManagerService.java
+++ b/services/core/java/com/android/server/statusbar/StatusBarManagerService.java
@@ -36,6 +36,7 @@
 import android.app.StatusBarManager;
 import android.app.compat.CompatChanges;
 import android.compat.annotation.ChangeId;
+import android.compat.annotation.EnabledAfter;
 import android.compat.annotation.EnabledSince;
 import android.content.ComponentName;
 import android.content.Context;
@@ -135,6 +136,17 @@
     @EnabledSince(targetSdkVersion = Build.VERSION_CODES.S)
     private static final long LOCK_DOWN_COLLAPSE_STATUS_BAR = 173031413L;
 
+    /**
+     * In apps targeting {@link android.os.Build.VERSION_CODES#TIRAMISU} or higher, calling
+     * {@link android.service.quicksettings.TileService#requestListeningState} will check that the 
+     * calling package (uid) and the package of the target {@link android.content.ComponentName} 
+     * match. It'll also make sure that the context used can take actions on behalf of the current 
+     * user.
+     */
+    @ChangeId
+    @EnabledAfter(targetSdkVersion = Build.VERSION_CODES.S_V2)
+    static final long REQUEST_LISTENING_MUST_MATCH_PACKAGE = 172251878L;
+
     private final Context mContext;
 
     private final Handler mHandler = new Handler();
@@ -1652,6 +1664,7 @@
 
     @Override
     public void hideCurrentInputMethodForBubbles() {
+        enforceStatusBarService();
         final long token = Binder.clearCallingIdentity();
         try {
             InputMethodManagerInternal.get().hideCurrentInputMethod(
@@ -1776,6 +1789,42 @@
     }
 
     @Override
+    public void requestTileServiceListeningState(
+            @NonNull ComponentName componentName,
+            int userId
+    ) {
+        int callingUid = Binder.getCallingUid();
+        String packageName = componentName.getPackageName();
+
+        boolean mustPerformChecks = CompatChanges.isChangeEnabled(
+                REQUEST_LISTENING_MUST_MATCH_PACKAGE, callingUid);
+
+        if (mustPerformChecks) {
+            // Check calling user can act on behalf of current user
+            userId = mActivityManagerInternal.handleIncomingUser(Binder.getCallingPid(), callingUid,
+                    userId, false, ActivityManagerInternal.ALLOW_NON_FULL,
+                    "requestTileServiceListeningState", packageName);
+
+            // Check calling uid matches package
+            checkCallingUidPackage(packageName, callingUid, userId);
+
+            int currentUser = mActivityManagerInternal.getCurrentUserId();
+
+            // Check current user
+            if (userId != currentUser) {
+                throw new IllegalArgumentException("User " + userId + " is not the current user.");
+            }
+        }
+        if (mBar != null) {
+            try {
+                mBar.requestTileServiceListeningState(componentName);
+            } catch (RemoteException e) {
+                Slog.e(TAG, "requestTileServiceListeningState", e);
+            }
+        }
+    }
+
+    @Override
     public void requestAddTile(
             @NonNull ComponentName componentName,
             @NonNull CharSequence label,
diff --git a/services/core/java/com/android/server/trust/TEST_MAPPING b/services/core/java/com/android/server/trust/TEST_MAPPING
new file mode 100644
index 0000000..be8ed67
--- /dev/null
+++ b/services/core/java/com/android/server/trust/TEST_MAPPING
@@ -0,0 +1,15 @@
+{
+    "presubmit": [
+      {
+        "name": "TrustTests",
+        "options": [
+          {
+            "include-filter": "android.trust.test"
+          },
+          {
+            "exclude-annotation": "androidx.test.filters.FlakyTest"
+          }
+        ]
+      }
+    ]
+  }
\ No newline at end of file
diff --git a/services/core/java/com/android/server/trust/TrustManagerService.java b/services/core/java/com/android/server/trust/TrustManagerService.java
index bd4b8d1..cc1d0e2 100644
--- a/services/core/java/com/android/server/trust/TrustManagerService.java
+++ b/services/core/java/com/android/server/trust/TrustManagerService.java
@@ -715,7 +715,7 @@
                     (disabledFeatures & DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS) != 0;
 
             List<ComponentName> enabledAgents = lockPatternUtils.getEnabledTrustAgents(userInfo.id);
-            if (enabledAgents == null) {
+            if (enabledAgents.isEmpty()) {
                 if (DEBUG) Slog.d(TAG, "refreshAgentList: skipping user " + userInfo.id
                         + ": no agents enabled by user");
                 continue;
@@ -1080,9 +1080,7 @@
         }
 
         List<ComponentName> previouslyEnabledAgents = utils.getEnabledTrustAgents(userId);
-        if (previouslyEnabledAgents != null) {
-            discoveredAgents.addAll(previouslyEnabledAgents);
-        }
+        discoveredAgents.addAll(previouslyEnabledAgents);
         utils.setEnabledTrustAgents(discoveredAgents, userId);
         Settings.Secure.putIntForUser(mContext.getContentResolver(),
                 Settings.Secure.TRUST_AGENTS_INITIALIZED, 1, userId);
diff --git a/services/core/java/com/android/server/vcn/TelephonySubscriptionTracker.java b/services/core/java/com/android/server/vcn/TelephonySubscriptionTracker.java
index 30e2617..092853f 100644
--- a/services/core/java/com/android/server/vcn/TelephonySubscriptionTracker.java
+++ b/services/core/java/com/android/server/vcn/TelephonySubscriptionTracker.java
@@ -39,7 +39,7 @@
 import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener;
 import android.telephony.TelephonyCallback;
 import android.telephony.TelephonyManager;
-import android.telephony.TelephonyManager.CarrierPrivilegesListener;
+import android.telephony.TelephonyManager.CarrierPrivilegesCallback;
 import android.util.ArrayMap;
 import android.util.ArraySet;
 import android.util.Slog;
@@ -98,8 +98,7 @@
     @NonNull private final OnSubscriptionsChangedListener mSubscriptionChangedListener;
 
     @NonNull
-    private final List<CarrierPrivilegesListener> mCarrierPrivilegesChangedListeners =
-            new ArrayList<>();
+    private final List<CarrierPrivilegesCallback> mCarrierPrivilegesCallbacks = new ArrayList<>();
 
     @NonNull private TelephonySubscriptionSnapshot mCurrentSnapshot;
 
@@ -151,20 +150,21 @@
                 executor, mSubscriptionChangedListener);
         mTelephonyManager.registerTelephonyCallback(executor, mActiveDataSubIdListener);
 
-        registerCarrierPrivilegesListeners();
+        registerCarrierPrivilegesCallbacks();
     }
 
-    private void registerCarrierPrivilegesListeners() {
+    // TODO(b/221306368): Refactor with the new onCarrierServiceChange in the new CPCallback
+    private void registerCarrierPrivilegesCallbacks() {
         final HandlerExecutor executor = new HandlerExecutor(mHandler);
         final int modemCount = mTelephonyManager.getActiveModemCount();
         try {
             for (int i = 0; i < modemCount; i++) {
-                CarrierPrivilegesListener carrierPrivilegesListener =
-                        new CarrierPrivilegesListener() {
+                CarrierPrivilegesCallback carrierPrivilegesCallback =
+                        new CarrierPrivilegesCallback() {
                             @Override
                             public void onCarrierPrivilegesChanged(
-                                    @NonNull List<String> privilegedPackageNames,
-                                    @NonNull int[] privilegedUids) {
+                                    @NonNull Set<String> privilegedPackageNames,
+                                    @NonNull Set<Integer> privilegedUids) {
                                 // Re-trigger the synchronous check (which is also very cheap due
                                 // to caching in CarrierPrivilegesTracker). This allows consistency
                                 // with the onSubscriptionsChangedListener and broadcasts.
@@ -172,9 +172,9 @@
                             }
                         };
 
-                mTelephonyManager.addCarrierPrivilegesListener(
-                        i, executor, carrierPrivilegesListener);
-                mCarrierPrivilegesChangedListeners.add(carrierPrivilegesListener);
+                mTelephonyManager.registerCarrierPrivilegesCallback(
+                        i, executor, carrierPrivilegesCallback);
+                mCarrierPrivilegesCallbacks.add(carrierPrivilegesCallback);
             }
         } catch (IllegalArgumentException e) {
             Slog.wtf(TAG, "Encounted exception registering carrier privileges listeners", e);
@@ -191,15 +191,15 @@
         mSubscriptionManager.removeOnSubscriptionsChangedListener(mSubscriptionChangedListener);
         mTelephonyManager.unregisterTelephonyCallback(mActiveDataSubIdListener);
 
-        unregisterCarrierPrivilegesListeners();
+        unregisterCarrierPrivilegesCallbacks();
     }
 
-    private void unregisterCarrierPrivilegesListeners() {
-        for (CarrierPrivilegesListener carrierPrivilegesListener :
-                mCarrierPrivilegesChangedListeners) {
-            mTelephonyManager.removeCarrierPrivilegesListener(carrierPrivilegesListener);
+    private void unregisterCarrierPrivilegesCallbacks() {
+        for (CarrierPrivilegesCallback carrierPrivilegesCallback :
+                mCarrierPrivilegesCallbacks) {
+            mTelephonyManager.unregisterCarrierPrivilegesCallback(carrierPrivilegesCallback);
         }
-        mCarrierPrivilegesChangedListeners.clear();
+        mCarrierPrivilegesCallbacks.clear();
     }
 
     /**
@@ -283,7 +283,7 @@
     }
 
     private void handleActionMultiSimConfigChanged(Context context, Intent intent) {
-        unregisterCarrierPrivilegesListeners();
+        unregisterCarrierPrivilegesCallbacks();
 
         // Clear invalid slotIds from the mReadySubIdsBySlotId map.
         final int modemCount = mTelephonyManager.getActiveModemCount();
@@ -296,7 +296,7 @@
             }
         }
 
-        registerCarrierPrivilegesListeners();
+        registerCarrierPrivilegesCallbacks();
         handleSubscriptionsChanged();
     }
 
diff --git a/services/core/java/com/android/server/wm/ActivityClientController.java b/services/core/java/com/android/server/wm/ActivityClientController.java
index a4a200d..543e44c 100644
--- a/services/core/java/com/android/server/wm/ActivityClientController.java
+++ b/services/core/java/com/android/server/wm/ActivityClientController.java
@@ -757,12 +757,12 @@
     }
 
     @Override
-    public void setPreferDockBigOverlays(IBinder token, boolean preferDockBigOverlays) {
+    public void setShouldDockBigOverlays(IBinder token, boolean shouldDockBigOverlays) {
         final long origId = Binder.clearCallingIdentity();
         try {
             synchronized (mGlobalLock) {
                 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
-                r.setPreferDockBigOverlays(preferDockBigOverlays);
+                r.setShouldDockBigOverlays(shouldDockBigOverlays);
             }
         } finally {
             Binder.restoreCallingIdentity(origId);
diff --git a/services/core/java/com/android/server/wm/ActivityMetricsLogger.java b/services/core/java/com/android/server/wm/ActivityMetricsLogger.java
index a8dd856..c0cdec9 100644
--- a/services/core/java/com/android/server/wm/ActivityMetricsLogger.java
+++ b/services/core/java/com/android/server/wm/ActivityMetricsLogger.java
@@ -657,7 +657,7 @@
         for (int i = mTransitionInfoList.size() - 2; i >= 0; i--) {
             final TransitionInfo prevInfo = mTransitionInfoList.get(i);
             if (prevInfo.mIsDrawn || !prevInfo.mLastLaunchedActivity.mVisibleRequested) {
-                abort(prevInfo, "nothing will be drawn");
+                scheduleCheckActivityToBeDrawn(prevInfo.mLastLaunchedActivity, 0 /* delay */);
             }
         }
     }
@@ -757,6 +757,10 @@
     /** Makes sure that the reference to the removed activity is cleared. */
     void notifyActivityRemoved(@NonNull ActivityRecord r) {
         mLastTransitionInfo.remove(r);
+        final TransitionInfo info = getActiveTransitionInfo(r);
+        if (info != null) {
+            abort(info, "removed");
+        }
 
         final int packageUid = r.info.applicationInfo.uid;
         final PackageCompatStateInfo compatStateInfo = mPackageUidToCompatStateInfo.get(packageUid);
diff --git a/services/core/java/com/android/server/wm/ActivityRecord.java b/services/core/java/com/android/server/wm/ActivityRecord.java
index 1c93abd..883ce99 100644
--- a/services/core/java/com/android/server/wm/ActivityRecord.java
+++ b/services/core/java/com/android/server/wm/ActivityRecord.java
@@ -534,7 +534,7 @@
         // activity can enter picture in picture while pausing (only when switching to another task)
     PictureInPictureParams pictureInPictureArgs = new PictureInPictureParams.Builder().build();
         // The PiP params used when deferring the entering of picture-in-picture.
-    boolean preferDockBigOverlays;
+    boolean shouldDockBigOverlays;
     int launchCount;        // count of launches since last state
     long lastLaunchTime;    // time of last launch of this activity
     ComponentName requestedVrComponent; // the requested component for handling VR mode.
@@ -2042,7 +2042,7 @@
         mLetterboxUiController = new LetterboxUiController(mWmService, this);
         mCameraCompatControlEnabled = mWmService.mContext.getResources()
                 .getBoolean(R.bool.config_isCameraCompatControlForStretchedIssuesEnabled);
-        preferDockBigOverlays = mWmService.mContext.getResources()
+        shouldDockBigOverlays = mWmService.mContext.getResources()
                 .getBoolean(R.bool.config_dockBigOverlayWindows);
 
         if (_createTime > 0) {
@@ -2476,7 +2476,8 @@
     }
 
     private boolean transferSplashScreenIfNeeded() {
-        if (!mHandleExitSplashScreen || mStartingSurface == null || mStartingWindow == null
+        if (finishing || !mHandleExitSplashScreen || mStartingSurface == null
+                || mStartingWindow == null
                 || mTransferringSplashScreenState == TRANSFER_SPLASH_SCREEN_FINISH) {
             return false;
         }
@@ -2914,7 +2915,9 @@
      */
     boolean supportsPictureInPicture() {
         return mAtmService.mSupportsPictureInPicture && isActivityTypeStandardOrUndefined()
-                && info.supportsPictureInPicture();
+                && info.supportsPictureInPicture()
+                && (mDisplayContent != null && mDisplayContent.mDwpcHelper.isWindowingModeSupported(
+                WINDOWING_MODE_PINNED));
     }
 
     /**
@@ -6767,7 +6770,8 @@
             }
             // Choose the default behavior for Launcher and SystemUI when the SplashScreen style is
             // not specified in the ActivityOptions.
-            if (mLaunchSourceType == LAUNCH_SOURCE_TYPE_HOME) {
+            if (mLaunchSourceType == LAUNCH_SOURCE_TYPE_HOME
+                    || launchedFromUid == Process.SHELL_UID) {
                 return false;
             } else if (mLaunchSourceType == LAUNCH_SOURCE_TYPE_SYSTEMUI) {
                 return true;
@@ -6787,7 +6791,8 @@
         // solid color splash screen.
         // Need to check sourceRecord before in case this activity is launched from service.
         return !startActivity || !(mLaunchSourceType == LAUNCH_SOURCE_TYPE_SYSTEM
-                || mLaunchSourceType == LAUNCH_SOURCE_TYPE_HOME);
+                || mLaunchSourceType == LAUNCH_SOURCE_TYPE_HOME
+                || launchedFromUid == Process.SHELL_UID);
     }
 
     private int getSplashscreenTheme(ActivityOptions options) {
@@ -9573,9 +9578,9 @@
         getTask().getRootTask().onPictureInPictureParamsChanged();
     }
 
-    void setPreferDockBigOverlays(boolean preferDockBigOverlays) {
-        this.preferDockBigOverlays = preferDockBigOverlays;
-        getTask().getRootTask().onPreferDockBigOverlaysChanged();
+    void setShouldDockBigOverlays(boolean shouldDockBigOverlays) {
+        this.shouldDockBigOverlays = shouldDockBigOverlays;
+        getTask().getRootTask().onShouldDockBigOverlaysChanged();
     }
 
     @Override
diff --git a/services/core/java/com/android/server/wm/ActivityStarter.java b/services/core/java/com/android/server/wm/ActivityStarter.java
index ac121a1..7d2dfa0 100644
--- a/services/core/java/com/android/server/wm/ActivityStarter.java
+++ b/services/core/java/com/android/server/wm/ActivityStarter.java
@@ -2023,22 +2023,12 @@
     private boolean canEmbedActivity(@NonNull TaskFragment taskFragment,
             @NonNull ActivityRecord starting, boolean newTask, Task targetTask) {
         final Task hostTask = taskFragment.getTask();
-        if (hostTask == null) {
+        // Not allowed embedding a separate task or without host task.
+        if (hostTask == null || newTask || targetTask != hostTask) {
             return false;
         }
 
-        // Allowing the embedding if the task is owned by system.
-        final int hostUid = hostTask.effectiveUid;
-        if (UserHandle.getAppId(hostUid) == Process.SYSTEM_UID) {
-            return true;
-        }
-
-        if (!taskFragment.isAllowedToEmbedActivity(starting)) {
-            return false;
-        }
-
-        // Not allowed embedding task.
-        return !newTask && (targetTask == null || targetTask == hostTask);
+        return taskFragment.isAllowedToEmbedActivity(starting);
     }
 
     /**
diff --git a/services/core/java/com/android/server/wm/ActivityTaskManagerService.java b/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
index 6d8b3b1d..b5312c4 100644
--- a/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
+++ b/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
@@ -205,8 +205,6 @@
 import android.os.UserHandle;
 import android.os.UserManager;
 import android.os.WorkSource;
-import android.os.storage.IStorageManager;
-import android.os.storage.StorageManager;
 import android.provider.Settings;
 import android.service.dreams.DreamActivity;
 import android.service.dreams.DreamManagerInternal;
@@ -4300,11 +4298,6 @@
             SystemProperties.set("persist.sys.locale",
                     locales.get(bestLocaleIndex).toLanguageTag());
             LocaleList.setDefault(locales, bestLocaleIndex);
-
-            final Message m = PooledLambda.obtainMessage(
-                    ActivityTaskManagerService::sendLocaleToMountDaemonMsg, this,
-                    locales.get(bestLocaleIndex));
-            mH.sendMessage(m);
         }
 
         mTempConfig.seq = increaseConfigurationSeqLocked();
@@ -4458,17 +4451,6 @@
         Settings.System.putConfigurationForUser(resolver, config, userId);
     }
 
-    private void sendLocaleToMountDaemonMsg(Locale l) {
-        try {
-            IBinder service = ServiceManager.getService("mount");
-            IStorageManager storageManager = IStorageManager.Stub.asInterface(service);
-            Log.d(TAG, "Storing locale " + l.toLanguageTag() + " for decryption UI");
-            storageManager.setField(StorageManager.SYSTEM_LOCALE_KEY, l.toLanguageTag());
-        } catch (RemoteException e) {
-            Log.e(TAG, "Error storing locale for decryption UI", e);
-        }
-    }
-
     private void expireStartAsCallerTokenMsg(IBinder permissionToken) {
         mStartActivitySources.remove(permissionToken);
         mExpiredStartAsCallerTokens.add(permissionToken);
diff --git a/services/core/java/com/android/server/wm/ActivityTaskSupervisor.java b/services/core/java/com/android/server/wm/ActivityTaskSupervisor.java
index 64f4261..eb5ca9c 100644
--- a/services/core/java/com/android/server/wm/ActivityTaskSupervisor.java
+++ b/services/core/java/com/android/server/wm/ActivityTaskSupervisor.java
@@ -402,7 +402,8 @@
                     activities.add(r.info);
                 });
             }
-            if (!displayContent.mDwpcHelper.canContainActivities(activities)) {
+            if (!displayContent.mDwpcHelper.canContainActivities(activities,
+                    displayContent.getWindowingMode())) {
                 return false;
             }
         }
diff --git a/services/core/java/com/android/server/wm/BLASTSync.md b/services/core/java/com/android/server/wm/BLASTSync.md
new file mode 100644
index 0000000..2f39d6d
--- /dev/null
+++ b/services/core/java/com/android/server/wm/BLASTSync.md
@@ -0,0 +1,108 @@
+= What does it mean for BLAST Sync to work? =
+There are two BLAST sync primitives on the server side, BLASTSyncEngine and applyWithNextDraw.
+Both of them are used to solve subclasses of this category of problem:
+ 1. You have some server side changes, which will trigger both WM/SysUI initiated SurfaceControl.Transactions, 
+    and also trigger a client redraw/update
+ 2. You want to synchronize the redraw of those clients with the application of those WM/SysUI side changes.
+
+Put simply, you would like to synchronize the graphical effects of some WM changes with the graphical output of various windows
+observing those changes.
+
+To talk about exactly what the primitives guarantee, we need to clarify what we mean by server side changes. 
+In this document we will use a term syncable state to refer to any state mutated under the WindowManager lock
+which when observed by the client, produces a visible outcome in the produced frame. 
+For example the current Configuration. 
+
+The guarantee provided by Server-side BLAST Sync Primitives is thus:
+Guarantee 1:	If you make a set of changes to syncable state, at the same time that you begin a sync, 
+then the first frame drawn by the client after observing the syncable state will be sent in a transaction
+to the consumer of the sync which was begun, rather than directly sent to SurfaceFlinger.
+
+Here "at the same time" means in the same critical section (while holding the WM lock)
+For example this guarantee means that you can write code like:
+	window.performConfigurationChange(someConfiguration)
+	window.applyOnNextDraw(consumer)
+And the consumer will always be passed the first frame containing the configuration change. This can work with any 
+syncable state not just Configuration.
+
+The following is the protocol and additional requirements for BLAST sync, and an analysis of why it is correct. Due to time
+constraints we analyze it for a single window only (as this is actually the hard part).
+
+Protocol requirements:
+    Server protocol, precondition, begin a syncSeqId integer per window at 0:
+        SA: Enter the critical section
+        SB: Change syncable state, any number of times, prepare any number of syncs (and
+            increment the seqId if a sync was prepared, assosciate it with the sync)
+        SC: Leave the critical section
+        SD: Send syncable state updates to the client, always paired with the current seqId
+        SE: When the client calls finishDrawing, execute the consumer for each sync with
+            seqId <= the seqId which the client passed to finishDrawing
+    Client protocol:
+        CA: Observe state and seqid changes up until a fixed frame deadline, then execute performTraversals
+        CB: If the seqId is incremeneted at the time of the frame deadline, configure the renderer to
+            redirect the next draw in to a transaction, record the seqId at the time
+        CC: When the draw is finished, send the transaction containing the draw to WM with the
+            previously recorded seqId
+    Additional requirements/assumptions:
+        1. The server may only send changes to the syncable state paired with the seqId. The client may
+           only receive them together (e.g. not from other sources)
+        2. In between changing and sending syncable state, the lock must be released and acquired again
+        3. The client wont draw a frame reflecting syncable state changes without passing through "performTraversals"
+        4. Drawing never fails
+        5. There are no blocking calls between the client or the server
+            
+Note that the server can begin the protocol at any time, so it may be possible for the client to proceed through
+phases SA, SB, SC, and SD multiple times before the client receives any messages.
+
+To show that the guarantee can't be violated, we use a notation of sequences, where we describe interleaving
+of protocol events. For duplicate events, we attach a number, e.g. SA_1, SA_2.
+
+We proceed by contradiction, imagine there was some sequence (..., SA_N, ...) for which the guarantee was
+not upheld. This means that either
+    1. finishDrawing with the assosciate seqId was never sent to the server OR
+    2. It was sent too late (after the first frame was sent to SF instead of WM) OR
+    3. It was sent too early (not containing the state changes originating with SA_N)
+If it was sent neither too late, nor too early, and contained the assosciated seqId, then protocol step SE
+says that the frame will be passed to the consumer and we uphold our guarantee.
+
+The first case is impossible because step SD says that the server always sends the seqId if a sync was prepared.
+If we send it the client must receive it. Since we only increment the seqId, and the client only takes the
+seqId from us (requirement 1, protocol step SB), the received ID must be higher than the clients previous seqId.
+CA says that performTraversals will execute, and CB says that when it does, if the seqId is higher than before
+it will schedule the render to sync. Requirement 4 says drawing never fails, so CC must execute, and so we will always
+eventually send every seqId (or a seqId > than it) back to the server.
+
+It also can't be sent too late. By requirement 2 we must release and acquire the lock
+after after changing and before emitting syncable state changes. This means it's guaranteed
+that even in an ordering like AcquireLock, ChangeState, PrepareSync, Release lock we can't
+send the state changes before prepareSync, and so they can always include at least the seqId
+assosciated with changestate (or a later one).
+Since we only receive the SeqId with the State changes (requirement 1),
+and we wont draw state changes without passing through perform traversals (requirement 3) the first frame
+containing the state change must have been generated by a call to performTraversals which also observed
+the seqId change, and so it will appropriately configure the renderer.
+
+By the same argument it can't be sent too early! Since we only send seqIds we receive from the server, 
+and we only send seqIds after completing a drawing pass of the assosciated state.
+
+So we can see that no matter at what time the server makes syncable state changes, the first frame will
+always be delivered to the draw handler. Assuming that the client and server uphold this protocol and these
+requirements.
+
+The trickiest part of the implementation at the moment is due to assosciating seqId. Currently we send one of the most
+most important pieces of syncable state (configuration) over multiple channels. Namely ClientTransaction
+and MSG_RESIZED. The ordering of these relative to sync preparation in server code is undefined, in fact we have cases like
+this all the time:
+    acquireLock()
+    changeConfiguration()
+    // time passes, but still in critical section
+    prepareSync()
+    releaseLock()
+This is exactly the kind of case Guarantee 1 mentions as an example. In previous incarnations of the code this worked
+because relayoutWindow needed to acquire the same lock and relayoutWindow was a necessary part of completing sync.
+
+Now that we have no barrier, that could create issues, because at the time we change configuration (and send the change
+to the client via ClientTransaction), we haven't even incremented the seqId yet, and so how can the client observe it
+at the same time as the state? We solve this by pushing all client communication through a handler thread that has to
+acquire the lock. This ensures we uphold requirement 2.
+    
diff --git a/services/core/java/com/android/server/wm/BackNavigationController.java b/services/core/java/com/android/server/wm/BackNavigationController.java
index 23f14a7..dbc0141 100644
--- a/services/core/java/com/android/server/wm/BackNavigationController.java
+++ b/services/core/java/com/android/server/wm/BackNavigationController.java
@@ -25,6 +25,7 @@
 import android.graphics.Point;
 import android.graphics.Rect;
 import android.hardware.HardwareBuffer;
+import android.os.Bundle;
 import android.os.RemoteCallback;
 import android.os.RemoteException;
 import android.os.SystemProperties;
@@ -44,7 +45,11 @@
 class BackNavigationController {
 
     private static final String TAG = "BackNavigationController";
-    private static final String BACK_PREDICTABILITY_PROP = "persist.debug.back_predictability";
+    // By default, enable new back dispatching without any animations.
+    private static final int BACK_PREDICTABILITY_PROP =
+            SystemProperties.getInt("persist.debug.back_predictability", 1);
+    private static final int ANIMATIONS_MASK = 1 << 1;
+    private static final int SCREENSHOT_MASK = 1 << 2;
 
     @Nullable
     private TaskSnapshotController mTaskSnapshotController;
@@ -53,11 +58,15 @@
      * Returns true if the back predictability feature is enabled
      */
     static boolean isEnabled() {
-        return SystemProperties.getInt(BACK_PREDICTABILITY_PROP, 0) > 0;
+        return BACK_PREDICTABILITY_PROP > 0;
     }
 
     static boolean isScreenshotEnabled() {
-        return false;
+        return (BACK_PREDICTABILITY_PROP & SCREENSHOT_MASK) != 0;
+    }
+
+    private static boolean isAnimationEnabled() {
+        return (BACK_PREDICTABILITY_PROP & ANIMATIONS_MASK) != 0;
     }
 
     /**
@@ -93,14 +102,17 @@
         ActivityRecord prev;
         WindowContainer<?> removedWindowContainer;
         ActivityRecord activityRecord;
+        ActivityRecord prevTaskTopActivity = null;
         SurfaceControl animationLeashParent;
         WindowConfiguration taskWindowConfiguration;
         HardwareBuffer screenshotBuffer = null;
+        SurfaceControl screenshotSurface;
         int prevTaskId;
         int prevUserId;
         RemoteAnimationTarget topAppTarget;
         SurfaceControl animLeash;
-        IOnBackInvokedCallback callback = null;
+        IOnBackInvokedCallback applicationCallback = null;
+        IOnBackInvokedCallback systemCallback = null;
 
         synchronized (task.mWmService.mGlobalLock) {
 
@@ -116,15 +128,14 @@
                 removedWindowContainer = activityRecord;
                 taskWindowConfiguration = window.getWindowConfiguration();
             }
-            IOnBackInvokedCallback applicationCallback = null;
-            IOnBackInvokedCallback systemCallback = null;
             if (window != null) {
                 applicationCallback = window.getApplicationOnBackInvokedCallback();
-                callback = applicationCallback;
-                if (callback == null) {
-                    systemCallback = window.getSystemOnBackInvokedCallback();
-                    callback = systemCallback;
-                }
+                systemCallback = window.getSystemOnBackInvokedCallback();
+            }
+            if (applicationCallback == null && systemCallback == null) {
+                // Return null when either there's no window, or apps have just initialized and
+                // have not finished registering callbacks.
+                return null;
             }
 
             ProtoLog.d(WM_DEBUG_BACK_PREVIEW, "startBackNavigation task=%s, "
@@ -133,24 +144,24 @@
                     task, activityRecord, applicationCallback, systemCallback);
 
             // TODO Temp workaround for Sysui until b/221071505 is fixed
-            if (activityRecord == null && callback != null) {
+            if (activityRecord == null && applicationCallback != null) {
                 return new BackNavigationInfo(BackNavigationInfo.TYPE_CALLBACK,
                         null /* topWindowLeash */, null /* screenshotSurface */,
                         null /* screenshotBuffer */, null /* taskWindowConfiguration */,
                         null /* onBackNavigationDone */,
-                        callback /* onBackInvokedCallback */);
+                        applicationCallback /* onBackInvokedCallback */);
             }
 
             // For IME and Home, either a callback is registered, or we do nothing. In both cases,
             // we don't need to pass the leashes below.
             if (activityRecord == null || task.getDisplayContent().getImeContainer().isVisible()
                     || activityRecord.isActivityTypeHome()) {
-                if (callback != null) {
+                if (applicationCallback != null) {
                     return new BackNavigationInfo(BackNavigationInfo.TYPE_CALLBACK,
                             null /* topWindowLeash */, null /* screenshotSurface */,
                             null /* screenshotBuffer */, null /* taskWindowConfiguration */,
                             null /* onBackNavigationDone */,
-                            callback /* onBackInvokedCallback */);
+                            applicationCallback /* onBackInvokedCallback */);
                 } else {
                     return null;
                 }
@@ -159,12 +170,12 @@
             prev = task.getActivity(
                     (r) -> !r.finishing && r.getTask() == task && !r.isTopRunningActivity());
 
-            if (callback != null) {
+            if (applicationCallback != null) {
                 return new BackNavigationInfo(BackNavigationInfo.TYPE_CALLBACK,
                         null /* topWindowLeash */, null /* screenshotSurface */,
                         null /* screenshotBuffer */, null /* taskWindowConfiguration */,
                         null /* onBackNavigationDone */,
-                        callback /* onBackInvokedCallback */);
+                        applicationCallback /* onBackInvokedCallback */);
             } else if (prev != null) {
                 backType = BackNavigationInfo.TYPE_CROSS_ACTIVITY;
             } else if (task.returnsToHomeRootTask()) {
@@ -188,8 +199,8 @@
             prevTaskId = prevTask != null ? prevTask.mTaskId : 0;
             prevUserId = prevTask != null ? prevTask.mUserId : 0;
 
-            ProtoLog.d(WM_DEBUG_BACK_PREVIEW, "Previous Activity is %s",
-                    prev != null ? prev.mActivityComponent : null);
+            ProtoLog.d(WM_DEBUG_BACK_PREVIEW, "Previous Activity is %s. "
+                    + "Back type is %s", prev != null ? prev.mActivityComponent : null, backType);
 
             //TODO(207481538) Remove once the infrastructure to support per-activity screenshot is
             // implemented. For now we simply have the mBackScreenshots hash map that dumbly
@@ -204,6 +215,7 @@
                 return null;
             }
             // Prepare a leash to animate the current top window
+            // TODO(b/220934562): Use surface animator to better manage animation conflicts.
             animLeash = removedWindowContainer.makeAnimationLeash()
                     .setName("BackPreview Leash for " + removedWindowContainer)
                     .setHidden(false)
@@ -231,12 +243,30 @@
                     activityRecord.windowType);
         }
 
-        SurfaceControl.Builder builder = new SurfaceControl.Builder()
+        screenshotSurface = new SurfaceControl.Builder()
                 .setName("BackPreview Screenshot for " + prev)
                 .setParent(animationLeashParent)
                 .setHidden(false)
-                .setBLASTLayer();
-        SurfaceControl screenshotSurface = builder.build();
+                .setBLASTLayer()
+                .build();
+        if (backType == BackNavigationInfo.TYPE_RETURN_TO_HOME && isAnimationEnabled()) {
+            task.mBackGestureStarted = true;
+            // Make launcher show from behind by marking its top activity as visible and
+            // launch-behind to bump its visibility for the duration of the back gesture.
+            prevTaskTopActivity = prevTask.getTopNonFinishingActivity();
+            if (prevTaskTopActivity != null) {
+                if (!prevTaskTopActivity.mVisibleRequested) {
+                    prevTaskTopActivity.setVisibility(true);
+                }
+                prevTaskTopActivity.mLaunchTaskBehind = true;
+                ProtoLog.d(WM_DEBUG_BACK_PREVIEW,
+                        "Setting Activity.mLauncherTaskBehind to true. Activity=%s",
+                        prevTaskTopActivity);
+                prevTaskTopActivity.mRootWindowContainer.ensureActivitiesVisible(
+                        null /* starting */, 0 /* configChanges */,
+                        false /* preserveWindows */);
+            }
+        }
 
         // Find a screenshot of the previous activity
 
@@ -253,17 +283,20 @@
 
         WindowContainer<?> finalRemovedWindowContainer = removedWindowContainer;
         try {
-            activityRecord.token.linkToDeath(
-                    () -> resetSurfaces(finalRemovedWindowContainer), 0);
+            activityRecord.token.linkToDeath(() -> resetSurfaces(finalRemovedWindowContainer), 0);
         } catch (RemoteException e) {
             Slog.e(TAG, "Failed to link to death", e);
             resetSurfaces(removedWindowContainer);
             return null;
         }
 
-        RemoteCallback onBackNavigationDone = new RemoteCallback(
-                result -> resetSurfaces(finalRemovedWindowContainer
-                ));
+        int finalBackType = backType;
+        final IOnBackInvokedCallback callback =
+                applicationCallback != null ? applicationCallback : systemCallback;
+        ActivityRecord finalPrevTaskTopActivity = prevTaskTopActivity;
+        RemoteCallback onBackNavigationDone = new RemoteCallback(result -> onBackNavigationDone(
+                result, finalRemovedWindowContainer, finalBackType, task,
+                finalPrevTaskTopActivity));
         return new BackNavigationInfo(backType,
                 topAppTarget,
                 screenshotSurface,
@@ -273,6 +306,39 @@
                 callback);
     }
 
+    private void onBackNavigationDone(
+            Bundle result, WindowContainer windowContainer, int backType,
+            Task task, ActivityRecord prevTaskTopActivity) {
+        SurfaceControl surfaceControl = windowContainer.getSurfaceControl();
+        boolean triggerBack = result != null
+                ? result.getBoolean(BackNavigationInfo.KEY_TRIGGER_BACK)
+                : false;
+        ProtoLog.d(WM_DEBUG_BACK_PREVIEW, "onBackNavigationDone backType=%s, "
+                + "task=%s, prevTaskTopActivity=%s", backType, task, prevTaskTopActivity);
+
+        if (backType == BackNavigationInfo.TYPE_RETURN_TO_HOME && isAnimationEnabled()) {
+            if (triggerBack) {
+                if (surfaceControl != null && surfaceControl.isValid()) {
+                    // When going back to home, hide the task surface before it is re-parented to
+                    // avoid flicker.
+                    SurfaceControl.Transaction t = windowContainer.getSyncTransaction();
+                    t.hide(surfaceControl);
+                    t.apply();
+                }
+            }
+            if (prevTaskTopActivity != null && !triggerBack) {
+                // Restore the launch-behind state.
+                task.mTaskSupervisor.scheduleLaunchTaskBehindComplete(prevTaskTopActivity.token);
+                prevTaskTopActivity.mLaunchTaskBehind = false;
+                ProtoLog.d(WM_DEBUG_BACK_PREVIEW,
+                        "Setting Activity.mLauncherTaskBehind to false. Activity=%s",
+                        prevTaskTopActivity);
+            }
+        } else {
+            task.mBackGestureStarted = false;
+        }
+        resetSurfaces(windowContainer);
+    }
 
     private HardwareBuffer getActivitySnapshot(@NonNull Task task,
             ComponentName activityComponent) {
diff --git a/services/core/java/com/android/server/wm/DisplayContent.java b/services/core/java/com/android/server/wm/DisplayContent.java
index d1b9a78..73673202 100644
--- a/services/core/java/com/android/server/wm/DisplayContent.java
+++ b/services/core/java/com/android/server/wm/DisplayContent.java
@@ -145,7 +145,6 @@
 import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
 import static com.android.server.wm.WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE;
 import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
-import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
 import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_PLACING_SURFACES;
 import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_ASSIGN_LAYERS;
 import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
@@ -4270,17 +4269,17 @@
      */
     @VisibleForTesting
     SurfaceControl computeImeParent() {
+        if (mImeLayeringTarget != null && mImeInputTarget != null
+                && mImeLayeringTarget.mActivityRecord != mImeInputTarget.getActivityRecord()) {
+            // Do not change parent if the window hasn't requested IME.
+            return null;
+        }
         // Attach it to app if the target is part of an app and such app is covering the entire
         // screen. If it's not covering the entire screen the IME might extend beyond the apps
         // bounds.
         if (shouldImeAttachedToApp()) {
-            if (mImeLayeringTarget.mActivityRecord != mImeInputTarget.getActivityRecord()) {
-                // Do not change parent if the window hasn't requested IME.
-                return null;
-            }
             return mImeLayeringTarget.mActivityRecord.getSurfaceControl();
         }
-
         // Otherwise, we just attach it to where the display area policy put it.
         return mImeWindowsContainer.getParent() != null
                 ? mImeWindowsContainer.getParent().getSurfaceControl() : null;
@@ -4504,56 +4503,38 @@
 
         mTmpUpdateAllDrawn.clear();
 
-        int repeats = 0;
-        do {
-            repeats++;
-            if (repeats > 6) {
-                Slog.w(TAG, "Animation repeat aborted after too many iterations");
-                clearLayoutNeeded();
-                break;
-            }
+        if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
+                pendingLayoutChanges);
 
-            if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
-                    pendingLayoutChanges);
+        if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
+            mWallpaperController.adjustWallpaperWindows();
+        }
 
-            if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
-                mWallpaperController.adjustWallpaperWindows();
-            }
-
-            if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
-                if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
-                if (updateOrientation()) {
-                    setLayoutNeeded();
-                    sendNewConfiguration();
-                }
-            }
-
-            if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
+        if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
+            if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
+            if (updateOrientation()) {
                 setLayoutNeeded();
+                sendNewConfiguration();
             }
+        }
 
-            // FIRST LOOP: Perform a layout, if needed.
-            if (repeats < LAYOUT_REPEAT_THRESHOLD) {
-                performLayout(repeats == 1, false /* updateInputWindows */);
-            } else {
-                Slog.w(TAG, "Layout repeat skipped after too many iterations");
-            }
+        if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
+            setLayoutNeeded();
+        }
 
-            // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
-            pendingLayoutChanges = 0;
+        // Perform a layout, if needed.
+        performLayout(true /* initial */, false /* updateInputWindows */);
+        pendingLayoutChanges = 0;
 
-            Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "applyPostLayoutPolicy");
-            try {
-                mDisplayPolicy.beginPostLayoutPolicyLw();
-                forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
-                pendingLayoutChanges |= mDisplayPolicy.finishPostLayoutPolicyLw();
-            } finally {
-                Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
-            }
-            if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
-                    "after finishPostLayoutPolicyLw", pendingLayoutChanges);
-            mInsetsStateController.onPostLayout();
-        } while (pendingLayoutChanges != 0);
+        Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "applyPostLayoutPolicy");
+        try {
+            mDisplayPolicy.beginPostLayoutPolicyLw();
+            forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
+            mDisplayPolicy.finishPostLayoutPolicyLw();
+        } finally {
+            Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
+        }
+        mInsetsStateController.onPostLayout();
 
         mTmpApplySurfaceChangesTransactionState.reset();
 
diff --git a/services/core/java/com/android/server/wm/DisplayPolicy.java b/services/core/java/com/android/server/wm/DisplayPolicy.java
index 4148d8b..bb5dacc 100644
--- a/services/core/java/com/android/server/wm/DisplayPolicy.java
+++ b/services/core/java/com/android/server/wm/DisplayPolicy.java
@@ -42,11 +42,9 @@
 import static android.view.WindowManager.LayoutParams.FIRST_SYSTEM_WINDOW;
 import static android.view.WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON;
 import static android.view.WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS;
-import static android.view.WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN;
 import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
 import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
 import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_FORCE_DRAW_BAR_BACKGROUNDS;
-import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_FORCE_SHOW_STATUS_BAR;
 import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_INTERCEPT_GLOBAL_DRAG_AND_DROP;
 import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_TRUSTED_OVERLAY;
 import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
@@ -79,7 +77,6 @@
 
 import static com.android.internal.protolog.ProtoLogGroup.WM_DEBUG_SCREEN_ON;
 import static com.android.server.policy.PhoneWindowManager.TOAST_WINDOW_TIMEOUT;
-import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
 import static com.android.server.policy.WindowManagerPolicy.TRANSIT_ENTER;
 import static com.android.server.policy.WindowManagerPolicy.TRANSIT_EXIT;
 import static com.android.server.policy.WindowManagerPolicy.TRANSIT_HIDE;
@@ -136,6 +133,7 @@
 import android.view.WindowManagerGlobal;
 import android.view.WindowManagerPolicyConstants;
 import android.view.accessibility.AccessibilityManager;
+import android.window.ClientWindowFrames;
 
 import com.android.internal.R;
 import com.android.internal.annotations.VisibleForTesting;
@@ -340,15 +338,12 @@
     private static final Rect sTmpRect2 = new Rect();
     private static final Rect sTmpLastParentFrame = new Rect();
     private static final Rect sTmpDisplayCutoutSafe = new Rect();
-    private static final Rect sTmpDisplayFrame = new Rect();
-    private static final Rect sTmpParentFrame = new Rect();
-    private static final Rect sTmpFrame = new Rect();
+    private static final ClientWindowFrames sTmpClientFrames = new ClientWindowFrames();
 
     private final WindowLayout mWindowLayout = new WindowLayout();
 
     private WindowState mTopFullscreenOpaqueWindowState;
     private boolean mTopIsFullscreen;
-    private boolean mForceStatusBar;
     private int mNavBarOpacityMode = NAV_BAR_OPAQUE_WHEN_FREEFORM_OR_DOCKED;
     private boolean mForceShowSystemBars;
 
@@ -940,6 +935,26 @@
                 break;
         }
 
+        if (LayoutParams.isSystemAlertWindowType(attrs.type)) {
+            float maxOpacity = mService.mMaximumObscuringOpacityForTouch;
+            if (attrs.alpha > maxOpacity
+                    && (attrs.flags & FLAG_NOT_TOUCHABLE) != 0
+                    && (attrs.privateFlags & PRIVATE_FLAG_TRUSTED_OVERLAY) == 0) {
+                // The app is posting a SAW with the intent of letting touches pass through, but
+                // they are going to be deemed untrusted and will be blocked. Try to honor the
+                // intent of letting touches pass through at the cost of 0.2 opacity for app
+                // compatibility reasons. More details on b/218777508.
+                Slog.w(TAG, String.format(
+                        "App %s has a system alert window (type = %d) with FLAG_NOT_TOUCHABLE and "
+                                + "LayoutParams.alpha = %.2f > %.2f, setting alpha to %.2f to "
+                                + "let touches pass through (if this is isn't desirable, remove "
+                                + "flag FLAG_NOT_TOUCHABLE).",
+                        attrs.packageName, attrs.type, attrs.alpha, maxOpacity, maxOpacity));
+                attrs.alpha = maxOpacity;
+                win.mWinAnimator.mAlpha = maxOpacity;
+            }
+        }
+
         // Check if alternate bars positions were updated.
         if (mStatusBarAlt == win) {
             mStatusBarAltPosition = getAltBarPosition(attrs);
@@ -1474,8 +1489,8 @@
                     displayFrames.mUnrestricted, win.getWindowingMode(), UNSPECIFIED_LENGTH,
                     UNSPECIFIED_LENGTH, win.getRequestedVisibilities(),
                     null /* attachedWindowFrame */, win.mGlobalScale,
-                    sTmpDisplayFrame, sTmpParentFrame, sTmpFrame);
-            controller.computeSimulatedState(win, displayFrames, sTmpFrame);
+                    sTmpClientFrames);
+            controller.computeSimulatedState(win, displayFrames, sTmpClientFrames.frame);
         }
     }
 
@@ -1518,12 +1533,12 @@
 
         sTmpLastParentFrame.set(pf);
 
-        final boolean clippedByDisplayCutout = mWindowLayout.computeFrames(attrs,
-                win.getInsetsState(), displayFrames.mDisplayCutoutSafe,
+        mWindowLayout.computeFrames(attrs, win.getInsetsState(), displayFrames.mDisplayCutoutSafe,
                 win.getBounds(), win.getWindowingMode(), requestedWidth, requestedHeight,
                 win.getRequestedVisibilities(), attachedWindowFrame, win.mGlobalScale,
-                df, pf, f);
-        windowFrames.setParentFrameWasClippedByDisplayCutout(clippedByDisplayCutout);
+                sTmpClientFrames);
+        windowFrames.setParentFrameWasClippedByDisplayCutout(
+                sTmpClientFrames.isParentFrameClippedByDisplayCutout);
 
         if (DEBUG_LAYOUT) Slog.v(TAG, "Compute frame " + attrs.getTitle()
                 + ": sim=#" + Integer.toHexString(attrs.softInputMode)
@@ -1536,7 +1551,7 @@
             windowFrames.setContentChanged(true);
         }
 
-        win.setFrame();
+        win.setFrames(sTmpClientFrames);
     }
 
     WindowState getTopFullscreenOpaqueWindow() {
@@ -1558,7 +1573,6 @@
         mStatusBarBackgroundWindows.clear();
         mStatusBarColorCheckedBounds.setEmpty();
         mStatusBarBackgroundCheckedBounds.setEmpty();
-        mForceStatusBar = false;
 
         mAllowLockscreenWhenOn = false;
         mShowingDream = false;
@@ -1599,9 +1613,6 @@
                 && attrs.type < FIRST_SYSTEM_WINDOW;
         if (mTopFullscreenOpaqueWindowState == null) {
             final int fl = attrs.flags;
-            if ((fl & FLAG_FORCE_NOT_FULLSCREEN) != 0) {
-                mForceStatusBar = true;
-            }
             if (win.isDreamWindow()) {
                 // If the lockscreen was showing when the dream started then wait
                 // for the dream to draw before hiding the lockscreen.
@@ -1710,21 +1721,9 @@
     }
 
     /**
-     * Called following layout of all windows and after policy has been applied
-     * to each window. If in this function you do
-     * something that may have modified the animation state of another window,
-     * be sure to return non-zero in order to perform another pass through layout.
-     *
-     * @return Return any bit set of
-     *         {@link WindowManagerPolicy#FINISH_LAYOUT_REDO_LAYOUT},
-     *         {@link WindowManagerPolicy#FINISH_LAYOUT_REDO_CONFIG},
-     *         {@link WindowManagerPolicy#FINISH_LAYOUT_REDO_WALLPAPER}, or
-     *         {@link WindowManagerPolicy#FINISH_LAYOUT_REDO_ANIM}.
+     * Called following layout of all windows and after policy has been applied to each window.
      */
-    public int finishPostLayoutPolicyLw() {
-        int changes = 0;
-        boolean topIsFullscreen = false;
-
+    public void finishPostLayoutPolicyLw() {
         // If we are not currently showing a dream then remember the current
         // lockscreen state.  We will use this to determine whether the dream
         // started while the lockscreen was showing and remember this state
@@ -1733,41 +1732,6 @@
             mDreamingLockscreen = mService.mPolicy.isKeyguardShowingAndNotOccluded();
         }
 
-        if (getStatusBar() != null) {
-            if (DEBUG_LAYOUT) Slog.i(TAG, "force=" + mForceStatusBar
-                    + " top=" + mTopFullscreenOpaqueWindowState);
-            final boolean forceShowStatusBar = (getStatusBar().getAttrs().privateFlags
-                    & PRIVATE_FLAG_FORCE_SHOW_STATUS_BAR) != 0;
-
-            boolean topAppHidesStatusBar = topAppHidesStatusBar();
-            if (mForceStatusBar || forceShowStatusBar) {
-                if (DEBUG_LAYOUT) Slog.v(TAG, "Showing status bar: forced");
-                // Maintain fullscreen layout until incoming animation is complete.
-                topIsFullscreen = mTopIsFullscreen && mStatusBar.isAnimatingLw();
-            } else if (mTopFullscreenOpaqueWindowState != null) {
-                topIsFullscreen = topAppHidesStatusBar;
-                // The subtle difference between the window for mTopFullscreenOpaqueWindowState
-                // and mTopIsFullscreen is that mTopIsFullscreen is set only if the window
-                // requests to hide the status bar.  Not sure if there is another way that to be the
-                // case though.
-                if (!topIsFullscreen) {
-                    topAppHidesStatusBar = false;
-                }
-            }
-            StatusBarManagerInternal statusBar = getStatusBarManagerInternal();
-            if (statusBar != null) {
-                statusBar.setTopAppHidesStatusBar(topAppHidesStatusBar);
-            }
-        }
-
-        if (mTopIsFullscreen != topIsFullscreen) {
-            if (!topIsFullscreen) {
-                // Force another layout when status bar becomes fully shown.
-                changes |= FINISH_LAYOUT_REDO_LAYOUT;
-            }
-            mTopIsFullscreen = topIsFullscreen;
-        }
-
         updateSystemBarAttributes();
 
         if (mShowingDream != mLastShowingDream) {
@@ -1777,7 +1741,6 @@
         }
 
         mService.mPolicy.setAllowLockscreenWhenOn(getDisplayId(), mAllowLockscreenWhenOn);
-        return changes;
     }
 
     /**
@@ -2441,6 +2404,18 @@
         mForceShowSystemBars = multiWindowTaskVisible || freeformRootTaskVisible;
         mDisplayContent.getInsetsPolicy().updateBarControlTarget(win);
 
+        final boolean topAppHidesStatusBar = topAppHidesStatusBar();
+        if (getStatusBar() != null) {
+            final StatusBarManagerInternal statusBar = getStatusBarManagerInternal();
+            if (statusBar != null) {
+                statusBar.setTopAppHidesStatusBar(topAppHidesStatusBar);
+            }
+        }
+
+        // If the top app is not fullscreen, only the default rotation animation is allowed.
+        mTopIsFullscreen = topAppHidesStatusBar
+                && (mNotificationShade == null || !mNotificationShade.isVisible());
+
         int appearance = APPEARANCE_OPAQUE_NAVIGATION_BARS | APPEARANCE_OPAQUE_STATUS_BARS;
         appearance = configureStatusBarOpacity(appearance);
         appearance = configureNavBarOpacity(appearance, multiWindowTaskVisible,
@@ -2781,7 +2756,6 @@
             }
         }
         pw.print(prefix); pw.print("mTopIsFullscreen="); pw.println(mTopIsFullscreen);
-        pw.print(prefix); pw.print("mForceStatusBar="); pw.print(mForceStatusBar);
         pw.print(prefix); pw.print("mForceShowNavigationBarEnabled=");
         pw.print(mForceShowNavigationBarEnabled);
         pw.print(" mAllowLockscreenWhenOn="); pw.println(mAllowLockscreenWhenOn);
diff --git a/services/core/java/com/android/server/wm/DisplayWindowPolicyControllerHelper.java b/services/core/java/com/android/server/wm/DisplayWindowPolicyControllerHelper.java
index 60d2a5d..27d46ec 100644
--- a/services/core/java/com/android/server/wm/DisplayWindowPolicyControllerHelper.java
+++ b/services/core/java/com/android/server/wm/DisplayWindowPolicyControllerHelper.java
@@ -18,6 +18,7 @@
 
 import android.annotation.NonNull;
 import android.annotation.Nullable;
+import android.app.WindowConfiguration;
 import android.content.pm.ActivityInfo;
 import android.os.UserHandle;
 import android.util.ArraySet;
@@ -63,13 +64,14 @@
     }
 
     /**
-     * @see DisplayWindowPolicyController#canContainActivities(List)
+     * @see DisplayWindowPolicyController#canContainActivities(List, int)
      */
-    public boolean canContainActivities(@NonNull List<ActivityInfo> activities) {
+    public boolean canContainActivities(@NonNull List<ActivityInfo> activities,
+            @WindowConfiguration.WindowingMode int windowingMode) {
         if (mDisplayWindowPolicyController == null) {
             return true;
         }
-        return mDisplayWindowPolicyController.canContainActivities(activities);
+        return mDisplayWindowPolicyController.canContainActivities(activities, windowingMode);
     }
 
     /**
@@ -126,6 +128,17 @@
         }
     }
 
+    /**
+     * @see DisplayWindowPolicyController#isWindowingModeSupported(int)
+     */
+    public final boolean isWindowingModeSupported(
+            @WindowConfiguration.WindowingMode int windowingMode) {
+        if (mDisplayWindowPolicyController == null) {
+            return true;
+        }
+        return mDisplayWindowPolicyController.isWindowingModeSupported(windowingMode);
+    }
+
     void dump(String prefix, PrintWriter pw) {
         if (mDisplayWindowPolicyController != null) {
             pw.println();
diff --git a/services/core/java/com/android/server/wm/InsetsPolicy.java b/services/core/java/com/android/server/wm/InsetsPolicy.java
index 398816b..3951c56 100644
--- a/services/core/java/com/android/server/wm/InsetsPolicy.java
+++ b/services/core/java/com/android/server/wm/InsetsPolicy.java
@@ -424,14 +424,12 @@
                 return state;
             }
         } else if (w.mActivityRecord != null && w.mActivityRecord.mImeInsetsFrozenUntilStartInput) {
-            // During switching tasks with gestural navigation, if the IME is attached to
-            // one app window on that time, even the next app window is behind the IME window,
-            // conceptually the window should not receive the IME insets if the next window is
-            // not eligible IME requester and ready to show IME on top of it.
-            final boolean shouldImeAttachedToApp = mDisplayContent.shouldImeAttachedToApp();
+            // During switching tasks with gestural navigation, before the next IME input target
+            // starts the input, we should adjust and freeze the last IME visibility of the window
+            // in case delivering obsoleted IME insets state during transitioning.
             final InsetsSource originalImeSource = originalState.peekSource(ITYPE_IME);
 
-            if (shouldImeAttachedToApp && originalImeSource != null) {
+            if (originalImeSource != null) {
                 final boolean imeVisibility =
                         w.mActivityRecord.mLastImeShown || w.getRequestedVisibility(ITYPE_IME);
                 final InsetsState state = copyState ? new InsetsState(originalState)
diff --git a/services/core/java/com/android/server/wm/KeyguardController.java b/services/core/java/com/android/server/wm/KeyguardController.java
index c55af9b..7bf150b 100644
--- a/services/core/java/com/android/server/wm/KeyguardController.java
+++ b/services/core/java/com/android/server/wm/KeyguardController.java
@@ -443,6 +443,13 @@
                 || !mWindowManager.isKeyguardSecure(mService.getCurrentUserId());
     }
 
+    /**
+     * @return Whether the dream activity is on top of default display.
+     */
+    boolean isShowingDream() {
+        return getDisplayState(DEFAULT_DISPLAY).mShowingDream;
+    }
+
     private void dismissMultiWindowModeForTaskIfNeeded(int displayId,
             @Nullable Task currentTaskControllingOcclusion) {
         // TODO(b/113840485): Handle docked stack for individual display.
@@ -501,6 +508,7 @@
         private boolean mKeyguardGoingAway;
         private boolean mDismissalRequested;
         private boolean mOccluded;
+        private boolean mShowingDream;
 
         private ActivityRecord mTopOccludesActivity;
         private ActivityRecord mDismissingKeyguardActivity;
@@ -536,6 +544,7 @@
 
             mRequestDismissKeyguard = false;
             mOccluded = false;
+            mShowingDream = false;
 
             mTopOccludesActivity = null;
             mDismissingKeyguardActivity = null;
@@ -570,9 +579,9 @@
                 }
             }
 
-            final boolean dreaming = display.getDisplayPolicy().isShowingDreamLw() && (top != null
+            mShowingDream = display.getDisplayPolicy().isShowingDreamLw() && (top != null
                     && top.getActivityType() == ACTIVITY_TYPE_DREAM);
-            mOccluded = dreaming || occludedByActivity;
+            mOccluded = mShowingDream || occludedByActivity;
             mRequestDismissKeyguard = lastDismissKeyguardActivity != mDismissingKeyguardActivity
                     && !mOccluded
                     && mDismissingKeyguardActivity != null
diff --git a/services/core/java/com/android/server/wm/MirrorActiveUids.java b/services/core/java/com/android/server/wm/MirrorActiveUids.java
index 4e7f1d4..b9aa959 100644
--- a/services/core/java/com/android/server/wm/MirrorActiveUids.java
+++ b/services/core/java/com/android/server/wm/MirrorActiveUids.java
@@ -19,7 +19,7 @@
 import static android.app.ActivityManager.PROCESS_STATE_NONEXISTENT;
 
 import android.app.ActivityManager.ProcessState;
-import android.util.SparseArray;
+import android.util.SparseIntArray;
 
 import java.io.PrintWriter;
 
@@ -29,15 +29,14 @@
  * adjustment) or getting state from window manager (background start check).
  */
 class MirrorActiveUids {
-    private final SparseArray<UidRecord> mUidStates = new SparseArray<>();
+    /** Uid -> process state. */
+    private final SparseIntArray mUidStates = new SparseIntArray();
+
+    /** Uid -> number of non-app visible windows belong to the uid. */
+    private final SparseIntArray mNumNonAppVisibleWindowMap = new SparseIntArray();
 
     synchronized void onUidActive(int uid, int procState) {
-        UidRecord r = mUidStates.get(uid);
-        if (r == null) {
-            r = new UidRecord();
-            mUidStates.put(uid, r);
-        }
-        r.mProcState = procState;
+        mUidStates.put(uid, procState);
     }
 
     synchronized void onUidInactive(int uid) {
@@ -45,22 +44,28 @@
     }
 
     synchronized void onUidProcStateChanged(int uid, int procState) {
-        final UidRecord r = mUidStates.get(uid);
-        if (r != null) {
-            r.mProcState = procState;
+        final int index = mUidStates.indexOfKey(uid);
+        if (index >= 0) {
+            mUidStates.setValueAt(index, procState);
         }
     }
 
     synchronized @ProcessState int getUidState(int uid) {
-        final UidRecord r = mUidStates.get(uid);
-        return r != null ? r.mProcState : PROCESS_STATE_NONEXISTENT;
+        return mUidStates.get(uid, PROCESS_STATE_NONEXISTENT);
     }
 
     /** Called when the surface of non-application (exclude toast) window is shown or hidden. */
     synchronized void onNonAppSurfaceVisibilityChanged(int uid, boolean visible) {
-        final UidRecord r = mUidStates.get(uid);
-        if (r != null) {
-            r.mNumNonAppVisibleWindow += visible ? 1 : -1;
+        final int index = mNumNonAppVisibleWindowMap.indexOfKey(uid);
+        if (index >= 0) {
+            final int num = mNumNonAppVisibleWindowMap.valueAt(index) + (visible ? 1 : -1);
+            if (num > 0) {
+                mNumNonAppVisibleWindowMap.setValueAt(index, num);
+            } else {
+                mNumNonAppVisibleWindowMap.removeAt(index);
+            }
+        } else if (visible) {
+            mNumNonAppVisibleWindowMap.append(uid, 1);
         }
     }
 
@@ -70,23 +75,15 @@
      * {@link VisibleActivityProcessTracker}.
      */
     synchronized boolean hasNonAppVisibleWindow(int uid) {
-        final UidRecord r = mUidStates.get(uid);
-        return r != null && r.mNumNonAppVisibleWindow > 0;
+        return mNumNonAppVisibleWindowMap.get(uid) > 0;
     }
 
     synchronized void dump(PrintWriter pw, String prefix) {
-        pw.print(prefix + "NumNonAppVisibleWindowByUid:[");
-        for (int i = mUidStates.size() - 1; i >= 0; i--) {
-            final UidRecord r = mUidStates.valueAt(i);
-            if (r.mNumNonAppVisibleWindow > 0) {
-                pw.print(" " + mUidStates.keyAt(i) + ":" + r.mNumNonAppVisibleWindow);
-            }
+        pw.print(prefix + "NumNonAppVisibleWindowUidMap:[");
+        for (int i = mNumNonAppVisibleWindowMap.size() - 1; i >= 0; i--) {
+            pw.print(" " + mNumNonAppVisibleWindowMap.keyAt(i) + ":"
+                    + mNumNonAppVisibleWindowMap.valueAt(i));
         }
         pw.println("]");
     }
-
-    private static final class UidRecord {
-        @ProcessState int mProcState;
-        int mNumNonAppVisibleWindow;
-    }
 }
diff --git a/services/core/java/com/android/server/wm/RecentsAnimationController.java b/services/core/java/com/android/server/wm/RecentsAnimationController.java
index a407021..a4d338c 100644
--- a/services/core/java/com/android/server/wm/RecentsAnimationController.java
+++ b/services/core/java/com/android/server/wm/RecentsAnimationController.java
@@ -553,10 +553,6 @@
 
             mPendingStart = false;
 
-            // Perform layout if it was scheduled before to make sure that we get correct content
-            // insets for the target app window after a rotation
-            mDisplayContent.performLayout(false /* initial */, false /* updateInputWindows */);
-
             final Rect contentInsets;
             final WindowState targetAppMainWindow = getTargetAppMainWindow();
             if (targetAppMainWindow != null) {
diff --git a/services/core/java/com/android/server/wm/RootWindowContainer.java b/services/core/java/com/android/server/wm/RootWindowContainer.java
index 2355333..94fc51d 100644
--- a/services/core/java/com/android/server/wm/RootWindowContainer.java
+++ b/services/core/java/com/android/server/wm/RootWindowContainer.java
@@ -3376,11 +3376,17 @@
                 return new ArrayList<>();
             }
         } else {
+            final RecentTasks recentTasks = mWindowManager.mAtmService.getRecentTasks();
+            final int recentsComponentUid = recentTasks != null
+                    ? recentTasks.getRecentsComponentUid()
+                    : -1;
             final ArrayList<ActivityRecord> activities = new ArrayList<>();
-            forAllRootTasks(rootTask -> {
-                if (!dumpVisibleRootTasksOnly || rootTask.shouldBeVisible(null)) {
-                    activities.addAll(rootTask.getDumpActivitiesLocked(name, userId));
+            forAllLeafTasks(task -> {
+                final boolean isRecents = (task.effectiveUid == recentsComponentUid);
+                if (!dumpVisibleRootTasksOnly || task.shouldBeVisible(null) || isRecents) {
+                    activities.addAll(task.getDumpActivitiesLocked(name, userId));
                 }
+                return false;
             });
             return activities;
         }
diff --git a/services/core/java/com/android/server/wm/Session.java b/services/core/java/com/android/server/wm/Session.java
index 0c609191..9ad25ac8 100644
--- a/services/core/java/com/android/server/wm/Session.java
+++ b/services/core/java/com/android/server/wm/Session.java
@@ -232,14 +232,14 @@
             int requestedWidth, int requestedHeight, int viewFlags, int flags,
             ClientWindowFrames outFrames, MergedConfiguration mergedConfiguration,
             SurfaceControl outSurfaceControl, InsetsState outInsetsState,
-            InsetsSourceControl[] outActiveControls) {
+            InsetsSourceControl[] outActiveControls, Bundle outSyncSeqIdBundle) {
         if (false) Slog.d(TAG_WM, ">>>>>> ENTERED relayout from "
                 + Binder.getCallingPid());
         Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, mRelayoutTag);
         int res = mService.relayoutWindow(this, window, attrs,
                 requestedWidth, requestedHeight, viewFlags, flags,
                 outFrames, mergedConfiguration, outSurfaceControl, outInsetsState,
-                outActiveControls);
+                outActiveControls, outSyncSeqIdBundle);
         Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
         if (false) Slog.d(TAG_WM, "<<<<<< EXITING relayout to "
                 + Binder.getCallingPid());
@@ -265,9 +265,9 @@
 
     @Override
     public void finishDrawing(IWindow window,
-            @Nullable SurfaceControl.Transaction postDrawTransaction) {
+            @Nullable SurfaceControl.Transaction postDrawTransaction, int seqId) {
         if (DEBUG) Slog.v(TAG_WM, "IWindow finishDrawing called for " + window);
-        mService.finishDrawingWindow(this, window, postDrawTransaction);
+        mService.finishDrawingWindow(this, window, postDrawTransaction, seqId);
     }
 
     @Override
diff --git a/services/core/java/com/android/server/wm/Task.java b/services/core/java/com/android/server/wm/Task.java
index b3ae602..f71bd72 100644
--- a/services/core/java/com/android/server/wm/Task.java
+++ b/services/core/java/com/android/server/wm/Task.java
@@ -72,6 +72,7 @@
 import static com.android.internal.policy.DecorView.DECOR_SHADOW_FOCUSED_HEIGHT_IN_DIP;
 import static com.android.internal.policy.DecorView.DECOR_SHADOW_UNFOCUSED_HEIGHT_IN_DIP;
 import static com.android.internal.protolog.ProtoLogGroup.WM_DEBUG_ADD_REMOVE;
+import static com.android.internal.protolog.ProtoLogGroup.WM_DEBUG_BACK_PREVIEW;
 import static com.android.internal.protolog.ProtoLogGroup.WM_DEBUG_LOCKTASK;
 import static com.android.internal.protolog.ProtoLogGroup.WM_DEBUG_RECENTS_ANIMATIONS;
 import static com.android.internal.protolog.ProtoLogGroup.WM_DEBUG_STATES;
@@ -609,6 +610,12 @@
 
     boolean mLastSurfaceShowing = true;
 
+    /**
+     * Tracks if a back gesture is in progress.
+     * Skips any system transition animations if this is set to {@code true}.
+     */
+    boolean mBackGestureStarted = false;
+
     private Task(ActivityTaskManagerService atmService, int _taskId, Intent _intent,
             Intent _affinityIntent, String _affinity, String _rootAffinity,
             ComponentName _realActivity, ComponentName _origActivity, boolean _rootWasReset,
@@ -1523,7 +1530,7 @@
                 mTaskSupervisor.removeTask(this, false /* killProcess */,
                         !REMOVE_FROM_RECENTS, reason);
             }
-        } else if (!mReuseTask && !mCreatedByOrganizer) {
+        } else if (!mReuseTask && shouldRemoveSelfOnLastChildRemoval()) {
             // Remove entire task if it doesn't have any activity left and it isn't marked for reuse
             // or created by task organizer.
             if (!isRootTask()) {
@@ -1623,12 +1630,16 @@
     }
 
     ActivityRecord performClearTop(ActivityRecord newR, int launchFlags) {
+        // The task should be preserved for putting new activity in case the last activity is
+        // finished if it is normal launch mode and not single top ("clear-task-top").
+        mReuseTask = true;
         mTaskSupervisor.beginDeferResume();
         final ActivityRecord result;
         try {
             result = clearTopActivities(newR, launchFlags);
         } finally {
             mTaskSupervisor.endDeferResume();
+            mReuseTask = false;
         }
         return result;
     }
@@ -2036,8 +2047,10 @@
         Rect outOverrideBounds = getResolvedOverrideConfiguration().windowConfiguration.getBounds();
 
         if (windowingMode == WINDOWING_MODE_FULLSCREEN) {
-            // Use empty bounds to indicate "fill parent".
-            outOverrideBounds.setEmpty();
+            if (!mCreatedByOrganizer) {
+                // Use empty bounds to indicate "fill parent".
+                outOverrideBounds.setEmpty();
+            }
             // The bounds for fullscreen mode shouldn't be adjusted by minimal size. Otherwise if
             // the parent or display is smaller than the size, the content may be cropped.
             return;
@@ -3320,6 +3333,14 @@
                     }
                 });
             }
+        } else if (mBackGestureStarted) {
+            // Cancel playing transitions if a back navigation animation is in progress.
+            // This bit is set by {@link BackNavigationController} when a back gesture is started.
+            // It is used as a one-off transition overwrite that is cleared when the back gesture
+            // is committed and triggers a transition, or when the gesture is cancelled.
+            mBackGestureStarted = false;
+            mDisplayContent.mSkipAppTransitionAnimation = true;
+            ProtoLog.d(WM_DEBUG_BACK_PREVIEW, "Skipping app transition animation. task=%s", this);
         } else {
             super.applyAnimationUnchecked(lp, enter, transit, isVoiceInteraction, sources);
         }
@@ -3401,7 +3422,7 @@
         info.positionInParent = getRelativePosition();
 
         info.pictureInPictureParams = getPictureInPictureParams(top);
-        info.preferDockBigOverlays = getPreferDockBigOverlays();
+        info.shouldDockBigOverlays = shouldDockBigOverlays();
         if (info.pictureInPictureParams != null
                 && info.pictureInPictureParams.isLaunchIntoPip()
                 && top.getTopMostActivity().getLastParentBeforePip() != null) {
@@ -3454,9 +3475,9 @@
                 ? null : new PictureInPictureParams(topMostActivity.pictureInPictureArgs);
     }
 
-    private boolean getPreferDockBigOverlays() {
+    private boolean shouldDockBigOverlays() {
         final ActivityRecord topMostActivity = getTopMostActivity();
-        return topMostActivity != null && topMostActivity.preferDockBigOverlays;
+        return topMostActivity != null && topMostActivity.shouldDockBigOverlays;
     }
 
     Rect getDisplayCutoutInsets() {
@@ -4352,7 +4373,7 @@
         }
     }
 
-    void onPreferDockBigOverlaysChanged() {
+    void onShouldDockBigOverlaysChanged() {
         dispatchTaskInfoChangedIfNeeded(true /* force */);
     }
 
diff --git a/services/core/java/com/android/server/wm/TaskFragment.java b/services/core/java/com/android/server/wm/TaskFragment.java
index 3411104..597d29f 100644
--- a/services/core/java/com/android/server/wm/TaskFragment.java
+++ b/services/core/java/com/android/server/wm/TaskFragment.java
@@ -242,7 +242,7 @@
 
     /** Client assigned unique token for this TaskFragment if this is created by an organizer. */
     @Nullable
-    private IBinder mFragmentToken;
+    private final IBinder mFragmentToken;
 
     /**
      * Whether to delay the last activity of TaskFragment being immediately removed while finishing.
@@ -2304,6 +2304,10 @@
         mMinHeight = minHeight;
     }
 
+    boolean shouldRemoveSelfOnLastChildRemoval() {
+        return !mCreatedByOrganizer || mIsRemovalRequested;
+    }
+
     @Override
     void removeChild(WindowContainer child) {
         removeChild(child, true /* removeSelfIfPossible */);
@@ -2319,7 +2323,7 @@
                 mBackScreenshots.remove(r.mActivityComponent.flattenToString());
             }
         }
-        if (removeSelfIfPossible && (!mCreatedByOrganizer || mIsRemovalRequested) && !hasChild()) {
+        if (removeSelfIfPossible && shouldRemoveSelfOnLastChildRemoval() && !hasChild()) {
             removeImmediately("removeLastChild " + child);
         }
     }
@@ -2337,13 +2341,18 @@
             return;
         }
         mIsRemovalRequested = true;
-        forAllActivities(r -> {
-            if (withTransition) {
+        // The task order may be changed by finishIfPossible() for adjusting focus if there are
+        // nested tasks, so add all activities into a list to avoid missed removals.
+        final ArrayList<ActivityRecord> removingActivities = new ArrayList<>();
+        forAllActivities((Consumer<ActivityRecord>) removingActivities::add);
+        for (int i = removingActivities.size() - 1; i >= 0; --i) {
+            final ActivityRecord r = removingActivities.get(i);
+            if (withTransition && r.isVisible()) {
                 r.finishIfPossible(reason, false /* oomAdj */);
             } else {
                 r.destroyIfPossible(reason);
             }
-        });
+        }
     }
 
     void setDelayLastActivityRemoval(boolean delay) {
@@ -2383,10 +2392,18 @@
     void removeImmediately() {
         mIsRemovalRequested = false;
         resetAdjacentTaskFragment();
+        cleanUp();
         super.removeImmediately();
         sendTaskFragmentVanished();
     }
 
+    /** Called on remove to cleanup. */
+    private void cleanUp() {
+        if (mIsEmbedded) {
+            mAtmService.mWindowOrganizerController.cleanUpEmbeddedTaskFragment(this);
+        }
+    }
+
     @Override
     Dimmer getDimmer() {
         // If the window is in an embedded TaskFragment, we want to dim at the TaskFragment.
diff --git a/services/core/java/com/android/server/wm/TaskFragmentOrganizerController.java b/services/core/java/com/android/server/wm/TaskFragmentOrganizerController.java
index 19f921d..bdec49e 100644
--- a/services/core/java/com/android/server/wm/TaskFragmentOrganizerController.java
+++ b/services/core/java/com/android/server/wm/TaskFragmentOrganizerController.java
@@ -140,7 +140,7 @@
                 mLastSentTaskFragmentInfos.put(tf, info);
                 tf.mTaskFragmentAppearedSent = true;
             } catch (RemoteException e) {
-                Slog.e(TAG, "Exception sending onTaskFragmentAppeared callback", e);
+                Slog.d(TAG, "Exception sending onTaskFragmentAppeared callback", e);
             }
             onTaskFragmentParentInfoChanged(organizer, tf);
         }
@@ -150,7 +150,7 @@
             try {
                 organizer.onTaskFragmentVanished(tf.getTaskFragmentInfo());
             } catch (RemoteException e) {
-                Slog.e(TAG, "Exception sending onTaskFragmentVanished callback", e);
+                Slog.d(TAG, "Exception sending onTaskFragmentVanished callback", e);
             }
             tf.mTaskFragmentAppearedSent = false;
             mLastSentTaskFragmentInfos.remove(tf);
@@ -175,7 +175,7 @@
                 organizer.onTaskFragmentInfoChanged(tf.getTaskFragmentInfo());
                 mLastSentTaskFragmentInfos.put(tf, info);
             } catch (RemoteException e) {
-                Slog.e(TAG, "Exception sending onTaskFragmentInfoChanged callback", e);
+                Slog.d(TAG, "Exception sending onTaskFragmentInfoChanged callback", e);
             }
         }
 
@@ -198,7 +198,7 @@
                 organizer.onTaskFragmentParentInfoChanged(tf.getFragmentToken(), parentConfig);
                 mLastSentTaskFragmentParentConfigs.put(tf, new Configuration(parentConfig));
             } catch (RemoteException e) {
-                Slog.e(TAG, "Exception sending onTaskFragmentParentInfoChanged callback", e);
+                Slog.d(TAG, "Exception sending onTaskFragmentParentInfoChanged callback", e);
             }
         }
 
@@ -210,7 +210,7 @@
             try {
                 organizer.onTaskFragmentError(errorCallbackToken, exceptionBundle);
             } catch (RemoteException e) {
-                Slog.e(TAG, "Exception sending onTaskFragmentError callback", e);
+                Slog.d(TAG, "Exception sending onTaskFragmentError callback", e);
             }
         }
     }
diff --git a/services/core/java/com/android/server/wm/TaskOrganizerController.java b/services/core/java/com/android/server/wm/TaskOrganizerController.java
index 331f124..ff5bfbe 100644
--- a/services/core/java/com/android/server/wm/TaskOrganizerController.java
+++ b/services/core/java/com/android/server/wm/TaskOrganizerController.java
@@ -800,17 +800,24 @@
         final long origId = Binder.clearCallingIdentity();
         try {
             synchronized (mGlobalLock) {
-                DisplayContent dc = mService.mWindowManager.mRoot
+                final DisplayContent dc = mService.mWindowManager.mRoot
                         .getDisplayContent(displayId);
-                if (dc == null || dc.getImeTarget(IME_TARGET_LAYERING) == null) {
+                if (dc == null) {
                     return null;
                 }
+
+                final InsetsControlTarget imeLayeringTarget = dc.getImeTarget(IME_TARGET_LAYERING);
+                if (imeLayeringTarget == null || imeLayeringTarget.getWindow() == null) {
+                    return null;
+                }
+
                 // Avoid WindowState#getRootTask() so we don't attribute system windows to a task.
-                final Task task = dc.getImeTarget(IME_TARGET_LAYERING).getWindow().getTask();
+                final Task task = imeLayeringTarget.getWindow().asTask();
                 if (task == null) {
                     return null;
                 }
-                return task.getRootTask().mRemoteToken.toWindowContainerToken();
+
+                return task.mRemoteToken.toWindowContainerToken();
             }
         } finally {
             Binder.restoreCallingIdentity(origId);
diff --git a/services/core/java/com/android/server/wm/Transition.java b/services/core/java/com/android/server/wm/Transition.java
index 4c23f39..21ca4bb 100644
--- a/services/core/java/com/android/server/wm/Transition.java
+++ b/services/core/java/com/android/server/wm/Transition.java
@@ -216,6 +216,7 @@
     }
 
     Task getTransientLaunchRestoreTarget(@NonNull WindowContainer container) {
+        if (mTransientLaunches == null) return null;
         for (int i = 0; i < mTransientLaunches.size(); ++i) {
             if (mTransientLaunches.keyAt(i).isDescendantOf(container)) {
                 return mTransientLaunches.valueAt(i);
@@ -491,7 +492,8 @@
                             // Avoid commit visibility to false here, or else we will get a sudden
                             // "flash" / surface going invisible for a split second.
                             commitVisibility = false;
-                        } else {
+                        } else if (ar.getDeferHidingClient()) {
+                            // Legacy PIP-enter requires pause event with user-leaving.
                             mController.mAtm.mTaskSupervisor.mUserLeaving = true;
                             ar.getTaskFragment().startPausing(false /* uiSleeping */,
                                     null /* resuming */, "finishTransition");
diff --git a/services/core/java/com/android/server/wm/WindowManagerService.java b/services/core/java/com/android/server/wm/WindowManagerService.java
index 4e47659..d5f7a22 100644
--- a/services/core/java/com/android/server/wm/WindowManagerService.java
+++ b/services/core/java/com/android/server/wm/WindowManagerService.java
@@ -745,6 +745,9 @@
 
     private final DisplayHashController mDisplayHashController;
 
+    volatile float mMaximumObscuringOpacityForTouch =
+            InputManager.DEFAULT_MAXIMUM_OBSCURING_OPACITY_FOR_TOUCH;
+
     @VisibleForTesting
     final WindowContextListenerController mWindowContextListenerController =
             new WindowContextListenerController();
@@ -782,6 +785,8 @@
                 DEVELOPMENT_RENDER_SHADOWS_IN_COMPOSITOR);
         private final Uri mDisplaySettingsPathUri = Settings.Global.getUriFor(
                 DEVELOPMENT_WM_DISPLAY_SETTINGS_PATH);
+        private final Uri mMaximumObscuringOpacityForTouchUri = Settings.Global.getUriFor(
+                Settings.Global.MAXIMUM_OBSCURING_OPACITY_FOR_TOUCH);
 
         public SettingsObserver() {
             super(new Handler());
@@ -806,6 +811,8 @@
                     UserHandle.USER_ALL);
             resolver.registerContentObserver(mDisplaySettingsPathUri, false, this,
                     UserHandle.USER_ALL);
+            resolver.registerContentObserver(mMaximumObscuringOpacityForTouchUri, false, this,
+                    UserHandle.USER_ALL);
         }
 
         @Override
@@ -849,6 +856,11 @@
                 return;
             }
 
+            if (mMaximumObscuringOpacityForTouchUri.equals(uri)) {
+                updateMaximumObscuringOpacityForTouch();
+                return;
+            }
+
             @UpdateAnimationScaleMode
             final int mode;
             if (mWindowAnimationScaleUri.equals(uri)) {
@@ -868,6 +880,14 @@
         void loadSettings() {
             updateSystemUiSettings(false /* handleChange */);
             updatePointerLocation();
+            updateMaximumObscuringOpacityForTouch();
+        }
+
+        void updateMaximumObscuringOpacityForTouch() {
+            ContentResolver resolver = mContext.getContentResolver();
+            mMaximumObscuringOpacityForTouch = Settings.Global.getFloat(resolver,
+                    Settings.Global.MAXIMUM_OBSCURING_OPACITY_FOR_TOUCH,
+                    InputManager.DEFAULT_MAXIMUM_OBSCURING_OPACITY_FOR_TOUCH);
         }
 
         void updateSystemUiSettings(boolean handleChange) {
@@ -2192,7 +2212,7 @@
             int requestedWidth, int requestedHeight, int viewVisibility, int flags,
             ClientWindowFrames outFrames, MergedConfiguration mergedConfiguration,
             SurfaceControl outSurfaceControl, InsetsState outInsetsState,
-            InsetsSourceControl[] outActiveControls) {
+            InsetsSourceControl[] outActiveControls, Bundle outSyncIdBundle) {
         Arrays.fill(outActiveControls, null);
         int result = 0;
         boolean configChanged;
@@ -2617,7 +2637,7 @@
     }
 
     void finishDrawingWindow(Session session, IWindow client,
-            @Nullable SurfaceControl.Transaction postDrawTransaction) {
+            @Nullable SurfaceControl.Transaction postDrawTransaction, int seqId) {
         if (postDrawTransaction != null) {
             postDrawTransaction.sanitize();
         }
@@ -2628,7 +2648,7 @@
                 WindowState win = windowForClientLocked(session, client, false);
                 ProtoLog.d(WM_DEBUG_ADD_REMOVE, "finishDrawingWindow: %s mDrawState=%s",
                         win, (win != null ? win.mWinAnimator.drawStateToString() : "null"));
-                if (win != null && win.finishDrawing(postDrawTransaction)) {
+                if (win != null && win.finishDrawing(postDrawTransaction, seqId)) {
                     if (win.hasWallpaper()) {
                         win.getDisplayContent().pendingLayoutChanges |=
                                 WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
@@ -3233,8 +3253,8 @@
         if (!checkCallingPermission(permission.CONTROL_KEYGUARD, "dismissKeyguard")) {
             throw new SecurityException("Requires CONTROL_KEYGUARD permission");
         }
-        if (mAtmService.isDreaming()) {
-            mAtmService.mTaskSupervisor.wakeUp("dismissKeyguard");
+        if (mAtmService.mKeyguardController.isShowingDream()) {
+            mAtmService.mTaskSupervisor.wakeUp("leaveDream");
         }
         synchronized (mGlobalLock) {
             mPolicy.dismissKeyguardLw(callback, message);
diff --git a/services/core/java/com/android/server/wm/WindowManagerShellCommand.java b/services/core/java/com/android/server/wm/WindowManagerShellCommand.java
index 1cf4c1b..5a2f28f 100644
--- a/services/core/java/com/android/server/wm/WindowManagerShellCommand.java
+++ b/services/core/java/com/android/server/wm/WindowManagerShellCommand.java
@@ -566,9 +566,14 @@
         try (ZipOutputStream out = new ZipOutputStream(getRawOutputStream())) {
             ArrayList<Pair<String, ByteTransferPipe>> requestList = new ArrayList<>();
             synchronized (mInternal.mGlobalLock) {
+                final RecentTasks recentTasks = mInternal.mAtmService.getRecentTasks();
+                final int recentsComponentUid = recentTasks != null
+                        ? recentTasks.getRecentsComponentUid()
+                        : -1;
                 // Request dump from all windows parallelly before writing to disk.
                 mInternal.mRoot.forAllWindows(w -> {
-                    if (w.isVisible()) {
+                    final boolean isRecents = (w.getUid() == recentsComponentUid);
+                    if (w.isVisible() || isRecents) {
                         ByteTransferPipe pipe = null;
                         try {
                             pipe = new ByteTransferPipe();
diff --git a/services/core/java/com/android/server/wm/WindowOrganizerController.java b/services/core/java/com/android/server/wm/WindowOrganizerController.java
index 81344ac..d862012 100644
--- a/services/core/java/com/android/server/wm/WindowOrganizerController.java
+++ b/services/core/java/com/android/server/wm/WindowOrganizerController.java
@@ -1470,6 +1470,10 @@
         return mLaunchTaskFragments.get(tfToken);
     }
 
+    void cleanUpEmbeddedTaskFragment(TaskFragment taskFragment) {
+        mLaunchTaskFragments.remove(taskFragment.getFragmentToken());
+    }
+
     static class CallerInfo {
         final int mPid;
         final int mUid;
diff --git a/services/core/java/com/android/server/wm/WindowState.java b/services/core/java/com/android/server/wm/WindowState.java
index db687f6..f667392 100644
--- a/services/core/java/com/android/server/wm/WindowState.java
+++ b/services/core/java/com/android/server/wm/WindowState.java
@@ -1393,13 +1393,13 @@
     }
 
     // TODO(b/161810301): Make the frame be passed from the client side.
-    void setFrame() {
-        // TODO(b/161810301): Set the window frame here. We don't have to do it now because
-        //                    DisplayPolicy has already done it for the window.
-
+    void setFrames(ClientWindowFrames clientWindowFrames) {
         mHaveFrame = true;
 
         final WindowFrames windowFrames = mWindowFrames;
+        windowFrames.mDisplayFrame.set(clientWindowFrames.displayFrame);
+        windowFrames.mParentFrame.set(clientWindowFrames.parentFrame);
+        windowFrames.mFrame.set(clientWindowFrames.frame);
 
         if (mRequestedWidth != mLastRequestedWidth || mRequestedHeight != mLastRequestedHeight) {
             mLastRequestedWidth = mRequestedWidth;
@@ -3946,7 +3946,7 @@
 
         try {
             mClient.resized(mClientWindowFrames, reportDraw, mLastReportedConfiguration,
-                    forceRelayout, alwaysConsumeSystemBars, displayId);
+                forceRelayout, alwaysConsumeSystemBars, displayId, Integer.MAX_VALUE);
             if (drawPending && reportOrientation && mOrientationChanging) {
                 mOrientationChangeRedrawRequestTime = SystemClock.elapsedRealtime();
                 ProtoLog.v(WM_DEBUG_ORIENTATION,
@@ -5938,7 +5938,7 @@
         super.finishSync(outMergedTransaction, cancel);
     }
 
-    boolean finishDrawing(SurfaceControl.Transaction postDrawTransaction) {
+    boolean finishDrawing(SurfaceControl.Transaction postDrawTransaction, int syncSeqId) {
         if (mOrientationChangeRedrawRequestTime > 0) {
             final long duration =
                     SystemClock.elapsedRealtime() - mOrientationChangeRedrawRequestTime;
@@ -5985,7 +5985,7 @@
 
     void immediatelyNotifyBlastSync() {
         prepareDrawHandlers();
-        finishDrawing(null);
+        finishDrawing(null, Integer.MAX_VALUE);
         mWmService.mH.removeMessages(WINDOW_STATE_BLAST_SYNC_TIMEOUT, this);
         if (!useBLASTSync()) return;
     }
diff --git a/services/core/jni/onload.cpp b/services/core/jni/onload.cpp
index 1c6a3b5..0cd9494 100644
--- a/services/core/jni/onload.cpp
+++ b/services/core/jni/onload.cpp
@@ -106,7 +106,6 @@
     register_android_server_HardwarePropertiesManagerService(env);
     register_android_server_storage_AppFuse(env);
     register_android_server_SyntheticPasswordManager(env);
-    register_android_graphics_GraphicsStatsService(env);
     register_android_hardware_display_DisplayViewport(env);
     register_android_server_am_CachedAppOptimizer(env);
     register_android_server_am_LowMemDetector(env);
diff --git a/services/devicepolicy/java/com/android/server/devicepolicy/ActiveAdmin.java b/services/devicepolicy/java/com/android/server/devicepolicy/ActiveAdmin.java
index 2090ab3..2f5ab0b 100644
--- a/services/devicepolicy/java/com/android/server/devicepolicy/ActiveAdmin.java
+++ b/services/devicepolicy/java/com/android/server/devicepolicy/ActiveAdmin.java
@@ -307,8 +307,8 @@
     public boolean mAdminCanGrantSensorsPermissions;
     public boolean mPreferentialNetworkServiceEnabled =
             DevicePolicyManager.PREFERENTIAL_NETWORK_SERVICE_ENABLED_DEFAULT;
-    public PreferentialNetworkServiceConfig mPreferentialNetworkServiceConfig =
-            PreferentialNetworkServiceConfig.DEFAULT;
+    public List<PreferentialNetworkServiceConfig> mPreferentialNetworkServiceConfigs =
+            List.of(PreferentialNetworkServiceConfig.DEFAULT);
 
     private static final boolean USB_DATA_SIGNALING_ENABLED_DEFAULT = true;
     boolean mUsbDataSignalingEnabled = USB_DATA_SIGNALING_ENABLED_DEFAULT;
diff --git a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java
index 5388f17..3d40f48 100644
--- a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java
+++ b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java
@@ -133,7 +133,6 @@
 import static android.net.ConnectivityManager.PROFILE_NETWORK_PREFERENCE_DEFAULT;
 import static android.net.ConnectivityManager.PROFILE_NETWORK_PREFERENCE_ENTERPRISE;
 import static android.net.ConnectivityManager.PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK;
-import static android.net.NetworkCapabilities.NET_ENTERPRISE_ID_1;
 import static android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK;
 import static android.provider.Settings.Global.PRIVATE_DNS_SPECIFIER;
 import static android.provider.Settings.Secure.MANAGED_PROVISIONING_DPC_DOWNLOADED;
@@ -141,6 +140,7 @@
 import static android.provider.Telephony.Carriers.DPC_URI;
 import static android.provider.Telephony.Carriers.ENFORCE_KEY;
 import static android.provider.Telephony.Carriers.ENFORCE_MANAGED_URI;
+import static android.provider.Telephony.Carriers.INVALID_APN_ID;
 import static android.security.keystore.AttestationUtils.USE_INDIVIDUAL_ATTESTATION;
 
 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PROVISIONING_ENTRY_POINT_ADB;
@@ -3357,14 +3357,14 @@
         updatePermissionPolicyCache(userId);
         updateAdminCanGrantSensorsPermissionCache(userId);
 
-        final PreferentialNetworkServiceConfig preferentialNetworkServiceConfig;
+        final List<PreferentialNetworkServiceConfig> preferentialNetworkServiceConfigs;
         synchronized (getLockObject()) {
             ActiveAdmin owner = getDeviceOrProfileOwnerAdminLocked(userId);
-            preferentialNetworkServiceConfig = owner != null
-                    ? owner.mPreferentialNetworkServiceConfig
-                    : PreferentialNetworkServiceConfig.DEFAULT;
+            preferentialNetworkServiceConfigs = owner != null
+                    ? owner.mPreferentialNetworkServiceConfigs
+                    : List.of(PreferentialNetworkServiceConfig.DEFAULT);
         }
-        updateNetworkPreferenceForUser(userId, preferentialNetworkServiceConfig);
+        updateNetworkPreferenceForUser(userId, preferentialNetworkServiceConfigs);
 
         startOwnerService(userId, "start-user");
     }
@@ -3381,7 +3381,7 @@
 
     @Override
     void handleStopUser(int userId) {
-        updateNetworkPreferenceForUser(userId, PreferentialNetworkServiceConfig.DEFAULT);
+        updateNetworkPreferenceForUser(userId, List.of(PreferentialNetworkServiceConfig.DEFAULT));
         stopOwnerService(userId, "stop-user");
     }
 
@@ -12258,87 +12258,50 @@
     }
 
     @Override
-    public void setPreferentialNetworkServiceEnabled(boolean enabled) {
+    public void setPreferentialNetworkServiceConfigs(
+            List<PreferentialNetworkServiceConfig> preferentialNetworkServiceConfigs) {
         if (!mHasFeature) {
             return;
         }
         final CallerIdentity caller = getCallerIdentity();
-        Preconditions.checkCallAuthorization(isProfileOwner(caller),
-                "Caller is not profile owner;"
-                        + " only profile owner may control the preferential network service");
+        Preconditions.checkCallAuthorization(isProfileOwner(caller)
+                        || isDefaultDeviceOwner(caller),
+                "Caller is not profile owner or device owner;"
+                        + " only profile owner or device owner may control the preferential"
+                        + " network service");
         synchronized (getLockObject()) {
             final ActiveAdmin requiredAdmin = getProfileOwnerAdminLocked(
                     caller.getUserId());
-            if (requiredAdmin != null
-                    && requiredAdmin.mPreferentialNetworkServiceEnabled != enabled) {
-                requiredAdmin.mPreferentialNetworkServiceEnabled = enabled;
+            if (!requiredAdmin.mPreferentialNetworkServiceConfigs.equals(
+                    preferentialNetworkServiceConfigs)) {
+                requiredAdmin.mPreferentialNetworkServiceConfigs =
+                        new ArrayList<>(preferentialNetworkServiceConfigs);
                 saveSettingsLocked(caller.getUserId());
             }
         }
-        updateNetworkPreferenceForUser(caller.getUserId(), enabled);
+        updateNetworkPreferenceForUser(caller.getUserId(), preferentialNetworkServiceConfigs);
         DevicePolicyEventLogger
                 .createEvent(DevicePolicyEnums.SET_PREFERENTIAL_NETWORK_SERVICE_ENABLED)
-                .setBoolean(enabled)
+                .setBoolean(preferentialNetworkServiceConfigs
+                        .stream().anyMatch(c -> c.isEnabled()))
                 .write();
     }
 
     @Override
-    public boolean isPreferentialNetworkServiceEnabled(int userHandle) {
+    public List<PreferentialNetworkServiceConfig> getPreferentialNetworkServiceConfigs() {
         if (!mHasFeature) {
-            return false;
+            return List.of(PreferentialNetworkServiceConfig.DEFAULT);
         }
 
         final CallerIdentity caller = getCallerIdentity();
-        Preconditions.checkCallAuthorization(isProfileOwner(caller),
-                "Caller is not profile owner");
-        synchronized (getLockObject()) {
-            final ActiveAdmin requiredAdmin = getProfileOwnerAdminLocked(userHandle);
-            if (requiredAdmin != null) {
-                return requiredAdmin.mPreferentialNetworkServiceEnabled;
-            } else {
-                return false;
-            }
-        }
-    }
-
-    @Override
-    public void setPreferentialNetworkServiceConfig(
-            PreferentialNetworkServiceConfig preferentialNetworkServiceConfig) {
-        if (!mHasFeature) {
-            return;
-        }
-        final CallerIdentity caller = getCallerIdentity();
-        Preconditions.checkCallAuthorization(isProfileOwner(caller),
-                "Caller is not profile owner;"
-                        + " only profile owner may control the preferential network service");
-        synchronized (getLockObject()) {
-            final ActiveAdmin requiredAdmin = getProfileOwnerAdminLocked(
-                    caller.getUserId());
-            if (!requiredAdmin.mPreferentialNetworkServiceConfig.equals(
-                    preferentialNetworkServiceConfig)) {
-                requiredAdmin.mPreferentialNetworkServiceConfig = preferentialNetworkServiceConfig;
-                saveSettingsLocked(caller.getUserId());
-            }
-        }
-        updateNetworkPreferenceForUser(caller.getUserId(), preferentialNetworkServiceConfig);
-        DevicePolicyEventLogger
-                .createEvent(DevicePolicyEnums.SET_PREFERENTIAL_NETWORK_SERVICE_ENABLED)
-                .setBoolean(preferentialNetworkServiceConfig.isEnabled())
-                .write();
-    }
-
-    @Override
-    public PreferentialNetworkServiceConfig getPreferentialNetworkServiceConfig() {
-        if (!mHasFeature) {
-            return PreferentialNetworkServiceConfig.DEFAULT;
-        }
-
-        final CallerIdentity caller = getCallerIdentity();
-        Preconditions.checkCallAuthorization(isProfileOwner(caller),
-                "Caller is not profile owner");
+        Preconditions.checkCallAuthorization(isProfileOwner(caller)
+                        || isDefaultDeviceOwner(caller),
+                "Caller is not profile owner or device owner;"
+                        + " only profile owner or device owner may retrieve the preferential"
+                        + " network service configurations");
         synchronized (getLockObject()) {
             final ActiveAdmin requiredAdmin = getProfileOwnerAdminLocked(caller.getUserId());
-            return requiredAdmin.mPreferentialNetworkServiceConfig;
+            return requiredAdmin.mPreferentialNetworkServiceConfigs;
         }
     }
 
@@ -16459,7 +16422,12 @@
         Objects.requireNonNull(who, "ComponentName is null");
         Objects.requireNonNull(apnSetting, "ApnSetting is null in addOverrideApn");
         final CallerIdentity caller = getCallerIdentity(who);
-        Preconditions.checkCallAuthorization(isDefaultDeviceOwner(caller));
+        if (apnSetting.getApnTypeBitmask() == ApnSetting.TYPE_ENTERPRISE) {
+            Preconditions.checkCallAuthorization(isDefaultDeviceOwner(caller)
+                    || isProfileOwner(caller));
+        } else {
+            Preconditions.checkCallAuthorization(isDefaultDeviceOwner(caller));
+        }
 
         TelephonyManager tm = mContext.getSystemService(TelephonyManager.class);
         if (tm != null) {
@@ -16467,7 +16435,7 @@
                     () -> tm.addDevicePolicyOverrideApn(mContext, apnSetting));
         } else {
             Slogf.w(LOG_TAG, "TelephonyManager is null when trying to add override apn");
-            return Telephony.Carriers.INVALID_APN_ID;
+            return INVALID_APN_ID;
         }
     }
 
@@ -16480,7 +16448,14 @@
         Objects.requireNonNull(who, "ComponentName is null");
         Objects.requireNonNull(apnSetting, "ApnSetting is null in updateOverrideApn");
         final CallerIdentity caller = getCallerIdentity(who);
-        Preconditions.checkCallAuthorization(isDefaultDeviceOwner(caller));
+        ApnSetting apn = getApnSetting(apnId);
+        if (apn != null && apn.getApnTypeBitmask() == ApnSetting.TYPE_ENTERPRISE
+                && apnSetting.getApnTypeBitmask() == ApnSetting.TYPE_ENTERPRISE) {
+            Preconditions.checkCallAuthorization(isDefaultDeviceOwner(caller)
+                    || isProfileOwner(caller));
+        } else {
+            Preconditions.checkCallAuthorization(isDefaultDeviceOwner(caller));
+        }
 
         if (apnId < 0) {
             return false;
@@ -16502,7 +16477,13 @@
         }
         Objects.requireNonNull(who, "ComponentName is null");
         final CallerIdentity caller = getCallerIdentity(who);
-        Preconditions.checkCallAuthorization(isDefaultDeviceOwner(caller));
+        ApnSetting apn = getApnSetting(apnId);
+        if (apn != null && apn.getApnTypeBitmask() == ApnSetting.TYPE_ENTERPRISE) {
+            Preconditions.checkCallAuthorization(isDefaultDeviceOwner(caller)
+                    || isProfileOwner(caller));
+        } else {
+            Preconditions.checkCallAuthorization(isDefaultDeviceOwner(caller));
+        }
         return removeOverrideApnUnchecked(apnId);
     }
 
@@ -16516,6 +16497,27 @@
         return numDeleted > 0;
     }
 
+    private ApnSetting getApnSetting(int apnId) {
+        if (apnId < 0) {
+            return null;
+        }
+        ApnSetting apnSetting = null;
+        Cursor cursor = mInjector.binderWithCleanCallingIdentity(
+                () -> mContext.getContentResolver().query(
+                        Uri.withAppendedPath(DPC_URI, Integer.toString(apnId)), null, null, null,
+                        Telephony.Carriers.DEFAULT_SORT_ORDER));
+        if (cursor != null) {
+            while (cursor.moveToNext()) {
+                apnSetting = ApnSetting.makeApnSetting(cursor);
+                if (apnSetting != null) {
+                    break;
+                }
+            }
+            cursor.close();
+        }
+        return apnSetting;
+    }
+
     @Override
     public List<ApnSetting> getOverrideApns(@NonNull ComponentName who) {
         if (!mHasFeature || !mHasTelephonyFeature) {
@@ -18363,54 +18365,38 @@
     }
 
     private void updateNetworkPreferenceForUser(int userId,
-            boolean preferentialNetworkServiceEnabled) {
+            List<PreferentialNetworkServiceConfig> preferentialNetworkServiceConfigs) {
         if (!isManagedProfile(userId)) {
             return;
         }
-        ProfileNetworkPreference.Builder preferenceBuilder =
-                new ProfileNetworkPreference.Builder();
-        if (preferentialNetworkServiceEnabled) {
-            preferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE);
-            preferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1);
-        } else {
-            preferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_DEFAULT);
-        }
         List<ProfileNetworkPreference> preferences = new ArrayList<>();
-        preferences.add(preferenceBuilder.build());
-        mInjector.binderWithCleanCallingIdentity(() ->
-                mInjector.getConnectivityManager().setProfileNetworkPreferences(
-                        UserHandle.of(userId), preferences,
-                        null /* executor */, null /* listener */));
-    }
-
-    private void updateNetworkPreferenceForUser(int userId,
-            PreferentialNetworkServiceConfig preferentialNetworkServiceConfig) {
-        if (!isManagedProfile(userId)) {
-            return;
-        }
-        ProfileNetworkPreference.Builder preferenceBuilder =
-                new ProfileNetworkPreference.Builder();
-        if (preferentialNetworkServiceConfig.isEnabled()) {
-            if (preferentialNetworkServiceConfig.isFallbackToDefaultConnectionAllowed()) {
-                preferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE);
+        for (PreferentialNetworkServiceConfig preferentialNetworkServiceConfig :
+                preferentialNetworkServiceConfigs) {
+            ProfileNetworkPreference.Builder preferenceBuilder =
+                    new ProfileNetworkPreference.Builder();
+            if (preferentialNetworkServiceConfig.isEnabled()) {
+                if (preferentialNetworkServiceConfig.isFallbackToDefaultConnectionAllowed()) {
+                    preferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE);
+                } else {
+                    preferenceBuilder.setPreference(
+                            PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK);
+                }
             } else {
-                preferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK);
+                preferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_DEFAULT);
             }
-        } else {
-            preferenceBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_DEFAULT);
+            List<Integer> allowedUids = Arrays.stream(
+                    preferentialNetworkServiceConfig.getIncludedUids()).boxed().collect(
+                    Collectors.toList());
+            List<Integer> excludedUids = Arrays.stream(
+                    preferentialNetworkServiceConfig.getExcludedUids()).boxed().collect(
+                    Collectors.toList());
+            preferenceBuilder.setIncludedUids(allowedUids);
+            preferenceBuilder.setExcludedUids(excludedUids);
+            preferenceBuilder.setPreferenceEnterpriseId(
+                    preferentialNetworkServiceConfig.getNetworkId());
+
+            preferences.add(preferenceBuilder.build());
         }
-        List<Integer> allowedUids = Arrays.stream(
-                preferentialNetworkServiceConfig.getIncludedUids()).boxed().collect(
-                        Collectors.toList());
-        List<Integer> excludedUids = Arrays.stream(
-                preferentialNetworkServiceConfig.getExcludedUids()).boxed().collect(
-                Collectors.toList());
-        preferenceBuilder.setIncludedUids(allowedUids);
-        preferenceBuilder.setExcludedUids(excludedUids);
-        preferenceBuilder.setPreferenceEnterpriseId(
-                preferentialNetworkServiceConfig.getNetworkId());
-        List<ProfileNetworkPreference> preferences = new ArrayList<>();
-        preferences.add(preferenceBuilder.build());
         mInjector.binderWithCleanCallingIdentity(() ->
                 mInjector.getConnectivityManager().setProfileNetworkPreferences(
                         UserHandle.of(userId), preferences,
diff --git a/services/java/com/android/server/SystemServer.java b/services/java/com/android/server/SystemServer.java
index fa2850a..f7c66c5 100644
--- a/services/java/com/android/server/SystemServer.java
+++ b/services/java/com/android/server/SystemServer.java
@@ -293,8 +293,6 @@
             "com.android.server.wifi.p2p.WifiP2pService";
     private static final String LOWPAN_SERVICE_CLASS =
             "com.android.server.lowpan.LowpanService";
-    private static final String ETHERNET_SERVICE_CLASS =
-            "com.android.server.ethernet.EthernetService";
     private static final String JOB_SCHEDULER_SERVICE_CLASS =
             "com.android.server.job.JobSchedulerService";
     private static final String LOCK_SETTINGS_SERVICE_CLASS =
@@ -423,6 +421,8 @@
 
     private static final String SDK_SANDBOX_MANAGER_SERVICE_CLASS =
             "com.android.server.sdksandbox.SdkSandboxManagerService$Lifecycle";
+    private static final String AD_SERVICES_MANAGER_SERVICE_CLASS =
+            "com.android.server.adservices.AdServicesManagerService$Lifecycle";
 
     private static final String TETHERING_CONNECTOR_CLASS = "android.net.ITetheringConnector";
 
@@ -1993,13 +1993,6 @@
                 t.traceEnd();
             }
 
-            if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_ETHERNET) ||
-                    mPackageManager.hasSystemFeature(PackageManager.FEATURE_USB_HOST)) {
-                t.traceBegin("StartEthernet");
-                mSystemServiceManager.startService(ETHERNET_SERVICE_CLASS);
-                t.traceEnd();
-            }
-
             t.traceBegin("StartPacProxyService");
             try {
                 pacProxyService = new PacProxyService(context);
@@ -2608,6 +2601,11 @@
         mSystemServiceManager.startService(SDK_SANDBOX_MANAGER_SERVICE_CLASS);
         t.traceEnd();
 
+        // AdServicesManagerService (PP API service)
+        t.traceBegin("StartAdServicesManagerService");
+        mSystemServiceManager.startService(AD_SERVICES_MANAGER_SERVICE_CLASS);
+        t.traceEnd();
+
         if (safeMode) {
             mActivityManagerService.enterSafeMode();
         }
diff --git a/services/profcollect/src/com/android/server/profcollect/ProfcollectForwardingService.java b/services/profcollect/src/com/android/server/profcollect/ProfcollectForwardingService.java
index c5f990d..66e840b 100644
--- a/services/profcollect/src/com/android/server/profcollect/ProfcollectForwardingService.java
+++ b/services/profcollect/src/com/android/server/profcollect/ProfcollectForwardingService.java
@@ -112,7 +112,7 @@
         try {
             mIProfcollect.registerProviderStatusCallback(mProviderStatusCallback);
         } catch (RemoteException e) {
-            Log.e(LOG_TAG, e.getMessage());
+            Log.e(LOG_TAG, "Failed to register provider status callback: " + e.getMessage());
         }
     }
 
@@ -123,7 +123,7 @@
         try {
             return !mIProfcollect.get_supported_provider().isEmpty();
         } catch (RemoteException e) {
-            Log.e(LOG_TAG, e.getMessage());
+            Log.e(LOG_TAG, "Failed to get supported provider: " + e.getMessage());
             return false;
         }
     }
@@ -219,7 +219,8 @@
                         try {
                             sSelfService.mIProfcollect.process();
                         } catch (RemoteException e) {
-                            Log.e(LOG_TAG, e.getMessage());
+                            Log.e(LOG_TAG, "Failed to process profiles in background: "
+                                    + e.getMessage());
                         }
                     });
             return true;
@@ -234,8 +235,11 @@
 
     // Event observers
     private void registerObservers() {
-        registerAppLaunchObserver();
-        registerOTAObserver();
+        BackgroundThread.get().getThreadHandler().post(
+                () -> {
+                    registerAppLaunchObserver();
+                    registerOTAObserver();
+                });
     }
 
     private final AppLaunchObserver mAppLaunchObserver = new AppLaunchObserver();
@@ -264,7 +268,7 @@
                 try {
                     mIProfcollect.trace_once("applaunch");
                 } catch (RemoteException e) {
-                    Log.e(LOG_TAG, e.getMessage());
+                    Log.e(LOG_TAG, "Failed to initiate trace: " + e.getMessage());
                 }
             });
         }
@@ -348,7 +352,7 @@
                         .putExtra("filename", reportName);
                 context.sendBroadcast(intent);
             } catch (RemoteException e) {
-                Log.e(LOG_TAG, e.getMessage());
+                Log.e(LOG_TAG, "Failed to upload report: " + e.getMessage());
             }
         });
     }
diff --git a/services/tests/mockingservicestests/src/com/android/server/alarm/AlarmManagerServiceTest.java b/services/tests/mockingservicestests/src/com/android/server/alarm/AlarmManagerServiceTest.java
index f05658b..e096687 100644
--- a/services/tests/mockingservicestests/src/com/android/server/alarm/AlarmManagerServiceTest.java
+++ b/services/tests/mockingservicestests/src/com/android/server/alarm/AlarmManagerServiceTest.java
@@ -494,9 +494,9 @@
 
         final ArgumentCaptor<AlarmManagerService.UninstallReceiver> packageReceiverCaptor =
                 ArgumentCaptor.forClass(AlarmManagerService.UninstallReceiver.class);
-        verify(mMockContext).registerReceiver(packageReceiverCaptor.capture(),
+        verify(mMockContext).registerReceiverForAllUsers(packageReceiverCaptor.capture(),
                 argThat((filter) -> filter.hasAction(Intent.ACTION_PACKAGE_ADDED)
-                        && filter.hasAction(Intent.ACTION_PACKAGE_REMOVED)));
+                        && filter.hasAction(Intent.ACTION_PACKAGE_REMOVED)), isNull(), isNull());
         mPackageChangesReceiver = packageReceiverCaptor.getValue();
 
         assertEquals(mService.mExactAlarmCandidates, Collections.emptySet());
diff --git a/services/tests/mockingservicestests/src/com/android/server/app/GameServiceProviderInstanceImplTest.java b/services/tests/mockingservicestests/src/com/android/server/app/GameServiceProviderInstanceImplTest.java
index 575e351..319a769 100644
--- a/services/tests/mockingservicestests/src/com/android/server/app/GameServiceProviderInstanceImplTest.java
+++ b/services/tests/mockingservicestests/src/com/android/server/app/GameServiceProviderInstanceImplTest.java
@@ -19,6 +19,7 @@
 import static com.android.dx.mockito.inline.extended.ExtendedMockito.doAnswer;
 import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession;
 import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify;
+import static com.android.dx.mockito.inline.extended.ExtendedMockito.verifyNoMoreInteractions;
 import static com.android.dx.mockito.inline.extended.ExtendedMockito.when;
 import static com.android.server.app.GameServiceProviderInstanceImplTest.FakeGameService.GameServiceState;
 
@@ -26,19 +27,21 @@
 import static com.google.common.truth.Truth.assertThat;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertThrows;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyBoolean;
 import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.ArgumentMatchers.eq;
-import static org.mockito.Mockito.verifyZeroInteractions;
 
 import android.Manifest;
 import android.annotation.Nullable;
 import android.app.ActivityManager.RunningTaskInfo;
+import android.app.ActivityManagerInternal;
 import android.app.ActivityTaskManager;
 import android.app.IActivityManager;
 import android.app.IActivityTaskManager;
+import android.app.IProcessObserver;
 import android.app.ITaskStackListener;
 import android.content.ComponentName;
 import android.content.Context;
@@ -135,6 +138,8 @@
     private MockitoSession mMockingSession;
     private GameServiceProviderInstance mGameServiceProviderInstance;
     @Mock
+    private ActivityManagerInternal mMockActivityManagerInternal;
+    @Mock
     private IActivityTaskManager mMockActivityTaskManager;
     @Mock
     private WindowManagerService mMockWindowManagerService;
@@ -151,6 +156,7 @@
     private FakeGameSessionService mFakeGameSessionService;
     private FakeServiceConnector<IGameSessionService> mFakeGameSessionServiceConnector;
     private ArrayList<ITaskStackListener> mTaskStackListeners;
+    private ArrayList<IProcessObserver> mProcessObservers;
     private ArrayList<TaskSystemBarsListener> mTaskSystemBarsListeners;
     private ArrayList<RunningTaskInfo> mRunningTaskInfos;
 
@@ -185,6 +191,16 @@
             return null;
         }).when(mMockActivityTaskManager).unregisterTaskStackListener(any());
 
+        mProcessObservers = new ArrayList<>();
+        doAnswer(invocation -> {
+            mProcessObservers.add(invocation.getArgument(0));
+            return null;
+        }).when(mMockActivityManager).registerProcessObserver(any());
+        doAnswer(invocation -> {
+            mProcessObservers.remove(invocation.getArgument(0));
+            return null;
+        }).when(mMockActivityManager).unregisterProcessObserver(any());
+
         mTaskSystemBarsListeners = new ArrayList<>();
         doAnswer(invocation -> {
             mTaskSystemBarsListeners.add(invocation.getArgument(0));
@@ -206,6 +222,7 @@
                 mMockContext,
                 mFakeGameClassifier,
                 mMockActivityManager,
+                mMockActivityManagerInternal,
                 mMockActivityTaskManager,
                 mMockWindowManagerService,
                 mMockWindowManagerInternal,
@@ -429,6 +446,214 @@
     }
 
     @Test
+    public void gameProcessStopped_soleProcess_destroysGameSession() throws Exception {
+        int gameProcessId = 1000;
+
+        mGameServiceProviderInstance.start();
+
+        startTask(10, GAME_A_MAIN_ACTIVITY);
+        startProcessForPackage(gameProcessId, GAME_A_PACKAGE);
+
+        mockPermissionGranted(Manifest.permission.MANAGE_GAME_ACTIVITY);
+        mFakeGameService.requestCreateGameSession(10);
+
+        FakeGameSession gameSession10 = new FakeGameSession();
+        SurfacePackage mockSurfacePackage10 = Mockito.mock(SurfacePackage.class);
+        mFakeGameSessionService.removePendingFutureForTaskId(10)
+                .complete(new CreateGameSessionResult(gameSession10, mockSurfacePackage10));
+        assertThat(gameSession10.mIsDestroyed).isFalse();
+
+        // Death of the sole game process destroys the game session.
+        dispatchProcessDied(gameProcessId);
+        assertThat(gameSession10.mIsDestroyed).isTrue();
+    }
+
+    @Test
+    public void gameProcessStopped_soleProcess_destroysMultipleGameSessionsForSamePackage()
+            throws Exception {
+        int gameProcessId = 1000;
+
+        mGameServiceProviderInstance.start();
+
+        // Multiple tasks exist for the same package.
+        startTask(10, GAME_A_MAIN_ACTIVITY);
+        startTask(11, GAME_A_MAIN_ACTIVITY);
+        startProcessForPackage(gameProcessId, GAME_A_PACKAGE);
+
+        mockPermissionGranted(Manifest.permission.MANAGE_GAME_ACTIVITY);
+        mFakeGameService.requestCreateGameSession(10);
+        mFakeGameService.requestCreateGameSession(11);
+
+        FakeGameSession gameSession10 = new FakeGameSession();
+        SurfacePackage mockSurfacePackage10 = Mockito.mock(SurfacePackage.class);
+        mFakeGameSessionService.removePendingFutureForTaskId(10)
+                .complete(new CreateGameSessionResult(gameSession10, mockSurfacePackage10));
+        FakeGameSession gameSession11 = new FakeGameSession();
+        SurfacePackage mockSurfacePackage11 = Mockito.mock(SurfacePackage.class);
+        mFakeGameSessionService.removePendingFutureForTaskId(11)
+                .complete(new CreateGameSessionResult(gameSession11, mockSurfacePackage11));
+
+        assertThat(gameSession10.mIsDestroyed).isFalse();
+        assertThat(gameSession11.mIsDestroyed).isFalse();
+
+        // Death of the sole game process destroys both game sessions.
+        dispatchProcessDied(gameProcessId);
+        assertThat(gameSession10.mIsDestroyed).isTrue();
+        assertThat(gameSession11.mIsDestroyed).isTrue();
+    }
+
+    @Test
+    public void gameProcessStopped_multipleProcesses_gameSessionDestroyedWhenAllDead()
+            throws Exception {
+        int firstGameProcessId = 1000;
+        int secondGameProcessId = 1001;
+
+        mGameServiceProviderInstance.start();
+
+        startTask(10, GAME_A_MAIN_ACTIVITY);
+        startProcessForPackage(firstGameProcessId, GAME_A_PACKAGE);
+        startProcessForPackage(secondGameProcessId, GAME_A_PACKAGE);
+
+        mockPermissionGranted(Manifest.permission.MANAGE_GAME_ACTIVITY);
+        mFakeGameService.requestCreateGameSession(10);
+
+        FakeGameSession gameSession10 = new FakeGameSession();
+        SurfacePackage mockSurfacePackage10 = Mockito.mock(SurfacePackage.class);
+        mFakeGameSessionService.removePendingFutureForTaskId(10)
+                .complete(new CreateGameSessionResult(gameSession10, mockSurfacePackage10));
+        assertThat(gameSession10.mIsDestroyed).isFalse();
+
+        // Death of the first process (with the second one still alive) does not destroy the game
+        // session.
+        dispatchProcessDied(firstGameProcessId);
+        assertThat(gameSession10.mIsDestroyed).isFalse();
+
+        // Death of the second process does destroy the game session.
+        dispatchProcessDied(secondGameProcessId);
+        assertThat(gameSession10.mIsDestroyed).isTrue();
+    }
+
+    @Test
+    public void gameProcessCreatedAfterInitialProcessDead_newGameSessionCreated() throws Exception {
+        int firstGameProcessId = 1000;
+        int secondGameProcessId = 1000;
+
+        mGameServiceProviderInstance.start();
+
+        startTask(10, GAME_A_MAIN_ACTIVITY);
+        startProcessForPackage(firstGameProcessId, GAME_A_PACKAGE);
+
+        mockPermissionGranted(Manifest.permission.MANAGE_GAME_ACTIVITY);
+        mFakeGameService.requestCreateGameSession(10);
+
+        FakeGameSession gameSession10 = new FakeGameSession();
+        SurfacePackage mockSurfacePackage10 = Mockito.mock(SurfacePackage.class);
+        mFakeGameSessionService.removePendingFutureForTaskId(10)
+                .complete(new CreateGameSessionResult(gameSession10, mockSurfacePackage10));
+        assertThat(gameSession10.mIsDestroyed).isFalse();
+
+        // After the first game process dies, the game session should be destroyed.
+        dispatchProcessDied(firstGameProcessId);
+        assertThat(gameSession10.mIsDestroyed).isTrue();
+
+        // However, when a new process for the game starts, a new game session should be created.
+        startProcessForPackage(secondGameProcessId, GAME_A_PACKAGE);
+        // Verify that a new pending game session is created for the game's taskId.
+        assertNotNull(mFakeGameSessionService.removePendingFutureForTaskId(10));
+    }
+
+    @Test
+    public void gameProcessCreatedAfterInitialProcessDead_multipleGameSessionsCreatedSamePackage()
+            throws Exception {
+        int firstGameProcessId = 1000;
+        int secondGameProcessId = 1000;
+
+        mGameServiceProviderInstance.start();
+
+        // Multiple tasks exist for the same package.
+        startTask(10, GAME_A_MAIN_ACTIVITY);
+        startTask(11, GAME_A_MAIN_ACTIVITY);
+        startProcessForPackage(firstGameProcessId, GAME_A_PACKAGE);
+
+        mockPermissionGranted(Manifest.permission.MANAGE_GAME_ACTIVITY);
+
+        mFakeGameService.requestCreateGameSession(10);
+        mFakeGameService.requestCreateGameSession(11);
+
+        FakeGameSession gameSession10 = new FakeGameSession();
+        SurfacePackage mockSurfacePackage10 = Mockito.mock(SurfacePackage.class);
+        mFakeGameSessionService.removePendingFutureForTaskId(10)
+                .complete(new CreateGameSessionResult(gameSession10, mockSurfacePackage10));
+        FakeGameSession gameSession11 = new FakeGameSession();
+        SurfacePackage mockSurfacePackage11 = Mockito.mock(SurfacePackage.class);
+        mFakeGameSessionService.removePendingFutureForTaskId(11)
+                .complete(new CreateGameSessionResult(gameSession11, mockSurfacePackage11));
+
+        assertThat(gameSession10.mIsDestroyed).isFalse();
+        assertThat(gameSession11.mIsDestroyed).isFalse();
+
+        // After the first game process dies, both game sessions for the package should be
+        // destroyed.
+        dispatchProcessDied(firstGameProcessId);
+        assertThat(gameSession10.mIsDestroyed).isTrue();
+        assertThat(gameSession11.mIsDestroyed).isTrue();
+
+        // However, when a new process for the game starts, new game sessions for the same
+        // package should be created.
+        startProcessForPackage(secondGameProcessId, GAME_A_PACKAGE);
+        // Verify that new pending game sessions were created for each of the game's taskIds.
+        assertNotNull(mFakeGameSessionService.removePendingFutureForTaskId(10));
+        assertNotNull(mFakeGameSessionService.removePendingFutureForTaskId(11));
+    }
+
+    @Test
+    public void gameProcessStarted_gameSessionNotRequested_doesNothing() throws Exception {
+        int gameProcessId = 1000;
+
+        mGameServiceProviderInstance.start();
+
+        // A game task and process are started, but requestCreateGameSession is never called.
+        startTask(10, GAME_A_MAIN_ACTIVITY);
+        startProcessForPackage(gameProcessId, GAME_A_PACKAGE);
+
+        mockPermissionGranted(Manifest.permission.MANAGE_GAME_ACTIVITY);
+
+        // No game session should be created.
+        assertThat(mFakeGameSessionService.getCapturedCreateInvocations()).isEmpty();
+    }
+
+    @Test
+    public void processActivityAndDeath_notForGame_gameSessionUnaffected() throws Exception {
+        mGameServiceProviderInstance.start();
+
+        startTask(10, GAME_A_MAIN_ACTIVITY);
+
+        mockPermissionGranted(Manifest.permission.MANAGE_GAME_ACTIVITY);
+        mFakeGameService.requestCreateGameSession(10);
+
+        FakeGameSession gameSession10 = new FakeGameSession();
+        SurfacePackage mockSurfacePackage10 = Mockito.mock(SurfacePackage.class);
+        mFakeGameSessionService.removePendingFutureForTaskId(10)
+                .complete(new CreateGameSessionResult(gameSession10, mockSurfacePackage10));
+
+        // Process activity for a process without a known package is ignored.
+        startProcessForPackage(1000, /*packageName=*/ null);
+        dispatchProcessActivity(1000);
+        dispatchProcessDied(1000);
+
+        // Process activity for a process with a different package is ignored
+        startProcessForPackage(1001, GAME_B_PACKAGE);
+        dispatchProcessActivity(1001);
+        dispatchProcessDied(1001);
+
+        // Death of a process for which there was no activity is ignored
+        dispatchProcessDied(1002);
+
+        // Despite all the process activity and death, the game session is not destroyed.
+        assertThat(gameSession10.mIsDestroyed).isFalse();
+    }
+
+    @Test
     public void taskSystemBarsListenerChanged_noAssociatedGameSession_doesNothing() {
         mGameServiceProviderInstance.start();
 
@@ -900,7 +1125,8 @@
                 mFakeGameSessionService.getCapturedCreateInvocations())
                 .mGameSessionController.restartGame(11);
 
-        verifyZeroInteractions(mMockActivityManager);
+        verify(mMockActivityManager).registerProcessObserver(any());
+        verifyNoMoreInteractions(mMockActivityManager);
         assertThat(mMockContext.getLastStartedIntent()).isNull();
     }
 
@@ -932,7 +1158,6 @@
         dispatchTaskRemoved(taskId);
     }
 
-
     private void dispatchTaskRemoved(int taskId) {
         dispatchTaskChangeEvent(taskStackListener -> {
             taskStackListener.onTaskRemoved(taskId);
@@ -958,6 +1183,37 @@
         }
     }
 
+    private void startProcessForPackage(int processId, @Nullable String packageName) {
+        if (packageName != null) {
+            when(mMockActivityManagerInternal.getPackageNameByPid(processId)).thenReturn(
+                    packageName);
+        }
+
+        dispatchProcessActivity(processId);
+    }
+
+    private void dispatchProcessActivity(int processId) {
+        dispatchProcessChangedEvent(processObserver -> {
+            // Neither uid nor foregroundActivities are used by the implementation being tested.
+            processObserver.onForegroundActivitiesChanged(processId, /*uid=*/
+                    0, /*foregroundActivities=*/ false);
+        });
+    }
+
+    private void dispatchProcessDied(int processId) {
+        dispatchProcessChangedEvent(processObserver -> {
+            // The uid param is not used by the implementation being tested.
+            processObserver.onProcessDied(processId, /*uid=*/ 0);
+        });
+    }
+
+    private void dispatchProcessChangedEvent(
+            ThrowingConsumer<IProcessObserver> processObserverConsumer) {
+        for (IProcessObserver processObserver : mProcessObservers) {
+            processObserverConsumer.accept(processObserver);
+        }
+    }
+
     private void mockPermissionGranted(String permission) {
         mMockContext.setPermission(permission, PackageManager.PERMISSION_GRANTED);
     }
diff --git a/services/tests/mockingservicestests/src/com/android/server/pm/BackgroundDexOptServiceUnitTest.java b/services/tests/mockingservicestests/src/com/android/server/pm/BackgroundDexOptServiceUnitTest.java
index 8223b8c..6f503c7 100644
--- a/services/tests/mockingservicestests/src/com/android/server/pm/BackgroundDexOptServiceUnitTest.java
+++ b/services/tests/mockingservicestests/src/com/android/server/pm/BackgroundDexOptServiceUnitTest.java
@@ -116,7 +116,7 @@
         when(mInjector.getDataDirStorageLowBytes()).thenReturn(STORAGE_LOW_BYTES);
         when(mInjector.getDexOptThermalCutoff()).thenReturn(PowerManager.THERMAL_STATUS_CRITICAL);
         when(mInjector.getCurrentThermalStatus()).thenReturn(PowerManager.THERMAL_STATUS_NONE);
-        when(mDexOptHelper.getOptimizablePackages()).thenReturn(DEFAULT_PACKAGE_LIST);
+        when(mDexOptHelper.getOptimizablePackages(any())).thenReturn(DEFAULT_PACKAGE_LIST);
         when(mDexOptHelper.performDexOptWithStatus(any())).thenReturn(
                 PackageDexOptimizer.DEX_OPT_PERFORMED);
 
@@ -158,7 +158,7 @@
     @Test
     public void testNoExecutionForNoOptimizablePackages() {
         initUntilBootCompleted();
-        when(mDexOptHelper.getOptimizablePackages()).thenReturn(EMPTY_PACKAGE_LIST);
+        when(mDexOptHelper.getOptimizablePackages(any())).thenReturn(EMPTY_PACKAGE_LIST);
 
         assertThat(mService.onStartJob(mJobServiceForPostBoot,
                 mJobParametersForPostBoot)).isFalse();
diff --git a/services/tests/mockingservicestests/src/com/android/server/pm/OWNERS b/services/tests/mockingservicestests/src/com/android/server/pm/OWNERS
index 46b797b..5181af1 100644
--- a/services/tests/mockingservicestests/src/com/android/server/pm/OWNERS
+++ b/services/tests/mockingservicestests/src/com/android/server/pm/OWNERS
@@ -1,3 +1,4 @@
 include /services/core/java/com/android/server/pm/OWNERS
 
+per-file BackgroundDexOptServiceUnitTest.java = file:/services/core/java/com/android/server/pm/dex/OWNERS
 per-file StagingManagerTest.java = dariofreni@google.com, ioffe@google.com, olilan@google.com
diff --git a/services/tests/mockingservicestests/src/com/android/server/pm/PackageManagerServiceHibernationTests.kt b/services/tests/mockingservicestests/src/com/android/server/pm/PackageManagerServiceHibernationTests.kt
index 1319903..537a028 100644
--- a/services/tests/mockingservicestests/src/com/android/server/pm/PackageManagerServiceHibernationTests.kt
+++ b/services/tests/mockingservicestests/src/com/android/server/pm/PackageManagerServiceHibernationTests.kt
@@ -157,7 +157,7 @@
         rule.system().validateFinalState()
         whenever(appHibernationManager.isHibernatingGlobally(TEST_PACKAGE_2_NAME)).thenReturn(true)
 
-        val optimizablePkgs = DexOptHelper(pm).optimizablePackages
+        val optimizablePkgs = DexOptHelper(pm).getOptimizablePackages(pm.snapshotComputer())
 
         assertTrue(optimizablePkgs.contains(TEST_PACKAGE_NAME))
         assertFalse(optimizablePkgs.contains(TEST_PACKAGE_2_NAME))
diff --git a/services/tests/mockingservicestests/src/com/android/server/pm/SharedLibrariesImplTest.kt b/services/tests/mockingservicestests/src/com/android/server/pm/SharedLibrariesImplTest.kt
index b063d22..97b52a9 100644
--- a/services/tests/mockingservicestests/src/com/android/server/pm/SharedLibrariesImplTest.kt
+++ b/services/tests/mockingservicestests/src/com/android/server/pm/SharedLibrariesImplTest.kt
@@ -24,7 +24,6 @@
 import android.os.storage.StorageManager
 import android.util.ArrayMap
 import android.util.PackageUtils
-import com.android.internal.util.FunctionalUtils
 import com.android.server.SystemConfig.SharedLibraryEntry
 import com.android.server.compat.PlatformCompat
 import com.android.server.extendedtestutils.wheneverStatic
@@ -35,7 +34,6 @@
 import com.android.server.testutils.any
 import com.android.server.testutils.eq
 import com.android.server.testutils.mock
-import com.android.server.testutils.mockThrowOnUnmocked
 import com.android.server.testutils.nullable
 import com.android.server.testutils.spy
 import com.android.server.testutils.whenever
@@ -59,6 +57,7 @@
 import kotlin.test.assertFalse
 import kotlin.test.assertTrue
 
+@Ignore("b/216603387")
 @RunWith(JUnit4::class)
 class SharedLibrariesImplTest {
 
@@ -85,6 +84,7 @@
     private lateinit var mSharedLibrariesImpl: SharedLibrariesImpl
     private lateinit var mPms: PackageManagerService
     private lateinit var mSettings: Settings
+    private lateinit var mComputer: Computer
 
     @Mock
     private lateinit var mDeletePackageHelper: DeletePackageHelper
@@ -114,22 +114,16 @@
         mSharedLibrariesImpl.setDeletePackageHelper(mDeletePackageHelper)
         addExistingSharedLibraries()
 
+        mComputer = mock {
+            whenever(sharedLibraries) { mSharedLibrariesImpl.sharedLibraries }
+            whenever(resolveInternalPackageName(anyString(), anyLong())) { arguments[0] }
+        }
+
         whenever(mSettings.getPackageLPr(any())) { mExistingSettings[arguments[0]] }
         whenever(mRule.mocks().injector.getSystemService(StorageManager::class.java))
             .thenReturn(mStorageManager)
         whenever(mStorageManager.findPathForUuid(nullable())).thenReturn(mFile)
-        doAnswer { it.arguments[0] }.`when`(mPms).resolveInternalPackageName(any(), any())
-        doAnswer {
-            mockThrowOnUnmocked<Computer> {
-                whenever(sharedLibraries) { mSharedLibrariesImpl.sharedLibraries }
-                whenever(resolveInternalPackageName(anyString(), anyLong())) {
-                    mPms.resolveInternalPackageName(getArgument(0), getArgument(1))
-                }
-                whenever(getPackageStateInternal(anyString())) {
-                    mPms.getPackageStateInternal(getArgument(0))
-                }
-            }
-        }.`when`(mPms).snapshotComputer()
+        doAnswer { mComputer }.`when`(mPms).snapshotComputer()
         whenever(mDeletePackageHelper.deletePackageX(any(), any(), any(), any(), any()))
             .thenReturn(PackageManager.DELETE_SUCCEEDED)
         whenever(mRule.mocks().injector.compatibility).thenReturn(mPlatformCompat)
@@ -189,7 +183,8 @@
 
     @Test
     fun removeSharedLibrary() {
-        doAnswer { mutableListOf(VersionedPackage(CONSUMER_PACKAGE_NAME, 1L)) }.`when`(mPms)
+        doAnswer { mutableListOf(VersionedPackage(CONSUMER_PACKAGE_NAME, 1L)) }
+            .`when`(mComputer)
             .getPackagesUsingSharedLibrary(any(), any(), any(), any())
         val staticInfo = mSharedLibrariesImpl
             .getSharedLibraryInfo(STATIC_LIB_NAME, STATIC_LIB_VERSION)!!
@@ -253,7 +248,6 @@
         assertThat(testPackageSetting.usesLibraryFiles).contains(builtinLibPath(BUILTIN_LIB_NAME))
     }
 
-    @Ignore("b/216603387")
     @Test
     fun updateSharedLibraries_withStaticLibPackage() {
         val testPackageSetting = mExistingSettings[STATIC_LIB_PACKAGE_NAME]!!
@@ -266,7 +260,6 @@
         assertThat(testPackageSetting.usesLibraryFiles).contains(apkPath(DYNAMIC_LIB_PACKAGE_NAME))
     }
 
-    @Ignore("b/216603387")
     @Test
     fun updateSharedLibraries_withConsumerPackage() {
         val testPackageSetting = mExistingSettings[CONSUMER_PACKAGE_NAME]!!
@@ -280,7 +273,6 @@
         assertThat(testPackageSetting.usesLibraryFiles).contains(apkPath(STATIC_LIB_PACKAGE_NAME))
     }
 
-    @Ignore("b/216603387")
     @Test
     fun updateAllSharedLibraries() {
         mExistingSettings.forEach {
diff --git a/services/tests/mockingservicestests/src/com/android/server/pm/StagingManagerTest.java b/services/tests/mockingservicestests/src/com/android/server/pm/StagingManagerTest.java
index f7b1dd5..1464405 100644
--- a/services/tests/mockingservicestests/src/com/android/server/pm/StagingManagerTest.java
+++ b/services/tests/mockingservicestests/src/com/android/server/pm/StagingManagerTest.java
@@ -39,8 +39,7 @@
 import android.content.pm.ApexStagedEvent;
 import android.content.pm.IStagedApexObserver;
 import android.content.pm.PackageInstaller;
-import android.content.pm.PackageInstaller.SessionInfo;
-import android.content.pm.PackageInstaller.SessionInfo.SessionErrorCode;
+import android.content.pm.PackageManager;
 import android.content.pm.StagedApexInfo;
 import android.os.SystemProperties;
 import android.os.storage.IStorageManager;
@@ -158,10 +157,10 @@
 
         mStagingManager.restoreSessions(Arrays.asList(session1, session2), true);
 
-        assertThat(session1.getErrorCode()).isEqualTo(SessionInfo.SESSION_ACTIVATION_FAILED);
+        assertThat(session1.getErrorCode()).isEqualTo(PackageManager.INSTALL_ACTIVATION_FAILED);
         assertThat(session1.getErrorMessage()).isEqualTo("Build fingerprint has changed");
 
-        assertThat(session2.getErrorCode()).isEqualTo(SessionInfo.SESSION_ACTIVATION_FAILED);
+        assertThat(session2.getErrorCode()).isEqualTo(PackageManager.INSTALL_ACTIVATION_FAILED);
         assertThat(session2.getErrorMessage()).isEqualTo("Build fingerprint has changed");
     }
 
@@ -247,12 +246,12 @@
         verify(mStorageManager, never()).abortChanges(eq("abort-staged-install"), eq(false));
 
         assertThat(apexSession.getErrorCode())
-                .isEqualTo(SessionInfo.SESSION_ACTIVATION_FAILED);
+                .isEqualTo(PackageManager.INSTALL_ACTIVATION_FAILED);
         assertThat(apexSession.getErrorMessage()).isEqualTo("apexd did not know anything about a "
                 + "staged session supposed to be activated");
 
         assertThat(apkSession.getErrorCode())
-                .isEqualTo(SessionInfo.SESSION_ACTIVATION_FAILED);
+                .isEqualTo(PackageManager.INSTALL_ACTIVATION_FAILED);
         assertThat(apkSession.getErrorMessage()).isEqualTo("Another apex session failed");
     }
 
@@ -303,22 +302,22 @@
         verify(mStorageManager, never()).abortChanges(eq("abort-staged-install"), eq(false));
 
         assertThat(apexSession1.getErrorCode())
-                .isEqualTo(SessionInfo.SESSION_ACTIVATION_FAILED);
+                .isEqualTo(PackageManager.INSTALL_ACTIVATION_FAILED);
         assertThat(apexSession1.getErrorMessage()).isEqualTo("APEX activation failed. "
                 + "Error: Failed for test");
 
         assertThat(apexSession2.getErrorCode())
-                .isEqualTo(SessionInfo.SESSION_ACTIVATION_FAILED);
+                .isEqualTo(PackageManager.INSTALL_ACTIVATION_FAILED);
         assertThat(apexSession2.getErrorMessage()).isEqualTo("Staged session 101 at boot didn't "
                 + "activate nor fail. Marking it as failed anyway.");
 
         assertThat(apexSession3.getErrorCode())
-                .isEqualTo(SessionInfo.SESSION_ACTIVATION_FAILED);
+                .isEqualTo(PackageManager.INSTALL_ACTIVATION_FAILED);
         assertThat(apexSession3.getErrorMessage()).isEqualTo("apexd did not know anything about a "
                 + "staged session supposed to be activated");
 
         assertThat(apkSession.getErrorCode())
-                .isEqualTo(SessionInfo.SESSION_ACTIVATION_FAILED);
+                .isEqualTo(PackageManager.INSTALL_ACTIVATION_FAILED);
         assertThat(apkSession.getErrorMessage()).isEqualTo("Another apex session failed");
     }
 
@@ -351,12 +350,12 @@
         verify(mStorageManager, never()).abortChanges(eq("abort-staged-install"), eq(false));
 
         assertThat(apexSession.getErrorCode())
-                .isEqualTo(SessionInfo.SESSION_ACTIVATION_FAILED);
+                .isEqualTo(PackageManager.INSTALL_ACTIVATION_FAILED);
         assertThat(apexSession.getErrorMessage()).isEqualTo("Staged session 1543 at boot didn't "
                 + "activate nor fail. Marking it as failed anyway.");
 
         assertThat(apkSession.getErrorCode())
-                .isEqualTo(SessionInfo.SESSION_ACTIVATION_FAILED);
+                .isEqualTo(PackageManager.INSTALL_ACTIVATION_FAILED);
         assertThat(apkSession.getErrorMessage()).isEqualTo("Another apex session failed");
     }
 
@@ -445,11 +444,11 @@
         verify(mStorageManager, never()).abortChanges(eq("abort-staged-install"), eq(false));
 
         assertThat(apexSession.getErrorCode())
-                .isEqualTo(SessionInfo.SESSION_ACTIVATION_FAILED);
+                .isEqualTo(PackageManager.INSTALL_ACTIVATION_FAILED);
         assertThat(apexSession.getErrorMessage()).isEqualTo("Impossible state");
 
         assertThat(apkSession.getErrorCode())
-                .isEqualTo(SessionInfo.SESSION_ACTIVATION_FAILED);
+                .isEqualTo(PackageManager.INSTALL_ACTIVATION_FAILED);
         assertThat(apkSession.getErrorMessage()).isEqualTo("Another apex session failed");
     }
 
@@ -755,7 +754,7 @@
                 /* isReady */ false,
                 /* isFailed */ false,
                 /* isApplied */false,
-                /* stagedSessionErrorCode */ PackageInstaller.SessionInfo.SESSION_NO_ERROR,
+                /* stagedSessionErrorCode */ PackageManager.INSTALL_UNKNOWN,
                 /* stagedSessionErrorMessage */ "no error");
 
         StagingManager.StagedSession stagedSession = spy(session.mStagedSession);
@@ -775,7 +774,7 @@
         private boolean mIsReady = false;
         private boolean mIsApplied = false;
         private boolean mIsFailed = false;
-        private @SessionErrorCode int mErrorCode = -1;
+        private int mErrorCode = -1;
         private String mErrorMessage;
         private boolean mIsDestroyed = false;
         private int mParentSessionId = -1;
@@ -828,7 +827,7 @@
             return this;
         }
 
-        private @SessionErrorCode int getErrorCode() {
+        private int getErrorCode() {
             return mErrorCode;
         }
 
@@ -940,7 +939,7 @@
         }
 
         @Override
-        public void setSessionFailed(@SessionErrorCode int errorCode, String errorMessage) {
+        public void setSessionFailed(int errorCode, String errorMessage) {
             Preconditions.checkState(!mIsApplied, "Already marked as applied");
             mIsFailed = true;
             mErrorCode = errorCode;
diff --git a/services/tests/mockingservicestests/src/com/android/server/pm/SuspendPackageHelperTest.kt b/services/tests/mockingservicestests/src/com/android/server/pm/SuspendPackageHelperTest.kt
index 5230ea7..4818573 100644
--- a/services/tests/mockingservicestests/src/com/android/server/pm/SuspendPackageHelperTest.kt
+++ b/services/tests/mockingservicestests/src/com/android/server/pm/SuspendPackageHelperTest.kt
@@ -104,9 +104,9 @@
             pms, rule.mocks().injector, broadcastHelper, protectedPackages)
         defaultAppProvider = rule.mocks().defaultAppProvider
         testHandler = rule.mocks().handler
-        packageSetting1 = pms.getPackageStateInternal(TEST_PACKAGE_1)!!
-        packageSetting2 = pms.getPackageStateInternal(TEST_PACKAGE_2)!!
-        ownerSetting = pms.getPackageStateInternal(DEVICE_OWNER_PACKAGE)!!
+        packageSetting1 = pms.snapshotComputer().getPackageStateInternal(TEST_PACKAGE_1)!!
+        packageSetting2 = pms.snapshotComputer().getPackageStateInternal(TEST_PACKAGE_2)!!
+        ownerSetting = pms.snapshotComputer().getPackageStateInternal(DEVICE_OWNER_PACKAGE)!!
         deviceOwnerUid = UserHandle.getUid(TEST_USER_ID, ownerSetting.appId)
         packagesToSuspend = arrayOf(TEST_PACKAGE_1, TEST_PACKAGE_2)
         uidsToSuspend = intArrayOf(packageSetting1.appId, packageSetting2.appId)
@@ -270,7 +270,7 @@
         testHandler.flush()
         assertThat(failedNames).isEmpty()
 
-        val result = suspendPackageHelper.getSuspendedPackageAppExtras(
+        val result = suspendPackageHelper.getSuspendedPackageAppExtras(pms.snapshotComputer(),
             TEST_PACKAGE_1, TEST_USER_ID, deviceOwnerUid)!!
 
         assertThat(result.getString(TEST_PACKAGE_1)).isEqualTo(TEST_PACKAGE_1)
@@ -286,13 +286,13 @@
             null /* dialogInfo */, DEVICE_OWNER_PACKAGE, TEST_USER_ID, deviceOwnerUid)
         testHandler.flush()
         assertThat(failedNames).isEmpty()
-        assertThat(suspendPackageHelper.getSuspendingPackage(
+        assertThat(suspendPackageHelper.getSuspendingPackage(pms.snapshotComputer(),
             TEST_PACKAGE_1, TEST_USER_ID, deviceOwnerUid)).isEqualTo(DEVICE_OWNER_PACKAGE)
-        assertThat(suspendPackageHelper.getSuspendingPackage(
+        assertThat(suspendPackageHelper.getSuspendingPackage(pms.snapshotComputer(),
             TEST_PACKAGE_2, TEST_USER_ID, deviceOwnerUid)).isEqualTo(DEVICE_OWNER_PACKAGE)
-        assertThat(suspendPackageHelper.getSuspendedPackageAppExtras(
+        assertThat(suspendPackageHelper.getSuspendedPackageAppExtras(pms.snapshotComputer(),
             TEST_PACKAGE_1, TEST_USER_ID, deviceOwnerUid)).isNotNull()
-        assertThat(suspendPackageHelper.getSuspendedPackageAppExtras(
+        assertThat(suspendPackageHelper.getSuspendedPackageAppExtras(pms.snapshotComputer(),
             TEST_PACKAGE_2, TEST_USER_ID, deviceOwnerUid)).isNotNull()
 
         suspendPackageHelper.removeSuspensionsBySuspendingPackage(pms.snapshotComputer(),
@@ -311,13 +311,13 @@
             nullable(), nullable(), any(), eq(TEST_PACKAGE_2), nullable(), any(), any(),
             nullable(), nullable())
 
-        assertThat(suspendPackageHelper.getSuspendingPackage(
+        assertThat(suspendPackageHelper.getSuspendingPackage(pms.snapshotComputer(),
             TEST_PACKAGE_1, TEST_USER_ID, deviceOwnerUid)).isNull()
-        assertThat(suspendPackageHelper.getSuspendingPackage(
+        assertThat(suspendPackageHelper.getSuspendingPackage(pms.snapshotComputer(),
             TEST_PACKAGE_2, TEST_USER_ID, deviceOwnerUid)).isNull()
-        assertThat(suspendPackageHelper.getSuspendedPackageAppExtras(
+        assertThat(suspendPackageHelper.getSuspendedPackageAppExtras(pms.snapshotComputer(),
             TEST_PACKAGE_1, TEST_USER_ID, deviceOwnerUid)).isNull()
-        assertThat(suspendPackageHelper.getSuspendedPackageAppExtras(
+        assertThat(suspendPackageHelper.getSuspendedPackageAppExtras(pms.snapshotComputer(),
             TEST_PACKAGE_2, TEST_USER_ID, deviceOwnerUid)).isNull()
     }
 
@@ -331,7 +331,7 @@
         testHandler.flush()
         assertThat(failedNames).isEmpty()
 
-        val result = suspendPackageHelper.getSuspendedPackageLauncherExtras(
+        val result = suspendPackageHelper.getSuspendedPackageLauncherExtras(pms.snapshotComputer(),
             TEST_PACKAGE_2, TEST_USER_ID, deviceOwnerUid)!!
 
         assertThat(result.getString(TEST_PACKAGE_2)).isEqualTo(TEST_PACKAGE_2)
@@ -346,7 +346,7 @@
         testHandler.flush()
         assertThat(failedNames).isEmpty()
 
-        assertThat(suspendPackageHelper.isPackageSuspended(
+        assertThat(suspendPackageHelper.isPackageSuspended(pms.snapshotComputer(),
             TEST_PACKAGE_1, TEST_USER_ID, deviceOwnerUid)).isTrue()
     }
 
@@ -360,7 +360,7 @@
         testHandler.flush()
         assertThat(failedNames).isEmpty()
 
-        assertThat(suspendPackageHelper.getSuspendingPackage(
+        assertThat(suspendPackageHelper.getSuspendingPackage(pms.snapshotComputer(),
             TEST_PACKAGE_2, TEST_USER_ID, deviceOwnerUid)).isEqualTo(DEVICE_OWNER_PACKAGE)
     }
 
@@ -375,7 +375,7 @@
         testHandler.flush()
         assertThat(failedNames).isEmpty()
 
-        val result = suspendPackageHelper.getSuspendedDialogInfo(
+        val result = suspendPackageHelper.getSuspendedDialogInfo(pms.snapshotComputer(),
             TEST_PACKAGE_1, DEVICE_OWNER_PACKAGE, TEST_USER_ID, deviceOwnerUid)!!
 
         assertThat(result.title).isEqualTo(TEST_PACKAGE_1)
@@ -387,8 +387,8 @@
         mockAllowList(packageSetting1, allowList(10001, 10002, 10003))
         mockAllowList(packageSetting2, allowList(10001, 10002, 10003))
 
-        suspendPackageHelper.sendPackagesSuspendedForUser(Intent.ACTION_PACKAGES_SUSPENDED,
-                packagesToSuspend, uidsToSuspend, TEST_USER_ID)
+        suspendPackageHelper.sendPackagesSuspendedForUser(pms.snapshotComputer(),
+            Intent.ACTION_PACKAGES_SUSPENDED, packagesToSuspend, uidsToSuspend, TEST_USER_ID)
         testHandler.flush()
         verify(broadcastHelper).sendPackageBroadcast(any(), nullable(), bundleCaptor.capture(),
                 anyInt(), nullable(), nullable(), any(), nullable(), any(), nullable())
@@ -406,8 +406,8 @@
         mockAllowList(packageSetting1, allowList(10001, 10002, 10003))
         mockAllowList(packageSetting2, allowList(10001, 10002, 10007))
 
-        suspendPackageHelper.sendPackagesSuspendedForUser(Intent.ACTION_PACKAGES_SUSPENDED,
-                packagesToSuspend, uidsToSuspend, TEST_USER_ID)
+        suspendPackageHelper.sendPackagesSuspendedForUser(pms.snapshotComputer(),
+            Intent.ACTION_PACKAGES_SUSPENDED, packagesToSuspend, uidsToSuspend, TEST_USER_ID)
         testHandler.flush()
         verify(broadcastHelper, times(2)).sendPackageBroadcast(
                 any(), nullable(), bundleCaptor.capture(), anyInt(), nullable(), nullable(), any(),
@@ -429,8 +429,8 @@
         mockAllowList(packageSetting1, allowList(10001, 10002, 10003))
         mockAllowList(packageSetting2, null)
 
-        suspendPackageHelper.sendPackagesSuspendedForUser(Intent.ACTION_PACKAGES_SUSPENDED,
-                packagesToSuspend, uidsToSuspend, TEST_USER_ID)
+        suspendPackageHelper.sendPackagesSuspendedForUser(pms.snapshotComputer(),
+            Intent.ACTION_PACKAGES_SUSPENDED, packagesToSuspend, uidsToSuspend, TEST_USER_ID)
         testHandler.flush()
         verify(broadcastHelper, times(2)).sendPackageBroadcast(
                 any(), nullable(), bundleCaptor.capture(), anyInt(), nullable(), nullable(), any(),
@@ -449,8 +449,9 @@
     @Test
     @Throws(Exception::class)
     fun sendPackagesSuspendModifiedForUser() {
-        suspendPackageHelper.sendPackagesSuspendedForUser(Intent.ACTION_PACKAGES_SUSPENSION_CHANGED,
-                packagesToSuspend, uidsToSuspend, TEST_USER_ID)
+        suspendPackageHelper.sendPackagesSuspendedForUser(pms.snapshotComputer(),
+            Intent.ACTION_PACKAGES_SUSPENSION_CHANGED, packagesToSuspend, uidsToSuspend,
+            TEST_USER_ID)
         testHandler.flush()
         verify(broadcastHelper).sendPackageBroadcast(
                 eq(Intent.ACTION_PACKAGES_SUSPENSION_CHANGED), nullable(), bundleCaptor.capture(),
@@ -483,13 +484,13 @@
         Mockito.doReturn(DIALER_PACKAGE).`when`(defaultAppProvider)
             .getDefaultDialer(eq(TEST_USER_ID))
         Mockito.doReturn(arrayOf(INSTALLER_PACKAGE)).`when`(pms).getKnownPackageNamesInternal(
-            eq(PackageManagerInternal.PACKAGE_INSTALLER), eq(TEST_USER_ID))
+            any(), eq(PackageManagerInternal.PACKAGE_INSTALLER), eq(TEST_USER_ID))
         Mockito.doReturn(arrayOf(UNINSTALLER_PACKAGE)).`when`(pms).getKnownPackageNamesInternal(
-            eq(PackageManagerInternal.PACKAGE_UNINSTALLER), eq(TEST_USER_ID))
+            any(), eq(PackageManagerInternal.PACKAGE_UNINSTALLER), eq(TEST_USER_ID))
         Mockito.doReturn(arrayOf(VERIFIER_PACKAGE)).`when`(pms).getKnownPackageNamesInternal(
-            eq(PackageManagerInternal.PACKAGE_VERIFIER), eq(TEST_USER_ID))
+            any(), eq(PackageManagerInternal.PACKAGE_VERIFIER), eq(TEST_USER_ID))
         Mockito.doReturn(arrayOf(PERMISSION_CONTROLLER_PACKAGE)).`when`(pms)
-            .getKnownPackageNamesInternal(
+            .getKnownPackageNamesInternal(any(),
                 eq(PackageManagerInternal.PACKAGE_PERMISSION_CONTROLLER), eq(TEST_USER_ID))
     }
 
diff --git a/services/tests/mockingservicestests/src/com/android/server/pm/dex/DexManagerTests.java b/services/tests/mockingservicestests/src/com/android/server/pm/dex/DexManagerTests.java
index 8abe46f..9d26971 100644
--- a/services/tests/mockingservicestests/src/com/android/server/pm/dex/DexManagerTests.java
+++ b/services/tests/mockingservicestests/src/com/android/server/pm/dex/DexManagerTests.java
@@ -159,8 +159,8 @@
             .when(mockContext)
                 .getSystemService(PowerManager.class);
 
-        mDexManager = new DexManager(mockContext, mPM, /*PackageDexOptimizer*/ null,
-                mInstaller, mInstallLock);
+        mDexManager = new DexManager(mockContext, /*PackageDexOptimizer*/ null,
+                mInstaller, mInstallLock, mPM);
 
         // Foo and Bar are available to user0.
         // Only Bar is available to user1;
diff --git a/services/tests/mockingservicestests/src/com/android/server/tare/AgentTest.java b/services/tests/mockingservicestests/src/com/android/server/tare/AgentTest.java
index 41d46f2..534d0a1 100644
--- a/services/tests/mockingservicestests/src/com/android/server/tare/AgentTest.java
+++ b/services/tests/mockingservicestests/src/com/android/server/tare/AgentTest.java
@@ -21,7 +21,6 @@
 
 import static org.junit.Assert.assertEquals;
 import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
 
 import android.app.AlarmManager;
 import android.content.Context;
@@ -71,10 +70,10 @@
                 .strictness(Strictness.LENIENT)
                 .mockStatic(LocalServices.class)
                 .startMocking();
-        when(mIrs.getContext()).thenReturn(mContext);
-        when(mIrs.getCompleteEconomicPolicyLocked()).thenReturn(mEconomicPolicy);
-        when(mIrs.getLock()).thenReturn(mIrs);
-        when(mContext.getSystemService(Context.ALARM_SERVICE)).thenReturn(mock(AlarmManager.class));
+        doReturn(mContext).when(mIrs).getContext();
+        doReturn(mEconomicPolicy).when(mIrs).getCompleteEconomicPolicyLocked();
+        doReturn(mIrs).when(mIrs).getLock();
+        doReturn(mock(AlarmManager.class)).when(mContext).getSystemService(Context.ALARM_SERVICE);
         mScribe = new MockScribe(mIrs);
     }
 
diff --git a/services/tests/mockingservicestests/src/com/android/server/tare/ScribeTest.java b/services/tests/mockingservicestests/src/com/android/server/tare/ScribeTest.java
index ab29e59..c2cf2ff 100644
--- a/services/tests/mockingservicestests/src/com/android/server/tare/ScribeTest.java
+++ b/services/tests/mockingservicestests/src/com/android/server/tare/ScribeTest.java
@@ -109,6 +109,7 @@
         long lastReclamationTime = System.currentTimeMillis();
         long remainingConsumableNarcs = 2000L;
         long consumptionLimit = 500_000L;
+        when(mIrs.getConsumptionLimitLocked()).thenReturn(consumptionLimit);
 
         Ledger ledger = mScribeUnderTest.getLedgerLocked(TEST_USER_ID, TEST_PACKAGE);
         ledger.recordTransaction(new Ledger.Transaction(0, 1000L, 1, null, 2000, 0));
@@ -119,8 +120,13 @@
         mScribeUnderTest.setConsumptionLimitLocked(consumptionLimit);
         mScribeUnderTest.adjustRemainingConsumableNarcsLocked(
                 remainingConsumableNarcs - consumptionLimit);
-        mScribeUnderTest.writeImmediatelyForTesting();
 
+        assertEquals(lastReclamationTime, mScribeUnderTest.getLastReclamationTimeLocked());
+        assertEquals(remainingConsumableNarcs,
+                mScribeUnderTest.getRemainingConsumableNarcsLocked());
+        assertEquals(consumptionLimit, mScribeUnderTest.getSatiatedConsumptionLimitLocked());
+
+        mScribeUnderTest.writeImmediatelyForTesting();
         mScribeUnderTest.loadFromDiskLocked();
 
         assertEquals(lastReclamationTime, mScribeUnderTest.getLastReclamationTimeLocked());
diff --git a/services/tests/servicestests/Android.bp b/services/tests/servicestests/Android.bp
index 152f3b3..e3be3a7 100644
--- a/services/tests/servicestests/Android.bp
+++ b/services/tests/servicestests/Android.bp
@@ -64,6 +64,7 @@
         "testng",
         "junit",
         "platform-compat-test-rules",
+        "ActivityContext",
     ],
 
     aidl: {
diff --git a/services/tests/servicestests/src/com/android/server/accessibility/AccessibilityManagerServiceTest.java b/services/tests/servicestests/src/com/android/server/accessibility/AccessibilityManagerServiceTest.java
index 1f016fb..56c5150 100644
--- a/services/tests/servicestests/src/com/android/server/accessibility/AccessibilityManagerServiceTest.java
+++ b/services/tests/servicestests/src/com/android/server/accessibility/AccessibilityManagerServiceTest.java
@@ -28,6 +28,8 @@
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.atLeastOnce;
+import static org.mockito.Mockito.doCallRealMethod;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
@@ -61,10 +63,12 @@
 import androidx.test.filters.SmallTest;
 
 import com.android.compatibility.common.util.TestUtils;
+import com.android.internal.compat.IPlatformCompat;
 import com.android.server.LocalServices;
 import com.android.server.accessibility.AccessibilityManagerService.AccessibilityDisplayListener;
 import com.android.server.accessibility.magnification.FullScreenMagnificationController;
 import com.android.server.accessibility.magnification.MagnificationController;
+import com.android.server.accessibility.magnification.MagnificationProcessor;
 import com.android.server.accessibility.magnification.WindowMagnificationManager;
 import com.android.server.accessibility.test.MessageCapturingHandler;
 import com.android.server.pm.UserManagerInternal;
@@ -185,7 +189,7 @@
         mA11yms.mUserStates.put(mA11yms.getCurrentUserIdLocked(), userState);
     }
 
-    private void setupAccessibilityServiceConnection() {
+    private void setupAccessibilityServiceConnection(int serviceInfoFlag) {
         final AccessibilityUserState userState = mA11yms.mUserStates.get(
                 mA11yms.getCurrentUserIdLocked());
         when(mMockServiceInfo.getResolveInfo()).thenReturn(mMockResolveInfo);
@@ -193,7 +197,12 @@
         mMockResolveInfo.serviceInfo.applicationInfo = mock(ApplicationInfo.class);
 
         when(mMockBinder.queryLocalInterface(any())).thenReturn(mMockServiceClient);
+        when(mMockSystemSupport.getKeyEventDispatcher()).thenReturn(mock(KeyEventDispatcher.class));
+        when(mMockSystemSupport.getMagnificationProcessor()).thenReturn(
+                mock(MagnificationProcessor.class));
         mTestableContext.addMockService(COMPONENT_NAME, mMockBinder);
+
+        mMockServiceInfo.flags = serviceInfoFlag;
         mAccessibilityServiceConnection = new AccessibilityServiceConnection(
                 userState,
                 mTestableContext,
@@ -256,7 +265,7 @@
     @SmallTest
     @Test
     public void testOnSystemActionsChanged() throws Exception {
-        setupAccessibilityServiceConnection();
+        setupAccessibilityServiceConnection(0);
         final AccessibilityUserState userState = mA11yms.mUserStates.get(
                 mA11yms.getCurrentUserIdLocked());
 
@@ -376,7 +385,7 @@
     @SmallTest
     @Test
     public void testOnClientChange_boundServiceCanControlMagnification_requestConnection() {
-        setupAccessibilityServiceConnection();
+        setupAccessibilityServiceConnection(0);
         when(mMockSecurityPolicy.canControlMagnification(any())).thenReturn(true);
 
         // Invokes client change to trigger onUserStateChanged.
@@ -385,6 +394,29 @@
         verify(mMockWindowMagnificationMgr).requestConnection(true);
     }
 
+    @Test
+    public void testUnbindIme_whenServiceUnbinds() {
+        setupAccessibilityServiceConnection(AccessibilityServiceInfo.FLAG_INPUT_METHOD_EDITOR);
+        mAccessibilityServiceConnection.unbindLocked();
+        verify(mMockSystemSupport, atLeastOnce()).unbindImeLocked(mAccessibilityServiceConnection);
+    }
+
+    @Test
+    public void testUnbindIme_whenServiceCrashed() {
+        setupAccessibilityServiceConnection(AccessibilityServiceInfo.FLAG_INPUT_METHOD_EDITOR);
+        mAccessibilityServiceConnection.binderDied();
+        verify(mMockSystemSupport).unbindImeLocked(mAccessibilityServiceConnection);
+    }
+
+    @Test
+    public void testUnbindIme_whenServiceStopsRequestingIme() {
+        setupAccessibilityServiceConnection(AccessibilityServiceInfo.FLAG_INPUT_METHOD_EDITOR);
+        doCallRealMethod().when(mMockServiceInfo).updateDynamicallyConfigurableProperties(
+                any(IPlatformCompat.class), any(AccessibilityServiceInfo.class));
+        mAccessibilityServiceConnection.setServiceInfo(new AccessibilityServiceInfo());
+        verify(mMockSystemSupport).unbindImeLocked(mAccessibilityServiceConnection);
+    }
+
     public static class FakeInputFilter extends AccessibilityInputFilter {
         FakeInputFilter(Context context,
                 AccessibilityManagerService service) {
diff --git a/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java b/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java
index b601d14..1ebcbe1 100644
--- a/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java
+++ b/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java
@@ -4157,8 +4157,8 @@
     @Test
     public void testSetPreferentialNetworkServiceConfig_noProfileOwner() throws Exception {
         assertExpectException(SecurityException.class, null,
-                () -> dpm.setPreferentialNetworkServiceConfig(
-                        PreferentialNetworkServiceConfig.DEFAULT));
+                () -> dpm.setPreferentialNetworkServiceConfigs(
+                        List.of(PreferentialNetworkServiceConfig.DEFAULT)));
     }
 
     @Test
@@ -4198,10 +4198,10 @@
         addManagedProfile(admin1, managedProfileAdminUid, admin1);
         mContext.binder.callingUid = managedProfileAdminUid;
 
-        dpm.setPreferentialNetworkServiceConfig(PreferentialNetworkServiceConfig.DEFAULT);
+        dpm.setPreferentialNetworkServiceConfigs(
+                List.of(PreferentialNetworkServiceConfig.DEFAULT));
         assertThat(dpm.isPreferentialNetworkServiceEnabled()).isFalse();
-        assertThat(dpm.getPreferentialNetworkServiceConfig()
-                .isEnabled()).isFalse();
+        assertThat(dpm.getPreferentialNetworkServiceConfigs().get(0).isEnabled()).isFalse();
 
         ProfileNetworkPreference preferenceDetails =
                 new ProfileNetworkPreference.Builder()
@@ -4227,8 +4227,8 @@
         addManagedProfile(admin1, managedProfileAdminUid, admin1);
         mContext.binder.callingUid = managedProfileAdminUid;
 
-        dpm.setPreferentialNetworkServiceConfig(preferentialNetworkServiceConfigEnabled);
-        assertThat(dpm.getPreferentialNetworkServiceConfig()
+        dpm.setPreferentialNetworkServiceConfigs(List.of(preferentialNetworkServiceConfigEnabled));
+        assertThat(dpm.getPreferentialNetworkServiceConfigs().get(0)
                 .isEnabled()).isTrue();
         ProfileNetworkPreference preferenceDetails =
                 new ProfileNetworkPreference.Builder()
@@ -4257,8 +4257,8 @@
                         .setFallbackToDefaultConnectionAllowed(false)
                         .setIncludedUids(new int[]{1, 2})
                         .build();
-        dpm.setPreferentialNetworkServiceConfig(preferentialNetworkServiceConfigEnabled);
-        assertThat(dpm.getPreferentialNetworkServiceConfig()
+        dpm.setPreferentialNetworkServiceConfigs(List.of(preferentialNetworkServiceConfigEnabled));
+        assertThat(dpm.getPreferentialNetworkServiceConfigs().get(0)
                 .isEnabled()).isTrue();
         List<Integer> includedList = new ArrayList<>();
         includedList.add(1);
@@ -4292,8 +4292,8 @@
                         .setExcludedUids(new int[]{1, 2})
                         .build();
 
-        dpm.setPreferentialNetworkServiceConfig(preferentialNetworkServiceConfigEnabled);
-        assertThat(dpm.getPreferentialNetworkServiceConfig()
+        dpm.setPreferentialNetworkServiceConfigs(List.of(preferentialNetworkServiceConfigEnabled));
+        assertThat(dpm.getPreferentialNetworkServiceConfigs().get(0)
                 .isEnabled()).isTrue();
         List<Integer> excludedUids = new ArrayList<>();
         excludedUids.add(1);
diff --git a/services/tests/servicestests/src/com/android/server/job/JobStoreTest.java b/services/tests/servicestests/src/com/android/server/job/JobStoreTest.java
index 4de15c8..928c76d 100644
--- a/services/tests/servicestests/src/com/android/server/job/JobStoreTest.java
+++ b/services/tests/servicestests/src/com/android/server/job/JobStoreTest.java
@@ -330,11 +330,12 @@
 
     @Test
     public void testPriorityPersisted() throws Exception {
-        final JobInfo.Builder b = new Builder(92, mComponent)
+        final JobInfo job = new Builder(92, mComponent)
                 .setOverrideDeadline(5000)
                 .setPriority(JobInfo.PRIORITY_MIN)
-                .setPersisted(true);
-        final JobStatus js = JobStatus.createFromJobInfo(b.build(), SOME_UID, null, -1, null);
+                .setPersisted(true)
+                .build();
+        final JobStatus js = JobStatus.createFromJobInfo(job, SOME_UID, null, -1, null);
         mTaskStoreUnderTest.add(js);
         waitForPendingIo();
 
@@ -342,7 +343,7 @@
         mTaskStoreUnderTest.readJobMapFromDisk(jobStatusSet, true);
         final JobStatus loaded = jobStatusSet.getAllJobs().iterator().next();
         assertEquals("Priority not correctly persisted.",
-                JobInfo.PRIORITY_MIN, loaded.getEffectivePriority());
+                JobInfo.PRIORITY_MIN, job.getPriority());
     }
 
     /**
diff --git a/services/tests/servicestests/src/com/android/server/locksettings/LockSettingsServiceTestable.java b/services/tests/servicestests/src/com/android/server/locksettings/LockSettingsServiceTestable.java
index 21c09a0..1d10b8a 100644
--- a/services/tests/servicestests/src/com/android/server/locksettings/LockSettingsServiceTestable.java
+++ b/services/tests/servicestests/src/com/android/server/locksettings/LockSettingsServiceTestable.java
@@ -217,7 +217,7 @@
     }
 
     @Override
-    protected boolean isCredentialSharedWithParent(int userId) {
+    protected boolean isCredentialSharableWithParent(int userId) {
         UserInfo userInfo = mUserManager.getUserInfo(userId);
         return userInfo.isCloneProfile() || userInfo.isManagedProfile();
     }
diff --git a/services/tests/servicestests/src/com/android/server/pm/CrossProfileAppsServiceImplTest.java b/services/tests/servicestests/src/com/android/server/pm/CrossProfileAppsServiceImplTest.java
index 3cb5d5f..ce322f7 100644
--- a/services/tests/servicestests/src/com/android/server/pm/CrossProfileAppsServiceImplTest.java
+++ b/services/tests/servicestests/src/com/android/server/pm/CrossProfileAppsServiceImplTest.java
@@ -20,6 +20,7 @@
 import android.Manifest;
 import android.app.ActivityManager;
 import android.app.ActivityManagerInternal;
+import android.app.ActivityOptions;
 import android.app.AppOpsManager;
 import android.app.IApplicationThread;
 import android.app.admin.DevicePolicyManagerInternal;
@@ -46,6 +47,7 @@
 import android.platform.test.annotations.Presubmit;
 import android.util.SparseArray;
 
+import com.android.activitycontext.ActivityContext;
 import com.android.internal.util.FunctionalUtils.ThrowingRunnable;
 import com.android.internal.util.FunctionalUtils.ThrowingSupplier;
 import com.android.server.LocalServices;
@@ -240,7 +242,9 @@
                                 FEATURE_ID,
                                 ACTIVITY_COMPONENT,
                                 UserHandle.of(PRIMARY_USER).getIdentifier(),
-                                true));
+                                true,
+                                /* targetTask */ null,
+                                /* options */ null));
 
         verify(mActivityTaskManagerInternal, never())
                 .startActivityAsUser(
@@ -265,7 +269,9 @@
                                 FEATURE_ID,
                                 ACTIVITY_COMPONENT,
                                 UserHandle.of(PRIMARY_USER).getIdentifier(),
-                                false));
+                                false,
+                                /* targetTask */ null,
+                                /* options */ null));
 
         verify(mActivityTaskManagerInternal, never())
                 .startActivityAsUser(
@@ -292,7 +298,9 @@
                                 FEATURE_ID,
                                 ACTIVITY_COMPONENT,
                                 UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(),
-                                true));
+                                true,
+                                /* targetTask */ null,
+                                /* options */ null));
 
         verify(mActivityTaskManagerInternal, never())
                 .startActivityAsUser(
@@ -319,7 +327,9 @@
                                 FEATURE_ID,
                                 ACTIVITY_COMPONENT,
                                 UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(),
-                                false));
+                                false,
+                                /* targetTask */ null,
+                                /* options */ null));
 
         verify(mActivityTaskManagerInternal, never())
                 .startActivityAsUser(
@@ -344,7 +354,9 @@
                                 FEATURE_ID,
                                 ACTIVITY_COMPONENT,
                                 UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(),
-                                true));
+                                true,
+                                /* targetTask */ null,
+                                /* options */ null));
 
         verify(mActivityTaskManagerInternal, never())
                 .startActivityAsUser(
@@ -369,7 +381,9 @@
                                 FEATURE_ID,
                                 ACTIVITY_COMPONENT,
                                 UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(),
-                                false));
+                                false,
+                                /* targetTask */ null,
+                                /* options */ null));
 
         verify(mActivityTaskManagerInternal, never())
                 .startActivityAsUser(
@@ -396,7 +410,9 @@
                                 FEATURE_ID,
                                 ACTIVITY_COMPONENT,
                                 UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(),
-                                true));
+                                true,
+                                /* targetTask */ null,
+                                /* options */ null));
 
         verify(mActivityTaskManagerInternal, never())
                 .startActivityAsUser(
@@ -440,7 +456,9 @@
                                 FEATURE_ID,
                                 ACTIVITY_COMPONENT,
                                 UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(),
-                                false));
+                                false,
+                                /* targetTask */ null,
+                                /* options */ null));
 
         verify(mActivityTaskManagerInternal, never())
                 .startActivityAsUser(
@@ -465,7 +483,9 @@
                                 FEATURE_ID,
                                 new ComponentName(PACKAGE_TWO, "test"),
                                 UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(),
-                                true));
+                                true,
+                                /* targetTask */ null,
+                                /* options */ null));
 
         verify(mActivityTaskManagerInternal, never())
                 .startActivityAsUser(
@@ -490,7 +510,9 @@
                                 FEATURE_ID,
                                 new ComponentName(PACKAGE_TWO, "test"),
                                 UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(),
-                                false));
+                                false,
+                                /* targetTask */ null,
+                                /* options */ null));
 
         verify(mActivityTaskManagerInternal, never())
                 .startActivityAsUser(
@@ -515,7 +537,9 @@
                                 FEATURE_ID,
                                 ACTIVITY_COMPONENT,
                                 UserHandle.of(SECONDARY_USER).getIdentifier(),
-                                true));
+                                true,
+                                /* targetTask */ null,
+                                /* options */ null));
 
         verify(mActivityTaskManagerInternal, never())
                 .startActivityAsUser(
@@ -540,7 +564,9 @@
                                 FEATURE_ID,
                                 ACTIVITY_COMPONENT,
                                 UserHandle.of(SECONDARY_USER).getIdentifier(),
-                                false));
+                                false,
+                                /* targetTask */ null,
+                                /* options */ null));
 
         verify(mActivityTaskManagerInternal, never())
                 .startActivityAsUser(
@@ -564,7 +590,9 @@
                 FEATURE_ID,
                 ACTIVITY_COMPONENT,
                 UserHandle.of(PRIMARY_USER).getIdentifier(),
-                true);
+                true,
+                /* targetTask */ null,
+                /* options */ null);
 
         verify(mActivityTaskManagerInternal)
                 .startActivityAsUser(
@@ -578,6 +606,44 @@
                         eq(PRIMARY_USER));
     }
 
+    @Test
+    public void startActivityAsUser_sameTask_fromProfile_success() throws Exception {
+        mTestInjector.setCallingUserId(PROFILE_OF_PRIMARY_USER);
+
+        Bundle options = ActivityOptions.makeOpenCrossProfileAppsAnimation().toBundle();
+        IBinder result = ActivityContext.getWithContext(activity -> {
+            try {
+                IBinder targetTask = activity.getActivityToken();
+                mCrossProfileAppsServiceImpl.startActivityAsUser(
+                        mIApplicationThread,
+                        PACKAGE_ONE,
+                        FEATURE_ID,
+                        ACTIVITY_COMPONENT,
+                        UserHandle.of(PRIMARY_USER).getIdentifier(),
+                        true,
+                        targetTask,
+                        options);
+                return targetTask;
+            } catch (Exception re) {
+                return null;
+            }
+        });
+        if (result == null) {
+            throw new Exception();
+        }
+
+        verify(mActivityTaskManagerInternal)
+                .startActivityAsUser(
+                        nullable(IApplicationThread.class),
+                        eq(PACKAGE_ONE),
+                        eq(FEATURE_ID),
+                        any(Intent.class),
+                        eq(result),
+                        anyInt(),
+                        eq(options),
+                        eq(PRIMARY_USER));
+    }
+
     private void mockAppsInstalled(String packageName, int user, boolean installed) {
         when(mPackageManagerInternal.getPackageInfo(
                 eq(packageName),
diff --git a/services/tests/servicestests/src/com/android/server/pm/PackageInstallerSessionTest.java b/services/tests/servicestests/src/com/android/server/pm/PackageInstallerSessionTest.java
index b37e94a..27c3ca4 100644
--- a/services/tests/servicestests/src/com/android/server/pm/PackageInstallerSessionTest.java
+++ b/services/tests/servicestests/src/com/android/server/pm/PackageInstallerSessionTest.java
@@ -28,6 +28,7 @@
 import static org.xmlpull.v1.XmlPullParser.START_TAG;
 
 import android.content.pm.PackageInstaller;
+import android.content.pm.PackageManager;
 import android.platform.test.annotations.Presubmit;
 import android.util.AtomicFile;
 import android.util.Slog;
@@ -197,7 +198,7 @@
                 /* isFailed */ false,
                 /* isApplied */false,
                 /* stagedSessionErrorCode */
-                PackageInstaller.SessionInfo.SESSION_VERIFICATION_FAILED,
+                PackageManager.INSTALL_FAILED_VERIFICATION_FAILURE,
                 /* stagedSessionErrorMessage */ "some error");
     }
 
diff --git a/services/tests/servicestests/src/com/android/server/pm/PackageManagerServiceTest.java b/services/tests/servicestests/src/com/android/server/pm/PackageManagerServiceTest.java
index 050b224..946108d 100644
--- a/services/tests/servicestests/src/com/android/server/pm/PackageManagerServiceTest.java
+++ b/services/tests/servicestests/src/com/android/server/pm/PackageManagerServiceTest.java
@@ -23,11 +23,10 @@
 import static org.junit.Assert.fail;
 
 import static java.lang.reflect.Modifier.isFinal;
-import static java.lang.reflect.Modifier.isPrivate;
-import static java.lang.reflect.Modifier.isProtected;
 import static java.lang.reflect.Modifier.isPublic;
 import static java.lang.reflect.Modifier.isStatic;
 
+import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.app.AppGlobals;
 import android.content.IIntentReceiver;
@@ -63,7 +62,6 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
-import java.util.HashMap;
 import java.util.List;
 import java.util.regex.Pattern;
 
@@ -101,7 +99,7 @@
                     @Nullable Bundle bOptions) {
             }
 
-            public void sendPackageAddedForNewUsers(String packageName,
+            public void sendPackageAddedForNewUsers(@NonNull Computer snapshot, String packageName,
                     boolean sendBootComplete, boolean includeStopped, int appId,
                     int[] userIds, int[] instantUserIds, int dataLoaderType) {
             }
@@ -456,147 +454,6 @@
         return null;
     }
 
-    // Return the boolean locked value.  A null return means the annotation was not
-    // found.  This method will fail if the annotation is found but is not one of the
-    // known constants.
-    private Boolean getOverride(Method m) {
-        final String name = "Computer." + displayName(m);
-        final Computer.LiveImplementation annotation =
-                m.getAnnotation(Computer.LiveImplementation.class);
-        if (annotation == null) {
-            return null;
-        }
-        final int override = annotation.override();
-        if (override == Computer.LiveImplementation.MANDATORY) {
-            return true;
-        } else if (override == Computer.LiveImplementation.NOT_ALLOWED) {
-            return false;
-        } else {
-            flag(name, "invalid Live value: " + override);
-            return null;
-        }
-    }
-
-    @Test
-    public void testComputerStructure() {
-        // Verify that Copmuter methods are properly annotated and that ComputerLocked is
-        // properly populated per annotations.
-        // Call PackageManagerService.validateComputer();
-        Class base = Computer.class;
-
-        HashMap<Method, Boolean> methodType = new HashMap<>();
-
-        // Verify that all Computer methods are annotated and that the annotation
-        // parameter locked() is valid.
-        for (Method m : base.getDeclaredMethods()) {
-            final String name = "Computer." + displayName(m);
-            Boolean override = getOverride(m);
-            if (override == null) {
-                flag(name, "missing required Live annotation");
-            }
-            methodType.put(m, override);
-        }
-
-        Class coreClass = ComputerEngine.class;
-        final Method[] coreMethods = coreClass.getDeclaredMethods();
-
-        // Examine every method in the core.  If it inherits from a base method it must be
-        // "public final" if the base is NOT_ALLOWED or "public" if the base is MANDATORY.
-        // If the core method does not inherit from the base then it must be either
-        // private or protected.
-        for (Method m : base.getDeclaredMethods()) {
-            String name = "Computer." + displayName(m);
-            final boolean locked = methodType.get(m);
-            final Method core = matchMethod(m, coreMethods);
-            if (core == null) {
-                flag(name, "not overridden in ComputerEngine");
-                continue;
-            }
-            name = "ComputerEngine." + displayName(m);
-            final int modifiers = core.getModifiers();
-            if (!locked) {
-                if (!isPublic(modifiers)) {
-                    flag(name, "is not public");
-                }
-                if (!isFinal(modifiers)) {
-                    flag(name, "is not final");
-                }
-            }
-        }
-        // Any methods left in the coreMethods array must be private or protected.
-        // Protected methods must be overridden (and final) in the live list.
-        Method[] coreHelpers = new Method[coreMethods.length];
-        int coreIndex = 0;
-        for (Method m : coreMethods) {
-            if (m != null) {
-                final String name = "ComputerEngine." + displayName(m);
-                if (name.contains(".lambda$static")) {
-                    // skip static lambda function
-                    continue;
-                }
-
-                final int modifiers = m.getModifiers();
-                if (isPrivate(modifiers)) {
-                    // Okay
-                } else if (isProtected(modifiers)) {
-                    coreHelpers[coreIndex++] = m;
-                } else {
-                    flag(name, "is neither private nor protected");
-                }
-            }
-        }
-
-        Class liveClass = ComputerLocked.class;
-        final Method[] liveMethods = liveClass.getDeclaredMethods();
-
-        // Examine every method in the live list.  Every method must be final and must
-        // inherit either from base or core.  If the method inherits from a base method
-        // then the base must be MANDATORY.
-        for (Method m : base.getDeclaredMethods()) {
-            String name = "Computer." + displayName(m);
-            final boolean locked = methodType.get(m);
-            final Method live = matchMethod(m, liveMethods);
-            if (live == null) {
-                if (locked) {
-                    flag(name, "not overridden in ComputerLocked");
-                }
-                continue;
-            }
-            if (!locked) {
-                flag(name, "improperly overridden in ComputerLocked");
-                continue;
-            }
-
-            name = "ComputerLocked." + displayName(m);
-            final int modifiers = live.getModifiers();
-            if (!locked) {
-                if (!isPublic(modifiers)) {
-                    flag(name, "is not public");
-                }
-                if (!isFinal(modifiers)) {
-                    flag(name, "is not final");
-                }
-            }
-        }
-        for (Method m : coreHelpers) {
-            if (m == null) {
-                continue;
-            }
-            String name = "ComputerLocked." + displayName(m);
-            final Method live = matchMethod(m, liveMethods);
-            if (live == null) {
-                flag(name, "is not overridden in ComputerLocked");
-                continue;
-            }
-        }
-        for (Method m : liveMethods) {
-            if (m != null) {
-                String name = "ComputerLocked." + displayName(m);
-                flag(name, "illegal local method");
-            }
-        }
-    }
-
     private static PerPackageReadTimeouts[] getPerPackageReadTimeouts(String knownDigestersList) {
         final String defaultTimeouts = "3600000001:3600000002:3600000003";
         List<PerPackageReadTimeouts> result = PerPackageReadTimeouts.parseDigestersList(
diff --git a/services/tests/servicestests/src/com/android/server/pm/PackageManagerSettingsTests.java b/services/tests/servicestests/src/com/android/server/pm/PackageManagerSettingsTests.java
index 2b34bc2..f4ab3db 100644
--- a/services/tests/servicestests/src/com/android/server/pm/PackageManagerSettingsTests.java
+++ b/services/tests/servicestests/src/com/android/server/pm/PackageManagerSettingsTests.java
@@ -31,11 +31,11 @@
 import static org.hamcrest.CoreMatchers.notNullValue;
 import static org.hamcrest.CoreMatchers.nullValue;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotSame;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
-import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 import android.annotation.NonNull;
@@ -1103,6 +1103,59 @@
         assertThat(countDownLatch.getCount(), is(0L));
     }
 
+    @Test
+    public void testRegisterAndRemoveAppId() throws PackageManagerException {
+        // Test that the first new app UID should start from FIRST_APPLICATION_UID
+        final Settings settings = makeSettings();
+        final PackageSetting ps = createPackageSetting("com.foo");
+        assertTrue(settings.registerAppIdLPw(ps, false));
+        assertEquals(10000, ps.getAppId());
+        // Set up existing app IDs: 10000, 10001, 10003
+        final PackageSetting ps1 = createPackageSetting("com.foo1");
+        ps1.setAppId(10001);
+        final PackageSetting ps2 = createPackageSetting("com.foo2");
+        ps2.setAppId(10003);
+        final PackageSetting ps3 = createPackageSetting("com.foo3");
+        assertEquals(0, ps3.getAppId());
+        assertTrue(settings.registerAppIdLPw(ps1, false));
+        assertTrue(settings.registerAppIdLPw(ps2, false));
+        assertTrue(settings.registerAppIdLPw(ps3, false));
+        assertEquals(10001, ps1.getAppId());
+        assertEquals(10003, ps2.getAppId());
+        // Expecting the new one to start with the next available uid
+        assertEquals(10002, ps3.getAppId());
+        // Remove and insert a new one and the new one should not reuse the same uid
+        settings.removeAppIdLPw(10002);
+        final PackageSetting ps4 = createPackageSetting("com.foo4");
+        assertTrue(settings.registerAppIdLPw(ps4, false));
+        assertEquals(10004, ps4.getAppId());
+        // Keep adding more
+        final PackageSetting ps5 = createPackageSetting("com.foo5");
+        assertTrue(settings.registerAppIdLPw(ps5, false));
+        assertEquals(10005, ps5.getAppId());
+        // Remove the last one and the new one should use incremented uid
+        settings.removeAppIdLPw(10005);
+        final PackageSetting ps6 = createPackageSetting("com.foo6");
+        assertTrue(settings.registerAppIdLPw(ps6, false));
+        assertEquals(10006, ps6.getAppId());
+    }
+
+    /**
+     * Test replacing a PackageSetting with a SharedUserSetting in mAppIds
+     */
+    @Test
+    public void testAddPackageSetting() throws PackageManagerException {
+        final Settings settings = makeSettings();
+        final SharedUserSetting sus1 = new SharedUserSetting(
+                "TestUser", 0 /*pkgFlags*/, 0 /*pkgPrivateFlags*/);
+        sus1.mAppId = 10001;
+        final PackageSetting ps1 = createPackageSetting("com.foo");
+        ps1.setAppId(10001);
+        assertTrue(settings.registerAppIdLPw(ps1, false));
+        settings.addPackageSettingLPw(ps1, sus1);
+        assertSame(sus1, settings.getSharedUserSettingLPr(ps1));
+    }
+
     private void verifyUserState(PackageUserState userState,
             boolean notLaunched, boolean stopped, boolean installed) {
         assertThat(userState.getEnabledState(), is(0));
diff --git a/services/tests/servicestests/src/com/android/server/pm/UserManagerTest.java b/services/tests/servicestests/src/com/android/server/pm/UserManagerTest.java
index c7b5547..06b7112 100644
--- a/services/tests/servicestests/src/com/android/server/pm/UserManagerTest.java
+++ b/services/tests/servicestests/src/com/android/server/pm/UserManagerTest.java
@@ -316,7 +316,8 @@
                 asHandle(currentUser));
         try {
             assertThat(mUserManager.removeUserWhenPossible(user1.getUserHandle(),
-                    /* overrideDevicePolicy= */ false)).isEqualTo(UserManager.REMOVE_RESULT_ERROR);
+                    /* overrideDevicePolicy= */ false))
+                            .isEqualTo(UserManager.REMOVE_RESULT_ERROR_USER_RESTRICTION);
         } finally {
             mUserManager.setUserRestriction(UserManager.DISALLOW_REMOVE_USER, /* value= */ false,
                     asHandle(currentUser));
@@ -353,7 +354,8 @@
     @Test
     public void testRemoveUserWhenPossible_systemUserReturnsError() throws Exception {
         assertThat(mUserManager.removeUserWhenPossible(UserHandle.SYSTEM,
-                /* overrideDevicePolicy= */ false)).isEqualTo(UserManager.REMOVE_RESULT_ERROR);
+                /* overrideDevicePolicy= */ false))
+                        .isEqualTo(UserManager.REMOVE_RESULT_ERROR_SYSTEM_USER);
 
         assertThat(hasUser(UserHandle.USER_SYSTEM)).isTrue();
     }
@@ -363,7 +365,8 @@
     public void testRemoveUserWhenPossible_invalidUserReturnsError() throws Exception {
         assertThat(hasUser(Integer.MAX_VALUE)).isFalse();
         assertThat(mUserManager.removeUserWhenPossible(UserHandle.of(Integer.MAX_VALUE),
-                /* overrideDevicePolicy= */ false)).isEqualTo(UserManager.REMOVE_RESULT_ERROR);
+                /* overrideDevicePolicy= */ false))
+                        .isEqualTo(UserManager.REMOVE_RESULT_ERROR_USER_NOT_FOUND);
     }
 
     @MediumTest
diff --git a/services/tests/servicestests/src/com/android/server/statusbar/StatusBarManagerServiceTest.java b/services/tests/servicestests/src/com/android/server/statusbar/StatusBarManagerServiceTest.java
index 1442f1c..83139b0 100644
--- a/services/tests/servicestests/src/com/android/server/statusbar/StatusBarManagerServiceTest.java
+++ b/services/tests/servicestests/src/com/android/server/statusbar/StatusBarManagerServiceTest.java
@@ -23,9 +23,11 @@
 import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.fail;
+import static org.mockito.ArgumentMatchers.anyBoolean;
 import static org.mockito.ArgumentMatchers.nullable;
 import static org.mockito.Mockito.any;
 import static org.mockito.Mockito.anyInt;
+import static org.mockito.Mockito.anyLong;
 import static org.mockito.Mockito.anyString;
 import static org.mockito.Mockito.argThat;
 import static org.mockito.Mockito.eq;
@@ -37,6 +39,7 @@
 import android.Manifest;
 import android.app.ActivityManagerInternal;
 import android.app.StatusBarManager;
+import android.compat.testing.PlatformCompatChangeRule;
 import android.content.ComponentName;
 import android.content.Intent;
 import android.content.om.IOverlayManager;
@@ -62,10 +65,13 @@
 import com.android.server.policy.GlobalActionsProvider;
 import com.android.server.wm.ActivityTaskManagerInternal;
 
+import libcore.junit.util.compat.CoreCompatChangeRule;
+
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.TestRule;
 import org.junit.runner.RunWith;
 import org.junit.runners.JUnit4;
 import org.mockito.ArgumentCaptor;
@@ -73,6 +79,7 @@
 import org.mockito.Captor;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
+import org.mockito.stubbing.Answer;
 
 @RunWith(JUnit4.class)
 public class StatusBarManagerServiceTest {
@@ -88,6 +95,9 @@
     public final TestableContext mContext =
             new NoBroadcastContextWrapper(InstrumentationRegistry.getContext());
 
+    @Rule
+    public TestRule mCompatChangeRule = new PlatformCompatChangeRule();
+
     @Mock
     private ActivityTaskManagerInternal mActivityTaskManagerInternal;
     @Mock
@@ -127,6 +137,7 @@
 
         when(mMockStatusBar.asBinder()).thenReturn(mMockStatusBar);
         when(mApplicationInfo.loadLabel(any())).thenReturn(APP_NAME);
+        mockHandleIncomingUser();
 
         mStatusBarManagerService = new StatusBarManagerService(mContext);
         LocalServices.removeServiceForTest(StatusBarManagerInternal.class);
@@ -142,6 +153,80 @@
     }
 
     @Test
+    @CoreCompatChangeRule.EnableCompatChanges(
+            {StatusBarManagerService.REQUEST_LISTENING_MUST_MATCH_PACKAGE})
+    public void testRequestActive_changeEnabled_OKCall() throws RemoteException {
+        int user = 0;
+        mockEverything(user);
+        mStatusBarManagerService.requestTileServiceListeningState(TEST_COMPONENT, user);
+
+        verify(mMockStatusBar).requestTileServiceListeningState(TEST_COMPONENT);
+    }
+
+    @Test
+    @CoreCompatChangeRule.EnableCompatChanges(
+            {StatusBarManagerService.REQUEST_LISTENING_MUST_MATCH_PACKAGE})
+    public void testRequestActive_changeEnabled_differentPackage_fail() throws RemoteException {
+        when(mPackageManagerInternal.getPackageUid(TEST_PACKAGE, 0L, mContext.getUserId()))
+                .thenReturn(Binder.getCallingUid() + 1);
+        try {
+            mStatusBarManagerService.requestTileServiceListeningState(TEST_COMPONENT, 0);
+            fail("Should cause security exception");
+        } catch (SecurityException e) { }
+        verify(mMockStatusBar, never()).requestTileServiceListeningState(TEST_COMPONENT);
+    }
+
+    @Test
+    @CoreCompatChangeRule.EnableCompatChanges(
+            {StatusBarManagerService.REQUEST_LISTENING_MUST_MATCH_PACKAGE})
+    public void testRequestActive_changeEnabled_notCurrentUser_fail() throws RemoteException {
+        mockUidCheck();
+        int user = 0;
+        mockCurrentUserCheck(user);
+        try {
+            mStatusBarManagerService.requestTileServiceListeningState(TEST_COMPONENT, user + 1);
+            fail("Should cause illegal argument exception");
+        } catch (IllegalArgumentException e) { }
+
+        // Do not call into SystemUI
+        verify(mMockStatusBar, never()).requestTileServiceListeningState(TEST_COMPONENT);
+    }
+
+    @Test
+    @CoreCompatChangeRule.DisableCompatChanges(
+            {StatusBarManagerService.REQUEST_LISTENING_MUST_MATCH_PACKAGE})
+    public void testRequestActive_changeDisabled_pass() throws RemoteException {
+        int user = 0;
+        mockEverything(user);
+        mStatusBarManagerService.requestTileServiceListeningState(TEST_COMPONENT, user);
+
+        verify(mMockStatusBar).requestTileServiceListeningState(TEST_COMPONENT);
+    }
+
+    @Test
+    @CoreCompatChangeRule.DisableCompatChanges(
+            {StatusBarManagerService.REQUEST_LISTENING_MUST_MATCH_PACKAGE})
+    public void testRequestActive_changeDisabled_differentPackage_pass() throws RemoteException {
+        when(mPackageManagerInternal.getPackageUid(TEST_PACKAGE, 0L, mContext.getUserId()))
+                .thenReturn(Binder.getCallingUid() + 1);
+        mStatusBarManagerService.requestTileServiceListeningState(TEST_COMPONENT, 0);
+
+        verify(mMockStatusBar).requestTileServiceListeningState(TEST_COMPONENT);
+    }
+
+    @Test
+    @CoreCompatChangeRule.DisableCompatChanges(
+            {StatusBarManagerService.REQUEST_LISTENING_MUST_MATCH_PACKAGE})
+    public void testRequestActive_changeDisabled_notCurrentUser_pass() throws RemoteException {
+        mockUidCheck();
+        int user = 0;
+        mockCurrentUserCheck(user);
+        mStatusBarManagerService.requestTileServiceListeningState(TEST_COMPONENT, user + 1);
+
+        verify(mMockStatusBar).requestTileServiceListeningState(TEST_COMPONENT);
+    }
+
+    @Test
     public void testHandleIncomingUserCalled() {
         int fakeUser = 17;
         try {
@@ -252,7 +337,7 @@
         mockCurrentUserCheck(user);
         IntentMatcher im = new IntentMatcher(
                 new Intent(TileService.ACTION_QS_TILE).setComponent(TEST_COMPONENT));
-        when(mPackageManagerInternal.resolveService(argThat(im), nullable(String.class), eq(0),
+        when(mPackageManagerInternal.resolveService(argThat(im), nullable(String.class), eq(0L),
                 eq(user), anyInt())).thenReturn(null);
 
         Callback callback = new Callback();
@@ -272,7 +357,7 @@
 
         IntentMatcher im = new IntentMatcher(
                 new Intent(TileService.ACTION_QS_TILE).setComponent(TEST_COMPONENT));
-        when(mPackageManagerInternal.resolveService(argThat(im), nullable(String.class), eq(0),
+        when(mPackageManagerInternal.resolveService(argThat(im), nullable(String.class), eq(0L),
                 eq(user), anyInt())).thenReturn(r);
         when(mPackageManagerInternal.getComponentEnabledSetting(TEST_COMPONENT,
                 Binder.getCallingUid(), user)).thenReturn(
@@ -294,7 +379,7 @@
 
         IntentMatcher im = new IntentMatcher(
                 new Intent(TileService.ACTION_QS_TILE).setComponent(TEST_COMPONENT));
-        when(mPackageManagerInternal.resolveService(argThat(im), nullable(String.class), eq(0),
+        when(mPackageManagerInternal.resolveService(argThat(im), nullable(String.class), eq(0L),
                 eq(user), anyInt())).thenReturn(r);
         when(mPackageManagerInternal.getComponentEnabledSetting(TEST_COMPONENT,
                 Binder.getCallingUid(), user)).thenReturn(
@@ -318,7 +403,7 @@
 
         IntentMatcher im = new IntentMatcher(
                 new Intent(TileService.ACTION_QS_TILE).setComponent(TEST_COMPONENT));
-        when(mPackageManagerInternal.resolveService(argThat(im), nullable(String.class), eq(0),
+        when(mPackageManagerInternal.resolveService(argThat(im), nullable(String.class), eq(0L),
                 eq(user), anyInt())).thenReturn(r);
         when(mPackageManagerInternal.getComponentEnabledSetting(TEST_COMPONENT,
                 Binder.getCallingUid(), user)).thenReturn(
@@ -342,7 +427,7 @@
 
         IntentMatcher im = new IntentMatcher(
                 new Intent(TileService.ACTION_QS_TILE).setComponent(TEST_COMPONENT));
-        when(mPackageManagerInternal.resolveService(argThat(im), nullable(String.class), eq(0),
+        when(mPackageManagerInternal.resolveService(argThat(im), nullable(String.class), eq(0L),
                 eq(user), anyInt())).thenReturn(r);
         when(mPackageManagerInternal.getComponentEnabledSetting(TEST_COMPONENT,
                 Binder.getCallingUid(), user)).thenReturn(
@@ -607,23 +692,12 @@
     public void testSetNavBarMode_invalidInputThrowsError() throws RemoteException {
         int navBarModeInvalid = -1;
 
-        assertThrows(UnsupportedOperationException.class,
+        assertThrows(IllegalArgumentException.class,
                 () -> mStatusBarManagerService.setNavBarMode(navBarModeInvalid));
         verify(mOverlayManager, never()).setEnabledExclusiveInCategory(anyString(), anyInt());
     }
 
     @Test
-    public void testSetNavBarMode_noOverlayManagerDoesNotEnable() throws RemoteException {
-        mOverlayManager = null;
-        int navBarModeKids = StatusBarManager.NAV_BAR_MODE_KIDS;
-
-        mStatusBarManagerService.setNavBarMode(navBarModeKids);
-
-        assertEquals(navBarModeKids, mStatusBarManagerService.getNavBarMode());
-        verify(mOverlayManager, never()).setEnabledExclusiveInCategory(anyString(), anyInt());
-    }
-
-    @Test
     public void testSetNavBarMode_noPackageDoesNotEnable() throws Exception {
         mContext.setMockPackageManager(mPackageManager);
         when(mPackageManager.getPackageInfo(anyString(),
@@ -641,7 +715,7 @@
     }
 
     private void mockUidCheck(String packageName) {
-        when(mPackageManagerInternal.getPackageUid(eq(packageName), anyInt(), anyInt()))
+        when(mPackageManagerInternal.getPackageUid(eq(packageName), anyLong(), anyInt()))
                 .thenReturn(Binder.getCallingUid());
     }
 
@@ -667,7 +741,7 @@
 
         IntentMatcher im = new IntentMatcher(
                 new Intent(TileService.ACTION_QS_TILE).setComponent(componentName));
-        when(mPackageManagerInternal.resolveService(argThat(im), nullable(String.class), eq(0),
+        when(mPackageManagerInternal.resolveService(argThat(im), nullable(String.class), eq(0L),
                 eq(user), anyInt())).thenReturn(r);
         when(mPackageManagerInternal.getComponentEnabledSetting(componentName,
                 Binder.getCallingUid(), user)).thenReturn(
@@ -679,6 +753,15 @@
                 PROCESS_STATE_TOP);
     }
 
+    private void mockHandleIncomingUser() {
+        when(mActivityManagerInternal.handleIncomingUser(anyInt(), anyInt(), anyInt(), anyBoolean(),
+                anyInt(), anyString(), anyString())).thenAnswer(
+                    (Answer<Integer>) invocation -> {
+                        return invocation.getArgument(2); // same user
+                    }
+        );
+    }
+
     private void mockEverything(int user) {
         mockUidCheck();
         mockCurrentUserCheck(user);
diff --git a/services/tests/servicestests/src/com/android/server/usage/AppStandbyControllerTests.java b/services/tests/servicestests/src/com/android/server/usage/AppStandbyControllerTests.java
index 18d3f3d..210d2fa 100644
--- a/services/tests/servicestests/src/com/android/server/usage/AppStandbyControllerTests.java
+++ b/services/tests/servicestests/src/com/android/server/usage/AppStandbyControllerTests.java
@@ -62,7 +62,10 @@
 import static org.mockito.ArgumentMatchers.anyLong;
 import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyInt;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Matchers.intThat;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.spy;
@@ -76,11 +79,13 @@
 import android.appwidget.AppWidgetManager;
 import android.content.Context;
 import android.content.ContextWrapper;
+import android.content.Intent;
 import android.content.pm.ActivityInfo;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.PackageManagerInternal;
+import android.content.pm.ResolveInfo;
 import android.hardware.display.DisplayManager;
 import android.os.Handler;
 import android.os.Looper;
@@ -118,6 +123,7 @@
 import java.util.Set;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
 
 /**
  * Unit test for AppStandbyController.
@@ -421,8 +427,31 @@
         pib.packageName = PACKAGE_BACKGROUND_LOCATION;
         packages.add(pib);
 
-        doReturn(packages).when(mockPm).getInstalledPackagesAsUser(anyInt(), anyInt());
 
+        // Set up getInstalledPackagesAsUser().
+        doReturn(packages).when(mockPm).getInstalledPackagesAsUser(anyInt(),
+                anyInt());
+
+        // Set up getInstalledPackagesAsUser() for "MATCH_ONLY_SYSTEM"
+        doReturn(
+                packages.stream().filter(pinfo -> pinfo.applicationInfo.isSystemApp())
+                .collect(Collectors.toList())
+        ).when(mockPm).getInstalledPackagesAsUser(
+                intThat(i -> (i & PackageManager.MATCH_SYSTEM_ONLY) != 0),
+                anyInt());
+
+        // Set up queryIntentActivitiesAsUser()
+        final ArrayList<ResolveInfo> systemFrontDoorActivities = new ArrayList<>();
+        final ResolveInfo frontDoorActivity = new ResolveInfo();
+        frontDoorActivity.activityInfo = new ActivityInfo();
+        frontDoorActivity.activityInfo.packageName = pis.packageName;
+        systemFrontDoorActivities.add(frontDoorActivity);
+        doReturn(systemFrontDoorActivities).when(mockPm)
+                .queryIntentActivitiesAsUser(any(Intent.class),
+                intThat(i -> (i & PackageManager.MATCH_SYSTEM_ONLY) != 0),
+                anyInt());
+
+        // Set up other APIs.
         try {
             for (int i = 0; i < packages.size(); ++i) {
                 PackageInfo pkg = packages.get(i);
@@ -489,6 +518,7 @@
 
     @Before
     public void setUp() throws Exception {
+        LocalServices.removeServiceForTest(UsageStatsManagerInternal.class);
         LocalServices.addService(
                 UsageStatsManagerInternal.class, mock(UsageStatsManagerInternal.class));
         MyContextWrapper myContext = new MyContextWrapper(InstrumentationRegistry.getContext());
diff --git a/services/tests/uiservicestests/src/com/android/server/notification/PermissionHelperTest.java b/services/tests/uiservicestests/src/com/android/server/notification/PermissionHelperTest.java
index 50151bf..46b47f4 100644
--- a/services/tests/uiservicestests/src/com/android/server/notification/PermissionHelperTest.java
+++ b/services/tests/uiservicestests/src/com/android/server/notification/PermissionHelperTest.java
@@ -88,7 +88,7 @@
     @Before
     public void setUp() throws Exception {
         MockitoAnnotations.initMocks(this);
-        mPermissionHelper = new PermissionHelper(mPmi, mPackageManager, mPermManager, true);
+        mPermissionHelper = new PermissionHelper(mPmi, mPackageManager, mPermManager, true, false);
         PackageInfo testPkgInfo = new PackageInfo();
         testPkgInfo.requestedPermissions = new String[]{ Manifest.permission.POST_NOTIFICATIONS };
         when(mPackageManager.getPackageInfo(anyString(), anyLong(), anyInt()))
@@ -100,7 +100,7 @@
     public void testMethodsThrowIfMigrationDisabled() throws IllegalAccessException,
             InvocationTargetException {
         PermissionHelper permHelper =
-                new PermissionHelper(mPmi, mPackageManager, mPermManager, false);
+                new PermissionHelper(mPmi, mPackageManager, mPermManager, false, false);
 
         Method[] allMethods = PermissionHelper.class.getDeclaredMethods();
         for (Method method : allMethods) {
@@ -302,6 +302,26 @@
     }
 
     @Test
+    public void testSetNotificationPermission_pkgPerm_grantedByDefaultPermSet_allUserSet()
+            throws Exception {
+        mPermissionHelper = new PermissionHelper(mPmi, mPackageManager, mPermManager, true, true);
+        when(mPmi.checkPermission(anyString(), anyString(), anyInt()))
+                .thenReturn(PERMISSION_DENIED);
+        when(mPermManager.getPermissionFlags(anyString(),
+                eq(Manifest.permission.POST_NOTIFICATIONS),
+                anyInt())).thenReturn(FLAG_PERMISSION_GRANTED_BY_DEFAULT);
+        PermissionHelper.PackagePermission pkgPerm = new PermissionHelper.PackagePermission(
+                "pkg", 10, true, false);
+
+        mPermissionHelper.setNotificationPermission(pkgPerm);
+        verify(mPermManager).grantRuntimePermission(
+                "pkg", Manifest.permission.POST_NOTIFICATIONS, 10);
+        verify(mPermManager).updatePermissionFlags("pkg", Manifest.permission.POST_NOTIFICATIONS,
+                FLAG_PERMISSION_USER_SET | FLAG_PERMISSION_REVIEW_REQUIRED,
+                FLAG_PERMISSION_USER_SET, true, 10);
+    }
+
+    @Test
     public void testSetNotificationPermission_revokeUserSet() throws Exception {
         when(mPmi.checkPermission(anyString(), anyString(), anyInt()))
                 .thenReturn(PERMISSION_GRANTED);
diff --git a/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java b/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java
index 31be33e..fd1536c 100644
--- a/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java
+++ b/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java
@@ -43,6 +43,7 @@
 import static com.android.os.AtomsProto.DNDModeProto.ID_FIELD_NUMBER;
 import static com.android.os.AtomsProto.DNDModeProto.UID_FIELD_NUMBER;
 import static com.android.os.AtomsProto.DNDModeProto.ZEN_MODE_FIELD_NUMBER;
+import static com.android.server.notification.ZenModeHelper.RULE_LIMIT_PER_PACKAGE;
 
 import static junit.framework.Assert.assertEquals;
 import static junit.framework.Assert.assertFalse;
@@ -1611,6 +1612,35 @@
     }
 
     @Test
+    public void testAddAutomaticZenRule_beyondSystemLimit() {
+        for (int i = 0; i < RULE_LIMIT_PER_PACKAGE; i++) {
+            ScheduleInfo si = new ScheduleInfo();
+            si.startHour = i;
+            AutomaticZenRule zenRule = new AutomaticZenRule("name" + i,
+                    null,
+                    new ComponentName("android", "ScheduleConditionProvider"),
+                    ZenModeConfig.toScheduleConditionId(si),
+                    new ZenPolicy.Builder().build(),
+                    NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
+            String id = mZenModeHelperSpy.addAutomaticZenRule("android", zenRule, "test");
+            assertNotNull(id);
+        }
+        try {
+            AutomaticZenRule zenRule = new AutomaticZenRule("name",
+                    null,
+                    new ComponentName("android", "ScheduleConditionProvider"),
+                    ZenModeConfig.toScheduleConditionId(new ScheduleInfo()),
+                    new ZenPolicy.Builder().build(),
+                    NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
+            String id = mZenModeHelperSpy.addAutomaticZenRule("android", zenRule, "test");
+            fail("allowed too many rules to be created");
+        } catch (IllegalArgumentException e) {
+            // yay
+        }
+
+    }
+
+    @Test
     public void testAddAutomaticZenRule_CA() {
         AutomaticZenRule zenRule = new AutomaticZenRule("name",
                 null,
diff --git a/services/tests/wmtests/src/com/android/server/wm/ActivityMetricsLaunchObserverTests.java b/services/tests/wmtests/src/com/android/server/wm/ActivityMetricsLaunchObserverTests.java
index d4d8b868..7689e08 100644
--- a/services/tests/wmtests/src/com/android/server/wm/ActivityMetricsLaunchObserverTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/ActivityMetricsLaunchObserverTests.java
@@ -256,6 +256,14 @@
         mActivityMetricsLogger.notifyVisibilityChanged(noDrawnActivity);
 
         verifyAsync(mLaunchObserver).onActivityLaunchCancelled(eqProto(noDrawnActivity));
+
+        // If an activity is removed immediately before visibility update, it should cancel too.
+        final ActivityRecord removedImm = new ActivityBuilder(mAtm).setCreateTask(true).build();
+        clearInvocations(mLaunchObserver);
+        onActivityLaunched(removedImm);
+        removedImm.removeImmediately();
+        // Verify any() instead of proto because the field of record may be changed.
+        verifyAsync(mLaunchObserver).onActivityLaunchCancelled(any());
     }
 
     @Test
@@ -299,15 +307,16 @@
     @Test
     public void testOnReportFullyDrawn() {
         // Create an invisible event that should be cancelled after the next event starts.
-        onActivityLaunched(mTrampolineActivity);
-        mTrampolineActivity.mVisibleRequested = false;
+        final ActivityRecord prev = new ActivityBuilder(mAtm).setCreateTask(true).build();
+        onActivityLaunched(prev);
+        prev.mVisibleRequested = false;
 
         mActivityOptions = ActivityOptions.makeBasic();
         mActivityOptions.setSourceInfo(SourceInfo.TYPE_LAUNCHER, SystemClock.uptimeMillis() - 10);
         onIntentStarted(mTopActivity.intent);
         notifyActivityLaunched(START_SUCCESS, mTopActivity);
         verifyAsync(mLaunchObserver).onActivityLaunched(eqProto(mTopActivity), anyInt());
-        verifyAsync(mLaunchObserver).onActivityLaunchCancelled(eqProto(mTrampolineActivity));
+        verifyAsync(mLaunchObserver).onActivityLaunchCancelled(eqProto(prev));
 
         // The activity reports fully drawn before windows drawn, then the fully drawn event will
         // be pending (see {@link WindowingModeTransitionInfo#pendingFullyDrawn}).
diff --git a/services/tests/wmtests/src/com/android/server/wm/ActivityRecordTests.java b/services/tests/wmtests/src/com/android/server/wm/ActivityRecordTests.java
index 40ab8eb..f8e15041 100644
--- a/services/tests/wmtests/src/com/android/server/wm/ActivityRecordTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/ActivityRecordTests.java
@@ -2184,11 +2184,17 @@
 
     @Test
     public void testSupportsPictureInPicture() {
+        final Task task = new TaskBuilder(mSupervisor)
+                .setDisplay(mDisplayContent).build();
         final ActivityRecord activity = new ActivityBuilder(mAtm)
-                .setCreateTask(true)
+                .setTask(task)
                 .setResizeMode(ActivityInfo.RESIZE_MODE_UNRESIZEABLE)
                 .setActivityFlags(FLAG_SUPPORTS_PICTURE_IN_PICTURE)
                 .build();
+        spyOn(mDisplayContent);
+        spyOn(mDisplayContent.mDwpcHelper);
+        doReturn(true).when(mDisplayContent.mDwpcHelper).isWindowingModeSupported(
+                WINDOWING_MODE_PINNED);
 
         // Device not supports PIP
         mAtm.mSupportsPictureInPicture = false;
@@ -2201,6 +2207,15 @@
         // Activity not supports PIP
         activity.info.flags &= ~FLAG_SUPPORTS_PICTURE_IN_PICTURE;
         assertFalse(activity.supportsPictureInPicture());
+
+        // Activity supports PIP
+        activity.info.flags |= FLAG_SUPPORTS_PICTURE_IN_PICTURE;
+        assertTrue(activity.supportsPictureInPicture());
+
+        // Display not supports PIP
+        doReturn(false).when(mDisplayContent.mDwpcHelper).isWindowingModeSupported(
+                WINDOWING_MODE_PINNED);
+        assertFalse(activity.supportsPictureInPicture());
     }
 
     @Test
@@ -3086,11 +3101,11 @@
 
         // Simulate app re-start input or turning screen off/on then unlocked by un-secure
         // keyguard to back to the app, expect IME insets is not frozen
+        mDisplayContent.updateImeInputAndControlTarget(app);
+        assertFalse(app.mActivityRecord.mImeInsetsFrozenUntilStartInput);
         imeSource.setFrame(new Rect(100, 400, 500, 500));
         app.getInsetsState().addSource(imeSource);
         app.getInsetsState().setSourceVisible(ITYPE_IME, true);
-        mDisplayContent.updateImeInputAndControlTarget(app);
-        assertFalse(app.mActivityRecord.mImeInsetsFrozenUntilStartInput);
 
         // Verify when IME is visible and the app can receive the right IME insets from policy.
         makeWindowVisibleAndDrawn(app, mImeWindow);
diff --git a/services/tests/wmtests/src/com/android/server/wm/BackNavigationControllerTests.java b/services/tests/wmtests/src/com/android/server/wm/BackNavigationControllerTests.java
index c21a5b6..92550a3 100644
--- a/services/tests/wmtests/src/com/android/server/wm/BackNavigationControllerTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/BackNavigationControllerTests.java
@@ -30,7 +30,10 @@
 import android.annotation.NonNull;
 import android.hardware.HardwareBuffer;
 import android.platform.test.annotations.Presubmit;
+import android.window.BackEvent;
 import android.window.BackNavigationInfo;
+import android.window.IOnBackInvokedCallback;
+import android.window.OnBackInvokedDispatcher;
 import android.window.TaskSnapshot;
 
 import org.junit.Before;
@@ -42,15 +45,19 @@
 public class BackNavigationControllerTests extends WindowTestsBase {
 
     private BackNavigationController mBackNavigationController;
+    private IOnBackInvokedCallback mOnBackInvokedCallback;
 
     @Before
     public void setUp() throws Exception {
         mBackNavigationController = new BackNavigationController();
+        mOnBackInvokedCallback = createBackCallback();
     }
 
     @Test
     public void backTypeHomeWhenBackToLauncher() {
         Task task = createTopTaskWithActivity();
+        registerSystemOnBackInvokedCallback();
+
         BackNavigationInfo backNavigationInfo =
                 mBackNavigationController.startBackNavigation(task, new StubTransaction());
         assertThat(backNavigationInfo).isNotNull();
@@ -63,6 +70,8 @@
         Task taskA = createTask(mDefaultDisplay);
         createActivityRecord(taskA);
         Task task = createTopTaskWithActivity();
+        registerSystemOnBackInvokedCallback();
+
         BackNavigationInfo backNavigationInfo =
                 mBackNavigationController.startBackNavigation(task, new StubTransaction());
         assertThat(backNavigationInfo).isNotNull();
@@ -75,6 +84,8 @@
         Task task = createTopTaskWithActivity();
         mAtm.setFocusedTask(task.mTaskId,
                 createAppWindow(task, FIRST_APPLICATION_WINDOW, "window").mActivityRecord);
+        registerSystemOnBackInvokedCallback();
+
         BackNavigationInfo backNavigationInfo =
                 mBackNavigationController.startBackNavigation(task, new StubTransaction());
         assertThat(backNavigationInfo).isNotNull();
@@ -89,6 +100,7 @@
     public void backNavInfoFullyPopulated() {
         Task task = createTopTaskWithActivity();
         createAppWindow(task, FIRST_APPLICATION_WINDOW, "window");
+        registerSystemOnBackInvokedCallback();
 
         // We need a mock screenshot so
         TaskSnapshotController taskSnapshotController = createMockTaskSnapshotController();
@@ -104,6 +116,30 @@
         assertThat(backNavigationInfo.getTaskWindowConfiguration()).isNotNull();
     }
 
+    @Test
+    public void preparesForBackToHome() {
+        Task task = createTopTaskWithActivity();
+        ActivityRecord activity = task.getTopActivity(false, false);
+        registerSystemOnBackInvokedCallback();
+
+        BackNavigationInfo backNavigationInfo =
+                mBackNavigationController.startBackNavigation(task, new StubTransaction());
+        assertThat(typeToString(backNavigationInfo.getType()))
+                .isEqualTo(typeToString(BackNavigationInfo.TYPE_RETURN_TO_HOME));
+    }
+
+    @Test
+    public void backTypeCallback() {
+        Task task = createTopTaskWithActivity();
+        ActivityRecord activity = task.getTopActivity(false, false);
+        registerApplicationOnBackInvokedCallback();
+
+        BackNavigationInfo backNavigationInfo =
+                mBackNavigationController.startBackNavigation(task, new StubTransaction());
+        assertThat(typeToString(backNavigationInfo.getType()))
+                .isEqualTo(typeToString(BackNavigationInfo.TYPE_CALLBACK));
+    }
+
     @NonNull
     private TaskSnapshotController createMockTaskSnapshotController() {
         TaskSnapshotController taskSnapshotController = mock(TaskSnapshotController.class);
@@ -126,4 +162,30 @@
         mAtm.setFocusedTask(task.mTaskId, record);
         return task;
     }
+
+    private void registerSystemOnBackInvokedCallback() {
+        mWm.getFocusedWindowLocked().setOnBackInvokedCallback(
+                mOnBackInvokedCallback, OnBackInvokedDispatcher.PRIORITY_SYSTEM);
+    }
+
+    private void registerApplicationOnBackInvokedCallback() {
+        mWm.getFocusedWindowLocked().setOnBackInvokedCallback(
+                mOnBackInvokedCallback, OnBackInvokedDispatcher.PRIORITY_DEFAULT);
+    }
+
+    private IOnBackInvokedCallback createBackCallback() {
+        return new IOnBackInvokedCallback.Stub() {
+            @Override
+            public void onBackStarted() { }
+
+            @Override
+            public void onBackProgressed(BackEvent backEvent) { }
+
+            @Override
+            public void onBackCancelled() { }
+
+            @Override
+            public void onBackInvoked() { }
+        };
+    }
 }
diff --git a/services/tests/wmtests/src/com/android/server/wm/DisplayContentTests.java b/services/tests/wmtests/src/com/android/server/wm/DisplayContentTests.java
index 8e990f7..40b4601 100644
--- a/services/tests/wmtests/src/com/android/server/wm/DisplayContentTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/DisplayContentTests.java
@@ -1102,6 +1102,21 @@
         assertEquals(dc.getImeContainer().getParentSurfaceControl(), dc.computeImeParent());
     }
 
+    @UseTestDisplay(addWindows = W_ACTIVITY)
+    @Test
+    public void testComputeImeParent_inputTargetNotUpdate() throws Exception {
+        WindowState app1 = createWindow(null, TYPE_BASE_APPLICATION, "app1");
+        WindowState app2 = createWindow(null, TYPE_BASE_APPLICATION, "app2");
+        doReturn(true).when(mDisplayContent).shouldImeAttachedToApp();
+        mDisplayContent.setImeLayeringTarget(app1);
+        mDisplayContent.setImeInputTarget(app1);
+        assertEquals(app1.mActivityRecord.getSurfaceControl(), mDisplayContent.computeImeParent());
+        mDisplayContent.setImeLayeringTarget(app2);
+        // Expect null means no change IME parent when the IME layering target not yet
+        // request IME to be the input target.
+        assertNull(mDisplayContent.computeImeParent());
+    }
+
     @Test
     public void testInputMethodInputTarget_isClearedWhenWindowStateIsRemoved() throws Exception {
         final DisplayContent dc = createNewDisplay();
diff --git a/services/tests/wmtests/src/com/android/server/wm/DisplayWindowPolicyControllerHelperTests.java b/services/tests/wmtests/src/com/android/server/wm/DisplayWindowPolicyControllerHelperTests.java
index 6e11d8c..f968999 100644
--- a/services/tests/wmtests/src/com/android/server/wm/DisplayWindowPolicyControllerHelperTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/DisplayWindowPolicyControllerHelperTests.java
@@ -16,13 +16,18 @@
 
 package com.android.server.wm;
 
+import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
+import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
+
 import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.anyInt;
 
+import android.app.WindowConfiguration;
 import android.content.ComponentName;
 import android.content.pm.ActivityInfo;
 import android.os.UserHandle;
@@ -37,6 +42,7 @@
 import org.junit.runner.RunWith;
 
 import java.util.List;
+import java.util.Set;
 
 /**
  * Tests for the {@link DisplayWindowPolicyControllerHelper} class.
@@ -113,6 +119,39 @@
         return activity;
     }
 
+    @Test
+    public void testIsWindowingModeSupported_noController_returnTrueForAnyWindowingMode() {
+        doReturn(null).when(mWm.mDisplayManagerInternal)
+                .getDisplayWindowPolicyController(anyInt());
+        mSecondaryDisplay = createNewDisplay();
+        assertFalse(mSecondaryDisplay.mDwpcHelper.hasController());
+
+        assertTrue(mSecondaryDisplay.mDwpcHelper.isWindowingModeSupported(WINDOWING_MODE_PINNED));
+        assertTrue(
+                mSecondaryDisplay.mDwpcHelper.isWindowingModeSupported(WINDOWING_MODE_FULLSCREEN));
+    }
+
+    @Test
+    public void testIsWindowingModeSupported_withoutSettingSupportedMode_returnFalse() {
+        assertFalse(mSecondaryDisplay.mDwpcHelper.isWindowingModeSupported(WINDOWING_MODE_PINNED));
+    }
+
+    @Test
+    public void testIsWindowingModeSupported_withoutSupportedMode_defaultSupportFullScreen() {
+        assertTrue(
+                mSecondaryDisplay.mDwpcHelper.isWindowingModeSupported(WINDOWING_MODE_FULLSCREEN));
+    }
+
+    @Test
+    public void testIsWindowingModeSupported_setPinnedMode_returnTrue() {
+        Set<Integer> supportedWindowingMode = new ArraySet<>();
+        supportedWindowingMode.add(WINDOWING_MODE_PINNED);
+
+        mDwpc.setSupportedWindowingModes(supportedWindowingMode);
+
+        assertTrue(mSecondaryDisplay.mDwpcHelper.isWindowingModeSupported(WINDOWING_MODE_PINNED));
+    }
+
     private class TestDisplayWindowPolicyController extends DisplayWindowPolicyController {
 
         ComponentName mTopActivity = null;
@@ -120,7 +159,8 @@
         ArraySet<Integer> mRunningUids = new ArraySet<>();
 
         @Override
-        public boolean canContainActivities(@NonNull List<ActivityInfo> activities) {
+        public boolean canContainActivities(@NonNull List<ActivityInfo> activities,
+                @WindowConfiguration.WindowingMode int windowingMode) {
             return false;
         }
 
diff --git a/services/tests/wmtests/src/com/android/server/wm/TaskFragmentOrganizerControllerTest.java b/services/tests/wmtests/src/com/android/server/wm/TaskFragmentOrganizerControllerTest.java
index 0debdfa..c615866 100644
--- a/services/tests/wmtests/src/com/android/server/wm/TaskFragmentOrganizerControllerTest.java
+++ b/services/tests/wmtests/src/com/android/server/wm/TaskFragmentOrganizerControllerTest.java
@@ -359,17 +359,11 @@
     public void testApplyTransaction_enforceHierarchyChange_createTaskFragment()
             throws RemoteException {
         mController.registerOrganizer(mIOrganizer);
-        final ActivityRecord activity = createActivityRecord(mDisplayContent);
-        final int uid = Binder.getCallingUid();
-        activity.info.applicationInfo.uid = uid;
-        activity.getTask().effectiveUid = uid;
+        final ActivityRecord ownerActivity = createActivityRecord(mDisplayContent);
         final IBinder fragmentToken = new Binder();
-        final TaskFragmentCreationParams params = new TaskFragmentCreationParams.Builder(
-                mOrganizerToken, fragmentToken, activity.token).build();
-        mOrganizer.applyTransaction(mTransaction);
 
         // Allow organizer to create TaskFragment and start/reparent activity to TaskFragment.
-        mTransaction.createTaskFragment(params);
+        createTaskFragmentFromOrganizer(mTransaction, ownerActivity, fragmentToken);
         mTransaction.startActivityInTaskFragment(
                 mFragmentToken, null /* callerToken */, new Intent(), null /* activityOptions */);
         mTransaction.reparentActivityToTaskFragment(mFragmentToken, mock(IBinder.class));
@@ -381,7 +375,7 @@
         final TaskFragment taskFragment = mAtm.mWindowOrganizerController
                 .getTaskFragment(fragmentToken);
         assertNotNull(taskFragment);
-        assertEquals(activity.getTask(), taskFragment.getTask());
+        assertEquals(ownerActivity.getTask(), taskFragment.getTask());
     }
 
     @Test
@@ -523,4 +517,43 @@
         mController.dispatchPendingEvents();
         verify(mOrganizer).onTaskFragmentInfoChanged(any());
     }
+
+    /**
+     * When an embedded {@link TaskFragment} is removed, we should clean up the reference in the
+     * {@link WindowOrganizerController}.
+     */
+    @Test
+    public void testTaskFragmentRemoved_cleanUpEmbeddedTaskFragment()
+            throws RemoteException {
+        mController.registerOrganizer(mIOrganizer);
+        final ActivityRecord ownerActivity = createActivityRecord(mDisplayContent);
+        final IBinder fragmentToken = new Binder();
+        createTaskFragmentFromOrganizer(mTransaction, ownerActivity, fragmentToken);
+        mAtm.getWindowOrganizerController().applyTransaction(mTransaction);
+        final TaskFragment taskFragment = mAtm.mWindowOrganizerController
+                .getTaskFragment(fragmentToken);
+
+        assertNotNull(taskFragment);
+
+        taskFragment.removeImmediately();
+
+        assertNull(mAtm.mWindowOrganizerController.getTaskFragment(fragmentToken));
+    }
+
+    /**
+     * Creates a {@link TaskFragment} with the {@link WindowContainerTransaction}. Calls
+     * {@link WindowOrganizerController#applyTransaction} to apply the transaction,
+     */
+    private void createTaskFragmentFromOrganizer(WindowContainerTransaction wct,
+            ActivityRecord ownerActivity, IBinder fragmentToken) {
+        final int uid = Binder.getCallingUid();
+        ownerActivity.info.applicationInfo.uid = uid;
+        ownerActivity.getTask().effectiveUid = uid;
+        final TaskFragmentCreationParams params = new TaskFragmentCreationParams.Builder(
+                mOrganizerToken, fragmentToken, ownerActivity.token).build();
+        mOrganizer.applyTransaction(wct);
+
+        // Allow organizer to create TaskFragment and start/reparent activity to TaskFragment.
+        wct.createTaskFragment(params);
+    }
 }
diff --git a/services/tests/wmtests/src/com/android/server/wm/TaskTests.java b/services/tests/wmtests/src/com/android/server/wm/TaskTests.java
index fe41734..9304761 100644
--- a/services/tests/wmtests/src/com/android/server/wm/TaskTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/TaskTests.java
@@ -146,6 +146,27 @@
     }
 
     @Test
+    public void testRemoveContainer_multipleNestedTasks() {
+        final Task rootTask = createTask(mDisplayContent);
+        rootTask.mCreatedByOrganizer = true;
+        final Task task1 = new TaskBuilder(mSupervisor).setParentTaskFragment(rootTask).build();
+        final Task task2 = new TaskBuilder(mSupervisor).setParentTaskFragment(rootTask).build();
+        final ActivityRecord activity1 = createActivityRecord(task1);
+        final ActivityRecord activity2 = createActivityRecord(task2);
+        activity1.setVisible(false);
+
+        // All activities under the root task should be finishing.
+        rootTask.remove(true /* withTransition */, "test");
+        assertTrue(activity1.finishing);
+        assertTrue(activity2.finishing);
+
+        // After all activities activities are destroyed, the root task should also be removed.
+        activity1.removeImmediately();
+        activity2.removeImmediately();
+        assertFalse(rootTask.isAttached());
+    }
+
+    @Test
     public void testRemoveContainer_deferRemoval() {
         final Task rootTask = createTask(mDisplayContent);
         final Task task = createTaskInRootTask(rootTask, 0 /* userId */);
@@ -238,6 +259,20 @@
     }
 
     @Test
+    public void testPerformClearTop() {
+        final Task task = createTask(mDisplayContent);
+        final ActivityRecord activity1 = new ActivityBuilder(mAtm).setTask(task).build();
+        final ActivityRecord activity2 = new ActivityBuilder(mAtm).setTask(task).build();
+        // Detach from process so the activities can be removed from hierarchy when finishing.
+        activity1.detachFromProcess();
+        activity2.detachFromProcess();
+        assertNull(task.performClearTop(activity1, 0 /* launchFlags */));
+        assertFalse(task.hasChild());
+        // In real case, the task should be preserved for adding new activity.
+        assertTrue(task.isAttached());
+    }
+
+    @Test
     public void testRemoveChildForOverlayTask() {
         final Task task = createTask(mDisplayContent);
         final int taskId = task.mTaskId;
diff --git a/services/tests/wmtests/src/com/android/server/wm/TestIWindow.java b/services/tests/wmtests/src/com/android/server/wm/TestIWindow.java
index 7dfb5ae..4f35d55 100644
--- a/services/tests/wmtests/src/com/android/server/wm/TestIWindow.java
+++ b/services/tests/wmtests/src/com/android/server/wm/TestIWindow.java
@@ -44,7 +44,7 @@
     @Override
     public void resized(ClientWindowFrames frames, boolean reportDraw,
             MergedConfiguration mergedConfig, boolean forceLayout, boolean alwaysConsumeSystemBars,
-            int displayId) throws RemoteException {
+            int displayId, int seqId) throws RemoteException {
     }
 
     @Override
diff --git a/services/tests/wmtests/src/com/android/server/wm/TransitionTests.java b/services/tests/wmtests/src/com/android/server/wm/TransitionTests.java
index c4547f6..7e5d017 100644
--- a/services/tests/wmtests/src/com/android/server/wm/TransitionTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/TransitionTests.java
@@ -692,7 +692,8 @@
         statusBar.mWinAnimator.mDrawState = WindowStateAnimator.DRAW_PENDING;
         final SurfaceControl.Transaction postDrawTransaction =
                 mock(SurfaceControl.Transaction.class);
-        final boolean layoutNeeded = statusBar.finishDrawing(postDrawTransaction);
+        final boolean layoutNeeded = statusBar.finishDrawing(postDrawTransaction,
+                Integer.MAX_VALUE);
         assertFalse(layoutNeeded);
 
         transactionCommittedListener.onTransactionCommitted();
@@ -742,7 +743,7 @@
         player.finish();
 
         // The controller should be cleared if the target windows are drawn.
-        statusBar.finishDrawing(mWm.mTransactionFactory.get());
+        statusBar.finishDrawing(mWm.mTransactionFactory.get(), Integer.MAX_VALUE);
         statusBar.setOrientationChanging(false);
         assertNull(mDisplayContent.getAsyncRotationController());
     }
diff --git a/services/tests/wmtests/src/com/android/server/wm/WindowLayoutTests.java b/services/tests/wmtests/src/com/android/server/wm/WindowLayoutTests.java
index 7d2e9bf..ea18e58 100644
--- a/services/tests/wmtests/src/com/android/server/wm/WindowLayoutTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/WindowLayoutTests.java
@@ -45,6 +45,7 @@
 import android.view.WindowInsets;
 import android.view.WindowLayout;
 import android.view.WindowManager;
+import android.window.ClientWindowFrames;
 
 import androidx.test.filters.SmallTest;
 
@@ -71,9 +72,7 @@
     private static final Insets WATERFALL_INSETS = Insets.of(6, 0, 12, 0);
 
     private final WindowLayout mWindowLayout = new WindowLayout();
-    private final Rect mDisplayFrame = new Rect();
-    private final Rect mParentFrame = new Rect();
-    private final Rect mFrame = new Rect();
+    private final ClientWindowFrames mOutFrames = new ClientWindowFrames();
 
     private WindowManager.LayoutParams mAttrs;
     private InsetsState mState;
@@ -108,7 +107,7 @@
     private void computeFrames() {
         mWindowLayout.computeFrames(mAttrs, mState, mDisplayCutoutSafe, mWindowBounds,
                 mWindowingMode, mRequestedWidth, mRequestedHeight, mRequestedVisibilities,
-                mAttachedWindowFrame, mCompatScale, mDisplayFrame, mParentFrame, mFrame);
+                mAttachedWindowFrame, mCompatScale, mOutFrames);
     }
 
     private void addDisplayCutout() {
@@ -146,9 +145,9 @@
     public void defaultParams() {
         computeFrames();
 
-        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mDisplayFrame);
-        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mParentFrame);
-        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mFrame);
+        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mOutFrames.displayFrame);
+        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mOutFrames.parentFrame);
+        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mOutFrames.frame);
     }
 
     @Test
@@ -157,9 +156,9 @@
         mRequestedHeight = UNSPECIFIED_LENGTH;
         computeFrames();
 
-        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mDisplayFrame);
-        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mParentFrame);
-        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mFrame);
+        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mOutFrames.displayFrame);
+        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mOutFrames.parentFrame);
+        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mOutFrames.frame);
     }
 
     @Test
@@ -173,9 +172,9 @@
         mAttrs.gravity = Gravity.LEFT | Gravity.TOP;
         computeFrames();
 
-        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mDisplayFrame);
-        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mParentFrame);
-        assertRect(0, STATUS_BAR_HEIGHT, width, STATUS_BAR_HEIGHT + height, mFrame);
+        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mOutFrames.displayFrame);
+        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mOutFrames.parentFrame);
+        assertRect(0, STATUS_BAR_HEIGHT, width, STATUS_BAR_HEIGHT + height, mOutFrames.frame);
     }
 
     @Test
@@ -186,9 +185,12 @@
         mRequestedHeight = UNSPECIFIED_LENGTH;
         computeFrames();
 
-        assertRect(0, top, DISPLAY_WIDTH, DISPLAY_HEIGHT - NAVIGATION_BAR_HEIGHT, mDisplayFrame);
-        assertRect(0, top, DISPLAY_WIDTH, DISPLAY_HEIGHT - NAVIGATION_BAR_HEIGHT, mParentFrame);
-        assertRect(0, top, DISPLAY_WIDTH, DISPLAY_HEIGHT - NAVIGATION_BAR_HEIGHT, mFrame);
+        assertRect(0, top, DISPLAY_WIDTH, DISPLAY_HEIGHT - NAVIGATION_BAR_HEIGHT,
+                mOutFrames.displayFrame);
+        assertRect(0, top, DISPLAY_WIDTH, DISPLAY_HEIGHT - NAVIGATION_BAR_HEIGHT,
+                mOutFrames.parentFrame);
+        assertRect(0, top, DISPLAY_WIDTH, DISPLAY_HEIGHT - NAVIGATION_BAR_HEIGHT,
+                mOutFrames.frame);
     }
 
     @Test
@@ -196,9 +198,9 @@
         mAttrs.setFitInsetsTypes(WindowInsets.Type.statusBars());
         computeFrames();
 
-        assertInsetByTopBottom(STATUS_BAR_HEIGHT, 0, mDisplayFrame);
-        assertInsetByTopBottom(STATUS_BAR_HEIGHT, 0, mParentFrame);
-        assertInsetByTopBottom(STATUS_BAR_HEIGHT, 0, mFrame);
+        assertInsetByTopBottom(STATUS_BAR_HEIGHT, 0, mOutFrames.displayFrame);
+        assertInsetByTopBottom(STATUS_BAR_HEIGHT, 0, mOutFrames.parentFrame);
+        assertInsetByTopBottom(STATUS_BAR_HEIGHT, 0, mOutFrames.frame);
     }
 
     @Test
@@ -206,9 +208,9 @@
         mAttrs.setFitInsetsTypes(WindowInsets.Type.navigationBars());
         computeFrames();
 
-        assertInsetByTopBottom(0, NAVIGATION_BAR_HEIGHT, mDisplayFrame);
-        assertInsetByTopBottom(0, NAVIGATION_BAR_HEIGHT, mParentFrame);
-        assertInsetByTopBottom(0, NAVIGATION_BAR_HEIGHT, mFrame);
+        assertInsetByTopBottom(0, NAVIGATION_BAR_HEIGHT, mOutFrames.displayFrame);
+        assertInsetByTopBottom(0, NAVIGATION_BAR_HEIGHT, mOutFrames.parentFrame);
+        assertInsetByTopBottom(0, NAVIGATION_BAR_HEIGHT, mOutFrames.frame);
     }
 
     @Test
@@ -216,9 +218,9 @@
         mAttrs.setFitInsetsTypes(0);
         computeFrames();
 
-        assertInsetByTopBottom(0, 0, mDisplayFrame);
-        assertInsetByTopBottom(0, 0, mParentFrame);
-        assertInsetByTopBottom(0, 0, mFrame);
+        assertInsetByTopBottom(0, 0, mOutFrames.displayFrame);
+        assertInsetByTopBottom(0, 0, mOutFrames.parentFrame);
+        assertInsetByTopBottom(0, 0, mOutFrames.frame);
     }
 
     @Test
@@ -226,9 +228,9 @@
         mAttrs.setFitInsetsSides(WindowInsets.Side.all());
         computeFrames();
 
-        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mDisplayFrame);
-        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mParentFrame);
-        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mFrame);
+        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mOutFrames.displayFrame);
+        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mOutFrames.parentFrame);
+        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mOutFrames.frame);
     }
 
     @Test
@@ -236,9 +238,9 @@
         mAttrs.setFitInsetsSides(WindowInsets.Side.TOP);
         computeFrames();
 
-        assertInsetByTopBottom(STATUS_BAR_HEIGHT, 0, mDisplayFrame);
-        assertInsetByTopBottom(STATUS_BAR_HEIGHT, 0, mParentFrame);
-        assertInsetByTopBottom(STATUS_BAR_HEIGHT, 0, mFrame);
+        assertInsetByTopBottom(STATUS_BAR_HEIGHT, 0, mOutFrames.displayFrame);
+        assertInsetByTopBottom(STATUS_BAR_HEIGHT, 0, mOutFrames.parentFrame);
+        assertInsetByTopBottom(STATUS_BAR_HEIGHT, 0, mOutFrames.frame);
     }
 
     @Test
@@ -246,9 +248,9 @@
         mAttrs.setFitInsetsSides(0);
         computeFrames();
 
-        assertInsetByTopBottom(0, 0, mDisplayFrame);
-        assertInsetByTopBottom(0, 0, mParentFrame);
-        assertInsetByTopBottom(0, 0, mFrame);
+        assertInsetByTopBottom(0, 0, mOutFrames.displayFrame);
+        assertInsetByTopBottom(0, 0, mOutFrames.parentFrame);
+        assertInsetByTopBottom(0, 0, mOutFrames.frame);
     }
 
     @Test
@@ -257,9 +259,9 @@
         mState.getSource(ITYPE_NAVIGATION_BAR).setVisible(false);
         computeFrames();
 
-        assertInsetByTopBottom(0, 0, mDisplayFrame);
-        assertInsetByTopBottom(0, 0, mParentFrame);
-        assertInsetByTopBottom(0, 0, mFrame);
+        assertInsetByTopBottom(0, 0, mOutFrames.displayFrame);
+        assertInsetByTopBottom(0, 0, mOutFrames.parentFrame);
+        assertInsetByTopBottom(0, 0, mOutFrames.frame);
     }
 
     @Test
@@ -269,9 +271,9 @@
         mAttrs.setFitInsetsIgnoringVisibility(true);
         computeFrames();
 
-        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mDisplayFrame);
-        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mParentFrame);
-        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mFrame);
+        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mOutFrames.displayFrame);
+        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mOutFrames.parentFrame);
+        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mOutFrames.frame);
     }
 
     @Test
@@ -282,9 +284,9 @@
         mAttrs.privateFlags |= PRIVATE_FLAG_INSET_PARENT_FRAME_BY_IME;
         computeFrames();
 
-        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mDisplayFrame);
-        assertInsetByTopBottom(STATUS_BAR_HEIGHT, IME_HEIGHT, mParentFrame);
-        assertInsetByTopBottom(STATUS_BAR_HEIGHT, IME_HEIGHT, mFrame);
+        assertInsetByTopBottom(STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT, mOutFrames.displayFrame);
+        assertInsetByTopBottom(STATUS_BAR_HEIGHT, IME_HEIGHT, mOutFrames.parentFrame);
+        assertInsetByTopBottom(STATUS_BAR_HEIGHT, IME_HEIGHT, mOutFrames.frame);
     }
 
     @Test
@@ -295,11 +297,11 @@
         computeFrames();
 
         assertInsetBy(WATERFALL_INSETS.left, DISPLAY_CUTOUT_HEIGHT, WATERFALL_INSETS.right, 0,
-                mDisplayFrame);
+                mOutFrames.displayFrame);
         assertInsetBy(WATERFALL_INSETS.left, DISPLAY_CUTOUT_HEIGHT, WATERFALL_INSETS.right, 0,
-                mParentFrame);
+                mOutFrames.parentFrame);
         assertInsetBy(WATERFALL_INSETS.left, DISPLAY_CUTOUT_HEIGHT, WATERFALL_INSETS.right, 0,
-                mFrame);
+                mOutFrames.frame);
     }
 
     @Test
@@ -310,11 +312,11 @@
         computeFrames();
 
         assertInsetBy(WATERFALL_INSETS.left, STATUS_BAR_HEIGHT, WATERFALL_INSETS.right, 0,
-                mDisplayFrame);
+                mOutFrames.displayFrame);
         assertInsetBy(WATERFALL_INSETS.left, STATUS_BAR_HEIGHT, WATERFALL_INSETS.right, 0,
-                mParentFrame);
+                mOutFrames.parentFrame);
         assertInsetBy(WATERFALL_INSETS.left, STATUS_BAR_HEIGHT, WATERFALL_INSETS.right, 0,
-                mFrame);
+                mOutFrames.frame);
     }
 
     @Test
@@ -325,9 +327,9 @@
         mAttrs.setFitInsetsTypes(0);
         computeFrames();
 
-        assertInsetBy(WATERFALL_INSETS.left, 0, WATERFALL_INSETS.right, 0, mDisplayFrame);
-        assertInsetBy(WATERFALL_INSETS.left, 0, WATERFALL_INSETS.right, 0, mParentFrame);
-        assertInsetBy(WATERFALL_INSETS.left, 0, WATERFALL_INSETS.right, 0, mFrame);
+        assertInsetBy(WATERFALL_INSETS.left, 0, WATERFALL_INSETS.right, 0, mOutFrames.displayFrame);
+        assertInsetBy(WATERFALL_INSETS.left, 0, WATERFALL_INSETS.right, 0, mOutFrames.parentFrame);
+        assertInsetBy(WATERFALL_INSETS.left, 0, WATERFALL_INSETS.right, 0, mOutFrames.frame);
     }
 
     @Test
@@ -342,9 +344,9 @@
         mAttrs.privateFlags |= PRIVATE_FLAG_LAYOUT_SIZE_EXTENDED_BY_CUTOUT;
         computeFrames();
 
-        assertRect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT, mDisplayFrame);
-        assertRect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT, mParentFrame);
-        assertRect(0, 0, DISPLAY_WIDTH, height + DISPLAY_CUTOUT_HEIGHT, mFrame);
+        assertRect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT, mOutFrames.displayFrame);
+        assertRect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT, mOutFrames.parentFrame);
+        assertRect(0, 0, DISPLAY_WIDTH, height + DISPLAY_CUTOUT_HEIGHT, mOutFrames.frame);
     }
 
     @Test
@@ -357,11 +359,11 @@
         computeFrames();
 
         assertInsetBy(WATERFALL_INSETS.left, STATUS_BAR_HEIGHT, WATERFALL_INSETS.right, 0,
-                mDisplayFrame);
+                mOutFrames.displayFrame);
         assertInsetBy(WATERFALL_INSETS.left, STATUS_BAR_HEIGHT, WATERFALL_INSETS.right, 0,
-                mParentFrame);
+                mOutFrames.parentFrame);
         assertInsetBy(WATERFALL_INSETS.left, STATUS_BAR_HEIGHT, WATERFALL_INSETS.right, 0,
-                mFrame);
+                mOutFrames.frame);
     }
 
     @Test
@@ -371,9 +373,9 @@
         mAttrs.setFitInsetsTypes(0);
         computeFrames();
 
-        assertInsetBy(WATERFALL_INSETS.left, 0, WATERFALL_INSETS.right, 0, mDisplayFrame);
-        assertInsetBy(WATERFALL_INSETS.left, 0, WATERFALL_INSETS.right, 0, mParentFrame);
-        assertInsetBy(WATERFALL_INSETS.left, 0, WATERFALL_INSETS.right, 0, mFrame);
+        assertInsetBy(WATERFALL_INSETS.left, 0, WATERFALL_INSETS.right, 0, mOutFrames.displayFrame);
+        assertInsetBy(WATERFALL_INSETS.left, 0, WATERFALL_INSETS.right, 0, mOutFrames.parentFrame);
+        assertInsetBy(WATERFALL_INSETS.left, 0, WATERFALL_INSETS.right, 0, mOutFrames.frame);
     }
 
     @Test
@@ -384,11 +386,11 @@
         computeFrames();
 
         assertInsetBy(WATERFALL_INSETS.left, STATUS_BAR_HEIGHT, WATERFALL_INSETS.right, 0,
-                mDisplayFrame);
+                mOutFrames.displayFrame);
         assertInsetBy(WATERFALL_INSETS.left, STATUS_BAR_HEIGHT, WATERFALL_INSETS.right, 0,
-                mParentFrame);
+                mOutFrames.parentFrame);
         assertInsetBy(WATERFALL_INSETS.left, STATUS_BAR_HEIGHT, WATERFALL_INSETS.right, 0,
-                mFrame);
+                mOutFrames.frame);
     }
 
     @Test
@@ -398,8 +400,8 @@
         mAttrs.setFitInsetsTypes(0);
         computeFrames();
 
-        assertInsetByTopBottom(0, 0, mDisplayFrame);
-        assertInsetByTopBottom(0, 0, mParentFrame);
-        assertInsetByTopBottom(0, 0, mFrame);
+        assertInsetByTopBottom(0, 0, mOutFrames.displayFrame);
+        assertInsetByTopBottom(0, 0, mOutFrames.parentFrame);
+        assertInsetByTopBottom(0, 0, mOutFrames.frame);
     }
 }
diff --git a/services/tests/wmtests/src/com/android/server/wm/WindowManagerServiceTests.java b/services/tests/wmtests/src/com/android/server/wm/WindowManagerServiceTests.java
index 68e90e1..08320f8 100644
--- a/services/tests/wmtests/src/com/android/server/wm/WindowManagerServiceTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/WindowManagerServiceTests.java
@@ -161,7 +161,7 @@
     @Test
     public void testDismissKeyguardCanWakeUp() {
         doReturn(true).when(mWm).checkCallingPermission(anyString(), anyString());
-        doReturn(true).when(mWm.mAtmService).isDreaming();
+        doReturn(true).when(mWm.mAtmService.mKeyguardController).isShowingDream();
         doNothing().when(mWm.mAtmService.mTaskSupervisor).wakeUp(anyString());
         mWm.dismissKeyguard(null, "test-dismiss-keyguard");
         verify(mWm.mAtmService.mTaskSupervisor).wakeUp(anyString());
diff --git a/services/tests/wmtests/src/com/android/server/wm/WindowStateTests.java b/services/tests/wmtests/src/com/android/server/wm/WindowStateTests.java
index e4f691b..6a3aa78 100644
--- a/services/tests/wmtests/src/com/android/server/wm/WindowStateTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/WindowStateTests.java
@@ -504,7 +504,7 @@
         assertTrue(win.useBLASTSync());
         final SurfaceControl.Transaction drawT = new StubTransaction();
         win.prepareDrawHandlers();
-        assertTrue(win.finishDrawing(drawT));
+        assertTrue(win.finishDrawing(drawT, Integer.MAX_VALUE));
         assertEquals(drawT, handledT[0]);
         assertFalse(win.useBLASTSync());
 
@@ -693,7 +693,7 @@
             doThrow(new RemoteException("test")).when(win.mClient).resized(any() /* frames */,
                     anyBoolean() /* reportDraw */, any() /* mergedConfig */,
                     anyBoolean() /* forceLayout */, anyBoolean() /* alwaysConsumeSystemBars */,
-                    anyInt() /* displayId */);
+                    anyInt() /* displayId */, anyInt() /* seqId */);
         } catch (RemoteException ignored) {
         }
         win.reportResized();
@@ -820,7 +820,6 @@
     @Test
     public void testHasActiveVisibleWindow() {
         final int uid = ActivityBuilder.DEFAULT_FAKE_UID;
-        mAtm.mActiveUids.onUidActive(uid, 0 /* any proc state */);
 
         final WindowState app = createWindow(null, TYPE_APPLICATION, "app", uid);
         app.mActivityRecord.setVisible(false);
@@ -848,6 +847,11 @@
         // Make the application overlay window visible. It should be a valid active visible window.
         overlay.onSurfaceShownChanged(true);
         assertTrue(mAtm.hasActiveVisibleWindow(uid));
+
+        // The number of windows should be independent of the existence of uid state.
+        mAtm.mActiveUids.onUidInactive(uid);
+        mAtm.mActiveUids.onUidActive(uid, 0 /* any proc state */);
+        assertTrue(mAtm.mActiveUids.hasNonAppVisibleWindow(uid));
     }
 
     @UseTestDisplay(addWindows = W_ACTIVITY)
@@ -945,6 +949,46 @@
         assertFalse(app2.getInsetsState().getSource(ITYPE_IME).isVisible());
     }
 
+    @Test
+    public void testAdjustImeInsetsVisibilityWhenSwitchingApps_toAppInMultiWindowMode() {
+        final WindowState app = createWindow(null, TYPE_APPLICATION, "app");
+        final WindowState app2 = createWindow(null, WINDOWING_MODE_MULTI_WINDOW,
+                ACTIVITY_TYPE_STANDARD, TYPE_APPLICATION, mDisplayContent, "app2");
+        final WindowState imeWindow = createWindow(null, TYPE_APPLICATION, "imeWindow");
+        spyOn(imeWindow);
+        doReturn(true).when(imeWindow).isVisible();
+        mDisplayContent.mInputMethodWindow = imeWindow;
+
+        final InsetsStateController controller = mDisplayContent.getInsetsStateController();
+        controller.getImeSourceProvider().setWindowContainer(imeWindow, null, null);
+
+        // Simulate app2 in multi-window mode is going to background to switch to the fullscreen
+        // app which requests IME with updating all windows Insets State when IME is above app.
+        app2.mActivityRecord.mImeInsetsFrozenUntilStartInput = true;
+        mDisplayContent.setImeLayeringTarget(app);
+        mDisplayContent.setImeInputTarget(app);
+        assertTrue(mDisplayContent.shouldImeAttachedToApp());
+        controller.getImeSourceProvider().scheduleShowImePostLayout(app);
+        controller.getImeSourceProvider().getSource().setVisible(true);
+        controller.updateAboveInsetsState(false);
+
+        // Expect app windows behind IME can receive IME insets visible,
+        // but not for app2 in background.
+        assertTrue(app.getInsetsState().getSource(ITYPE_IME).isVisible());
+        assertFalse(app2.getInsetsState().getSource(ITYPE_IME).isVisible());
+
+        // Simulate app plays closing transition to app2.
+        // And app2 is now IME layering target but not yet to be the IME input target.
+        mDisplayContent.setImeLayeringTarget(app2);
+        app.mActivityRecord.commitVisibility(false, false);
+        assertTrue(app.mActivityRecord.mLastImeShown);
+        assertTrue(app.mActivityRecord.mImeInsetsFrozenUntilStartInput);
+
+        // Verify the IME insets is still visible on app, but not for app2 during task switching.
+        assertTrue(app.getInsetsState().getSource(ITYPE_IME).isVisible());
+        assertFalse(app2.getInsetsState().getSource(ITYPE_IME).isVisible());
+    }
+
     @UseTestDisplay(addWindows = {W_ACTIVITY})
     @Test
     public void testUpdateImeControlTargetWhenLeavingMultiWindow() {
diff --git a/services/voiceinteraction/java/com/android/server/voiceinteraction/HotwordDetectionConnection.java b/services/voiceinteraction/java/com/android/server/voiceinteraction/HotwordDetectionConnection.java
index a597fc6..3988892 100644
--- a/services/voiceinteraction/java/com/android/server/voiceinteraction/HotwordDetectionConnection.java
+++ b/services/voiceinteraction/java/com/android/server/voiceinteraction/HotwordDetectionConnection.java
@@ -23,6 +23,25 @@
 import static android.service.voice.HotwordDetectionService.INITIALIZATION_STATUS_UNKNOWN;
 import static android.service.voice.HotwordDetectionService.KEY_INITIALIZATION_STATUS;
 
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTION_SERVICE_INIT_RESULT_REPORTED__RESULT__CALLBACK_INIT_STATE_ERROR;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTION_SERVICE_INIT_RESULT_REPORTED__RESULT__CALLBACK_INIT_STATE_SUCCESS;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTION_SERVICE_INIT_RESULT_REPORTED__RESULT__CALLBACK_INIT_STATE_UNKNOWN_NO_VALUE;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTION_SERVICE_INIT_RESULT_REPORTED__RESULT__CALLBACK_INIT_STATE_UNKNOWN_OVER_MAX_CUSTOM_VALUE;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTION_SERVICE_INIT_RESULT_REPORTED__RESULT__CALLBACK_INIT_STATE_UNKNOWN_TIMEOUT;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTION_SERVICE_RESTARTED__REASON__AUDIO_SERVICE_DIED;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTION_SERVICE_RESTARTED__REASON__SCHEDULE;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTOR_EVENTS__EVENT__CALLBACK_UPDATE_STATE_AFTER_TIMEOUT;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTOR_EVENTS__EVENT__ON_CONNECTED;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTOR_EVENTS__EVENT__REQUEST_BIND_SERVICE;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTOR_EVENTS__EVENT__REQUEST_BIND_SERVICE_FAIL;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTOR_EVENTS__EVENT__REQUEST_UPDATE_STATE;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__DETECTOR_TYPE__NORMAL_DETECTOR;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__DETECTOR_TYPE__TRUSTED_DETECTOR_DSP;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__RESULT__DETECTED;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__RESULT__DETECT_EXCEPTION;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__RESULT__DETECT_TIMEOUT;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__RESULT__KEYPHRASE_TRIGGER;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__RESULT__REJECTED;
 import static com.android.server.voiceinteraction.SoundTriggerSessionPermissionsDecorator.enforcePermissionForPreflight;
 
 import android.annotation.NonNull;
@@ -48,6 +67,7 @@
 import android.os.RemoteException;
 import android.os.ServiceManager;
 import android.os.SharedMemory;
+import android.provider.DeviceConfig;
 import android.service.voice.HotwordDetectedResult;
 import android.service.voice.HotwordDetectionService;
 import android.service.voice.HotwordDetector;
@@ -91,33 +111,56 @@
     private static final String TAG = "HotwordDetectionConnection";
     static final boolean DEBUG = false;
 
+    private static final String KEY_RESTART_PERIOD_IN_SECONDS = "restart_period_in_seconds";
     // TODO: These constants need to be refined.
-    private static final long VALIDATION_TIMEOUT_MILLIS = 3000;
+    private static final long VALIDATION_TIMEOUT_MILLIS = 4000;
     private static final long MAX_UPDATE_TIMEOUT_MILLIS = 6000;
     private static final Duration MAX_UPDATE_TIMEOUT_DURATION =
             Duration.ofMillis(MAX_UPDATE_TIMEOUT_MILLIS);
     private static final long RESET_DEBUG_HOTWORD_LOGGING_TIMEOUT_MILLIS = 60 * 60 * 1000; // 1 hour
+    /**
+     * Time after which each HotwordDetectionService process is stopped and replaced by a new one.
+     * 0 indicates no restarts.
+     */
+    private static final int RESTART_PERIOD_SECONDS =
+            DeviceConfig.getInt(DeviceConfig.NAMESPACE_VOICE_INTERACTION,
+                    KEY_RESTART_PERIOD_IN_SECONDS, 0);
     private static final int MAX_ISOLATED_PROCESS_NUMBER = 10;
 
+    // Hotword metrics
+    private static final int METRICS_INIT_UNKNOWN_TIMEOUT =
+            HOTWORD_DETECTION_SERVICE_INIT_RESULT_REPORTED__RESULT__CALLBACK_INIT_STATE_UNKNOWN_TIMEOUT;
+    private static final int METRICS_INIT_UNKNOWN_NO_VALUE =
+            HOTWORD_DETECTION_SERVICE_INIT_RESULT_REPORTED__RESULT__CALLBACK_INIT_STATE_UNKNOWN_NO_VALUE;
+    private static final int METRICS_INIT_UNKNOWN_OVER_MAX_CUSTOM_VALUE =
+            HOTWORD_DETECTION_SERVICE_INIT_RESULT_REPORTED__RESULT__CALLBACK_INIT_STATE_UNKNOWN_OVER_MAX_CUSTOM_VALUE;
+    private static final int METRICS_INIT_CALLBACK_STATE_ERROR =
+            HOTWORD_DETECTION_SERVICE_INIT_RESULT_REPORTED__RESULT__CALLBACK_INIT_STATE_ERROR;
+    private static final int METRICS_INIT_CALLBACK_STATE_SUCCESS =
+            HOTWORD_DETECTION_SERVICE_INIT_RESULT_REPORTED__RESULT__CALLBACK_INIT_STATE_SUCCESS;
+
     private final Executor mAudioCopyExecutor = Executors.newCachedThreadPool();
     // TODO: This may need to be a Handler(looper)
     private final ScheduledExecutorService mScheduledExecutorService =
             Executors.newSingleThreadScheduledExecutor();
+    @Nullable private final ScheduledFuture<?> mCancellationTaskFuture;
     private final AtomicBoolean mUpdateStateAfterStartFinished = new AtomicBoolean(false);
     private final IBinder.DeathRecipient mAudioServerDeathRecipient = this::audioServerDied;
     private final @NonNull ServiceConnectionFactory mServiceConnectionFactory;
     private final IHotwordRecognitionStatusCallback mCallback;
+    private final int mDetectorType;
 
     final Object mLock;
     final int mVoiceInteractionServiceUid;
     final ComponentName mDetectionComponentName;
     final int mUser;
     final Context mContext;
+
     volatile HotwordDetectionServiceIdentity mIdentity;
     private IMicrophoneHotwordDetectionVoiceInteractionCallback mSoftwareCallback;
     private Instant mLastRestartInstant;
 
-    private ScheduledFuture<?> mCancellationTaskFuture;
+    private ScheduledFuture<?> mCancellationKeyPhraseDetectionFuture;
     private ScheduledFuture<?> mDebugHotwordLoggingTimeoutFuture = null;
 
     /** Identity used for attributing app ops when delivering data to the Interactor. */
@@ -133,7 +176,6 @@
     private @NonNull ServiceConnection mRemoteHotwordDetectionService;
     private IBinder mAudioFlinger;
     private boolean mDebugHotwordLogging = false;
-    private final int mDetectorType;
 
     HotwordDetectionConnection(Object lock, Context context, int voiceInteractionServiceUid,
             Identity voiceInteractorIdentity, ComponentName serviceName, int userId,
@@ -163,14 +205,20 @@
         mLastRestartInstant = Instant.now();
         updateStateAfterProcessStart(options, sharedMemory);
 
-        // TODO(volnov): we need to be smarter here, e.g. schedule it a bit more often, but wait
-        // until the current session is closed.
-        mCancellationTaskFuture = mScheduledExecutorService.scheduleAtFixedRate(() -> {
-            Slog.v(TAG, "Time to restart the process, TTL has passed");
-            synchronized (mLock) {
-                restartProcessLocked();
-            }
-        }, 30, 30, TimeUnit.MINUTES);
+        if (RESTART_PERIOD_SECONDS <= 0) {
+            mCancellationTaskFuture = null;
+        } else {
+            // TODO(volnov): we need to be smarter here, e.g. schedule it a bit more often, but wait
+            // until the current session is closed.
+            mCancellationTaskFuture = mScheduledExecutorService.scheduleAtFixedRate(() -> {
+                Slog.v(TAG, "Time to restart the process, TTL has passed");
+                synchronized (mLock) {
+                    restartProcessLocked();
+                    HotwordMetricsLogger.writeServiceRestartEvent(mDetectorType,
+                            HOTWORD_DETECTION_SERVICE_RESTARTED__REASON__SCHEDULE);
+                }
+            }, RESTART_PERIOD_SECONDS, RESTART_PERIOD_SECONDS, TimeUnit.SECONDS);
+        }
     }
 
     private void initAudioFlingerLocked() {
@@ -201,6 +249,8 @@
             // We restart the process instead of simply sending over the new binder, to avoid race
             // conditions with audio reading in the service.
             restartProcessLocked();
+            HotwordMetricsLogger.writeServiceRestartEvent(mDetectorType,
+                    HOTWORD_DETECTION_SERVICE_RESTARTED__REASON__AUDIO_SERVICE_DIED);
         }
     }
 
@@ -220,26 +270,32 @@
                     future.complete(null);
                     if (mUpdateStateAfterStartFinished.getAndSet(true)) {
                         Slog.w(TAG, "call callback after timeout");
+                        HotwordMetricsLogger.writeDetectorEvent(mDetectorType,
+                                HOTWORD_DETECTOR_EVENTS__EVENT__CALLBACK_UPDATE_STATE_AFTER_TIMEOUT,
+                                mVoiceInteractionServiceUid);
                         return;
                     }
-                    int status = bundle != null ? bundle.getInt(
-                            KEY_INITIALIZATION_STATUS,
-                            INITIALIZATION_STATUS_UNKNOWN)
-                            : INITIALIZATION_STATUS_UNKNOWN;
-                    // Add the protection to avoid unexpected status
-                    if (status > HotwordDetectionService.getMaxCustomInitializationStatus()
-                            && status != INITIALIZATION_STATUS_UNKNOWN) {
-                        status = INITIALIZATION_STATUS_UNKNOWN;
-                    }
+                    Pair<Integer, Integer> statusResultPair = getInitStatusAndMetricsResult(bundle);
+                    int status = statusResultPair.first;
+                    int initResultMetricsResult = statusResultPair.second;
                     try {
                         mCallback.onStatusReported(status);
+                        HotwordMetricsLogger.writeServiceInitResultEvent(mDetectorType,
+                                initResultMetricsResult);
                     } catch (RemoteException e) {
+                        // TODO: Add a new atom for RemoteException case, the error doesn't very
+                        // correct here
                         Slog.w(TAG, "Failed to report initialization status: " + e);
+                        HotwordMetricsLogger.writeServiceInitResultEvent(mDetectorType,
+                                METRICS_INIT_CALLBACK_STATE_ERROR);
                     }
                 }
             };
             try {
                 service.updateState(options, sharedMemory, statusCallback);
+                HotwordMetricsLogger.writeDetectorEvent(mDetectorType,
+                        HOTWORD_DETECTOR_EVENTS__EVENT__REQUEST_UPDATE_STATE,
+                        mVoiceInteractionServiceUid);
             } catch (RemoteException e) {
                 // TODO: (b/181842909) Report an error to voice interactor
                 Slog.w(TAG, "Failed to updateState for HotwordDetectionService", e);
@@ -254,8 +310,12 @@
                         }
                         try {
                             mCallback.onStatusReported(INITIALIZATION_STATUS_UNKNOWN);
+                            HotwordMetricsLogger.writeServiceInitResultEvent(mDetectorType,
+                                    METRICS_INIT_UNKNOWN_TIMEOUT);
                         } catch (RemoteException e) {
                             Slog.w(TAG, "Failed to report initialization status UNKNOWN", e);
+                            HotwordMetricsLogger.writeServiceInitResultEvent(mDetectorType,
+                                    METRICS_INIT_CALLBACK_STATE_ERROR);
                         }
                     } else if (err != null) {
                         Slog.w(TAG, "Failed to update state: " + err);
@@ -265,6 +325,23 @@
                 });
     }
 
+    private static Pair<Integer, Integer> getInitStatusAndMetricsResult(Bundle bundle) {
+        if (bundle == null) {
+            return new Pair<>(INITIALIZATION_STATUS_UNKNOWN, METRICS_INIT_UNKNOWN_NO_VALUE);
+        }
+        int status = bundle.getInt(KEY_INITIALIZATION_STATUS, INITIALIZATION_STATUS_UNKNOWN);
+        if (status > HotwordDetectionService.getMaxCustomInitializationStatus()
+                && status != INITIALIZATION_STATUS_UNKNOWN) {
+            return new Pair<>(INITIALIZATION_STATUS_UNKNOWN,
+                    METRICS_INIT_UNKNOWN_OVER_MAX_CUSTOM_VALUE);
+        }
+        // TODO: should guard against negative here
+        int metricsResult = status == INITIALIZATION_STATUS_UNKNOWN
+                ? METRICS_INIT_CALLBACK_STATE_ERROR
+                : METRICS_INIT_CALLBACK_STATE_SUCCESS;
+        return new Pair<>(status, metricsResult);
+    }
+
     private boolean isBound() {
         synchronized (mLock) {
             return mRemoteHotwordDetectionService.isBound();
@@ -282,7 +359,9 @@
             removeServiceUidForAudioPolicy(mIdentity.getIsolatedUid());
         }
         mIdentity = null;
-        mCancellationTaskFuture.cancel(/* may interrupt */ true);
+        if (mCancellationTaskFuture != null) {
+            mCancellationTaskFuture.cancel(/* may interrupt */ true);
+        }
         if (mAudioFlinger != null) {
             mAudioFlinger.unlinkToDeath(mAudioServerDeathRecipient, /* flags= */ 0);
         }
@@ -481,12 +560,31 @@
                     Slog.d(TAG, "onDetected");
                 }
                 synchronized (mLock) {
+                    // TODO: If the dsp trigger comes in after the timeout, we will log both events.
+                    // Because we don't enforce the timeout yet. We should add some synchronizations
+                    // within the runnable to prevent the race condition to log both events.
+                    if (mCancellationKeyPhraseDetectionFuture != null) {
+                        mCancellationKeyPhraseDetectionFuture.cancel(true);
+                    }
+                    HotwordMetricsLogger.writeKeyphraseTriggerEvent(
+                            mDetectorType,
+                            HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__RESULT__DETECTED);
                     if (!mValidatingDspTrigger) {
                         Slog.i(TAG, "Ignoring #onDetected due to a process restart");
+                        HotwordMetricsLogger.writeKeyphraseTriggerEvent(
+                                mDetectorType,
+                                HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__RESULT__DETECT_EXCEPTION);
                         return;
                     }
                     mValidatingDspTrigger = false;
-                    enforcePermissionsForDataDelivery();
+                    try {
+                        enforcePermissionsForDataDelivery();
+                    } catch (SecurityException e) {
+                        HotwordMetricsLogger.writeKeyphraseTriggerEvent(
+                                mDetectorType,
+                                HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__RESULT__DETECT_EXCEPTION);
+                        throw e;
+                    }
                     externalCallback.onKeyphraseDetected(recognitionEvent, result);
                     if (result != null) {
                         Slog.i(TAG, "Egressed " + HotwordDetectedResult.getUsageSize(result)
@@ -504,8 +602,17 @@
                     Slog.d(TAG, "onRejected");
                 }
                 synchronized (mLock) {
+                    if (mCancellationKeyPhraseDetectionFuture != null) {
+                        mCancellationKeyPhraseDetectionFuture.cancel(true);
+                    }
+                    HotwordMetricsLogger.writeKeyphraseTriggerEvent(
+                            mDetectorType,
+                            HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__RESULT__REJECTED);
                     if (!mValidatingDspTrigger) {
                         Slog.i(TAG, "Ignoring #onRejected due to a process restart");
+                        HotwordMetricsLogger.writeKeyphraseTriggerEvent(
+                                mDetectorType,
+                                HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__RESULT__DETECT_EXCEPTION);
                         return;
                     }
                     mValidatingDspTrigger = false;
@@ -520,11 +627,20 @@
         synchronized (mLock) {
             mValidatingDspTrigger = true;
             mRemoteHotwordDetectionService.run(
-                    service -> service.detectFromDspSource(
-                            recognitionEvent,
-                            recognitionEvent.getCaptureFormat(),
-                            VALIDATION_TIMEOUT_MILLIS,
-                            internalCallback));
+                    service -> {
+                        // TODO: avoid allocate every time
+                        mCancellationKeyPhraseDetectionFuture = mScheduledExecutorService.schedule(
+                                () -> HotwordMetricsLogger
+                                        .writeKeyphraseTriggerEvent(mDetectorType,
+                                        HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__RESULT__DETECT_TIMEOUT),
+                                VALIDATION_TIMEOUT_MILLIS,
+                                TimeUnit.MILLISECONDS);
+                        service.detectFromDspSource(
+                                recognitionEvent,
+                                recognitionEvent.getCaptureFormat(),
+                                VALIDATION_TIMEOUT_MILLIS,
+                                internalCallback);
+                    });
         }
     }
 
@@ -625,10 +741,16 @@
             }
             final boolean useHotwordDetectionService = mHotwordDetectionConnection != null;
             if (useHotwordDetectionService) {
+                HotwordMetricsLogger.writeKeyphraseTriggerEvent(
+                        HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__DETECTOR_TYPE__TRUSTED_DETECTOR_DSP,
+                        HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__RESULT__KEYPHRASE_TRIGGER);
                 mRecognitionEvent = recognitionEvent;
                 mHotwordDetectionConnection.detectFromDspSource(
                         recognitionEvent, mExternalCallback);
             } else {
+                HotwordMetricsLogger.writeKeyphraseTriggerEvent(
+                        HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__DETECTOR_TYPE__NORMAL_DETECTOR,
+                        HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__RESULT__KEYPHRASE_TRIGGER);
                 mExternalCallback.onKeyphraseDetected(recognitionEvent, null);
             }
         }
@@ -657,6 +779,7 @@
     }
 
     public void dump(String prefix, PrintWriter pw) {
+        pw.print(prefix); pw.print("RESTART_PERIOD_SECONDS="); pw.println(RESTART_PERIOD_SECONDS);
         pw.print(prefix);
         pw.print("mBound=" + mRemoteHotwordDetectionService.isBound());
         pw.print(", mValidatingDspTrigger=" + mValidatingDspTrigger);
@@ -793,6 +916,7 @@
 
         private boolean mRespectServiceConnectionStatusChanged = true;
         private boolean mIsBound = false;
+        private boolean mIsLoggedFirstConnect = false;
 
         ServiceConnection(@NonNull Context context,
                 @NonNull Intent intent, int bindingFlags, int userId,
@@ -816,6 +940,12 @@
                     return;
                 }
                 mIsBound = connected;
+                if (connected && !mIsLoggedFirstConnect) {
+                    mIsLoggedFirstConnect = true;
+                    HotwordMetricsLogger.writeDetectorEvent(mDetectorType,
+                            HOTWORD_DETECTOR_EVENTS__EVENT__ON_CONNECTED,
+                            mVoiceInteractionServiceUid);
+                }
             }
         }
 
@@ -846,13 +976,25 @@
         protected boolean bindService(
                 @NonNull android.content.ServiceConnection serviceConnection) {
             try {
-                return mContext.bindIsolatedService(
+                HotwordMetricsLogger.writeDetectorEvent(mDetectorType,
+                        HOTWORD_DETECTOR_EVENTS__EVENT__REQUEST_BIND_SERVICE,
+                        mVoiceInteractionServiceUid);
+                boolean bindResult = mContext.bindIsolatedService(
                         mIntent,
                         Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE | mBindingFlags,
                         "hotword_detector_" + mInstanceNumber,
                         mExecutor,
                         serviceConnection);
+                if (!bindResult) {
+                    HotwordMetricsLogger.writeDetectorEvent(mDetectorType,
+                            HOTWORD_DETECTOR_EVENTS__EVENT__REQUEST_BIND_SERVICE_FAIL,
+                            mVoiceInteractionServiceUid);
+                }
+                return bindResult;
             } catch (IllegalArgumentException e) {
+                HotwordMetricsLogger.writeDetectorEvent(mDetectorType,
+                        HOTWORD_DETECTOR_EVENTS__EVENT__REQUEST_BIND_SERVICE_FAIL,
+                        mVoiceInteractionServiceUid);
                 Slog.wtf(TAG, "Can't bind to the hotword detection service!", e);
                 return false;
             }
diff --git a/services/voiceinteraction/java/com/android/server/voiceinteraction/HotwordMetricsLogger.java b/services/voiceinteraction/java/com/android/server/voiceinteraction/HotwordMetricsLogger.java
index de0b960..940aed3 100644
--- a/services/voiceinteraction/java/com/android/server/voiceinteraction/HotwordMetricsLogger.java
+++ b/services/voiceinteraction/java/com/android/server/voiceinteraction/HotwordMetricsLogger.java
@@ -16,6 +16,24 @@
 
 package com.android.server.voiceinteraction;
 
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTION_SERVICE_INIT_RESULT_REPORTED__DETECTOR_TYPE__NORMAL_DETECTOR;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTION_SERVICE_INIT_RESULT_REPORTED__DETECTOR_TYPE__TRUSTED_DETECTOR_DSP;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTION_SERVICE_INIT_RESULT_REPORTED__DETECTOR_TYPE__TRUSTED_DETECTOR_SOFTWARE;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTION_SERVICE_RESTARTED__DETECTOR_TYPE__NORMAL_DETECTOR;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTION_SERVICE_RESTARTED__DETECTOR_TYPE__TRUSTED_DETECTOR_DSP;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTION_SERVICE_RESTARTED__DETECTOR_TYPE__TRUSTED_DETECTOR_SOFTWARE;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTOR_CREATE_REQUESTED__DETECTOR_TYPE__NORMAL_DETECTOR;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTOR_CREATE_REQUESTED__DETECTOR_TYPE__TRUSTED_DETECTOR_DSP;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTOR_CREATE_REQUESTED__DETECTOR_TYPE__TRUSTED_DETECTOR_SOFTWARE;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTOR_EVENTS__DETECTOR_TYPE__NORMAL_DETECTOR;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTOR_EVENTS__DETECTOR_TYPE__TRUSTED_DETECTOR_DSP;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTOR_EVENTS__DETECTOR_TYPE__TRUSTED_DETECTOR_SOFTWARE;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__DETECTOR_TYPE__NORMAL_DETECTOR;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__DETECTOR_TYPE__TRUSTED_DETECTOR_DSP;
+import static com.android.internal.util.FrameworkStatsLog.HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__DETECTOR_TYPE__TRUSTED_DETECTOR_SOFTWARE;
+
+import android.service.voice.HotwordDetector;
+
 import com.android.internal.util.FrameworkStatsLog;
 
 /**
@@ -23,6 +41,13 @@
  */
 public final class HotwordMetricsLogger {
 
+    private static final int METRICS_INIT_DETECTOR_SOFTWARE =
+            HOTWORD_DETECTION_SERVICE_INIT_RESULT_REPORTED__DETECTOR_TYPE__TRUSTED_DETECTOR_SOFTWARE;
+    private static final int METRICS_INIT_DETECTOR_DSP =
+            HOTWORD_DETECTION_SERVICE_INIT_RESULT_REPORTED__DETECTOR_TYPE__TRUSTED_DETECTOR_DSP;
+    private static final int METRICS_INIT_NORMAL_DETECTOR =
+            HOTWORD_DETECTION_SERVICE_INIT_RESULT_REPORTED__DETECTOR_TYPE__NORMAL_DETECTOR;
+
     private HotwordMetricsLogger() {
         // Class only contains static utility functions, and should not be instantiated
     }
@@ -31,39 +56,99 @@
      * Logs information related to create hotword detector.
      */
     public static void writeDetectorCreateEvent(int detectorType, boolean isCreated, int uid) {
-        FrameworkStatsLog.write(FrameworkStatsLog.HOTWORD_DETECTOR_CREATE_REQUESTED, detectorType,
-                isCreated, uid);
+        int metricsDetectorType = getCreateMetricsDetectorType(detectorType);
+        FrameworkStatsLog.write(FrameworkStatsLog.HOTWORD_DETECTOR_CREATE_REQUESTED,
+                metricsDetectorType, isCreated, uid);
     }
 
     /**
      * Logs information related to hotword detection service init result.
      */
     public static void writeServiceInitResultEvent(int detectorType, int result) {
+        int metricsDetectorType = getInitMetricsDetectorType(detectorType);
         FrameworkStatsLog.write(FrameworkStatsLog.HOTWORD_DETECTION_SERVICE_INIT_RESULT_REPORTED,
-                detectorType, result);
+                metricsDetectorType, result);
     }
 
     /**
      * Logs information related to hotword detection service restarting.
      */
     public static void writeServiceRestartEvent(int detectorType, int reason) {
+        int metricsDetectorType = getRestartMetricsDetectorType(detectorType);
         FrameworkStatsLog.write(FrameworkStatsLog.HOTWORD_DETECTION_SERVICE_RESTARTED,
-                detectorType, reason);
+                metricsDetectorType, reason);
     }
 
     /**
      * Logs information related to keyphrase trigger.
      */
     public static void writeKeyphraseTriggerEvent(int detectorType, int result) {
+        int metricsDetectorType = getKeyphraseMetricsDetectorType(detectorType);
         FrameworkStatsLog.write(FrameworkStatsLog.HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED,
-                detectorType, result);
+                metricsDetectorType, result);
     }
 
     /**
      * Logs information related to hotword detector events.
      */
     public static void writeDetectorEvent(int detectorType, int event, int uid) {
+        int metricsDetectorType = getDetectorMetricsDetectorType(detectorType);
         FrameworkStatsLog.write(FrameworkStatsLog.HOTWORD_DETECTOR_EVENTS,
-                detectorType, event, uid);
+                metricsDetectorType, event, uid);
+    }
+
+    private static int getCreateMetricsDetectorType(int detectorType) {
+        switch (detectorType) {
+            case HotwordDetector.DETECTOR_TYPE_TRUSTED_HOTWORD_SOFTWARE:
+                return HOTWORD_DETECTOR_CREATE_REQUESTED__DETECTOR_TYPE__TRUSTED_DETECTOR_SOFTWARE;
+            case HotwordDetector.DETECTOR_TYPE_TRUSTED_HOTWORD_DSP:
+                return HOTWORD_DETECTOR_CREATE_REQUESTED__DETECTOR_TYPE__TRUSTED_DETECTOR_DSP;
+            default:
+                return HOTWORD_DETECTOR_CREATE_REQUESTED__DETECTOR_TYPE__NORMAL_DETECTOR;
+        }
+    }
+
+    private static int getRestartMetricsDetectorType(int detectorType) {
+        switch (detectorType) {
+            case HotwordDetector.DETECTOR_TYPE_TRUSTED_HOTWORD_SOFTWARE:
+                return HOTWORD_DETECTION_SERVICE_RESTARTED__DETECTOR_TYPE__TRUSTED_DETECTOR_SOFTWARE;
+            case HotwordDetector.DETECTOR_TYPE_TRUSTED_HOTWORD_DSP:
+                return HOTWORD_DETECTION_SERVICE_RESTARTED__DETECTOR_TYPE__TRUSTED_DETECTOR_DSP;
+            default:
+                return HOTWORD_DETECTION_SERVICE_RESTARTED__DETECTOR_TYPE__NORMAL_DETECTOR;
+        }
+    }
+
+    private static int getInitMetricsDetectorType(int detectorType) {
+        switch (detectorType) {
+            case HotwordDetector.DETECTOR_TYPE_TRUSTED_HOTWORD_SOFTWARE:
+                return METRICS_INIT_DETECTOR_SOFTWARE;
+            case HotwordDetector.DETECTOR_TYPE_TRUSTED_HOTWORD_DSP:
+                return METRICS_INIT_DETECTOR_DSP;
+            default:
+                return METRICS_INIT_NORMAL_DETECTOR;
+        }
+    }
+
+    private static int getKeyphraseMetricsDetectorType(int detectorType) {
+        switch (detectorType) {
+            case HotwordDetector.DETECTOR_TYPE_TRUSTED_HOTWORD_SOFTWARE:
+                return HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__DETECTOR_TYPE__TRUSTED_DETECTOR_SOFTWARE;
+            case HotwordDetector.DETECTOR_TYPE_TRUSTED_HOTWORD_DSP:
+                return HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__DETECTOR_TYPE__TRUSTED_DETECTOR_DSP;
+            default:
+                return HOTWORD_DETECTOR_KEYPHRASE_TRIGGERED__DETECTOR_TYPE__NORMAL_DETECTOR;
+        }
+    }
+
+    private static int getDetectorMetricsDetectorType(int detectorType) {
+        switch (detectorType) {
+            case HotwordDetector.DETECTOR_TYPE_TRUSTED_HOTWORD_SOFTWARE:
+                return HOTWORD_DETECTOR_EVENTS__DETECTOR_TYPE__TRUSTED_DETECTOR_SOFTWARE;
+            case HotwordDetector.DETECTOR_TYPE_TRUSTED_HOTWORD_DSP:
+                return HOTWORD_DETECTOR_EVENTS__DETECTOR_TYPE__TRUSTED_DETECTOR_DSP;
+            default:
+                return HOTWORD_DETECTOR_EVENTS__DETECTOR_TYPE__NORMAL_DETECTOR;
+        }
     }
 }
diff --git a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerServiceImpl.java b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerServiceImpl.java
index fb4d73c..0519873 100644
--- a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerServiceImpl.java
+++ b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerServiceImpl.java
@@ -462,24 +462,33 @@
             IHotwordRecognitionStatusCallback callback,
             int detectorType) {
         Slog.v(TAG, "updateStateLocked");
+        int voiceInteractionServiceUid = mInfo.getServiceInfo().applicationInfo.uid;
         if (mHotwordDetectionComponentName == null) {
             Slog.w(TAG, "Hotword detection service name not found");
+            logDetectorCreateEventIfNeeded(callback, detectorType, false,
+                    voiceInteractionServiceUid);
             throw new IllegalStateException("Hotword detection service name not found");
         }
         ServiceInfo hotwordDetectionServiceInfo = getServiceInfoLocked(
                 mHotwordDetectionComponentName, mUser);
         if (hotwordDetectionServiceInfo == null) {
             Slog.w(TAG, "Hotword detection service info not found");
+            logDetectorCreateEventIfNeeded(callback, detectorType, false,
+                    voiceInteractionServiceUid);
             throw new IllegalStateException("Hotword detection service info not found");
         }
         if (!isIsolatedProcessLocked(hotwordDetectionServiceInfo)) {
             Slog.w(TAG, "Hotword detection service not in isolated process");
+            logDetectorCreateEventIfNeeded(callback, detectorType, false,
+                    voiceInteractionServiceUid);
             throw new IllegalStateException("Hotword detection service not in isolated process");
         }
         if (!Manifest.permission.BIND_HOTWORD_DETECTION_SERVICE.equals(
                 hotwordDetectionServiceInfo.permission)) {
             Slog.w(TAG, "Hotword detection service does not require permission "
                     + Manifest.permission.BIND_HOTWORD_DETECTION_SERVICE);
+            logDetectorCreateEventIfNeeded(callback, detectorType, false,
+                    voiceInteractionServiceUid);
             throw new SecurityException("Hotword detection service does not require permission "
                     + Manifest.permission.BIND_HOTWORD_DETECTION_SERVICE);
         }
@@ -488,17 +497,23 @@
                 mInfo.getServiceInfo().packageName) == PackageManager.PERMISSION_GRANTED) {
             Slog.w(TAG, "Voice interaction service should not hold permission "
                     + Manifest.permission.BIND_HOTWORD_DETECTION_SERVICE);
+            logDetectorCreateEventIfNeeded(callback, detectorType, false,
+                    voiceInteractionServiceUid);
             throw new SecurityException("Voice interaction service should not hold permission "
                     + Manifest.permission.BIND_HOTWORD_DETECTION_SERVICE);
         }
 
         if (sharedMemory != null && !sharedMemory.setProtect(OsConstants.PROT_READ)) {
             Slog.w(TAG, "Can't set sharedMemory to be read-only");
+            logDetectorCreateEventIfNeeded(callback, detectorType, false,
+                    voiceInteractionServiceUid);
             throw new IllegalStateException("Can't set sharedMemory to be read-only");
         }
 
         mDetectorType = detectorType;
 
+        logDetectorCreateEventIfNeeded(callback, detectorType, true,
+                voiceInteractionServiceUid);
         if (mHotwordDetectionConnection == null) {
             mHotwordDetectionConnection = new HotwordDetectionConnection(mServiceStub, mContext,
                     mInfo.getServiceInfo().applicationInfo.uid, voiceInteractorIdentity,
@@ -509,6 +524,15 @@
         }
     }
 
+    private void logDetectorCreateEventIfNeeded(IHotwordRecognitionStatusCallback callback,
+            int detectorType, boolean isCreated, int voiceInteractionServiceUid) {
+        if (callback != null) {
+            HotwordMetricsLogger.writeDetectorCreateEvent(detectorType, true,
+                    voiceInteractionServiceUid);
+        }
+
+    }
+
     public void shutdownHotwordDetectionServiceLocked() {
         if (DEBUG) {
             Slog.d(TAG, "shutdownHotwordDetectionServiceLocked");
diff --git a/telecomm/java/android/telecom/ConnectionService.java b/telecomm/java/android/telecom/ConnectionService.java
index 27d423b..bce6809 100755
--- a/telecomm/java/android/telecom/ConnectionService.java
+++ b/telecomm/java/android/telecom/ConnectionService.java
@@ -3171,9 +3171,14 @@
      *
      * {@link TelecomManager#addNewIncomingCall(PhoneAccountHandle, android.os.Bundle)}.
      *
+     * @param connectionManagerPhoneAccount The connection manager account to use for managing
+     *                                      this call
+     * @param request Details about the outgoing call
+     * @return The {@code Connection} object to satisfy this call, or the result of an invocation
+     *         of {@link Connection#createFailedConnection(DisconnectCause)} to not handle the call
      * @hide
      */
-    @SystemApi
+    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
     @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
     public @Nullable Connection onCreateUnknownConnection(
             @NonNull PhoneAccountHandle connectionManagerPhoneAccount,
diff --git a/telephony/java/android/telephony/DataFailCause.java b/telephony/java/android/telephony/DataFailCause.java
index 3a3b363..d2a4c3e 100644
--- a/telephony/java/android/telephony/DataFailCause.java
+++ b/telephony/java/android/telephony/DataFailCause.java
@@ -1090,6 +1090,13 @@
      */
     public static final int NO_RETRY_FAILURE = 0x1000B;
 
+    /**
+     * Traffic descriptors in DataCallResponse is empty.
+     *
+     * @hide
+     */
+    public static final int NO_TRAFFIC_DESCRIPTORS = 0x1000C;
+
     private static final Map<Integer, String> sFailCauseMap;
     static {
         sFailCauseMap = new HashMap<>();
@@ -1524,6 +1531,7 @@
         sFailCauseMap.put(SERVICE_TEMPORARILY_UNAVAILABLE, "SERVICE_TEMPORARILY_UNAVAILABLE");
         sFailCauseMap.put(REQUEST_NOT_SUPPORTED, "REQUEST_NOT_SUPPORTED");
         sFailCauseMap.put(NO_RETRY_FAILURE, "NO_RETRY_FAILURE");
+        sFailCauseMap.put(NO_TRAFFIC_DESCRIPTORS, "NO_TRAFFIC_DESCRIPTORS");
     }
 
     private DataFailCause() {
diff --git a/telephony/java/android/telephony/TelephonyManager.java b/telephony/java/android/telephony/TelephonyManager.java
index 1eb391d..3f430ab 100644
--- a/telephony/java/android/telephony/TelephonyManager.java
+++ b/telephony/java/android/telephony/TelephonyManager.java
@@ -8352,24 +8352,6 @@
     }
 
     /**
-     * Get P-CSCF address from PCO after data connection is established or modified.
-     * @param apnType the apnType, "ims" for IMS APN, "emergency" for EMERGENCY APN
-     * @return array of P-CSCF address
-     * @hide
-     */
-    public String[] getPcscfAddress(String apnType) {
-        try {
-            ITelephony telephony = getITelephony();
-            if (telephony == null)
-                return new String[0];
-            return telephony.getPcscfAddress(apnType, getOpPackageName(), getAttributionTag());
-        } catch (RemoteException e) {
-            return new String[0];
-        }
-    }
-
-
-    /**
      * Resets the {@link android.telephony.ims.ImsService} associated with the specified sim slot.
      * Used by diagnostic apps to force the IMS stack to be disabled and re-enabled in an effort to
      * recover from scenarios where the {@link android.telephony.ims.ImsService} gets in to a bad
@@ -16790,7 +16772,10 @@
      * Callback to listen for when the set of packages with carrier privileges for a SIM changes.
      *
      * @hide
+     * @deprecated Use {@link CarrierPrivilegesCallback} instead. This API will be removed soon
+     * prior to API finalization.
      */
+    @Deprecated
     @SystemApi
     public interface CarrierPrivilegesListener {
         /**
@@ -16810,6 +16795,54 @@
     }
 
     /**
+     * Callbacks to listen for when the set of packages with carrier privileges for a SIM changes.
+     *
+     * <p>Of note, when multiple callbacks are registered, they may be triggered one after another.
+     * The ordering of them is not guaranteed and thus should not be depend on.
+     *
+     * @hide
+     */
+    @SystemApi
+    public interface CarrierPrivilegesCallback {
+        /**
+         * Called when the set of packages with carrier privileges has changed.
+         *
+         * <p>Of note, this callback will <b>not</b> be fired if a carrier triggers a SIM profile
+         * switch and the same set of packages remains privileged after the switch.
+         *
+         * <p>At registration, the callback will receive the current set of privileged packages.
+         *
+         * @param privilegedPackageNames The updated set of package names that have carrier
+         *                               privileges
+         * @param privilegedUids         The updated set of UIDs that have carrier privileges
+         */
+        void onCarrierPrivilegesChanged(
+                @NonNull Set<String> privilegedPackageNames, @NonNull Set<Integer> privilegedUids);
+
+        /**
+         * Called when the {@link CarrierService} for the current user profile has changed.
+         *
+         * <p>This method does nothing by default. Clients that are interested in the carrier
+         * service change should override this method to get package name and UID info.
+         *
+         * <p>At registration, the callback will receive the current carrier service info.
+         *
+         * <p>Of note, this callback will <b>not</b> be fired if a carrier triggers a SIM profile
+         * switch and the same carrier service remains after switch.
+         *
+         * @param carrierServicePackageName package name of the {@link CarrierService}. May be
+         *                                  {@code null} when no carrier service is detected.
+         * @param carrierServiceUid         UID of the {@link CarrierService}. May be
+         *                                  {@link android.os.Process#INVALID_UID} if no carrier
+         *                                  service is detected.
+         */
+        default void onCarrierServiceChanged(
+                @Nullable String carrierServicePackageName, int carrierServiceUid) {
+            // do nothing by default
+        }
+    }
+
+    /**
      * Registers a {@link CarrierPrivilegesListener} on the given {@code logicalSlotIndex} to
      * receive callbacks when the set of packages with carrier privileges changes. The callback will
      * immediately be called with the latest state.
@@ -16818,7 +16851,10 @@
      * @param executor The executor where {@code listener} will be invoked
      * @param listener The callback to register
      * @hide
+     * @deprecated Use {@link #unregisterCarrierPrivilegesCallback} instead. This API will be
+     * removed prior to API finalization.
      */
+    @Deprecated
     @SystemApi
     @RequiresPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
     public void addCarrierPrivilegesListener(
@@ -16842,7 +16878,10 @@
      * Unregisters an existing {@link CarrierPrivilegesListener}.
      *
      * @hide
+     * @deprecated Use {@link #unregisterCarrierPrivilegesCallback} instead. This API will be
+     * removed prior to API finalization.
      */
+    @Deprecated
     @SystemApi
     @RequiresPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
     public void removeCarrierPrivilegesListener(@NonNull CarrierPrivilegesListener listener) {
@@ -16890,4 +16929,53 @@
             ex.rethrowAsRuntimeException();
         }
     }
+
+    /**
+     * Registers a {@link CarrierPrivilegesCallback} on the given {@code logicalSlotIndex} to
+     * receive callbacks when the set of packages with carrier privileges changes. The callback will
+     * immediately be called with the latest state.
+     *
+     * @param logicalSlotIndex The SIM slot to listen on
+     * @param executor The executor where {@code callback} will be invoked
+     * @param callback The callback to register
+     * @hide
+     */
+    @SystemApi
+    @RequiresPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
+    public void registerCarrierPrivilegesCallback(
+            int logicalSlotIndex,
+            @NonNull @CallbackExecutor Executor executor,
+            @NonNull CarrierPrivilegesCallback callback) {
+        if (mContext == null) {
+            throw new IllegalStateException("Telephony service is null");
+        } else if (executor == null || callback == null) {
+            throw new IllegalArgumentException(
+                    "CarrierPrivilegesCallback and executor must be non-null");
+        }
+        mTelephonyRegistryMgr = mContext.getSystemService(TelephonyRegistryManager.class);
+        if (mTelephonyRegistryMgr == null) {
+            throw new IllegalStateException("Telephony registry service is null");
+        }
+        mTelephonyRegistryMgr.addCarrierPrivilegesCallback(logicalSlotIndex, executor, callback);
+    }
+
+    /**
+     * Unregisters an existing {@link CarrierPrivilegesCallback}.
+     *
+     * @hide
+     */
+    @SystemApi
+    @RequiresPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
+    public void unregisterCarrierPrivilegesCallback(@NonNull CarrierPrivilegesCallback callback) {
+        if (mContext == null) {
+            throw new IllegalStateException("Telephony service is null");
+        } else if (callback == null) {
+            throw new IllegalArgumentException("CarrierPrivilegesCallback must be non-null");
+        }
+        mTelephonyRegistryMgr = mContext.getSystemService(TelephonyRegistryManager.class);
+        if (mTelephonyRegistryMgr == null) {
+            throw new IllegalStateException("Telephony registry service is null");
+        }
+        mTelephonyRegistryMgr.removeCarrierPrivilegesCallback(callback);
+    }
 }
diff --git a/telephony/java/android/telephony/data/ApnSetting.java b/telephony/java/android/telephony/data/ApnSetting.java
index fc94ebf..532679c 100644
--- a/telephony/java/android/telephony/data/ApnSetting.java
+++ b/telephony/java/android/telephony/data/ApnSetting.java
@@ -1103,8 +1103,10 @@
         sb.append(", ").append(MVNO_TYPE_INT_MAP.get(mMvnoType));
         sb.append(", ").append(mMvnoMatchData);
         sb.append(", ").append(mPermanentFailed);
-        sb.append(", ").append(mNetworkTypeBitmask);
-        sb.append(", ").append(mLingeringNetworkTypeBitmask);
+        sb.append(", ").append(TelephonyManager.convertNetworkTypeBitmaskToString(
+                mNetworkTypeBitmask));
+        sb.append(", ").append(TelephonyManager.convertNetworkTypeBitmaskToString(
+                mLingeringNetworkTypeBitmask));
         sb.append(", ").append(mApnSetId);
         sb.append(", ").append(mCarrierId);
         sb.append(", ").append(mSkip464Xlat);
diff --git a/telephony/java/android/telephony/ims/aidl/SipDelegateAidlWrapper.java b/telephony/java/android/telephony/ims/aidl/SipDelegateAidlWrapper.java
index 493ad5e..1ccb464 100644
--- a/telephony/java/android/telephony/ims/aidl/SipDelegateAidlWrapper.java
+++ b/telephony/java/android/telephony/ims/aidl/SipDelegateAidlWrapper.java
@@ -48,6 +48,9 @@
         @Override
         public void sendMessage(SipMessage sipMessage, long configVersion) {
             SipDelegate d = mDelegate;
+            if (d == null) {
+                return;
+            }
             final long token = Binder.clearCallingIdentity();
             try {
                 mExecutor.execute(() -> d.sendMessage(sipMessage, configVersion));
@@ -59,6 +62,9 @@
         @Override
         public void notifyMessageReceived(String viaTransactionId)  {
             SipDelegate d = mDelegate;
+            if (d == null) {
+                return;
+            }
             final long token = Binder.clearCallingIdentity();
             try {
                 mExecutor.execute(() -> d.notifyMessageReceived(viaTransactionId));
@@ -71,6 +77,9 @@
         @Override
         public void notifyMessageReceiveError(String viaTransactionId, int reason) {
             SipDelegate d = mDelegate;
+            if (d == null) {
+                return;
+            }
             final long token = Binder.clearCallingIdentity();
             try {
                 mExecutor.execute(() -> d.notifyMessageReceiveError(viaTransactionId, reason));
@@ -83,6 +92,9 @@
         @Override
         public void cleanupSession(String callId)  {
             SipDelegate d = mDelegate;
+            if (d == null) {
+                return;
+            }
             final long token = Binder.clearCallingIdentity();
             try {
                 mExecutor.execute(() -> d.cleanupSession(callId));
diff --git a/telephony/java/com/android/internal/telephony/ITelephony.aidl b/telephony/java/com/android/internal/telephony/ITelephony.aidl
index dc96b35..a5e2c1f 100644
--- a/telephony/java/com/android/internal/telephony/ITelephony.aidl
+++ b/telephony/java/com/android/internal/telephony/ITelephony.aidl
@@ -978,14 +978,6 @@
      boolean isManualNetworkSelectionAllowed(int subId);
 
     /**
-     * Get P-CSCF address from PCO after data connection is established or modified.
-     * @param apnType the apnType, "ims" for IMS APN, "emergency" for EMERGENCY APN
-     * @param callingPackage The package making the call.
-     * @param callingFeatureId The feature in the package.
-     */
-    String[] getPcscfAddress(String apnType, String callingPackage, String callingFeatureId);
-
-    /**
      * Set IMS registration state
      */
     void setImsRegistrationState(boolean registered);
diff --git a/tests/FlickerTests/src/com/android/server/wm/flicker/launch/OpenAppNonResizeableTest.kt b/tests/FlickerTests/src/com/android/server/wm/flicker/launch/OpenAppNonResizeableTest.kt
index b0e53e9..c3a4769 100644
--- a/tests/FlickerTests/src/com/android/server/wm/flicker/launch/OpenAppNonResizeableTest.kt
+++ b/tests/FlickerTests/src/com/android/server/wm/flicker/launch/OpenAppNonResizeableTest.kt
@@ -74,7 +74,7 @@
      * Checks that the nav bar layer starts invisible, becomes visible during unlocking animation
      * and remains visible at the end
      */
-    @Postsubmit
+    @Presubmit
     @Test
     fun navBarLayerVisibilityChanges() {
         testSpec.assertLayers {
diff --git a/tests/TrustTests/AndroidManifest.xml b/tests/TrustTests/AndroidManifest.xml
index c94152d..68bc1f69 100644
--- a/tests/TrustTests/AndroidManifest.xml
+++ b/tests/TrustTests/AndroidManifest.xml
@@ -23,7 +23,9 @@
     <uses-permission android:name="android.permission.BIND_DEVICE_ADMIN" />
     <uses-permission android:name="android.permission.CONTROL_KEYGUARD" />
     <uses-permission android:name="android.permission.DEVICE_POWER" />
+    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
     <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" />
+    <uses-permission android:name="android.permission.MANAGE_USERS" />
     <uses-permission android:name="android.permission.PROVIDE_TRUST_AGENT" />
     <uses-permission android:name="android.permission.TRUST_LISTENER" />
 
diff --git a/tests/TrustTests/TEST_MAPPING b/tests/TrustTests/TEST_MAPPING
new file mode 100644
index 0000000..b9c46bf
--- /dev/null
+++ b/tests/TrustTests/TEST_MAPPING
@@ -0,0 +1,15 @@
+{
+  "presubmit": [
+    {
+      "name": "TrustTests",
+      "options": [
+        {
+          "include-filter": "android.trust.test"
+        },
+        {
+          "exclude-annotation": "androidx.test.filters.FlakyTest"
+        }
+      ]
+    }
+  ]
+}
\ No newline at end of file
diff --git a/tests/TrustTests/src/android/trust/test/LockUserTest.kt b/tests/TrustTests/src/android/trust/test/LockUserTest.kt
index 83fc28f..8f200a6 100644
--- a/tests/TrustTests/src/android/trust/test/LockUserTest.kt
+++ b/tests/TrustTests/src/android/trust/test/LockUserTest.kt
@@ -25,7 +25,6 @@
 import androidx.test.ext.junit.rules.ActivityScenarioRule
 import androidx.test.ext.junit.runners.AndroidJUnit4
 import com.google.common.truth.Truth.assertThat
-import org.junit.Ignore
 import org.junit.Rule
 import org.junit.Test
 import org.junit.rules.RuleChain
@@ -49,7 +48,6 @@
         .around(lockStateTrackingRule)
         .around(trustAgentRule)
 
-    @Ignore("Causes issues with subsequent tests") // TODO: Enable test
     @Test
     fun lockUser_locksTheDevice() {
         Log.i(TAG, "Locking user")
diff --git a/tests/TrustTests/src/android/trust/test/lib/ScreenLockRule.kt b/tests/TrustTests/src/android/trust/test/lib/ScreenLockRule.kt
index bc100ba..006525d 100644
--- a/tests/TrustTests/src/android/trust/test/lib/ScreenLockRule.kt
+++ b/tests/TrustTests/src/android/trust/test/lib/ScreenLockRule.kt
@@ -20,6 +20,8 @@
 import android.util.Log
 import android.view.WindowManagerGlobal
 import androidx.test.core.app.ApplicationProvider.getApplicationContext
+import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
+import androidx.test.uiautomator.UiDevice
 import com.android.internal.widget.LockPatternUtils
 import com.android.internal.widget.LockscreenCredential
 import com.google.common.truth.Truth.assertWithMessage
@@ -32,6 +34,7 @@
  */
 class ScreenLockRule : TestRule {
     private val context: Context = getApplicationContext()
+    private val uiDevice = UiDevice.getInstance(getInstrumentation())
     private val windowManager = WindowManagerGlobal.getWindowManagerService()
     private val lockPatternUtils = LockPatternUtils(context)
     private var instantLockSavedValue = false
@@ -48,19 +51,21 @@
             } finally {
                 removeScreenLock()
                 revertLockOnPowerButton()
+                verifyKeyguardDismissed()
             }
         }
     }
 
     private fun verifyNoScreenLockAlreadySet() {
         assertWithMessage("Screen Lock must not already be set on device")
-            .that(lockPatternUtils.isSecure(context.userId))
-            .isFalse()
+                .that(lockPatternUtils.isSecure(context.userId))
+                .isFalse()
     }
 
     private fun verifyKeyguardDismissed() {
         val maxWaits = 30
         var waitCount = 0
+
         while (windowManager.isKeyguardLocked && waitCount < maxWaits) {
             Log.i(TAG, "Keyguard still showing; attempting to dismiss and wait 50ms ($waitCount)")
             windowManager.dismissKeyguard(null, null)
@@ -68,19 +73,19 @@
             waitCount++
         }
         assertWithMessage("Keyguard should be unlocked")
-            .that(windowManager.isKeyguardLocked)
-            .isFalse()
+                .that(windowManager.isKeyguardLocked)
+                .isFalse()
     }
 
     private fun setScreenLock() {
         lockPatternUtils.setLockCredential(
-            LockscreenCredential.createPin(PIN),
-            LockscreenCredential.createNone(),
-            context.userId
+                LockscreenCredential.createPin(PIN),
+                LockscreenCredential.createNone(),
+                context.userId
         )
         assertWithMessage("Screen Lock should now be set")
-            .that(lockPatternUtils.isSecure(context.userId))
-            .isTrue()
+                .that(lockPatternUtils.isSecure(context.userId))
+                .isTrue()
         Log.i(TAG, "Device PIN set to $PIN")
     }
 
@@ -90,14 +95,25 @@
     }
 
     private fun removeScreenLock() {
-        lockPatternUtils.setLockCredential(
-            LockscreenCredential.createNone(),
-            LockscreenCredential.createPin(PIN),
-            context.userId
-        )
-        Log.i(TAG, "Device PIN cleared; waiting 50 ms then dismissing Keyguard")
-        Thread.sleep(50)
-        windowManager.dismissKeyguard(null, null)
+        var lockCredentialUnset = lockPatternUtils.setLockCredential(
+                LockscreenCredential.createNone(),
+                LockscreenCredential.createPin(PIN),
+                context.userId)
+        Thread.sleep(100)
+        assertWithMessage("Lock screen credential should be unset")
+                .that(lockCredentialUnset)
+                .isTrue()
+
+        lockPatternUtils.setLockScreenDisabled(true, context.userId)
+        Thread.sleep(100)
+        assertWithMessage("Lockscreen needs to be disabled")
+                .that(lockPatternUtils.isLockScreenDisabled(context.userId))
+                .isTrue()
+
+        // this is here because somehow it helps the keyguard not get stuck
+        uiDevice.sleep()
+        Thread.sleep(500) // delay added to avoid initiating camera by double clicking power
+        uiDevice.wakeUp()
     }
 
     private fun revertLockOnPowerButton() {
diff --git a/tests/componentalias/src/android/content/componentalias/tests/BaseComponentAliasTest.java b/tests/componentalias/src/android/content/componentalias/tests/BaseComponentAliasTest.java
index 9658d6f..164f61c 100644
--- a/tests/componentalias/src/android/content/componentalias/tests/BaseComponentAliasTest.java
+++ b/tests/componentalias/src/android/content/componentalias/tests/BaseComponentAliasTest.java
@@ -37,7 +37,7 @@
     protected static final Context sContext = InstrumentationRegistry.getTargetContext();
 
     protected static final DeviceConfigStateHelper sDeviceConfig = new DeviceConfigStateHelper(
-            DeviceConfig.NAMESPACE_ACTIVITY_MANAGER);
+            DeviceConfig.NAMESPACE_ACTIVITY_MANAGER_COMPONENT_ALIAS);
     @Before
     public void enableComponentAliasWithCompatFlag() throws Exception {
         Assume.assumeTrue(Build.isDebuggable());
diff --git a/tests/componentalias/src/android/content/componentalias/tests/ComponentAliasEnableWithDeviceConfigTest.java b/tests/componentalias/src/android/content/componentalias/tests/ComponentAliasEnableWithDeviceConfigTest.java
index 52c6d5b..ee20379 100644
--- a/tests/componentalias/src/android/content/componentalias/tests/ComponentAliasEnableWithDeviceConfigTest.java
+++ b/tests/componentalias/src/android/content/componentalias/tests/ComponentAliasEnableWithDeviceConfigTest.java
@@ -28,7 +28,7 @@
 
 public class ComponentAliasEnableWithDeviceConfigTest {
     protected static final DeviceConfigStateHelper sDeviceConfig = new DeviceConfigStateHelper(
-            DeviceConfig.NAMESPACE_ACTIVITY_MANAGER);
+            DeviceConfig.NAMESPACE_ACTIVITY_MANAGER_COMPONENT_ALIAS);
 
     @AfterClass
     public static void restoreDeviceConfig() throws Exception {
diff --git a/tests/componentalias/src/android/content/componentalias/tests/ComponentAliasNotSupportedOnUserBuildTest.java b/tests/componentalias/src/android/content/componentalias/tests/ComponentAliasNotSupportedOnUserBuildTest.java
index 7935476..0899886 100644
--- a/tests/componentalias/src/android/content/componentalias/tests/ComponentAliasNotSupportedOnUserBuildTest.java
+++ b/tests/componentalias/src/android/content/componentalias/tests/ComponentAliasNotSupportedOnUserBuildTest.java
@@ -32,7 +32,7 @@
  */
 public class ComponentAliasNotSupportedOnUserBuildTest {
     protected static final DeviceConfigStateHelper sDeviceConfig = new DeviceConfigStateHelper(
-            DeviceConfig.NAMESPACE_ACTIVITY_MANAGER);
+            DeviceConfig.NAMESPACE_ACTIVITY_MANAGER_COMPONENT_ALIAS);
 
     @AfterClass
     public static void restoreDeviceConfig() throws Exception {
diff --git a/tests/vcn/java/com/android/server/vcn/TelephonySubscriptionTrackerTest.java b/tests/vcn/java/com/android/server/vcn/TelephonySubscriptionTrackerTest.java
index 7b1f7a5..b673957 100644
--- a/tests/vcn/java/com/android/server/vcn/TelephonySubscriptionTrackerTest.java
+++ b/tests/vcn/java/com/android/server/vcn/TelephonySubscriptionTrackerTest.java
@@ -60,7 +60,7 @@
 import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener;
 import android.telephony.TelephonyCallback;
 import android.telephony.TelephonyManager;
-import android.telephony.TelephonyManager.CarrierPrivilegesListener;
+import android.telephony.TelephonyManager.CarrierPrivilegesCallback;
 import android.util.ArrayMap;
 import android.util.ArraySet;
 
@@ -187,11 +187,11 @@
         return captor.getValue();
     }
 
-    private List<CarrierPrivilegesListener> getCarrierPrivilegesListeners() {
-        final ArgumentCaptor<CarrierPrivilegesListener> captor =
-                ArgumentCaptor.forClass(CarrierPrivilegesListener.class);
+    private List<CarrierPrivilegesCallback> getCarrierPrivilegesCallbacks() {
+        final ArgumentCaptor<CarrierPrivilegesCallback> captor =
+                ArgumentCaptor.forClass(CarrierPrivilegesCallback.class);
         verify(mTelephonyManager, atLeastOnce())
-                .addCarrierPrivilegesListener(anyInt(), any(), captor.capture());
+                .registerCarrierPrivilegesCallback(anyInt(), any(), captor.capture());
 
         return captor.getAllValues();
     }
@@ -270,12 +270,12 @@
         assertNotNull(getOnSubscriptionsChangedListener());
 
         verify(mTelephonyManager, times(2))
-                .addCarrierPrivilegesListener(anyInt(), any(HandlerExecutor.class), any());
+                .registerCarrierPrivilegesCallback(anyInt(), any(HandlerExecutor.class), any());
         verify(mTelephonyManager)
-                .addCarrierPrivilegesListener(eq(0), any(HandlerExecutor.class), any());
+                .registerCarrierPrivilegesCallback(eq(0), any(HandlerExecutor.class), any());
         verify(mTelephonyManager)
-                .addCarrierPrivilegesListener(eq(1), any(HandlerExecutor.class), any());
-        assertEquals(2, getCarrierPrivilegesListeners().size());
+                .registerCarrierPrivilegesCallback(eq(1), any(HandlerExecutor.class), any());
+        assertEquals(2, getCarrierPrivilegesCallbacks().size());
     }
 
     @Test
@@ -287,10 +287,10 @@
         final OnSubscriptionsChangedListener listener = getOnSubscriptionsChangedListener();
         verify(mSubscriptionManager).removeOnSubscriptionsChangedListener(eq(listener));
 
-        for (CarrierPrivilegesListener carrierPrivilegesListener :
-                getCarrierPrivilegesListeners()) {
+        for (CarrierPrivilegesCallback carrierPrivilegesCallback :
+                getCarrierPrivilegesCallbacks()) {
             verify(mTelephonyManager)
-                    .removeCarrierPrivilegesListener(eq(carrierPrivilegesListener));
+                    .unregisterCarrierPrivilegesCallback(eq(carrierPrivilegesCallback));
         }
     }
 
@@ -303,15 +303,15 @@
         mTelephonySubscriptionTracker.setReadySubIdsBySlotId(readySubIdsBySlotId);
         doReturn(1).when(mTelephonyManager).getActiveModemCount();
 
-        List<CarrierPrivilegesListener> carrierPrivilegesListeners =
-                getCarrierPrivilegesListeners();
+        List<CarrierPrivilegesCallback> carrierPrivilegesCallbacks =
+                getCarrierPrivilegesCallbacks();
 
         mTelephonySubscriptionTracker.onReceive(mContext, buildTestMultiSimConfigBroadcastIntent());
         mTestLooper.dispatchAll();
 
-        for (CarrierPrivilegesListener carrierPrivilegesListener : carrierPrivilegesListeners) {
+        for (CarrierPrivilegesCallback carrierPrivilegesCallback : carrierPrivilegesCallbacks) {
             verify(mTelephonyManager)
-                    .removeCarrierPrivilegesListener(eq(carrierPrivilegesListener));
+                    .unregisterCarrierPrivilegesCallback(eq(carrierPrivilegesCallback));
         }
 
         // Expect cache cleared for inactive slots.
@@ -323,9 +323,9 @@
         // Expect a new CarrierPrivilegesListener to have been registered for slot 0, and none other
         // (2 previously registered during startup, for slots 0 & 1)
         verify(mTelephonyManager, times(3))
-                .addCarrierPrivilegesListener(anyInt(), any(HandlerExecutor.class), any());
+                .registerCarrierPrivilegesCallback(anyInt(), any(HandlerExecutor.class), any());
         verify(mTelephonyManager, times(2))
-                .addCarrierPrivilegesListener(eq(0), any(HandlerExecutor.class), any());
+                .registerCarrierPrivilegesCallback(eq(0), any(HandlerExecutor.class), any());
 
         // Verify that this triggers a re-evaluation
         verify(mCallback).onNewSnapshot(eq(buildExpectedSnapshot(TEST_PRIVILEGED_PACKAGES)));
@@ -391,8 +391,8 @@
     public void testOnCarrierPrivilegesChanged() throws Exception {
         setupReadySubIds();
 
-        final CarrierPrivilegesListener listener = getCarrierPrivilegesListeners().get(0);
-        listener.onCarrierPrivilegesChanged(Collections.emptyList(), new int[] {});
+        final CarrierPrivilegesCallback callback = getCarrierPrivilegesCallbacks().get(0);
+        callback.onCarrierPrivilegesChanged(Collections.emptySet(), Collections.emptySet());
         mTestLooper.dispatchAll();
 
         verify(mCallback).onNewSnapshot(eq(buildExpectedSnapshot(TEST_PRIVILEGED_PACKAGES)));
diff --git a/tools/processors/staledataclass/src/android/processor/staledataclass/StaleDataclassProcessor.kt b/tools/processors/staledataclass/src/android/processor/staledataclass/StaleDataclassProcessor.kt
index 1aec9b8..2e60f64 100644
--- a/tools/processors/staledataclass/src/android/processor/staledataclass/StaleDataclassProcessor.kt
+++ b/tools/processors/staledataclass/src/android/processor/staledataclass/StaleDataclassProcessor.kt
@@ -21,8 +21,6 @@
 import com.android.codegen.CANONICAL_BUILDER_CLASS
 import com.android.codegen.CODEGEN_NAME
 import com.android.codegen.CODEGEN_VERSION
-import com.sun.tools.javac.code.Symbol
-import com.sun.tools.javac.code.Type
 import java.io.File
 import java.io.FileNotFoundException
 import javax.annotation.processing.AbstractProcessor
@@ -33,6 +31,7 @@
 import javax.lang.model.element.Element
 import javax.lang.model.element.ElementKind
 import javax.lang.model.element.TypeElement
+import javax.lang.model.type.ExecutableType
 import javax.tools.Diagnostic
 
 private const val STALE_FILE_THRESHOLD_MS = 1000
@@ -102,14 +101,13 @@
             append(" ")
             append(elem.annotationMirrors.joinToString(" ", transform = { annotationToString(it) }))
             append(" ")
-            if (elem is Symbol) {
-                if (elem.type is Type.MethodType) {
-                    append((elem.type as Type.MethodType).returnType)
-                } else {
-                    append(elem.type)
-                }
-                append(" ")
+            val type = elem.asType()
+            if (type is ExecutableType) {
+                append(type.returnType)
+            } else {
+                append(type)
             }
+            append(" ")
             append(elem)
         }
     }
@@ -234,4 +232,4 @@
     override fun getSupportedSourceVersion(): SourceVersion {
         return SourceVersion.latest()
     }
-}
\ No newline at end of file
+}
diff --git a/tools/validatekeymaps/Android.bp b/tools/validatekeymaps/Android.bp
index 0423b7a..ff24d16 100644
--- a/tools/validatekeymaps/Android.bp
+++ b/tools/validatekeymaps/Android.bp
@@ -32,7 +32,7 @@
         "libui-types",
     ],
     target: {
-        linux_glibc: {
+        host_linux: {
             static_libs: [
                 // libbinder is only available for linux
                 "libbinder",
diff --git a/wifi/java/src/android/net/wifi/nl80211/WifiNl80211Manager.java b/wifi/java/src/android/net/wifi/nl80211/WifiNl80211Manager.java
index 9604475..d85a5bd 100644
--- a/wifi/java/src/android/net/wifi/nl80211/WifiNl80211Manager.java
+++ b/wifi/java/src/android/net/wifi/nl80211/WifiNl80211Manager.java
@@ -963,6 +963,25 @@
     }
 
     /**
+     * Get the max number of SSIDs that the driver supports per scan.
+     *
+     * @param ifaceName Name of the interface.
+     */
+    public int getMaxSsidsPerScan(@NonNull String ifaceName) {
+        IWifiScannerImpl scannerImpl = getScannerImpl(ifaceName);
+        if (scannerImpl == null) {
+            Log.e(TAG, "No valid wificond scanner interface handler for iface=" + ifaceName);
+            return 0;
+        }
+        try {
+            return scannerImpl.getMaxSsidsPerScan();
+        } catch (RemoteException e1) {
+            Log.e(TAG, "Failed to getMaxSsidsPerScan");
+        }
+        return 0;
+    }
+
+    /**
      * Return scan type for the parcelable {@link SingleScanSettings}
      */
     private static int getScanType(@WifiAnnotations.ScanType int scanType) {