Merge "Wrap ShadeViewController in a Provider in BaseCommunalViewModel" into main
diff --git a/apex/jobscheduler/service/java/com/android/server/job/JobSchedulerService.java b/apex/jobscheduler/service/java/com/android/server/job/JobSchedulerService.java
index f97100b..83db4cb 100644
--- a/apex/jobscheduler/service/java/com/android/server/job/JobSchedulerService.java
+++ b/apex/jobscheduler/service/java/com/android/server/job/JobSchedulerService.java
@@ -1433,10 +1433,10 @@
                         Slog.d(TAG, "Removing jobs for pkg " + pkgName + " at uid " + pkgUid);
                     }
                     synchronized (mLock) {
-                        // Exclude jobs scheduled on behalf of this app for now because SyncManager
+                        // Exclude jobs scheduled on behalf of this app because SyncManager
                         // and other job proxy agents may not know to reschedule the job properly
                         // after force stop.
-                        // TODO(209852664): determine how to best handle syncs & other proxied jobs
+                        // Proxied jobs will not be allowed to run if the source app is stopped.
                         cancelJobsForPackageAndUidLocked(pkgName, pkgUid,
                                 /* includeSchedulingApp */ true, /* includeSourceApp */ false,
                                 JobParameters.STOP_REASON_USER,
@@ -1448,7 +1448,9 @@
         }
     };
 
-    private String getPackageName(Intent intent) {
+    /** Returns the package name stored in the intent's data. */
+    @Nullable
+    public static String getPackageName(Intent intent) {
         Uri uri = intent.getData();
         String pkg = uri != null ? uri.getSchemeSpecificPart() : null;
         return pkg;
@@ -5365,6 +5367,14 @@
             }
             pw.println();
 
+            pw.println("Aconfig flags:");
+            pw.increaseIndent();
+            pw.print(Flags.FLAG_THROW_ON_UNSUPPORTED_BIAS_USAGE,
+                    Flags.throwOnUnsupportedBiasUsage());
+            pw.println();
+            pw.decreaseIndent();
+            pw.println();
+
             for (int i = mJobRestrictions.size() - 1; i >= 0; i--) {
                 mJobRestrictions.get(i).dumpConstants(pw);
             }
diff --git a/apex/jobscheduler/service/java/com/android/server/job/JobSchedulerShellCommand.java b/apex/jobscheduler/service/java/com/android/server/job/JobSchedulerShellCommand.java
index 293088d..c14efae 100644
--- a/apex/jobscheduler/service/java/com/android/server/job/JobSchedulerShellCommand.java
+++ b/apex/jobscheduler/service/java/com/android/server/job/JobSchedulerShellCommand.java
@@ -58,6 +58,8 @@
                     return cancelJob(pw);
                 case "monitor-battery":
                     return monitorBattery(pw);
+                case "get-aconfig-flag-state":
+                    return getAconfigFlagState(pw);
                 case "get-battery-seq":
                     return getBatterySeq(pw);
                 case "get-battery-charging":
@@ -336,6 +338,28 @@
         return 0;
     }
 
+    private int getAconfigFlagState(PrintWriter pw) throws Exception {
+        checkPermission("get aconfig flag state");
+
+        final String flagName = getNextArgRequired();
+
+        switch (flagName) {
+            case android.app.job.Flags.FLAG_JOB_DEBUG_INFO_APIS:
+                pw.println(android.app.job.Flags.jobDebugInfoApis());
+                break;
+            case android.app.job.Flags.FLAG_ENFORCE_MINIMUM_TIME_WINDOWS:
+                pw.println(android.app.job.Flags.enforceMinimumTimeWindows());
+                break;
+            case com.android.server.job.Flags.FLAG_THROW_ON_UNSUPPORTED_BIAS_USAGE:
+                pw.println(com.android.server.job.Flags.throwOnUnsupportedBiasUsage());
+                break;
+            default:
+                pw.println("Unknown flag: " + flagName);
+                break;
+        }
+        return 0;
+    }
+
     private int getBatterySeq(PrintWriter pw) {
         int seq = mInternal.getBatterySeq();
         pw.println(seq);
@@ -693,6 +717,9 @@
         pw.println("  monitor-battery [on|off]");
         pw.println("    Control monitoring of all battery changes.  Off by default.  Turning");
         pw.println("    on makes get-battery-seq useful.");
+        pw.println("  get-aconfig-flag-state FULL_FLAG_NAME");
+        pw.println("    Return the state of the specified aconfig flag, if known. The flag name");
+        pw.println("         must be fully qualified.");
         pw.println("  get-battery-seq");
         pw.println("    Return the last battery update sequence number that was received.");
         pw.println("  get-battery-charging");
diff --git a/apex/jobscheduler/service/java/com/android/server/job/controllers/BackgroundJobsController.java b/apex/jobscheduler/service/java/com/android/server/job/controllers/BackgroundJobsController.java
index cd3ba6b..4aadc90 100644
--- a/apex/jobscheduler/service/java/com/android/server/job/controllers/BackgroundJobsController.java
+++ b/apex/jobscheduler/service/java/com/android/server/job/controllers/BackgroundJobsController.java
@@ -17,18 +17,26 @@
 package com.android.server.job.controllers;
 
 import static com.android.server.job.JobSchedulerService.NEVER_INDEX;
+import static com.android.server.job.JobSchedulerService.getPackageName;
 import static com.android.server.job.JobSchedulerService.sElapsedRealtimeClock;
 
 import android.app.ActivityManager;
 import android.app.ActivityManagerInternal;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.pm.PackageManagerInternal;
 import android.os.SystemClock;
 import android.os.UserHandle;
 import android.util.ArraySet;
 import android.util.IndentingPrintWriter;
 import android.util.Log;
 import android.util.Slog;
+import android.util.SparseArrayMap;
 import android.util.proto.ProtoOutputStream;
 
+import com.android.internal.annotations.GuardedBy;
 import com.android.server.AppStateTracker;
 import com.android.server.AppStateTrackerImpl;
 import com.android.server.AppStateTrackerImpl.Listener;
@@ -50,6 +58,8 @@
  *
  * - the uid-active boolean state expressed by the AppStateTracker.  Jobs in 'active'
  *    uids are inherently eligible to run jobs regardless of the uid's standby bucket.
+ *
+ * - the app's stopped state
  */
 public final class BackgroundJobsController extends StateController {
     private static final String TAG = "JobScheduler.Background";
@@ -63,9 +73,48 @@
 
     private final ActivityManagerInternal mActivityManagerInternal;
     private final AppStateTrackerImpl mAppStateTracker;
+    private final PackageManagerInternal mPackageManagerInternal;
+
+    @GuardedBy("mLock")
+    private final SparseArrayMap<String, Boolean> mPackageStoppedState = new SparseArrayMap<>();
 
     private final UpdateJobFunctor mUpdateJobFunctor = new UpdateJobFunctor();
 
+    private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            final String pkgName = getPackageName(intent);
+            final int pkgUid = intent.getIntExtra(Intent.EXTRA_UID, -1);
+            final String action = intent.getAction();
+            if (pkgUid == -1) {
+                Slog.e(TAG, "Didn't get package UID in intent (" + action + ")");
+                return;
+            }
+
+            if (DEBUG) {
+                Slog.d(TAG, "Got " + action + " for " + pkgUid + "/" + pkgName);
+            }
+
+            switch (action) {
+                case Intent.ACTION_PACKAGE_RESTARTED: {
+                    synchronized (mLock) {
+                        mPackageStoppedState.add(pkgUid, pkgName, Boolean.TRUE);
+                        updateJobRestrictionsForUidLocked(pkgUid, false);
+                    }
+                }
+                break;
+
+                case Intent.ACTION_PACKAGE_UNSTOPPED: {
+                    synchronized (mLock) {
+                        mPackageStoppedState.add(pkgUid, pkgName, Boolean.FALSE);
+                        updateJobRestrictionsLocked(pkgUid, UNKNOWN);
+                    }
+                }
+                break;
+            }
+        }
+    };
+
     public BackgroundJobsController(JobSchedulerService service) {
         super(service);
 
@@ -73,11 +122,18 @@
                 LocalServices.getService(ActivityManagerInternal.class));
         mAppStateTracker = (AppStateTrackerImpl) Objects.requireNonNull(
                 LocalServices.getService(AppStateTracker.class));
+        mPackageManagerInternal = LocalServices.getService(PackageManagerInternal.class);
     }
 
     @Override
     public void startTrackingLocked() {
         mAppStateTracker.addListener(mForceAppStandbyListener);
+        final IntentFilter filter = new IntentFilter();
+        filter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
+        filter.addAction(Intent.ACTION_PACKAGE_UNSTOPPED);
+        filter.addDataScheme("package");
+        mContext.registerReceiverAsUser(
+                mBroadcastReceiver, UserHandle.ALL, filter, null, null);
     }
 
     @Override
@@ -99,11 +155,45 @@
     }
 
     @Override
+    public void onAppRemovedLocked(String packageName, int uid) {
+        mPackageStoppedState.delete(uid, packageName);
+    }
+
+    @Override
+    public void onUserRemovedLocked(int userId) {
+        for (int u = mPackageStoppedState.numMaps() - 1; u >= 0; --u) {
+            final int uid = mPackageStoppedState.keyAt(u);
+            if (UserHandle.getUserId(uid) == userId) {
+                mPackageStoppedState.deleteAt(u);
+            }
+        }
+    }
+
+    @Override
     public void dumpControllerStateLocked(final IndentingPrintWriter pw,
             final Predicate<JobStatus> predicate) {
+        pw.println("Aconfig flags:");
+        pw.increaseIndent();
+        pw.print(android.content.pm.Flags.FLAG_STAY_STOPPED,
+                android.content.pm.Flags.stayStopped());
+        pw.println();
+        pw.decreaseIndent();
+        pw.println();
+
         mAppStateTracker.dump(pw);
         pw.println();
 
+        pw.println("Stopped packages:");
+        pw.increaseIndent();
+        mPackageStoppedState.forEach((uid, pkgName, isStopped) -> {
+            pw.print(uid);
+            pw.print(":");
+            pw.print(pkgName);
+            pw.print("=");
+            pw.println(isStopped);
+        });
+        pw.println();
+
         mService.getJobStore().forEachJob(predicate, (jobStatus) -> {
             final int uid = jobStatus.getSourceUid();
             final String sourcePkg = jobStatus.getSourcePackageName();
@@ -205,14 +295,34 @@
         }
     }
 
+    private boolean isPackageStopped(String packageName, int uid) {
+        if (mPackageStoppedState.contains(uid, packageName)) {
+            return mPackageStoppedState.get(uid, packageName);
+        }
+        final boolean isStopped = mPackageManagerInternal.isPackageStopped(packageName, uid);
+        mPackageStoppedState.add(uid, packageName, isStopped);
+        return isStopped;
+    }
+
     boolean updateSingleJobRestrictionLocked(JobStatus jobStatus, final long nowElapsed,
             int activeState) {
         final int uid = jobStatus.getSourceUid();
         final String packageName = jobStatus.getSourcePackageName();
 
-        final boolean isUserBgRestricted =
-                !mActivityManagerInternal.isBgAutoRestrictedBucketFeatureFlagEnabled()
-                        && !mAppStateTracker.isRunAnyInBackgroundAppOpsAllowed(uid, packageName);
+        final boolean isSourcePkgStopped =
+                isPackageStopped(jobStatus.getSourcePackageName(), jobStatus.getSourceUid());
+        final boolean isCallingPkgStopped;
+        if (!jobStatus.isProxyJob()) {
+            isCallingPkgStopped = isSourcePkgStopped;
+        } else {
+            isCallingPkgStopped =
+                    isPackageStopped(jobStatus.getCallingPackageName(), jobStatus.getUid());
+        }
+        final boolean isStopped = android.content.pm.Flags.stayStopped()
+                && (isCallingPkgStopped || isSourcePkgStopped);
+        final boolean isUserBgRestricted = isStopped
+                || (!mActivityManagerInternal.isBgAutoRestrictedBucketFeatureFlagEnabled()
+                        && !mAppStateTracker.isRunAnyInBackgroundAppOpsAllowed(uid, packageName));
         // If a job started with the foreground flag, it'll cause the UID to stay active
         // and thus cause areJobsRestricted() to always return false, so if
         // areJobsRestricted() returns false and the app is BG restricted and not TOP,
@@ -233,7 +343,8 @@
                 && isUserBgRestricted
                 && mService.getUidProcState(uid)
                         > ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE;
-        final boolean canRun = !shouldStopImmediately
+        // Don't let jobs (including proxied jobs) run if the app is in the stopped state.
+        final boolean canRun = !isStopped && !shouldStopImmediately
                 && !mAppStateTracker.areJobsRestricted(
                         uid, packageName, jobStatus.canRunInBatterySaver());
 
diff --git a/apex/jobscheduler/service/java/com/android/server/job/controllers/JobStatus.java b/apex/jobscheduler/service/java/com/android/server/job/controllers/JobStatus.java
index b7480649..d1f575e 100644
--- a/apex/jobscheduler/service/java/com/android/server/job/controllers/JobStatus.java
+++ b/apex/jobscheduler/service/java/com/android/server/job/controllers/JobStatus.java
@@ -1102,6 +1102,12 @@
         return job.getService();
     }
 
+    /** Return the package name of the app that scheduled the job. */
+    public String getCallingPackageName() {
+        return job.getService().getPackageName();
+    }
+
+    /** Return the package name of the app on whose behalf the job was scheduled. */
     public String getSourcePackageName() {
         return sourcePackageName;
     }
diff --git a/cmds/idmap2/Android.bp b/cmds/idmap2/Android.bp
index 55ec7da..6e51f00 100644
--- a/cmds/idmap2/Android.bp
+++ b/cmds/idmap2/Android.bp
@@ -86,6 +86,7 @@
             static_libs: [
                 "libidmap2_policies",
                 "libidmap2_protos",
+                "libpng",
             ],
             shared_libs: [
                 "libandroidfw",
@@ -107,6 +108,7 @@
                 "libcutils",
                 "libidmap2_policies",
                 "libidmap2_protos",
+                "libpng",
                 "libprotobuf-cpp-lite",
                 "libutils",
                 "libz",
@@ -185,6 +187,7 @@
     static_libs: [
         "libgmock",
         "libidmap2_protos",
+        "libpng",
     ],
     target: {
         android: {
@@ -258,6 +261,7 @@
                 "libbase",
                 "libcutils",
                 "libidmap2",
+                "libpng",
                 "libprotobuf-cpp-lite",
                 "libutils",
                 "libz",
@@ -275,6 +279,7 @@
                 "libidmap2",
                 "libidmap2_policies",
                 "liblog",
+                "libpng",
                 "libprotobuf-cpp-lite",
                 "libutils",
                 "libziparchive",
diff --git a/cmds/idmap2/idmap2d/Idmap2Service.cpp b/cmds/idmap2/idmap2d/Idmap2Service.cpp
index d76ca5b..f264125 100644
--- a/cmds/idmap2/idmap2d/Idmap2Service.cpp
+++ b/cmds/idmap2/idmap2d/Idmap2Service.cpp
@@ -266,7 +266,8 @@
     } else if (res.binaryData.has_value()) {
       builder.SetResourceValue(res.resourceName, res.binaryData->get(),
                                res.binaryDataOffset, res.binaryDataSize,
-                               res.configuration.value_or(std::string()));
+                               res.configuration.value_or(std::string()),
+                               res.isNinePatch);
     } else {
       builder.SetResourceValue(res.resourceName, res.dataType, res.data,
             res.configuration.value_or(std::string()));
diff --git a/cmds/idmap2/idmap2d/aidl/core/android/os/FabricatedOverlayInternalEntry.aidl b/cmds/idmap2/idmap2d/aidl/core/android/os/FabricatedOverlayInternalEntry.aidl
index 8ebd454..bca2ff3 100644
--- a/cmds/idmap2/idmap2d/aidl/core/android/os/FabricatedOverlayInternalEntry.aidl
+++ b/cmds/idmap2/idmap2d/aidl/core/android/os/FabricatedOverlayInternalEntry.aidl
@@ -28,4 +28,5 @@
     @nullable @utf8InCpp String configuration;
     long binaryDataOffset;
     long binaryDataSize;
+    boolean isNinePatch;
 }
\ No newline at end of file
diff --git a/cmds/idmap2/include/idmap2/FabricatedOverlay.h b/cmds/idmap2/include/idmap2/FabricatedOverlay.h
index 1e7d4c2..bfcd4b9 100644
--- a/cmds/idmap2/include/idmap2/FabricatedOverlay.h
+++ b/cmds/idmap2/include/idmap2/FabricatedOverlay.h
@@ -19,6 +19,8 @@
 
 #include <libidmap2/proto/fabricated_v1.pb.h>
 
+#include "androidfw/Streams.h"
+
 #include <istream>
 #include <map>
 #include <memory>
@@ -51,7 +53,8 @@
                               std::optional<android::base::borrowed_fd>&& binary_value,
                               off64_t data_binary_offset,
                               size_t data_binary_size,
-                              const std::string& configuration);
+                              const std::string& configuration,
+                              bool nine_patch);
 
     inline Builder& setFrroPath(std::string frro_path) {
       frro_path_ = std::move(frro_path);
@@ -70,6 +73,7 @@
       off64_t data_binary_offset;
       size_t data_binary_size;
       std::string configuration;
+      bool nine_patch;
     };
 
     std::string package_name_;
@@ -81,7 +85,7 @@
   };
 
   struct BinaryData {
-    android::base::borrowed_fd file_descriptor;
+    std::unique_ptr<android::InputStream> input_stream;
     off64_t offset;
     size_t size;
   };
diff --git a/cmds/idmap2/include/idmap2/ResourceUtils.h b/cmds/idmap2/include/idmap2/ResourceUtils.h
index d4490ef4..9e463c9 100644
--- a/cmds/idmap2/include/idmap2/ResourceUtils.h
+++ b/cmds/idmap2/include/idmap2/ResourceUtils.h
@@ -45,6 +45,7 @@
   std::optional<android::base::borrowed_fd> data_binary_value;
   off64_t data_binary_offset;
   size_t data_binary_size;
+  bool nine_patch;
 };
 
 struct TargetValueWithConfig {
diff --git a/cmds/idmap2/libidmap2/FabricatedOverlay.cpp b/cmds/idmap2/libidmap2/FabricatedOverlay.cpp
index 47daf23..16bb896 100644
--- a/cmds/idmap2/libidmap2/FabricatedOverlay.cpp
+++ b/cmds/idmap2/libidmap2/FabricatedOverlay.cpp
@@ -20,8 +20,16 @@
 #include <sys/types.h>  // umask
 
 #include <android-base/file.h>
+#include <android-base/strings.h>
+#include <androidfw/BigBuffer.h>
+#include <androidfw/BigBufferStream.h>
+#include <androidfw/FileStream.h>
+#include <androidfw/Image.h>
+#include <androidfw/Png.h>
 #include <androidfw/ResourceUtils.h>
+#include <androidfw/StringPiece.h>
 #include <androidfw/StringPool.h>
+#include <androidfw/Streams.h>
 #include <google/protobuf/io/coded_stream.h>
 #include <google/protobuf/io/zero_copy_stream_impl.h>
 #include <utils/ByteOrder.h>
@@ -32,9 +40,9 @@
 #include <memory>
 #include <string>
 #include <utility>
+#include <sys/utsname.h>
 
 namespace android::idmap2 {
-
 constexpr auto kBufferSize = 1024;
 
 namespace {
@@ -81,7 +89,7 @@
     const std::string& resource_name, uint8_t data_type, uint32_t data_value,
     const std::string& configuration) {
   entries_.emplace_back(
-      Entry{resource_name, data_type, data_value, "", std::nullopt, 0, 0, configuration});
+      Entry{resource_name, data_type, data_value, "", std::nullopt, 0, 0, configuration, false});
   return *this;
 }
 
@@ -89,18 +97,90 @@
     const std::string& resource_name, uint8_t data_type, const std::string& data_string_value,
     const std::string& configuration) {
   entries_.emplace_back(
-      Entry{resource_name, data_type, 0, data_string_value, std::nullopt, 0, 0, configuration});
+      Entry{resource_name,
+            data_type,
+            0,
+            data_string_value,
+            std::nullopt,
+            0,
+            0,
+            configuration,
+            false});
   return *this;
 }
 
 FabricatedOverlay::Builder& FabricatedOverlay::Builder::SetResourceValue(
     const std::string& resource_name, std::optional<android::base::borrowed_fd>&& binary_value,
-    off64_t data_binary_offset, size_t data_binary_size, const std::string& configuration) {
+    off64_t data_binary_offset, size_t data_binary_size, const std::string& configuration,
+    bool nine_patch) {
   entries_.emplace_back(Entry{resource_name, 0, 0, "", binary_value,
-                              data_binary_offset, data_binary_size, configuration});
+                              data_binary_offset, data_binary_size, configuration, nine_patch});
   return *this;
 }
 
+static Result<FabricatedOverlay::BinaryData> buildBinaryData(
+        pb::ResourceValue* pb_value, const TargetValue &value) {
+  pb_value->set_data_type(Res_value::TYPE_STRING);
+  size_t binary_size;
+  off64_t binary_offset;
+  std::unique_ptr<android::InputStream> binary_stream;
+
+  if (value.nine_patch) {
+    std::string file_contents;
+    file_contents.resize(value.data_binary_size);
+    if (!base::ReadFullyAtOffset(value.data_binary_value->get(), file_contents.data(),
+                                 value.data_binary_size, value.data_binary_offset)) {
+      return Error("Failed to read binary file data.");
+    }
+    const StringPiece content(file_contents.c_str(), file_contents.size());
+    android::PngChunkFilter png_chunk_filter(content);
+    android::AndroidLogDiagnostics diag;
+    auto png = android::ReadPng(&png_chunk_filter, &diag);
+    if (!png) {
+      return Error("Error opening file as png");
+    }
+
+    std::string err;
+    std::unique_ptr<NinePatch> nine_patch = NinePatch::Create(png->rows.get(),
+                                                              png->width, png->height,
+                                                              &err);
+    if (!nine_patch) {
+      return Error("%s", err.c_str());
+    }
+
+    png->width -= 2;
+    png->height -= 2;
+    memmove(png->rows.get(), png->rows.get() + 1, png->height * sizeof(uint8_t**));
+    for (int32_t h = 0; h < png->height; h++) {
+      memmove(png->rows[h], png->rows[h] + 4, png->width * 4);
+    }
+
+    android::BigBuffer buffer(value.data_binary_size);
+    android::BigBufferOutputStream buffer_output_stream(&buffer);
+    if (!android::WritePng(png.get(), nine_patch.get(), &buffer_output_stream, {},
+                           &diag, false)) {
+      return Error("Error writing frro png");
+    }
+
+    binary_size = buffer.size();
+    binary_offset = 0;
+    android::BigBufferInputStream *buffer_input_stream
+            = new android::BigBufferInputStream(std::move(buffer));
+    binary_stream.reset(buffer_input_stream);
+  } else {
+    binary_size = value.data_binary_size;
+    binary_offset = value.data_binary_offset;
+    android::FileInputStream *fis
+            = new android::FileInputStream(value.data_binary_value.value());
+    binary_stream.reset(fis);
+  }
+
+  return FabricatedOverlay::BinaryData{
+          std::move(binary_stream),
+          binary_offset,
+          binary_size};
+}
+
 Result<FabricatedOverlay> FabricatedOverlay::Builder::Build() {
   using ConfigMap = std::map<std::string, TargetValue, std::less<>>;
   using EntryMap = std::map<std::string, ConfigMap, std::less<>>;
@@ -150,7 +230,8 @@
 
     value->second = TargetValue{res_entry.data_type, res_entry.data_value,
                                 res_entry.data_string_value, res_entry.data_binary_value,
-                                res_entry.data_binary_offset, res_entry.data_binary_size};
+                                res_entry.data_binary_offset, res_entry.data_binary_size,
+                                res_entry.nine_patch};
   }
 
   pb::FabricatedOverlay overlay_pb;
@@ -183,18 +264,20 @@
             auto ref = string_pool.MakeRef(value.second.data_string_value);
             pb_value->set_data_value(ref.index());
           } else if (value.second.data_binary_value.has_value()) {
-              pb_value->set_data_type(Res_value::TYPE_STRING);
-              std::string uri
-                  = StringPrintf("frro:/%s?offset=%d&size=%d", frro_path_.c_str(),
-                                 static_cast<int> (FRRO_HEADER_SIZE + total_binary_bytes),
-                                 static_cast<int> (value.second.data_binary_size));
-              total_binary_bytes += value.second.data_binary_size;
-              binary_files.emplace_back(FabricatedOverlay::BinaryData{
-                  value.second.data_binary_value->get(),
-                  value.second.data_binary_offset,
-                  value.second.data_binary_size});
-              auto ref = string_pool.MakeRef(std::move(uri));
-              pb_value->set_data_value(ref.index());
+            auto binary_data = buildBinaryData(pb_value, value.second);
+            if (!binary_data) {
+              return binary_data.GetError();
+            }
+            pb_value->set_data_type(Res_value::TYPE_STRING);
+
+            std::string uri
+                = StringPrintf("frro:/%s?offset=%d&size=%d", frro_path_.c_str(),
+                               static_cast<int> (FRRO_HEADER_SIZE + total_binary_bytes),
+                               static_cast<int> (binary_data->size));
+            total_binary_bytes += binary_data->size;
+            binary_files.emplace_back(std::move(*binary_data));
+            auto ref = string_pool.MakeRef(std::move(uri));
+            pb_value->set_data_value(ref.index());
           } else {
             pb_value->set_data_value(value.second.data_value);
           }
@@ -311,9 +394,9 @@
   Write32(stream, (*data)->pb_crc);
   Write32(stream, total_binary_bytes_);
   std::string file_contents;
-  for (const FabricatedOverlay::BinaryData fd : binary_files_) {
-    file_contents.resize(fd.size);
-    if (!ReadFullyAtOffset(fd.file_descriptor, file_contents.data(), fd.size, fd.offset)) {
+  for (const FabricatedOverlay::BinaryData& bd : binary_files_) {
+    file_contents.resize(bd.size);
+    if (!bd.input_stream->ReadFullyAtOffset(file_contents.data(), bd.size, bd.offset)) {
       return Error("Failed to read binary file data.");
     }
     stream.write(file_contents.data(), file_contents.length());
diff --git a/cmds/idmap2/self_targeting/SelfTargeting.cpp b/cmds/idmap2/self_targeting/SelfTargeting.cpp
index c7f5cf3..7f9c468 100644
--- a/cmds/idmap2/self_targeting/SelfTargeting.cpp
+++ b/cmds/idmap2/self_targeting/SelfTargeting.cpp
@@ -53,7 +53,7 @@
         if (entry_params.data_binary_value.has_value()) {
             builder.SetResourceValue(entry_params.resource_name, *entry_params.data_binary_value,
                                      entry_params.binary_data_offset, entry_params.binary_data_size,
-                                     entry_params.configuration);
+                                     entry_params.configuration, entry_params.nine_patch);
         } else  if (dataType >= Res_value::TYPE_FIRST_INT && dataType <= Res_value::TYPE_LAST_INT) {
            builder.SetResourceValue(entry_params.resource_name, dataType,
                                     entry_params.data_value, entry_params.configuration);
diff --git a/cmds/idmap2/tests/FabricatedOverlayTests.cpp b/cmds/idmap2/tests/FabricatedOverlayTests.cpp
index b460bb3..6b1c7e8 100644
--- a/cmds/idmap2/tests/FabricatedOverlayTests.cpp
+++ b/cmds/idmap2/tests/FabricatedOverlayTests.cpp
@@ -59,7 +59,7 @@
               Res_value::TYPE_STRING,
               "foobar",
               "en-rUS-normal-xxhdpi-v21")
-          .SetResourceValue("com.example.target:drawable/dr1", fd, 0, 8341, "port-xxhdpi-v7")
+          .SetResourceValue("com.example.target:drawable/dr1", fd, 0, 8341, "port-xxhdpi-v7", false)
           .setFrroPath("/foo/bar/biz.frro")
           .Build();
   ASSERT_TRUE(overlay);
diff --git a/cmds/idmap2/tests/IdmapTests.cpp b/cmds/idmap2/tests/IdmapTests.cpp
index a3448fd..a384305 100644
--- a/cmds/idmap2/tests/IdmapTests.cpp
+++ b/cmds/idmap2/tests/IdmapTests.cpp
@@ -269,7 +269,7 @@
                   .SetResourceValue("integer/int1", Res_value::TYPE_INT_DEC, 2U, "land-xxhdpi-v7")
                   .SetResourceValue("string/str1", Res_value::TYPE_REFERENCE, 0x7f010000, "land")
                   .SetResourceValue("string/str2", Res_value::TYPE_STRING, "foobar", "xxhdpi-v7")
-                  .SetResourceValue("drawable/dr1", fd, 0, 8341, "port-xxhdpi-v7")
+                  .SetResourceValue("drawable/dr1", fd, 0, 8341, "port-xxhdpi-v7", false)
                   .setFrroPath("/foo/bar/biz.frro")
                   .Build();
 
diff --git a/cmds/idmap2/tests/ResourceMappingTests.cpp b/cmds/idmap2/tests/ResourceMappingTests.cpp
index 40f98c2..db44c23 100644
--- a/cmds/idmap2/tests/ResourceMappingTests.cpp
+++ b/cmds/idmap2/tests/ResourceMappingTests.cpp
@@ -212,7 +212,7 @@
                   .SetResourceValue("integer/int1", Res_value::TYPE_INT_DEC, 2U, "")
                   .SetResourceValue("string/str1", Res_value::TYPE_REFERENCE, 0x7f010000, "")
                   .SetResourceValue("string/str2", Res_value::TYPE_STRING, "foobar", "")
-                  .SetResourceValue("drawable/dr1", fd, 0, 8341, "")
+                  .SetResourceValue("drawable/dr1", fd, 0, 8341, "", false)
                   .setFrroPath("/foo/bar/biz.frro")
                   .Build();
 
diff --git a/core/api/current.txt b/core/api/current.txt
index 17c11a8..7731fac 100644
--- a/core/api/current.txt
+++ b/core/api/current.txt
@@ -11787,6 +11787,7 @@
   public class FabricatedOverlay {
     ctor public FabricatedOverlay(@NonNull String, @NonNull String);
     method @NonNull public android.content.om.OverlayIdentifier getIdentifier();
+    method @FlaggedApi("android.content.res.nine_patch_frro") @NonNull public void setNinePatchResourceValue(@NonNull String, @NonNull android.os.ParcelFileDescriptor, @Nullable String);
     method @NonNull public void setResourceValue(@NonNull String, @IntRange(from=android.util.TypedValue.TYPE_FIRST_INT, to=android.util.TypedValue.TYPE_LAST_INT) int, int, @Nullable String);
     method @NonNull public void setResourceValue(@NonNull String, int, @NonNull String, @Nullable String);
     method @NonNull public void setResourceValue(@NonNull String, @NonNull android.os.ParcelFileDescriptor, @Nullable String);
@@ -13044,7 +13045,7 @@
     field public static final int MATCH_DIRECT_BOOT_UNAWARE = 262144; // 0x40000
     field public static final int MATCH_DISABLED_COMPONENTS = 512; // 0x200
     field public static final int MATCH_DISABLED_UNTIL_USED_COMPONENTS = 32768; // 0x8000
-    field @FlaggedApi("android.content.pm.quarantined_enabled") public static final long MATCH_QUARANTINED_COMPONENTS = 4294967296L; // 0x100000000L
+    field @FlaggedApi("android.content.pm.quarantined_enabled") public static final long MATCH_QUARANTINED_COMPONENTS = 8589934592L; // 0x200000000L
     field public static final int MATCH_SYSTEM_ONLY = 1048576; // 0x100000
     field public static final int MATCH_UNINSTALLED_PACKAGES = 8192; // 0x2000
     field public static final long MAXIMUM_VERIFICATION_TIMEOUT = 3600000L; // 0x36ee80L
@@ -18977,6 +18978,7 @@
     field public static final int EXTENSION_AUTOMATIC = 0; // 0x0
     field @Deprecated public static final int EXTENSION_BEAUTY = 1; // 0x1
     field public static final int EXTENSION_BOKEH = 2; // 0x2
+    field @FlaggedApi("com.android.internal.camera.flags.concert_mode") public static final int EXTENSION_EYES_FREE_VIDEOGRAPHY = 5; // 0x5
     field public static final int EXTENSION_FACE_RETOUCH = 1; // 0x1
     field public static final int EXTENSION_HDR = 3; // 0x3
     field public static final int EXTENSION_NIGHT = 4; // 0x4
@@ -24167,6 +24169,7 @@
     method @Nullable public android.media.MediaRouter2.RoutingController getController(@NonNull String);
     method @NonNull public java.util.List<android.media.MediaRouter2.RoutingController> getControllers();
     method @NonNull public static android.media.MediaRouter2 getInstance(@NonNull android.content.Context);
+    method @FlaggedApi("com.android.media.flags.enable_cross_user_routing_in_media_router2") @NonNull @RequiresPermission(anyOf={android.Manifest.permission.MEDIA_CONTENT_CONTROL, android.Manifest.permission.MEDIA_ROUTING_CONTROL}) public static android.media.MediaRouter2 getInstance(@NonNull android.content.Context, @NonNull android.os.Looper, @NonNull String, @NonNull android.os.UserHandle);
     method @FlaggedApi("com.android.media.flags.enable_rlp_callbacks_in_media_router2") @Nullable public android.media.RouteListingPreference getRouteListingPreference();
     method @NonNull public java.util.List<android.media.MediaRoute2Info> getRoutes();
     method @NonNull public android.media.MediaRouter2.RoutingController getSystemController();
diff --git a/core/api/system-current.txt b/core/api/system-current.txt
index e0dfd39..dc39bea 100644
--- a/core/api/system-current.txt
+++ b/core/api/system-current.txt
@@ -4480,6 +4480,94 @@
 
 }
 
+package android.hardware.camera2.extension {
+
+  @FlaggedApi("com.android.internal.camera.flags.concert_mode") public abstract class AdvancedExtender {
+    ctor @FlaggedApi("com.android.internal.camera.flags.concert_mode") protected AdvancedExtender(@NonNull android.hardware.camera2.CameraManager);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") @NonNull public abstract java.util.List<android.hardware.camera2.CaptureRequest.Key> getAvailableCaptureRequestKeys(@NonNull String);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") @NonNull public abstract java.util.List<android.hardware.camera2.CaptureResult.Key> getAvailableCaptureResultKeys(@NonNull String);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public long getMetadataVendorId(@NonNull String);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") @NonNull public abstract android.hardware.camera2.extension.SessionProcessor getSessionProcessor();
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") @NonNull public abstract java.util.Map<java.lang.Integer,java.util.List<android.util.Size>> getSupportedCaptureOutputResolutions(@NonNull String);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") @NonNull public abstract java.util.Map<java.lang.Integer,java.util.List<android.util.Size>> getSupportedPreviewOutputResolutions(@NonNull String);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public abstract void init(@NonNull String, @NonNull android.hardware.camera2.extension.CharacteristicsMap);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public abstract boolean isExtensionAvailable(@NonNull String, @NonNull android.hardware.camera2.extension.CharacteristicsMap);
+  }
+
+  @FlaggedApi("com.android.internal.camera.flags.concert_mode") public abstract class CameraExtensionService extends android.app.Service {
+    ctor @FlaggedApi("com.android.internal.camera.flags.concert_mode") protected CameraExtensionService();
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") @NonNull public android.os.IBinder onBind(@Nullable android.content.Intent);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") @NonNull public abstract android.hardware.camera2.extension.AdvancedExtender onInitializeAdvancedExtension(int);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public abstract boolean onRegisterClient(@NonNull android.os.IBinder);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public abstract void onUnregisterClient(@NonNull android.os.IBinder);
+  }
+
+  @FlaggedApi("com.android.internal.camera.flags.concert_mode") public final class CameraOutputSurface {
+    ctor @FlaggedApi("com.android.internal.camera.flags.concert_mode") public CameraOutputSurface(@NonNull android.view.Surface, @Nullable android.util.Size);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public int getImageFormat();
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") @Nullable public android.util.Size getSize();
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") @Nullable public android.view.Surface getSurface();
+  }
+
+  @FlaggedApi("com.android.internal.camera.flags.concert_mode") public class CharacteristicsMap {
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") @Nullable public android.hardware.camera2.CameraCharacteristics get(@NonNull String);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") @NonNull public java.util.Set<java.lang.String> getCameraIds();
+  }
+
+  @FlaggedApi("com.android.internal.camera.flags.concert_mode") public class ExtensionConfiguration {
+    ctor @FlaggedApi("com.android.internal.camera.flags.concert_mode") public ExtensionConfiguration(int, int, @NonNull java.util.List<android.hardware.camera2.extension.ExtensionOutputConfiguration>, @Nullable android.hardware.camera2.CaptureRequest);
+  }
+
+  @FlaggedApi("com.android.internal.camera.flags.concert_mode") public class ExtensionOutputConfiguration {
+    ctor @FlaggedApi("com.android.internal.camera.flags.concert_mode") public ExtensionOutputConfiguration(@NonNull java.util.List<android.hardware.camera2.extension.CameraOutputSurface>, int, @Nullable String, int);
+  }
+
+  @FlaggedApi("com.android.internal.camera.flags.concert_mode") public final class RequestProcessor {
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public void abortCaptures();
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public int setRepeating(@NonNull android.hardware.camera2.extension.RequestProcessor.Request, @Nullable java.util.concurrent.Executor, @NonNull android.hardware.camera2.extension.RequestProcessor.RequestCallback);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public void stopRepeating();
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public int submit(@NonNull android.hardware.camera2.extension.RequestProcessor.Request, @Nullable java.util.concurrent.Executor, @NonNull android.hardware.camera2.extension.RequestProcessor.RequestCallback);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public int submitBurst(@NonNull java.util.List<android.hardware.camera2.extension.RequestProcessor.Request>, @Nullable java.util.concurrent.Executor, @NonNull android.hardware.camera2.extension.RequestProcessor.RequestCallback);
+  }
+
+  @FlaggedApi("com.android.internal.camera.flags.concert_mode") public static final class RequestProcessor.Request {
+    ctor @FlaggedApi("com.android.internal.camera.flags.concert_mode") public RequestProcessor.Request(@NonNull java.util.List<java.lang.Integer>, @NonNull java.util.List<android.util.Pair<android.hardware.camera2.CaptureRequest.Key,java.lang.Object>>, int);
+  }
+
+  @FlaggedApi("com.android.internal.camera.flags.concert_mode") public static interface RequestProcessor.RequestCallback {
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public void onCaptureBufferLost(@NonNull android.hardware.camera2.extension.RequestProcessor.Request, long, int);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public void onCaptureCompleted(@NonNull android.hardware.camera2.extension.RequestProcessor.Request, @Nullable android.hardware.camera2.TotalCaptureResult);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public void onCaptureFailed(@NonNull android.hardware.camera2.extension.RequestProcessor.Request, @NonNull android.hardware.camera2.CaptureFailure);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public void onCaptureProgressed(@NonNull android.hardware.camera2.extension.RequestProcessor.Request, @NonNull android.hardware.camera2.CaptureResult);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public void onCaptureSequenceAborted(int);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public void onCaptureSequenceCompleted(int, long);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public void onCaptureStarted(@NonNull android.hardware.camera2.extension.RequestProcessor.Request, long, long);
+  }
+
+  @FlaggedApi("com.android.internal.camera.flags.concert_mode") public abstract class SessionProcessor {
+    ctor @FlaggedApi("com.android.internal.camera.flags.concert_mode") protected SessionProcessor();
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public abstract void deInitSession(@NonNull android.os.IBinder);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") @NonNull public abstract android.hardware.camera2.extension.ExtensionConfiguration initSession(@NonNull android.os.IBinder, @NonNull String, @NonNull android.hardware.camera2.extension.CharacteristicsMap, @NonNull android.hardware.camera2.extension.CameraOutputSurface, @NonNull android.hardware.camera2.extension.CameraOutputSurface);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public abstract void onCaptureSessionEnd();
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public abstract void onCaptureSessionStart(@NonNull android.hardware.camera2.extension.RequestProcessor, @NonNull String);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public abstract void setParameters(@NonNull android.hardware.camera2.CaptureRequest);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public abstract int startCapture(@Nullable java.util.concurrent.Executor, @NonNull android.hardware.camera2.extension.SessionProcessor.CaptureCallback);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public abstract int startRepeating(@Nullable java.util.concurrent.Executor, @NonNull android.hardware.camera2.extension.SessionProcessor.CaptureCallback);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public abstract int startTrigger(@NonNull android.hardware.camera2.CaptureRequest, @Nullable java.util.concurrent.Executor, @NonNull android.hardware.camera2.extension.SessionProcessor.CaptureCallback);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public abstract void stopRepeating();
+  }
+
+  @FlaggedApi("com.android.internal.camera.flags.concert_mode") public static interface SessionProcessor.CaptureCallback {
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public void onCaptureCompleted(long, int, @NonNull android.hardware.camera2.CaptureResult);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public void onCaptureFailed(int);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public void onCaptureProcessStarted(int);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public void onCaptureSequenceAborted(int);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public void onCaptureSequenceCompleted(int);
+    method @FlaggedApi("com.android.internal.camera.flags.concert_mode") public void onCaptureStarted(int, long);
+  }
+
+}
+
 package android.hardware.camera2.params {
 
   public final class OutputConfiguration implements android.os.Parcelable {
diff --git a/core/api/test-current.txt b/core/api/test-current.txt
index 71a05a9..98a78cf 100644
--- a/core/api/test-current.txt
+++ b/core/api/test-current.txt
@@ -1162,6 +1162,13 @@
     field public static final int SHOW_IN_LAUNCHER_WITH_PARENT = 0; // 0x0
   }
 
+  public static final class UserProperties.Builder {
+    ctor public UserProperties.Builder();
+    method @NonNull public android.content.pm.UserProperties build();
+    method @NonNull public android.content.pm.UserProperties.Builder setShowInQuietMode(int);
+    method @NonNull public android.content.pm.UserProperties.Builder setShowInSharingSurfaces(int);
+  }
+
 }
 
 package android.content.res {
@@ -1597,6 +1604,10 @@
     method public void restoreDozeSettings(int);
   }
 
+  public final class ColorDisplayManager {
+    method @FlaggedApi("android.app.modes_api") @RequiresPermission(android.Manifest.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS) public boolean isSaturationActivated();
+  }
+
   public final class DisplayManager {
     method public boolean areUserDisabledHdrTypesAllowed();
     method @RequiresPermission(android.Manifest.permission.MODIFY_USER_PREFERRED_DISPLAY_MODE) public void clearGlobalUserPreferredDisplayMode();
diff --git a/core/java/android/accessibilityservice/AccessibilityService.java b/core/java/android/accessibilityservice/AccessibilityService.java
index 1000612..2a7dbab 100644
--- a/core/java/android/accessibilityservice/AccessibilityService.java
+++ b/core/java/android/accessibilityservice/AccessibilityService.java
@@ -566,8 +566,10 @@
     public static final int GLOBAL_ACTION_TAKE_SCREENSHOT = 9;
 
     /**
-     * Action to send the KEYCODE_HEADSETHOOK KeyEvent, which is used to answer/hang up calls and
-     * play/stop media
+     * Action to send the KEYCODE_HEADSETHOOK KeyEvent, which is used to answer and hang up calls
+     * and play and stop media. Calling takes priority. If there is an incoming call,
+     * this action can be used to answer that call, and if there is an ongoing call, to hang up on
+     * that call.
      */
     public static final int GLOBAL_ACTION_KEYCODE_HEADSETHOOK = 10;
 
diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java
index 8af1216..adaaee2 100644
--- a/core/java/android/app/ActivityThread.java
+++ b/core/java/android/app/ActivityThread.java
@@ -2292,7 +2292,8 @@
                     case DUMP_HEAP: return "DUMP_HEAP";
                     case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
                     case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
-                    case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
+                    case UPDATE_PACKAGE_COMPATIBILITY_INFO:
+                        return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
                     case DUMP_PROVIDER: return "DUMP_PROVIDER";
                     case UNSTABLE_PROVIDER_DIED: return "UNSTABLE_PROVIDER_DIED";
                     case REQUEST_ASSIST_CONTEXT_EXTRAS: return "REQUEST_ASSIST_CONTEXT_EXTRAS";
@@ -3804,7 +3805,7 @@
 
         boolean isSandboxActivityContext =
                 sandboxActivitySdkBasedContext()
-                        && SdkSandboxActivityAuthority.isSdkSandboxActivity(
+                        && SdkSandboxActivityAuthority.isSdkSandboxActivityIntent(
                                 mSystemContext, r.intent);
         boolean isSandboxedSdkContextUsed = false;
         ContextImpl activityBaseContext;
diff --git a/core/java/android/app/WallpaperManager.java b/core/java/android/app/WallpaperManager.java
index d660078..820ff3e 100644
--- a/core/java/android/app/WallpaperManager.java
+++ b/core/java/android/app/WallpaperManager.java
@@ -21,6 +21,8 @@
 import static android.content.pm.PackageManager.PERMISSION_GRANTED;
 import static android.os.ParcelFileDescriptor.MODE_READ_ONLY;
 
+import static com.android.window.flags.Flags.multiCrop;
+
 import android.annotation.FloatRange;
 import android.annotation.IntDef;
 import android.annotation.NonNull;
@@ -857,8 +859,7 @@
      */
     public static boolean isMultiCropEnabled() {
         if (sGlobals == null) {
-            sIsMultiCropEnabled = SystemProperties.getBoolean(
-                    "persist.wm.debug.wallpaper_multi_crop", false);
+            sIsMultiCropEnabled = multiCrop();
         }
         if (sIsMultiCropEnabled == null) {
             try {
diff --git a/core/java/android/app/admin/DevicePolicyManager.java b/core/java/android/app/admin/DevicePolicyManager.java
index fc3a906..1e538c5 100644
--- a/core/java/android/app/admin/DevicePolicyManager.java
+++ b/core/java/android/app/admin/DevicePolicyManager.java
@@ -9146,7 +9146,7 @@
     @UserHandleAware(enabledSinceTargetSdkVersion = Build.VERSION_CODES.VANILLA_ICE_CREAM)
     public boolean isDeviceOwnerApp(String packageName) {
         throwIfParentInstance("isDeviceOwnerApp");
-        if (android.permission.flags.Flags.roleControllerInSystemServer()
+        if (android.permission.flags.Flags.systemServerRoleControllerEnabled()
                 && CompatChanges.isChangeEnabled(IS_DEVICE_OWNER_USER_AWARE)) {
             return isDeviceOwnerAppOnContextUser(packageName);
         }
diff --git a/core/java/android/content/Intent.java b/core/java/android/content/Intent.java
index 38bcfa2..23a5d4d 100644
--- a/core/java/android/content/Intent.java
+++ b/core/java/android/content/Intent.java
@@ -2805,7 +2805,7 @@
      * and the package in the stopped state cannot self-start for any reason unless there's an
      * explicit request to start a component in the package. The {@link #ACTION_PACKAGE_UNSTOPPED}
      * broadcast is sent when such an explicit process start occurs and the package is taken
-     * out of the stopped state.
+     * out of the stopped state. The data contains the name of the package.
      * </p>
      * <ul>
      * <li> {@link #EXTRA_UID} containing the integer uid assigned to the package.
@@ -12606,8 +12606,11 @@
     }
 
     /**
-     * @deprecated Use {@link SdkSandboxActivityAuthority#isSdkSandboxActivity} instead.
+     * @deprecated Use {@link SdkSandboxActivityAuthority#isSdkSandboxActivityIntent} instead.
      * Once the other API is finalized this method will be removed.
+     *
+     * TODO(b/300059435): remove as part of the cleanup.
+     *
      * @hide
      */
     @Deprecated
diff --git a/core/java/android/content/om/FabricatedOverlay.java b/core/java/android/content/om/FabricatedOverlay.java
index df2d7e7..40ffb0f 100644
--- a/core/java/android/content/om/FabricatedOverlay.java
+++ b/core/java/android/content/om/FabricatedOverlay.java
@@ -281,8 +281,8 @@
                 @NonNull ParcelFileDescriptor value,
                 @Nullable String configuration) {
             ensureValidResourceName(resourceName);
-            mEntries.add(
-                    generateFabricatedOverlayInternalEntry(resourceName, value, configuration));
+            mEntries.add(generateFabricatedOverlayInternalEntry(
+                    resourceName, value, configuration, false));
             return this;
         }
 
@@ -361,6 +361,16 @@
     }
 
     /**
+     * Set the package that owns the overlay
+     *
+     * @param owningPackage the package that should own the overlay.
+     * @hide
+     */
+    public void setOwningPackage(@NonNull String owningPackage) {
+        mOverlay.packageName = owningPackage;
+    }
+
+    /**
      * Set the target overlayable name of the overlay
      *
      * The target package defines may define several overlayables. The {@link FabricatedOverlay}
@@ -442,13 +452,14 @@
     @NonNull
     private static FabricatedOverlayInternalEntry generateFabricatedOverlayInternalEntry(
             @NonNull String resourceName, @NonNull ParcelFileDescriptor parcelFileDescriptor,
-            @Nullable String configuration) {
+            @Nullable String configuration, boolean isNinePatch) {
         final FabricatedOverlayInternalEntry entry = new FabricatedOverlayInternalEntry();
         entry.resourceName = resourceName;
         entry.binaryData = Objects.requireNonNull(parcelFileDescriptor);
         entry.configuration = configuration;
         entry.binaryDataOffset = 0;
         entry.binaryDataSize = parcelFileDescriptor.getStatSize();
+        entry.isNinePatch = isNinePatch;
         return entry;
     }
 
@@ -534,7 +545,26 @@
             @Nullable String configuration) {
         ensureValidResourceName(resourceName);
         mOverlay.entries.add(
-                generateFabricatedOverlayInternalEntry(resourceName, value, configuration));
+                generateFabricatedOverlayInternalEntry(resourceName, value, configuration, false));
+    }
+
+    /**
+     * Sets the resource value in the fabricated overlay from a nine patch.
+     *
+     * @param resourceName name of the target resource to overlay (in the form
+     *     [package]:type/entry)
+     * @param value the file descriptor whose contents are the value of the frro
+     * @param configuration The string representation of the config this overlay is enabled for
+     */
+    @NonNull
+    @FlaggedApi(android.content.res.Flags.FLAG_NINE_PATCH_FRRO)
+    public void setNinePatchResourceValue(
+            @NonNull String resourceName,
+            @NonNull ParcelFileDescriptor value,
+            @Nullable String configuration) {
+        ensureValidResourceName(resourceName);
+        mOverlay.entries.add(
+                generateFabricatedOverlayInternalEntry(resourceName, value, configuration, true));
     }
 
     /**
diff --git a/core/java/android/content/pm/ActivityInfo.java b/core/java/android/content/pm/ActivityInfo.java
index 323592c..d13d962 100644
--- a/core/java/android/content/pm/ActivityInfo.java
+++ b/core/java/android/content/pm/ActivityInfo.java
@@ -55,6 +55,7 @@
  * from the AndroidManifest.xml's &lt;activity&gt; and
  * &lt;receiver&gt; tags.
  */
+@android.ravenwood.annotation.RavenwoodKeepWholeClass
 public class ActivityInfo extends ComponentInfo implements Parcelable {
 
     private static final Parcelling.BuiltIn.ForStringSet sForStringSet =
diff --git a/core/java/android/content/pm/ApplicationInfo.java b/core/java/android/content/pm/ApplicationInfo.java
index f0b99f1..16a80e9 100644
--- a/core/java/android/content/pm/ApplicationInfo.java
+++ b/core/java/android/content/pm/ApplicationInfo.java
@@ -66,6 +66,7 @@
  * corresponds to information collected from the AndroidManifest.xml's
  * &lt;application&gt; tag.
  */
+@android.ravenwood.annotation.RavenwoodKeepWholeClass
 public class ApplicationInfo extends PackageItemInfo implements Parcelable {
     private static final ForBoolean sForBoolean = Parcelling.Cache.getOrCreate(ForBoolean.class);
     private static final Parcelling.BuiltIn.ForStringSet sForStringSet =
@@ -1386,6 +1387,7 @@
      *
      * @see #category
      */
+    @android.ravenwood.annotation.RavenwoodThrow(blockedBy = android.content.res.Resources.class)
     public static CharSequence getCategoryTitle(Context context, @Category int category) {
         switch (category) {
             case ApplicationInfo.CATEGORY_GAME:
@@ -2187,6 +2189,7 @@
      * @return Returns a CharSequence containing the application's description.
      * If there is no description, null is returned.
      */
+    @android.ravenwood.annotation.RavenwoodThrow(blockedBy = android.content.res.Resources.class)
     public CharSequence loadDescription(PackageManager pm) {
         if (descriptionRes != 0) {
             CharSequence label = pm.getText(packageName, descriptionRes, this);
@@ -2222,6 +2225,7 @@
     }
 
     /** {@hide} */
+    @android.ravenwood.annotation.RavenwoodThrow(blockedBy = Environment.class)
     public void initForUser(int userId) {
         uid = UserHandle.getUid(userId, UserHandle.getAppId(uid));
 
@@ -2414,6 +2418,7 @@
      * @hide
      */
     @Override
+    @android.ravenwood.annotation.RavenwoodThrow(blockedBy = android.content.res.Resources.class)
     public Drawable loadDefaultIcon(PackageManager pm) {
         if ((flags & FLAG_EXTERNAL_STORAGE) != 0
                 && isPackageUnavailable(pm)) {
@@ -2424,6 +2429,7 @@
     }
 
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
+    @android.ravenwood.annotation.RavenwoodThrow(blockedBy = PackageManager.class)
     private boolean isPackageUnavailable(PackageManager pm) {
         try {
             return pm.getPackageInfo(packageName, 0) == null;
diff --git a/core/java/android/content/pm/ComponentInfo.java b/core/java/android/content/pm/ComponentInfo.java
index 42847c8..ff48ffa 100644
--- a/core/java/android/content/pm/ComponentInfo.java
+++ b/core/java/android/content/pm/ComponentInfo.java
@@ -37,6 +37,7 @@
  * implement Parcelable, but does provide convenience methods to assist
  * in the implementation of Parcelable in subclasses.
  */
+@android.ravenwood.annotation.RavenwoodKeepWholeClass
 public class ComponentInfo extends PackageItemInfo {
     /**
      * Global information about the application/package this component is a
@@ -258,6 +259,7 @@
      * @hide
      */
     @Override
+    @android.ravenwood.annotation.RavenwoodThrow(blockedBy = android.content.res.Resources.class)
     public Drawable loadDefaultIcon(PackageManager pm) {
         return applicationInfo.loadIcon(pm);
     }
@@ -265,6 +267,7 @@
     /**
      * @hide
      */
+    @android.ravenwood.annotation.RavenwoodThrow(blockedBy = android.content.res.Resources.class)
     @Override protected Drawable loadDefaultBanner(PackageManager pm) {
         return applicationInfo.loadBanner(pm);
     }
@@ -273,6 +276,7 @@
      * @hide
      */
     @Override
+    @android.ravenwood.annotation.RavenwoodThrow(blockedBy = android.content.res.Resources.class)
     protected Drawable loadDefaultLogo(PackageManager pm) {
         return applicationInfo.loadLogo(pm);
     }
diff --git a/core/java/android/content/pm/PackageInfo.java b/core/java/android/content/pm/PackageInfo.java
index 5736a6d..5dee65b 100644
--- a/core/java/android/content/pm/PackageInfo.java
+++ b/core/java/android/content/pm/PackageInfo.java
@@ -29,6 +29,7 @@
  * Overall information about the contents of a package.  This corresponds
  * to all of the information collected from AndroidManifest.xml.
  */
+@android.ravenwood.annotation.RavenwoodKeepWholeClass
 public class PackageInfo implements Parcelable {
     /**
      * The name of this package.  From the &lt;manifest&gt; tag's "name"
diff --git a/core/java/android/content/pm/PackageItemInfo.java b/core/java/android/content/pm/PackageItemInfo.java
index 70e6f98..1f821b9 100644
--- a/core/java/android/content/pm/PackageItemInfo.java
+++ b/core/java/android/content/pm/PackageItemInfo.java
@@ -49,6 +49,7 @@
  * itself implement Parcelable, but does provide convenience methods to assist
  * in the implementation of Parcelable in subclasses.
  */
+@android.ravenwood.annotation.RavenwoodKeepWholeClass
 public class PackageItemInfo {
 
     /**
@@ -214,6 +215,7 @@
      * @return Returns a CharSequence containing the item's label.  If the
      * item does not have a label, its name is returned.
      */
+    @android.ravenwood.annotation.RavenwoodThrow(blockedBy = android.content.res.Resources.class)
     public @NonNull CharSequence loadLabel(@NonNull PackageManager pm) {
         if (sForceSafeLabels && !Objects.equals(packageName, ActivityThread.currentPackageName())) {
             return loadSafeLabel(pm, DEFAULT_MAX_LABEL_SIZE_PX, SAFE_STRING_FLAG_TRIM
@@ -226,6 +228,7 @@
     }
 
     /** {@hide} */
+    @android.ravenwood.annotation.RavenwoodThrow(blockedBy = android.content.res.Resources.class)
     public CharSequence loadUnsafeLabel(PackageManager pm) {
         if (nonLocalizedLabel != null) {
             return nonLocalizedLabel;
@@ -248,6 +251,7 @@
      */
     @SystemApi
     @Deprecated
+    @android.ravenwood.annotation.RavenwoodThrow(blockedBy = android.content.res.Resources.class)
     public @NonNull CharSequence loadSafeLabel(@NonNull PackageManager pm) {
         return loadSafeLabel(pm, DEFAULT_MAX_LABEL_SIZE_PX, SAFE_STRING_FLAG_TRIM
                 | SAFE_STRING_FLAG_FIRST_LINE);
@@ -261,6 +265,7 @@
      * @hide
     */
     @SystemApi
+    @android.ravenwood.annotation.RavenwoodThrow(blockedBy = android.content.res.Resources.class)
     public @NonNull CharSequence loadSafeLabel(@NonNull PackageManager pm,
             @FloatRange(from = 0) float ellipsizeDip, @TextUtils.SafeStringFlags int flags) {
         Objects.requireNonNull(pm);
@@ -281,6 +286,7 @@
      * item does not have an icon, the item's default icon is returned
      * such as the default activity icon.
      */
+    @android.ravenwood.annotation.RavenwoodThrow(blockedBy = android.content.res.Resources.class)
     public Drawable loadIcon(PackageManager pm) {
         return pm.loadItemIcon(this, getApplicationInfo());
     }
@@ -298,6 +304,7 @@
      * item does not have an icon, the item's default icon is returned
      * such as the default activity icon.
      */
+    @android.ravenwood.annotation.RavenwoodThrow(blockedBy = android.content.res.Resources.class)
     public Drawable loadUnbadgedIcon(PackageManager pm) {
         return pm.loadUnbadgedItemIcon(this, getApplicationInfo());
     }
@@ -313,6 +320,7 @@
      * @return Returns a Drawable containing the item's banner.  If the item
      * does not have a banner, this method will return null.
      */
+    @android.ravenwood.annotation.RavenwoodThrow(blockedBy = android.content.res.Resources.class)
     public Drawable loadBanner(PackageManager pm) {
         if (banner != 0) {
             Drawable dr = pm.getDrawable(packageName, banner, getApplicationInfo());
@@ -334,6 +342,7 @@
      *
      * @hide
      */
+    @android.ravenwood.annotation.RavenwoodThrow(blockedBy = android.content.res.Resources.class)
     public Drawable loadDefaultIcon(PackageManager pm) {
         return pm.getDefaultActivityIcon();
     }
@@ -349,6 +358,7 @@
      *
      * @hide
      */
+    @android.ravenwood.annotation.RavenwoodThrow(blockedBy = android.content.res.Resources.class)
     protected Drawable loadDefaultBanner(PackageManager pm) {
         return null;
     }
@@ -364,6 +374,7 @@
      * @return Returns a Drawable containing the item's logo. If the item
      * does not have a logo, this method will return null.
      */
+    @android.ravenwood.annotation.RavenwoodThrow(blockedBy = android.content.res.Resources.class)
     public Drawable loadLogo(PackageManager pm) {
         if (logo != 0) {
             Drawable d = pm.getDrawable(packageName, logo, getApplicationInfo());
@@ -385,6 +396,7 @@
      *
      * @hide
      */
+    @android.ravenwood.annotation.RavenwoodThrow(blockedBy = android.content.res.Resources.class)
     protected Drawable loadDefaultLogo(PackageManager pm) {
         return null;
     }
@@ -402,6 +414,7 @@
      * assigned as the given meta-data.  If the meta-data name is not defined
      * or the XML resource could not be found, null is returned.
      */
+    @android.ravenwood.annotation.RavenwoodThrow(blockedBy = android.content.res.Resources.class)
     public XmlResourceParser loadXmlMetaData(PackageManager pm, String name) {
         if (metaData != null) {
             int resid = metaData.getInt(name);
diff --git a/core/java/android/content/pm/PackageManager.java b/core/java/android/content/pm/PackageManager.java
index 7bb673a..607e904 100644
--- a/core/java/android/content/pm/PackageManager.java
+++ b/core/java/android/content/pm/PackageManager.java
@@ -1279,7 +1279,7 @@
      * @see #isPackageQuarantined
      */
     @FlaggedApi(android.content.pm.Flags.FLAG_QUARANTINED_ENABLED)
-    public static final long MATCH_QUARANTINED_COMPONENTS = 0x100000000L;
+    public static final long MATCH_QUARANTINED_COMPONENTS = 1L << 33;
 
     /**
      * Flag for {@link #addCrossProfileIntentFilter}: if this flag is set: when
diff --git a/core/java/android/content/pm/PathPermission.java b/core/java/android/content/pm/PathPermission.java
index 11c9a7d..743ff9a 100644
--- a/core/java/android/content/pm/PathPermission.java
+++ b/core/java/android/content/pm/PathPermission.java
@@ -24,6 +24,7 @@
  * Description of permissions needed to access a particular path
  * in a {@link ProviderInfo}.
  */
+@android.ravenwood.annotation.RavenwoodKeepWholeClass
 public class PathPermission extends PatternMatcher {
     private final String mReadPermission;
     private final String mWritePermission;
diff --git a/core/java/android/content/pm/ProviderInfo.java b/core/java/android/content/pm/ProviderInfo.java
index 3984ade..9e553db 100644
--- a/core/java/android/content/pm/ProviderInfo.java
+++ b/core/java/android/content/pm/ProviderInfo.java
@@ -27,6 +27,7 @@
  * {@link android.content.pm.PackageManager#resolveContentProvider(java.lang.String, int)
  * PackageManager.resolveContentProvider()}.
  */
+@android.ravenwood.annotation.RavenwoodKeepWholeClass
 public final class ProviderInfo extends ComponentInfo
         implements Parcelable {
     
diff --git a/core/java/android/content/pm/ResolveInfo.java b/core/java/android/content/pm/ResolveInfo.java
index 36c03fd..25bb9e1 100644
--- a/core/java/android/content/pm/ResolveInfo.java
+++ b/core/java/android/content/pm/ResolveInfo.java
@@ -42,6 +42,7 @@
  * information collected from the AndroidManifest.xml's
  * &lt;intent&gt; tags.
  */
+@android.ravenwood.annotation.RavenwoodKeepWholeClass
 public class ResolveInfo implements Parcelable {
     private static final String TAG = "ResolveInfo";
     private static final String INTENT_FORWARDER_ACTIVITY =
@@ -227,6 +228,7 @@
      * item does not have a label, its name is returned.
      */
     @NonNull
+    @android.ravenwood.annotation.RavenwoodThrow(blockedBy = android.content.res.Resources.class)
     public CharSequence loadLabel(@NonNull PackageManager pm) {
         if (nonLocalizedLabel != null) {
             return nonLocalizedLabel;
@@ -304,6 +306,7 @@
      * @return Returns a Drawable containing the resolution's icon.  If the
      * item does not have an icon, the default activity icon is returned.
      */
+    @android.ravenwood.annotation.RavenwoodThrow(blockedBy = android.content.res.Resources.class)
     public Drawable loadIcon(PackageManager pm) {
         Drawable dr = null;
         if (resolvePackageName != null && iconResourceId != 0) {
diff --git a/core/java/android/content/pm/ServiceInfo.java b/core/java/android/content/pm/ServiceInfo.java
index 9869179..4d704c3 100644
--- a/core/java/android/content/pm/ServiceInfo.java
+++ b/core/java/android/content/pm/ServiceInfo.java
@@ -31,6 +31,7 @@
  * service. This corresponds to information collected from the
  * AndroidManifest.xml's &lt;service&gt; tags.
  */
+@android.ravenwood.annotation.RavenwoodKeepWholeClass
 public class ServiceInfo extends ComponentInfo
         implements Parcelable {
     /**
diff --git a/core/java/android/content/pm/Signature.java b/core/java/android/content/pm/Signature.java
index a69eee7..f173334 100644
--- a/core/java/android/content/pm/Signature.java
+++ b/core/java/android/content/pm/Signature.java
@@ -43,6 +43,7 @@
  * <p>
  * This class name is slightly misleading, since it's not actually a signature.
  */
+@android.ravenwood.annotation.RavenwoodKeepWholeClass
 public class Signature implements Parcelable {
     private final byte[] mSignature;
     private int mHashCode;
diff --git a/core/java/android/content/pm/UserProperties.java b/core/java/android/content/pm/UserProperties.java
index 445ca0c..56e8291 100644
--- a/core/java/android/content/pm/UserProperties.java
+++ b/core/java/android/content/pm/UserProperties.java
@@ -1076,6 +1076,8 @@
      * Intended for building default values (and so all properties are present in the built object).
      * @hide
      */
+    @TestApi
+    @SuppressLint("UnflaggedApi") // b/306636213
     public static final class Builder {
         // UserProperties fields and their default values.
         private @ShowInLauncher int mShowInLauncher = SHOW_IN_LAUNCHER_WITH_PARENT;
@@ -1099,54 +1101,82 @@
         private boolean mDeleteAppWithParent = false;
         private boolean mAlwaysVisible = false;
 
+        /**
+         * @hide
+         */
+        @SuppressLint("UnflaggedApi") // b/306636213
+        @TestApi
+        public Builder() {}
+
+        /** @hide */
         public Builder setShowInLauncher(@ShowInLauncher int showInLauncher) {
             mShowInLauncher = showInLauncher;
             return this;
         }
 
+        /** @hide */
         public Builder setStartWithParent(boolean startWithParent) {
             mStartWithParent = startWithParent;
             return this;
         }
 
-        /** Sets the value for {@link #mShowInSettings} */
+        /** Sets the value for {@link #mShowInSettings}
+         * @hide
+         */
         public Builder setShowInSettings(@ShowInSettings int showInSettings) {
             mShowInSettings = showInSettings;
             return this;
         }
 
-        /** Sets the value for {@link #mShowInQuietMode} */
+        /** Sets the value for {@link #mShowInQuietMode}
+         * @hide
+         */
+        @TestApi
+        @SuppressLint("UnflaggedApi") // b/306636213
+        @NonNull
         public Builder setShowInQuietMode(@ShowInQuietMode int showInQuietMode) {
             mShowInQuietMode = showInQuietMode;
             return this;
         }
 
-        /** Sets the value for {@link #mShowInSharingSurfaces}. */
+        /** Sets the value for {@link #mShowInSharingSurfaces}.
+         * @hide
+         */
+        @TestApi
+        @SuppressLint("UnflaggedApi") // b/306636213
+        @NonNull
         public Builder setShowInSharingSurfaces(@ShowInSharingSurfaces int showInSharingSurfaces) {
             mShowInSharingSurfaces = showInSharingSurfaces;
             return this;
         }
 
-        /** Sets the value for {@link #mInheritDevicePolicy}*/
+        /** Sets the value for {@link #mInheritDevicePolicy}
+         * @hide
+         */
         public Builder setInheritDevicePolicy(
                 @InheritDevicePolicy int inheritRestrictionsDevicePolicy) {
             mInheritDevicePolicy = inheritRestrictionsDevicePolicy;
             return this;
         }
 
+        /** @hide */
         public Builder setUseParentsContacts(boolean useParentsContacts) {
             mUseParentsContacts = useParentsContacts;
             return this;
         }
 
-        /** Sets the value for {@link #mUpdateCrossProfileIntentFiltersOnOTA} */
+        /** Sets the value for {@link #mUpdateCrossProfileIntentFiltersOnOTA}
+         * @hide
+         */
         public Builder setUpdateCrossProfileIntentFiltersOnOTA(boolean
                 updateCrossProfileIntentFiltersOnOTA) {
             mUpdateCrossProfileIntentFiltersOnOTA = updateCrossProfileIntentFiltersOnOTA;
             return this;
         }
 
-        /** Sets the value for {@link #mCrossProfileIntentFilterAccessControl} */
+        /** Sets the value for {@link #mCrossProfileIntentFilterAccessControl}
+         * @hide
+         */
         public Builder setCrossProfileIntentFilterAccessControl(
                 @CrossProfileIntentFilterAccessControlLevel int
                         crossProfileIntentFilterAccessControl) {
@@ -1154,24 +1184,30 @@
             return this;
         }
 
-        /** Sets the value for {@link #mCrossProfileIntentResolutionStrategy} */
+        /** Sets the value for {@link #mCrossProfileIntentResolutionStrategy}
+         * @hide
+         */
         public Builder setCrossProfileIntentResolutionStrategy(@CrossProfileIntentResolutionStrategy
                 int crossProfileIntentResolutionStrategy) {
             mCrossProfileIntentResolutionStrategy = crossProfileIntentResolutionStrategy;
             return this;
         }
 
+        /** @hide */
         public Builder setMediaSharedWithParent(boolean mediaSharedWithParent) {
             mMediaSharedWithParent = mediaSharedWithParent;
             return this;
         }
 
+        /** @hide */
         public Builder setCredentialShareableWithParent(boolean credentialShareableWithParent) {
             mCredentialShareableWithParent = credentialShareableWithParent;
             return this;
         }
 
-        /** Sets the value for {@link #mAuthAlwaysRequiredToDisableQuietMode} */
+        /** Sets the value for {@link #mAuthAlwaysRequiredToDisableQuietMode}
+         * @hide
+         */
         public Builder setAuthAlwaysRequiredToDisableQuietMode(
                 boolean authAlwaysRequiredToDisableQuietMode) {
             mAuthAlwaysRequiredToDisableQuietMode =
@@ -1179,19 +1215,28 @@
             return this;
         }
 
-        /** Sets the value for {@link #mDeleteAppWithParent}*/
+        /** Sets the value for {@link #mDeleteAppWithParent}
+         * @hide
+         */
         public Builder setDeleteAppWithParent(boolean deleteAppWithParent) {
             mDeleteAppWithParent = deleteAppWithParent;
             return this;
         }
 
-        /** Sets the value for {@link #mAlwaysVisible}*/
+        /** Sets the value for {@link #mAlwaysVisible}
+         * @hide
+         */
         public Builder setAlwaysVisible(boolean alwaysVisible) {
             mAlwaysVisible = alwaysVisible;
             return this;
         }
 
-        /** Builds a UserProperties object with *all* values populated. */
+        /** Builds a UserProperties object with *all* values populated.
+         * @hide
+         */
+        @TestApi
+        @SuppressLint("UnflaggedApi") // b/306636213
+        @NonNull
         public UserProperties build() {
             return new UserProperties(
                     mShowInLauncher,
diff --git a/core/java/android/content/res/flags.aconfig b/core/java/android/content/res/flags.aconfig
index 40592a1..3a00d91 100644
--- a/core/java/android/content/res/flags.aconfig
+++ b/core/java/android/content/res/flags.aconfig
@@ -24,3 +24,10 @@
     # This flag is read in PackageParser at boot time, and in aapt2 which is a build tool.
     is_fixed_read_only: true
 }
+
+flag {
+    name: "nine_patch_frro"
+    namespace: "resource_manager"
+    description: "Feature flag for creating an frro from a 9-patch"
+    bug: "309232726"
+}
diff --git a/core/java/android/hardware/camera2/CameraExtensionCharacteristics.java b/core/java/android/hardware/camera2/CameraExtensionCharacteristics.java
index 0a61c32..d4d1ab3 100644
--- a/core/java/android/hardware/camera2/CameraExtensionCharacteristics.java
+++ b/core/java/android/hardware/camera2/CameraExtensionCharacteristics.java
@@ -15,6 +15,7 @@
  */
 package android.hardware.camera2;
 
+import android.annotation.FlaggedApi;
 import android.annotation.IntDef;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
@@ -39,11 +40,15 @@
 import android.os.IBinder;
 import android.os.RemoteException;
 import android.os.SystemProperties;
+import android.util.FeatureFlagUtils;
+import android.util.IntArray;
 import android.util.Log;
 import android.util.Pair;
 import android.util.Range;
 import android.util.Size;
 
+import com.android.internal.camera.flags.Flags;
+
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.util.ArrayList;
@@ -129,6 +134,12 @@
     public static final int EXTENSION_NIGHT = 4;
 
     /**
+     * An extension that aims to lock and stabilize a given region or object of interest.
+     */
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    public static final int EXTENSION_EYES_FREE_VIDEOGRAPHY = 5;
+
+    /**
      * @hide
      */
     @Retention(RetentionPolicy.SOURCE)
@@ -136,7 +147,8 @@
                 EXTENSION_FACE_RETOUCH,
                 EXTENSION_BOKEH,
                 EXTENSION_HDR,
-                EXTENSION_NIGHT})
+                EXTENSION_NIGHT,
+                EXTENSION_EYES_FREE_VIDEOGRAPHY})
     public @interface Extension {
     }
 
@@ -594,8 +606,13 @@
             return Collections.unmodifiableList(ret);
         }
 
+        IntArray extensionList = new IntArray(EXTENSION_LIST.length);
+        extensionList.addAll(EXTENSION_LIST);
+        if (Flags.concertMode()) {
+            extensionList.add(EXTENSION_EYES_FREE_VIDEOGRAPHY);
+        }
         try {
-            for (int extensionType : EXTENSION_LIST) {
+            for (int extensionType : extensionList.toArray()) {
                 if (isExtensionSupported(mCameraId, extensionType, mCharacteristicsMapNative)) {
                     ret.add(extensionType);
                 }
diff --git a/core/java/android/hardware/camera2/extension/AdvancedExtender.java b/core/java/android/hardware/camera2/extension/AdvancedExtender.java
new file mode 100644
index 0000000..fb2df54
--- /dev/null
+++ b/core/java/android/hardware/camera2/extension/AdvancedExtender.java
@@ -0,0 +1,353 @@
+/*
+ * Copyright (C) 2023 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 android.hardware.camera2.extension;
+
+import android.annotation.FlaggedApi;
+import android.annotation.NonNull;
+import android.annotation.SystemApi;
+import android.hardware.camera2.CameraAccessException;
+import android.hardware.camera2.CameraCharacteristics;
+import android.hardware.camera2.CameraManager;
+import android.hardware.camera2.CaptureRequest;
+import android.hardware.camera2.CaptureResult;
+import android.hardware.camera2.impl.CameraMetadataNative;
+import android.hardware.camera2.impl.CaptureCallback;
+import android.util.Log;
+import android.util.Size;
+
+import com.android.internal.camera.flags.Flags;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Advanced contract for implementing Extensions. ImageCapture/Preview
+ * Extensions are both implemented on this interface.
+ *
+ * <p>This advanced contract empowers implementations to gain access to
+ * more Camera2 capability. This includes: (1) Add custom surfaces with
+ * specific formats like YUV, RAW, RAW_DEPTH. (2) Access to
+ * the capture request callbacks as well as all the images retrieved of
+ * various image formats. (3)
+ * Able to triggers single or repeating request with the capabilities to
+ * specify target surfaces, template id and parameters.
+ *
+ * @hide
+ */
+@SystemApi
+@FlaggedApi(Flags.FLAG_CONCERT_MODE)
+public abstract class AdvancedExtender {
+    private HashMap<String, Long> mMetadataVendorIdMap = new HashMap<>();
+    private final CameraManager mCameraManager;
+
+    private static final String TAG = "AdvancedExtender";
+
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    protected AdvancedExtender(@NonNull CameraManager cameraManager) {
+        mCameraManager = cameraManager;
+        try {
+            String [] cameraIds = mCameraManager.getCameraIdListNoLazy();
+            if (cameraIds != null) {
+                for (String cameraId : cameraIds) {
+                    CameraCharacteristics chars = mCameraManager.getCameraCharacteristics(cameraId);
+                    Object thisClass = CameraCharacteristics.Key.class;
+                    Class<CameraCharacteristics.Key<?>> keyClass =
+                            (Class<CameraCharacteristics.Key<?>>)thisClass;
+                    ArrayList<CameraCharacteristics.Key<?>> vendorKeys =
+                            chars.getNativeMetadata().getAllVendorKeys(keyClass);
+                    if ((vendorKeys != null) && !vendorKeys.isEmpty()) {
+                        mMetadataVendorIdMap.put(cameraId, vendorKeys.get(0).getVendorId());
+                    }
+                }
+            }
+        } catch (CameraAccessException e) {
+            Log.e(TAG, "Failed to query camera characteristics!");
+        }
+    }
+
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    public long getMetadataVendorId(@NonNull String cameraId) {
+        long vendorId = mMetadataVendorIdMap.containsKey(cameraId) ?
+                mMetadataVendorIdMap.get(cameraId) : Long.MAX_VALUE;
+        return vendorId;
+    }
+
+    /**
+     * Indicates whether the extension is supported on the device.
+     *
+     * @param cameraId           The camera2 id string of the camera.
+     * @param charsMap           A map consisting of the camera ids and
+     *                           the {@link android.hardware.camera2.CameraCharacteristics}s. For
+     *                           every camera, the map contains at least
+     *                           the CameraCharacteristics for the camera
+     *                           id.
+     *                           If the camera is logical camera, it will
+     *                           also contain associated
+     *                           physical camera ids and their
+     *                           CameraCharacteristics.
+     * @return true if the extension is supported, otherwise false
+     */
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    public abstract boolean isExtensionAvailable(@NonNull String cameraId,
+            @NonNull CharacteristicsMap charsMap);
+
+    /**
+     * Initializes the extender to be used with the specified camera.
+     *
+     * <p>This should be called before any other method on the extender.
+     * The exception is {@link #isExtensionAvailable}.
+     *
+     * @param cameraId           The camera2 id string of the camera.
+     * @param map                A map consisting of the camera ids and
+     *                           the {@link android.hardware.camera2.CameraCharacteristics}s. For
+     *                           every camera, the map contains at least
+     *                           the CameraCharacteristics for the camera
+     *                           id.
+     *                           If the camera is logical camera, it will
+     *                           also contain associated
+     *                           physical camera ids and their
+     *                           CameraCharacteristics.
+     */
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    public abstract void init(@NonNull String cameraId, @NonNull CharacteristicsMap map);
+
+    /**
+     * Returns supported output format/size map for preview. The format
+     * could be PRIVATE or YUV_420_888. Implementations must support
+     * PRIVATE format at least.
+     *
+     * <p>The preview surface format in the CameraCaptureSession may not
+     * be identical to the supported preview output format returned here.
+     * @param cameraId           The camera2 id string of the camera.
+     */
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    @NonNull
+    public abstract Map<Integer, List<Size>> getSupportedPreviewOutputResolutions(
+            @NonNull String cameraId);
+
+    /**
+     * Returns supported output format/size map for image capture. OEM is
+     * required to support both JPEG and YUV_420_888 format output.
+     *
+     * <p>The surface created with this supported
+     * format/size could be either added in CameraCaptureSession with HAL
+     * processing OR it  configures intermediate surfaces(YUV/RAW..) and
+     * writes the output to the output surface.
+     * @param cameraId           The camera2 id string of the camera.
+     */
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    @NonNull
+    public abstract Map<Integer, List<Size>> getSupportedCaptureOutputResolutions(
+            @NonNull String cameraId);
+
+    /**
+     * Returns a processor for activating extension sessions. It
+     * implements all the interactions required for starting an extension
+     * and cleanup.
+     */
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    @NonNull
+    public abstract SessionProcessor getSessionProcessor();
+
+    /**
+     * Returns a list of orthogonal capture request keys.
+     *
+     * <p>Any keys included in the list will be configurable by clients of
+     * the extension and will affect the extension functionality.</p>
+     *
+     * <p>Please note that the keys {@link CaptureRequest#JPEG_QUALITY}
+     * and {@link CaptureRequest#JPEG_ORIENTATION} are always supported
+     * regardless of being added to the list or not. To support common
+     * camera operations like zoom, tap-to-focus, flash and
+     * exposure compensation, we recommend supporting the following keys
+     * if  possible.
+     * <pre>
+     *  zoom:  {@link CaptureRequest#CONTROL_ZOOM_RATIO}
+     *         {@link CaptureRequest#SCALER_CROP_REGION}
+     *  tap-to-focus:
+     *         {@link CaptureRequest#CONTROL_AF_MODE}
+     *         {@link CaptureRequest#CONTROL_AF_TRIGGER}
+     *         {@link CaptureRequest#CONTROL_AF_REGIONS}
+     *         {@link CaptureRequest#CONTROL_AE_REGIONS}
+     *         {@link CaptureRequest#CONTROL_AWB_REGIONS}
+     *  flash:
+     *         {@link CaptureRequest#CONTROL_AE_MODE}
+     *         {@link CaptureRequest#CONTROL_AE_PRECAPTURE_TRIGGER}
+     *         {@link CaptureRequest#FLASH_MODE}
+     *  exposure compensation:
+     *         {@link CaptureRequest#CONTROL_AE_EXPOSURE_COMPENSATION}
+     * </pre>
+     *
+     * @param cameraId           The camera2 id string of the camera.
+     *
+     * @return The list of supported orthogonal capture keys, or empty
+     * list if no capture settings are not supported.
+     */
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    @NonNull
+    public abstract List<CaptureRequest.Key> getAvailableCaptureRequestKeys(
+            @NonNull String cameraId);
+
+    /**
+     * Returns a list of supported capture result keys.
+     *
+     * <p>Any keys included in this list must be available as part of the
+     * registered {@link CaptureCallback#onCaptureCompleted} callback.</p>
+     *
+     * <p>At the very minimum, it is expected that the result key list is
+     * a superset of the capture request keys.</p>
+     *
+     * @param cameraId           The camera2 id string of the camera.
+     * @return The list of supported capture result keys, or
+     * empty list if capture results are not supported.
+     */
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    @NonNull
+    public abstract List<CaptureResult.Key> getAvailableCaptureResultKeys(
+            @NonNull String cameraId);
+
+
+    private final class AdvancedExtenderImpl extends IAdvancedExtenderImpl.Stub {
+        @Override
+        public boolean isExtensionAvailable(String cameraId,
+                Map<String, CameraMetadataNative> charsMapNative) {
+            return AdvancedExtender.this.isExtensionAvailable(cameraId,
+                    new CharacteristicsMap(charsMapNative));
+        }
+
+        @Override
+        public void init(String cameraId, Map<String, CameraMetadataNative> charsMapNative) {
+            AdvancedExtender.this.init(cameraId, new CharacteristicsMap(charsMapNative));
+        }
+
+        @Override
+        public List<SizeList> getSupportedPostviewResolutions(
+                android.hardware.camera2.extension.Size captureSize) {
+            // Feature is currently unsupported
+            return null;
+        }
+
+        @Override
+        public List<SizeList> getSupportedPreviewOutputResolutions(String cameraId) {
+                return initializeParcelable(
+                        AdvancedExtender.this.getSupportedPreviewOutputResolutions(cameraId));
+        }
+
+        @Override
+        public List<SizeList> getSupportedCaptureOutputResolutions(String cameraId) {
+            return initializeParcelable(
+                    AdvancedExtender.this.getSupportedCaptureOutputResolutions(cameraId));
+        }
+
+        @Override
+        public LatencyRange getEstimatedCaptureLatencyRange(String cameraId,
+                android.hardware.camera2.extension.Size outputSize, int format) {
+            // Feature is currently unsupported
+            return null;
+        }
+
+        @Override
+        public ISessionProcessorImpl getSessionProcessor() {
+            return AdvancedExtender.this.getSessionProcessor().getSessionProcessorBinder();
+        }
+
+        @Override
+        public CameraMetadataNative getAvailableCaptureRequestKeys(String cameraId) {
+            List<CaptureRequest.Key> supportedCaptureKeys =
+                    AdvancedExtender.this.getAvailableCaptureRequestKeys(cameraId);
+
+            if (!supportedCaptureKeys.isEmpty()) {
+                CameraMetadataNative ret = new CameraMetadataNative();
+                long vendorId = getMetadataVendorId(cameraId);
+                ret.setVendorId(vendorId);
+                int requestKeyTags[] = new int[supportedCaptureKeys.size()];
+                int i = 0;
+                for (CaptureRequest.Key key : supportedCaptureKeys) {
+                    requestKeyTags[i++] = CameraMetadataNative.getTag(key.getName(), vendorId);
+                }
+                ret.set(CameraCharacteristics.REQUEST_AVAILABLE_REQUEST_KEYS, requestKeyTags);
+
+                return ret;
+            }
+
+            return null;
+        }
+
+        @Override
+        public CameraMetadataNative getAvailableCaptureResultKeys(String cameraId) {
+            List<CaptureResult.Key> supportedResultKeys =
+                    AdvancedExtender.this.getAvailableCaptureResultKeys(cameraId);
+
+            if (!supportedResultKeys.isEmpty()) {
+                CameraMetadataNative ret = new CameraMetadataNative();
+                long vendorId = getMetadataVendorId(cameraId);
+                ret.setVendorId(vendorId);
+                int resultKeyTags [] = new int[supportedResultKeys.size()];
+                int i = 0;
+                for (CaptureResult.Key key : supportedResultKeys) {
+                    resultKeyTags[i++] = CameraMetadataNative.getTag(key.getName(), vendorId);
+                }
+                ret.set(CameraCharacteristics.REQUEST_AVAILABLE_RESULT_KEYS, resultKeyTags);
+
+                return ret;
+            }
+
+            return null;
+        }
+
+        @Override
+        public boolean isCaptureProcessProgressAvailable() {
+            // Feature is currently unsupported
+            return false;
+        }
+
+        @Override
+        public boolean isPostviewAvailable() {
+            // Feature is currently unsupported
+            return false;
+        }
+    }
+
+    @NonNull IAdvancedExtenderImpl getAdvancedExtenderBinder() {
+        return new AdvancedExtenderImpl();
+    }
+
+    private static List<SizeList> initializeParcelable(
+            Map<Integer, List<android.util.Size>> sizes) {
+        if (sizes == null) {
+            return null;
+        }
+        ArrayList<SizeList> ret = new ArrayList<>(sizes.size());
+        for (Map.Entry<Integer, List<android.util.Size>> entry : sizes.entrySet()) {
+            SizeList sizeList = new SizeList();
+            sizeList.format = entry.getKey();
+            sizeList.sizes = new ArrayList<>();
+            for (android.util.Size size : entry.getValue()) {
+                android.hardware.camera2.extension.Size sz =
+                        new android.hardware.camera2.extension.Size();
+                sz.width = size.getWidth();
+                sz.height = size.getHeight();
+                sizeList.sizes.add(sz);
+            }
+            ret.add(sizeList);
+        }
+
+        return ret;
+    }
+}
diff --git a/core/java/android/hardware/camera2/extension/CameraExtensionService.java b/core/java/android/hardware/camera2/extension/CameraExtensionService.java
new file mode 100644
index 0000000..1426d7b
--- /dev/null
+++ b/core/java/android/hardware/camera2/extension/CameraExtensionService.java
@@ -0,0 +1,170 @@
+/*
+ * Copyright (C) 2023 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 android.hardware.camera2.extension;
+
+import android.annotation.FlaggedApi;
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.annotation.SystemApi;
+import android.app.Service;
+import android.content.Intent;
+import android.os.IBinder;
+import android.os.RemoteException;
+import android.util.Log;
+
+import com.android.internal.annotations.GuardedBy;
+import com.android.internal.camera.flags.Flags;
+
+/**
+ * Base service class that extension service implementations must extend.
+ *
+ * @hide
+ */
+@SystemApi
+@FlaggedApi(Flags.FLAG_CONCERT_MODE)
+public abstract class CameraExtensionService extends Service {
+    private static final String TAG = "CameraExtensionService";
+    private static Object mLock = new Object();
+
+    @GuardedBy("mLock")
+    private static IInitializeSessionCallback mInitializeCb = null;
+
+    private IBinder.DeathRecipient mDeathRecipient = new IBinder.DeathRecipient() {
+        @Override
+        public void binderDied() {
+            synchronized (mLock) {
+                mInitializeCb = null;
+            }
+        }
+    };
+
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    protected CameraExtensionService() {}
+
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    @Override
+    @NonNull
+    public IBinder onBind(@Nullable Intent intent) {
+        return new CameraExtensionServiceImpl();
+    }
+
+    private class CameraExtensionServiceImpl extends ICameraExtensionsProxyService.Stub {
+        @Override
+        public boolean registerClient(IBinder token) throws RemoteException {
+            return CameraExtensionService.this.onRegisterClient(token);
+        }
+
+        @Override
+        public void unregisterClient(IBinder token) throws RemoteException {
+            CameraExtensionService.this.onUnregisterClient(token);
+        }
+
+        @Override
+        public boolean advancedExtensionsSupported() throws RemoteException {
+            return true;
+        }
+
+        @Override
+        public void initializeSession(IInitializeSessionCallback cb) {
+            boolean ret = false;
+            synchronized (mLock) {
+                if (mInitializeCb == null) {
+                    mInitializeCb = cb;
+                    try {
+                        mInitializeCb.asBinder().linkToDeath(mDeathRecipient, 0);
+                    } catch (RemoteException e) {
+                        Log.e(TAG, "Failure to register binder death notifier!");
+                    }
+                    ret = true;
+                }
+            }
+
+            try {
+                if (ret) {
+                    cb.onSuccess();
+                } else {
+                    cb.onFailure();
+                }
+            } catch (RemoteException e) {
+
+                Log.e(TAG, "Client doesn't respond!");
+            }
+        }
+
+        @Override
+        public void releaseSession() {
+            synchronized (mLock) {
+                if (mInitializeCb != null) {
+                    mInitializeCb.asBinder().unlinkToDeath(mDeathRecipient, 0);
+                    mInitializeCb = null;
+                }
+            }
+        }
+
+        @Override
+        public IPreviewExtenderImpl initializePreviewExtension(int extensionType)
+                throws RemoteException {
+            // Basic Extension API is not supported
+            return null;
+        }
+
+        @Override
+        public IImageCaptureExtenderImpl initializeImageExtension(int extensionType)
+                throws RemoteException {
+            // Basic Extension API is not supported
+            return null;
+        }
+
+        @Override
+        public IAdvancedExtenderImpl initializeAdvancedExtension(int extensionType)
+                throws RemoteException {
+            return CameraExtensionService.this.onInitializeAdvancedExtension(
+                    extensionType).getAdvancedExtenderBinder();
+        }
+    }
+
+    /**
+     * Register an extension client. The client must call this method
+     * after successfully binding to the service.
+     *
+     * @param token              Binder token that can be used for adding
+     *                           death notifier in case the client exits
+     *                           unexpectedly.
+     * @return true if the registration is successful, false otherwise
+     */
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    public abstract boolean onRegisterClient(@NonNull IBinder token);
+
+    /**
+     * Unregister an extension client.
+     *
+     * @param token              Binder token
+     */
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    public abstract void onUnregisterClient(@NonNull IBinder token);
+
+    /**
+     * Initialize and return an advanced extension.
+     *
+     * @param extensionType {@link android.hardware.camera2.CameraExtensionCharacteristics}
+     *                      extension type
+     * @return Valid advanced extender of the requested type
+     */
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    @NonNull
+    public abstract AdvancedExtender onInitializeAdvancedExtension(int extensionType);
+}
diff --git a/core/java/android/hardware/camera2/extension/CameraOutputSurface.java b/core/java/android/hardware/camera2/extension/CameraOutputSurface.java
new file mode 100644
index 0000000..f98ebee
--- /dev/null
+++ b/core/java/android/hardware/camera2/extension/CameraOutputSurface.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2023 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 android.hardware.camera2.extension;
+
+import android.annotation.FlaggedApi;
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.annotation.SystemApi;
+import android.hardware.camera2.utils.SurfaceUtils;
+import android.util.Size;
+import android.view.Surface;
+
+import com.android.internal.camera.flags.Flags;
+
+
+/**
+ * @hide
+ */
+@SystemApi
+@FlaggedApi(Flags.FLAG_CONCERT_MODE)
+public final class CameraOutputSurface {
+    private final OutputSurface mOutputSurface;
+
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    CameraOutputSurface(@NonNull OutputSurface surface) {
+       mOutputSurface = surface;
+    }
+
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    public CameraOutputSurface(@NonNull Surface surface,
+            @Nullable Size size ) {
+        mOutputSurface = new OutputSurface();
+        mOutputSurface.surface = surface;
+        mOutputSurface.imageFormat = SurfaceUtils.getSurfaceFormat(surface);
+        if (size != null) {
+            mOutputSurface.size = new android.hardware.camera2.extension.Size();
+            mOutputSurface.size.width = size.getWidth();
+            mOutputSurface.size.height = size.getHeight();
+        }
+    }
+
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    @Nullable
+    public Surface getSurface() {
+        return mOutputSurface.surface;
+    }
+
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    @Nullable
+    public android.util.Size getSize() {
+        if (mOutputSurface.size != null) {
+            return new Size(mOutputSurface.size.width, mOutputSurface.size.height);
+        }
+        return null;
+    }
+
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    public int getImageFormat() {
+        return mOutputSurface.imageFormat;
+    }
+}
diff --git a/core/java/android/hardware/camera2/extension/CharacteristicsMap.java b/core/java/android/hardware/camera2/extension/CharacteristicsMap.java
new file mode 100644
index 0000000..af83595
--- /dev/null
+++ b/core/java/android/hardware/camera2/extension/CharacteristicsMap.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2023 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 android.hardware.camera2.extension;
+
+import android.annotation.FlaggedApi;
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.annotation.SystemApi;
+import android.hardware.camera2.CameraCharacteristics;
+import android.hardware.camera2.impl.CameraMetadataNative;
+
+import com.android.internal.camera.flags.Flags;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * @hide
+ */
+@SystemApi
+@FlaggedApi(Flags.FLAG_CONCERT_MODE)
+public class CharacteristicsMap {
+    private final HashMap<String, CameraCharacteristics> mCharMap;
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    CharacteristicsMap(@NonNull Map<String, CameraMetadataNative> charsMap) {
+        mCharMap = new HashMap<>();
+        for (Map.Entry<String, CameraMetadataNative> entry : charsMap.entrySet()) {
+            mCharMap.put(entry.getKey(), new CameraCharacteristics(entry.getValue()));
+        }
+    }
+
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    @NonNull
+    public Set<String> getCameraIds() {
+        return mCharMap.keySet();
+    }
+
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    @Nullable
+    public CameraCharacteristics get(@NonNull String cameraId) {
+        return mCharMap.get(cameraId);
+    }
+}
diff --git a/core/java/android/hardware/camera2/extension/ExtensionConfiguration.java b/core/java/android/hardware/camera2/extension/ExtensionConfiguration.java
new file mode 100644
index 0000000..2d9ab76
--- /dev/null
+++ b/core/java/android/hardware/camera2/extension/ExtensionConfiguration.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2023 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 android.hardware.camera2.extension;
+
+import android.annotation.FlaggedApi;
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.annotation.SystemApi;
+import android.hardware.camera2.CaptureRequest;
+
+import com.android.internal.camera.flags.Flags;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @hide
+ */
+@SystemApi
+@FlaggedApi(Flags.FLAG_CONCERT_MODE)
+public class ExtensionConfiguration {
+    private final int mSessionType;
+    private final int mSessionTemplateId;
+    private final List<ExtensionOutputConfiguration> mOutputs;
+    private final CaptureRequest mSessionParameters;
+
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    public ExtensionConfiguration(int sessionType, int sessionTemplateId, @NonNull
+            List<ExtensionOutputConfiguration> outputs, @Nullable CaptureRequest sessionParams) {
+        mSessionType = sessionType;
+        mSessionTemplateId = sessionTemplateId;
+        mOutputs = outputs;
+        mSessionParameters = sessionParams;
+    }
+
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    CameraSessionConfig getCameraSessionConfig() {
+        if (mOutputs.isEmpty()) {
+            return null;
+        }
+
+        CameraSessionConfig ret = new CameraSessionConfig();
+        ret.sessionTemplateId = mSessionTemplateId;
+        ret.sessionType = mSessionType;
+        ret.outputConfigs = new ArrayList<>(mOutputs.size());
+        for (ExtensionOutputConfiguration outputConfig : mOutputs) {
+            ret.outputConfigs.add(outputConfig.getOutputConfig());
+        }
+        if (mSessionParameters != null) {
+            ret.sessionParameter = mSessionParameters.getNativeCopy();
+        }
+
+        return ret;
+    }
+}
diff --git a/core/java/android/hardware/camera2/extension/ExtensionOutputConfiguration.java b/core/java/android/hardware/camera2/extension/ExtensionOutputConfiguration.java
new file mode 100644
index 0000000..85d180d
--- /dev/null
+++ b/core/java/android/hardware/camera2/extension/ExtensionOutputConfiguration.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright (C) 2023 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 android.hardware.camera2.extension;
+
+import android.annotation.FlaggedApi;
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.annotation.SystemApi;
+
+import com.android.internal.camera.flags.Flags;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @hide
+ */
+@SystemApi
+@FlaggedApi(Flags.FLAG_CONCERT_MODE)
+public class ExtensionOutputConfiguration {
+    private final List<CameraOutputSurface> mSurfaces;
+    private final String mPhysicalCameraId;
+    private final int mOutputConfigId;
+    private final int mSurfaceGroupId;
+
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    public ExtensionOutputConfiguration(@NonNull List<CameraOutputSurface> outputs,
+            int outputConfigId, @Nullable String physicalCameraId, int surfaceGroupId) {
+        mSurfaces = outputs;
+        mPhysicalCameraId = physicalCameraId;
+        mOutputConfigId = outputConfigId;
+        mSurfaceGroupId = surfaceGroupId;
+    }
+
+    private void initializeOutputConfig(@NonNull CameraOutputConfig config,
+            @NonNull CameraOutputSurface surface) {
+        config.surface = surface.getSurface();
+        if (surface.getSize() != null) {
+            config.size = new Size();
+            config.size.width = surface.getSize().getWidth();
+            config.size.height = surface.getSize().getHeight();
+        }
+        config.imageFormat = surface.getImageFormat();
+        config.type = CameraOutputConfig.TYPE_SURFACE;
+        config.physicalCameraId = mPhysicalCameraId;
+        config.outputId = new OutputConfigId();
+        config.outputId.id = mOutputConfigId;
+        config.surfaceGroupId = mSurfaceGroupId;
+    }
+
+    @Nullable CameraOutputConfig getOutputConfig() {
+        if (mSurfaces.isEmpty()) {
+            return null;
+        }
+
+        CameraOutputConfig ret = new CameraOutputConfig();
+        initializeOutputConfig(ret, mSurfaces.get(0));
+        if (mSurfaces.size() > 1) {
+            ret.sharedSurfaceConfigs = new ArrayList<>(mSurfaces.size() - 1);
+            for (int i = 1; i < mSurfaces.size(); i++) {
+                CameraOutputConfig sharedConfig = new CameraOutputConfig();
+                initializeOutputConfig(sharedConfig, mSurfaces.get(i));
+                ret.sharedSurfaceConfigs.add(sharedConfig);
+            }
+        }
+
+        return ret;
+    }
+}
diff --git a/core/java/android/hardware/camera2/extension/ISessionProcessorImpl.aidl b/core/java/android/hardware/camera2/extension/ISessionProcessorImpl.aidl
index 0581ec0..a4a1770 100644
--- a/core/java/android/hardware/camera2/extension/ISessionProcessorImpl.aidl
+++ b/core/java/android/hardware/camera2/extension/ISessionProcessorImpl.aidl
@@ -34,7 +34,7 @@
             in Map<String, CameraMetadataNative> charsMap, in OutputSurface previewSurface,
             in OutputSurface imageCaptureSurface, in OutputSurface postviewSurface);
     void deInitSession(in IBinder token);
-    void onCaptureSessionStart(IRequestProcessorImpl requestProcessor);
+    void onCaptureSessionStart(IRequestProcessorImpl requestProcessor, in String statsKey);
     void onCaptureSessionEnd();
     int startRepeating(in ICaptureCallback callback);
     void stopRepeating();
diff --git a/core/java/android/hardware/camera2/extension/RequestProcessor.java b/core/java/android/hardware/camera2/extension/RequestProcessor.java
new file mode 100644
index 0000000..7c099d6
--- /dev/null
+++ b/core/java/android/hardware/camera2/extension/RequestProcessor.java
@@ -0,0 +1,582 @@
+/*
+ * Copyright (C) 2023 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 android.hardware.camera2.extension;
+
+import android.annotation.FlaggedApi;
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.annotation.SystemApi;
+import android.hardware.camera2.CameraCharacteristics;
+import android.hardware.camera2.CaptureFailure;
+import android.hardware.camera2.CaptureRequest;
+import android.hardware.camera2.CaptureResult;
+import android.hardware.camera2.TotalCaptureResult;
+import android.hardware.camera2.impl.CameraMetadataNative;
+import android.hardware.camera2.impl.PhysicalCaptureResultInfo;
+import android.os.Binder;
+import android.os.RemoteException;
+import android.util.Log;
+import android.util.Pair;
+
+import com.android.internal.camera.flags.Flags;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Executor;
+
+/**
+ * An Interface to execute Camera2 capture requests.
+ * @hide
+ */
+@SystemApi
+@FlaggedApi(Flags.FLAG_CONCERT_MODE)
+public final class RequestProcessor {
+    private final static String TAG = "RequestProcessor";
+    private final IRequestProcessorImpl mRequestProcessor;
+    private final long mVendorId;
+
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    RequestProcessor (@NonNull IRequestProcessorImpl requestProcessor, long vendorId) {
+        mRequestProcessor = requestProcessor;
+        mVendorId = vendorId;
+    }
+
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    public interface RequestCallback {
+        /**
+         * This method is called when the camera device has started
+         * capturing the output image for the request, at the beginning of
+         * image exposure, or when the camera device has started
+         * processing an input image for a reprocess request.
+         *
+         * @param request The request that was given to the
+         *                RequestProcessor
+         * @param timestamp the timestamp at start of capture for a
+         *                  regular request, or the timestamp at the input
+         *                  image's start of capture for a
+         *                  reprocess request, in nanoseconds.
+         * @param frameNumber the frame number for this capture
+         *
+         */
+        @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+        void onCaptureStarted(@NonNull Request request, long frameNumber, long timestamp);
+
+        /**
+         * This method is called when an image capture makes partial forward
+         * progress; some (but not all) results from an image capture are
+         * available.
+         *
+         * <p>The result provided here will contain some subset of the fields
+         * of  a full result. Multiple {@link #onCaptureProgressed} calls may
+         * happen per capture; a given result field will only be present in
+         * one partial capture at most. The final {@link #onCaptureCompleted}
+         * call will always contain all the fields (in particular, the union
+         * of all the fields of all the partial results composing the total
+         * result).</p>
+         *
+         * <p>For each request, some result data might be available earlier
+         * than others. The typical delay between each partial result (per
+         * request) is a single frame interval.
+         * For performance-oriented use-cases, applications should query the
+         * metadata they need to make forward progress from the partial
+         * results and avoid waiting for the completed result.</p>
+         *
+         * <p>For a particular request, {@link #onCaptureProgressed} may happen
+         * before or after {@link #onCaptureStarted}.</p>
+         *
+         * <p>Each request will generate at least {@code 1} partial results,
+         * and at most {@link
+         * CameraCharacteristics#REQUEST_PARTIAL_RESULT_COUNT} partial
+         * results.</p>
+         *
+         * <p>Depending on the request settings, the number of partial
+         * results per request  will vary, although typically the partial
+         * count could be the same as long as the
+         * camera device subsystems enabled stay the same.</p>
+         *
+         * @param request The request that was given to the RequestProcessor
+         * @param partialResult The partial output metadata from the capture,
+         *                      which includes a subset of the {@link
+         *                      TotalCaptureResult} fields.
+         */
+        @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+        void onCaptureProgressed(@NonNull Request request, @NonNull CaptureResult partialResult);
+
+        /**
+         * This method is called when an image capture has fully completed and
+         * all the result metadata is available.
+         *
+         * <p>This callback will always fire after the last {@link
+         * #onCaptureProgressed}; in other words, no more partial results will
+         * be delivered once the completed result is available.</p>
+         *
+         * <p>For performance-intensive use-cases where latency is a factor,
+         * consider using {@link #onCaptureProgressed} instead.</p>
+         *
+         *
+         * @param request The request that was given to the RequestProcessor
+         * @param totalCaptureResult The total output metadata from the
+         *                           capture, including the final capture
+         *                           parameters and the state of the camera
+         *                           system during capture.
+         */
+        @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+        void onCaptureCompleted(@NonNull Request request,
+                @Nullable TotalCaptureResult totalCaptureResult);
+
+        /**
+         * This method is called instead of {@link #onCaptureCompleted} when the
+         * camera device failed to produce a {@link CaptureResult} for the
+         * request.
+         *
+         * <p>Other requests are unaffected, and some or all image buffers
+         * from the capture may have been pushed to their respective output
+         * streams.</p>
+         *
+         * <p>If a logical multi-camera fails to generate capture result for
+         * one of its physical cameras, this method will be called with a
+         * {@link CaptureFailure} for that physical camera. In such cases, as
+         * long as the logical camera capture result is valid, {@link
+         * #onCaptureCompleted} will still be called.</p>
+         *
+         * <p>The default implementation of this method does nothing.</p>
+         *
+         * @param request The request that was given to the RequestProcessor
+         * @param failure The output failure from the capture, including the
+         *                failure reason and the frame number.
+         */
+        @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+        void onCaptureFailed(@NonNull Request request, @NonNull CaptureFailure failure);
+
+        /**
+         * <p>This method is called if a single buffer for a capture could not
+         * be sent to its destination surface.</p>
+         *
+         * <p>If the whole capture failed, then {@link #onCaptureFailed} will be
+         * called instead. If some but not all buffers were captured but the
+         * result metadata will not be available, then captureFailed will be
+         * invoked with {@link CaptureFailure#wasImageCaptured}
+         * returning true, along with one or more calls to {@link
+         * #onCaptureBufferLost} for the failed outputs.</p>
+         *
+         * @param request The request that was given to the RequestProcessor
+         * @param frameNumber The frame number for the request
+         * @param outputStreamId The output stream id that the buffer will not
+         *                       be produced for
+         */
+        @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+        void onCaptureBufferLost(@NonNull Request request, long frameNumber, int outputStreamId);
+
+        /**
+         * This method is called independently of the others in
+         * CaptureCallback, when a capture sequence finishes and all {@link
+         * CaptureResult} or {@link CaptureFailure} for it have been returned
+         * via this listener.
+         *
+         * <p>In total, there will be at least one result/failure returned by
+         * this listener  before this callback is invoked. If the capture
+         * sequence is aborted before any requests have been processed,
+         * {@link #onCaptureSequenceAborted} is invoked instead.</p>
+         *
+         * @param sequenceId A sequence ID returned by the RequestProcessor
+         *                   capture family of methods
+         * @param frameNumber The last frame number (returned by {@link
+         *                    CaptureResult#getFrameNumber}
+         *                    or {@link CaptureFailure#getFrameNumber}) in
+         *                    the capture sequence.
+         */
+        @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+        void onCaptureSequenceCompleted(int sequenceId, long frameNumber);
+
+        /**
+         * This method is called independently of the others in
+         * CaptureCallback, when a capture sequence aborts before any {@link
+         * CaptureResult} or {@link CaptureFailure} for it have been returned
+         * via this listener.
+         *
+         * <p>Due to the asynchronous nature of the camera device, not all
+         * submitted captures are immediately processed. It is possible to
+         * clear out the pending requests by a variety of operations such as
+         * {@link RequestProcessor#stopRepeating} or
+         * {@link RequestProcessor#abortCaptures}. When such an event
+         * happens, {@link #onCaptureSequenceCompleted} will not be called.</p>
+         * @param sequenceId A sequence ID returned by the RequestProcessor
+         *                   capture family of methods
+         */
+        @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+        void onCaptureSequenceAborted(int sequenceId);
+    }
+
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    public final static class Request {
+        private final List<Integer> mOutputIds;
+        private final List<Pair<CaptureRequest.Key, Object>> mParameters;
+        private final int mTemplateId;
+
+        @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+        public Request(@NonNull List<Integer> outputConfigIds,
+                @NonNull List<Pair<CaptureRequest.Key, Object>> parameters, int templateId) {
+            mOutputIds = outputConfigIds;
+            mParameters = parameters;
+            mTemplateId = templateId;
+        }
+
+        /**
+         * Gets the target ids of {@link ExtensionOutputConfiguration} which identifies
+         * corresponding Surface to be the targeted for the request.
+         */
+        @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+        @NonNull
+        List<Integer> getOutputConfigIds() {
+            return mOutputIds;
+        }
+
+        /**
+         * Gets all the parameters.
+         */
+        @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+        @NonNull
+        List<Pair<CaptureRequest.Key, Object>> getParameters() {
+            return mParameters;
+        }
+
+        /**
+         * Gets the template id.
+         */
+        @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+        Integer getTemplateId() {
+            return mTemplateId;
+        }
+
+        @NonNull List<OutputConfigId> getTargetIds() {
+            ArrayList<OutputConfigId> ret = new ArrayList<>(mOutputIds.size());
+            int idx = 0;
+            for (Integer outputId : mOutputIds) {
+                OutputConfigId configId = new OutputConfigId();
+                configId.id = outputId;
+                ret.add(idx++, configId);
+            }
+
+            return ret;
+        }
+
+        @NonNull
+        static CameraMetadataNative getParametersMetadata(long vendorId,
+                @NonNull List<Pair<CaptureRequest.Key, Object>> parameters) {
+            CameraMetadataNative ret = new CameraMetadataNative();
+            ret.setVendorId(vendorId);
+            for (Pair<CaptureRequest.Key, Object> pair : parameters) {
+                ret.set(pair.first, pair.second);
+            }
+
+            return ret;
+        }
+
+        @NonNull
+        static List<android.hardware.camera2.extension.Request> initializeParcelable(
+                long vendorId, @NonNull List<Request> requests) {
+            ArrayList<android.hardware.camera2.extension.Request> ret =
+                    new ArrayList<>(requests.size());
+            int requestId = 0;
+            for (Request req : requests) {
+                android.hardware.camera2.extension.Request request =
+                        new android.hardware.camera2.extension.Request();
+                request.requestId = requestId++;
+                request.templateId = req.getTemplateId();
+                request.targetOutputConfigIds = req.getTargetIds();
+                request.parameters = getParametersMetadata(vendorId, req.getParameters());
+                ret.add(request.requestId, request);
+            }
+
+            return ret;
+        }
+    }
+
+    /**
+     * Submit a capture request.
+     * @param request  Capture request to queued in the Camera2 session
+     * @param executor the executor which will be used for
+     *                 invoking the callbacks or null to use the
+     *                 current thread's looper
+     * @param callback Request callback implementation
+     * @return the id of the capture sequence or -1 in case the processor
+     *         encounters a fatal error or receives an invalid argument.
+     */
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    public int submit(@NonNull Request request, @Nullable Executor executor,
+            @NonNull RequestCallback callback) {
+        ArrayList<Request> requests = new ArrayList<>(1);
+        requests.add(0, request);
+        List<android.hardware.camera2.extension.Request> parcelableRequests =
+                Request.initializeParcelable(mVendorId, requests);
+
+        try {
+            return mRequestProcessor.submit(parcelableRequests.get(0),
+                    new RequestCallbackImpl(requests, callback, executor));
+        } catch (RemoteException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Submits a list of requests.
+     * @param requests List of capture requests to be queued in the
+     *                 Camera2 session
+     * @param executor the executor which will be used for
+     *                 invoking the callbacks or null to use the
+     *                 current thread's looper
+     * @param callback Request callback implementation
+     * @return the id of the capture sequence or -1 in case the processor
+     *         encounters a fatal error or receives an invalid argument.
+     */
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    public int submitBurst(@NonNull List<Request> requests, @Nullable Executor executor,
+            @NonNull RequestCallback callback) {
+        List<android.hardware.camera2.extension.Request> parcelableRequests =
+                Request.initializeParcelable(mVendorId, requests);
+
+        try {
+            return mRequestProcessor.submitBurst(parcelableRequests,
+                    new RequestCallbackImpl(requests, callback, executor));
+        } catch (RemoteException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Set a repeating request.
+     * @param request  Repeating capture request to be se in the
+     *                 Camera2 session
+     * @param executor the executor which will be used for
+     *                 invoking the callbacks or null to use the
+     *                 current thread's looper
+     * @param callback Request callback implementation
+     * @return the id of the capture sequence or -1 in case the processor
+     *         encounters a fatal error or receives an invalid argument.
+     */
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    public int setRepeating(@NonNull Request request, @Nullable Executor executor,
+            @NonNull RequestCallback callback) {
+        ArrayList<Request> requests = new ArrayList<>(1);
+        requests.add(0, request);
+        List<android.hardware.camera2.extension.Request> parcelableRequests =
+                Request.initializeParcelable(mVendorId, requests);
+
+        try {
+            return mRequestProcessor.setRepeating(parcelableRequests.get(0),
+                    new RequestCallbackImpl(requests, callback, executor));
+        } catch (RemoteException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Abort all ongoing capture requests.
+     */
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    public void abortCaptures() {
+        try {
+            mRequestProcessor.abortCaptures();
+        } catch (RemoteException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Stop the current repeating request.
+     */
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    public void stopRepeating() {
+        try {
+            mRequestProcessor.stopRepeating();
+        } catch (RemoteException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    private static class RequestCallbackImpl extends IRequestCallback.Stub {
+        private final List<Request> mRequests;
+        private final RequestCallback mCallback;
+        private final Executor mExecutor;
+
+        public RequestCallbackImpl(@NonNull List<Request> requests,
+                @NonNull RequestCallback callback, @Nullable Executor executor) {
+            mCallback = callback;
+            mRequests = requests;
+            mExecutor = executor;
+        }
+
+        @Override
+        public void onCaptureStarted(int requestId, long frameNumber, long timestamp) {
+            if (mRequests.get(requestId) != null) {
+                final long ident = Binder.clearCallingIdentity();
+                try {
+                    if (mExecutor != null) {
+                        mExecutor.execute(() -> mCallback.onCaptureStarted(
+                                mRequests.get(requestId), frameNumber, timestamp));
+                    } else {
+                        mCallback.onCaptureStarted(mRequests.get(requestId), frameNumber,
+                                timestamp);
+                    }
+                } finally {
+                    Binder.restoreCallingIdentity(ident);
+                }
+            } else {
+                Log.e(TAG,"Request id: " + requestId + " not found!");
+            }
+        }
+
+        @Override
+        public void onCaptureProgressed(int requestId, ParcelCaptureResult partialResult) {
+            if (mRequests.get(requestId) != null) {
+                CaptureResult result = new CaptureResult(partialResult.cameraId,
+                        partialResult.results, partialResult.parent, partialResult.sequenceId,
+                        partialResult.frameNumber);
+                final long ident = Binder.clearCallingIdentity();
+                try {
+                    if (mExecutor != null) {
+                        mExecutor.execute(
+                                () -> mCallback.onCaptureProgressed(mRequests.get(requestId),
+                                        result));
+                    } else {
+                        mCallback.onCaptureProgressed(mRequests.get(requestId), result);
+                    }
+
+                } finally {
+                    Binder.restoreCallingIdentity(ident);
+                }
+            } else {
+                Log.e(TAG,"Request id: " + requestId + " not found!");
+            }
+        }
+
+        @Override
+        public void onCaptureCompleted(int requestId, ParcelTotalCaptureResult totalCaptureResult) {
+            if (mRequests.get(requestId) != null) {
+                PhysicalCaptureResultInfo[] physicalResults = new PhysicalCaptureResultInfo[0];
+                if ((totalCaptureResult.physicalResult != null) &&
+                        (!totalCaptureResult.physicalResult.isEmpty())) {
+                    int count = totalCaptureResult.physicalResult.size();
+                    physicalResults = new PhysicalCaptureResultInfo[count];
+                    physicalResults = totalCaptureResult.physicalResult.toArray(
+                            physicalResults);
+                }
+                ArrayList<CaptureResult> partials = new ArrayList<>(
+                        totalCaptureResult.partials.size());
+                for (ParcelCaptureResult parcelResult : totalCaptureResult.partials) {
+                    partials.add(new CaptureResult(parcelResult.cameraId, parcelResult.results,
+                            parcelResult.parent, parcelResult.sequenceId,
+                            parcelResult.frameNumber));
+                }
+                TotalCaptureResult result = new TotalCaptureResult(
+                        totalCaptureResult.logicalCameraId, totalCaptureResult.results,
+                        totalCaptureResult.parent, totalCaptureResult.sequenceId,
+                        totalCaptureResult.frameNumber, partials, totalCaptureResult.sessionId,
+                        physicalResults);
+                final long ident = Binder.clearCallingIdentity();
+                try {
+                    if (mExecutor != null) {
+                        mExecutor.execute(
+                                () -> mCallback.onCaptureCompleted(mRequests.get(requestId),
+                                        result));
+                    } else {
+                        mCallback.onCaptureCompleted(mRequests.get(requestId), result);
+                    }
+                } finally {
+                    Binder.restoreCallingIdentity(ident);
+                }
+            } else {
+                Log.e(TAG,"Request id: " + requestId + " not found!");
+            }
+        }
+
+        @Override
+        public void onCaptureFailed(int requestId,
+                android.hardware.camera2.extension.CaptureFailure captureFailure) {
+            if (mRequests.get(requestId) != null) {
+                android.hardware.camera2.CaptureFailure failure =
+                        new android.hardware.camera2.CaptureFailure(captureFailure.request,
+                                captureFailure.reason, captureFailure.dropped,
+                                captureFailure.sequenceId, captureFailure.frameNumber,
+                                captureFailure.errorPhysicalCameraId);
+                final long ident = Binder.clearCallingIdentity();
+                try {
+                    if (mExecutor != null) {
+                        mExecutor.execute(() -> mCallback.onCaptureFailed(mRequests.get(requestId),
+                                failure));
+                    } else {
+                        mCallback.onCaptureFailed(mRequests.get(requestId), failure);
+                    }
+                } finally {
+                    Binder.restoreCallingIdentity(ident);
+                }
+            } else {
+                Log.e(TAG,"Request id: " + requestId + " not found!");
+            }
+        }
+
+        @Override
+        public void onCaptureBufferLost(int requestId, long frameNumber, int outputStreamId) {
+            if (mRequests.get(requestId) != null) {
+                final long ident = Binder.clearCallingIdentity();
+                try {
+                    if (mExecutor != null) {
+                        mExecutor.execute(
+                                () -> mCallback.onCaptureBufferLost(mRequests.get(requestId),
+                                        frameNumber, outputStreamId));
+                    } else {
+                        mCallback.onCaptureBufferLost(mRequests.get(requestId), frameNumber,
+                                outputStreamId);
+                    }
+                } finally {
+                    Binder.restoreCallingIdentity(ident);
+                }
+            } else {
+                Log.e(TAG,"Request id: " + requestId + " not found!");
+            }
+        }
+
+        @Override
+        public void onCaptureSequenceCompleted(int sequenceId, long frameNumber) {
+            final long ident = Binder.clearCallingIdentity();
+            try {
+                if (mExecutor != null) {
+                    mExecutor.execute(() -> mCallback.onCaptureSequenceCompleted(sequenceId,
+                            frameNumber));
+                } else {
+                    mCallback.onCaptureSequenceCompleted(sequenceId, frameNumber);
+                }
+            } finally {
+                Binder.restoreCallingIdentity(ident);
+            }
+        }
+
+        @Override
+        public void onCaptureSequenceAborted(int sequenceId) {
+            final long ident = Binder.clearCallingIdentity();
+            try {
+                if (mExecutor != null) {
+                    mExecutor.execute(() -> mCallback.onCaptureSequenceAborted(sequenceId));
+                } else {
+                    mCallback.onCaptureSequenceAborted(sequenceId);
+                }
+            } finally {
+                Binder.restoreCallingIdentity(ident);
+            }
+        }
+    }
+}
diff --git a/core/java/android/hardware/camera2/extension/SessionProcessor.java b/core/java/android/hardware/camera2/extension/SessionProcessor.java
new file mode 100644
index 0000000..6ed0c14
--- /dev/null
+++ b/core/java/android/hardware/camera2/extension/SessionProcessor.java
@@ -0,0 +1,495 @@
+/*
+ * Copyright (C) 2023 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 android.hardware.camera2.extension;
+
+import android.annotation.FlaggedApi;
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.annotation.SystemApi;
+import android.hardware.camera2.CameraCharacteristics;
+import android.hardware.camera2.CaptureRequest;
+import android.hardware.camera2.CaptureResult;
+import android.hardware.camera2.impl.CameraMetadataNative;
+import android.os.IBinder;
+import android.os.RemoteException;
+import android.util.Log;
+
+import com.android.internal.camera.flags.Flags;
+
+import java.util.ArrayList;
+import java.util.Map;
+import java.util.concurrent.Executor;
+
+/**
+ * Interface for creating Camera2 CameraCaptureSessions with extension
+ * enabled based on the advanced extension interface.
+ *
+ * <p><pre>
+ * The flow of a extension session is shown below:
+ * (1) {@link #initSession}: Camera framework prepares streams
+ * configuration for creating CameraCaptureSession. Output surfaces for
+ * Preview and ImageCapture are passed in and implementation is
+ * responsible for outputting the results to these surfaces.
+ *
+ * (2) {@link #onCaptureSessionStart}: It is called after
+ * CameraCaptureSession is configured. A {@link RequestProcessor} is
+ * passed for the implementation to send repeating requests and single
+ * requests.
+ *
+ * (3) {@link #startRepeating}:  Camera framework will call this method to
+ * start the repeating request after CameraCaptureSession is called.
+ * Implementations should start the repeating request by  {@link
+ * RequestProcessor}. Implementations can also update the repeating
+ * request if needed later.
+ *
+ * (4) {@link #setParameters}: The passed parameters will be attached
+ * to the repeating request and single requests but the implementation can
+ * choose to apply some of them only.
+ *
+ * (5) {@link #startCapture}: It is called when apps want
+ * to start a multi-frame image capture.  {@link CaptureCallback} will be
+ * called to report the status and the output image will be written to the
+ * capture output surface specified in {@link #initSession}.
+ *
+ * (5) {@link #onCaptureSessionEnd}: It is called right BEFORE
+ * CameraCaptureSession.close() is called.
+ *
+ * (6) {@link #deInitSession}: called when CameraCaptureSession is closed.
+ * </pre>
+ * @hide
+ */
+@SystemApi
+@FlaggedApi(Flags.FLAG_CONCERT_MODE)
+public abstract class SessionProcessor {
+    private static final String TAG = "SessionProcessor";
+
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    protected SessionProcessor() {}
+
+    /**
+     * Callback for notifying the status of {@link
+     * #startCapture} and {@link #startRepeating}.
+     * @hide
+     */
+    @SystemApi
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    public interface CaptureCallback {
+        /**
+         * This method is called when the camera device has started
+         * capturing the initial input
+         * image.
+         *
+         * For a multi-frame capture, the method is called when the
+         * CameraCaptureSession.CaptureCallback onCaptureStarted of first
+         * frame is called and its timestamp is directly forwarded to
+         * timestamp parameter of this method.
+         *
+         * @param captureSequenceId id of the current capture sequence
+         * @param timestamp         the timestamp at start of capture for
+         *                          repeating request or the timestamp at
+         *                          start of capture of the
+         *                          first frame in a multi-frame capture,
+         *                          in nanoseconds.
+         */
+        @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+        void onCaptureStarted(int captureSequenceId, long timestamp);
+
+        /**
+         * This method is called when an image (or images in case of
+         * multi-frame capture) is captured and device-specific extension
+         * processing is triggered.
+         *
+         * @param captureSequenceId id of the current capture sequence
+         */
+        @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+        void onCaptureProcessStarted(int captureSequenceId);
+
+        /**
+         * This method is called instead of
+         * {@link #onCaptureProcessStarted} when the camera device failed
+         * to produce the required input for the device-specific
+         * extension. The cause could be a failed camera capture request,
+         * a failed capture result or dropped camera frame.
+         *
+         * @param captureSequenceId id of the current capture sequence
+         */
+        @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+        void onCaptureFailed(int captureSequenceId);
+
+        /**
+         * This method is called independently of the others in the
+         * CaptureCallback, when a capture sequence finishes.
+         *
+         * <p>In total, there will be at least one
+         * {@link #onCaptureProcessStarted}/{@link #onCaptureFailed}
+         * invocation before this callback is triggered. If the capture
+         * sequence is aborted before any requests have begun processing,
+         * {@link #onCaptureSequenceAborted} is invoked instead.</p>
+         *
+         * @param captureSequenceId id of the current capture sequence
+         */
+        @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+        void onCaptureSequenceCompleted(int captureSequenceId);
+
+        /**
+         * This method is called when a capture sequence aborts.
+         *
+         * @param captureSequenceId id of the current capture sequence
+         */
+        @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+        void onCaptureSequenceAborted(int captureSequenceId);
+
+        /**
+         * Capture result callback that needs to be called when the
+         * process capture results are ready as part of frame
+         * post-processing.
+         *
+         * This callback will fire after {@link #onCaptureStarted}, {@link
+         * #onCaptureProcessStarted} and before {@link
+         * #onCaptureSequenceCompleted}. The callback is not expected to
+         * fire in case of capture failure  {@link #onCaptureFailed} or
+         * capture abort {@link #onCaptureSequenceAborted}.
+         *
+         * @param shutterTimestamp The timestamp at the start
+         *                         of capture. The same timestamp value
+         *                         passed to {@link #onCaptureStarted}.
+         * @param requestId  the capture request id that generated the
+         *                   capture results. This is the return value of
+         *                   either {@link #startRepeating} or {@link
+         *                   #startCapture}.
+         * @param results  The supported capture results. Do note
+         *                  that if results 'android.jpeg.quality' and
+         *                  android.jpeg.orientation' are present in the
+         *                  process capture input results, then the values
+         *                  must also be passed as part of this callback.
+         *                  The camera framework guarantees that those two
+         *                  settings and results are always supported and
+         *                  applied by the corresponding framework.
+         */
+        @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+        void onCaptureCompleted(long shutterTimestamp, int requestId,
+                @NonNull CaptureResult results);
+    }
+
+    /**
+     * Initializes the session for the extension. This is where the
+     * extension implementations allocate resources for
+     * preparing a CameraCaptureSession. After initSession() is called,
+     * the camera ID, cameraCharacteristics and context will not change
+     * until deInitSession() has been called.
+     *
+     * <p>The framework specifies the output surface configurations for
+     * preview using the 'previewSurface' argument and for still capture
+     * using the 'imageCaptureSurface' argument and implementations must
+     * return a {@link ExtensionConfiguration} which consists of a list of
+     * {@link CameraOutputSurface} and session parameters. The {@link
+     * ExtensionConfiguration} will be used to configure the
+     * CameraCaptureSession.
+     *
+     * <p>Implementations are responsible for outputting correct camera
+     * images output to these output surfaces.</p>
+     *
+     * @param token Binder token that can be used to register a death
+     *              notifier callback
+     * @param cameraId  The camera2 id string of the camera.
+     * @param map Maps camera ids to camera characteristics
+     * @param previewSurface contains output surface for preview
+     * @param imageCaptureSurface contains the output surface for image
+     *                            capture
+     * @return a {@link ExtensionConfiguration} consisting of a list of
+     * {@link CameraOutputConfig} and session parameters which will decide
+     * the  {@link android.hardware.camera2.params.SessionConfiguration}
+     * for configuring the CameraCaptureSession. Please note that the
+     * OutputConfiguration list may not be part of any
+     * supported or mandatory stream combination BUT implementations must
+     * ensure this list will always  produce a valid camera capture
+     * session.
+     */
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    @NonNull
+    public abstract ExtensionConfiguration initSession(@NonNull IBinder token,
+            @NonNull String cameraId, @NonNull CharacteristicsMap map,
+            @NonNull CameraOutputSurface previewSurface,
+            @NonNull CameraOutputSurface imageCaptureSurface);
+
+    /**
+     * Notify to de-initialize the extension. This callback will be
+     * invoked after CameraCaptureSession is closed. After onDeInit() was
+     * called, it is expected that the camera ID, cameraCharacteristics
+     * will no longer hold and tear down any resources allocated
+     * for this extension. Aborts all pending captures.
+     * @param token Binder token that can be used to unlink any previously
+     *              linked death notifier callbacks
+     */
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    public abstract void deInitSession(@NonNull IBinder token);
+
+    /**
+     * This will be invoked once after the {@link
+     * android.hardware.camera2.CameraCaptureSession}
+     * has been created. {@link RequestProcessor} is passed for
+     * implementations to submit single requests or set repeating
+     * requests. This extension RequestProcessor will be valid to use
+     * until onCaptureSessionEnd is called.
+     * @param requestProcessor The request processor to be used for
+     *                         managing capture requests
+     * @param statsKey         Unique key for telemetry
+     */
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    public abstract void onCaptureSessionStart(@NonNull RequestProcessor requestProcessor,
+            @NonNull String statsKey);
+
+    /**
+     * This will be invoked before the {@link
+     * android.hardware.camera2.CameraCaptureSession} is
+     * closed. {@link RequestProcessor} passed in onCaptureSessionStart
+     * will no longer accept any requests after onCaptureSessionEnd()
+     * returns.
+     */
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    public abstract void onCaptureSessionEnd();
+
+    /**
+     * Starts the repeating request after CameraCaptureSession is called.
+     * Implementations should start the repeating request by {@link
+     * RequestProcessor}. Implementations can also update the
+     * repeating request when needed later.
+     *
+     * @param executor the executor which will be used for
+     *                 invoking the callbacks or null to use the
+     *                 current thread's looper
+     * @param callback a callback to report the status.
+     * @return the id of the capture sequence.
+     */
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    public abstract int startRepeating(@Nullable Executor executor,
+            @NonNull CaptureCallback callback);
+
+    /**
+     * Stop the repeating request. To prevent implementations from not
+     * calling stopRepeating, the framework will first stop the repeating
+     * request of current CameraCaptureSession and call this API to signal
+     * implementations that the repeating request was stopped and going
+     * forward calling {@link RequestProcessor#setRepeating} will simply
+     * do nothing.
+     */
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    public abstract void stopRepeating();
+
+    /**
+     * Start a multi-frame capture.
+     *
+     * When the capture is completed, {@link
+     * CaptureCallback#onCaptureSequenceCompleted}
+     * is called and {@code OnImageAvailableListener#onImageAvailable}
+     * will also be called on the ImageReader that creates the image
+     * capture output surface.
+     *
+     * <p>Only one capture can perform at a time. Starting a capture when
+     * another capture is running  will cause onCaptureFailed to be called
+     * immediately.
+     *
+     * @param executor the executor which will be used for
+     *                 invoking the callbacks or null to use the
+     *                 current thread's looper
+     * @param callback a callback to report the status.
+     * @return the id of the capture sequence.
+     */
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    public abstract int startCapture(@Nullable Executor executor,
+            @NonNull CaptureCallback callback);
+
+    /**
+     * The camera framework will call these APIs to pass parameters from
+     * the app to the extension implementation. It is expected that the
+     * implementation would (eventually) update the repeating request if
+     * the keys are supported. Setting a value to null explicitly un-sets
+     * the value.
+     *@param captureRequest Request that includes all client parameter
+     */
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    public abstract void setParameters(@NonNull CaptureRequest captureRequest);
+
+    /**
+     * The camera framework will call this interface in response to client
+     * requests involving  the output preview surface. Typical examples
+     * include requests that include AF/AE triggers.
+     * Extensions can disregard any capture request keys that were not
+     * advertised in
+     * {@link AdvancedExtender#getAvailableCaptureRequestKeys}.
+     *
+     * @param captureRequest Capture request that includes the respective
+     *                       triggers.
+     * @param executor the executor which will be used for
+     *                 invoking the callbacks or null to use the
+     *                 current thread's looper
+     * @param callback a callback to report the status.
+     * @return the id of the capture sequence.
+     *
+     */
+    @FlaggedApi(Flags.FLAG_CONCERT_MODE)
+    public abstract int startTrigger(@NonNull CaptureRequest captureRequest,
+            @Nullable Executor executor, @NonNull CaptureCallback callback);
+
+    private final class SessionProcessorImpl extends ISessionProcessorImpl.Stub {
+        private long mVendorId = -1;
+        @Override
+        public CameraSessionConfig initSession(IBinder token, String cameraId,
+                Map<String, CameraMetadataNative> charsMap, OutputSurface previewSurface,
+                OutputSurface imageCaptureSurface, OutputSurface postviewSurface)
+                throws RemoteException {
+            ExtensionConfiguration config = SessionProcessor.this.initSession(token, cameraId,
+                    new CharacteristicsMap(charsMap),
+                    new CameraOutputSurface(previewSurface),
+                    new CameraOutputSurface(imageCaptureSurface));
+            if (config == null) {
+                throw  new  IllegalArgumentException("Invalid extension configuration");
+            }
+
+            Object thisClass = CameraCharacteristics.Key.class;
+            Class<CameraCharacteristics.Key<?>> keyClass =
+                    (Class<CameraCharacteristics.Key<?>>)thisClass;
+            ArrayList<CameraCharacteristics.Key<?>> vendorKeys =
+                    charsMap.get(cameraId).getAllVendorKeys(keyClass);
+            if ((vendorKeys != null) && !vendorKeys.isEmpty()) {
+                mVendorId = vendorKeys.get(0).getVendorId();
+            }
+            return config.getCameraSessionConfig();
+        }
+
+        @Override
+        public void deInitSession(IBinder token) throws RemoteException {
+            SessionProcessor.this.deInitSession(token);
+        }
+
+        @Override
+        public void onCaptureSessionStart(IRequestProcessorImpl requestProcessor, String statsKey)
+                throws RemoteException {
+            SessionProcessor.this.onCaptureSessionStart(
+                    new RequestProcessor(requestProcessor, mVendorId), statsKey);
+        }
+
+        @Override
+        public void onCaptureSessionEnd() throws RemoteException {
+            SessionProcessor.this.onCaptureSessionEnd();
+        }
+
+        @Override
+        public int startRepeating(ICaptureCallback callback) throws RemoteException {
+            return SessionProcessor.this.startRepeating(/*executor*/ null,
+                    new CaptureCallbackImpl(callback));
+        }
+
+        @Override
+        public void stopRepeating() throws RemoteException {
+            SessionProcessor.this.stopRepeating();
+        }
+
+        @Override
+        public int startCapture(ICaptureCallback callback, boolean isPostviewRequested)
+                throws RemoteException {
+            return SessionProcessor.this.startCapture(/*executor*/ null,
+                    new CaptureCallbackImpl(callback));
+        }
+
+        @Override
+        public void setParameters(CaptureRequest captureRequest) throws RemoteException {
+            SessionProcessor.this.setParameters(captureRequest);
+        }
+
+        @Override
+        public int startTrigger(CaptureRequest captureRequest, ICaptureCallback callback)
+                throws RemoteException {
+            return SessionProcessor.this.startTrigger(captureRequest, /*executor*/ null,
+                    new CaptureCallbackImpl(callback));
+        }
+
+        @Override
+        public LatencyPair getRealtimeCaptureLatency() throws RemoteException {
+            // Feature is not supported
+            return null;
+        }
+    }
+
+    private static final class CaptureCallbackImpl implements CaptureCallback {
+        private final ICaptureCallback mCaptureCallback;
+
+        CaptureCallbackImpl(@NonNull ICaptureCallback cb) {
+            mCaptureCallback = cb;
+        }
+
+        @Override
+        public void onCaptureStarted(int captureSequenceId, long timestamp) {
+            try {
+                mCaptureCallback.onCaptureStarted(captureSequenceId, timestamp);
+            } catch (RemoteException e) {
+                Log.e(TAG, "Failed to notify capture start due to remote exception!");
+            }
+        }
+
+        @Override
+        public void onCaptureProcessStarted(int captureSequenceId) {
+            try {
+                mCaptureCallback.onCaptureProcessStarted(captureSequenceId);
+            } catch (RemoteException e) {
+                Log.e(TAG, "Failed to notify process start due to remote exception!");
+            }
+        }
+
+        @Override
+        public void onCaptureFailed(int captureSequenceId) {
+            try {
+                mCaptureCallback.onCaptureFailed(captureSequenceId);
+            } catch (RemoteException e) {
+                Log.e(TAG, "Failed to notify capture failure start due to remote exception!");
+            }
+        }
+
+        @Override
+        public void onCaptureSequenceCompleted(int captureSequenceId) {
+            try {
+                mCaptureCallback.onCaptureSequenceCompleted(captureSequenceId);
+            } catch (RemoteException e) {
+                Log.e(TAG, "Failed to notify capture sequence done due to remote exception!");
+            }
+        }
+
+        @Override
+        public void onCaptureSequenceAborted(int captureSequenceId) {
+            try {
+                mCaptureCallback.onCaptureSequenceAborted(captureSequenceId);
+            } catch (RemoteException e) {
+                Log.e(TAG, "Failed to notify capture sequence abort due to remote exception!");
+            }
+        }
+
+        @Override
+        public void onCaptureCompleted(long shutterTimestamp, int requestId,
+                @androidx.annotation.NonNull CaptureResult results) {
+            try {
+                mCaptureCallback.onCaptureCompleted(shutterTimestamp, requestId,
+                        results.getNativeCopy());
+            } catch (RemoteException e) {
+                Log.e(TAG, "Failed to notify capture complete due to remote exception!");
+            }
+        }
+    }
+
+    @NonNull ISessionProcessorImpl getSessionProcessorBinder() {
+        return new SessionProcessorImpl();
+    }
+}
diff --git a/core/java/android/hardware/camera2/impl/CameraAdvancedExtensionSessionImpl.java b/core/java/android/hardware/camera2/impl/CameraAdvancedExtensionSessionImpl.java
index 4ef4572..98bc311 100644
--- a/core/java/android/hardware/camera2/impl/CameraAdvancedExtensionSessionImpl.java
+++ b/core/java/android/hardware/camera2/impl/CameraAdvancedExtensionSessionImpl.java
@@ -674,7 +674,8 @@
                         try {
                             if (mSessionProcessor != null) {
                                 mInitialized = true;
-                                mSessionProcessor.onCaptureSessionStart(mRequestProcessor);
+                                mSessionProcessor.onCaptureSessionStart(mRequestProcessor,
+                                        mStatsAggregator.getStatsKey());
                             } else {
                                 Log.v(TAG, "Failed to start capture session, session " +
                                                 " released before extension start!");
diff --git a/core/java/android/hardware/camera2/utils/ExtensionSessionStatsAggregator.java b/core/java/android/hardware/camera2/utils/ExtensionSessionStatsAggregator.java
index 8cd5e83..3050a51 100644
--- a/core/java/android/hardware/camera2/utils/ExtensionSessionStatsAggregator.java
+++ b/core/java/android/hardware/camera2/utils/ExtensionSessionStatsAggregator.java
@@ -118,4 +118,13 @@
                 + "  type: '" + stats.type + "'\n"
                 + "  isAdvanced: '" + stats.isAdvanced + "'\n";
     }
+
+    /**
+     * Return the current statistics key
+     *
+     * @return the current statistics key
+     */
+    public String getStatsKey() {
+        return mStats.key;
+    }
 }
diff --git a/core/java/android/hardware/display/ColorDisplayManager.java b/core/java/android/hardware/display/ColorDisplayManager.java
index aafa7d5..f927b8b 100644
--- a/core/java/android/hardware/display/ColorDisplayManager.java
+++ b/core/java/android/hardware/display/ColorDisplayManager.java
@@ -17,12 +17,14 @@
 package android.hardware.display;
 
 import android.Manifest;
+import android.annotation.FlaggedApi;
 import android.annotation.IntDef;
 import android.annotation.IntRange;
 import android.annotation.NonNull;
 import android.annotation.RequiresPermission;
 import android.annotation.SystemApi;
 import android.annotation.SystemService;
+import android.annotation.TestApi;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.metrics.LogMaker;
@@ -397,6 +399,8 @@
      * @return {@code true} if the display is not at full saturation
      * @hide
      */
+    @TestApi
+    @FlaggedApi(android.app.Flags.FLAG_MODES_API)
     @RequiresPermission(Manifest.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS)
     public boolean isSaturationActivated() {
         return mManager.isSaturationActivated();
diff --git a/core/java/android/permission/flags.aconfig b/core/java/android/permission/flags.aconfig
index 5cbc18e..cbeb821 100644
--- a/core/java/android/permission/flags.aconfig
+++ b/core/java/android/permission/flags.aconfig
@@ -16,7 +16,7 @@
 }
 
 flag {
-    name: "role_controller_in_system_server"
+    name: "system_server_role_controller_enabled"
     is_fixed_read_only: true
     namespace: "permissions"
     description: "enable role controller in system server"
diff --git a/core/java/android/security/FileIntegrityManager.java b/core/java/android/security/FileIntegrityManager.java
index dae3202..025aac9 100644
--- a/core/java/android/security/FileIntegrityManager.java
+++ b/core/java/android/security/FileIntegrityManager.java
@@ -53,10 +53,10 @@
      * verification, although the app APIs are only made available to apps in a later SDK version.
      * Only when this method returns true, the other fs-verity APIs in the same class can succeed.
      *
-     * <p>The app may not need this method and just call the other APIs (i.e. {@link
-     * #setupFsVerity(File)} and {@link #getFsVerityDigest(File)}) normally and handle any failure.
-     * If some app feature really depends on fs-verity (e.g. protecting integrity of a large file
-     * download), an early check of support status may avoid any cost if it is to fail late.
+     * <p>The app may not need this method and just call the other APIs normally and handle any
+     * failure. If some app feature really depends on fs-verity (e.g. protecting integrity of a
+     * large file download), an early check of support status may avoid any cost if it is to fail
+     * late.
      *
      * <p>Note: for historical reasons this is named {@code isApkVeritySupported()} instead of
      * {@code isFsVeritySupported()}. It has also been available since API level 30, predating the
diff --git a/core/java/android/service/autofill/FillRequest.java b/core/java/android/service/autofill/FillRequest.java
index 7ec1483..ca20801 100644
--- a/core/java/android/service/autofill/FillRequest.java
+++ b/core/java/android/service/autofill/FillRequest.java
@@ -127,6 +127,12 @@
      */
     public static final @RequestFlags int FLAG_SCREEN_HAS_CREDMAN_FIELD = 0x400;
 
+    /**
+     * Indicate whether the user has focused on a credman field view.
+     * @hide
+     */
+    public static final @RequestFlags int FLAG_VIEW_REQUESTS_CREDMAN_SERVICE = 0x800;
+
     /** @hide */
     public static final int INVALID_REQUEST_ID = Integer.MIN_VALUE;
 
@@ -241,7 +247,8 @@
         FLAG_IME_SHOWING,
         FLAG_RESET_FILL_DIALOG_STATE,
         FLAG_PCC_DETECTION,
-        FLAG_SCREEN_HAS_CREDMAN_FIELD
+        FLAG_SCREEN_HAS_CREDMAN_FIELD,
+        FLAG_VIEW_REQUESTS_CREDMAN_SERVICE
     })
     @Retention(RetentionPolicy.SOURCE)
     @DataClass.Generated.Member
@@ -275,6 +282,8 @@
                     return "FLAG_PCC_DETECTION";
             case FLAG_SCREEN_HAS_CREDMAN_FIELD:
                     return "FLAG_SCREEN_HAS_CREDMAN_FIELD";
+            case FLAG_VIEW_REQUESTS_CREDMAN_SERVICE:
+                    return "FLAG_VIEW_REQUESTS_CREDMAN_SERVICE";
             default: return Integer.toHexString(value);
         }
     }
@@ -368,7 +377,8 @@
                         | FLAG_IME_SHOWING
                         | FLAG_RESET_FILL_DIALOG_STATE
                         | FLAG_PCC_DETECTION
-                        | FLAG_SCREEN_HAS_CREDMAN_FIELD);
+                        | FLAG_SCREEN_HAS_CREDMAN_FIELD
+                        | FLAG_VIEW_REQUESTS_CREDMAN_SERVICE);
         this.mInlineSuggestionsRequest = inlineSuggestionsRequest;
         this.mDelayedFillIntentSender = delayedFillIntentSender;
 
@@ -555,7 +565,8 @@
                         | FLAG_IME_SHOWING
                         | FLAG_RESET_FILL_DIALOG_STATE
                         | FLAG_PCC_DETECTION
-                        | FLAG_SCREEN_HAS_CREDMAN_FIELD);
+                        | FLAG_SCREEN_HAS_CREDMAN_FIELD
+                        | FLAG_VIEW_REQUESTS_CREDMAN_SERVICE);
         this.mInlineSuggestionsRequest = inlineSuggestionsRequest;
         this.mDelayedFillIntentSender = delayedFillIntentSender;
 
@@ -577,10 +588,10 @@
     };
 
     @DataClass.Generated(
-            time = 1682097266850L,
+            time = 1701010178309L,
             codegenVersion = "1.0.23",
             sourceFile = "frameworks/base/core/java/android/service/autofill/FillRequest.java",
-            inputSignatures = "public static final @android.service.autofill.FillRequest.RequestFlags int FLAG_MANUAL_REQUEST\npublic static final @android.service.autofill.FillRequest.RequestFlags int FLAG_COMPATIBILITY_MODE_REQUEST\npublic static final @android.service.autofill.FillRequest.RequestFlags int FLAG_PASSWORD_INPUT_TYPE\npublic static final @android.service.autofill.FillRequest.RequestFlags int FLAG_VIEW_NOT_FOCUSED\npublic static final @android.service.autofill.FillRequest.RequestFlags int FLAG_SUPPORTS_FILL_DIALOG\npublic static final @android.service.autofill.FillRequest.RequestFlags int FLAG_IME_SHOWING\npublic static final @android.service.autofill.FillRequest.RequestFlags int FLAG_RESET_FILL_DIALOG_STATE\npublic static final @android.service.autofill.FillRequest.RequestFlags int FLAG_PCC_DETECTION\npublic static final @android.service.autofill.FillRequest.RequestFlags int FLAG_SCREEN_HAS_CREDMAN_FIELD\npublic static final  int INVALID_REQUEST_ID\nprivate final  int mId\nprivate final @android.annotation.NonNull java.util.List<android.service.autofill.FillContext> mFillContexts\nprivate final @android.annotation.NonNull java.util.List<java.lang.String> mHints\nprivate final @android.annotation.Nullable android.os.Bundle mClientState\nprivate final @android.service.autofill.FillRequest.RequestFlags int mFlags\nprivate final @android.annotation.Nullable android.view.inputmethod.InlineSuggestionsRequest mInlineSuggestionsRequest\nprivate final @android.annotation.Nullable android.content.IntentSender mDelayedFillIntentSender\nprivate  void onConstructed()\nclass FillRequest extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genToString=true, genHiddenConstructor=true, genHiddenConstDefs=true)")
+            inputSignatures = "public static final @android.service.autofill.FillRequest.RequestFlags int FLAG_MANUAL_REQUEST\npublic static final @android.service.autofill.FillRequest.RequestFlags int FLAG_COMPATIBILITY_MODE_REQUEST\npublic static final @android.service.autofill.FillRequest.RequestFlags int FLAG_PASSWORD_INPUT_TYPE\npublic static final @android.service.autofill.FillRequest.RequestFlags int FLAG_VIEW_NOT_FOCUSED\npublic static final @android.service.autofill.FillRequest.RequestFlags int FLAG_SUPPORTS_FILL_DIALOG\npublic static final @android.service.autofill.FillRequest.RequestFlags int FLAG_IME_SHOWING\npublic static final @android.service.autofill.FillRequest.RequestFlags int FLAG_RESET_FILL_DIALOG_STATE\npublic static final @android.service.autofill.FillRequest.RequestFlags int FLAG_PCC_DETECTION\npublic static final @android.service.autofill.FillRequest.RequestFlags int FLAG_SCREEN_HAS_CREDMAN_FIELD\npublic static final @android.service.autofill.FillRequest.RequestFlags int FLAG_VIEW_REQUESTS_CREDMAN_SERVICE\npublic static final  int INVALID_REQUEST_ID\nprivate final  int mId\nprivate final @android.annotation.NonNull java.util.List<android.service.autofill.FillContext> mFillContexts\nprivate final @android.annotation.NonNull java.util.List<java.lang.String> mHints\nprivate final @android.annotation.Nullable android.os.Bundle mClientState\nprivate final @android.service.autofill.FillRequest.RequestFlags int mFlags\nprivate final @android.annotation.Nullable android.view.inputmethod.InlineSuggestionsRequest mInlineSuggestionsRequest\nprivate final @android.annotation.Nullable android.content.IntentSender mDelayedFillIntentSender\nprivate  void onConstructed()\nclass FillRequest extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genToString=true, genHiddenConstructor=true, genHiddenConstDefs=true)")
     @Deprecated
     private void __metadata() {}
 
diff --git a/core/java/android/service/notification/DeviceEffectsApplier.java b/core/java/android/service/notification/DeviceEffectsApplier.java
new file mode 100644
index 0000000..234ff4d
--- /dev/null
+++ b/core/java/android/service/notification/DeviceEffectsApplier.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2023 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 android.service.notification;
+
+/**
+ * Responsible for making any service calls needed to apply the set of {@link ZenDeviceEffects} that
+ * make sense for the current platform.
+ * @hide
+ */
+public interface DeviceEffectsApplier {
+    /**
+     * Applies the {@link ZenDeviceEffects} to the device.
+     *
+     * <p>The supplied {@code effects} represents the "consolidated" device effects, i.e. the
+     * union of the effects of all the {@link ZenModeConfig.ZenRule} instances that are currently
+     * active. If no rules are active (or no active rules specify custom effects) then {@code
+     * effects} will be all-default (i.e. {@link ZenDeviceEffects#hasEffects} will return {@code
+     * false}.
+     *
+     * <p>This will be called whenever the set of consolidated effects changes (normally through
+     * the activation or deactivation of zen rules).
+     */
+    void apply(ZenDeviceEffects effects);
+}
diff --git a/core/java/android/service/notification/ZenDeviceEffects.java b/core/java/android/service/notification/ZenDeviceEffects.java
index db0b7ff..0e82b6c 100644
--- a/core/java/android/service/notification/ZenDeviceEffects.java
+++ b/core/java/android/service/notification/ZenDeviceEffects.java
@@ -18,6 +18,7 @@
 
 import android.annotation.FlaggedApi;
 import android.annotation.NonNull;
+import android.annotation.Nullable;
 import android.app.Flags;
 import android.os.Parcel;
 import android.os.Parcelable;
@@ -359,6 +360,27 @@
             return this;
         }
 
+        /**
+         * Applies the effects that are {@code true} on the supplied {@link ZenDeviceEffects} to
+         * this builder (essentially logically-ORing the effect set).
+         * @hide
+         */
+        @NonNull
+        public Builder add(@Nullable ZenDeviceEffects effects) {
+            if (effects == null) return this;
+            if (effects.shouldDisplayGrayscale()) setShouldDisplayGrayscale(true);
+            if (effects.shouldSuppressAmbientDisplay()) setShouldSuppressAmbientDisplay(true);
+            if (effects.shouldDimWallpaper()) setShouldDimWallpaper(true);
+            if (effects.shouldUseNightMode()) setShouldUseNightMode(true);
+            if (effects.shouldDisableAutoBrightness()) setShouldDisableAutoBrightness(true);
+            if (effects.shouldDisableTapToWake()) setShouldDisableTapToWake(true);
+            if (effects.shouldDisableTiltToWake()) setShouldDisableTiltToWake(true);
+            if (effects.shouldDisableTouch()) setShouldDisableTouch(true);
+            if (effects.shouldMinimizeRadioUsage()) setShouldMinimizeRadioUsage(true);
+            if (effects.shouldMaximizeDoze()) setShouldMaximizeDoze(true);
+            return this;
+        }
+
         /** Builds a {@link ZenDeviceEffects} object based on the builder's state. */
         @NonNull
         public ZenDeviceEffects build() {
diff --git a/core/java/android/view/AttachedSurfaceControl.java b/core/java/android/view/AttachedSurfaceControl.java
index f28574e..fd5517d 100644
--- a/core/java/android/view/AttachedSurfaceControl.java
+++ b/core/java/android/view/AttachedSurfaceControl.java
@@ -27,6 +27,9 @@
 
 import com.android.window.flags.Flags;
 
+import java.util.concurrent.Executor;
+import java.util.function.Consumer;
+
 /**
  * Provides an interface to the root-Surface of a View Hierarchy or Window. This
  * is used in combination with the {@link android.view.SurfaceControl} API to enable
@@ -194,6 +197,42 @@
     }
 
     /**
+     * Add a trusted presentation listener on the SurfaceControl associated with this window.
+     *
+     * @param t          Transaction that the trusted presentation listener is added on. This should
+     *                   be applied by the caller.
+     * @param thresholds The {@link SurfaceControl.TrustedPresentationThresholds} that will specify
+     *                   when the to invoke the callback.
+     * @param executor   The {@link Executor} where the callback will be invoked on.
+     * @param listener   The {@link Consumer} that will receive the callbacks when entered or
+     *                   exited the threshold.
+     *
+     * @see SurfaceControl.Transaction#setTrustedPresentationCallback(SurfaceControl,
+     * SurfaceControl.TrustedPresentationThresholds, Executor, Consumer)
+     *
+     * @hide b/287076178 un-hide with API bump
+     */
+    default void addTrustedPresentationCallback(@NonNull SurfaceControl.Transaction t,
+            @NonNull SurfaceControl.TrustedPresentationThresholds thresholds,
+            @NonNull Executor executor, @NonNull Consumer<Boolean> listener) {
+    }
+
+    /**
+     * Remove a trusted presentation listener on the SurfaceControl associated with this window.
+     *
+     * @param t          Transaction that the trusted presentation listener removed on. This should
+     *                   be applied by the caller.
+     * @param listener   The {@link Consumer} that was previously registered with
+     *                   addTrustedPresentationCallback that should be removed.
+     *
+     * @see SurfaceControl.Transaction#clearTrustedPresentationCallback(SurfaceControl)
+     * @hide b/287076178 un-hide with API bump
+     */
+    default void removeTrustedPresentationCallback(@NonNull SurfaceControl.Transaction t,
+            @NonNull Consumer<Boolean> listener) {
+    }
+
+    /**
      * Transfer the currently in progress touch gesture from the host to the requested
      * {@link SurfaceControlViewHost.SurfacePackage}. This requires that the
      * SurfaceControlViewHost was created with the current host's inputToken.
diff --git a/core/java/android/view/IWindowManager.aidl b/core/java/android/view/IWindowManager.aidl
index 36b74e3..17bbee6d0 100644
--- a/core/java/android/view/IWindowManager.aidl
+++ b/core/java/android/view/IWindowManager.aidl
@@ -73,8 +73,6 @@
 import android.window.ITaskFpsCallback;
 import android.window.ScreenCapture;
 import android.window.WindowContextInfo;
-import android.window.ITrustedPresentationListener;
-import android.window.TrustedPresentationThresholds;
 
 /**
  * System private interface to the window manager.
@@ -1077,10 +1075,4 @@
     @JavaPassthrough(annotation = "@android.annotation.RequiresPermission(android.Manifest"
             + ".permission.MONITOR_INPUT)")
     void unregisterDecorViewGestureListener(IDecorViewGestureListener listener, int displayId);
-
-    void registerTrustedPresentationListener(in IBinder window, in ITrustedPresentationListener listener,
-            in TrustedPresentationThresholds thresholds, int id);
-
-
-    void unregisterTrustedPresentationListener(in ITrustedPresentationListener listener, int id);
 }
diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java
index 1a4dbef..870ec4b 100644
--- a/core/java/android/view/ViewRootImpl.java
+++ b/core/java/android/view/ViewRootImpl.java
@@ -12032,6 +12032,18 @@
         scheduleTraversals();
     }
 
+    @Override
+    public void addTrustedPresentationCallback(@NonNull SurfaceControl.Transaction t,
+            @NonNull SurfaceControl.TrustedPresentationThresholds thresholds,
+            @NonNull Executor executor, @NonNull Consumer<Boolean> listener) {
+        t.setTrustedPresentationCallback(getSurfaceControl(), thresholds, executor, listener);
+    }
+
+    @Override
+    public void removeTrustedPresentationCallback(@NonNull SurfaceControl.Transaction t,
+            @NonNull Consumer<Boolean> listener) {
+        t.clearTrustedPresentationCallback(getSurfaceControl());
+    }
 
     private void logAndTrace(String msg) {
         if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) {
diff --git a/core/java/android/view/WindowManager.java b/core/java/android/view/WindowManager.java
index f668088..046ea77 100644
--- a/core/java/android/view/WindowManager.java
+++ b/core/java/android/view/WindowManager.java
@@ -122,9 +122,7 @@
 import android.view.WindowInsets.Type;
 import android.view.WindowInsets.Type.InsetsType;
 import android.view.accessibility.AccessibilityNodeInfo;
-import android.window.ITrustedPresentationListener;
 import android.window.TaskFpsCallback;
-import android.window.TrustedPresentationThresholds;
 
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
@@ -5886,35 +5884,4 @@
     default boolean replaceContentOnDisplayWithSc(int displayId, @NonNull SurfaceControl sc) {
         throw new UnsupportedOperationException();
     }
-
-    /**
-     * Add a trusted presentation listener associated with a window. If the listener has already
-     * been registered, an AndroidRuntimeException will be thrown.
-     *
-     * @param window  The Window to add the trusted presentation listener for
-     * @param thresholds The {@link TrustedPresentationThresholds} that will specify
-     *                   when the to invoke the callback.
-     * @param executor   The {@link Executor} where the callback will be invoked on.
-     * @param listener   The {@link ITrustedPresentationListener} that will receive the callbacks
-     *                  when entered or exited trusted presentation per the thresholds.
-     *
-     * @hide b/287076178 un-hide with API bump
-     */
-    default void registerTrustedPresentationListener(@NonNull IBinder window,
-            @NonNull TrustedPresentationThresholds thresholds,  @NonNull Executor executor,
-            @NonNull Consumer<Boolean> listener) {
-        throw new UnsupportedOperationException();
-    }
-
-    /**
-     * Removes a presentation listener associated with a window. If the listener was not previously
-     * registered, the call will be a noop.
-     *
-     * @hide
-     * @see #registerTrustedPresentationListener(IBinder,
-     * TrustedPresentationThresholds, Executor, Consumer)
-     */
-    default void unregisterTrustedPresentationListener(@NonNull Consumer<Boolean> listener) {
-        throw new UnsupportedOperationException();
-    }
 }
diff --git a/core/java/android/view/WindowManagerGlobal.java b/core/java/android/view/WindowManagerGlobal.java
index a7d814e..214f1ec 100644
--- a/core/java/android/view/WindowManagerGlobal.java
+++ b/core/java/android/view/WindowManagerGlobal.java
@@ -30,13 +30,9 @@
 import android.os.ServiceManager;
 import android.os.SystemProperties;
 import android.util.AndroidRuntimeException;
-import android.util.ArrayMap;
 import android.util.ArraySet;
 import android.util.Log;
-import android.util.Pair;
 import android.view.inputmethod.InputMethodManager;
-import android.window.ITrustedPresentationListener;
-import android.window.TrustedPresentationThresholds;
 
 import com.android.internal.util.FastPrintWriter;
 
@@ -47,7 +43,6 @@
 import java.util.ArrayList;
 import java.util.WeakHashMap;
 import java.util.concurrent.Executor;
-import java.util.function.Consumer;
 import java.util.function.IntConsumer;
 
 /**
@@ -148,9 +143,6 @@
 
     private Runnable mSystemPropertyUpdater;
 
-    private final TrustedPresentationListener mTrustedPresentationListener =
-            new TrustedPresentationListener();
-
     private WindowManagerGlobal() {
     }
 
@@ -332,7 +324,7 @@
             final Context context = view.getContext();
             if (context != null
                     && (context.getApplicationInfo().flags
-                    & ApplicationInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
+                            & ApplicationInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
                 wparams.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
             }
         }
@@ -490,7 +482,7 @@
                     if (who != null) {
                         WindowLeaked leak = new WindowLeaked(
                                 what + " " + who + " has leaked window "
-                                        + root.getView() + " that was originally added here");
+                                + root.getView() + " that was originally added here");
                         leak.setStackTrace(root.getLocation().getStackTrace());
                         Log.e(TAG, "", leak);
                     }
@@ -798,86 +790,6 @@
         }
     }
 
-    public void registerTrustedPresentationListener(@NonNull IBinder window,
-            @NonNull TrustedPresentationThresholds thresholds, Executor executor,
-            @NonNull Consumer<Boolean> listener) {
-        mTrustedPresentationListener.addListener(window, thresholds, listener, executor);
-    }
-
-    public void unregisterTrustedPresentationListener(@NonNull Consumer<Boolean> listener) {
-        mTrustedPresentationListener.removeListener(listener);
-    }
-
-    private final class TrustedPresentationListener extends
-            ITrustedPresentationListener.Stub {
-        private static int sId = 0;
-        private final ArrayMap<Consumer<Boolean>, Pair<Integer, Executor>> mListeners =
-                new ArrayMap<>();
-
-        private final Object mTplLock = new Object();
-
-        private void addListener(IBinder window, TrustedPresentationThresholds thresholds,
-                Consumer<Boolean> listener, Executor executor) {
-            synchronized (mTplLock) {
-                if (mListeners.containsKey(listener)) {
-                    throw new AndroidRuntimeException("Trying to add duplicate listener");
-                }
-                int id = sId++;
-                mListeners.put(listener, new Pair<>(id, executor));
-                try {
-                    WindowManagerGlobal.getWindowManagerService()
-                            .registerTrustedPresentationListener(window, this, thresholds, id);
-                } catch (RemoteException e) {
-                    e.rethrowAsRuntimeException();
-                }
-            }
-        }
-
-        private void removeListener(Consumer<Boolean> listener) {
-            synchronized (mTplLock) {
-                var removedListener = mListeners.remove(listener);
-                if (removedListener == null) {
-                    Log.i(TAG, "listener " + listener + " does not exist.");
-                    return;
-                }
-
-                try {
-                    WindowManagerGlobal.getWindowManagerService()
-                            .unregisterTrustedPresentationListener(this, removedListener.first);
-                } catch (RemoteException e) {
-                    e.rethrowAsRuntimeException();
-                }
-            }
-        }
-
-        @Override
-        public void onTrustedPresentationChanged(int[] inTrustedStateListenerIds,
-                int[] outOfTrustedStateListenerIds) {
-            ArrayList<Runnable> firedListeners = new ArrayList<>();
-            synchronized (mTplLock) {
-                mListeners.forEach((listener, idExecutorPair) -> {
-                    final var listenerId =  idExecutorPair.first;
-                    final var executor = idExecutorPair.second;
-                    for (int id : inTrustedStateListenerIds) {
-                        if (listenerId == id) {
-                            firedListeners.add(() -> executor.execute(
-                                    () -> listener.accept(/*presentationState*/true)));
-                        }
-                    }
-                    for (int id : outOfTrustedStateListenerIds) {
-                        if (listenerId == id) {
-                            firedListeners.add(() -> executor.execute(
-                                    () -> listener.accept(/*presentationState*/false)));
-                        }
-                    }
-                });
-            }
-            for (int i = 0; i < firedListeners.size(); i++) {
-                firedListeners.get(i).run();
-            }
-        }
-    }
-
     /** @hide */
     public void addWindowlessRoot(ViewRootImpl impl) {
         synchronized (mLock) {
@@ -889,7 +801,7 @@
     public void removeWindowlessRoot(ViewRootImpl impl) {
         synchronized (mLock) {
             mWindowlessRoots.remove(impl);
-        }
+	}
     }
 
     public void setRecentsAppBehindSystemBars(boolean behindSystemBars) {
diff --git a/core/java/android/view/WindowManagerImpl.java b/core/java/android/view/WindowManagerImpl.java
index b4b1fde..d7b74b3 100644
--- a/core/java/android/view/WindowManagerImpl.java
+++ b/core/java/android/view/WindowManagerImpl.java
@@ -37,7 +37,6 @@
 import android.util.Log;
 import android.window.ITaskFpsCallback;
 import android.window.TaskFpsCallback;
-import android.window.TrustedPresentationThresholds;
 import android.window.WindowContext;
 import android.window.WindowMetricsController;
 import android.window.WindowProvider;
@@ -509,17 +508,4 @@
         }
         return false;
     }
-
-    @Override
-    public void registerTrustedPresentationListener(@NonNull IBinder window,
-            @NonNull TrustedPresentationThresholds thresholds, @NonNull Executor executor,
-            @NonNull Consumer<Boolean> listener) {
-        mGlobal.registerTrustedPresentationListener(window, thresholds, executor, listener);
-    }
-
-    @Override
-    public void unregisterTrustedPresentationListener(@NonNull Consumer<Boolean> listener) {
-        mGlobal.unregisterTrustedPresentationListener(listener);
-
-    }
 }
diff --git a/core/java/android/view/autofill/AutofillManager.java b/core/java/android/view/autofill/AutofillManager.java
index 96574f5..6bc2a13 100644
--- a/core/java/android/view/autofill/AutofillManager.java
+++ b/core/java/android/view/autofill/AutofillManager.java
@@ -24,6 +24,7 @@
 import static android.service.autofill.FillRequest.FLAG_SCREEN_HAS_CREDMAN_FIELD;
 import static android.service.autofill.FillRequest.FLAG_SUPPORTS_FILL_DIALOG;
 import static android.service.autofill.FillRequest.FLAG_VIEW_NOT_FOCUSED;
+import static android.service.autofill.FillRequest.FLAG_VIEW_REQUESTS_CREDMAN_SERVICE;
 import static android.view.ContentInfo.SOURCE_AUTOFILL;
 import static android.view.autofill.Helper.sDebug;
 import static android.view.autofill.Helper.sVerbose;
@@ -61,7 +62,6 @@
 import android.service.autofill.AutofillService;
 import android.service.autofill.FillEventHistory;
 import android.service.autofill.Flags;
-import android.service.autofill.IFillCallback;
 import android.service.autofill.UserData;
 import android.text.TextUtils;
 import android.util.ArrayMap;
@@ -729,6 +729,9 @@
     // focus due to autofill showing biometric activity, password manager, or password breach check.
     private boolean mRelayoutFix;
 
+    // Indicates whether the credman integration is enabled.
+    private final boolean mIsCredmanIntegrationEnabled;
+
     // Indicates whether called the showAutofillDialog() method.
     private boolean mShowAutofillDialogCalled = false;
 
@@ -952,6 +955,7 @@
                 AutofillFeatureFlags.shouldAlwaysIncludeWebviewInAssistStructure();
 
         mRelayoutFix = Flags.relayout();
+        mIsCredmanIntegrationEnabled = Flags.autofillCredmanIntegration();
     }
 
     /**
@@ -1804,7 +1808,9 @@
             }
             return mCallback;
         }
-
+        if (mIsCredmanIntegrationEnabled && isCredmanRequested(view)) {
+            flags |= FLAG_VIEW_REQUESTS_CREDMAN_SERVICE;
+        }
         mIsFillRequested.set(true);
 
         // don't notify entered when Activity is already in background
@@ -3384,6 +3390,9 @@
     }
 
     private boolean isCredmanRequested(View view) {
+        if (view == null) {
+            return false;
+        }
         if (view.isCredential()) {
             return true;
         }
diff --git a/core/java/android/view/contentcapture/MainContentCaptureSession.java b/core/java/android/view/contentcapture/MainContentCaptureSession.java
index 14ec14b..966161f 100644
--- a/core/java/android/view/contentcapture/MainContentCaptureSession.java
+++ b/core/java/android/view/contentcapture/MainContentCaptureSession.java
@@ -31,6 +31,7 @@
 import static android.view.contentcapture.ContentCaptureHelper.sDebug;
 import static android.view.contentcapture.ContentCaptureHelper.sVerbose;
 import static android.view.contentcapture.ContentCaptureManager.RESULT_CODE_FALSE;
+import static android.view.contentcapture.flags.Flags.runOnBackgroundThreadEnabled;
 
 import android.annotation.NonNull;
 import android.annotation.Nullable;
@@ -209,14 +210,14 @@
                 binder = resultData.getBinder(EXTRA_BINDER);
                 if (binder == null) {
                     Log.wtf(TAG, "No " + EXTRA_BINDER + " extra result");
-                    mainSession.mHandler.post(() -> mainSession.resetSession(
+                    mainSession.runOnContentCaptureThread(() -> mainSession.resetSession(
                             STATE_DISABLED | STATE_INTERNAL_ERROR));
                     return;
                 }
             } else {
                 binder = null;
             }
-            mainSession.mHandler.post(() ->
+            mainSession.runOnContentCaptureThread(() ->
                     mainSession.onSessionStarted(resultCode, binder));
         }
     }
@@ -256,7 +257,13 @@
      */
     void start(@NonNull IBinder token, @NonNull IBinder shareableActivityToken,
             @NonNull ComponentName component, int flags) {
-        runOnContentCaptureThread(() -> startImpl(token, shareableActivityToken, component, flags));
+        if (runOnBackgroundThreadEnabled()) {
+            runOnContentCaptureThread(
+                    () -> startImpl(token, shareableActivityToken, component, flags));
+        } else {
+            // Preserve the control arm behaviour.
+            startImpl(token, shareableActivityToken, component, flags);
+        }
     }
 
     private void startImpl(@NonNull IBinder token, @NonNull IBinder shareableActivityToken,
@@ -613,7 +620,12 @@
     @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
     @Override
     public void flush(@FlushReason int reason) {
-        runOnContentCaptureThread(() -> flushImpl(reason));
+        if (runOnBackgroundThreadEnabled()) {
+            runOnContentCaptureThread(() -> flushImpl(reason));
+        } else {
+            // Preserve the control arm behaviour.
+            flushImpl(reason);
+        }
     }
 
     private void flushImpl(@FlushReason int reason) {
@@ -904,7 +916,12 @@
     /** public because is also used by ViewRootImpl */
     public void notifyContentCaptureEvents(
             @NonNull SparseArray<ArrayList<Object>> contentCaptureEvents) {
-        runOnContentCaptureThread(() -> notifyContentCaptureEventsImpl(contentCaptureEvents));
+        if (runOnBackgroundThreadEnabled()) {
+            runOnContentCaptureThread(() -> notifyContentCaptureEventsImpl(contentCaptureEvents));
+        } else {
+            // Preserve the control arm behaviour.
+            notifyContentCaptureEventsImpl(contentCaptureEvents);
+        }
     }
 
     private void notifyContentCaptureEventsImpl(
@@ -1076,19 +1093,30 @@
      * </p>
      */
     private void runOnContentCaptureThread(@NonNull Runnable r) {
-        if (!mHandler.getLooper().isCurrentThread()) {
-            mHandler.post(r);
+        if (runOnBackgroundThreadEnabled()) {
+            if (!mHandler.getLooper().isCurrentThread()) {
+                mHandler.post(r);
+            } else {
+                r.run();
+            }
         } else {
-            r.run();
+            // Preserve the control arm behaviour to always post to the handler.
+            mHandler.post(r);
         }
     }
 
     private void clearAndRunOnContentCaptureThread(@NonNull Runnable r, int what) {
-        if (!mHandler.getLooper().isCurrentThread()) {
+        if (runOnBackgroundThreadEnabled()) {
+            if (!mHandler.getLooper().isCurrentThread()) {
+                mHandler.removeMessages(what);
+                mHandler.post(r);
+            } else {
+                r.run();
+            }
+        } else {
+            // Preserve the control arm behaviour to always post to the handler.
             mHandler.removeMessages(what);
             mHandler.post(r);
-        } else {
-            r.run();
         }
     }
 }
diff --git a/core/java/android/view/inputmethod/ImeTracker.java b/core/java/android/view/inputmethod/ImeTracker.java
index d4cfd63..fab8c77 100644
--- a/core/java/android/view/inputmethod/ImeTracker.java
+++ b/core/java/android/view/inputmethod/ImeTracker.java
@@ -737,7 +737,7 @@
          */
         public void onCancelAnimation(@AnimationType int animType) {
             final int cujType = getImeInsetsCujFromAnimation(animType);
-            if (cujType == -1) {
+            if (cujType != -1) {
                 InteractionJankMonitor.getInstance().cancel(cujType);
             }
         }
diff --git a/core/java/android/window/ITrustedPresentationListener.aidl b/core/java/android/window/ITrustedPresentationListener.aidl
deleted file mode 100644
index b33128a..0000000
--- a/core/java/android/window/ITrustedPresentationListener.aidl
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Copyright (C) 2023 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 android.window;
-
-/**
- * @hide
- */
-oneway interface ITrustedPresentationListener {
-    void onTrustedPresentationChanged(in int[] enteredTrustedStateIds, in int[] exitedTrustedStateIds);
-}
\ No newline at end of file
diff --git a/core/java/android/window/TrustedPresentationListener.java b/core/java/android/window/TrustedPresentationListener.java
deleted file mode 100644
index 02fd6d9..0000000
--- a/core/java/android/window/TrustedPresentationListener.java
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
- * Copyright 2023 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 android.window;
-
-/**
- * @hide
- */
-public interface TrustedPresentationListener {
-
-    void onTrustedPresentationChanged(boolean inTrustedPresentationState);
-
-}
diff --git a/core/java/android/window/TrustedPresentationThresholds.aidl b/core/java/android/window/TrustedPresentationThresholds.aidl
deleted file mode 100644
index d7088bf..0000000
--- a/core/java/android/window/TrustedPresentationThresholds.aidl
+++ /dev/null
@@ -1,3 +0,0 @@
-package android.window;
-
-parcelable TrustedPresentationThresholds;
diff --git a/core/java/android/window/TrustedPresentationThresholds.java b/core/java/android/window/TrustedPresentationThresholds.java
deleted file mode 100644
index 801d35c..0000000
--- a/core/java/android/window/TrustedPresentationThresholds.java
+++ /dev/null
@@ -1,127 +0,0 @@
-/*
- * Copyright 2023 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 android.window;
-
-import android.annotation.FloatRange;
-import android.annotation.IntRange;
-import android.os.Parcel;
-import android.os.Parcelable;
-import android.view.SurfaceControl;
-
-import androidx.annotation.NonNull;
-
-/**
- * @hide
- */
-public final class TrustedPresentationThresholds implements Parcelable {
-    /**
-     * The min alpha the {@link SurfaceControl} is required to have to be considered inside the
-     * threshold.
-     */
-    @FloatRange(from = 0f, fromInclusive = false, to = 1f)
-    public final float mMinAlpha;
-
-    /**
-     * The min fraction of the SurfaceControl that was presented to the user to be considered
-     * inside the threshold.
-     */
-    @FloatRange(from = 0f, fromInclusive = false, to = 1f)
-    public final float mMinFractionRendered;
-
-    /**
-     * The time in milliseconds required for the {@link SurfaceControl} to be in the threshold.
-     */
-    @IntRange(from = 1)
-    public final int mStabilityRequirementMs;
-
-    private void checkValid() {
-        if (mMinAlpha <= 0 || mMinFractionRendered <= 0 || mStabilityRequirementMs < 1) {
-            throw new IllegalArgumentException(
-                    "TrustedPresentationThresholds values are invalid");
-        }
-    }
-
-    /**
-     * Creates a new TrustedPresentationThresholds.
-     *
-     * @param minAlpha               The min alpha the {@link SurfaceControl} is required to
-     *                               have to be considered inside the
-     *                               threshold.
-     * @param minFractionRendered    The min fraction of the SurfaceControl that was presented
-     *                               to the user to be considered
-     *                               inside the threshold.
-     * @param stabilityRequirementMs The time in milliseconds required for the
-     *                               {@link SurfaceControl} to be in the threshold.
-     */
-    public TrustedPresentationThresholds(
-            @FloatRange(from = 0f, fromInclusive = false, to = 1f) float minAlpha,
-            @FloatRange(from = 0f, fromInclusive = false, to = 1f) float minFractionRendered,
-            @IntRange(from = 1) int stabilityRequirementMs) {
-        this.mMinAlpha = minAlpha;
-        this.mMinFractionRendered = minFractionRendered;
-        this.mStabilityRequirementMs = stabilityRequirementMs;
-        checkValid();
-    }
-
-    @Override
-    public String toString() {
-        return "TrustedPresentationThresholds { "
-                + "minAlpha = " + mMinAlpha + ", "
-                + "minFractionRendered = " + mMinFractionRendered + ", "
-                + "stabilityRequirementMs = " + mStabilityRequirementMs
-                + " }";
-    }
-
-    @Override
-    public void writeToParcel(@NonNull Parcel dest, int flags) {
-        dest.writeFloat(mMinAlpha);
-        dest.writeFloat(mMinFractionRendered);
-        dest.writeInt(mStabilityRequirementMs);
-    }
-
-    @Override
-    public int describeContents() {
-        return 0;
-    }
-
-    /**
-     * @hide
-     */
-    TrustedPresentationThresholds(@NonNull Parcel in) {
-        mMinAlpha = in.readFloat();
-        mMinFractionRendered = in.readFloat();
-        mStabilityRequirementMs = in.readInt();
-
-        checkValid();
-    }
-
-    /**
-     * @hide
-     */
-    public static final @NonNull Creator<TrustedPresentationThresholds> CREATOR =
-            new Creator<TrustedPresentationThresholds>() {
-                @Override
-                public TrustedPresentationThresholds[] newArray(int size) {
-                    return new TrustedPresentationThresholds[size];
-                }
-
-                @Override
-                public TrustedPresentationThresholds createFromParcel(@NonNull Parcel in) {
-                    return new TrustedPresentationThresholds(in);
-                }
-            };
-}
diff --git a/core/java/android/window/flags/wallpaper_manager.aconfig b/core/java/android/window/flags/wallpaper_manager.aconfig
index 09be0cf..f03c993 100644
--- a/core/java/android/window/flags/wallpaper_manager.aconfig
+++ b/core/java/android/window/flags/wallpaper_manager.aconfig
@@ -5,4 +5,11 @@
     namespace: "wear_frameworks"
     description: "Allow out of focus process to update wallpaper complications"
     bug: "271132915"
-}
\ No newline at end of file
+}
+
+flag {
+  name: "multi_crop"
+  namespace: "systemui"
+  description: "Support storing different wallpaper crops for different display dimensions. Only effective after rebooting."
+  bug: "281648899"
+}
diff --git a/core/java/com/android/internal/protolog/ProtoLogGroup.java b/core/java/com/android/internal/protolog/ProtoLogGroup.java
index 8c2a525..4bb7c33 100644
--- a/core/java/com/android/internal/protolog/ProtoLogGroup.java
+++ b/core/java/com/android/internal/protolog/ProtoLogGroup.java
@@ -93,7 +93,6 @@
     WM_DEBUG_DREAM(Consts.ENABLE_DEBUG, Consts.ENABLE_LOG_TO_PROTO_DEBUG, true, Consts.TAG_WM),
 
     WM_DEBUG_DIMMER(Consts.ENABLE_DEBUG, Consts.ENABLE_LOG_TO_PROTO_DEBUG, false, Consts.TAG_WM),
-    WM_DEBUG_TPL(Consts.ENABLE_DEBUG, Consts.ENABLE_LOG_TO_PROTO_DEBUG, false, Consts.TAG_WM),
     TEST_GROUP(true, true, false, "WindowManagerProtoLogTest");
 
     private final boolean mEnabled;
diff --git a/core/jni/com_android_internal_content_om_OverlayManagerImpl.cpp b/core/jni/com_android_internal_content_om_OverlayManagerImpl.cpp
index 0c39a69..358531a 100644
--- a/core/jni/com_android_internal_content_om_OverlayManagerImpl.cpp
+++ b/core/jni/com_android_internal_content_om_OverlayManagerImpl.cpp
@@ -46,6 +46,7 @@
     jfieldID configuration;
     jfieldID binaryDataOffset;
     jfieldID binaryDataSize;
+    jfieldID isNinePatch;
 } gFabricatedOverlayInternalEntryOffsets;
 
 static struct parcel_file_descriptor_offsets_t {
@@ -288,13 +289,16 @@
                 env->GetLongField(entry, gFabricatedOverlayInternalEntryOffsets.binaryDataOffset);
         const auto data_size =
                 env->GetLongField(entry, gFabricatedOverlayInternalEntryOffsets.binaryDataSize);
+        const auto nine_patch =
+                env->GetBooleanField(entry, gFabricatedOverlayInternalEntryOffsets.isNinePatch);
         entries_params.push_back(
                 FabricatedOverlayEntryParameters{resourceName.c_str(), (DataType)dataType,
                                                  (DataValue)data,
                                                  string_data.value_or(std::string()), binary_data,
                                                  static_cast<off64_t>(data_offset),
                                                  static_cast<size_t>(data_size),
-                                                 configuration.value_or(std::string())});
+                                                 configuration.value_or(std::string()),
+                                                 static_cast<bool>(nine_patch)});
         ALOGV("resourceName = %s, dataType = 0x%08x, data = 0x%08x, dataString = %s,"
               " binaryData = %d, configuration = %s",
               resourceName.c_str(), dataType, data, string_data.value_or(std::string()).c_str(),
@@ -455,6 +459,9 @@
     gFabricatedOverlayInternalEntryOffsets.binaryDataSize =
             GetFieldIDOrDie(env, gFabricatedOverlayInternalEntryOffsets.classObject,
                             "binaryDataSize", "J");
+    gFabricatedOverlayInternalEntryOffsets.isNinePatch =
+            GetFieldIDOrDie(env, gFabricatedOverlayInternalEntryOffsets.classObject, "isNinePatch",
+                            "Z");
 
     jclass parcelFileDescriptorClass =
             android::FindClassOrDie(env, "android/os/ParcelFileDescriptor");
diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml
index ba1f392..1229453 100644
--- a/core/res/res/values/config.xml
+++ b/core/res/res/values/config.xml
@@ -693,6 +693,16 @@
         -->
     </integer-array>
 
+    <!-- The device states (supplied by DeviceStateManager) that should be treated as concurrent
+    display state. Default is empty. -->
+    <integer-array name="config_concurrentDisplayDeviceStates">
+        <!-- Example:
+        <item>0</item>
+        <item>1</item>
+        <item>2</item>
+        -->
+    </integer-array>
+
     <!-- Indicates whether the window manager reacts to half-fold device states by overriding
      rotation. -->
     <bool name="config_windowManagerHalfFoldAutoRotateOverride">false</bool>
diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml
index 7787c5d..93aacdf 100644
--- a/core/res/res/values/symbols.xml
+++ b/core/res/res/values/symbols.xml
@@ -4142,6 +4142,7 @@
   <java-symbol type="array" name="config_foldedDeviceStates" />
   <java-symbol type="array" name="config_halfFoldedDeviceStates" />
   <java-symbol type="array" name="config_rearDisplayDeviceStates" />
+  <java-symbol type="array" name="config_concurrentDisplayDeviceStates" />
   <java-symbol type="bool" name="config_windowManagerHalfFoldAutoRotateOverride" />
   <java-symbol type="bool" name="config_windowManagerPauseRotationWhenUnfolding" />
   <java-symbol type="integer" name="config_pauseRotationWhenUnfolding_hingeEventTimeout" />
diff --git a/core/res/res/values/themes_material.xml b/core/res/res/values/themes_material.xml
index cd4c0d6..8e2fb34 100644
--- a/core/res/res/values/themes_material.xml
+++ b/core/res/res/values/themes_material.xml
@@ -181,6 +181,7 @@
         <item name="windowSharedElementExitTransition">@transition/move</item>
         <item name="windowContentTransitions">false</item>
         <item name="windowActivityTransitions">true</item>
+        <item name="windowSwipeToDismiss">@empty</item>
 
         <!-- Dialog attributes -->
         <item name="dialogTheme">@style/ThemeOverlay.Material.Dialog</item>
@@ -554,6 +555,7 @@
         <item name="windowSharedElementExitTransition">@transition/move</item>
         <item name="windowContentTransitions">false</item>
         <item name="windowActivityTransitions">true</item>
+        <item name="windowSwipeToDismiss">@empty</item>
 
         <!-- Dialog attributes -->
         <item name="dialogTheme">@style/ThemeOverlay.Material.Dialog</item>
diff --git a/data/etc/privapp-permissions-platform.xml b/data/etc/privapp-permissions-platform.xml
index 3cf28c9..69a6e6d 100644
--- a/data/etc/privapp-permissions-platform.xml
+++ b/data/etc/privapp-permissions-platform.xml
@@ -535,6 +535,8 @@
         <!-- Permission required for CTS test IntentRedirectionTest -->
         <permission name="android.permission.QUERY_CLONED_APPS"/>
         <permission name="android.permission.GET_BINDING_UID_IMPORTANCE"/>
+        <!-- Permission required for CTS test NotificationManagerZenTest -->
+        <permission name="android.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS" />
     </privapp-permissions>
 
     <privapp-permissions package="com.android.statementservice">
diff --git a/data/etc/services.core.protolog.json b/data/etc/services.core.protolog.json
index 1912821..2237ba1 100644
--- a/data/etc/services.core.protolog.json
+++ b/data/etc/services.core.protolog.json
@@ -595,12 +595,6 @@
       "group": "WM_ERROR",
       "at": "com\/android\/server\/wm\/WindowManagerService.java"
     },
-    "-1518132958": {
-      "message": "fractionRendered boundsOverSource=%f",
-      "level": "VERBOSE",
-      "group": "WM_DEBUG_TPL",
-      "at": "com\/android\/server\/wm\/TrustedPresentationListenerController.java"
-    },
     "-1517908912": {
       "message": "requestScrollCapture: caught exception dispatching to window.token=%s",
       "level": "WARN",
@@ -967,12 +961,6 @@
       "group": "WM_DEBUG_CONTENT_RECORDING",
       "at": "com\/android\/server\/wm\/ContentRecorder.java"
     },
-    "-1209762265": {
-      "message": "Registering listener=%s with id=%d for window=%s with %s",
-      "level": "DEBUG",
-      "group": "WM_DEBUG_TPL",
-      "at": "com\/android\/server\/wm\/TrustedPresentationListenerController.java"
-    },
     "-1209252064": {
       "message": "Clear animatingExit: reason=clearAnimatingFlags win=%s",
       "level": "DEBUG",
@@ -1345,12 +1333,6 @@
       "group": "WM_DEBUG_WINDOW_TRANSITIONS",
       "at": "com\/android\/server\/wm\/Transition.java"
     },
-    "-888703350": {
-      "message": "Skipping %s",
-      "level": "VERBOSE",
-      "group": "WM_DEBUG_TPL",
-      "at": "com\/android\/server\/wm\/TrustedPresentationListenerController.java"
-    },
     "-883738232": {
       "message": "Adding more than one toast window for UID at a time.",
       "level": "WARN",
@@ -2821,12 +2803,6 @@
       "group": "WM_DEBUG_ORIENTATION",
       "at": "com\/android\/server\/wm\/WindowManagerService.java"
     },
-    "360319850": {
-      "message": "fractionRendered scale=%f",
-      "level": "VERBOSE",
-      "group": "WM_DEBUG_TPL",
-      "at": "com\/android\/server\/wm\/TrustedPresentationListenerController.java"
-    },
     "364992694": {
       "message": "freezeDisplayRotation: current rotation=%d, new rotation=%d, caller=%s",
       "level": "VERBOSE",
@@ -3007,12 +2983,6 @@
       "group": "WM_DEBUG_BACK_PREVIEW",
       "at": "com\/android\/server\/wm\/BackNavigationController.java"
     },
-    "532771960": {
-      "message": "Adding untrusted state listener=%s with id=%d",
-      "level": "DEBUG",
-      "group": "WM_DEBUG_TPL",
-      "at": "com\/android\/server\/wm\/TrustedPresentationListenerController.java"
-    },
     "535103992": {
       "message": "Wallpaper may change!  Adjusting",
       "level": "VERBOSE",
@@ -3091,12 +3061,6 @@
       "group": "WM_DEBUG_DREAM",
       "at": "com\/android\/server\/wm\/ActivityTaskManagerService.java"
     },
-    "605179032": {
-      "message": "checkIfInThreshold fractionRendered=%f alpha=%f currTimeMs=%d",
-      "level": "VERBOSE",
-      "group": "WM_DEBUG_TPL",
-      "at": "com\/android\/server\/wm\/TrustedPresentationListenerController.java"
-    },
     "608694300": {
       "message": "  NEW SURFACE SESSION %s",
       "level": "INFO",
@@ -3325,12 +3289,6 @@
       "group": "WM_SHOW_TRANSACTIONS",
       "at": "com\/android\/server\/wm\/WindowState.java"
     },
-    "824532141": {
-      "message": "lastState=%s newState=%s alpha=%f minAlpha=%f fractionRendered=%f minFractionRendered=%f",
-      "level": "VERBOSE",
-      "group": "WM_DEBUG_TPL",
-      "at": "com\/android\/server\/wm\/TrustedPresentationListenerController.java"
-    },
     "829434921": {
       "message": "Draw state now committed in %s",
       "level": "VERBOSE",
@@ -3625,12 +3583,6 @@
       "group": "WM_SHOW_SURFACE_ALLOC",
       "at": "com\/android\/server\/wm\/ScreenRotationAnimation.java"
     },
-    "1090378847": {
-      "message": "Checking %d windows",
-      "level": "VERBOSE",
-      "group": "WM_DEBUG_TPL",
-      "at": "com\/android\/server\/wm\/TrustedPresentationListenerController.java"
-    },
     "1100065297": {
       "message": "Attempted to get IME policy of a display that does not exist: %d",
       "level": "WARN",
@@ -3763,12 +3715,6 @@
       "group": "WM_DEBUG_FOCUS",
       "at": "com\/android\/server\/wm\/ActivityRecord.java"
     },
-    "1251721200": {
-      "message": "unregister failed, couldn't find deathRecipient for %s with id=%d",
-      "level": "ERROR",
-      "group": "WM_DEBUG_TPL",
-      "at": "com\/android\/server\/wm\/TrustedPresentationListenerController.java"
-    },
     "1252594551": {
       "message": "Window types in WindowContext and LayoutParams.type should match! Type from LayoutParams is %d, but type from WindowContext is %d",
       "level": "WARN",
@@ -3907,12 +3853,6 @@
       "group": "WM_DEBUG_ORIENTATION",
       "at": "com\/android\/server\/wm\/TaskDisplayArea.java"
     },
-    "1382634842": {
-      "message": "Unregistering listener=%s with id=%d",
-      "level": "DEBUG",
-      "group": "WM_DEBUG_TPL",
-      "at": "com\/android\/server\/wm\/TrustedPresentationListenerController.java"
-    },
     "1393721079": {
       "message": "Starting remote display change: from [rot = %d], to [%dx%d, rot = %d]",
       "level": "VERBOSE",
@@ -3961,12 +3901,6 @@
       "group": "WM_ERROR",
       "at": "com\/android\/server\/wm\/WindowManagerService.java"
     },
-    "1445704347": {
-      "message": "coveredRegionsAbove updated with %s frame:%s region:%s",
-      "level": "VERBOSE",
-      "group": "WM_DEBUG_TPL",
-      "at": "com\/android\/server\/wm\/TrustedPresentationListenerController.java"
-    },
     "1448683958": {
       "message": "Override pending remote transitionSet=%b adapter=%s",
       "level": "INFO",
@@ -4267,12 +4201,6 @@
       "group": "WM_DEBUG_RECENTS_ANIMATIONS",
       "at": "com\/android\/server\/wm\/RecentsAnimation.java"
     },
-    "1786463281": {
-      "message": "Adding trusted state listener=%s with id=%d",
-      "level": "DEBUG",
-      "group": "WM_DEBUG_TPL",
-      "at": "com\/android\/server\/wm\/TrustedPresentationListenerController.java"
-    },
     "1789321832": {
       "message": "Then token:%s is invalid. It might be removed",
       "level": "WARN",
@@ -4447,12 +4375,6 @@
       "group": "WM_DEBUG_TASKS",
       "at": "com\/android\/server\/wm\/RootWindowContainer.java"
     },
-    "1955470028": {
-      "message": "computeFractionRendered: visibleRegion=%s screenBounds=%s contentSize=%s scale=%f,%f",
-      "level": "VERBOSE",
-      "group": "WM_DEBUG_TPL",
-      "at": "com\/android\/server\/wm\/TrustedPresentationListenerController.java"
-    },
     "1964565370": {
       "message": "Starting remote animation",
       "level": "INFO",
@@ -4737,9 +4659,6 @@
     "WM_DEBUG_TASKS": {
       "tag": "WindowManager"
     },
-    "WM_DEBUG_TPL": {
-      "tag": "WindowManager"
-    },
     "WM_DEBUG_WALLPAPER": {
       "tag": "WindowManager"
     },
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/bubbles/BubblePositioner.java b/libs/WindowManager/Shell/src/com/android/wm/shell/bubbles/BubblePositioner.java
index 5d16196..662a5c4 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/bubbles/BubblePositioner.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/bubbles/BubblePositioner.java
@@ -242,6 +242,18 @@
         return mDeviceConfig.isLandscape();
     }
 
+    /**
+     * On large screen (not small tablet), while in portrait, expanded bubbles are aligned to
+     * the bottom of the screen.
+     *
+     * @return whether bubbles are bottom aligned while expanded
+     */
+    public boolean areBubblesBottomAligned() {
+        return isLargeScreen()
+                && !mDeviceConfig.isSmallTablet()
+                && !isLandscape();
+    }
+
     /** @return whether the screen is considered large. */
     public boolean isLargeScreen() {
         return mDeviceConfig.isLargeScreen();
@@ -417,7 +429,10 @@
                 - bottomPadding;
     }
 
-    private int getExpandedViewHeightForLargeScreen() {
+    /**
+     * Returns the height to use for the expanded view when showing on a large screen.
+     */
+    public int getExpandedViewHeightForLargeScreen() {
         // the expanded view height on large tablets is calculated based on the shortest screen
         // size and is the same in both portrait and landscape
         int maxVerticalInset = Math.max(mInsets.top, mInsets.bottom);
@@ -460,13 +475,21 @@
         boolean isOverflow = bubble == null || BubbleOverflow.KEY.equals(bubble.getKey());
         float expandedViewHeight = getExpandedViewHeight(bubble);
         float topAlignment = getExpandedViewYTopAligned();
+        int manageButtonHeight =
+                isOverflow ? mExpandedViewPadding : mManageButtonHeightIncludingMargins;
+
+        // On largescreen portrait bubbles are bottom aligned.
+        if (areBubblesBottomAligned() && expandedViewHeight == MAX_HEIGHT) {
+            return mPositionRect.bottom - manageButtonHeight
+                    - getExpandedViewHeightForLargeScreen() - mPointerWidth;
+        }
+
         if (!showBubblesVertically() || expandedViewHeight == MAX_HEIGHT) {
             // Top-align when bubbles are shown at the top or are max size.
             return topAlignment;
         }
+
         // If we're here, we're showing vertically & developer has made height less than maximum.
-        int manageButtonHeight =
-                isOverflow ? mExpandedViewPadding : mManageButtonHeightIncludingMargins;
         float pointerPosition = getPointerPosition(bubblePosition);
         float bottomIfCentered = pointerPosition + (expandedViewHeight / 2) + manageButtonHeight;
         float topIfCentered = pointerPosition - (expandedViewHeight / 2);
@@ -524,14 +547,8 @@
             // Last bubble has screen index 0 and first bubble has max screen index value.
             onScreenIndex = state.numberOfBubbles - 1 - index;
         }
-
         final float positionInRow = onScreenIndex * (mBubbleSize + mSpacingBetweenBubbles);
-        final float expandedStackSize = getExpandedStackSize(state.numberOfBubbles);
-        final float centerPosition = showBubblesVertically
-                ? mPositionRect.centerY()
-                : mPositionRect.centerX();
-        // alignment - centered on the edge
-        final float rowStart = centerPosition - (expandedStackSize / 2f);
+        final float rowStart = getBubbleRowStart(state);
         float x;
         float y;
         if (showBubblesVertically) {
@@ -557,6 +574,25 @@
         return new PointF(x, y);
     }
 
+    private float getBubbleRowStart(BubbleStackView.StackViewState state) {
+        final float expandedStackSize = getExpandedStackSize(state.numberOfBubbles);
+        final float rowStart;
+        if (areBubblesBottomAligned()) {
+            final float expandedViewHeight = getExpandedViewHeightForLargeScreen();
+            final float expandedViewBottom = mScreenRect.bottom
+                    - Math.max(mInsets.bottom, mInsets.top)
+                    - mManageButtonHeight - mPointerWidth;
+            final float expandedViewCenter = expandedViewBottom - (expandedViewHeight / 2f);
+            rowStart = expandedViewCenter - (expandedStackSize / 2f);
+        } else {
+            final float centerPosition = showBubblesVertically()
+                    ? mPositionRect.centerY()
+                    : mPositionRect.centerX();
+            rowStart = centerPosition - (expandedStackSize / 2f);
+        }
+        return rowStart;
+    }
+
     /**
      * Returns the position of the bubble on-screen when the stack is expanded and the IME
      * is showing.
@@ -577,9 +613,8 @@
         final float bottomHeight = getImeHeight() + mInsets.bottom + (mSpacingBetweenBubbles * 2);
         final float bottomInset = mScreenRect.bottom - bottomHeight;
         final float expandedStackSize = getExpandedStackSize(state.numberOfBubbles);
-        final float centerPosition = mPositionRect.centerY();
-        final float rowBottom = centerPosition + (expandedStackSize / 2f);
-        final float rowTop = centerPosition - (expandedStackSize / 2f);
+        final float rowTop = getBubbleRowStart(state);
+        final float rowBottom = rowTop + expandedStackSize;
         float rowTopForIme = rowTop;
         if (rowBottom > bottomInset) {
             // We overlap with IME, must shift the bubbles
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/common/split/SplitScreenConstants.java b/libs/WindowManager/Shell/src/com/android/wm/shell/common/split/SplitScreenConstants.java
index e734300..49db8d9 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/common/split/SplitScreenConstants.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/common/split/SplitScreenConstants.java
@@ -26,11 +26,12 @@
 
 /** Helper utility class of methods and constants that are available to be imported in Launcher. */
 public class SplitScreenConstants {
-    /**
-     * Duration used for every split fade-in or fade-out.
-     */
+    /** Duration used for every split fade-in or fade-out. */
     public static final int FADE_DURATION = 133;
 
+    /** Key for passing in widget intents when invoking split from launcher workspace. */
+    public static final String KEY_EXTRA_WIDGET_INTENT = "key_extra_widget_intent";
+
     ///////////////
     // IMPORTANT for the following SPLIT_POSITION and SNAP_TO constants:
     // These int values must not be changed -- they are persisted to user-defined app pairs, and
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/draganddrop/DragAndDropPolicy.java b/libs/WindowManager/Shell/src/com/android/wm/shell/draganddrop/DragAndDropPolicy.java
index 162ce19..a31a773 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/draganddrop/DragAndDropPolicy.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/draganddrop/DragAndDropPolicy.java
@@ -40,6 +40,7 @@
 import static com.android.wm.shell.draganddrop.DragAndDropPolicy.Target.TYPE_SPLIT_RIGHT;
 import static com.android.wm.shell.draganddrop.DragAndDropPolicy.Target.TYPE_SPLIT_TOP;
 
+import android.app.ActivityOptions;
 import android.app.ActivityTaskManager;
 import android.app.PendingIntent;
 import android.content.ActivityNotFoundException;
@@ -246,8 +247,15 @@
             @SplitPosition int position) {
         final boolean isTask = description.hasMimeType(MIMETYPE_APPLICATION_TASK);
         final boolean isShortcut = description.hasMimeType(MIMETYPE_APPLICATION_SHORTCUT);
-        final Bundle opts = intent.hasExtra(EXTRA_ACTIVITY_OPTIONS)
-                ? intent.getBundleExtra(EXTRA_ACTIVITY_OPTIONS) : new Bundle();
+        final ActivityOptions baseActivityOpts = ActivityOptions.makeBasic();
+        baseActivityOpts.setDisallowEnterPictureInPictureWhileLaunching(true);
+        final Bundle opts = baseActivityOpts.toBundle();
+        if (intent.hasExtra(EXTRA_ACTIVITY_OPTIONS)) {
+            opts.putAll(intent.getBundleExtra(EXTRA_ACTIVITY_OPTIONS));
+        }
+        // Put BAL flags to avoid activity start aborted.
+        opts.putBoolean(KEY_PENDING_INTENT_BACKGROUND_ACTIVITY_ALLOWED, true);
+        opts.putBoolean(KEY_PENDING_INTENT_BACKGROUND_ACTIVITY_ALLOWED_BY_PERMISSION, true);
         final UserHandle user = intent.getParcelableExtra(EXTRA_USER);
 
         if (isTask) {
@@ -259,9 +267,6 @@
             mStarter.startShortcut(packageName, id, position, opts, user);
         } else {
             final PendingIntent launchIntent = intent.getParcelableExtra(EXTRA_PENDING_INTENT);
-            // Put BAL flags to avoid activity start aborted.
-            opts.putBoolean(KEY_PENDING_INTENT_BACKGROUND_ACTIVITY_ALLOWED, true);
-            opts.putBoolean(KEY_PENDING_INTENT_BACKGROUND_ACTIVITY_ALLOWED_BY_PERMISSION, true);
             mStarter.startIntent(launchIntent, user.getIdentifier(), null /* fillIntent */,
                     position, opts);
         }
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/splitscreen/SplitScreenController.java b/libs/WindowManager/Shell/src/com/android/wm/shell/splitscreen/SplitScreenController.java
index 37b24e5..56f1c78 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/splitscreen/SplitScreenController.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/splitscreen/SplitScreenController.java
@@ -25,6 +25,7 @@
 import static android.view.RemoteAnimationTarget.MODE_OPENING;
 
 import static com.android.wm.shell.common.ExecutorUtils.executeRemoteCallWithTaskPermission;
+import static com.android.wm.shell.common.split.SplitScreenConstants.KEY_EXTRA_WIDGET_INTENT;
 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;
 import static com.android.wm.shell.common.split.SplitScreenConstants.SPLIT_POSITION_UNDEFINED;
@@ -719,10 +720,10 @@
         //       recents that hasn't launched and is not being organized
         final String packageName2 = SplitScreenUtils.getPackageName(taskId, mTaskOrganizer);
         final int userId2 = SplitScreenUtils.getUserId(taskId, mTaskOrganizer);
+        boolean setSecondIntentMultipleTask = false;
         if (samePackage(packageName1, packageName2, userId1, userId2)) {
             if (supportMultiInstancesSplit(packageName1)) {
-                fillInIntent = new Intent();
-                fillInIntent.addFlags(FLAG_ACTIVITY_MULTIPLE_TASK);
+                setSecondIntentMultipleTask = true;
                 ProtoLog.v(ShellProtoLogGroup.WM_SHELL_SPLIT_SCREEN, "Adding MULTIPLE_TASK");
             } else {
                 if (mRecentTasksOptional.isPresent()) {
@@ -737,6 +738,10 @@
                         Toast.LENGTH_SHORT).show();
             }
         }
+        if (options2 != null) {
+            Intent widgetIntent = options2.getParcelable(KEY_EXTRA_WIDGET_INTENT, Intent.class);
+            fillInIntent = resolveWidgetFillinIntent(widgetIntent, setSecondIntentMultipleTask);
+        }
         mStageCoordinator.startIntentAndTask(pendingIntent, fillInIntent, options1, taskId,
                 options2, splitPosition, snapPosition, remoteTransition, instanceId);
     }
@@ -787,12 +792,12 @@
                 ? ActivityOptions.fromBundle(options1) : ActivityOptions.makeBasic();
         final ActivityOptions activityOptions2 = options2 != null
                 ? ActivityOptions.fromBundle(options2) : ActivityOptions.makeBasic();
+        boolean setSecondIntentMultipleTask = false;
         if (samePackage(packageName1, packageName2, userId1, userId2)) {
             if (supportMultiInstancesSplit(packageName1)) {
                 fillInIntent1 = new Intent();
                 fillInIntent1.addFlags(FLAG_ACTIVITY_MULTIPLE_TASK);
-                fillInIntent2 = new Intent();
-                fillInIntent2.addFlags(FLAG_ACTIVITY_MULTIPLE_TASK);
+                setSecondIntentMultipleTask = true;
 
                 if (shortcutInfo1 != null) {
                     activityOptions1.setApplyMultipleTaskFlagForShortcut(true);
@@ -811,6 +816,10 @@
                         Toast.LENGTH_SHORT).show();
             }
         }
+        if (options2 != null) {
+            Intent widgetIntent = options2.getParcelable(KEY_EXTRA_WIDGET_INTENT, Intent.class);
+            fillInIntent2 = resolveWidgetFillinIntent(widgetIntent, setSecondIntentMultipleTask);
+        }
         mStageCoordinator.startIntents(pendingIntent1, fillInIntent1, shortcutInfo1,
                 activityOptions1.toBundle(), pendingIntent2, fillInIntent2, shortcutInfo2,
                 activityOptions2.toBundle(), splitPosition, snapPosition, remoteTransition,
@@ -916,6 +925,34 @@
         return false;
     }
 
+    /**
+     * Determines whether the widgetIntent needs to be modified if multiple tasks of its
+     * corresponding package/app are supported. There are 4 possible paths:
+     *  <li> We select a widget for second app which is the same as the first app </li>
+     *  <li> We select a widget for second app which is different from the first app </li>
+     *  <li> No widgets involved, we select a second app that is the same as first app </li>
+     *  <li> No widgets involved, we select a second app that is different from the first app
+     *       (returns null) </li>
+     *
+     * @return an {@link Intent} with the appropriate {@link Intent#FLAG_ACTIVITY_MULTIPLE_TASK}
+     *         added on or not depending on {@param launchMultipleTasks}.
+     */
+    @Nullable
+    private Intent resolveWidgetFillinIntent(@Nullable Intent widgetIntent,
+            boolean launchMultipleTasks) {
+        Intent fillInIntent2 = null;
+        if (launchMultipleTasks && widgetIntent != null) {
+            fillInIntent2 = widgetIntent;
+            fillInIntent2.addFlags(FLAG_ACTIVITY_MULTIPLE_TASK);
+        } else if (widgetIntent != null) {
+            fillInIntent2 = widgetIntent;
+        } else if (launchMultipleTasks) {
+            fillInIntent2 = new Intent();
+            fillInIntent2.addFlags(FLAG_ACTIVITY_MULTIPLE_TASK);
+        }
+        return fillInIntent2;
+    }
+
     RemoteAnimationTarget[] onGoingToRecentsLegacy(RemoteAnimationTarget[] apps) {
         if (ENABLE_SHELL_TRANSITIONS) return null;
 
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/windowdecor/DesktopModeWindowDecorViewModel.java b/libs/WindowManager/Shell/src/com/android/wm/shell/windowdecor/DesktopModeWindowDecorViewModel.java
index 03006f9..ab29df1 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/windowdecor/DesktopModeWindowDecorViewModel.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/windowdecor/DesktopModeWindowDecorViewModel.java
@@ -22,6 +22,7 @@
 import static android.app.WindowConfiguration.WINDOWING_MODE_MULTI_WINDOW;
 import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
 import static android.view.WindowInsets.Type.statusBars;
+import static android.view.WindowManager.TRANSIT_FLAG_KEYGUARD_GOING_AWAY;
 
 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;
@@ -221,7 +222,7 @@
         mRecentsTransitionHandler.addTransitionStateListener(new RecentsTransitionStateListener() {
             @Override
             public void onTransitionStarted(IBinder transition) {
-                onRecentsTransitionStarted(transition);
+                blockRelayoutOnTransitionStarted(transition);
             }
         });
         mShellCommandHandler.addDumpCallback(this::dump, this);
@@ -281,6 +282,10 @@
             if (decor != null) {
                 decor.addTransitionPausingRelayout(transition);
             }
+        } else if (change.getMode() == WindowManager.TRANSIT_TO_FRONT
+                && ((info.getFlags() & TRANSIT_FLAG_KEYGUARD_GOING_AWAY) != 0)
+                && change.getTaskInfo() != null) {
+            blockRelayoutOnTransitionStarted(transition);
         }
     }
 
@@ -358,7 +363,7 @@
         }
     }
 
-    private void onRecentsTransitionStarted(IBinder transition) {
+    private void blockRelayoutOnTransitionStarted(IBinder transition) {
         // Block relayout on window decorations originating from #onTaskInfoChanges until the
         // animation completes to avoid interfering with the transition animation.
         for (int i = 0; i < mWindowDecorByTaskId.size(); i++) {
diff --git a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/bubbles/BubblePositionerTest.java b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/bubbles/BubblePositionerTest.java
index e5ae6e5..6ebee73 100644
--- a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/bubbles/BubblePositionerTest.java
+++ b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/bubbles/BubblePositionerTest.java
@@ -332,6 +332,201 @@
                 .isWithin(0.1f).of(expectedHeight);
     }
 
+    @Test
+    public void testAreBubblesBottomAligned_largeScreen_true() {
+        Insets insets = Insets.of(10, 20, 5, 15);
+        Rect screenBounds = new Rect(0, 0, 1800, 2600);
+
+        DeviceConfig deviceConfig = new ConfigBuilder()
+                .setLargeScreen()
+                .setInsets(insets)
+                .setScreenBounds(screenBounds)
+                .build();
+        mPositioner.update(deviceConfig);
+
+        assertThat(mPositioner.areBubblesBottomAligned()).isTrue();
+    }
+
+    @Test
+    public void testAreBubblesBottomAligned_largeScreen_false() {
+        Insets insets = Insets.of(10, 20, 5, 15);
+        Rect screenBounds = new Rect(0, 0, 1800, 2600);
+
+        DeviceConfig deviceConfig = new ConfigBuilder()
+                .setLargeScreen()
+                .setLandscape()
+                .setInsets(insets)
+                .setScreenBounds(screenBounds)
+                .build();
+        mPositioner.update(deviceConfig);
+
+        assertThat(mPositioner.areBubblesBottomAligned()).isFalse();
+    }
+
+    @Test
+    public void testAreBubblesBottomAligned_smallTablet_false() {
+        Insets insets = Insets.of(10, 20, 5, 15);
+        Rect screenBounds = new Rect(0, 0, 1800, 2600);
+
+        DeviceConfig deviceConfig = new ConfigBuilder()
+                .setLargeScreen()
+                .setSmallTablet()
+                .setInsets(insets)
+                .setScreenBounds(screenBounds)
+                .build();
+        mPositioner.update(deviceConfig);
+
+        assertThat(mPositioner.areBubblesBottomAligned()).isFalse();
+    }
+
+    @Test
+    public void testAreBubblesBottomAligned_phone_false() {
+        Insets insets = Insets.of(10, 20, 5, 15);
+        Rect screenBounds = new Rect(0, 0, 1800, 2600);
+
+        DeviceConfig deviceConfig = new ConfigBuilder()
+                .setInsets(insets)
+                .setScreenBounds(screenBounds)
+                .build();
+        mPositioner.update(deviceConfig);
+
+        assertThat(mPositioner.areBubblesBottomAligned()).isFalse();
+    }
+
+    @Test
+    public void testExpandedViewY_phoneLandscape() {
+        Insets insets = Insets.of(10, 20, 5, 15);
+        Rect screenBounds = new Rect(0, 0, 1800, 2600);
+
+        DeviceConfig deviceConfig = new ConfigBuilder()
+                .setLandscape()
+                .setInsets(insets)
+                .setScreenBounds(screenBounds)
+                .build();
+        mPositioner.update(deviceConfig);
+
+        Intent intent = new Intent(Intent.ACTION_VIEW).setPackage(mContext.getPackageName());
+        Bubble bubble = Bubble.createAppBubble(intent, new UserHandle(1), null, directExecutor());
+
+        // This bubble will have max height so it'll always be top aligned
+        assertThat(mPositioner.getExpandedViewY(bubble, 0f /* bubblePosition */))
+                .isEqualTo(mPositioner.getExpandedViewYTopAligned());
+    }
+
+    @Test
+    public void testExpandedViewY_phonePortrait() {
+        Insets insets = Insets.of(10, 20, 5, 15);
+        Rect screenBounds = new Rect(0, 0, 1800, 2600);
+
+        DeviceConfig deviceConfig = new ConfigBuilder()
+                .setInsets(insets)
+                .setScreenBounds(screenBounds)
+                .build();
+        mPositioner.update(deviceConfig);
+
+        Intent intent = new Intent(Intent.ACTION_VIEW).setPackage(mContext.getPackageName());
+        Bubble bubble = Bubble.createAppBubble(intent, new UserHandle(1), null, directExecutor());
+
+        // Always top aligned in phone portrait
+        assertThat(mPositioner.getExpandedViewY(bubble, 0f /* bubblePosition */))
+                .isEqualTo(mPositioner.getExpandedViewYTopAligned());
+    }
+
+    @Test
+    public void testExpandedViewY_smallTabletLandscape() {
+        Insets insets = Insets.of(10, 20, 5, 15);
+        Rect screenBounds = new Rect(0, 0, 1800, 2600);
+
+        DeviceConfig deviceConfig = new ConfigBuilder()
+                .setSmallTablet()
+                .setLandscape()
+                .setInsets(insets)
+                .setScreenBounds(screenBounds)
+                .build();
+        mPositioner.update(deviceConfig);
+
+        Intent intent = new Intent(Intent.ACTION_VIEW).setPackage(mContext.getPackageName());
+        Bubble bubble = Bubble.createAppBubble(intent, new UserHandle(1), null, directExecutor());
+
+        // This bubble will have max height which is always top aligned on small tablets
+        assertThat(mPositioner.getExpandedViewY(bubble, 0f /* bubblePosition */))
+                .isEqualTo(mPositioner.getExpandedViewYTopAligned());
+    }
+
+    @Test
+    public void testExpandedViewY_smallTabletPortrait() {
+        Insets insets = Insets.of(10, 20, 5, 15);
+        Rect screenBounds = new Rect(0, 0, 1800, 2600);
+
+        DeviceConfig deviceConfig = new ConfigBuilder()
+                .setSmallTablet()
+                .setInsets(insets)
+                .setScreenBounds(screenBounds)
+                .build();
+        mPositioner.update(deviceConfig);
+
+        Intent intent = new Intent(Intent.ACTION_VIEW).setPackage(mContext.getPackageName());
+        Bubble bubble = Bubble.createAppBubble(intent, new UserHandle(1), null, directExecutor());
+
+        // This bubble will have max height which is always top aligned on small tablets
+        assertThat(mPositioner.getExpandedViewY(bubble, 0f /* bubblePosition */))
+                .isEqualTo(mPositioner.getExpandedViewYTopAligned());
+    }
+
+    @Test
+    public void testExpandedViewY_largeScreenLandscape() {
+        Insets insets = Insets.of(10, 20, 5, 15);
+        Rect screenBounds = new Rect(0, 0, 1800, 2600);
+
+        DeviceConfig deviceConfig = new ConfigBuilder()
+                .setLargeScreen()
+                .setLandscape()
+                .setInsets(insets)
+                .setScreenBounds(screenBounds)
+                .build();
+        mPositioner.update(deviceConfig);
+
+        Intent intent = new Intent(Intent.ACTION_VIEW).setPackage(mContext.getPackageName());
+        Bubble bubble = Bubble.createAppBubble(intent, new UserHandle(1), null, directExecutor());
+
+        // This bubble will have max height which is always top aligned on landscape, large tablet
+        assertThat(mPositioner.getExpandedViewY(bubble, 0f /* bubblePosition */))
+                .isEqualTo(mPositioner.getExpandedViewYTopAligned());
+    }
+
+    @Test
+    public void testExpandedViewY_largeScreenPortrait() {
+        Insets insets = Insets.of(10, 20, 5, 15);
+        Rect screenBounds = new Rect(0, 0, 1800, 2600);
+
+        DeviceConfig deviceConfig = new ConfigBuilder()
+                .setLargeScreen()
+                .setInsets(insets)
+                .setScreenBounds(screenBounds)
+                .build();
+        mPositioner.update(deviceConfig);
+
+        Intent intent = new Intent(Intent.ACTION_VIEW).setPackage(mContext.getPackageName());
+        Bubble bubble = Bubble.createAppBubble(intent, new UserHandle(1), null, directExecutor());
+
+        int manageButtonHeight =
+                mContext.getResources().getDimensionPixelSize(R.dimen.bubble_manage_button_height);
+        int manageButtonPlusMargin = manageButtonHeight + 2
+                * mContext.getResources().getDimensionPixelSize(
+                        R.dimen.bubble_manage_button_margin);
+        int pointerWidth = mContext.getResources().getDimensionPixelSize(
+                R.dimen.bubble_pointer_width);
+
+        final float expectedExpandedViewY = mPositioner.getAvailableRect().bottom
+                - manageButtonPlusMargin
+                - mPositioner.getExpandedViewHeightForLargeScreen()
+                - pointerWidth;
+
+        // Bubbles are bottom aligned on portrait, large tablet
+        assertThat(mPositioner.getExpandedViewY(bubble, 0f /* bubblePosition */))
+                .isEqualTo(expectedExpandedViewY);
+    }
+
     /**
      * Calculates the Y position bubbles should be placed based on the config. Based on
      * the calculations in {@link BubblePositioner#getDefaultStartPosition()} and
diff --git a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/windowdecor/DesktopModeWindowDecorViewModelTests.kt b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/windowdecor/DesktopModeWindowDecorViewModelTests.kt
index 57aa47e..883c24e 100644
--- a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/windowdecor/DesktopModeWindowDecorViewModelTests.kt
+++ b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/windowdecor/DesktopModeWindowDecorViewModelTests.kt
@@ -22,6 +22,7 @@
 import android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM
 import android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN
 import android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED
+import android.content.Context
 import android.graphics.Rect
 import android.hardware.display.DisplayManager
 import android.hardware.display.VirtualDisplay
@@ -39,7 +40,8 @@
 import android.view.SurfaceView
 import android.view.WindowInsets.Type.navigationBars
 import android.view.WindowInsets.Type.statusBars
-import androidx.core.content.getSystemService
+import android.view.WindowManager
+import android.window.TransitionInfo
 import androidx.test.filters.SmallTest
 import com.android.wm.shell.RootTaskDisplayAreaOrganizer
 import com.android.wm.shell.ShellTaskOrganizer
@@ -291,6 +293,30 @@
     }
 
     @Test
+    fun testRelayoutBlockedDuringKeyguardTransition() {
+        val transition = mock(IBinder::class.java)
+        val task = createTask(windowingMode = WINDOWING_MODE_FREEFORM)
+        val decoration = setUpMockDecorationForTask(task)
+        val transitionInfo = mock(TransitionInfo::class.java)
+        val transitionChange = mock(TransitionInfo.Change::class.java)
+        val taskInfo = mock(RunningTaskInfo()::class.java)
+
+        // Replicate a keyguard going away transition for a task
+        whenever(transitionInfo.getFlags())
+                .thenReturn(WindowManager.TRANSIT_FLAG_KEYGUARD_GOING_AWAY)
+        whenever(transitionChange.getMode()).thenReturn(WindowManager.TRANSIT_TO_FRONT)
+        whenever(transitionChange.getTaskInfo()).thenReturn(taskInfo)
+
+        // Make sure a window decorations exists first by launching a freeform task.
+        onTaskOpening(task)
+        // OnTransition ready is called when a keyguard going away transition happens
+        desktopModeWindowDecorViewModel
+                .onTransitionReady(transition, transitionInfo, transitionChange)
+
+        verify(decoration).incrementRelayoutBlock()
+        verify(decoration).addTransitionPausingRelayout(transition)
+    }
+    @Test
     fun testRelayoutRunsWhenStatusBarsInsetsSourceVisibilityChanges() {
         val task = createTask(windowingMode = WINDOWING_MODE_FREEFORM, focused = true)
         val decoration = setUpMockDecorationForTask(task)
@@ -401,7 +427,8 @@
 
     private fun createVirtualDisplay(): VirtualDisplay? {
         val surfaceView = SurfaceView(mContext)
-        return mContext.getSystemService<DisplayManager>()?.createVirtualDisplay(
+        val dm = mContext.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager
+        return dm.createVirtualDisplay(
                 "testEventReceiversOnMultipleDisplays",
                 /*width=*/ 400,
                 /*height=*/ 400,
diff --git a/libs/androidfw/FileStream.cpp b/libs/androidfw/FileStream.cpp
index b86c9cb..e898949 100644
--- a/libs/androidfw/FileStream.cpp
+++ b/libs/androidfw/FileStream.cpp
@@ -22,6 +22,7 @@
 
 #include "android-base/errors.h"
 #include "android-base/file.h"  // for O_BINARY
+#include "android-base/logging.h"
 #include "android-base/macros.h"
 #include "android-base/utf8.h"
 
@@ -37,9 +38,9 @@
 namespace android {
 
 FileInputStream::FileInputStream(const std::string& path, size_t buffer_capacity)
-    : buffer_capacity_(buffer_capacity) {
+    : should_close_(true), buffer_capacity_(buffer_capacity) {
   int mode = O_RDONLY | O_CLOEXEC | O_BINARY;
-  fd_.reset(TEMP_FAILURE_RETRY(::android::base::utf8::open(path.c_str(), mode)));
+  fd_ = TEMP_FAILURE_RETRY(::android::base::utf8::open(path.c_str(), mode));
   if (fd_ == -1) {
     error_ = SystemErrorCodeToString(errno);
   } else {
@@ -48,7 +49,7 @@
 }
 
 FileInputStream::FileInputStream(int fd, size_t buffer_capacity)
-    : fd_(fd), buffer_capacity_(buffer_capacity) {
+    : fd_(fd), should_close_(true), buffer_capacity_(buffer_capacity) {
   if (fd_ < 0) {
     error_ = "Bad File Descriptor";
   } else {
@@ -56,6 +57,17 @@
   }
 }
 
+FileInputStream::FileInputStream(android::base::borrowed_fd fd, size_t buffer_capacity)
+    : fd_(fd.get()), should_close_(false), buffer_capacity_(buffer_capacity) {
+
+  if (fd_ < 0) {
+    error_ = "Bad File Descriptor";
+  } else {
+    buffer_.reset(new uint8_t[buffer_capacity_]);
+  }
+}
+
+
 bool FileInputStream::Next(const void** data, size_t* size) {
   if (HadError()) {
     return false;
@@ -73,7 +85,12 @@
   ssize_t n = TEMP_FAILURE_RETRY(read(fd_, buffer_.get(), buffer_capacity_));
   if (n < 0) {
     error_ = SystemErrorCodeToString(errno);
-    fd_.reset();
+    if (fd_ != -1) {
+      if (should_close_) {
+        close(fd_);
+      }
+      fd_ = -1;
+    }
     buffer_.reset();
     return false;
   }
diff --git a/libs/androidfw/include/androidfw/BigBufferStream.h b/libs/androidfw/include/androidfw/BigBufferStream.h
index e55fe0d..c23194b 100644
--- a/libs/androidfw/include/androidfw/BigBufferStream.h
+++ b/libs/androidfw/include/androidfw/BigBufferStream.h
@@ -24,8 +24,13 @@
 class BigBufferInputStream : public KnownSizeInputStream {
  public:
   inline explicit BigBufferInputStream(const BigBuffer* buffer)
-      : buffer_(buffer), iter_(buffer->begin()) {
+      : owning_buffer_(0), buffer_(buffer), iter_(buffer->begin()) {
   }
+
+  inline explicit BigBufferInputStream(android::BigBuffer&& buffer)
+      : owning_buffer_(std::move(buffer)), buffer_(&owning_buffer_), iter_(buffer_->begin()) {
+  }
+
   virtual ~BigBufferInputStream() = default;
 
   bool Next(const void** data, size_t* size) override;
@@ -47,6 +52,7 @@
  private:
   DISALLOW_COPY_AND_ASSIGN(BigBufferInputStream);
 
+  android::BigBuffer owning_buffer_;
   const BigBuffer* buffer_;
   BigBuffer::const_iterator iter_;
   size_t offset_ = 0;
diff --git a/libs/androidfw/include/androidfw/FileStream.h b/libs/androidfw/include/androidfw/FileStream.h
index fb84a91..87c42d1 100644
--- a/libs/androidfw/include/androidfw/FileStream.h
+++ b/libs/androidfw/include/androidfw/FileStream.h
@@ -18,6 +18,7 @@
 
 #include <memory>
 #include <string>
+#include <unistd.h>
 
 #include "Streams.h"
 #include "android-base/macros.h"
@@ -35,6 +36,16 @@
   // Take ownership of `fd`.
   explicit FileInputStream(int fd, size_t buffer_capacity = kDefaultBufferCapacity);
 
+  // Take ownership of `fd`.
+  explicit FileInputStream(android::base::borrowed_fd fd,
+                           size_t buffer_capacity = kDefaultBufferCapacity);
+
+  ~FileInputStream() {
+    if (should_close_ && (fd_ != -1)) {
+      close(fd_);
+    }
+  }
+
   bool Next(const void** data, size_t* size) override;
 
   void BackUp(size_t count) override;
@@ -50,8 +61,9 @@
  private:
   DISALLOW_COPY_AND_ASSIGN(FileInputStream);
 
-  android::base::unique_fd fd_;
+  int fd_ = -1;
   std::string error_;
+  bool should_close_;
   std::unique_ptr<uint8_t[]> buffer_;
   size_t buffer_capacity_ = 0u;
   size_t buffer_offset_ = 0u;
diff --git a/libs/androidfw/include/androidfw/IDiagnostics.h b/libs/androidfw/include/androidfw/IDiagnostics.h
index 865a298..d1dda81 100644
--- a/libs/androidfw/include/androidfw/IDiagnostics.h
+++ b/libs/androidfw/include/androidfw/IDiagnostics.h
@@ -17,10 +17,15 @@
 #ifndef _ANDROID_DIAGNOSTICS_H
 #define _ANDROID_DIAGNOSTICS_H
 
+// on some systems ERROR is defined as 0 so android::base::ERROR becomes android::base::0
+// which doesn't compile. We undef it here to avoid that and because we don't ever need that def.
+#undef ERROR
+
 #include <sstream>
 #include <string>
 
 #include "Source.h"
+#include "android-base/logging.h"
 #include "android-base/macros.h"
 #include "androidfw/StringPiece.h"
 
@@ -144,6 +149,36 @@
   DISALLOW_COPY_AND_ASSIGN(NoOpDiagnostics);
 };
 
+class AndroidLogDiagnostics : public IDiagnostics {
+  public:
+    AndroidLogDiagnostics() = default;
+
+    void Log(Level level, DiagMessageActual& actual_msg) override {
+      android::base::LogSeverity severity;
+      switch (level) {
+        case Level::Error:
+          severity = android::base::ERROR;
+          break;
+
+        case Level::Warn:
+          severity = android::base::WARNING;
+          break;
+
+        case Level::Note:
+          severity = android::base::INFO;
+          break;
+      }
+      if (!actual_msg.source.path.empty()) {
+        LOG(severity) << actual_msg.source << ": " + actual_msg.message;
+      } else {
+        LOG(severity) << actual_msg.message;
+      }
+    }
+
+    DISALLOW_COPY_AND_ASSIGN(AndroidLogDiagnostics);
+};
+
+
 }  // namespace android
 
 #endif /* _ANDROID_DIAGNOSTICS_H */
diff --git a/libs/androidfw/include/androidfw/ResourceTypes.h b/libs/androidfw/include/androidfw/ResourceTypes.h
index fdb3551..c0514fd 100644
--- a/libs/androidfw/include/androidfw/ResourceTypes.h
+++ b/libs/androidfw/include/androidfw/ResourceTypes.h
@@ -1875,6 +1875,7 @@
   off64_t binary_data_offset;
   size_t binary_data_size;
   std::string configuration;
+  bool nine_patch;
 };
 
 class AssetManager2;
diff --git a/libs/hwui/aconfig/hwui_flags.aconfig b/libs/hwui/aconfig/hwui_flags.aconfig
index 78a6479..64b2a93 100644
--- a/libs/hwui/aconfig/hwui_flags.aconfig
+++ b/libs/hwui/aconfig/hwui_flags.aconfig
@@ -1,6 +1,13 @@
 package: "com.android.graphics.hwui.flags"
 
 flag {
+  name: "matrix_44"
+  namespace: "core_graphics"
+  description: "API for 4x4 matrix and related canvas functions"
+  bug: "280116960"
+}
+
+flag {
   name: "limited_hdr"
   namespace: "core_graphics"
   description: "API to enable apps to restrict the amount of HDR headroom that is used"
diff --git a/media/java/android/media/IMediaRouterService.aidl b/media/java/android/media/IMediaRouterService.aidl
index 4a5b4f2..fa4d1a1 100644
--- a/media/java/android/media/IMediaRouterService.aidl
+++ b/media/java/android/media/IMediaRouterService.aidl
@@ -26,6 +26,7 @@
 import android.media.RouteListingPreference;
 import android.media.RoutingSessionInfo;
 import android.os.Bundle;
+import android.os.UserHandle;
 
 /**
  * {@hide}
@@ -50,7 +51,6 @@
     // MediaRouterService.java for readability.
 
     // Methods for MediaRouter2
-    boolean verifyPackageExists(String clientPackageName);
     List<MediaRoute2Info> getSystemRoutes();
     RoutingSessionInfo getSystemSessionInfo();
 
@@ -76,6 +76,7 @@
     List<RoutingSessionInfo> getRemoteSessions(IMediaRouter2Manager manager);
     RoutingSessionInfo getSystemSessionInfoForPackage(String packageName);
     void registerManager(IMediaRouter2Manager manager, String packageName);
+    void registerProxyRouter(IMediaRouter2Manager manager, String callingPackageName, String targetPackageName, in UserHandle targetUser);
     void unregisterManager(IMediaRouter2Manager manager);
     void setRouteVolumeWithManager(IMediaRouter2Manager manager, int requestId,
             in MediaRoute2Info route, int volume);
diff --git a/media/java/android/media/MediaRouter2.java b/media/java/android/media/MediaRouter2.java
index bde0c0c..ba26df9 100644
--- a/media/java/android/media/MediaRouter2.java
+++ b/media/java/android/media/MediaRouter2.java
@@ -18,6 +18,7 @@
 
 import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage;
 import static com.android.media.flags.Flags.FLAG_ENABLE_RLP_CALLBACKS_IN_MEDIA_ROUTER2;
+import static com.android.media.flags.Flags.FLAG_ENABLE_CROSS_USER_ROUTING_IN_MEDIA_ROUTER2;
 
 import android.Manifest;
 import android.annotation.CallbackExecutor;
@@ -32,8 +33,10 @@
 import android.os.Handler;
 import android.os.Looper;
 import android.os.Message;
+import android.os.Process;
 import android.os.RemoteException;
 import android.os.ServiceManager;
+import android.os.UserHandle;
 import android.text.TextUtils;
 import android.util.ArrayMap;
 import android.util.ArraySet;
@@ -78,8 +81,11 @@
     // The manager request ID representing that no manager is involved.
     private static final long MANAGER_REQUEST_ID_NONE = MediaRoute2ProviderService.REQUEST_ID_NONE;
 
+    private record PackageNameUserHandlePair(String packageName, UserHandle user) {}
+
     @GuardedBy("sSystemRouterLock")
-    private static final Map<String, MediaRouter2> sSystemMediaRouter2Map = new ArrayMap<>();
+    private static final Map<PackageNameUserHandlePair, MediaRouter2> sAppToProxyRouterMap =
+            new ArrayMap<>();
 
     @GuardedBy("sRouterLock")
     private static MediaRouter2 sInstance;
@@ -161,66 +167,121 @@
     }
 
     /**
-     * Gets an instance of the system media router which controls the app's media routing. Returns
-     * {@code null} if the given package name is invalid. There are several things to note when
-     * using the media routers created with this method.
+     * Returns a proxy MediaRouter2 instance that allows you to control the routing of an app
+     * specified by {@code clientPackageName}. Returns {@code null} if the specified package name
+     * does not exist.
      *
-     * <p>First of all, the discovery preference passed to {@link #registerRouteCallback} will have
-     * no effect. The callback will be called accordingly with the client app's discovery
-     * preference. Therefore, it is recommended to pass {@link RouteDiscoveryPreference#EMPTY}
-     * there.
-     *
-     * <p>Also, do not keep/compare the instances of the {@link RoutingController}, since they are
-     * always newly created with the latest session information whenever below methods are called:
+     * <p>Proxy MediaRouter2 instances operate differently than regular MediaRouter2 instances:
      *
      * <ul>
-     *   <li>{@link #getControllers()}
-     *   <li>{@link #getController(String)}
-     *   <li>{@link TransferCallback#onTransfer(RoutingController, RoutingController)}
-     *   <li>{@link TransferCallback#onStop(RoutingController)}
-     *   <li>{@link ControllerCallback#onControllerUpdated(RoutingController)}
+     *   <li>
+     *       <p>{@link #registerRouteCallback} ignores any {@link RouteDiscoveryPreference discovery
+     *       preference} passed by a proxy router. Use {@link RouteDiscoveryPreference#EMPTY} when
+     *       setting a route callback.
+     *   <li>
+     *       <p>Methods returning non-system {@link RoutingController controllers} always return
+     *       new instances with the latest data. Do not attempt to compare or store them. Instead,
+     *       use {@link #getController(String)} or {@link #getControllers()} to query the most
+     *       up-to-date state.
+     *   <li>
+     *       <p>Calls to {@link #setOnGetControllerHintsListener} are ignored.
      * </ul>
      *
-     * Therefore, in order to track the current routing status, keep the controller's ID instead,
-     * and use {@link #getController(String)} and {@link #getSystemController()} for getting
-     * controllers.
-     *
-     * <p>Finally, it will have no effect to call {@link #setOnGetControllerHintsListener}.
-     *
      * @param clientPackageName the package name of the app to control
      * @throws SecurityException if the caller doesn't have {@link
      *     Manifest.permission#MEDIA_CONTENT_CONTROL MEDIA_CONTENT_CONTROL} permission.
      * @hide
      */
+    // TODO (b/311711420): Deprecate once #getInstance(Context, Looper, String, UserHandle)
+    //  reaches public SDK.
     @SystemApi
     @RequiresPermission(Manifest.permission.MEDIA_CONTENT_CONTROL)
     @Nullable
     public static MediaRouter2 getInstance(
             @NonNull Context context, @NonNull String clientPackageName) {
-        Objects.requireNonNull(context, "context must not be null");
-        Objects.requireNonNull(clientPackageName, "clientPackageName must not be null");
-
-        // Note: Even though this check could be somehow bypassed, the other permission checks
-        // in system server will not allow MediaRouter2Manager to be registered.
-        IMediaRouterService serviceBinder =
-                IMediaRouterService.Stub.asInterface(
-                        ServiceManager.getService(Context.MEDIA_ROUTER_SERVICE));
+        // Capturing the IAE here to not break nullability.
         try {
-            // verifyPackageExists throws SecurityException if the caller doesn't hold
-            // MEDIA_CONTENT_CONTROL permission.
-            if (!serviceBinder.verifyPackageExists(clientPackageName)) {
-                Log.e(TAG, "Package " + clientPackageName + " not found. Ignoring.");
-                return null;
-            }
-        } catch (RemoteException e) {
-            e.rethrowFromSystemServer();
+            return findOrCreateProxyInstanceForCallingUser(
+                    context, Looper.getMainLooper(), clientPackageName, context.getUser());
+        } catch (IllegalArgumentException ex) {
+            Log.e(TAG, "Package " + clientPackageName + " not found. Ignoring.");
+            return null;
+        }
+    }
+
+    /**
+     * Returns a proxy MediaRouter2 instance that allows you to control the routing of an app
+     * specified by {@code clientPackageName} and {@code user}.
+     *
+     * <p>You can specify any {@link Looper} of choice on which internal state updates will run.
+     *
+     * <p>Proxy MediaRouter2 instances operate differently than regular MediaRouter2 instances:
+     *
+     * <ul>
+     *   <li>
+     *       <p>{@link #registerRouteCallback} ignores any {@link RouteDiscoveryPreference discovery
+     *       preference} passed by a proxy router. Use a {@link RouteDiscoveryPreference} with empty
+     *       {@link RouteDiscoveryPreference.Builder#setPreferredFeatures(List) preferred features}
+     *       when setting a route callback.
+     *   <li>
+     *       <p>Methods returning non-system {@link RoutingController controllers} always return
+     *       new instances with the latest data. Do not attempt to compare or store them. Instead,
+     *       use {@link #getController(String)} or {@link #getControllers()} to query the most
+     *       up-to-date state.
+     *   <li>
+     *       <p>Calls to {@link #setOnGetControllerHintsListener} are ignored.
+     * </ul>
+     *
+     * @param context The {@link Context} of the caller.
+     * @param looper The {@link Looper} on which to process internal state changes.
+     * @param clientPackageName The package name of the app you want to control the routing of.
+     * @param user The {@link UserHandle} of the user running the app for which to get the proxy
+     *     router instance. Must match {@link Process#myUserHandle()} if the caller doesn't hold
+     *     {@code Manifest.permission#INTERACT_ACROSS_USERS_FULL}.
+     * @throws SecurityException if {@code user} does not match {@link Process#myUserHandle()} and
+     *     the caller does not hold {@code Manifest.permission#INTERACT_ACROSS_USERS_FULL}.
+     * @throws IllegalArgumentException if {@code clientPackageName} does not exist in {@code user}.
+     */
+    @FlaggedApi(FLAG_ENABLE_CROSS_USER_ROUTING_IN_MEDIA_ROUTER2)
+    @RequiresPermission(
+            anyOf = {
+                Manifest.permission.MEDIA_CONTENT_CONTROL,
+                Manifest.permission.MEDIA_ROUTING_CONTROL
+            })
+    @NonNull
+    public static MediaRouter2 getInstance(
+            @NonNull Context context,
+            @NonNull Looper looper,
+            @NonNull String clientPackageName,
+            @NonNull UserHandle user) {
+        return findOrCreateProxyInstanceForCallingUser(context, looper, clientPackageName, user);
+    }
+
+    /**
+     * Returns the per-process singleton proxy router instance for the {@code clientPackageName} and
+     * {@code user} if it exists, or otherwise it creates the appropriate instance.
+     *
+     * <p>If no instance has been created previously, the method will create an instance via {@link
+     * #MediaRouter2(Context, Looper, String, UserHandle)}.
+     */
+    @NonNull
+    private static MediaRouter2 findOrCreateProxyInstanceForCallingUser(
+            Context context, Looper looper, String clientPackageName, UserHandle user) {
+        Objects.requireNonNull(context, "context must not be null");
+        Objects.requireNonNull(looper, "looper must not be null");
+        Objects.requireNonNull(user, "user must not be null");
+
+        if (TextUtils.isEmpty(clientPackageName)) {
+            throw new IllegalArgumentException("clientPackageName must not be null or empty");
         }
 
+        PackageNameUserHandlePair key = new PackageNameUserHandlePair(clientPackageName, user);
+
         synchronized (sSystemRouterLock) {
-            MediaRouter2 instance = sSystemMediaRouter2Map.get(clientPackageName);
+            MediaRouter2 instance = sAppToProxyRouterMap.get(key);
             if (instance == null) {
-                instance = new MediaRouter2(context, clientPackageName);
-                sSystemMediaRouter2Map.put(clientPackageName, instance);
+                instance = new MediaRouter2(context, looper, clientPackageName, user);
+                sAppToProxyRouterMap.put(key, instance);
             }
             return instance;
         }
@@ -304,9 +365,10 @@
         mSystemController = new SystemRoutingController(currentSystemSessionInfo);
     }
 
-    private MediaRouter2(Context context, String clientPackageName) {
+    private MediaRouter2(
+            Context context, Looper looper, String clientPackageName, UserHandle user) {
         mContext = context;
-        mHandler = new Handler(Looper.getMainLooper());
+        mHandler = new Handler(looper);
         mMediaRouterService =
                 IMediaRouterService.Stub.asInterface(
                         ServiceManager.getService(Context.MEDIA_ROUTER_SERVICE));
@@ -315,7 +377,7 @@
                 new SystemRoutingController(
                         ProxyMediaRouter2Impl.getSystemSessionInfoImpl(
                                 mMediaRouterService, clientPackageName));
-        mImpl = new ProxyMediaRouter2Impl(context, clientPackageName);
+        mImpl = new ProxyMediaRouter2Impl(context, clientPackageName, user);
     }
 
     /**
@@ -2000,25 +2062,30 @@
      */
     private class ProxyMediaRouter2Impl implements MediaRouter2Impl {
         // Fields originating from MediaRouter2Manager.
-        private final MediaRouter2Manager mManager;
         private final IMediaRouter2Manager.Stub mClient;
         private final CopyOnWriteArrayList<MediaRouter2Manager.TransferRequest>
                 mTransferRequests = new CopyOnWriteArrayList<>();
+        private final AtomicInteger mScanRequestCount = new AtomicInteger(/* initialValue= */ 0);
 
         // Fields originating from MediaRouter2.
         @NonNull private final String mClientPackageName;
-
-        // TODO(b/281072508): Implement scan request counting when MediaRouter2Manager is removed.
+        @NonNull private final UserHandle mClientUser;
         private final AtomicBoolean mIsScanning = new AtomicBoolean(/* initialValue= */ false);
 
-        ProxyMediaRouter2Impl(@NonNull Context context, @NonNull String clientPackageName) {
-            mManager = MediaRouter2Manager.getInstance(context.getApplicationContext());
+        ProxyMediaRouter2Impl(
+                @NonNull Context context,
+                @NonNull String clientPackageName,
+                @NonNull UserHandle user) {
+            mClientUser = user;
             mClientPackageName = clientPackageName;
             mClient = new Client();
 
             try {
-                mMediaRouterService.registerManager(
-                        mClient, context.getApplicationContext().getPackageName());
+                mMediaRouterService.registerProxyRouter(
+                        mClient,
+                        context.getApplicationContext().getPackageName(),
+                        clientPackageName,
+                        user);
             } catch (RemoteException ex) {
                 throw ex.rethrowFromSystemServer();
             }
@@ -2029,14 +2096,35 @@
         @Override
         public void startScan() {
             if (!mIsScanning.getAndSet(true)) {
-                mManager.registerScanRequest();
+                if (mScanRequestCount.getAndIncrement() == 0) {
+                    try {
+                        mMediaRouterService.startScan(mClient);
+                    } catch (RemoteException ex) {
+                        throw ex.rethrowFromSystemServer();
+                    }
+                }
             }
         }
 
         @Override
         public void stopScan() {
             if (mIsScanning.getAndSet(false)) {
-                mManager.unregisterScanRequest();
+                if (mScanRequestCount.updateAndGet(
+                                count -> {
+                                    if (count == 0) {
+                                        throw new IllegalStateException(
+                                                "No active scan requests to unregister.");
+                                    } else {
+                                        return --count;
+                                    }
+                                })
+                        == 0) {
+                    try {
+                        mMediaRouterService.stopScan(mClient);
+                    } catch (RemoteException ex) {
+                        throw ex.rethrowFromSystemServer();
+                    }
+                }
             }
         }
 
diff --git a/media/java/android/media/flags/media_better_together.aconfig b/media/java/android/media/flags/media_better_together.aconfig
index 283d61b..3c0b002 100644
--- a/media/java/android/media/flags/media_better_together.aconfig
+++ b/media/java/android/media/flags/media_better_together.aconfig
@@ -55,3 +55,10 @@
     description: "Allow access to privileged routing capabilities to MEDIA_ROUTING_CONTROL holders."
     bug: "305919655"
 }
+
+flag {
+    name: "enable_cross_user_routing_in_media_router2"
+    namespace: "media_solutions"
+    description: "Allows clients of privileged MediaRouter2 that hold INTERACT_ACROSS_USERS_FULL to control routing across users."
+    bug: "288580225"
+}
diff --git a/packages/CompanionDeviceManager/res/drawable-night/ic_permission_media_routing_control.xml b/packages/CompanionDeviceManager/res/drawable-night/ic_permission_media_routing_control.xml
new file mode 100644
index 0000000..a0426a3
--- /dev/null
+++ b/packages/CompanionDeviceManager/res/drawable-night/ic_permission_media_routing_control.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="960"
+        android:viewportHeight="960"
+        android:tint="@android:color/system_accent1_200">
+    <path android:fillColor="@android:color/system_accent1_200"
+          android:pathData="M360,840L200,840Q167,840 143.5,816.5Q120,793 120,760L120,480Q120,405 148.5,339.5Q177,274 225.5,225.5Q274,177 339.5,148.5Q405,120 480,120Q555,120 620.5,148.5Q686,177 734.5,225.5Q783,274 811.5,339.5Q840,405 840,480L840,760Q840,793 816.5,816.5Q793,840 760,840L600,840L600,520L760,520L760,480Q760,363 678.5,281.5Q597,200 480,200Q363,200 281.5,281.5Q200,363 200,480L200,520L360,520L360,840ZM280,600L200,600L200,760Q200,760 200,760Q200,760 200,760L280,760L280,600ZM680,600L680,760L760,760Q760,760 760,760Q760,760 760,760L760,600L680,600ZM280,600L280,600L200,600Q200,600 200,600Q200,600 200,600L200,600L280,600ZM680,600L760,600L760,600Q760,600 760,600Q760,600 760,600L680,600L680,600Z"/>
+</vector>
\ No newline at end of file
diff --git a/packages/CompanionDeviceManager/res/drawable/ic_permission_media_routing_control.xml b/packages/CompanionDeviceManager/res/drawable/ic_permission_media_routing_control.xml
new file mode 100644
index 0000000..9a7525b
--- /dev/null
+++ b/packages/CompanionDeviceManager/res/drawable/ic_permission_media_routing_control.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="960"
+        android:viewportHeight="960"
+        android:tint="?attr/colorControlNormal">
+    <path android:fillColor="@android:color/system_accent1_600"
+          android:pathData="M360,840L200,840Q167,840 143.5,816.5Q120,793 120,760L120,480Q120,405 148.5,339.5Q177,274 225.5,225.5Q274,177 339.5,148.5Q405,120 480,120Q555,120 620.5,148.5Q686,177 734.5,225.5Q783,274 811.5,339.5Q840,405 840,480L840,760Q840,793 816.5,816.5Q793,840 760,840L600,840L600,520L760,520L760,480Q760,363 678.5,281.5Q597,200 480,200Q363,200 281.5,281.5Q200,363 200,480L200,520L360,520L360,840ZM280,600L200,600L200,760Q200,760 200,760Q200,760 200,760L280,760L280,600ZM680,600L680,760L760,760Q760,760 760,760Q760,760 760,760L760,600L680,600ZM280,600L280,600L200,600Q200,600 200,600Q200,600 200,600L200,600L280,600ZM680,600L760,600L760,600Q760,600 760,600Q760,600 760,600L680,600L680,600Z"/>
+</vector>
\ No newline at end of file
diff --git a/packages/CompanionDeviceManager/res/values/strings.xml b/packages/CompanionDeviceManager/res/values/strings.xml
index 7a6fad4..281eba6 100644
--- a/packages/CompanionDeviceManager/res/values/strings.xml
+++ b/packages/CompanionDeviceManager/res/values/strings.xml
@@ -149,6 +149,9 @@
     <!-- Nearby devices' permission will be granted of corresponding profile [CHAR LIMIT=30] -->
     <string name="permission_nearby_devices">Nearby devices</string>
 
+    <!-- Change media output permission will be granted to the corresponding profile [CHAR LIMIT=30] -->
+    <string name="permission_media_routing_control">Change media output</string>
+
     <!-- Storage permission will be granted of corresponding profile [CHAR LIMIT=30] -->
     <string name="permission_storage">Photos and media</string>
 
@@ -194,6 +197,9 @@
     <!-- Description of nearby_device_streaming permission of corresponding profile [CHAR LIMIT=NONE] -->
     <string name="permission_nearby_device_streaming_summary">Stream apps and other system features from your phone</string>
 
+    <!-- Description of change media output permission to be granted to the corresponding profile [CHAR LIMIT=NONE] -->
+    <string name="permission_media_routing_control_summary">Access a list of available devices and control which one streams or casts audio or video from other apps</string>
+
     <!-- The type of the device for phone [CHAR LIMIT=30] -->
     <string name="device_type" product="default">phone</string>
 
diff --git a/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/CompanionDeviceResources.java b/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/CompanionDeviceResources.java
index 060c032..551e975 100644
--- a/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/CompanionDeviceResources.java
+++ b/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/CompanionDeviceResources.java
@@ -26,6 +26,7 @@
 import static com.android.companiondevicemanager.PermissionListAdapter.PERMISSION_APP_STREAMING;
 import static com.android.companiondevicemanager.PermissionListAdapter.PERMISSION_CALENDAR;
 import static com.android.companiondevicemanager.PermissionListAdapter.PERMISSION_CALL_LOGS;
+import static com.android.companiondevicemanager.PermissionListAdapter.PERMISSION_CHANGE_MEDIA_OUTPUT;
 import static com.android.companiondevicemanager.PermissionListAdapter.PERMISSION_CONTACTS;
 import static com.android.companiondevicemanager.PermissionListAdapter.PERMISSION_MICROPHONE;
 import static com.android.companiondevicemanager.PermissionListAdapter.PERMISSION_NEARBY_DEVICES;
@@ -41,6 +42,8 @@
 import android.util.ArrayMap;
 import android.util.ArraySet;
 
+import com.android.media.flags.Flags;
+
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
@@ -73,9 +76,15 @@
                 PERMISSION_NOTIFICATION, PERMISSION_STORAGE));
         map.put(DEVICE_PROFILE_NEARBY_DEVICE_STREAMING,
                 Arrays.asList(PERMISSION_NEARBY_DEVICE_STREAMING));
-        map.put(DEVICE_PROFILE_WATCH, Arrays.asList(PERMISSION_NOTIFICATION, PERMISSION_PHONE,
-                PERMISSION_CALL_LOGS, PERMISSION_SMS, PERMISSION_CONTACTS, PERMISSION_CALENDAR,
-                PERMISSION_NEARBY_DEVICES));
+        if (!Flags.enablePrivilegedRoutingForMediaRoutingControl()) {
+            map.put(DEVICE_PROFILE_WATCH, Arrays.asList(PERMISSION_NOTIFICATION, PERMISSION_PHONE,
+                    PERMISSION_CALL_LOGS, PERMISSION_SMS, PERMISSION_CONTACTS, PERMISSION_CALENDAR,
+                    PERMISSION_NEARBY_DEVICES));
+        } else {
+            map.put(DEVICE_PROFILE_WATCH, Arrays.asList(PERMISSION_NOTIFICATION, PERMISSION_PHONE,
+                    PERMISSION_CALL_LOGS, PERMISSION_SMS, PERMISSION_CONTACTS, PERMISSION_CALENDAR,
+                    PERMISSION_NEARBY_DEVICES, PERMISSION_CHANGE_MEDIA_OUTPUT));
+        }
         map.put(DEVICE_PROFILE_GLASSES, Arrays.asList(PERMISSION_NOTIFICATION, PERMISSION_PHONE,
                 PERMISSION_SMS, PERMISSION_CONTACTS, PERMISSION_MICROPHONE,
                 PERMISSION_NEARBY_DEVICES));
diff --git a/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/PermissionListAdapter.java b/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/PermissionListAdapter.java
index 7ed1816..e21aee3 100644
--- a/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/PermissionListAdapter.java
+++ b/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/PermissionListAdapter.java
@@ -54,6 +54,7 @@
     static final int PERMISSION_NEARBY_DEVICE_STREAMING = 8;
     static final int PERMISSION_MICROPHONE = 9;
     static final int PERMISSION_CALL_LOGS = 10;
+    static final int PERMISSION_CHANGE_MEDIA_OUTPUT = 11;
 
     private static final Map<Integer, Integer> sTitleMap;
     static {
@@ -69,6 +70,7 @@
         map.put(PERMISSION_NEARBY_DEVICE_STREAMING, R.string.permission_nearby_device_streaming);
         map.put(PERMISSION_MICROPHONE, R.string.permission_microphone);
         map.put(PERMISSION_CALL_LOGS, R.string.permission_call_logs);
+        map.put(PERMISSION_CHANGE_MEDIA_OUTPUT, R.string.permission_media_routing_control);
         sTitleMap = unmodifiableMap(map);
     }
 
@@ -87,6 +89,7 @@
                 R.string.permission_nearby_device_streaming_summary);
         map.put(PERMISSION_MICROPHONE, R.string.permission_microphone_summary);
         map.put(PERMISSION_CALL_LOGS, R.string.permission_call_logs_summary);
+        map.put(PERMISSION_CHANGE_MEDIA_OUTPUT, R.string.permission_media_routing_control_summary);
         sSummaryMap = unmodifiableMap(map);
     }
 
@@ -105,6 +108,7 @@
                 R.drawable.ic_permission_nearby_device_streaming);
         map.put(PERMISSION_MICROPHONE, R.drawable.ic_permission_microphone);
         map.put(PERMISSION_CALL_LOGS, R.drawable.ic_permission_call_logs);
+        map.put(PERMISSION_CHANGE_MEDIA_OUTPUT, R.drawable.ic_permission_media_routing_control);
         sIconMap = unmodifiableMap(map);
     }
 
diff --git a/packages/PackageInstaller/AndroidManifest.xml b/packages/PackageInstaller/AndroidManifest.xml
index 2e4fd9b..5a21d59 100644
--- a/packages/PackageInstaller/AndroidManifest.xml
+++ b/packages/PackageInstaller/AndroidManifest.xml
@@ -36,21 +36,13 @@
             android:forceQueryable="true"
             android:directBootAware="true">
 
-        <receiver android:name=".TemporaryFileManager"
+        <receiver android:name=".common.TemporaryFileManager"
             android:exported="false">
             <intent-filter>
                 <action android:name="android.intent.action.BOOT_COMPLETED" />
             </intent-filter>
         </receiver>
 
-        <receiver android:name="v2.model.TemporaryFileManager"
-            android:exported="false"
-            android:enabled="false">
-            <intent-filter>
-                <action android:name="android.intent.action.BOOT_COMPLETED" />
-            </intent-filter>
-        </receiver>
-
         <activity android:name=".v2.ui.InstallLaunch"
             android:configChanges="orientation|keyboardHidden|screenSize"
             android:theme="@style/Theme.AlertDialogActivity"
@@ -101,7 +93,7 @@
                 android:theme="@style/Theme.AlertDialogActivity.NoAnimation"
                 android:exported="false" />
 
-        <receiver android:name=".InstallEventReceiver"
+        <receiver android:name=".common.InstallEventReceiver"
                 android:permission="android.permission.INSTALL_PACKAGES"
                 android:exported="false">
             <intent-filter android:priority="1">
@@ -109,15 +101,6 @@
             </intent-filter>
         </receiver>
 
-        <receiver android:name=".v2.model.InstallEventReceiver"
-            android:permission="android.permission.INSTALL_PACKAGES"
-            android:exported="false"
-            android:enabled="false">
-            <intent-filter android:priority="1">
-                <action android:name="com.android.packageinstaller.ACTION_INSTALL_COMMIT" />
-            </intent-filter>
-        </receiver>
-
         <activity android:name=".InstallSuccess"
                 android:theme="@style/Theme.AlertDialogActivity.NoAnimation"
                 android:exported="false" />
@@ -148,7 +131,7 @@
             android:exported="false">
         </activity>
 
-        <receiver android:name=".UninstallEventReceiver"
+        <receiver android:name=".common.UninstallEventReceiver"
             android:permission="android.permission.INSTALL_PACKAGES"
             android:exported="false">
             <intent-filter android:priority="1">
@@ -156,15 +139,6 @@
             </intent-filter>
         </receiver>
 
-        <receiver android:name=".v2.model.UninstallEventReceiver"
-            android:permission="android.permission.INSTALL_PACKAGES"
-            android:exported="false"
-            android:enabled="false">
-            <intent-filter android:priority="1">
-                <action android:name="com.android.packageinstaller.ACTION_UNINSTALL_COMMIT" />
-            </intent-filter>
-        </receiver>
-
         <receiver android:name=".PackageInstalledReceiver"
                 android:exported="false">
             <intent-filter android:priority="1">
diff --git a/packages/PackageInstaller/src/com/android/packageinstaller/InstallInstalling.java b/packages/PackageInstaller/src/com/android/packageinstaller/InstallInstalling.java
index 8d8254a..1a6c2bb 100644
--- a/packages/PackageInstaller/src/com/android/packageinstaller/InstallInstalling.java
+++ b/packages/PackageInstaller/src/com/android/packageinstaller/InstallInstalling.java
@@ -33,9 +33,9 @@
 import android.util.Log;
 import android.view.View;
 import android.widget.Button;
-
 import androidx.annotation.Nullable;
-
+import com.android.packageinstaller.common.EventResultPersister;
+import com.android.packageinstaller.common.InstallEventReceiver;
 import java.io.File;
 import java.io.IOException;
 
diff --git a/packages/PackageInstaller/src/com/android/packageinstaller/InstallStart.java b/packages/PackageInstaller/src/com/android/packageinstaller/InstallStart.java
index 4187058..dbf0b48 100644
--- a/packages/PackageInstaller/src/com/android/packageinstaller/InstallStart.java
+++ b/packages/PackageInstaller/src/com/android/packageinstaller/InstallStart.java
@@ -23,7 +23,6 @@
 import android.app.Activity;
 import android.app.DialogFragment;
 import android.app.admin.DevicePolicyManager;
-import android.content.ComponentName;
 import android.content.ContentResolver;
 import android.content.Intent;
 import android.content.pm.ApplicationInfo;
@@ -39,7 +38,6 @@
 import android.text.TextUtils;
 import android.util.EventLog;
 import android.util.Log;
-
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
 import com.android.packageinstaller.v2.ui.InstallLaunch;
@@ -63,17 +61,10 @@
     protected void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
-        mPackageManager = getPackageManager();
+
         if (usePiaV2()) {
             Log.i(TAG, "Using Pia V2");
 
-            mPackageManager.setComponentEnabledSetting(new ComponentName(this,
-                    com.android.packageinstaller.InstallEventReceiver.class),
-                PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
-            mPackageManager.setComponentEnabledSetting(new ComponentName(this,
-                    com.android.packageinstaller.v2.model.InstallEventReceiver.class),
-                PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0);
-
             Intent piaV2 = new Intent(getIntent());
             piaV2.putExtra(InstallLaunch.EXTRA_CALLING_PKG_NAME, getCallingPackage());
             piaV2.putExtra(InstallLaunch.EXTRA_CALLING_PKG_UID, getLaunchedFromUid());
@@ -83,6 +74,7 @@
             finish();
             return;
         }
+        mPackageManager = getPackageManager();
         mUserManager = getSystemService(UserManager.class);
 
         Intent intent = getIntent();
diff --git a/packages/PackageInstaller/src/com/android/packageinstaller/TemporaryFileManager.java b/packages/PackageInstaller/src/com/android/packageinstaller/TemporaryFileManager.java
deleted file mode 100644
index afb2ea4..0000000
--- a/packages/PackageInstaller/src/com/android/packageinstaller/TemporaryFileManager.java
+++ /dev/null
@@ -1,96 +0,0 @@
-/*
- * Copyright (C) 2017 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.packageinstaller;
-
-import android.content.BroadcastReceiver;
-import android.content.Context;
-import android.content.Intent;
-import android.os.SystemClock;
-import android.util.Log;
-
-import androidx.annotation.NonNull;
-
-import java.io.File;
-import java.io.IOException;
-
-/**
- * Manages files of the package installer and resets state during boot.
- */
-public class TemporaryFileManager extends BroadcastReceiver {
-    private static final String LOG_TAG = TemporaryFileManager.class.getSimpleName();
-
-    /**
-     * Create a new file to hold a staged file.
-     *
-     * @param context The context of the caller
-     *
-     * @return A new file
-     */
-    @NonNull
-    public static File getStagedFile(@NonNull Context context) throws IOException {
-        return File.createTempFile("package", ".apk", context.getNoBackupFilesDir());
-    }
-
-    /**
-     * Get the file used to store the results of installs.
-     *
-     * @param context The context of the caller
-     *
-     * @return the file used to store the results of installs
-     */
-    @NonNull
-    public static File getInstallStateFile(@NonNull Context context) {
-        return new File(context.getNoBackupFilesDir(), "install_results.xml");
-    }
-
-    /**
-     * Get the file used to store the results of uninstalls.
-     *
-     * @param context The context of the caller
-     *
-     * @return the file used to store the results of uninstalls
-     */
-    @NonNull
-    public static File getUninstallStateFile(@NonNull Context context) {
-        return new File(context.getNoBackupFilesDir(), "uninstall_results.xml");
-    }
-
-    @Override
-    public void onReceive(Context context, Intent intent) {
-        long systemBootTime = System.currentTimeMillis() - SystemClock.elapsedRealtime();
-
-        File[] filesOnBoot = context.getNoBackupFilesDir().listFiles();
-
-        if (filesOnBoot == null) {
-            return;
-        }
-
-        for (int i = 0; i < filesOnBoot.length; i++) {
-            File fileOnBoot = filesOnBoot[i];
-
-            if (systemBootTime > fileOnBoot.lastModified()) {
-                boolean wasDeleted = fileOnBoot.delete();
-                if (!wasDeleted) {
-                    Log.w(LOG_TAG, "Could not delete " + fileOnBoot.getName() + " onBoot");
-                }
-            } else {
-                Log.w(LOG_TAG, fileOnBoot.getName() + " was created before onBoot broadcast was "
-                        + "received");
-            }
-        }
-    }
-}
diff --git a/packages/PackageInstaller/src/com/android/packageinstaller/UninstallEventReceiver.java b/packages/PackageInstaller/src/com/android/packageinstaller/UninstallEventReceiver.java
deleted file mode 100644
index 86b0321..0000000
--- a/packages/PackageInstaller/src/com/android/packageinstaller/UninstallEventReceiver.java
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- * Copyright (C) 2016 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.packageinstaller;
-
-import android.content.BroadcastReceiver;
-import android.content.Context;
-import android.content.Intent;
-
-import androidx.annotation.NonNull;
-
-/**
- * Receives uninstall events and persists them using a {@link EventResultPersister}.
- */
-public class UninstallEventReceiver extends BroadcastReceiver {
-    private static final Object sLock = new Object();
-    private static EventResultPersister sReceiver;
-
-    /**
-     * Get the event receiver persisting the results
-     *
-     * @return The event receiver.
-     */
-    @NonNull private static EventResultPersister getReceiver(@NonNull Context context) {
-        synchronized (sLock) {
-            if (sReceiver == null) {
-                sReceiver = new EventResultPersister(
-                        TemporaryFileManager.getUninstallStateFile(context));
-            }
-        }
-
-        return sReceiver;
-    }
-
-    @Override
-    public void onReceive(Context context, Intent intent) {
-        getReceiver(context).onEventReceived(context, intent);
-    }
-
-    /**
-     * Add an observer. If there is already an event for this id, call back inside of this call.
-     *
-     * @param context  A context of the current app
-     * @param id       The id the observer is for or {@code GENERATE_NEW_ID} to generate a new one.
-     * @param observer The observer to call back.
-     *
-     * @return The id for this event
-     */
-    public static int addObserver(@NonNull Context context, int id,
-            @NonNull EventResultPersister.EventResultObserver observer)
-            throws EventResultPersister.OutOfIdsException {
-        return getReceiver(context).addObserver(id, observer);
-    }
-
-    /**
-     * Remove a observer.
-     *
-     * @param context  A context of the current app
-     * @param id The id the observer was added for
-     */
-    static void removeObserver(@NonNull Context context, int id) {
-        getReceiver(context).removeObserver(id);
-    }
-
-    /**
-     * @param context A context of the current app
-     *
-     * @return A new uninstall id
-     */
-    static int getNewId(@NonNull Context context) throws EventResultPersister.OutOfIdsException {
-        return getReceiver(context).getNewId();
-    }
-}
diff --git a/packages/PackageInstaller/src/com/android/packageinstaller/UninstallUninstalling.java b/packages/PackageInstaller/src/com/android/packageinstaller/UninstallUninstalling.java
index 09be768..a60e015 100644
--- a/packages/PackageInstaller/src/com/android/packageinstaller/UninstallUninstalling.java
+++ b/packages/PackageInstaller/src/com/android/packageinstaller/UninstallUninstalling.java
@@ -34,8 +34,9 @@
 import android.os.UserManager;
 import android.util.Log;
 import android.widget.Toast;
-
 import androidx.annotation.Nullable;
+import com.android.packageinstaller.common.EventResultPersister;
+import com.android.packageinstaller.common.UninstallEventReceiver;
 
 /**
  * Start an uninstallation, show a dialog while uninstalling and return result to the caller.
diff --git a/packages/PackageInstaller/src/com/android/packageinstaller/UninstallerActivity.java b/packages/PackageInstaller/src/com/android/packageinstaller/UninstallerActivity.java
index 5c9b728..170cb45 100644
--- a/packages/PackageInstaller/src/com/android/packageinstaller/UninstallerActivity.java
+++ b/packages/PackageInstaller/src/com/android/packageinstaller/UninstallerActivity.java
@@ -47,15 +47,15 @@
 import android.os.UserHandle;
 import android.os.UserManager;
 import android.util.Log;
-
 import androidx.annotation.NonNull;
 import androidx.annotation.StringRes;
-
 import com.android.packageinstaller.handheld.ErrorDialogFragment;
 import com.android.packageinstaller.handheld.UninstallAlertDialogFragment;
 import com.android.packageinstaller.television.ErrorFragment;
 import com.android.packageinstaller.television.UninstallAlertFragment;
 import com.android.packageinstaller.television.UninstallAppProgress;
+import com.android.packageinstaller.common.EventResultPersister;
+import com.android.packageinstaller.common.UninstallEventReceiver;
 import com.android.packageinstaller.v2.ui.UninstallLaunch;
 
 import java.util.List;
@@ -94,15 +94,6 @@
         if (usePiaV2() && !isTv()) {
             Log.i(TAG, "Using Pia V2");
 
-            PackageManager pm = getPackageManager();
-            pm.setComponentEnabledSetting(
-                new ComponentName(this, com.android.packageinstaller.UninstallEventReceiver.class),
-                PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
-            pm.setComponentEnabledSetting(
-                new ComponentName(this,
-                    com.android.packageinstaller.v2.model.UninstallEventReceiver.class),
-                PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0);
-
             boolean returnResult = getIntent().getBooleanExtra(Intent.EXTRA_RETURN_RESULT, false);
             Intent piaV2 = new Intent(getIntent());
             piaV2.putExtra(UninstallLaunch.EXTRA_CALLING_PKG_UID, getLaunchedFromUid());
diff --git a/packages/PackageInstaller/src/com/android/packageinstaller/EventResultPersister.java b/packages/PackageInstaller/src/com/android/packageinstaller/common/EventResultPersister.java
similarity index 98%
rename from packages/PackageInstaller/src/com/android/packageinstaller/EventResultPersister.java
rename to packages/PackageInstaller/src/com/android/packageinstaller/common/EventResultPersister.java
index 0d1475a..8b40e61 100644
--- a/packages/PackageInstaller/src/com/android/packageinstaller/EventResultPersister.java
+++ b/packages/PackageInstaller/src/com/android/packageinstaller/common/EventResultPersister.java
@@ -1,11 +1,11 @@
 /*
- * Copyright (C) 2016 The Android Open Source Project
+ * Copyright (C) 2023 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
+ *      https://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,
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package com.android.packageinstaller;
+package com.android.packageinstaller.common;
 
 import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
 
diff --git a/packages/PackageInstaller/src/com/android/packageinstaller/InstallEventReceiver.java b/packages/PackageInstaller/src/com/android/packageinstaller/common/InstallEventReceiver.java
similarity index 88%
rename from packages/PackageInstaller/src/com/android/packageinstaller/InstallEventReceiver.java
rename to packages/PackageInstaller/src/com/android/packageinstaller/common/InstallEventReceiver.java
index be8eabb..ac0dbf7 100644
--- a/packages/PackageInstaller/src/com/android/packageinstaller/InstallEventReceiver.java
+++ b/packages/PackageInstaller/src/com/android/packageinstaller/common/InstallEventReceiver.java
@@ -1,11 +1,11 @@
 /*
- * Copyright (C) 2016 The Android Open Source Project
+ * Copyright (C) 2023 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
+ *      https://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,
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package com.android.packageinstaller;
+package com.android.packageinstaller.common;
 
 import android.content.BroadcastReceiver;
 import android.content.Context;
@@ -59,7 +59,7 @@
      *
      * @return The id for this event
      */
-    static int addObserver(@NonNull Context context, int id,
+    public static int addObserver(@NonNull Context context, int id,
             @NonNull EventResultPersister.EventResultObserver observer)
             throws EventResultPersister.OutOfIdsException {
         return getReceiver(context).addObserver(id, observer);
@@ -71,7 +71,7 @@
      * @param context  A context of the current app
      * @param id The id the observer was added for
      */
-    static void removeObserver(@NonNull Context context, int id) {
+    public static void removeObserver(@NonNull Context context, int id) {
         getReceiver(context).removeObserver(id);
     }
 }
diff --git a/packages/PackageInstaller/src/com/android/packageinstaller/v2/model/TemporaryFileManager.java b/packages/PackageInstaller/src/com/android/packageinstaller/common/TemporaryFileManager.java
similarity index 94%
rename from packages/PackageInstaller/src/com/android/packageinstaller/v2/model/TemporaryFileManager.java
rename to packages/PackageInstaller/src/com/android/packageinstaller/common/TemporaryFileManager.java
index 3a1c3973..1556793 100644
--- a/packages/PackageInstaller/src/com/android/packageinstaller/v2/model/TemporaryFileManager.java
+++ b/packages/PackageInstaller/src/com/android/packageinstaller/common/TemporaryFileManager.java
@@ -5,7 +5,7 @@
  * 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
+ *      https://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,
@@ -14,14 +14,16 @@
  * limitations under the License.
  */
 
-package com.android.packageinstaller.v2.model;
+package com.android.packageinstaller.common;
 
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.os.SystemClock;
 import android.util.Log;
+
 import androidx.annotation.NonNull;
+
 import java.io.File;
 import java.io.IOException;
 
@@ -29,13 +31,13 @@
  * Manages files of the package installer and resets state during boot.
  */
 public class TemporaryFileManager extends BroadcastReceiver {
-
     private static final String LOG_TAG = TemporaryFileManager.class.getSimpleName();
 
     /**
      * Create a new file to hold a staged file.
      *
      * @param context The context of the caller
+     *
      * @return A new file
      */
     @NonNull
@@ -47,6 +49,7 @@
      * Get the file used to store the results of installs.
      *
      * @param context The context of the caller
+     *
      * @return the file used to store the results of installs
      */
     @NonNull
@@ -58,6 +61,7 @@
      * Get the file used to store the results of uninstalls.
      *
      * @param context The context of the caller
+     *
      * @return the file used to store the results of uninstalls
      */
     @NonNull
@@ -85,7 +89,7 @@
                 }
             } else {
                 Log.w(LOG_TAG, fileOnBoot.getName() + " was created before onBoot broadcast was "
-                    + "received");
+                        + "received");
             }
         }
     }
diff --git a/packages/PackageInstaller/src/com/android/packageinstaller/v2/model/UninstallEventReceiver.java b/packages/PackageInstaller/src/com/android/packageinstaller/common/UninstallEventReceiver.java
similarity index 91%
rename from packages/PackageInstaller/src/com/android/packageinstaller/v2/model/UninstallEventReceiver.java
rename to packages/PackageInstaller/src/com/android/packageinstaller/common/UninstallEventReceiver.java
index 79e00df..cef3ba4 100644
--- a/packages/PackageInstaller/src/com/android/packageinstaller/v2/model/UninstallEventReceiver.java
+++ b/packages/PackageInstaller/src/com/android/packageinstaller/common/UninstallEventReceiver.java
@@ -14,11 +14,12 @@
  * limitations under the License.
  */
 
-package com.android.packageinstaller.v2.model;
+package com.android.packageinstaller.common;
 
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
+
 import androidx.annotation.NonNull;
 
 /**
@@ -70,7 +71,7 @@
      * @param context  A context of the current app
      * @param id The id the observer was added for
      */
-    static void removeObserver(@NonNull Context context, int id) {
+    public static void removeObserver(@NonNull Context context, int id) {
         getReceiver(context).removeObserver(id);
     }
 
@@ -79,7 +80,8 @@
      *
      * @return A new uninstall id
      */
-    static int getNewId(@NonNull Context context) throws EventResultPersister.OutOfIdsException {
+    public static int getNewId(@NonNull Context context)
+        throws EventResultPersister.OutOfIdsException {
         return getReceiver(context).getNewId();
     }
 }
diff --git a/packages/PackageInstaller/src/com/android/packageinstaller/television/UninstallAppProgress.java b/packages/PackageInstaller/src/com/android/packageinstaller/television/UninstallAppProgress.java
old mode 100755
new mode 100644
index 0c59d44..60964b9
--- a/packages/PackageInstaller/src/com/android/packageinstaller/television/UninstallAppProgress.java
+++ b/packages/PackageInstaller/src/com/android/packageinstaller/television/UninstallAppProgress.java
@@ -37,14 +37,11 @@
 import android.util.TypedValue;
 import android.view.KeyEvent;
 import android.widget.Toast;
-
 import androidx.annotation.Nullable;
-
-import com.android.packageinstaller.EventResultPersister;
 import com.android.packageinstaller.PackageUtil;
 import com.android.packageinstaller.R;
-import com.android.packageinstaller.UninstallEventReceiver;
-
+import com.android.packageinstaller.common.EventResultPersister;
+import com.android.packageinstaller.common.UninstallEventReceiver;
 import java.lang.ref.WeakReference;
 import java.util.List;
 
diff --git a/packages/PackageInstaller/src/com/android/packageinstaller/v2/model/EventResultPersister.java b/packages/PackageInstaller/src/com/android/packageinstaller/v2/model/EventResultPersister.java
deleted file mode 100644
index 4d2d911..0000000
--- a/packages/PackageInstaller/src/com/android/packageinstaller/v2/model/EventResultPersister.java
+++ /dev/null
@@ -1,378 +0,0 @@
-/*
- * Copyright (C) 2023 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.packageinstaller.v2.model;
-
-import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
-
-import android.content.Context;
-import android.content.Intent;
-import android.content.pm.PackageInstaller;
-import android.os.AsyncTask;
-import android.util.AtomicFile;
-import android.util.Log;
-import android.util.SparseArray;
-import android.util.Xml;
-import androidx.annotation.NonNull;
-import androidx.annotation.Nullable;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.nio.charset.StandardCharsets;
-import org.xmlpull.v1.XmlPullParser;
-import org.xmlpull.v1.XmlPullParserException;
-import org.xmlpull.v1.XmlSerializer;
-
-/**
- * Persists results of events and calls back observers when a matching result arrives.
- */
-public class EventResultPersister {
-
-    /**
-     * Id passed to {@link #addObserver(int, EventResultObserver)} to generate new id
-     */
-    public static final int GENERATE_NEW_ID = Integer.MIN_VALUE;
-    /**
-     * The extra with the id to set in the intent delivered to
-     * {@link #onEventReceived(Context, Intent)}
-     */
-    public static final String EXTRA_ID = "EventResultPersister.EXTRA_ID";
-    public static final String EXTRA_SERVICE_ID = "EventResultPersister.EXTRA_SERVICE_ID";
-    private static final String TAG = EventResultPersister.class.getSimpleName();
-    /**
-     * Persisted state of this object
-     */
-    private final AtomicFile mResultsFile;
-
-    private final Object mLock = new Object();
-
-    /**
-     * Currently stored but not yet called back results (install id -> status, status message)
-     */
-    private final SparseArray<EventResult> mResults = new SparseArray<>();
-
-    /**
-     * Currently registered, not called back observers (install id -> observer)
-     */
-    private final SparseArray<EventResultObserver> mObservers = new SparseArray<>();
-
-    /**
-     * Always increasing counter for install event ids
-     */
-    private int mCounter;
-
-    /**
-     * If a write that will persist the state is scheduled
-     */
-    private boolean mIsPersistScheduled;
-
-    /**
-     * If the state was changed while the data was being persisted
-     */
-    private boolean mIsPersistingStateValid;
-
-    /**
-     * Read persisted state.
-     *
-     * @param resultFile The file the results are persisted in
-     */
-    EventResultPersister(@NonNull File resultFile) {
-        mResultsFile = new AtomicFile(resultFile);
-        mCounter = GENERATE_NEW_ID + 1;
-
-        try (FileInputStream stream = mResultsFile.openRead()) {
-            XmlPullParser parser = Xml.newPullParser();
-            parser.setInput(stream, StandardCharsets.UTF_8.name());
-
-            nextElement(parser);
-            while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
-                String tagName = parser.getName();
-                if ("results".equals(tagName)) {
-                    mCounter = readIntAttribute(parser, "counter");
-                } else if ("result".equals(tagName)) {
-                    int id = readIntAttribute(parser, "id");
-                    int status = readIntAttribute(parser, "status");
-                    int legacyStatus = readIntAttribute(parser, "legacyStatus");
-                    String statusMessage = readStringAttribute(parser, "statusMessage");
-                    int serviceId = readIntAttribute(parser, "serviceId");
-
-                    if (mResults.get(id) != null) {
-                        throw new Exception("id " + id + " has two results");
-                    }
-
-                    mResults.put(id, new EventResult(status, legacyStatus, statusMessage,
-                        serviceId));
-                } else {
-                    throw new Exception("unexpected tag");
-                }
-
-                nextElement(parser);
-            }
-        } catch (Exception e) {
-            mResults.clear();
-            writeState();
-        }
-    }
-
-    /**
-     * Progress parser to the next element.
-     *
-     * @param parser The parser to progress
-     */
-    private static void nextElement(@NonNull XmlPullParser parser)
-        throws XmlPullParserException, IOException {
-        int type;
-        do {
-            type = parser.next();
-        } while (type != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT);
-    }
-
-    /**
-     * Read an int attribute from the current element
-     *
-     * @param parser The parser to read from
-     * @param name The attribute name to read
-     * @return The value of the attribute
-     */
-    private static int readIntAttribute(@NonNull XmlPullParser parser, @NonNull String name) {
-        return Integer.parseInt(parser.getAttributeValue(null, name));
-    }
-
-    /**
-     * Read an String attribute from the current element
-     *
-     * @param parser The parser to read from
-     * @param name The attribute name to read
-     * @return The value of the attribute or null if the attribute is not set
-     */
-    private static String readStringAttribute(@NonNull XmlPullParser parser, @NonNull String name) {
-        return parser.getAttributeValue(null, name);
-    }
-
-    /**
-     * @return a new event id.
-     */
-    public int getNewId() throws OutOfIdsException {
-        synchronized (mLock) {
-            if (mCounter == Integer.MAX_VALUE) {
-                throw new OutOfIdsException();
-            }
-
-            mCounter++;
-            writeState();
-
-            return mCounter - 1;
-        }
-    }
-
-    /**
-     * Add a result. If the result is a pending user action, execute the pending user action
-     * directly and do not queue a result.
-     *
-     * @param context The context the event was received in
-     * @param intent The intent the activity received
-     */
-    void onEventReceived(@NonNull Context context, @NonNull Intent intent) {
-        int status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS, 0);
-
-        if (status == PackageInstaller.STATUS_PENDING_USER_ACTION) {
-            Intent intentToStart = intent.getParcelableExtra(Intent.EXTRA_INTENT, Intent.class);
-            intentToStart.addFlags(FLAG_ACTIVITY_NEW_TASK);
-            context.startActivity(intentToStart);
-
-            return;
-        }
-
-        int id = intent.getIntExtra(EXTRA_ID, 0);
-        String statusMessage = intent.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE);
-        int legacyStatus = intent.getIntExtra(PackageInstaller.EXTRA_LEGACY_STATUS, 0);
-        int serviceId = intent.getIntExtra(EXTRA_SERVICE_ID, 0);
-
-        EventResultObserver observerToCall = null;
-        synchronized (mLock) {
-            int numObservers = mObservers.size();
-            for (int i = 0; i < numObservers; i++) {
-                if (mObservers.keyAt(i) == id) {
-                    observerToCall = mObservers.valueAt(i);
-                    mObservers.removeAt(i);
-
-                    break;
-                }
-            }
-
-            if (observerToCall != null) {
-                observerToCall.onResult(status, legacyStatus, statusMessage, serviceId);
-            } else {
-                mResults.put(id, new EventResult(status, legacyStatus, statusMessage, serviceId));
-                writeState();
-            }
-        }
-    }
-
-    /**
-     * Persist current state. The persistence might be delayed.
-     */
-    private void writeState() {
-        synchronized (mLock) {
-            mIsPersistingStateValid = false;
-
-            if (!mIsPersistScheduled) {
-                mIsPersistScheduled = true;
-
-                AsyncTask.execute(() -> {
-                    int counter;
-                    SparseArray<EventResult> results;
-
-                    while (true) {
-                        // Take snapshot of state
-                        synchronized (mLock) {
-                            counter = mCounter;
-                            results = mResults.clone();
-                            mIsPersistingStateValid = true;
-                        }
-
-                        try (FileOutputStream stream = mResultsFile.startWrite()) {
-                            try {
-                                XmlSerializer serializer = Xml.newSerializer();
-                                serializer.setOutput(stream, StandardCharsets.UTF_8.name());
-                                serializer.startDocument(null, true);
-                                serializer.setFeature(
-                                    "http://xmlpull.org/v1/doc/features.html#indent-output", true);
-                                serializer.startTag(null, "results");
-                                serializer.attribute(null, "counter", Integer.toString(counter));
-
-                                int numResults = results.size();
-                                for (int i = 0; i < numResults; i++) {
-                                    serializer.startTag(null, "result");
-                                    serializer.attribute(null, "id",
-                                        Integer.toString(results.keyAt(i)));
-                                    serializer.attribute(null, "status",
-                                        Integer.toString(results.valueAt(i).status));
-                                    serializer.attribute(null, "legacyStatus",
-                                        Integer.toString(results.valueAt(i).legacyStatus));
-                                    if (results.valueAt(i).message != null) {
-                                        serializer.attribute(null, "statusMessage",
-                                            results.valueAt(i).message);
-                                    }
-                                    serializer.attribute(null, "serviceId",
-                                        Integer.toString(results.valueAt(i).serviceId));
-                                    serializer.endTag(null, "result");
-                                }
-
-                                serializer.endTag(null, "results");
-                                serializer.endDocument();
-
-                                mResultsFile.finishWrite(stream);
-                            } catch (IOException e) {
-                                Log.e(TAG, "error writing results", e);
-                                mResultsFile.failWrite(stream);
-                                mResultsFile.delete();
-                            }
-                        } catch (IOException e) {
-                            Log.e(TAG, "error writing results", e);
-                            mResultsFile.delete();
-                        }
-
-                        // Check if there was changed state since we persisted. If so, we need to
-                        // persist again.
-                        synchronized (mLock) {
-                            if (mIsPersistingStateValid) {
-                                mIsPersistScheduled = false;
-                                break;
-                            }
-                        }
-                    }
-                });
-            }
-        }
-    }
-
-    /**
-     * Add an observer. If there is already an event for this id, call back inside of this call.
-     *
-     * @param id The id the observer is for or {@code GENERATE_NEW_ID} to generate a new one.
-     * @param observer The observer to call back.
-     * @return The id for this event
-     */
-    int addObserver(int id, @NonNull EventResultObserver observer)
-        throws OutOfIdsException {
-        synchronized (mLock) {
-            int resultIndex = -1;
-
-            if (id == GENERATE_NEW_ID) {
-                id = getNewId();
-            } else {
-                resultIndex = mResults.indexOfKey(id);
-            }
-
-            // Check if we can instantly call back
-            if (resultIndex >= 0) {
-                EventResult result = mResults.valueAt(resultIndex);
-
-                observer.onResult(result.status, result.legacyStatus, result.message,
-                    result.serviceId);
-                mResults.removeAt(resultIndex);
-                writeState();
-            } else {
-                mObservers.put(id, observer);
-            }
-        }
-
-        return id;
-    }
-
-    /**
-     * Remove a observer.
-     *
-     * @param id The id the observer was added for
-     */
-    void removeObserver(int id) {
-        synchronized (mLock) {
-            mObservers.delete(id);
-        }
-    }
-
-    /**
-     * Call back when a result is received. Observer is removed when onResult it called.
-     */
-    public interface EventResultObserver {
-
-        void onResult(int status, int legacyStatus, @Nullable String message, int serviceId);
-    }
-
-    /**
-     * The status from an event.
-     */
-    private static class EventResult {
-
-        public final int status;
-        public final int legacyStatus;
-        @Nullable
-        public final String message;
-        public final int serviceId;
-
-        private EventResult(int status, int legacyStatus, @Nullable String message, int serviceId) {
-            this.status = status;
-            this.legacyStatus = legacyStatus;
-            this.message = message;
-            this.serviceId = serviceId;
-        }
-    }
-
-    public static class OutOfIdsException extends Exception {
-    }
-}
diff --git a/packages/PackageInstaller/src/com/android/packageinstaller/v2/model/InstallEventReceiver.java b/packages/PackageInstaller/src/com/android/packageinstaller/v2/model/InstallEventReceiver.java
deleted file mode 100644
index bcb11c8..0000000
--- a/packages/PackageInstaller/src/com/android/packageinstaller/v2/model/InstallEventReceiver.java
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- * Copyright (C) 2023 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.packageinstaller.v2.model;
-
-import android.content.BroadcastReceiver;
-import android.content.Context;
-import android.content.Intent;
-import androidx.annotation.NonNull;
-
-/**
- * Receives install events and perists them using a {@link EventResultPersister}.
- */
-public class InstallEventReceiver extends BroadcastReceiver {
-
-    private static final Object sLock = new Object();
-    private static EventResultPersister sReceiver;
-
-    /**
-     * Get the event receiver persisting the results
-     *
-     * @return The event receiver.
-     */
-    @NonNull
-    private static EventResultPersister getReceiver(@NonNull Context context) {
-        synchronized (sLock) {
-            if (sReceiver == null) {
-                sReceiver = new EventResultPersister(
-                    TemporaryFileManager.getInstallStateFile(context));
-            }
-        }
-
-        return sReceiver;
-    }
-
-    /**
-     * Add an observer. If there is already an event for this id, call back inside of this call.
-     *
-     * @param context A context of the current app
-     * @param id The id the observer is for or {@code GENERATE_NEW_ID} to generate a new one.
-     * @param observer The observer to call back.
-     * @return The id for this event
-     */
-    static int addObserver(@NonNull Context context, int id,
-        @NonNull EventResultPersister.EventResultObserver observer)
-        throws EventResultPersister.OutOfIdsException {
-        return getReceiver(context).addObserver(id, observer);
-    }
-
-    /**
-     * Remove a observer.
-     *
-     * @param context A context of the current app
-     * @param id The id the observer was added for
-     */
-    static void removeObserver(@NonNull Context context, int id) {
-        getReceiver(context).removeObserver(id);
-    }
-
-    @Override
-    public void onReceive(Context context, Intent intent) {
-        getReceiver(context).onEventReceived(context, intent);
-    }
-}
diff --git a/packages/PackageInstaller/src/com/android/packageinstaller/v2/model/InstallRepository.java b/packages/PackageInstaller/src/com/android/packageinstaller/v2/model/InstallRepository.java
index 203af44..c8175ad 100644
--- a/packages/PackageInstaller/src/com/android/packageinstaller/v2/model/InstallRepository.java
+++ b/packages/PackageInstaller/src/com/android/packageinstaller/v2/model/InstallRepository.java
@@ -61,7 +61,8 @@
 import androidx.annotation.Nullable;
 import androidx.lifecycle.MutableLiveData;
 import com.android.packageinstaller.R;
-import com.android.packageinstaller.v2.model.EventResultPersister.OutOfIdsException;
+import com.android.packageinstaller.common.EventResultPersister;
+import com.android.packageinstaller.common.InstallEventReceiver;
 import com.android.packageinstaller.v2.model.PackageUtil.AppSnippet;
 import com.android.packageinstaller.v2.model.installstagedata.InstallAborted;
 import com.android.packageinstaller.v2.model.installstagedata.InstallFailed;
@@ -773,7 +774,7 @@
             mInstallResult.setValue(new InstallInstalling(mAppSnippet));
             installId = InstallEventReceiver.addObserver(mContext,
                 EventResultPersister.GENERATE_NEW_ID, this::setStageBasedOnResult);
-        } catch (OutOfIdsException e) {
+        } catch (EventResultPersister.OutOfIdsException e) {
             setStageBasedOnResult(PackageInstaller.STATUS_FAILURE,
                 PackageManager.INSTALL_FAILED_INTERNAL_ERROR, null, -1);
             return;
diff --git a/packages/PackageInstaller/src/com/android/packageinstaller/v2/model/UninstallRepository.java b/packages/PackageInstaller/src/com/android/packageinstaller/v2/model/UninstallRepository.java
index 2e43b75..a07c532 100644
--- a/packages/PackageInstaller/src/com/android/packageinstaller/v2/model/UninstallRepository.java
+++ b/packages/PackageInstaller/src/com/android/packageinstaller/v2/model/UninstallRepository.java
@@ -60,6 +60,8 @@
 import androidx.annotation.Nullable;
 import androidx.lifecycle.MutableLiveData;
 import com.android.packageinstaller.R;
+import com.android.packageinstaller.common.EventResultPersister;
+import com.android.packageinstaller.common.UninstallEventReceiver;
 import com.android.packageinstaller.v2.model.uninstallstagedata.UninstallAborted;
 import com.android.packageinstaller.v2.model.uninstallstagedata.UninstallFailed;
 import com.android.packageinstaller.v2.model.uninstallstagedata.UninstallReady;
diff --git a/packages/PackageInstaller/src/com/android/packageinstaller/wear/WearPackageInstallerService.java b/packages/PackageInstaller/src/com/android/packageinstaller/wear/WearPackageInstallerService.java
index 959257f..ae0f4ec 100644
--- a/packages/PackageInstaller/src/com/android/packageinstaller/wear/WearPackageInstallerService.java
+++ b/packages/PackageInstaller/src/com/android/packageinstaller/wear/WearPackageInstallerService.java
@@ -44,15 +44,12 @@
 import android.util.ArrayMap;
 import android.util.Log;
 import android.util.Pair;
-
 import androidx.annotation.Nullable;
-
 import com.android.packageinstaller.DeviceUtils;
-import com.android.packageinstaller.EventResultPersister;
 import com.android.packageinstaller.PackageUtil;
 import com.android.packageinstaller.R;
-import com.android.packageinstaller.UninstallEventReceiver;
-
+import com.android.packageinstaller.common.EventResultPersister;
+import com.android.packageinstaller.common.UninstallEventReceiver;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.util.Arrays;
diff --git a/packages/SettingsLib/Spa/TEST_MAPPING b/packages/SettingsLib/Spa/TEST_MAPPING
index b7ce518..be1e888 100644
--- a/packages/SettingsLib/Spa/TEST_MAPPING
+++ b/packages/SettingsLib/Spa/TEST_MAPPING
@@ -9,5 +9,10 @@
     {
       "name": "SettingsSpaUnitTests"
     }
+  ],
+  "postsubmit": [
+    {
+      "name": "SpaScreenshotTests"
+    }
   ]
 }
diff --git a/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_actionButtons.png b/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_actionButtons.png
index 74113d8..1a59722 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_actionButtons.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_actionButtons.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_barChart.png b/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_barChart.png
index 0d22c6a..69017d0 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_barChart.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_barChart.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_footer.png b/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_footer.png
index f77b8a7..dc2e594 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_footer.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_footer.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_imageIllustration.png b/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_imageIllustration.png
index 9372791..7e1de6a 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_imageIllustration.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_imageIllustration.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_lineChart.png b/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_lineChart.png
index dda9e9e..2218b5b 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_lineChart.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_lineChart.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_mainSwitchPreference.png b/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_mainSwitchPreference.png
index bf19a2c..e18f42b 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_mainSwitchPreference.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_mainSwitchPreference.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_pieChart.png b/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_pieChart.png
index b14e196e..21291a7 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_pieChart.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_pieChart.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_preference.png b/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_preference.png
index c77f9b1..5c1a0e24 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_preference.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_preference.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_progressBar.png b/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_progressBar.png
index f513830..4003506 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_progressBar.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_progressBar.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_slider.png b/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_slider.png
index dda6f0a..1c669a6 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_slider.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_slider.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_spinner.png b/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_spinner.png
index 7468169..101eb0c 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_spinner.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_spinner.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_switchPreference.png b/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_switchPreference.png
index 669f443..9cb16ef 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_switchPreference.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_switchPreference.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_twoTargetSwitchPreference.png b/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_twoTargetSwitchPreference.png
index 8e37cc0..20b11e3 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_twoTargetSwitchPreference.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/phone/light_landscape_twoTargetSwitchPreference.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_actionButtons.png b/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_actionButtons.png
index b0543e0..c2ad8ad 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_actionButtons.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_actionButtons.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_barChart.png b/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_barChart.png
index 3755928..3408cca 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_barChart.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_barChart.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_footer.png b/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_footer.png
index f77b8a7..dc2e594 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_footer.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_footer.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_imageIllustration.png b/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_imageIllustration.png
index 7800149..c3ca8cd 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_imageIllustration.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_imageIllustration.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_lineChart.png b/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_lineChart.png
index be40959..53b92dd 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_lineChart.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_lineChart.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_mainSwitchPreference.png b/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_mainSwitchPreference.png
index 2d5894e..caa26a3 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_mainSwitchPreference.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_mainSwitchPreference.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_pieChart.png b/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_pieChart.png
index 2a54b9e..02a943d 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_pieChart.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_pieChart.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_preference.png b/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_preference.png
index b0fc305..c1abbc2 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_preference.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_preference.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_progressBar.png b/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_progressBar.png
index 73f2407..cdb62f9 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_progressBar.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_progressBar.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_slider.png b/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_slider.png
index 213c0b2..b3ae6b3 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_slider.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_slider.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_spinner.png b/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_spinner.png
index 7468169..101eb0c 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_spinner.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_spinner.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_switchPreference.png b/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_switchPreference.png
index ce7ab78..4227bb7 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_switchPreference.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_switchPreference.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_twoTargetSwitchPreference.png b/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_twoTargetSwitchPreference.png
index 9d92f7a..52c4341 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_twoTargetSwitchPreference.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/phone/light_portrait_twoTargetSwitchPreference.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_actionButtons.png b/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_actionButtons.png
index 5255d14..eebac76 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_actionButtons.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_actionButtons.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_barChart.png b/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_barChart.png
index 8f3f664..477d16c 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_barChart.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_barChart.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_footer.png b/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_footer.png
index cc1de55..0dc83f9 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_footer.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_footer.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_imageIllustration.png b/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_imageIllustration.png
index e29f26a..f32d7421 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_imageIllustration.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_imageIllustration.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_lineChart.png b/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_lineChart.png
index 8b9bc5c..1de3b7f 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_lineChart.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_lineChart.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_mainSwitchPreference.png b/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_mainSwitchPreference.png
index b1676b3..5e5ed6a 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_mainSwitchPreference.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_mainSwitchPreference.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_pieChart.png b/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_pieChart.png
index 2a7b341..c133a6e 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_pieChart.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_pieChart.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_preference.png b/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_preference.png
index 4845ea8..55acf95 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_preference.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_preference.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_progressBar.png b/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_progressBar.png
index 6e860d3..f72dbc2 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_progressBar.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_progressBar.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_slider.png b/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_slider.png
index d1abaa6..5a1c85d 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_slider.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_slider.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_spinner.png b/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_spinner.png
index c211e0e..bc8144e 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_spinner.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_spinner.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_switchPreference.png b/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_switchPreference.png
index 9c57e33..2b0351a 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_switchPreference.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_switchPreference.png
Binary files differ
diff --git a/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_twoTargetSwitchPreference.png b/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_twoTargetSwitchPreference.png
index d416a0b..bfcdb55 100644
--- a/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_twoTargetSwitchPreference.png
+++ b/packages/SettingsLib/Spa/screenshot/assets/tablet/dark_portrait_twoTargetSwitchPreference.png
Binary files differ
diff --git a/packages/SettingsLib/SpaPrivileged/src/com/android/settingslib/spaprivileged/framework/common/BroadcastReceiverAsUserFlow.kt b/packages/SettingsLib/SpaPrivileged/src/com/android/settingslib/spaprivileged/framework/common/BroadcastReceiverAsUserFlow.kt
index 2c60db4..8c52d57 100644
--- a/packages/SettingsLib/SpaPrivileged/src/com/android/settingslib/spaprivileged/framework/common/BroadcastReceiverAsUserFlow.kt
+++ b/packages/SettingsLib/SpaPrivileged/src/com/android/settingslib/spaprivileged/framework/common/BroadcastReceiverAsUserFlow.kt
@@ -21,13 +21,17 @@
 import android.content.Intent
 import android.content.IntentFilter
 import android.os.UserHandle
+import android.util.Log
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.channels.awaitClose
 import kotlinx.coroutines.flow.Flow
 import kotlinx.coroutines.flow.callbackFlow
+import kotlinx.coroutines.flow.catch
 import kotlinx.coroutines.flow.conflate
 import kotlinx.coroutines.flow.flowOn
 
+private const val TAG = "BroadcastReceiverAsUser"
+
 /**
  * A [BroadcastReceiver] flow for the given [intentFilter].
  */
@@ -50,4 +54,6 @@
     )
 
     awaitClose { unregisterReceiver(broadcastReceiver) }
+}.catch { e ->
+    Log.e(TAG, "Error while broadcastReceiverAsUserFlow", e)
 }.conflate().flowOn(Dispatchers.Default)
diff --git a/packages/SettingsLib/SpaPrivileged/tests/src/com/android/settingslib/spaprivileged/framework/common/BroadcastReceiverAsUserFlowTest.kt b/packages/SettingsLib/SpaPrivileged/tests/src/com/android/settingslib/spaprivileged/framework/common/BroadcastReceiverAsUserFlowTest.kt
index dfb8e22..9cb33d2 100644
--- a/packages/SettingsLib/SpaPrivileged/tests/src/com/android/settingslib/spaprivileged/framework/common/BroadcastReceiverAsUserFlowTest.kt
+++ b/packages/SettingsLib/SpaPrivileged/tests/src/com/android/settingslib/spaprivileged/framework/common/BroadcastReceiverAsUserFlowTest.kt
@@ -32,9 +32,11 @@
 import org.junit.runner.RunWith
 import org.mockito.kotlin.any
 import org.mockito.kotlin.doAnswer
+import org.mockito.kotlin.doThrow
 import org.mockito.kotlin.eq
 import org.mockito.kotlin.isNull
 import org.mockito.kotlin.mock
+import org.mockito.kotlin.stub
 
 @RunWith(AndroidJUnit4::class)
 class BroadcastReceiverAsUserFlowTest {
@@ -83,6 +85,18 @@
         assertThat(onReceiveIsCalled).isTrue()
     }
 
+    @Test
+    fun broadcastReceiverAsUserFlow_unregisterReceiverThrowException_noCrash() = runBlocking {
+        context.stub {
+            on { unregisterReceiver(any()) } doThrow IllegalArgumentException()
+        }
+        val flow = context.broadcastReceiverAsUserFlow(INTENT_FILTER, USER_HANDLE)
+
+        flow.firstWithTimeoutOrNull()
+
+        assertThat(registeredBroadcastReceiver).isNotNull()
+    }
+
     private companion object {
         val USER_HANDLE: UserHandle = UserHandle.of(0)
 
diff --git a/packages/Shell/AndroidManifest.xml b/packages/Shell/AndroidManifest.xml
index b6a0c7b..d12d9d6 100644
--- a/packages/Shell/AndroidManifest.xml
+++ b/packages/Shell/AndroidManifest.xml
@@ -562,6 +562,9 @@
     <!-- Permissions required for CTS test - NotificationManagerTest -->
     <uses-permission android:name="android.permission.MANAGE_NOTIFICATION_LISTENERS" />
 
+    <!-- Permissions required for CTS test - NotificationManagerZenTest -->
+    <uses-permission android:name="android.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS" />
+
     <!-- Permissions required for CTS test - CtsContactsProviderTestCases -->
     <uses-permission android:name="android.contacts.permission.MANAGE_SIM_ACCOUNTS" />
     <uses-permission android:name="android.permission.SET_DEFAULT_ACCOUNT_FOR_CONTACTS" />
diff --git a/packages/SystemUI/Android.bp b/packages/SystemUI/Android.bp
index 7cf562f..c2c5e00 100644
--- a/packages/SystemUI/Android.bp
+++ b/packages/SystemUI/Android.bp
@@ -190,6 +190,7 @@
         "androidx.room_room-runtime",
         "androidx.room_room-ktx",
         "com.google.android.material_material",
+        "device_state_flags_lib",
         "kotlinx_coroutines_android",
         "kotlinx_coroutines",
         "iconloader_base",
@@ -302,6 +303,7 @@
         "androidx.exifinterface_exifinterface",
         "androidx.room_room-runtime",
         "androidx.room_room-ktx",
+        "device_state_flags_lib",
         "kotlinx-coroutines-android",
         "kotlinx-coroutines-core",
         "kotlinx_coroutines_test",
diff --git a/packages/SystemUI/aconfig/systemui.aconfig b/packages/SystemUI/aconfig/systemui.aconfig
index d8d3f87..c52a89c 100644
--- a/packages/SystemUI/aconfig/systemui.aconfig
+++ b/packages/SystemUI/aconfig/systemui.aconfig
@@ -189,8 +189,23 @@
 }
 
 flag {
+   name: "migrate_clocks_to_blueprint"
+   namespace: "systemui"
+   description: "Move clock related views from KeyguardStatusView to KeyguardRootView, "
+        "and use modern architecture for lockscreen clocks"
+   bug: "301502635"
+}
+
+flag {
    name: "fast_unlock_transition"
    namespace: "systemui"
    description: "Faster wallpaper unlock transition"
    bug: "298186160"
 }
+
+flag {
+   name: "quick_settings_visual_haptics_longpress"
+   namespace: "systemui"
+   description: "Enable special visual and haptic effects for quick settings tiles with long-press actions"
+   bug: "229856884"
+}
diff --git a/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/NestedScrollToScene.kt b/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/NestedScrollToScene.kt
index 658b45f..2986504 100644
--- a/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/NestedScrollToScene.kt
+++ b/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/NestedScrollToScene.kt
@@ -17,11 +17,13 @@
 package com.android.compose.animation.scene
 
 import androidx.compose.foundation.gestures.Orientation
-import androidx.compose.runtime.DisposableEffect
-import androidx.compose.runtime.remember
 import androidx.compose.ui.Modifier
-import androidx.compose.ui.composed
-import androidx.compose.ui.input.nestedscroll.nestedScroll
+import androidx.compose.ui.input.nestedscroll.nestedScrollModifierNode
+import androidx.compose.ui.node.DelegatableNode
+import androidx.compose.ui.node.DelegatingNode
+import androidx.compose.ui.node.ModifierNodeElement
+import androidx.compose.ui.platform.InspectorInfo
+import com.android.compose.nestedscroll.PriorityNestedScrollConnection
 
 /**
  * Defines the behavior of the [SceneTransitionLayout] when a scrollable component is scrolled.
@@ -32,8 +34,9 @@
  */
 enum class NestedScrollBehavior(val canStartOnPostFling: Boolean) {
     /**
-     * During scene transitions, scroll events are consumed by the [SceneTransitionLayout] instead
-     * of the scrollable component.
+     * During scene transitions, if we are within
+     * [SceneTransitionLayoutImpl.transitionInterceptionThreshold], the [SceneTransitionLayout]
+     * consumes scroll events instead of the scrollable component.
      */
     DuringTransitionBetweenScenes(canStartOnPostFling = false),
 
@@ -72,21 +75,101 @@
     orientation: Orientation,
     startBehavior: NestedScrollBehavior,
     endBehavior: NestedScrollBehavior,
-): Modifier = composed {
-    val connection =
-        remember(layoutImpl, orientation, startBehavior, endBehavior) {
+) =
+    this then
+        NestedScrollToSceneElement(
+            layoutImpl = layoutImpl,
+            orientation = orientation,
+            startBehavior = startBehavior,
+            endBehavior = endBehavior,
+        )
+
+private data class NestedScrollToSceneElement(
+    private val layoutImpl: SceneTransitionLayoutImpl,
+    private val orientation: Orientation,
+    private val startBehavior: NestedScrollBehavior,
+    private val endBehavior: NestedScrollBehavior,
+) : ModifierNodeElement<NestedScrollToSceneNode>() {
+    override fun create() =
+        NestedScrollToSceneNode(
+            layoutImpl = layoutImpl,
+            orientation = orientation,
+            startBehavior = startBehavior,
+            endBehavior = endBehavior,
+        )
+
+    override fun update(node: NestedScrollToSceneNode) {
+        node.update(
+            layoutImpl = layoutImpl,
+            orientation = orientation,
+            startBehavior = startBehavior,
+            endBehavior = endBehavior,
+        )
+    }
+
+    override fun InspectorInfo.inspectableProperties() {
+        name = "nestedScrollToScene"
+        properties["layoutImpl"] = layoutImpl
+        properties["orientation"] = orientation
+        properties["startBehavior"] = startBehavior
+        properties["endBehavior"] = endBehavior
+    }
+}
+
+private class NestedScrollToSceneNode(
+    layoutImpl: SceneTransitionLayoutImpl,
+    orientation: Orientation,
+    startBehavior: NestedScrollBehavior,
+    endBehavior: NestedScrollBehavior,
+) : DelegatingNode() {
+    private var priorityNestedScrollConnection: PriorityNestedScrollConnection =
+        scenePriorityNestedScrollConnection(
+            layoutImpl = layoutImpl,
+            orientation = orientation,
+            startBehavior = startBehavior,
+            endBehavior = endBehavior,
+        )
+
+    private var nestedScrollNode: DelegatableNode =
+        nestedScrollModifierNode(
+            connection = priorityNestedScrollConnection,
+            dispatcher = null,
+        )
+
+    override fun onAttach() {
+        delegate(nestedScrollNode)
+    }
+
+    override fun onDetach() {
+        // Make sure we reset the scroll connection when this modifier is removed from composition
+        priorityNestedScrollConnection.reset()
+    }
+
+    fun update(
+        layoutImpl: SceneTransitionLayoutImpl,
+        orientation: Orientation,
+        startBehavior: NestedScrollBehavior,
+        endBehavior: NestedScrollBehavior,
+    ) {
+        // Clean up the old nested scroll connection
+        priorityNestedScrollConnection.reset()
+        undelegate(nestedScrollNode)
+
+        // Create a new nested scroll connection
+        priorityNestedScrollConnection =
             scenePriorityNestedScrollConnection(
                 layoutImpl = layoutImpl,
                 orientation = orientation,
                 startBehavior = startBehavior,
-                endBehavior = endBehavior
+                endBehavior = endBehavior,
             )
-        }
-
-    // Make sure we reset the scroll connection when this modifier is removed from composition
-    DisposableEffect(connection) { onDispose { connection.reset() } }
-
-    nestedScroll(connection = connection)
+        nestedScrollNode =
+            nestedScrollModifierNode(
+                connection = priorityNestedScrollConnection,
+                dispatcher = null,
+            )
+        delegate(nestedScrollNode)
+    }
 }
 
 private fun scenePriorityNestedScrollConnection(
diff --git a/packages/SystemUI/customization/src/com/android/systemui/shared/clocks/DefaultClockController.kt b/packages/SystemUI/customization/src/com/android/systemui/shared/clocks/DefaultClockController.kt
index 141e1c1..01c03b1 100644
--- a/packages/SystemUI/customization/src/com/android/systemui/shared/clocks/DefaultClockController.kt
+++ b/packages/SystemUI/customization/src/com/android/systemui/shared/clocks/DefaultClockController.kt
@@ -198,7 +198,6 @@
         }
 
         override fun recomputePadding(targetRegion: Rect?) {
-            // TODO(b/310989341): remove after changing migrate_clocks_to_blueprint to aconfig
             if (migratedClocks) {
                 return
             }
diff --git a/packages/SystemUI/multivalentTests/src/com/android/systemui/scene/SceneFrameworkIntegrationTest.kt b/packages/SystemUI/multivalentTests/src/com/android/systemui/scene/SceneFrameworkIntegrationTest.kt
index c110de9..70be031 100644
--- a/packages/SystemUI/multivalentTests/src/com/android/systemui/scene/SceneFrameworkIntegrationTest.kt
+++ b/packages/SystemUI/multivalentTests/src/com/android/systemui/scene/SceneFrameworkIntegrationTest.kt
@@ -63,6 +63,7 @@
 import com.android.systemui.util.mockito.whenever
 import com.google.common.truth.Truth.assertThat
 import com.google.common.truth.Truth.assertWithMessage
+import kotlin.test.Ignore
 import kotlinx.coroutines.ExperimentalCoroutinesApi
 import kotlinx.coroutines.Job
 import kotlinx.coroutines.flow.MutableStateFlow
@@ -395,6 +396,7 @@
         }
 
     @Test
+    @Ignore("b/315130482")
     fun deviceGoesToSleep_wakeUp_unlock() =
         testScope.runTest {
             unlockDevice()
diff --git a/packages/SystemUI/res/layout/connected_display_dialog.xml b/packages/SystemUI/res/layout/connected_display_dialog.xml
index 3f65aa7..8d7f7eb 100644
--- a/packages/SystemUI/res/layout/connected_display_dialog.xml
+++ b/packages/SystemUI/res/layout/connected_display_dialog.xml
@@ -45,6 +45,15 @@
         android:text="@string/connected_display_dialog_start_mirroring"
         android:textAppearance="@style/TextAppearance.Dialog.Title" />
 
+    <TextView
+        android:id="@+id/dual_display_warning"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:gravity="center"
+        android:visibility="gone"
+        android:text="@string/connected_display_dialog_dual_display_stop_warning"
+        android:textAppearance="@style/TextAppearance.Dialog.Body" />
+
     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
diff --git a/packages/SystemUI/res/values/strings.xml b/packages/SystemUI/res/values/strings.xml
index f49d2a1..7ca0b6e 100644
--- a/packages/SystemUI/res/values/strings.xml
+++ b/packages/SystemUI/res/values/strings.xml
@@ -3245,6 +3245,8 @@
 
     <!--- Title of the dialog appearing when an external display is connected, asking whether to start mirroring [CHAR LIMIT=NONE]-->
     <string name="connected_display_dialog_start_mirroring">Mirror to external display?</string>
+    <!--- Body of the mirroring dialog, shown when dual display is enabled. This signals that enabling mirroring will stop concurrent displays on a foldable device. [CHAR LIMIT=NONE]-->
+    <string name="connected_display_dialog_dual_display_stop_warning">Any dual screen activity currently running will be stopped</string>
     <!--- Label of the "enable display" button of the dialog appearing when an external display is connected [CHAR LIMIT=NONE]-->
     <string name="mirror_display">Mirror display</string>
     <!--- Label of the dismiss button of the dialog appearing when an external display is connected [CHAR LIMIT=NONE]-->
diff --git a/packages/SystemUI/src/com/android/keyguard/KeyguardClockSwitchController.java b/packages/SystemUI/src/com/android/keyguard/KeyguardClockSwitchController.java
index 85c9fff..be2c65f 100644
--- a/packages/SystemUI/src/com/android/keyguard/KeyguardClockSwitchController.java
+++ b/packages/SystemUI/src/com/android/keyguard/KeyguardClockSwitchController.java
@@ -21,6 +21,7 @@
 
 import static com.android.keyguard.KeyguardClockSwitch.LARGE;
 import static com.android.keyguard.KeyguardClockSwitch.SMALL;
+import static com.android.systemui.Flags.migrateClocksToBlueprint;
 import static com.android.systemui.flags.Flags.LOCKSCREEN_WALLPAPER_DREAM_ENABLED;
 import static com.android.systemui.util.kotlin.JavaAdapterKt.collectFlow;
 
@@ -43,7 +44,6 @@
 import com.android.systemui.dagger.qualifiers.Main;
 import com.android.systemui.dump.DumpManager;
 import com.android.systemui.flags.FeatureFlagsClassic;
-import com.android.systemui.flags.Flags;
 import com.android.systemui.keyguard.KeyguardUnlockAnimationController;
 import com.android.systemui.keyguard.domain.interactor.KeyguardClockInteractor;
 import com.android.systemui.keyguard.domain.interactor.KeyguardInteractor;
@@ -232,7 +232,7 @@
         mClockChangedListener = new ClockRegistry.ClockChangeListener() {
             @Override
             public void onCurrentClockChanged() {
-                if (!featureFlags.isEnabled(Flags.MIGRATE_CLOCKS_TO_BLUEPRINT)) {
+                if (!migrateClocksToBlueprint()) {
                     setClock(mClockRegistry.createCurrentClock());
                 }
             }
@@ -367,7 +367,7 @@
                 addDateWeatherView();
             }
         }
-        if (!mFeatureFlags.isEnabled(Flags.MIGRATE_CLOCKS_TO_BLUEPRINT)) {
+        if (!migrateClocksToBlueprint()) {
             setDateWeatherVisibility();
             setWeatherVisibility();
         }
@@ -418,7 +418,7 @@
     }
 
     private void addDateWeatherView() {
-        if (mFeatureFlags.isEnabled(Flags.MIGRATE_CLOCKS_TO_BLUEPRINT)) {
+        if (migrateClocksToBlueprint()) {
             return;
         }
         mDateWeatherView = (ViewGroup) mSmartspaceController.buildAndConnectDateView(mView);
@@ -434,7 +434,7 @@
     }
 
     private void addWeatherView() {
-        if (mFeatureFlags.isEnabled(Flags.MIGRATE_CLOCKS_TO_BLUEPRINT)) {
+        if (migrateClocksToBlueprint()) {
             return;
         }
         LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
@@ -447,7 +447,7 @@
     }
 
     private void addSmartspaceView() {
-        if (mFeatureFlags.isEnabled(Flags.MIGRATE_CLOCKS_TO_BLUEPRINT)) {
+        if (migrateClocksToBlueprint()) {
             return;
         }
 
@@ -650,7 +650,7 @@
     }
 
     private void setClock(ClockController clock) {
-        if (mFeatureFlags.isEnabled(Flags.MIGRATE_CLOCKS_TO_BLUEPRINT)) {
+        if (migrateClocksToBlueprint()) {
             return;
         }
         if (clock != null && mLogBuffer != null) {
@@ -664,7 +664,7 @@
 
     @Nullable
     public ClockController getClock() {
-        if (mFeatureFlags.isEnabled(Flags.MIGRATE_CLOCKS_TO_BLUEPRINT)) {
+        if (migrateClocksToBlueprint()) {
             return mKeyguardClockInteractor.getClock();
         } else {
             return mClockEventController.getClock();
@@ -676,7 +676,7 @@
     }
 
     private void updateDoubleLineClock() {
-        if (mFeatureFlags.isEnabled(Flags.MIGRATE_CLOCKS_TO_BLUEPRINT)) {
+        if (migrateClocksToBlueprint()) {
             return;
         }
         mCanShowDoubleLineClock = mSecureSettings.getIntForUser(
diff --git a/packages/SystemUI/src/com/android/keyguard/dagger/ClockRegistryModule.java b/packages/SystemUI/src/com/android/keyguard/dagger/ClockRegistryModule.java
index ee35bb9..661ce2c 100644
--- a/packages/SystemUI/src/com/android/keyguard/dagger/ClockRegistryModule.java
+++ b/packages/SystemUI/src/com/android/keyguard/dagger/ClockRegistryModule.java
@@ -16,6 +16,8 @@
 
 package com.android.keyguard.dagger;
 
+import static com.android.systemui.Flags.migrateClocksToBlueprint;
+
 import android.content.Context;
 import android.content.res.Resources;
 import android.view.LayoutInflater;
@@ -68,7 +70,7 @@
                         layoutInflater,
                         resources,
                         featureFlags.isEnabled(Flags.STEP_CLOCK_ANIMATION),
-                        featureFlags.isEnabled(Flags.MIGRATE_CLOCKS_TO_BLUEPRINT)),
+                        migrateClocksToBlueprint()),
                 context.getString(R.string.lockscreen_clock_id_fallback),
                 logBuffer,
                 /* keepAllLoaded = */ false,
diff --git a/packages/SystemUI/src/com/android/systemui/authentication/data/repository/AuthenticationRepository.kt b/packages/SystemUI/src/com/android/systemui/authentication/data/repository/AuthenticationRepository.kt
index 341d214..dd4ca92 100644
--- a/packages/SystemUI/src/com/android/systemui/authentication/data/repository/AuthenticationRepository.kt
+++ b/packages/SystemUI/src/com/android/systemui/authentication/data/repository/AuthenticationRepository.kt
@@ -21,7 +21,6 @@
 import android.app.admin.DevicePolicyManager
 import android.content.IntentFilter
 import android.os.UserHandle
-import com.android.internal.widget.LockPatternChecker
 import com.android.internal.widget.LockPatternUtils
 import com.android.internal.widget.LockscreenCredential
 import com.android.keyguard.KeyguardSecurityModel
@@ -40,8 +39,6 @@
 import dagger.Module
 import java.util.function.Function
 import javax.inject.Inject
-import kotlin.coroutines.resume
-import kotlin.coroutines.suspendCoroutine
 import kotlinx.coroutines.CoroutineDispatcher
 import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.ExperimentalCoroutinesApi
@@ -193,8 +190,8 @@
     override val throttling: MutableStateFlow<AuthenticationThrottlingModel?> =
         MutableStateFlow(null)
 
-    private val UserRepository.selectedUserId: Int
-        get() = getSelectedUserInfo().id
+    private val selectedUserId: Int
+        get() = userRepository.getSelectedUserInfo().id
 
     override val authenticationMethod: Flow<AuthenticationMethodModel> =
         combine(userRepository.selectedUserInfo, mobileConnectionsRepository.isAnySimSecure) {
@@ -233,19 +230,15 @@
 
     override suspend fun getAuthenticationMethod(): AuthenticationMethodModel {
         return withContext(backgroundDispatcher) {
-            blockingAuthenticationMethodInternal(userRepository.selectedUserId)
+            blockingAuthenticationMethodInternal(selectedUserId)
         }
     }
 
     override suspend fun getPinLength(): Int {
-        return withContext(backgroundDispatcher) {
-            val selectedUserId = userRepository.selectedUserId
-            lockPatternUtils.getPinLength(selectedUserId)
-        }
+        return withContext(backgroundDispatcher) { lockPatternUtils.getPinLength(selectedUserId) }
     }
 
     override suspend fun reportAuthenticationAttempt(isSuccessful: Boolean) {
-        val selectedUserId = userRepository.selectedUserId
         withContext(backgroundDispatcher) {
             if (isSuccessful) {
                 lockPatternUtils.reportSuccessfulPasswordAttempt(selectedUserId)
@@ -258,52 +251,32 @@
 
     override suspend fun getFailedAuthenticationAttemptCount(): Int {
         return withContext(backgroundDispatcher) {
-            val selectedUserId = userRepository.selectedUserId
             lockPatternUtils.getCurrentFailedPasswordAttempts(selectedUserId)
         }
     }
 
     override suspend fun getThrottlingEndTimestamp(): Long {
         return withContext(backgroundDispatcher) {
-            val selectedUserId = userRepository.selectedUserId
             lockPatternUtils.getLockoutAttemptDeadline(selectedUserId)
         }
     }
 
     override suspend fun setThrottleDuration(durationMs: Int) {
         withContext(backgroundDispatcher) {
-            lockPatternUtils.setLockoutAttemptDeadline(
-                userRepository.selectedUserId,
-                durationMs,
-            )
+            lockPatternUtils.setLockoutAttemptDeadline(selectedUserId, durationMs)
         }
     }
 
     override suspend fun checkCredential(
         credential: LockscreenCredential
     ): AuthenticationResultModel {
-        return suspendCoroutine { continuation ->
-            LockPatternChecker.checkCredential(
-                lockPatternUtils,
-                credential,
-                userRepository.selectedUserId,
-                object : LockPatternChecker.OnCheckCallback {
-                    override fun onChecked(matched: Boolean, throttleTimeoutMs: Int) {
-                        continuation.resume(
-                            AuthenticationResultModel(
-                                isSuccessful = matched,
-                                throttleDurationMs = throttleTimeoutMs,
-                            )
-                        )
-                    }
-
-                    override fun onCancelled() {
-                        continuation.resume(AuthenticationResultModel(isSuccessful = false))
-                    }
-
-                    override fun onEarlyMatched() = Unit
-                }
-            )
+        return withContext(backgroundDispatcher) {
+            try {
+                val matched = lockPatternUtils.checkCredential(credential, selectedUserId) {}
+                AuthenticationResultModel(isSuccessful = matched, throttleDurationMs = 0)
+            } catch (ex: LockPatternUtils.RequestThrottledException) {
+                AuthenticationResultModel(isSuccessful = false, throttleDurationMs = ex.timeoutMs)
+            }
         }
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/authentication/domain/interactor/AuthenticationInteractor.kt b/packages/SystemUI/src/com/android/systemui/authentication/domain/interactor/AuthenticationInteractor.kt
index 1ba0220..eb87505 100644
--- a/packages/SystemUI/src/com/android/systemui/authentication/domain/interactor/AuthenticationInteractor.kt
+++ b/packages/SystemUI/src/com/android/systemui/authentication/domain/interactor/AuthenticationInteractor.kt
@@ -45,6 +45,7 @@
 import kotlinx.coroutines.flow.map
 import kotlinx.coroutines.flow.stateIn
 import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
 
 /**
  * Hosts application business logic related to user authentication.
@@ -227,6 +228,10 @@
             // Since authentication succeeded, we should refresh throttling to make sure that our
             // state is completely reflecting the upstream source of truth.
             refreshThrottling()
+
+            // Force a garbage collection in an attempt to erase any credentials left in memory.
+            // Do it after a 5-sec delay to avoid making the bouncer dismiss animation janky.
+            initiateGarbageCollection(delayMs = 5000)
         }
 
         return if (authenticationResult.isSuccessful) {
@@ -310,6 +315,15 @@
         }
     }
 
+    private suspend fun initiateGarbageCollection(delayMs: Long) {
+        withContext(backgroundDispatcher) {
+            delay(delayMs)
+            System.gc()
+            System.runFinalization()
+            System.gc()
+        }
+    }
+
     companion object {
         const val TAG = "AuthenticationInteractor"
     }
diff --git a/packages/SystemUI/src/com/android/systemui/battery/BatteryMeterView.java b/packages/SystemUI/src/com/android/systemui/battery/BatteryMeterView.java
index b1a153a..24cd9b5 100644
--- a/packages/SystemUI/src/com/android/systemui/battery/BatteryMeterView.java
+++ b/packages/SystemUI/src/com/android/systemui/battery/BatteryMeterView.java
@@ -66,6 +66,8 @@
     public static final int MODE_ON = 1;
     public static final int MODE_OFF = 2;
     public static final int MODE_ESTIMATE = 3;
+    @VisibleForTesting
+    public static final long LAYOUT_TRANSITION_DURATION = 200;
 
     private final AccessorizedBatteryDrawable mDrawable;
     private final ImageView mBatteryIconView;
@@ -134,7 +136,7 @@
 
     private void setupLayoutTransition() {
         LayoutTransition transition = new LayoutTransition();
-        transition.setDuration(200);
+        transition.setDuration(LAYOUT_TRANSITION_DURATION);
 
         // Animates appearing/disappearing of the battery percentage text using fade-in/fade-out
         // and disables all other animation types
diff --git a/packages/SystemUI/src/com/android/systemui/biometrics/data/repository/DisplayStateRepository.kt b/packages/SystemUI/src/com/android/systemui/biometrics/data/repository/DisplayStateRepository.kt
index ff23837..b0143f5 100644
--- a/packages/SystemUI/src/com/android/systemui/biometrics/data/repository/DisplayStateRepository.kt
+++ b/packages/SystemUI/src/com/android/systemui/biometrics/data/repository/DisplayStateRepository.kt
@@ -60,6 +60,8 @@
     val currentRotation: StateFlow<DisplayRotation>
 }
 
+// TODO(b/296211844): This class could directly use DeviceStateRepository and DisplayRepository
+// instead.
 @SysUISingleton
 class DisplayStateRepositoryImpl
 @Inject
diff --git a/packages/SystemUI/src/com/android/systemui/display/DisplayModule.kt b/packages/SystemUI/src/com/android/systemui/display/DisplayModule.kt
index 65cd84b..373279c 100644
--- a/packages/SystemUI/src/com/android/systemui/display/DisplayModule.kt
+++ b/packages/SystemUI/src/com/android/systemui/display/DisplayModule.kt
@@ -16,6 +16,8 @@
 
 package com.android.systemui.display
 
+import com.android.systemui.display.data.repository.DeviceStateRepository
+import com.android.systemui.display.data.repository.DeviceStateRepositoryImpl
 import com.android.systemui.display.data.repository.DisplayRepository
 import com.android.systemui.display.data.repository.DisplayRepositoryImpl
 import com.android.systemui.display.domain.interactor.ConnectedDisplayInteractor
@@ -32,4 +34,9 @@
     ): ConnectedDisplayInteractor
 
     @Binds fun bindsDisplayRepository(displayRepository: DisplayRepositoryImpl): DisplayRepository
+
+    @Binds
+    fun bindsDeviceStateRepository(
+        deviceStateRepository: DeviceStateRepositoryImpl
+    ): DeviceStateRepository
 }
diff --git a/packages/SystemUI/src/com/android/systemui/display/data/repository/DeviceStateRepository.kt b/packages/SystemUI/src/com/android/systemui/display/data/repository/DeviceStateRepository.kt
new file mode 100644
index 0000000..83337f7
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/display/data/repository/DeviceStateRepository.kt
@@ -0,0 +1,88 @@
+/*
+ * Copyright (C) 2023 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.display.data.repository
+
+import android.content.Context
+import android.hardware.devicestate.DeviceStateManager
+import com.android.internal.R
+import com.android.systemui.common.coroutine.ConflatedCallbackFlow.conflatedCallbackFlow
+import com.android.systemui.dagger.qualifiers.Background
+import com.android.systemui.display.data.repository.DeviceStateRepository.DeviceState
+import java.util.concurrent.Executor
+import javax.inject.Inject
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.channels.awaitClose
+import kotlinx.coroutines.flow.SharingStarted
+import kotlinx.coroutines.flow.StateFlow
+import kotlinx.coroutines.flow.stateIn
+
+interface DeviceStateRepository {
+    val state: StateFlow<DeviceState>
+
+    enum class DeviceState {
+        /** Device state in [R.array.config_foldedDeviceStates] */
+        FOLDED,
+        /** Device state in [R.array.config_halfFoldedDeviceStates] */
+        HALF_FOLDED,
+        /** Device state in [R.array.config_openDeviceStates] */
+        UNFOLDED,
+        /** Device state in [R.array.config_rearDisplayDeviceStates] */
+        REAR_DISPLAY,
+        /** Device state in [R.array.config_concurrentDisplayDeviceStates] */
+        CONCURRENT_DISPLAY,
+        /** Device state in none of the other arrays. */
+        UNKNOWN,
+    }
+}
+
+class DeviceStateRepositoryImpl
+@Inject
+constructor(
+    context: Context,
+    deviceStateManager: DeviceStateManager,
+    @Background bgScope: CoroutineScope,
+    @Background executor: Executor
+) : DeviceStateRepository {
+
+    override val state: StateFlow<DeviceState> =
+        conflatedCallbackFlow {
+                val callback =
+                    DeviceStateManager.DeviceStateCallback { state ->
+                        trySend(deviceStateToPosture(state))
+                    }
+                deviceStateManager.registerCallback(executor, callback)
+                awaitClose { deviceStateManager.unregisterCallback(callback) }
+            }
+            .stateIn(bgScope, started = SharingStarted.WhileSubscribed(), DeviceState.UNKNOWN)
+
+    private fun deviceStateToPosture(deviceStateId: Int): DeviceState {
+        return deviceStateMap.firstOrNull { (ids, _) -> deviceStateId in ids }?.deviceState
+            ?: DeviceState.UNKNOWN
+    }
+
+    private val deviceStateMap =
+        listOf(
+                R.array.config_foldedDeviceStates to DeviceState.FOLDED,
+                R.array.config_halfFoldedDeviceStates to DeviceState.HALF_FOLDED,
+                R.array.config_openDeviceStates to DeviceState.UNFOLDED,
+                R.array.config_rearDisplayDeviceStates to DeviceState.REAR_DISPLAY,
+                R.array.config_concurrentDisplayDeviceStates to DeviceState.CONCURRENT_DISPLAY,
+            )
+            .map { IdsPerDeviceState(context.resources.getIntArray(it.first).toSet(), it.second) }
+
+    private data class IdsPerDeviceState(val ids: Set<Int>, val deviceState: DeviceState)
+}
diff --git a/packages/SystemUI/src/com/android/systemui/display/domain/interactor/ConnectedDisplayInteractor.kt b/packages/SystemUI/src/com/android/systemui/display/domain/interactor/ConnectedDisplayInteractor.kt
index 20a9e5d..73b7a8a 100644
--- a/packages/SystemUI/src/com/android/systemui/display/domain/interactor/ConnectedDisplayInteractor.kt
+++ b/packages/SystemUI/src/com/android/systemui/display/domain/interactor/ConnectedDisplayInteractor.kt
@@ -21,6 +21,7 @@
 import android.view.Display
 import com.android.systemui.dagger.SysUISingleton
 import com.android.systemui.dagger.qualifiers.Background
+import com.android.systemui.display.data.repository.DeviceStateRepository
 import com.android.systemui.display.data.repository.DisplayRepository
 import com.android.systemui.display.domain.interactor.ConnectedDisplayInteractor.PendingDisplay
 import com.android.systemui.display.domain.interactor.ConnectedDisplayInteractor.State
@@ -55,6 +56,9 @@
     /** Pending display that can be enabled to be used by the system. */
     val pendingDisplay: Flow<PendingDisplay?>
 
+    /** Pending display that can be enabled to be used by the system. */
+    val concurrentDisplaysInProgress: Flow<Boolean>
+
     /** Possible connected display state. */
     enum class State {
         DISCONNECTED,
@@ -84,6 +88,7 @@
     private val virtualDeviceManager: VirtualDeviceManager,
     keyguardRepository: KeyguardRepository,
     displayRepository: DisplayRepository,
+    deviceStateRepository: DeviceStateRepository,
     @Background backgroundCoroutineDispatcher: CoroutineDispatcher,
 ) : ConnectedDisplayInteractor {
 
@@ -128,9 +133,16 @@
             }
         }
 
+    override val concurrentDisplaysInProgress: Flow<Boolean> =
+        deviceStateRepository.state
+            .map { it == DeviceStateRepository.DeviceState.CONCURRENT_DISPLAY }
+            .distinctUntilChanged()
+            .flowOn(backgroundCoroutineDispatcher)
+
     private fun DisplayRepository.PendingDisplay.toInteractorPendingDisplay(): PendingDisplay =
         object : PendingDisplay {
             override suspend fun enable() = this@toInteractorPendingDisplay.enable()
+
             override suspend fun ignore() = this@toInteractorPendingDisplay.ignore()
         }
 
diff --git a/packages/SystemUI/src/com/android/systemui/display/ui/view/MirroringConfirmationDialog.kt b/packages/SystemUI/src/com/android/systemui/display/ui/view/MirroringConfirmationDialog.kt
index d500d1c2..c0a873a 100644
--- a/packages/SystemUI/src/com/android/systemui/display/ui/view/MirroringConfirmationDialog.kt
+++ b/packages/SystemUI/src/com/android/systemui/display/ui/view/MirroringConfirmationDialog.kt
@@ -37,11 +37,13 @@
     private val onCancelMirroring: View.OnClickListener,
     private val navbarBottomInsetsProvider: () -> Int,
     configurationController: ConfigurationController? = null,
+    private val showConcurrentDisplayInfo: Boolean = false,
     theme: Int = R.style.Theme_SystemUI_Dialog,
 ) : SystemUIBottomSheetDialog(context, configurationController, theme) {
 
     private lateinit var mirrorButton: TextView
     private lateinit var dismissButton: TextView
+    private lateinit var dualDisplayWarning: TextView
     private var enabledPressed = false
 
     override fun onCreate(savedInstanceState: Bundle?) {
@@ -56,6 +58,11 @@
         dismissButton =
             requireViewById<TextView>(R.id.cancel).apply { setOnClickListener(onCancelMirroring) }
 
+        dualDisplayWarning =
+            requireViewById<TextView>(R.id.dual_display_warning).apply {
+                visibility = if (showConcurrentDisplayInfo) View.VISIBLE else View.GONE
+            }
+
         setOnDismissListener {
             if (!enabledPressed) {
                 onCancelMirroring.onClick(null)
diff --git a/packages/SystemUI/src/com/android/systemui/display/ui/viewmodel/ConnectingDisplayViewModel.kt b/packages/SystemUI/src/com/android/systemui/display/ui/viewmodel/ConnectingDisplayViewModel.kt
index 19b4d22..10aa703 100644
--- a/packages/SystemUI/src/com/android/systemui/display/ui/viewmodel/ConnectingDisplayViewModel.kt
+++ b/packages/SystemUI/src/com/android/systemui/display/ui/viewmodel/ConnectingDisplayViewModel.kt
@@ -17,6 +17,7 @@
 
 import android.app.Dialog
 import android.content.Context
+import com.android.server.policy.feature.flags.Flags
 import com.android.systemui.biometrics.Utils
 import com.android.systemui.dagger.SysUISingleton
 import com.android.systemui.dagger.qualifiers.Application
@@ -28,8 +29,9 @@
 import javax.inject.Inject
 import kotlinx.coroutines.CoroutineDispatcher
 import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.flow.combine
+import kotlinx.coroutines.flow.flow
 import kotlinx.coroutines.flow.launchIn
-import kotlinx.coroutines.flow.onEach
 import kotlinx.coroutines.launch
 
 /**
@@ -44,25 +46,33 @@
     private val connectedDisplayInteractor: ConnectedDisplayInteractor,
     @Application private val scope: CoroutineScope,
     @Background private val bgDispatcher: CoroutineDispatcher,
-    private val configurationController: ConfigurationController
+    private val configurationController: ConfigurationController,
 ) {
 
     private var dialog: Dialog? = null
 
     /** Starts listening for pending displays. */
     fun init() {
-        connectedDisplayInteractor.pendingDisplay
-            .onEach { pendingDisplay ->
+        val pendingDisplayFlow = connectedDisplayInteractor.pendingDisplay
+        val concurrentDisplaysInProgessFlow =
+            if (Flags.enableDualDisplayBlocking()) {
+                connectedDisplayInteractor.concurrentDisplaysInProgress
+            } else {
+                flow { emit(false) }
+            }
+        pendingDisplayFlow
+            .combine(concurrentDisplaysInProgessFlow) { pendingDisplay, concurrentDisplaysInProgress
+                ->
                 if (pendingDisplay == null) {
                     hideDialog()
                 } else {
-                    showDialog(pendingDisplay)
+                    showDialog(pendingDisplay, concurrentDisplaysInProgress)
                 }
             }
             .launchIn(scope)
     }
 
-    private fun showDialog(pendingDisplay: PendingDisplay) {
+    private fun showDialog(pendingDisplay: PendingDisplay, concurrentDisplaysInProgess: Boolean) {
         hideDialog()
         dialog =
             MirroringConfirmationDialog(
@@ -77,6 +87,7 @@
                     },
                     navbarBottomInsetsProvider = { Utils.getNavbarInsets(context).bottom },
                     configurationController,
+                    showConcurrentDisplayInfo = concurrentDisplaysInProgess
                 )
                 .apply { show() }
     }
diff --git a/packages/SystemUI/src/com/android/systemui/flags/Flags.kt b/packages/SystemUI/src/com/android/systemui/flags/Flags.kt
index 7cb2c6e..e8ceabf 100644
--- a/packages/SystemUI/src/com/android/systemui/flags/Flags.kt
+++ b/packages/SystemUI/src/com/android/systemui/flags/Flags.kt
@@ -230,11 +230,6 @@
     @JvmField val MIGRATE_KEYGUARD_STATUS_BAR_VIEW =
         unreleasedFlag("migrate_keyguard_status_bar_view")
 
-    /** Migrate clocks from keyguard status view to keyguard root view*/
-    // TODO(b/301502635): Tracking Bug.
-    @JvmField val MIGRATE_CLOCKS_TO_BLUEPRINT =
-            unreleasedFlag("migrate_clocks_to_blueprint")
-
     /** Enables preview loading animation in the wallpaper picker. */
     // TODO(b/274443705): Tracking Bug
     @JvmField
@@ -443,12 +438,6 @@
     val LOCKSCREEN_ENABLE_LANDSCAPE =
             unreleasedFlag("lockscreen.enable_landscape")
 
-    // TODO(b/281648899): Tracking bug
-    @Keep
-    @JvmField
-    val WALLPAPER_MULTI_CROP =
-        sysPropBooleanFlag("persist.wm.debug.wallpaper_multi_crop", default = false)
-
     // 1200 - predictive back
     @Keep
     @JvmField
diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewConfigurator.kt b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewConfigurator.kt
index 017dac2..20da00e 100644
--- a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewConfigurator.kt
+++ b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewConfigurator.kt
@@ -43,6 +43,7 @@
 import com.android.systemui.keyguard.ui.viewmodel.KeyguardIndicationAreaViewModel
 import com.android.systemui.keyguard.ui.viewmodel.KeyguardRootViewModel
 import com.android.systemui.keyguard.ui.viewmodel.OccludingAppDeviceEntryMessageViewModel
+import com.android.systemui.plugins.FalsingManager
 import com.android.systemui.res.R
 import com.android.systemui.shade.NotificationShadeWindowView
 import com.android.systemui.shade.domain.interactor.ShadeInteractor
@@ -81,6 +82,7 @@
     private val interactionJankMonitor: InteractionJankMonitor,
     private val deviceEntryHapticsInteractor: DeviceEntryHapticsInteractor,
     private val vibratorHelper: VibratorHelper,
+    private val falsingManager: FalsingManager,
 ) : CoreStartable {
 
     private var rootViewHandle: DisposableHandle? = null
@@ -155,6 +157,7 @@
                 interactionJankMonitor,
                 deviceEntryHapticsInteractor,
                 vibratorHelper,
+                falsingManager,
             )
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/data/repository/KeyguardRepository.kt b/packages/SystemUI/src/com/android/systemui/keyguard/data/repository/KeyguardRepository.kt
index 2d6c0e1..b51edab6 100644
--- a/packages/SystemUI/src/com/android/systemui/keyguard/data/repository/KeyguardRepository.kt
+++ b/packages/SystemUI/src/com/android/systemui/keyguard/data/repository/KeyguardRepository.kt
@@ -157,6 +157,9 @@
 
     val lastDozeTapToWakePosition: StateFlow<Point?>
 
+    /** Last point that [KeyguardRootView] was tapped */
+    val lastRootViewTapPosition: MutableStateFlow<Point?>
+
     /** Observable for the [StatusBarState] */
     val statusBarState: StateFlow<StatusBarState>
 
@@ -418,6 +421,8 @@
         _lastDozeTapToWakePosition.value = position
     }
 
+    override val lastRootViewTapPosition: MutableStateFlow<Point?> = MutableStateFlow(null)
+
     override val isDreamingWithOverlay: Flow<Boolean> =
         conflatedCallbackFlow {
                 val callback =
diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/domain/interactor/KeyguardInteractor.kt b/packages/SystemUI/src/com/android/systemui/keyguard/domain/interactor/KeyguardInteractor.kt
index b8c3925..702386d 100644
--- a/packages/SystemUI/src/com/android/systemui/keyguard/domain/interactor/KeyguardInteractor.kt
+++ b/packages/SystemUI/src/com/android/systemui/keyguard/domain/interactor/KeyguardInteractor.kt
@@ -171,6 +171,9 @@
     /** Whether the keyguard is going away. */
     val isKeyguardGoingAway: Flow<Boolean> = repository.isKeyguardGoingAway
 
+    /** Last point that [KeyguardRootView] view was tapped */
+    val lastRootViewTapPosition: Flow<Point?> = repository.lastRootViewTapPosition.asStateFlow()
+
     /** Whether the primary bouncer is showing or not. */
     val primaryBouncerShowing: Flow<Boolean> = bouncerRepository.primaryBouncerShow
 
@@ -304,6 +307,10 @@
         repository.setClockShouldBeCentered(shouldBeCentered)
     }
 
+    fun setLastRootViewTapPosition(point: Point?) {
+        repository.lastRootViewTapPosition.value = point
+    }
+
     companion object {
         private const val TAG = "KeyguardInteractor"
     }
diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/ui/binder/KeyguardClockViewBinder.kt b/packages/SystemUI/src/com/android/systemui/keyguard/ui/binder/KeyguardClockViewBinder.kt
index b1c40b5..7d290c3 100644
--- a/packages/SystemUI/src/com/android/systemui/keyguard/ui/binder/KeyguardClockViewBinder.kt
+++ b/packages/SystemUI/src/com/android/systemui/keyguard/ui/binder/KeyguardClockViewBinder.kt
@@ -23,8 +23,7 @@
 import androidx.constraintlayout.widget.ConstraintSet
 import androidx.lifecycle.Lifecycle
 import androidx.lifecycle.repeatOnLifecycle
-import com.android.systemui.flags.FeatureFlagsClassic
-import com.android.systemui.flags.Flags
+import com.android.systemui.Flags.migrateClocksToBlueprint
 import com.android.systemui.keyguard.domain.interactor.KeyguardClockInteractor
 import com.android.systemui.keyguard.ui.view.layout.sections.ClockSection
 import com.android.systemui.keyguard.ui.viewmodel.KeyguardClockViewModel
@@ -42,7 +41,6 @@
         keyguardRootView: ConstraintLayout,
         viewModel: KeyguardClockViewModel,
         keyguardClockInteractor: KeyguardClockInteractor,
-        featureFlags: FeatureFlagsClassic,
     ) {
         keyguardRootView.repeatWhenAttached {
             repeatOnLifecycle(Lifecycle.State.CREATED) {
@@ -52,7 +50,7 @@
         keyguardRootView.repeatWhenAttached {
             repeatOnLifecycle(Lifecycle.State.STARTED) {
                 launch {
-                    if (!featureFlags.isEnabled(Flags.MIGRATE_CLOCKS_TO_BLUEPRINT)) return@launch
+                    if (!migrateClocksToBlueprint()) return@launch
                     viewModel.currentClock.collect { currentClock ->
                         cleanupClockViews(viewModel.clock, keyguardRootView, viewModel.burnInLayer)
                         viewModel.clock = currentClock
@@ -65,19 +63,19 @@
                 // will trigger both shouldBeCentered and clockSize change
                 // we should avoid this
                 launch {
-                    if (!featureFlags.isEnabled(Flags.MIGRATE_CLOCKS_TO_BLUEPRINT)) return@launch
+                    if (!migrateClocksToBlueprint()) return@launch
                     viewModel.clockSize.collect {
                         applyConstraints(clockSection, keyguardRootView, true)
                     }
                 }
                 launch {
-                    if (!featureFlags.isEnabled(Flags.MIGRATE_CLOCKS_TO_BLUEPRINT)) return@launch
+                    if (!migrateClocksToBlueprint()) return@launch
                     viewModel.clockShouldBeCentered.collect {
                         applyConstraints(clockSection, keyguardRootView, true)
                     }
                 }
                 launch {
-                    if (!featureFlags.isEnabled(Flags.MIGRATE_CLOCKS_TO_BLUEPRINT)) return@launch
+                    if (!migrateClocksToBlueprint()) return@launch
                     viewModel.hasCustomWeatherDataDisplay.collect {
                         applyConstraints(clockSection, keyguardRootView, true)
                     }
diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/ui/binder/KeyguardRootViewBinder.kt b/packages/SystemUI/src/com/android/systemui/keyguard/ui/binder/KeyguardRootViewBinder.kt
index ebc9c5b..e603ead 100644
--- a/packages/SystemUI/src/com/android/systemui/keyguard/ui/binder/KeyguardRootViewBinder.kt
+++ b/packages/SystemUI/src/com/android/systemui/keyguard/ui/binder/KeyguardRootViewBinder.kt
@@ -19,6 +19,8 @@
 import android.animation.Animator
 import android.animation.AnimatorListenerAdapter
 import android.annotation.DrawableRes
+import android.annotation.SuppressLint
+import android.graphics.Point
 import android.view.HapticFeedbackConstants
 import android.view.View
 import android.view.View.OnLayoutChangeListener
@@ -34,6 +36,7 @@
 import com.android.internal.jank.InteractionJankMonitor.CUJ_SCREEN_OFF_SHOW_AOD
 import com.android.keyguard.KeyguardClockSwitch.MISSING_CLOCK_ID
 import com.android.systemui.Flags.keyguardBottomAreaRefactor
+import com.android.systemui.Flags.migrateClocksToBlueprint
 import com.android.systemui.Flags.newAodTransition
 import com.android.systemui.common.shared.model.Icon
 import com.android.systemui.common.shared.model.Text
@@ -41,12 +44,12 @@
 import com.android.systemui.common.ui.ConfigurationState
 import com.android.systemui.deviceentry.domain.interactor.DeviceEntryHapticsInteractor
 import com.android.systemui.flags.FeatureFlagsClassic
-import com.android.systemui.flags.Flags
 import com.android.systemui.keyguard.shared.KeyguardShadeMigrationNssl
 import com.android.systemui.keyguard.shared.model.TransitionState
 import com.android.systemui.keyguard.ui.viewmodel.KeyguardRootViewModel
 import com.android.systemui.keyguard.ui.viewmodel.OccludingAppDeviceEntryMessageViewModel
 import com.android.systemui.lifecycle.repeatWhenAttached
+import com.android.systemui.plugins.FalsingManager
 import com.android.systemui.plugins.clocks.ClockController
 import com.android.systemui.res.R
 import com.android.systemui.shade.domain.interactor.ShadeInteractor
@@ -73,6 +76,7 @@
 @OptIn(ExperimentalCoroutinesApi::class)
 object KeyguardRootViewBinder {
 
+    @SuppressLint("ClickableViewAccessibility")
     @JvmStatic
     fun bind(
         view: ViewGroup,
@@ -87,6 +91,7 @@
         interactionJankMonitor: InteractionJankMonitor?,
         deviceEntryHapticsInteractor: DeviceEntryHapticsInteractor?,
         vibratorHelper: VibratorHelper?,
+        falsingManager: FalsingManager?,
     ): DisposableHandle {
         var onLayoutChangeListener: OnLayoutChange? = null
         val childViews = mutableMapOf<Int, View>()
@@ -94,6 +99,16 @@
         val burnInLayerId = R.id.burn_in_layer
         val aodNotificationIconContainerId = R.id.aod_notification_icon_container
         val largeClockId = R.id.lockscreen_clock_view_large
+
+        if (keyguardBottomAreaRefactor()) {
+            view.setOnTouchListener { _, event ->
+                if (falsingManager?.isFalseTap(FalsingManager.LOW_PENALTY) == false) {
+                    viewModel.setRootViewLastTapPosition(Point(event.x.toInt(), event.y.toInt()))
+                }
+                false
+            }
+        }
+
         val disposableHandle =
             view.repeatWhenAttached {
                 repeatOnLifecycle(Lifecycle.State.CREATED) {
@@ -264,7 +279,7 @@
                 }
             }
 
-        if (!featureFlags.isEnabled(Flags.MIGRATE_CLOCKS_TO_BLUEPRINT)) {
+        if (!migrateClocksToBlueprint()) {
             viewModel.clockControllerProvider = clockControllerProvider
         }
 
diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/ui/binder/KeyguardSettingsViewBinder.kt b/packages/SystemUI/src/com/android/systemui/keyguard/ui/binder/KeyguardSettingsViewBinder.kt
index 8514225..11e63e7 100644
--- a/packages/SystemUI/src/com/android/systemui/keyguard/ui/binder/KeyguardSettingsViewBinder.kt
+++ b/packages/SystemUI/src/com/android/systemui/keyguard/ui/binder/KeyguardSettingsViewBinder.kt
@@ -17,6 +17,7 @@
 
 package com.android.systemui.keyguard.ui.binder
 
+import android.graphics.Rect
 import android.view.View
 import androidx.core.view.isVisible
 import androidx.lifecycle.Lifecycle
@@ -25,6 +26,8 @@
 import com.android.systemui.animation.view.LaunchableLinearLayout
 import com.android.systemui.common.ui.binder.IconViewBinder
 import com.android.systemui.common.ui.binder.TextViewBinder
+import com.android.systemui.keyguard.ui.viewmodel.KeyguardLongPressViewModel
+import com.android.systemui.keyguard.ui.viewmodel.KeyguardRootViewModel
 import com.android.systemui.keyguard.ui.viewmodel.KeyguardSettingsMenuViewModel
 import com.android.systemui.keyguard.util.WallpaperPickerIntentUtils
 import com.android.systemui.keyguard.util.WallpaperPickerIntentUtils.LAUNCH_SOURCE_KEYGUARD
@@ -35,12 +38,15 @@
 import kotlinx.coroutines.DisposableHandle
 import kotlinx.coroutines.flow.distinctUntilChanged
 import kotlinx.coroutines.flow.filter
+import kotlinx.coroutines.flow.filterNotNull
 import kotlinx.coroutines.launch
 
 object KeyguardSettingsViewBinder {
     fun bind(
         parentView: View,
         viewModel: KeyguardSettingsMenuViewModel,
+        longPressViewModel: KeyguardLongPressViewModel,
+        rootViewModel: KeyguardRootViewModel,
         vibratorHelper: VibratorHelper,
         activityStarter: ActivityStarter
     ): DisposableHandle {
@@ -88,6 +94,18 @@
                                 }
                         }
                     }
+
+                    launch {
+                        rootViewModel.lastRootViewTapPosition.filterNotNull().collect { point ->
+                            if (view.isVisible) {
+                                val hitRect = Rect()
+                                view.getHitRect(hitRect)
+                                if (!hitRect.contains(point.x, point.y)) {
+                                    longPressViewModel.onTouchedOutside()
+                                }
+                            }
+                        }
+                    }
                 }
             }
         return disposableHandle
diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/ui/preview/KeyguardPreviewRenderer.kt b/packages/SystemUI/src/com/android/systemui/keyguard/ui/preview/KeyguardPreviewRenderer.kt
index 4eecfde..03e45fd 100644
--- a/packages/SystemUI/src/com/android/systemui/keyguard/ui/preview/KeyguardPreviewRenderer.kt
+++ b/packages/SystemUI/src/com/android/systemui/keyguard/ui/preview/KeyguardPreviewRenderer.kt
@@ -376,6 +376,7 @@
                     null, // jank monitor not required for preview mode
                     null, // device entry haptics not required preview mode
                     null, // device entry haptics not required for preview mode
+                    null, // falsing manager not required for preview mode
                 )
             )
         }
diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/ui/view/layout/sections/AodBurnInSection.kt b/packages/SystemUI/src/com/android/systemui/keyguard/ui/view/layout/sections/AodBurnInSection.kt
index df9ae41..8166b45 100644
--- a/packages/SystemUI/src/com/android/systemui/keyguard/ui/view/layout/sections/AodBurnInSection.kt
+++ b/packages/SystemUI/src/com/android/systemui/keyguard/ui/view/layout/sections/AodBurnInSection.kt
@@ -22,8 +22,8 @@
 import androidx.constraintlayout.helper.widget.Layer
 import androidx.constraintlayout.widget.ConstraintLayout
 import androidx.constraintlayout.widget.ConstraintSet
+import com.android.systemui.Flags.migrateClocksToBlueprint
 import com.android.systemui.flags.FeatureFlagsClassic
-import com.android.systemui.flags.Flags
 import com.android.systemui.keyguard.shared.KeyguardShadeMigrationNssl
 import com.android.systemui.keyguard.shared.model.KeyguardSection
 import com.android.systemui.keyguard.ui.viewmodel.KeyguardClockViewModel
@@ -52,13 +52,13 @@
             Layer(context).apply {
                 id = R.id.burn_in_layer
                 addView(nic)
-                if (!featureFlags.isEnabled(Flags.MIGRATE_CLOCKS_TO_BLUEPRINT)) {
+                if (!migrateClocksToBlueprint()) {
                     val statusView =
                         constraintLayout.requireViewById<View>(R.id.keyguard_status_view)
                     addView(statusView)
                 }
             }
-        if (featureFlags.isEnabled(Flags.MIGRATE_CLOCKS_TO_BLUEPRINT)) {
+        if (migrateClocksToBlueprint()) {
             addSmartspaceViews(constraintLayout)
         }
         constraintLayout.addView(burnInLayer)
@@ -68,7 +68,7 @@
         if (!KeyguardShadeMigrationNssl.isEnabled) {
             return
         }
-        if (featureFlags.isEnabled(Flags.MIGRATE_CLOCKS_TO_BLUEPRINT)) {
+        if (migrateClocksToBlueprint()) {
             clockViewModel.burnInLayer = burnInLayer
         }
     }
diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/ui/view/layout/sections/AodNotificationIconsSection.kt b/packages/SystemUI/src/com/android/systemui/keyguard/ui/view/layout/sections/AodNotificationIconsSection.kt
index 12de185..96efb23 100644
--- a/packages/SystemUI/src/com/android/systemui/keyguard/ui/view/layout/sections/AodNotificationIconsSection.kt
+++ b/packages/SystemUI/src/com/android/systemui/keyguard/ui/view/layout/sections/AodNotificationIconsSection.kt
@@ -26,9 +26,9 @@
 import androidx.constraintlayout.widget.ConstraintSet.PARENT_ID
 import androidx.constraintlayout.widget.ConstraintSet.START
 import androidx.constraintlayout.widget.ConstraintSet.TOP
+import com.android.systemui.Flags.migrateClocksToBlueprint
 import com.android.systemui.common.ui.ConfigurationState
 import com.android.systemui.flags.FeatureFlagsClassic
-import com.android.systemui.flags.Flags
 import com.android.systemui.keyguard.shared.KeyguardShadeMigrationNssl
 import com.android.systemui.keyguard.shared.model.KeyguardSection
 import com.android.systemui.keyguard.ui.viewmodel.KeyguardSmartspaceViewModel
@@ -118,7 +118,7 @@
                 BOTTOM
             }
         constraintSet.apply {
-            if (featureFlags.isEnabled(Flags.MIGRATE_CLOCKS_TO_BLUEPRINT)) {
+            if (migrateClocksToBlueprint()) {
                 connect(
                     nicId,
                     TOP,
diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/ui/view/layout/sections/ClockSection.kt b/packages/SystemUI/src/com/android/systemui/keyguard/ui/view/layout/sections/ClockSection.kt
index c8b2d39..1df920a 100644
--- a/packages/SystemUI/src/com/android/systemui/keyguard/ui/view/layout/sections/ClockSection.kt
+++ b/packages/SystemUI/src/com/android/systemui/keyguard/ui/view/layout/sections/ClockSection.kt
@@ -68,7 +68,6 @@
             constraintLayout,
             keyguardClockViewModel,
             clockInteractor,
-            featureFlags
         )
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/ui/view/layout/sections/DefaultNotificationStackScrollLayoutSection.kt b/packages/SystemUI/src/com/android/systemui/keyguard/ui/view/layout/sections/DefaultNotificationStackScrollLayoutSection.kt
index 108f2a3..a64a422 100644
--- a/packages/SystemUI/src/com/android/systemui/keyguard/ui/view/layout/sections/DefaultNotificationStackScrollLayoutSection.kt
+++ b/packages/SystemUI/src/com/android/systemui/keyguard/ui/view/layout/sections/DefaultNotificationStackScrollLayoutSection.kt
@@ -24,10 +24,9 @@
 import androidx.constraintlayout.widget.ConstraintSet.PARENT_ID
 import androidx.constraintlayout.widget.ConstraintSet.START
 import androidx.constraintlayout.widget.ConstraintSet.TOP
+import com.android.systemui.Flags.migrateClocksToBlueprint
 import com.android.systemui.dagger.qualifiers.Main
 import com.android.systemui.deviceentry.shared.DeviceEntryUdfpsRefactor
-import com.android.systemui.flags.FeatureFlags
-import com.android.systemui.flags.Flags
 import com.android.systemui.keyguard.shared.KeyguardShadeMigrationNssl
 import com.android.systemui.keyguard.ui.viewmodel.KeyguardSmartspaceViewModel
 import com.android.systemui.res.R
@@ -47,7 +46,6 @@
 @Inject
 constructor(
     context: Context,
-    private val featureFlags: FeatureFlags,
     sceneContainerFlags: SceneContainerFlags,
     notificationPanelView: NotificationPanelView,
     sharedNotificationContainer: SharedNotificationContainer,
@@ -79,7 +77,7 @@
             val bottomMargin =
                 context.resources.getDimensionPixelSize(R.dimen.keyguard_status_view_bottom_margin)
 
-            if (featureFlags.isEnabled(Flags.MIGRATE_CLOCKS_TO_BLUEPRINT)) {
+            if (migrateClocksToBlueprint()) {
                 connect(
                     R.id.nssl_placeholder,
                     TOP,
diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/ui/view/layout/sections/DefaultSettingsPopupMenuSection.kt b/packages/SystemUI/src/com/android/systemui/keyguard/ui/view/layout/sections/DefaultSettingsPopupMenuSection.kt
index 9a33f08..4bc2d86 100644
--- a/packages/SystemUI/src/com/android/systemui/keyguard/ui/view/layout/sections/DefaultSettingsPopupMenuSection.kt
+++ b/packages/SystemUI/src/com/android/systemui/keyguard/ui/view/layout/sections/DefaultSettingsPopupMenuSection.kt
@@ -29,15 +29,15 @@
 import androidx.constraintlayout.widget.ConstraintSet.WRAP_CONTENT
 import androidx.core.view.isVisible
 import com.android.systemui.Flags.keyguardBottomAreaRefactor
-import com.android.systemui.res.R
 import com.android.systemui.animation.view.LaunchableLinearLayout
 import com.android.systemui.dagger.qualifiers.Main
-import com.android.systemui.flags.FeatureFlags
-import com.android.systemui.flags.Flags
 import com.android.systemui.keyguard.shared.model.KeyguardSection
 import com.android.systemui.keyguard.ui.binder.KeyguardSettingsViewBinder
+import com.android.systemui.keyguard.ui.viewmodel.KeyguardLongPressViewModel
+import com.android.systemui.keyguard.ui.viewmodel.KeyguardRootViewModel
 import com.android.systemui.keyguard.ui.viewmodel.KeyguardSettingsMenuViewModel
 import com.android.systemui.plugins.ActivityStarter
+import com.android.systemui.res.R
 import com.android.systemui.statusbar.VibratorHelper
 import javax.inject.Inject
 import kotlinx.coroutines.DisposableHandle
@@ -47,6 +47,8 @@
 constructor(
     @Main private val resources: Resources,
     private val keyguardSettingsMenuViewModel: KeyguardSettingsMenuViewModel,
+    private val keyguardLongPressViewModel: KeyguardLongPressViewModel,
+    private val keyguardRootViewModel: KeyguardRootViewModel,
     private val vibratorHelper: VibratorHelper,
     private val activityStarter: ActivityStarter,
 ) : KeyguardSection() {
@@ -73,6 +75,8 @@
                 KeyguardSettingsViewBinder.bind(
                     constraintLayout.requireViewById<View>(R.id.keyguard_settings_button),
                     keyguardSettingsMenuViewModel,
+                    keyguardLongPressViewModel,
+                    keyguardRootViewModel,
                     vibratorHelper,
                     activityStarter,
                 )
diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/ui/view/layout/sections/SmartspaceSection.kt b/packages/SystemUI/src/com/android/systemui/keyguard/ui/view/layout/sections/SmartspaceSection.kt
index a005692..368b388 100644
--- a/packages/SystemUI/src/com/android/systemui/keyguard/ui/view/layout/sections/SmartspaceSection.kt
+++ b/packages/SystemUI/src/com/android/systemui/keyguard/ui/view/layout/sections/SmartspaceSection.kt
@@ -26,8 +26,7 @@
 import androidx.constraintlayout.widget.ConstraintSet.START
 import androidx.constraintlayout.widget.ConstraintSet.TOP
 import androidx.constraintlayout.widget.ConstraintSet.WRAP_CONTENT
-import com.android.systemui.flags.FeatureFlagsClassic
-import com.android.systemui.flags.Flags
+import com.android.systemui.Flags.migrateClocksToBlueprint
 import com.android.systemui.keyguard.KeyguardUnlockAnimationController
 import com.android.systemui.keyguard.shared.model.KeyguardSection
 import com.android.systemui.keyguard.ui.binder.KeyguardSmartspaceViewBinder
@@ -45,14 +44,13 @@
     private val context: Context,
     val smartspaceController: LockscreenSmartspaceController,
     val keyguardUnlockAnimationController: KeyguardUnlockAnimationController,
-    val featureFlags: FeatureFlagsClassic,
 ) : KeyguardSection() {
     private var smartspaceView: View? = null
     private var weatherView: View? = null
     private var dateView: View? = null
 
     override fun addViews(constraintLayout: ConstraintLayout) {
-        if (!featureFlags.isEnabled(Flags.MIGRATE_CLOCKS_TO_BLUEPRINT)) {
+        if (!migrateClocksToBlueprint()) {
             return
         }
         smartspaceView = smartspaceController.buildAndConnectView(constraintLayout)
diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/ui/view/layout/sections/SplitShadeNotificationStackScrollLayoutSection.kt b/packages/SystemUI/src/com/android/systemui/keyguard/ui/view/layout/sections/SplitShadeNotificationStackScrollLayoutSection.kt
index 8640e00..921fb3b 100644
--- a/packages/SystemUI/src/com/android/systemui/keyguard/ui/view/layout/sections/SplitShadeNotificationStackScrollLayoutSection.kt
+++ b/packages/SystemUI/src/com/android/systemui/keyguard/ui/view/layout/sections/SplitShadeNotificationStackScrollLayoutSection.kt
@@ -24,10 +24,9 @@
 import androidx.constraintlayout.widget.ConstraintSet.PARENT_ID
 import androidx.constraintlayout.widget.ConstraintSet.START
 import androidx.constraintlayout.widget.ConstraintSet.TOP
+import com.android.systemui.Flags.migrateClocksToBlueprint
 import com.android.systemui.dagger.qualifiers.Main
 import com.android.systemui.deviceentry.shared.DeviceEntryUdfpsRefactor
-import com.android.systemui.flags.FeatureFlags
-import com.android.systemui.flags.Flags
 import com.android.systemui.keyguard.shared.KeyguardShadeMigrationNssl
 import com.android.systemui.keyguard.ui.viewmodel.KeyguardSmartspaceViewModel
 import com.android.systemui.res.R
@@ -47,7 +46,6 @@
 @Inject
 constructor(
     context: Context,
-    private val featureFlags: FeatureFlags,
     sceneContainerFlags: SceneContainerFlags,
     notificationPanelView: NotificationPanelView,
     sharedNotificationContainer: SharedNotificationContainer,
@@ -79,7 +77,7 @@
             val bottomMargin =
                 context.resources.getDimensionPixelSize(R.dimen.keyguard_status_view_bottom_margin)
 
-            if (featureFlags.isEnabled(Flags.MIGRATE_CLOCKS_TO_BLUEPRINT)) {
+            if (migrateClocksToBlueprint()) {
                 connect(
                     R.id.nssl_placeholder,
                     TOP,
diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/ui/viewmodel/KeyguardRootViewModel.kt b/packages/SystemUI/src/com/android/systemui/keyguard/ui/viewmodel/KeyguardRootViewModel.kt
index d250c1b..4588e02 100644
--- a/packages/SystemUI/src/com/android/systemui/keyguard/ui/viewmodel/KeyguardRootViewModel.kt
+++ b/packages/SystemUI/src/com/android/systemui/keyguard/ui/viewmodel/KeyguardRootViewModel.kt
@@ -17,17 +17,18 @@
 
 package com.android.systemui.keyguard.ui.viewmodel
 
+import android.graphics.Point
 import android.util.MathUtils
 import android.view.View.VISIBLE
 import com.android.app.animation.Interpolators
 import com.android.keyguard.KeyguardClockSwitch.LARGE
+import com.android.systemui.Flags.migrateClocksToBlueprint
 import com.android.systemui.Flags.newAodTransition
 import com.android.systemui.common.shared.model.NotificationContainerBounds
 import com.android.systemui.common.ui.domain.interactor.ConfigurationInteractor
 import com.android.systemui.dagger.SysUISingleton
 import com.android.systemui.deviceentry.domain.interactor.DeviceEntryInteractor
 import com.android.systemui.flags.FeatureFlagsClassic
-import com.android.systemui.flags.Flags
 import com.android.systemui.keyguard.domain.interactor.BurnInInteractor
 import com.android.systemui.keyguard.domain.interactor.KeyguardInteractor
 import com.android.systemui.keyguard.domain.interactor.KeyguardTransitionInteractor
@@ -82,7 +83,7 @@
 ) {
     var clockControllerProvider: Provider<ClockController>? = null
         get() {
-            if (featureFlags.isEnabled(Flags.MIGRATE_CLOCKS_TO_BLUEPRINT)) {
+            if (migrateClocksToBlueprint()) {
                 return Provider { keyguardClockViewModel.clock }
             } else {
                 return field
@@ -101,6 +102,9 @@
 
     val goneToAodTransition = keyguardTransitionInteractor.transition(from = GONE, to = AOD)
 
+    /** Last point that the root view was tapped */
+    val lastRootViewTapPosition: Flow<Point?> = keyguardInteractor.lastRootViewTapPosition
+
     /** the shared notification container bounds *on the lockscreen* */
     val notificationBounds: StateFlow<NotificationContainerBounds> =
         keyguardInteractor.notificationContainerBounds
@@ -134,7 +138,7 @@
                 // Ensure the desired translation doesn't encroach on the top inset
                 val burnInY = MathUtils.lerp(0, burnIn.translationY, interpolation).toInt()
                 val translationY =
-                    if (featureFlags.isEnabled(Flags.MIGRATE_CLOCKS_TO_BLUEPRINT)) {
+                    if (migrateClocksToBlueprint()) {
                         burnInY
                     } else {
                         -(statusViewTop - Math.max(topInset, statusViewTop + burnInY))
@@ -262,4 +266,8 @@
             }
             .toAnimatedValueFlow()
     }
+
+    fun setRootViewLastTapPosition(point: Point) {
+        keyguardInteractor.setLastRootViewTapPosition(point)
+    }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/shade/NotificationPanelViewController.java b/packages/SystemUI/src/com/android/systemui/shade/NotificationPanelViewController.java
index 1e86b11..95f7c94a 100644
--- a/packages/SystemUI/src/com/android/systemui/shade/NotificationPanelViewController.java
+++ b/packages/SystemUI/src/com/android/systemui/shade/NotificationPanelViewController.java
@@ -25,6 +25,7 @@
 import static com.android.keyguard.KeyguardClockSwitch.LARGE;
 import static com.android.keyguard.KeyguardClockSwitch.SMALL;
 import static com.android.systemui.Flags.keyguardBottomAreaRefactor;
+import static com.android.systemui.Flags.migrateClocksToBlueprint;
 import static com.android.systemui.classifier.Classifier.BOUNCER_UNLOCK;
 import static com.android.systemui.classifier.Classifier.GENERIC;
 import static com.android.systemui.classifier.Classifier.QUICK_SETTINGS;
@@ -1608,7 +1609,7 @@
         int userSwitcherPreferredY = mStatusBarHeaderHeightKeyguard;
         boolean bypassEnabled = mKeyguardBypassController.getBypassEnabled();
         boolean shouldAnimateClockChange = mScreenOffAnimationController.shouldAnimateClockChange();
-        if (mFeatureFlags.isEnabled(Flags.MIGRATE_CLOCKS_TO_BLUEPRINT)) {
+        if (migrateClocksToBlueprint()) {
             mKeyguardClockInteractor.setClockSize(computeDesiredClockSize());
         } else {
             mKeyguardStatusViewController.displayClock(computeDesiredClockSize(),
@@ -1741,7 +1742,7 @@
         } else {
             layout = mNotificationContainerParent;
         }
-        if (mFeatureFlags.isEnabled(Flags.MIGRATE_CLOCKS_TO_BLUEPRINT)) {
+        if (migrateClocksToBlueprint()) {
             mKeyguardInteractor.setClockShouldBeCentered(shouldBeCentered);
         } else {
             mKeyguardStatusViewController.updateAlignment(
diff --git a/packages/SystemUI/src/com/android/systemui/shade/QuickSettingsController.java b/packages/SystemUI/src/com/android/systemui/shade/QuickSettingsController.java
index 8397caa..dd194eaa 100644
--- a/packages/SystemUI/src/com/android/systemui/shade/QuickSettingsController.java
+++ b/packages/SystemUI/src/com/android/systemui/shade/QuickSettingsController.java
@@ -794,6 +794,13 @@
 
     /** update Qs height state */
     public void setExpansionHeight(float height) {
+        // TODO(b/277909752): remove below log when bug is fixed
+        if (mSplitShadeEnabled && mShadeExpandedFraction == 1.0f && height == 0
+                && mBarState == SHADE) {
+            Log.wtf(TAG,
+                    "setting QS height to 0 in split shade while shade is open(ing). "
+                            + "Value of isExpandImmediate() = " + isExpandImmediate());
+        }
         int maxHeight = getMaxExpansionHeight();
         height = Math.min(Math.max(
                 height, getMinExpansionHeight()), maxHeight);
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/row/ExpandableNotificationRow.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/row/ExpandableNotificationRow.java
index 6cb079a..b6d4ded 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/row/ExpandableNotificationRow.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/row/ExpandableNotificationRow.java
@@ -139,7 +139,6 @@
     private static final long RECENTLY_ALERTED_THRESHOLD_MS = TimeUnit.SECONDS.toMillis(30);
     private static final SourceType BASE_VALUE = SourceType.from("BaseValue");
     private static final SourceType FROM_PARENT = SourceType.from("FromParent(ENR)");
-    private static final SourceType PINNED = SourceType.from("Pinned");
 
     // We don't correctly track dark mode until the content views are inflated, so always update
     // the background on first content update just in case it happens to be during a theme change.
@@ -147,7 +146,6 @@
     private boolean mIsSnoozed;
     private boolean mShowSnooze = false;
     private boolean mIsFaded;
-    private boolean mAnimatePinnedRoundness = false;
 
     /**
      * Listener for when {@link ExpandableNotificationRow} is laid out.
@@ -1053,14 +1051,6 @@
         if (isAboveShelf() != wasAboveShelf) {
             mAboveShelfChangedListener.onAboveShelfStateChanged(!wasAboveShelf);
         }
-        if (pinned) {
-            // Should be animated if someone explicitly set it to 0 and the row is shown.
-            boolean animated = mAnimatePinnedRoundness && isShown();
-            requestRoundness(/* top = */ 1f, /* bottom = */ 1f, PINNED, animated);
-        } else {
-            requestRoundnessReset(PINNED);
-            mAnimatePinnedRoundness = true;
-        }
     }
 
     @Override
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/StackStateLogger.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/StackStateLogger.kt
index d635f89..bf0c823 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/StackStateLogger.kt
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/StackStateLogger.kt
@@ -29,7 +29,7 @@
             TAG,
             LogLevel.ERROR,
             { str1 = logKey(key) },
-            { "Heads up view disappearing $str1 for ANIMATION_TYPE_ADD" }
+            { "Heads up view appearing $str1 for ANIMATION_TYPE_ADD" }
         )
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/SystemUIDialog.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/SystemUIDialog.java
index 93bc960..af6da3f 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/SystemUIDialog.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/SystemUIDialog.java
@@ -146,7 +146,7 @@
          * When you just need a dialog, call this.
          */
         public SystemUIDialog create() {
-            return create(new DialogDelegate<>(){});
+            return create(new DialogDelegate<>(){}, mContext);
         }
 
         /**
@@ -155,13 +155,18 @@
          *
          * When you need to customize the dialog, pass it a delegate.
          */
-        public SystemUIDialog create(Delegate delegate) {
-            return create((DialogDelegate<SystemUIDialog>) delegate);
+        public SystemUIDialog create(Delegate delegate, Context context) {
+            return create((DialogDelegate<SystemUIDialog>) delegate, context);
         }
 
-        private SystemUIDialog create(DialogDelegate<SystemUIDialog> dialogDelegate) {
+        public SystemUIDialog create(Delegate delegate) {
+            return create(delegate, mContext);
+        }
+
+        private SystemUIDialog create(DialogDelegate<SystemUIDialog> dialogDelegate,
+                Context context) {
             return new SystemUIDialog(
-                    mContext,
+                    context,
                     DEFAULT_THEME,
                     DEFAULT_DISMISS_ON_DEVICE_LOCK,
                     mFeatureFlags,
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/pipeline/mobile/ui/binder/MobileIconBinder.kt b/packages/SystemUI/src/com/android/systemui/statusbar/pipeline/mobile/ui/binder/MobileIconBinder.kt
index b93e443..a14e87c 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/pipeline/mobile/ui/binder/MobileIconBinder.kt
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/pipeline/mobile/ui/binder/MobileIconBinder.kt
@@ -154,8 +154,13 @@
                                 dataTypeId,
                             )
                             dataTypeId?.let { IconViewBinder.bind(dataTypeId, networkTypeView) }
+                            val prevVis = networkTypeContainer.visibility
                             networkTypeContainer.visibility =
                                 if (dataTypeId != null) VISIBLE else GONE
+
+                            if (prevVis != networkTypeContainer.visibility) {
+                                view.requestLayout()
+                            }
                         }
                     }
 
diff --git a/packages/SystemUI/tests/src/com/android/keyguard/KeyguardClockSwitchControllerBaseTest.java b/packages/SystemUI/tests/src/com/android/keyguard/KeyguardClockSwitchControllerBaseTest.java
index 2bbf0df..24917b3 100644
--- a/packages/SystemUI/tests/src/com/android/keyguard/KeyguardClockSwitchControllerBaseTest.java
+++ b/packages/SystemUI/tests/src/com/android/keyguard/KeyguardClockSwitchControllerBaseTest.java
@@ -19,7 +19,6 @@
 import static android.view.View.INVISIBLE;
 
 import static com.android.systemui.flags.Flags.LOCKSCREEN_WALLPAPER_DREAM_ENABLED;
-import static com.android.systemui.flags.Flags.MIGRATE_CLOCKS_TO_BLUEPRINT;
 
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.atLeast;
@@ -179,7 +178,6 @@
         mExecutor = new FakeExecutor(new FakeSystemClock());
         mFakeFeatureFlags = new FakeFeatureFlags();
         mFakeFeatureFlags.set(LOCKSCREEN_WALLPAPER_DREAM_ENABLED, false);
-        mFakeFeatureFlags.set(MIGRATE_CLOCKS_TO_BLUEPRINT, false);
         mController = new KeyguardClockSwitchController(
                 mView,
                 mStatusBarStateController,
diff --git a/packages/SystemUI/tests/src/com/android/systemui/display/data/repository/DeviceStateRepositoryTest.kt b/packages/SystemUI/tests/src/com/android/systemui/display/data/repository/DeviceStateRepositoryTest.kt
new file mode 100644
index 0000000..21b8aca
--- /dev/null
+++ b/packages/SystemUI/tests/src/com/android/systemui/display/data/repository/DeviceStateRepositoryTest.kt
@@ -0,0 +1,164 @@
+/*
+ * Copyright (C) 2023 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.display.data.repository
+
+import android.hardware.devicestate.DeviceStateManager
+import android.testing.AndroidTestingRunner
+import android.testing.TestableLooper
+import androidx.test.filters.SmallTest
+import com.android.internal.R
+import com.android.systemui.SysuiTestCase
+import com.android.systemui.coroutines.FlowValue
+import com.android.systemui.coroutines.collectLastValue
+import com.android.systemui.display.data.repository.DeviceStateRepository.DeviceState
+import com.android.systemui.util.concurrency.FakeExecutor
+import com.android.systemui.util.mockito.any
+import com.android.systemui.util.mockito.kotlinArgumentCaptor
+import com.android.systemui.util.mockito.mock
+import com.android.systemui.util.time.FakeSystemClock
+import com.google.common.truth.Truth.assertThat
+import kotlin.test.Test
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.test.TestScope
+import kotlinx.coroutines.test.UnconfinedTestDispatcher
+import kotlinx.coroutines.test.runTest
+import org.junit.Before
+import org.junit.runner.RunWith
+import org.mockito.Mockito.never
+import org.mockito.Mockito.verify
+
+@RunWith(AndroidTestingRunner::class)
+@TestableLooper.RunWithLooper
+@OptIn(ExperimentalCoroutinesApi::class)
+@SmallTest
+class DeviceStateRepositoryTest : SysuiTestCase() {
+
+    private val deviceStateManager = mock<DeviceStateManager>()
+    private val deviceStateManagerListener =
+        kotlinArgumentCaptor<DeviceStateManager.DeviceStateCallback>()
+
+    private val testScope = TestScope(UnconfinedTestDispatcher())
+    private val fakeExecutor = FakeExecutor(FakeSystemClock())
+
+    private lateinit var deviceStateRepository: DeviceStateRepositoryImpl
+
+    @Before
+    fun setup() {
+        mContext.orCreateTestableResources.apply {
+            addOverride(R.array.config_foldedDeviceStates, listOf(TEST_FOLDED).toIntArray())
+            addOverride(R.array.config_halfFoldedDeviceStates, TEST_HALF_FOLDED.toIntArray())
+            addOverride(R.array.config_openDeviceStates, TEST_UNFOLDED.toIntArray())
+            addOverride(R.array.config_rearDisplayDeviceStates, TEST_REAR_DISPLAY.toIntArray())
+            addOverride(
+                R.array.config_concurrentDisplayDeviceStates,
+                TEST_CONCURRENT_DISPLAY.toIntArray()
+            )
+        }
+        deviceStateRepository =
+            DeviceStateRepositoryImpl(
+                mContext,
+                deviceStateManager,
+                TestScope(UnconfinedTestDispatcher()),
+                fakeExecutor
+            )
+
+        // It should register only after there are clients collecting the flow
+        verify(deviceStateManager, never()).registerCallback(any(), any())
+    }
+
+    @Test
+    fun folded_receivesFoldedState() =
+        testScope.runTest {
+            val state = displayState()
+
+            deviceStateManagerListener.value.onStateChanged(TEST_FOLDED)
+
+            assertThat(state()).isEqualTo(DeviceState.FOLDED)
+        }
+
+    @Test
+    fun halfFolded_receivesHalfFoldedState() =
+        testScope.runTest {
+            val state = displayState()
+
+            deviceStateManagerListener.value.onStateChanged(TEST_HALF_FOLDED)
+
+            assertThat(state()).isEqualTo(DeviceState.HALF_FOLDED)
+        }
+
+    @Test
+    fun unfolded_receivesUnfoldedState() =
+        testScope.runTest {
+            val state = displayState()
+
+            deviceStateManagerListener.value.onStateChanged(TEST_UNFOLDED)
+
+            assertThat(state()).isEqualTo(DeviceState.UNFOLDED)
+        }
+
+    @Test
+    fun rearDisplay_receivesRearDisplayState() =
+        testScope.runTest {
+            val state = displayState()
+
+            deviceStateManagerListener.value.onStateChanged(TEST_REAR_DISPLAY)
+
+            assertThat(state()).isEqualTo(DeviceState.REAR_DISPLAY)
+        }
+
+    @Test
+    fun concurrentDisplay_receivesConcurrentDisplayState() =
+        testScope.runTest {
+            val state = displayState()
+
+            deviceStateManagerListener.value.onStateChanged(TEST_CONCURRENT_DISPLAY)
+
+            assertThat(state()).isEqualTo(DeviceState.CONCURRENT_DISPLAY)
+        }
+
+    @Test
+    fun unknownState_receivesUnknownState() =
+        testScope.runTest {
+            val state = displayState()
+
+            deviceStateManagerListener.value.onStateChanged(123456)
+
+            assertThat(state()).isEqualTo(DeviceState.UNKNOWN)
+        }
+
+    private fun TestScope.displayState(): FlowValue<DeviceState?> {
+        val flowValue = collectLastValue(deviceStateRepository.state)
+        verify(deviceStateManager)
+            .registerCallback(
+                any(),
+                deviceStateManagerListener.capture(),
+            )
+        return flowValue
+    }
+
+    private fun Int.toIntArray() = listOf(this).toIntArray()
+
+    private companion object {
+        // Used to fake the ids in the test. Note that there is no guarantees different devices will
+        // have the same ids (that's why the ones in this test start from 41)
+        const val TEST_FOLDED = 41
+        const val TEST_HALF_FOLDED = 42
+        const val TEST_UNFOLDED = 43
+        const val TEST_REAR_DISPLAY = 44
+        const val TEST_CONCURRENT_DISPLAY = 45
+    }
+}
diff --git a/packages/SystemUI/tests/src/com/android/systemui/display/domain/interactor/ConnectedDisplayInteractorTest.kt b/packages/SystemUI/tests/src/com/android/systemui/display/domain/interactor/ConnectedDisplayInteractorTest.kt
index 1f18705..42b0f50 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/display/domain/interactor/ConnectedDisplayInteractorTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/display/domain/interactor/ConnectedDisplayInteractorTest.kt
@@ -28,6 +28,9 @@
 import com.android.systemui.SysuiTestCase
 import com.android.systemui.coroutines.FlowValue
 import com.android.systemui.coroutines.collectLastValue
+import com.android.systemui.display.data.repository.DeviceStateRepository
+import com.android.systemui.display.data.repository.DeviceStateRepository.DeviceState.CONCURRENT_DISPLAY
+import com.android.systemui.display.data.repository.FakeDeviceStateRepository
 import com.android.systemui.display.data.repository.FakeDisplayRepository
 import com.android.systemui.display.data.repository.createPendingDisplay
 import com.android.systemui.display.data.repository.display
@@ -59,11 +62,13 @@
 
     private val fakeDisplayRepository = FakeDisplayRepository()
     private val fakeKeyguardRepository = FakeKeyguardRepository()
+    private val fakeDeviceStateRepository = FakeDeviceStateRepository()
     private val connectedDisplayStateProvider: ConnectedDisplayInteractor =
         ConnectedDisplayInteractorImpl(
             virtualDeviceManager,
             fakeKeyguardRepository,
             fakeDisplayRepository,
+            fakeDeviceStateRepository,
             UnconfinedTestDispatcher(),
         )
     private val testScope = TestScope(UnconfinedTestDispatcher())
@@ -283,6 +288,44 @@
             assertThat(pendingDisplay).isNull()
         }
 
+    @Test
+    fun concurrentDisplaysInProgress_started_returnsTrue() =
+        testScope.runTest {
+            val concurrentDisplaysInProgress =
+                collectLastValue(connectedDisplayStateProvider.concurrentDisplaysInProgress)
+
+            fakeDeviceStateRepository.emit(CONCURRENT_DISPLAY)
+
+            assertThat(concurrentDisplaysInProgress()).isTrue()
+        }
+
+    @Test
+    fun concurrentDisplaysInProgress_stopped_returnsFalse() =
+        testScope.runTest {
+            val concurrentDisplaysInProgress =
+                collectLastValue(connectedDisplayStateProvider.concurrentDisplaysInProgress)
+
+            fakeDeviceStateRepository.emit(CONCURRENT_DISPLAY)
+            fakeDeviceStateRepository.emit(DeviceStateRepository.DeviceState.UNKNOWN)
+
+            assertThat(concurrentDisplaysInProgress()).isFalse()
+        }
+
+    @Test
+    fun concurrentDisplaysInProgress_otherStates_returnsFalse() =
+        testScope.runTest {
+            val concurrentDisplaysInProgress =
+                collectLastValue(connectedDisplayStateProvider.concurrentDisplaysInProgress)
+
+            DeviceStateRepository.DeviceState.entries
+                .filter { it != CONCURRENT_DISPLAY }
+                .forEach { deviceState ->
+                    fakeDeviceStateRepository.emit(deviceState)
+
+                    assertThat(concurrentDisplaysInProgress()).isFalse()
+                }
+        }
+
     private fun TestScope.lastValue(): FlowValue<State?> =
         collectLastValue(connectedDisplayStateProvider.connectedDisplayState)
 
diff --git a/packages/SystemUI/tests/src/com/android/systemui/keyguard/ui/view/layout/sections/ClockSectionTest.kt b/packages/SystemUI/tests/src/com/android/systemui/keyguard/ui/view/layout/sections/ClockSectionTest.kt
index 64a07fa..e89b61f 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/keyguard/ui/view/layout/sections/ClockSectionTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/keyguard/ui/view/layout/sections/ClockSectionTest.kt
@@ -21,7 +21,6 @@
 import androidx.test.filters.SmallTest
 import com.android.systemui.SysuiTestCase
 import com.android.systemui.flags.FakeFeatureFlagsClassic
-import com.android.systemui.flags.Flags.MIGRATE_CLOCKS_TO_BLUEPRINT
 import com.android.systemui.keyguard.domain.interactor.KeyguardClockInteractor
 import com.android.systemui.keyguard.ui.viewmodel.KeyguardClockViewModel
 import com.android.systemui.keyguard.ui.viewmodel.KeyguardSmartspaceViewModel
@@ -82,7 +81,6 @@
             .thenReturn(SMART_SPACE_DATE_WEATHER_HEIGHT)
         whenever(smartspaceViewModel.getDimen("enhanced_smartspace_height"))
             .thenReturn(ENHANCED_SMART_SPACE_HEIGHT)
-        featureFlags.set(MIGRATE_CLOCKS_TO_BLUEPRINT, true)
         underTest =
             ClockSection(
                 keyguardClockInteractor,
diff --git a/packages/SystemUI/tests/src/com/android/systemui/keyguard/ui/view/layout/sections/SmartspaceSectionTest.kt b/packages/SystemUI/tests/src/com/android/systemui/keyguard/ui/view/layout/sections/SmartspaceSectionTest.kt
index 02bafd0..bff27f6 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/keyguard/ui/view/layout/sections/SmartspaceSectionTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/keyguard/ui/view/layout/sections/SmartspaceSectionTest.kt
@@ -23,9 +23,8 @@
 import androidx.constraintlayout.widget.ConstraintSet.GONE
 import androidx.constraintlayout.widget.ConstraintSet.VISIBLE
 import androidx.test.filters.SmallTest
+import com.android.systemui.Flags
 import com.android.systemui.SysuiTestCase
-import com.android.systemui.flags.FakeFeatureFlagsClassic
-import com.android.systemui.flags.Flags.MIGRATE_CLOCKS_TO_BLUEPRINT
 import com.android.systemui.keyguard.KeyguardUnlockAnimationController
 import com.android.systemui.keyguard.ui.viewmodel.KeyguardClockViewModel
 import com.android.systemui.keyguard.ui.viewmodel.KeyguardSmartspaceViewModel
@@ -44,14 +43,12 @@
 @RunWith(JUnit4::class)
 @SmallTest
 class SmartspaceSectionTest : SysuiTestCase() {
-
     private lateinit var underTest: SmartspaceSection
     @Mock private lateinit var keyguardClockViewModel: KeyguardClockViewModel
     @Mock private lateinit var keyguardSmartspaceViewModel: KeyguardSmartspaceViewModel
     @Mock private lateinit var lockscreenSmartspaceController: LockscreenSmartspaceController
     @Mock private lateinit var keyguardUnlockAnimationController: KeyguardUnlockAnimationController
     @Mock private lateinit var hasCustomWeatherDataDisplay: StateFlow<Boolean>
-    private lateinit var mFakeFeatureFlags: FakeFeatureFlagsClassic
 
     private val smartspaceView = View(mContext).also { it.id = View.generateViewId() }
     private val weatherView = View(mContext).also { it.id = View.generateViewId() }
@@ -62,8 +59,7 @@
     @Before
     fun setup() {
         MockitoAnnotations.initMocks(this)
-        mFakeFeatureFlags = FakeFeatureFlagsClassic()
-        mFakeFeatureFlags.set(MIGRATE_CLOCKS_TO_BLUEPRINT, true)
+        mSetFlagsRule.enableFlags(Flags.FLAG_MIGRATE_CLOCKS_TO_BLUEPRINT)
         underTest =
             SmartspaceSection(
                 keyguardClockViewModel,
@@ -71,7 +67,6 @@
                 mContext,
                 lockscreenSmartspaceController,
                 keyguardUnlockAnimationController,
-                mFakeFeatureFlags
             )
         constraintLayout = ConstraintLayout(mContext)
         whenever(lockscreenSmartspaceController.buildAndConnectView(constraintLayout))
diff --git a/packages/SystemUI/tests/src/com/android/systemui/keyguard/ui/viewmodel/KeyguardRootViewModelTest.kt b/packages/SystemUI/tests/src/com/android/systemui/keyguard/ui/viewmodel/KeyguardRootViewModelTest.kt
index 23a2709..58624d3 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/keyguard/ui/viewmodel/KeyguardRootViewModelTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/keyguard/ui/viewmodel/KeyguardRootViewModelTest.kt
@@ -30,7 +30,6 @@
 import com.android.systemui.coroutines.collectLastValue
 import com.android.systemui.deviceentry.data.repository.fakeDeviceEntryRepository
 import com.android.systemui.deviceentry.domain.interactor.deviceEntryInteractor
-import com.android.systemui.flags.Flags
 import com.android.systemui.flags.featureFlagsClassic
 import com.android.systemui.keyguard.data.repository.fakeKeyguardRepository
 import com.android.systemui.keyguard.data.repository.fakeKeyguardTransitionRepository
@@ -72,10 +71,7 @@
 @SmallTest
 @RunWith(JUnit4::class)
 class KeyguardRootViewModelTest : SysuiTestCase() {
-    private val kosmos =
-        testKosmos().apply {
-            featureFlagsClassic.apply { set(Flags.MIGRATE_CLOCKS_TO_BLUEPRINT, false) }
-        }
+    private val kosmos = testKosmos()
     private val testScope = kosmos.testScope
     private val repository = kosmos.fakeKeyguardRepository
     private val configurationRepository = kosmos.fakeConfigurationRepository
@@ -110,6 +106,7 @@
 
         mSetFlagsRule.enableFlags(AConfigFlags.FLAG_KEYGUARD_BOTTOM_AREA_REFACTOR)
         mSetFlagsRule.enableFlags(FLAG_NEW_AOD_TRANSITION)
+        mSetFlagsRule.disableFlags(AConfigFlags.FLAG_MIGRATE_CLOCKS_TO_BLUEPRINT)
 
         whenever(goneToAodTransitionViewModel.enterFromTopTranslationY(anyInt()))
             .thenReturn(emptyFlow<Float>())
diff --git a/packages/SystemUI/tests/src/com/android/systemui/shade/NotificationPanelViewControllerBaseTest.java b/packages/SystemUI/tests/src/com/android/systemui/shade/NotificationPanelViewControllerBaseTest.java
index daf0654..657f912 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/shade/NotificationPanelViewControllerBaseTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/shade/NotificationPanelViewControllerBaseTest.java
@@ -385,10 +385,10 @@
         mFeatureFlags.set(Flags.TRACKPAD_GESTURE_FEATURES, false);
         mFeatureFlags.set(Flags.LOCKSCREEN_ENABLE_LANDSCAPE, false);
         mFeatureFlags.set(Flags.QS_USER_DETAIL_SHORTCUT, false);
-        mFeatureFlags.set(Flags.MIGRATE_CLOCKS_TO_BLUEPRINT, false);
 
         mSetFlagsRule.disableFlags(KeyguardShadeMigrationNssl.FLAG_NAME);
         mSetFlagsRule.disableFlags(com.android.systemui.Flags.FLAG_KEYGUARD_BOTTOM_AREA_REFACTOR);
+        mSetFlagsRule.disableFlags(com.android.systemui.Flags.FLAG_MIGRATE_CLOCKS_TO_BLUEPRINT);
 
         mMainDispatcher = getMainDispatcher();
         KeyguardInteractorFactory.WithDependencies keyguardInteractorDeps =
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/events/SystemEventCoordinatorTest.kt b/packages/SystemUI/tests/src/com/android/systemui/statusbar/events/SystemEventCoordinatorTest.kt
index bbc63f2..ae84df5 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/events/SystemEventCoordinatorTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/events/SystemEventCoordinatorTest.kt
@@ -21,7 +21,6 @@
 import com.android.systemui.SysuiTestCase
 import com.android.systemui.display.domain.interactor.ConnectedDisplayInteractor
 import com.android.systemui.display.domain.interactor.ConnectedDisplayInteractor.PendingDisplay
-import com.android.systemui.display.domain.interactor.ConnectedDisplayInteractor.State.CONNECTED
 import com.android.systemui.privacy.PrivacyItemController
 import com.android.systemui.statusbar.policy.BatteryController
 import com.android.systemui.util.mockito.any
@@ -107,5 +106,7 @@
             get() = flow
         override val pendingDisplay: Flow<PendingDisplay?>
             get() = MutableSharedFlow<PendingDisplay>()
+        override val concurrentDisplaysInProgress: Flow<Boolean>
+            get() = TODO("Not yet implemented")
     }
 }
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicyTest.kt b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicyTest.kt
index da6c28a..7deee5a 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicyTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicyTest.kt
@@ -321,5 +321,7 @@
             get() = TODO("Not yet implemented")
         override val pendingDisplay: Flow<PendingDisplay?>
             get() = TODO("Not yet implemented")
+        override val concurrentDisplaysInProgress: Flow<Boolean>
+            get() = TODO("Not yet implemented")
     }
 }
diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/display/data/repository/FakeDeviceStateRepository.kt b/packages/SystemUI/tests/utils/src/com/android/systemui/display/data/repository/FakeDeviceStateRepository.kt
new file mode 100644
index 0000000..5f6dc6e
--- /dev/null
+++ b/packages/SystemUI/tests/utils/src/com/android/systemui/display/data/repository/FakeDeviceStateRepository.kt
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2023 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.display.data.repository
+
+import kotlinx.coroutines.flow.MutableStateFlow
+import kotlinx.coroutines.flow.StateFlow
+
+/** Fake [DeviceStateRepository] implementation for testing. */
+class FakeDeviceStateRepository : DeviceStateRepository {
+    private val flow = MutableStateFlow(DeviceStateRepository.DeviceState.UNKNOWN)
+
+    /** Emits [value] as [displays] flow value. */
+    suspend fun emit(value: DeviceStateRepository.DeviceState) = flow.emit(value)
+
+    override val state: StateFlow<DeviceStateRepository.DeviceState>
+        get() = flow
+}
diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/keyguard/data/repository/FakeKeyguardRepository.kt b/packages/SystemUI/tests/utils/src/com/android/systemui/keyguard/data/repository/FakeKeyguardRepository.kt
index 81a7bec..0e7c662 100644
--- a/packages/SystemUI/tests/utils/src/com/android/systemui/keyguard/data/repository/FakeKeyguardRepository.kt
+++ b/packages/SystemUI/tests/utils/src/com/android/systemui/keyguard/data/repository/FakeKeyguardRepository.kt
@@ -119,6 +119,8 @@
     private val _keyguardAlpha = MutableStateFlow(1f)
     override val keyguardAlpha: StateFlow<Float> = _keyguardAlpha
 
+    override val lastRootViewTapPosition: MutableStateFlow<Point?> = MutableStateFlow(null)
+
     override fun setQuickSettingsVisible(isVisible: Boolean) {
         _isQuickSettingsVisible.value = isVisible
     }
diff --git a/packages/services/CameraExtensionsProxy/src/com/android/cameraextensions/CameraExtensionsProxyService.java b/packages/services/CameraExtensionsProxy/src/com/android/cameraextensions/CameraExtensionsProxyService.java
index 502ee4d..b315f4a 100644
--- a/packages/services/CameraExtensionsProxy/src/com/android/cameraextensions/CameraExtensionsProxyService.java
+++ b/packages/services/CameraExtensionsProxy/src/com/android/cameraextensions/CameraExtensionsProxyService.java
@@ -1329,7 +1329,7 @@
         }
 
         @Override
-        public void onCaptureSessionStart(IRequestProcessorImpl requestProcessor) {
+        public void onCaptureSessionStart(IRequestProcessorImpl requestProcessor, String statsKey) {
             mSessionProcessor.onCaptureSessionStart(
                     new RequestProcessorStub(requestProcessor, mCameraId));
         }
diff --git a/ravenwood/ravenwood-annotation-allowed-classes.txt b/ravenwood/ravenwood-annotation-allowed-classes.txt
index 2902932..468b7c9 100644
--- a/ravenwood/ravenwood-annotation-allowed-classes.txt
+++ b/ravenwood/ravenwood-annotation-allowed-classes.txt
@@ -41,6 +41,17 @@
 android.content.IntentFilter
 android.content.UriMatcher
 
+android.content.pm.PackageInfo
+android.content.pm.ApplicationInfo
+android.content.pm.PackageItemInfo
+android.content.pm.ComponentInfo
+android.content.pm.ActivityInfo
+android.content.pm.ServiceInfo
+android.content.pm.PathPermission
+android.content.pm.ProviderInfo
+android.content.pm.ResolveInfo
+android.content.pm.Signature
+
 android.database.AbstractCursor
 android.database.CharArrayBuffer
 android.database.ContentObservable
diff --git a/services/appwidget/java/com/android/server/appwidget/AppWidgetServiceImpl.java b/services/appwidget/java/com/android/server/appwidget/AppWidgetServiceImpl.java
index 258820a..77a5e3d 100644
--- a/services/appwidget/java/com/android/server/appwidget/AppWidgetServiceImpl.java
+++ b/services/appwidget/java/com/android/server/appwidget/AppWidgetServiceImpl.java
@@ -43,7 +43,9 @@
 import android.app.PendingIntent;
 import android.app.admin.DevicePolicyManagerInternal;
 import android.app.admin.DevicePolicyManagerInternal.OnCrossProfileWidgetProvidersChangeListener;
+import android.app.usage.Flags;
 import android.app.usage.UsageEvents;
+import android.app.usage.UsageStatsManager;
 import android.app.usage.UsageStatsManagerInternal;
 import android.appwidget.AppWidgetManager;
 import android.appwidget.AppWidgetManagerInternal;
@@ -83,6 +85,7 @@
 import android.os.IBinder;
 import android.os.Looper;
 import android.os.Message;
+import android.os.PersistableBundle;
 import android.os.Process;
 import android.os.RemoteException;
 import android.os.SystemClock;
@@ -3815,14 +3818,27 @@
                 final SparseArray<String> uid2PackageName = new SparseArray<String>();
                 uid2PackageName.put(providerId.uid, packageName);
                 mAppOpsManagerInternal.updateAppWidgetVisibility(uid2PackageName, true);
-                mUsageStatsManagerInternal.reportEvent(packageName,
-                        UserHandle.getUserId(providerId.uid), UsageEvents.Event.USER_INTERACTION);
+                reportWidgetInteractionEvent(packageName, UserHandle.getUserId(providerId.uid),
+                        "tap");
             }
         } finally {
             Binder.restoreCallingIdentity(ident);
         }
     }
 
+    private void reportWidgetInteractionEvent(@NonNull String packageName, @UserIdInt int userId,
+            @NonNull String action) {
+        if (Flags.userInteractionTypeApi()) {
+            PersistableBundle extras = new PersistableBundle();
+            extras.putString(UsageStatsManager.EXTRA_EVENT_CATEGORY, "android.appwidget");
+            extras.putString(UsageStatsManager.EXTRA_EVENT_ACTION, action);
+            mUsageStatsManagerInternal.reportUserInteractionEvent(packageName, userId, extras);
+        } else {
+            mUsageStatsManagerInternal.reportEvent(packageName, userId,
+                    UsageEvents.Event.USER_INTERACTION);
+        }
+    }
+
     private final class CallbackHandler extends Handler {
         public static final int MSG_NOTIFY_UPDATE_APP_WIDGET = 1;
         public static final int MSG_NOTIFY_PROVIDER_CHANGED = 2;
diff --git a/services/autofill/java/com/android/server/autofill/AutofillManagerServiceImpl.java b/services/autofill/java/com/android/server/autofill/AutofillManagerServiceImpl.java
index 518b81f..fd8ab96 100644
--- a/services/autofill/java/com/android/server/autofill/AutofillManagerServiceImpl.java
+++ b/services/autofill/java/com/android/server/autofill/AutofillManagerServiceImpl.java
@@ -19,7 +19,7 @@
 import static android.service.autofill.FillEventHistory.Event.NO_SAVE_UI_REASON_NONE;
 import static android.service.autofill.FillEventHistory.Event.UI_TYPE_INLINE;
 import static android.service.autofill.FillRequest.FLAG_MANUAL_REQUEST;
-import static android.service.autofill.FillRequest.FLAG_SCREEN_HAS_CREDMAN_FIELD;
+import static android.service.autofill.FillRequest.FLAG_VIEW_REQUESTS_CREDMAN_SERVICE;
 import static android.view.autofill.AutofillManager.ACTION_START_SESSION;
 import static android.view.autofill.AutofillManager.FLAG_ADD_CLIENT_ENABLED;
 import static android.view.autofill.AutofillManager.FLAG_ADD_CLIENT_ENABLED_FOR_AUGMENTED_AUTOFILL_ONLY;
@@ -104,10 +104,6 @@
         extends AbstractPerUserSystemService<AutofillManagerServiceImpl, AutofillManagerService> {
 
     private static final String TAG = "AutofillManagerServiceImpl";
-
-    private static final ComponentName CREDMAN_SERVICE_COMPONENT_NAME =
-            new ComponentName("com.android.credentialmanager",
-                    "com.android.credentialmanager.autofill.CredentialAutofillService");
     private static final int MAX_SESSION_ID_CREATE_TRIES = 2048;
 
     /** Minimum interval to prune abandoned sessions */
@@ -536,22 +532,15 @@
                 || mSessions.indexOfKey(sessionId) >= 0);
 
         assertCallerLocked(clientActivity, compatMode);
-
         ComponentName serviceComponentName = mInfo == null ? null
                 : mInfo.getServiceInfo().getComponentName();
-
-        if (isAutofillCredmanIntegrationEnabled()
-                && ((flags & FLAG_SCREEN_HAS_CREDMAN_FIELD) != 0)) {
-            // Hardcode to credential manager proxy service
-            Slog.i(TAG, "Routing to CredentialAutofillService");
-            serviceComponentName = CREDMAN_SERVICE_COMPONENT_NAME;
-        }
+        boolean isPrimaryCredential = (flags & FLAG_VIEW_REQUESTS_CREDMAN_SERVICE) != 0;
 
         final Session newSession = new Session(this, mUi, getContext(), mHandler, mUserId, mLock,
                 sessionId, taskId, clientUid, clientActivityToken, clientCallback, hasCallback,
                 mUiLatencyHistory, mWtfHistory, serviceComponentName,
                 clientActivity, compatMode, bindInstantServiceAllowed, forAugmentedAutofillOnly,
-                flags, mInputMethodManagerInternal);
+                flags, mInputMethodManagerInternal, isPrimaryCredential);
         mSessions.put(newSession.id, newSession);
 
         return newSession;
diff --git a/services/autofill/java/com/android/server/autofill/SecondaryProviderHandler.java b/services/autofill/java/com/android/server/autofill/SecondaryProviderHandler.java
new file mode 100644
index 0000000..d9741c8
--- /dev/null
+++ b/services/autofill/java/com/android/server/autofill/SecondaryProviderHandler.java
@@ -0,0 +1,121 @@
+/*
+ * Copyright (C) 2016 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.autofill;
+
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.IntentSender;
+import android.service.autofill.FillRequest;
+import android.service.autofill.FillResponse;
+import android.util.Slog;
+
+import java.util.Objects;
+
+
+/**
+ * Requests autofill response from a Remote Autofill Service. This autofill service can be
+ * either a Credential Autofill Service or the user-opted autofill service.
+ *
+ * <p> With the credman integration, Autofill Framework handles two types of autofill flows -
+ * regular autofill flow and the credman integrated autofill flow. With the credman integrated
+ * autofill, the data source for the autofill is handled by the credential autofill proxy
+ * service, which is hidden from users. By the time a session gets created, the framework
+ * decides on one of the two flows by setting the remote fill service to be either the
+ * user-elected autofill service or the hidden credential autofill service by looking at the
+ * user-focused view's credential attribute. If the user needs both flows concurrently because
+ * the screen has both regular autofill fields and credential fields, then secondary provider
+ * handler will be used to fetch supplementary fill response. Depending on which remote fill
+ * service the session was initially created with, the secondary provider handler will contain
+ * the remaining autofill service. </p>
+ *
+ * @hide
+ */
+final class SecondaryProviderHandler implements RemoteFillService.FillServiceCallbacks {
+    private static final String TAG = "SecondaryProviderHandler";
+
+    private final RemoteFillService mRemoteFillService;
+    private final SecondaryProviderCallback mCallback;
+    private FillRequest mLastFillRequest;
+    private int mLastFlag;
+
+    SecondaryProviderHandler(
+            @NonNull Context context, int userId, boolean bindInstantServiceAllowed,
+            SecondaryProviderCallback callback, ComponentName componentName) {
+        mRemoteFillService = new RemoteFillService(context, componentName, userId, this,
+                bindInstantServiceAllowed);
+        mCallback = callback;
+        Slog.v(TAG, "Creating a secondary provider handler with component name, " + componentName);
+    }
+    @Override
+    public void onServiceDied(RemoteFillService service) {
+        mRemoteFillService.destroy();
+    }
+
+    @Override
+    public void onFillRequestSuccess(int requestId, @Nullable FillResponse response,
+                                     @NonNull String servicePackageName, int requestFlags) {
+        Slog.v(TAG, "Received a fill response: " + response);
+        mCallback.onSecondaryFillResponse(response, mLastFlag);
+    }
+
+    @Override
+    public void onFillRequestFailure(int requestId, @Nullable CharSequence message) {
+
+    }
+
+    @Override
+    public void onFillRequestTimeout(int requestId) {
+
+    }
+
+    @Override
+    public void onSaveRequestSuccess(@NonNull String servicePackageName,
+                                     @Nullable IntentSender intentSender) {
+
+    }
+
+    @Override
+    public void onSaveRequestFailure(@Nullable CharSequence message,
+                                     @NonNull String servicePackageName) {
+
+    }
+
+    /**
+     * Requests a new fill response. If the fill request is same as the last requested fill request,
+     * then the request is duped.
+     */
+    public void onFillRequest(FillRequest pendingFillRequest, int flag) {
+        if (Objects.equals(pendingFillRequest, mLastFillRequest)) {
+            Slog.v(TAG, "Deduping fill request to secondary provider.");
+            return;
+        }
+        Slog.v(TAG, "Requesting fill response to secondary provider.");
+        mLastFlag = flag;
+        mLastFillRequest = pendingFillRequest;
+        mRemoteFillService.onFillRequest(pendingFillRequest);
+    }
+
+    public void destroy() {
+        mRemoteFillService.destroy();
+    }
+
+    interface SecondaryProviderCallback {
+        void onSecondaryFillResponse(@Nullable FillResponse fillResponse, int flags);
+    }
+}
diff --git a/services/autofill/java/com/android/server/autofill/Session.java b/services/autofill/java/com/android/server/autofill/Session.java
index 07e9c50..d527ce0 100644
--- a/services/autofill/java/com/android/server/autofill/Session.java
+++ b/services/autofill/java/com/android/server/autofill/Session.java
@@ -35,6 +35,7 @@
 import static android.service.autofill.FillRequest.FLAG_SCREEN_HAS_CREDMAN_FIELD;
 import static android.service.autofill.FillRequest.FLAG_SUPPORTS_FILL_DIALOG;
 import static android.service.autofill.FillRequest.FLAG_VIEW_NOT_FOCUSED;
+import static android.service.autofill.FillRequest.FLAG_VIEW_REQUESTS_CREDMAN_SERVICE;
 import static android.service.autofill.FillRequest.INVALID_REQUEST_ID;
 import static android.view.autofill.AutofillManager.ACTION_RESPONSE_EXPIRED;
 import static android.view.autofill.AutofillManager.ACTION_START_SESSION;
@@ -228,6 +229,10 @@
     private static final int DEFAULT__FILL_REQUEST_ID_SNAPSHOT = -2;
     private static final int DEFAULT__FIELD_CLASSIFICATION_REQUEST_ID_SNAPSHOT = -2;
 
+    private static final ComponentName CREDMAN_SERVICE_COMPONENT_NAME =
+            new ComponentName("com.android.credentialmanager",
+                    "com.android.credentialmanager.autofill.CredentialAutofillService");
+
     final Object mLock;
 
     private final AutofillManagerServiceImpl mService;
@@ -343,6 +348,22 @@
     @Nullable
     private final RemoteFillService mRemoteFillService;
 
+    /**
+     * With the credman integration, Autofill Framework handles two types of autofill flows -
+     * regular autofill flow and the credman integrated autofill flow. With the credman integrated
+     * autofill, the data source for the autofill is handled by the credential autofill proxy
+     * service, which is hidden from users. By the time a session gets created, the framework
+     * decides on one of the two flows by setting the remote fill service to be either the
+     * user-elected autofill service or the hidden credential autofill service by looking at the
+     * user-focused view's credential attribute. If the user needs both flows concurrently because
+     * the screen has both regular autofill fields and credential fields, then secondary provider
+     * handler will be used to fetch supplementary fill response. Depending on which remote fill
+     * service the session was initially created with, the secondary provider handler will contain
+     * the remaining autofill service.
+     */
+    @Nullable
+    private final SecondaryProviderHandler mSecondaryProviderHandler;
+
     @GuardedBy("mLock")
     private SparseArray<FillResponse> mResponses;
 
@@ -358,6 +379,9 @@
      */
     private boolean mHasCallback;
 
+    /** Whether the session has credential provider as the primary provider. */
+    private boolean mIsPrimaryCredential;
+
     @GuardedBy("mLock")
     private boolean mDelayedFillBroadcastReceiverRegistered;
 
@@ -689,7 +713,6 @@
                         mPendingFillRequest.getDelayedFillIntentSender());
             }
             mLastFillRequest = mPendingFillRequest;
-
             mRemoteFillService.onFillRequest(mPendingFillRequest);
             mPendingInlineSuggestionsRequest = null;
             mWaitForInlineRequest = false;
@@ -776,7 +799,7 @@
                                         + mUrlBar.getWebDomain());
                             }
                             final ViewState viewState = new ViewState(urlBarId, Session.this,
-                                    ViewState.STATE_URL_BAR);
+                                    ViewState.STATE_URL_BAR, mIsPrimaryCredential);
                             mViewStates.put(urlBarId, viewState);
                         }
                     }
@@ -1187,7 +1210,8 @@
             setViewStatesLocked(
                     existingResponse,
                     ViewState.STATE_INITIAL,
-                    /* clearResponse= */ true);
+                    /* clearResponse= */ true,
+                    /* isPrimary= */ true);
             mFillRequestEventLogger.maybeSetRequestTriggerReason(
                     TRIGGER_REASON_SERVED_FROM_CACHED_RESPONSE);
         }
@@ -1352,7 +1376,8 @@
             @NonNull LocalLog wtfHistory, @Nullable ComponentName serviceComponentName,
             @NonNull ComponentName componentName, boolean compatMode,
             boolean bindInstantServiceAllowed, boolean forAugmentedAutofillOnly, int flags,
-            @NonNull InputMethodManagerInternal inputMethodManagerInternal) {
+            @NonNull InputMethodManagerInternal inputMethodManagerInternal,
+            boolean isPrimaryCredential) {
         if (sessionId < 0) {
             wtf(null, "Non-positive sessionId: %s", sessionId);
         }
@@ -1365,9 +1390,24 @@
         mLock = lock;
         mUi = ui;
         mHandler = handler;
-        mRemoteFillService = serviceComponentName == null ? null
-                : new RemoteFillService(context, serviceComponentName, userId, this,
+
+        ComponentName primaryServiceComponentName, secondaryServiceComponentName;
+        if (isPrimaryCredential) {
+            primaryServiceComponentName = CREDMAN_SERVICE_COMPONENT_NAME;
+            secondaryServiceComponentName = serviceComponentName;
+        } else {
+            primaryServiceComponentName = serviceComponentName;
+            secondaryServiceComponentName = CREDMAN_SERVICE_COMPONENT_NAME;
+        }
+        Slog.v(TAG, "Primary service component name: " + primaryServiceComponentName
+                + ", secondary service component name: " + secondaryServiceComponentName);
+
+        mRemoteFillService = primaryServiceComponentName == null ? null
+                : new RemoteFillService(context, primaryServiceComponentName, userId, this,
                         bindInstantServiceAllowed);
+        mSecondaryProviderHandler = secondaryServiceComponentName == null ? null
+                : new SecondaryProviderHandler(context, userId, bindInstantServiceAllowed,
+                this::onSecondaryFillResponse, secondaryServiceComponentName);
         mActivityToken = activityToken;
         mHasCallback = hasCallback;
         mUiLatencyHistory = uiLatencyHistory;
@@ -1389,6 +1429,7 @@
         mSessionCommittedEventLogger = SessionCommittedEventLogger.forSessionId(sessionId);
         mSessionCommittedEventLogger.maybeSetComponentPackageUid(uid);
         mSaveEventLogger = SaveEventLogger.forSessionId(sessionId);
+        mIsPrimaryCredential = isPrimaryCredential;
 
         synchronized (mLock) {
             mSessionFlags = new SessionFlags();
@@ -1758,6 +1799,22 @@
         return createShallowCopy(response, resultContainer);
     }
 
+    private void onSecondaryFillResponse(@Nullable FillResponse fillResponse, int flags) {
+        if (fillResponse == null) {
+            return;
+        }
+        synchronized (mLock) {
+            setViewStatesLocked(fillResponse, ViewState.STATE_FILLABLE, /* clearResponse= */ false,
+                    /* isPrimary= */ false);
+
+            // Updates the UI, if necessary.
+            final ViewState currentView = mViewStates.get(mCurrentViewId);
+            if (currentView != null) {
+                currentView.maybeCallOnFillReady(flags);
+            }
+        }
+    }
+
     private FillResponse createShallowCopy(
             FillResponse response, DatasetComputationContainer container) {
         return FillResponse.shallowCopy(
@@ -4008,6 +4065,17 @@
         return true;
     }
 
+    boolean shouldRequestSecondaryProvider(int flags) {
+        if (mIsPrimaryCredential) {
+            return (flags & FLAG_VIEW_REQUESTS_CREDMAN_SERVICE) == 0;
+        } else {
+            return (flags & FLAG_VIEW_REQUESTS_CREDMAN_SERVICE) != 0;
+        }
+    }
+
+    // ErrorProne says mAssistReceiver#mLastFillRequest needs to be guarded by
+    // 'Session.this.mLock', which is the same as mLock.
+    @SuppressWarnings("GuardedBy")
     @GuardedBy("mLock")
     void updateLocked(AutofillId id, Rect virtualBounds, AutofillValue value, int action,
             int flags) {
@@ -4045,7 +4113,8 @@
                 if (sVerbose) Slog.v(TAG, "Creating viewState for " + id);
                 boolean isIgnored = isIgnoredLocked(id);
                 viewState = new ViewState(id, this,
-                        isIgnored ? ViewState.STATE_IGNORED : ViewState.STATE_INITIAL);
+                        isIgnored ? ViewState.STATE_IGNORED : ViewState.STATE_INITIAL,
+                        mIsPrimaryCredential);
                 mViewStates.put(id, viewState);
 
                 // TODO(b/73648631): for optimization purposes, should also ignore if change is
@@ -4136,6 +4205,12 @@
                 }
                 break;
             case ACTION_VIEW_ENTERED:
+                if (shouldRequestSecondaryProvider(flags)
+                        && mSecondaryProviderHandler != null
+                        && mAssistReceiver.mLastFillRequest != null) {
+                    mSecondaryProviderHandler.onFillRequest(mAssistReceiver.mLastFillRequest,
+                            flags);
+                }
                 mLatencyBaseTime = SystemClock.elapsedRealtime();
                 boolean wasPreviouslyFillDialog = mPreviouslyFillDialogPotentiallyStarted;
                 mPreviouslyFillDialogPotentiallyStarted = false;
@@ -4911,7 +4986,8 @@
     private void replaceResponseLocked(@NonNull FillResponse oldResponse,
             @NonNull FillResponse newResponse, @Nullable Bundle newClientState) {
         // Disassociate view states with the old response
-        setViewStatesLocked(oldResponse, ViewState.STATE_INITIAL, true);
+        setViewStatesLocked(oldResponse, ViewState.STATE_INITIAL, /* clearResponse= */ true,
+                /* isPrimary= */ true);
         // Move over the id
         newResponse.setRequestId(oldResponse.getRequestId());
         // Now process the new response
@@ -5308,7 +5384,8 @@
         mPresentationStatsEventLogger.maybeSetAvailableCount(datasetList, mCurrentViewId);
         mFillResponseEventLogger.maybeSetDatasetsCountAfterPotentialPccFiltering(datasetList);
 
-        setViewStatesLocked(newResponse, ViewState.STATE_FILLABLE, false);
+        setViewStatesLocked(newResponse, ViewState.STATE_FILLABLE, /* clearResponse= */ false,
+                /* isPrimary= */ true);
         updateFillDialogTriggerIdsLocked();
         updateTrackedIdsLocked();
 
@@ -5325,7 +5402,8 @@
      * Sets the state of all views in the given response.
      */
     @GuardedBy("mLock")
-    private void setViewStatesLocked(FillResponse response, int state, boolean clearResponse) {
+    private void setViewStatesLocked(FillResponse response, int state, boolean clearResponse,
+                                     boolean isPrimary) {
         final List<Dataset> datasets = response.getDatasets();
         if (datasets != null && !datasets.isEmpty()) {
             for (int i = 0; i < datasets.size(); i++) {
@@ -5334,15 +5412,15 @@
                     Slog.w(TAG, "Ignoring null dataset on " + datasets);
                     continue;
                 }
-                setViewStatesLocked(response, dataset, state, clearResponse);
+                setViewStatesLocked(response, dataset, state, clearResponse, isPrimary);
             }
         } else if (response.getAuthentication() != null) {
             for (AutofillId autofillId : response.getAuthenticationIds()) {
                 final ViewState viewState = createOrUpdateViewStateLocked(autofillId, state, null);
                 if (!clearResponse) {
-                    viewState.setResponse(response);
+                    viewState.setResponse(response, isPrimary);
                 } else {
-                    viewState.setResponse(null);
+                    viewState.setResponse(null, isPrimary);
                 }
             }
         }
@@ -5375,7 +5453,7 @@
      */
     @GuardedBy("mLock")
     private void setViewStatesLocked(@Nullable FillResponse response, @NonNull Dataset dataset,
-            int state, boolean clearResponse) {
+            int state, boolean clearResponse, boolean isPrimary) {
         final ArrayList<AutofillId> ids = dataset.getFieldIds();
         final ArrayList<AutofillValue> values = dataset.getFieldValues();
         for (int j = 0; j < ids.size(); j++) {
@@ -5387,9 +5465,9 @@
                 viewState.setDatasetId(datasetId);
             }
             if (clearResponse) {
-                viewState.setResponse(null);
+                viewState.setResponse(null, isPrimary);
             } else if (response != null) {
-                viewState.setResponse(response);
+                viewState.setResponse(response, isPrimary);
             }
         }
     }
@@ -5401,7 +5479,7 @@
         if (viewState != null)  {
             viewState.setState(state);
         } else {
-            viewState = new ViewState(id, this, state);
+            viewState = new ViewState(id, this, state, mIsPrimaryCredential);
             if (sVerbose) {
                 Slog.v(TAG, "Adding autofillable view with id " + id + " and state " + state);
             }
@@ -5446,7 +5524,9 @@
             mService.logDatasetAuthenticationSelected(dataset.getId(), id, mClientState, uiType);
             mPresentationStatsEventLogger.maybeSetAuthenticationType(
                 AUTHENTICATION_TYPE_DATASET_AUTHENTICATION);
-            setViewStatesLocked(null, dataset, ViewState.STATE_WAITING_DATASET_AUTH, false);
+            // does not matter the value of isPrimary because null response won't be overridden.
+            setViewStatesLocked(null, dataset, ViewState.STATE_WAITING_DATASET_AUTH,
+                    /* clearResponse= */ false, /* isPrimary= */ true);
             final Intent fillInIntent = createAuthFillInIntentLocked(requestId, mClientState,
                     dataset.getAuthenticationExtras());
             if (fillInIntent == null) {
@@ -6044,7 +6124,10 @@
                         }
                         mSelectedDatasetIds.add(dataset.getId());
                     }
-                    setViewStatesLocked(null, dataset, ViewState.STATE_AUTOFILLED, false);
+                    // does not matter the value of isPrimary because null response won't be
+                    // overridden.
+                    setViewStatesLocked(null, dataset, ViewState.STATE_AUTOFILLED,
+                            /* clearResponse= */ false, /* isPrimary= */ true);
                 }
             } catch (RemoteException e) {
                 Slog.w(TAG, "Error autofilling activity: " + e);
@@ -6222,6 +6305,9 @@
         if (remoteFillService != null) {
             remoteFillService.destroy();
         }
+        if (mSecondaryProviderHandler != null) {
+            mSecondaryProviderHandler.destroy();
+        }
         mSessionState = STATE_REMOVED;
     }
 
diff --git a/services/autofill/java/com/android/server/autofill/ViewState.java b/services/autofill/java/com/android/server/autofill/ViewState.java
index 12695ac..b0bb9ec 100644
--- a/services/autofill/java/com/android/server/autofill/ViewState.java
+++ b/services/autofill/java/com/android/server/autofill/ViewState.java
@@ -17,6 +17,7 @@
 package com.android.server.autofill;
 
 import static android.service.autofill.FillRequest.FLAG_MANUAL_REQUEST;
+import static android.service.autofill.FillRequest.FLAG_VIEW_REQUESTS_CREDMAN_SERVICE;
 
 import static com.android.server.autofill.Helper.sDebug;
 
@@ -89,7 +90,21 @@
 
     private final Listener mListener;
 
-    private FillResponse mResponse;
+    private final boolean mIsPrimaryCredential;
+
+    /**
+     * There are two sources of fill response. The fill response from the session's remote fill
+     * service and the fill response from the secondary provider handler. Primary Fill Response
+     * stores the fill response from the session's remote fill service.
+     */
+    private FillResponse mPrimaryFillResponse;
+
+    /**
+     * Secondary fill response stores the fill response from the secondary provider handler. Based
+     * on whether the user focuses on a credential view or an autofill view, the relevant fill
+     * response will be used to show the autofill suggestions.
+     */
+    private FillResponse mSecondaryFillResponse;
     private AutofillValue mCurrentValue;
     private AutofillValue mAutofilledValue;
     private AutofillValue mSanitizedValue;
@@ -97,10 +112,11 @@
     private int mState;
     private String mDatasetId;
 
-    ViewState(AutofillId id, Listener listener, int state) {
+    ViewState(AutofillId id, Listener listener, int state, boolean isPrimaryCredential) {
         this.id = id;
         mListener = listener;
         mState = state;
+        mIsPrimaryCredential = isPrimaryCredential;
     }
 
     /**
@@ -143,11 +159,19 @@
 
     @Nullable
     FillResponse getResponse() {
-        return mResponse;
+        return mPrimaryFillResponse;
     }
 
     void setResponse(FillResponse response) {
-        mResponse = response;
+        setResponse(response, /* isPrimary= */ true);
+    }
+
+    void setResponse(@Nullable FillResponse response, boolean isPrimary) {
+        if (isPrimary) {
+            mPrimaryFillResponse = response;
+        } else {
+            mSecondaryFillResponse = response;
+        }
     }
 
     int getState() {
@@ -211,13 +235,24 @@
             return;
         }
         // First try the current response associated with this View.
-        if (mResponse != null) {
-            if (mResponse.getDatasets() != null || mResponse.getAuthentication() != null) {
-                mListener.onFillReady(mResponse, this.id, mCurrentValue, flags);
+        FillResponse requestedResponse = requestingPrimaryResponse(flags)
+                ? mPrimaryFillResponse : mSecondaryFillResponse;
+        if (requestedResponse != null) {
+            if (requestedResponse.getDatasets() != null
+                    || requestedResponse.getAuthentication() != null) {
+                mListener.onFillReady(requestedResponse, this.id, mCurrentValue, flags);
             }
         }
     }
 
+    private boolean requestingPrimaryResponse(int flags) {
+        if (mIsPrimaryCredential) {
+            return (flags & FLAG_VIEW_REQUESTS_CREDMAN_SERVICE) != 0;
+        } else {
+            return (flags & FLAG_VIEW_REQUESTS_CREDMAN_SERVICE) == 0;
+        }
+    }
+
     @Override
     public String toString() {
         final StringBuilder builder = new StringBuilder("ViewState: [id=").append(id);
@@ -247,8 +282,14 @@
             pw.print(prefix); pw.print("datasetId:" ); pw.println(mDatasetId);
         }
         pw.print(prefix); pw.print("state:" ); pw.println(getStateAsString());
-        if (mResponse != null) {
-            pw.print(prefix); pw.print("response id:");pw.println(mResponse.getRequestId());
+        pw.print(prefix); pw.print("is primary credential:"); pw.println(mIsPrimaryCredential);
+        if (mPrimaryFillResponse != null) {
+            pw.print(prefix); pw.print("primary response id:");
+            pw.println(mPrimaryFillResponse.getRequestId());
+        }
+        if (mSecondaryFillResponse != null) {
+            pw.print(prefix); pw.print("secondary response id:");
+            pw.println(mSecondaryFillResponse.getRequestId());
         }
         if (mCurrentValue != null) {
             pw.print(prefix); pw.print("currentValue:" ); pw.println(mCurrentValue);
diff --git a/services/core/Android.bp b/services/core/Android.bp
index 8ed3fd6..b4cf34e 100644
--- a/services/core/Android.bp
+++ b/services/core/Android.bp
@@ -201,6 +201,7 @@
         "biometrics_flags_lib",
         "am_flags_lib",
         "com_android_wm_shell_flags_lib",
+        "com.android.server.utils_aconfig-java",
         "service-jobscheduler-deviceidle.flags-aconfig-java",
     ],
     javac_shard_size: 50,
diff --git a/services/core/java/com/android/server/BinaryTransparencyService.java b/services/core/java/com/android/server/BinaryTransparencyService.java
index eb3ec24..05d07ae 100644
--- a/services/core/java/com/android/server/BinaryTransparencyService.java
+++ b/services/core/java/com/android/server/BinaryTransparencyService.java
@@ -1464,15 +1464,17 @@
         FrameworkStatsLog.write(FrameworkStatsLog.VBMETA_DIGEST_REPORTED, mVbmetaDigest);
 
         if (android.security.Flags.binaryTransparencySepolicyHash()) {
-            byte[] sepolicyHash = PackageUtils.computeSha256DigestForLargeFileAsBytes(
-                    "/sys/fs/selinux/policy", PackageUtils.createLargeFileBuffer());
-            String sepolicyHashEncoded = null;
-            if (sepolicyHash != null) {
-                sepolicyHashEncoded = HexEncoding.encodeToString(sepolicyHash, false);
-                Slog.d(TAG, "sepolicy hash: " + sepolicyHashEncoded);
-            }
-            FrameworkStatsLog.write(FrameworkStatsLog.BOOT_INTEGRITY_INFO_REPORTED,
-                    sepolicyHashEncoded, mVbmetaDigest);
+            IoThread.getExecutor().execute(() -> {
+                byte[] sepolicyHash = PackageUtils.computeSha256DigestForLargeFileAsBytes(
+                        "/sys/fs/selinux/policy", PackageUtils.createLargeFileBuffer());
+                String sepolicyHashEncoded = null;
+                if (sepolicyHash != null) {
+                    sepolicyHashEncoded = HexEncoding.encodeToString(sepolicyHash, false);
+                    Slog.d(TAG, "sepolicy hash: " + sepolicyHashEncoded);
+                }
+                FrameworkStatsLog.write(FrameworkStatsLog.BOOT_INTEGRITY_INFO_REPORTED,
+                        sepolicyHashEncoded, mVbmetaDigest);
+            });
         }
     }
 
diff --git a/services/core/java/com/android/server/Watchdog.java b/services/core/java/com/android/server/Watchdog.java
index 382ee6e..4bb9f4f 100644
--- a/services/core/java/com/android/server/Watchdog.java
+++ b/services/core/java/com/android/server/Watchdog.java
@@ -103,7 +103,7 @@
     // will be half the full timeout).
     //
     // The pre-watchdog event is similar to a full watchdog except it does not crash system server.
-    private static final int PRE_WATCHDOG_TIMEOUT_RATIO = 3;
+    private static final int PRE_WATCHDOG_TIMEOUT_RATIO = 4;
 
     // These are temporally ordered: larger values as lateness increases
     static final int COMPLETED = 0;
diff --git a/services/core/java/com/android/server/am/ActiveServices.java b/services/core/java/com/android/server/am/ActiveServices.java
index 5f1a7e7..7191684 100644
--- a/services/core/java/com/android/server/am/ActiveServices.java
+++ b/services/core/java/com/android/server/am/ActiveServices.java
@@ -241,6 +241,7 @@
 import com.android.server.am.ServiceRecord.ShortFgsInfo;
 import com.android.server.pm.KnownPackages;
 import com.android.server.uri.NeededUriGrants;
+import com.android.server.utils.AnrTimer;
 import com.android.server.wm.ActivityServiceConnectionsHolder;
 
 import java.io.FileDescriptor;
diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java
index 469f209..2d687de 100644
--- a/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -475,6 +475,7 @@
 import com.android.server.uri.GrantUri;
 import com.android.server.uri.NeededUriGrants;
 import com.android.server.uri.UriGrantsManagerInternal;
+import com.android.server.utils.AnrTimer;
 import com.android.server.utils.PriorityDump;
 import com.android.server.utils.Slogf;
 import com.android.server.utils.TimingsTraceAndSlog;
diff --git a/services/core/java/com/android/server/am/BroadcastQueueModernImpl.java b/services/core/java/com/android/server/am/BroadcastQueueModernImpl.java
index ad49991..2cac7a0 100644
--- a/services/core/java/com/android/server/am/BroadcastQueueModernImpl.java
+++ b/services/core/java/com/android/server/am/BroadcastQueueModernImpl.java
@@ -88,6 +88,7 @@
 import com.android.server.am.BroadcastProcessQueue.BroadcastConsumer;
 import com.android.server.am.BroadcastProcessQueue.BroadcastPredicate;
 import com.android.server.am.BroadcastRecord.DeliveryState;
+import com.android.server.utils.AnrTimer;
 
 import dalvik.annotation.optimization.NeverCompile;
 
diff --git a/services/core/java/com/android/server/am/flags.aconfig b/services/core/java/com/android/server/am/flags.aconfig
index 2ed079a..d9e8ddd 100644
--- a/services/core/java/com/android/server/am/flags.aconfig
+++ b/services/core/java/com/android/server/am/flags.aconfig
@@ -9,14 +9,6 @@
 }
 
 flag {
-     name: "anr_timer_service_enabled"
-     namespace: "system_performance"
-     is_fixed_read_only: true
-     description: "Feature flag for the ANR timer service"
-     bug: "282428924"
-}
-
-flag {
     name: "fgs_abuse_detection"
     namespace: "backstage_power"
     description: "Detect abusive FGS behavior for certain types (camera, mic, media, location)."
diff --git a/services/core/java/com/android/server/content/SyncJobService.java b/services/core/java/com/android/server/content/SyncJobService.java
index cd3f0f0..1da7f0c 100644
--- a/services/core/java/com/android/server/content/SyncJobService.java
+++ b/services/core/java/com/android/server/content/SyncJobService.java
@@ -19,7 +19,6 @@
 import android.annotation.Nullable;
 import android.app.job.JobParameters;
 import android.app.job.JobService;
-import android.content.pm.PackageManagerInternal;
 import android.os.Message;
 import android.os.SystemClock;
 import android.util.Log;
@@ -29,7 +28,6 @@
 import android.util.SparseLongArray;
 
 import com.android.internal.annotations.GuardedBy;
-import com.android.server.LocalServices;
 
 public class SyncJobService extends JobService {
     private static final String TAG = "SyncManager";
@@ -99,20 +97,6 @@
             return true;
         }
 
-        // TODO(b/209852664): remove this logic from here once it's added within JobScheduler.
-        // JobScheduler should not call onStartJob for syncs whose source packages are stopped.
-        // Until JS adds the relevant logic, this is a temporary solution to keep deferring syncs
-        // for packages in the stopped state.
-        if (android.content.pm.Flags.stayStopped()) {
-            if (LocalServices.getService(PackageManagerInternal.class)
-                    .isPackageStopped(op.owningPackage, op.target.userId)) {
-                if (Log.isLoggable(TAG, Log.DEBUG)) {
-                    Slog.d(TAG, "Skipping sync for force-stopped package: " + op.owningPackage);
-                }
-                return false;
-            }
-        }
-
         boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE);
         synchronized (sLock) {
             final int jobId = params.getJobId();
diff --git a/services/core/java/com/android/server/display/color/ColorDisplayService.java b/services/core/java/com/android/server/display/color/ColorDisplayService.java
index e3aa161..a313bcf 100644
--- a/services/core/java/com/android/server/display/color/ColorDisplayService.java
+++ b/services/core/java/com/android/server/display/color/ColorDisplayService.java
@@ -1745,8 +1745,8 @@
 
         @Override
         public boolean setSaturationLevel(int level) {
-            final boolean hasTransformsPermission = getContext()
-                    .checkCallingPermission(Manifest.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS)
+            final boolean hasTransformsPermission = getContext().checkCallingOrSelfPermission(
+                    Manifest.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS)
                     == PackageManager.PERMISSION_GRANTED;
             final boolean hasLegacyPermission = getContext()
                     .checkCallingPermission(Manifest.permission.CONTROL_DISPLAY_SATURATION)
diff --git a/services/core/java/com/android/server/hdmi/HdmiCecLocalDevice.java b/services/core/java/com/android/server/hdmi/HdmiCecLocalDevice.java
index 0671464..952af69 100644
--- a/services/core/java/com/android/server/hdmi/HdmiCecLocalDevice.java
+++ b/services/core/java/com/android/server/hdmi/HdmiCecLocalDevice.java
@@ -1117,6 +1117,7 @@
     }
 
     // Returns all actions matched with given class type.
+    @VisibleForTesting
     @ServiceThreadOnly
     <T extends HdmiCecFeatureAction> List<T> getActions(final Class<T> clazz) {
         assertRunOnServiceThread();
diff --git a/services/core/java/com/android/server/hdmi/HdmiCecLocalDevicePlayback.java b/services/core/java/com/android/server/hdmi/HdmiCecLocalDevicePlayback.java
index 824c8db..ba4d320 100644
--- a/services/core/java/com/android/server/hdmi/HdmiCecLocalDevicePlayback.java
+++ b/services/core/java/com/android/server/hdmi/HdmiCecLocalDevicePlayback.java
@@ -126,6 +126,10 @@
     private void launchDeviceDiscovery() {
         assertRunOnServiceThread();
         clearDeviceInfoList();
+        if (hasAction(DeviceDiscoveryAction.class)) {
+            Slog.i(TAG, "Device Discovery Action is in progress. Restarting.");
+            removeAction(DeviceDiscoveryAction.class);
+        }
         DeviceDiscoveryAction action = new DeviceDiscoveryAction(this,
                 new DeviceDiscoveryAction.DeviceDiscoveryCallback() {
                     @Override
diff --git a/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java b/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java
index 98f627c..b700785 100644
--- a/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java
+++ b/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java
@@ -1188,23 +1188,6 @@
     }
 
     /**
-     * {@link BroadcastReceiver} that is intended to listen to broadcasts sent to the system user
-     * only.
-     */
-    private final class ImmsBroadcastReceiverForSystemUser extends BroadcastReceiver {
-        @Override
-        public void onReceive(Context context, Intent intent) {
-            final String action = intent.getAction();
-            if (Intent.ACTION_USER_ADDED.equals(action)
-                    || Intent.ACTION_USER_REMOVED.equals(action)) {
-                updateCurrentProfileIds();
-            } else {
-                Slog.w(TAG, "Unexpected intent " + intent);
-            }
-        }
-    }
-
-    /**
      * {@link BroadcastReceiver} that is intended to listen to broadcasts sent to all the users.
      */
     private final class ImmsBroadcastReceiverForAllUsers extends BroadcastReceiver {
@@ -1611,9 +1594,16 @@
         @Override
         public void onUserUnlocking(@NonNull TargetUser user) {
             // Called on ActivityManager thread.
+            SecureSettingsWrapper.onUserUnlocking(user.getUserIdentifier());
             mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER, user.getUserIdentifier(), 0)
                     .sendToTarget();
         }
+
+        @Override
+        public void onUserStarting(TargetUser user) {
+            // Called on ActivityManager thread.
+            SecureSettingsWrapper.onUserStarting(user.getUserIdentifier());
+        }
     }
 
     void onUnlockUser(@UserIdInt int userId) {
@@ -1665,6 +1655,7 @@
             @Nullable InputMethodBindingController bindingControllerForTesting) {
         mContext = context;
         mRes = context.getResources();
+        SecureSettingsWrapper.onStart(mContext);
         // TODO(b/196206770): Disallow I/O on this thread. Currently it's needed for loading
         // additional subtypes in switchUserOnHandlerLocked().
         final ServiceThread thread =
@@ -1700,7 +1691,6 @@
         // mSettings should be created before buildInputMethodListLocked
         mSettings = new InputMethodSettings(mContext, mMethodMap, userId, !mSystemReady);
 
-        updateCurrentProfileIds();
         AdditionalSubtypeUtils.load(mAdditionalSubtypeMap, userId);
         mSwitchingController =
                 InputMethodSubtypeSwitchingController.createInstanceLocked(mSettings, context);
@@ -1818,7 +1808,6 @@
         final boolean useCopyOnWriteSettings =
                 !mSystemReady || !mUserManagerInternal.isUserUnlockingOrUnlocked(newUserId);
         mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
-        updateCurrentProfileIds();
         // Additional subtypes should be reset when the user is changed
         AdditionalSubtypeUtils.load(mAdditionalSubtypeMap, newUserId);
         final String defaultImiId = mSettings.getSelectedInputMethod();
@@ -1869,12 +1858,6 @@
         }
     }
 
-    void updateCurrentProfileIds() {
-        mSettings.setCurrentProfileIds(
-                mUserManagerInternal.getProfileIds(mSettings.getCurrentUserId(),
-                        false /* enabledOnly */));
-    }
-
     /**
      * TODO(b/32343335): The entire systemRunning() method needs to be revisited.
      */
@@ -1921,12 +1904,6 @@
                 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
                 mSettingsObserver.registerContentObserverLocked(currentUserId);
 
-                final IntentFilter broadcastFilterForSystemUser = new IntentFilter();
-                broadcastFilterForSystemUser.addAction(Intent.ACTION_USER_ADDED);
-                broadcastFilterForSystemUser.addAction(Intent.ACTION_USER_REMOVED);
-                mContext.registerReceiver(new ImmsBroadcastReceiverForSystemUser(),
-                        broadcastFilterForSystemUser);
-
                 final IntentFilter broadcastFilterForAllUsers = new IntentFilter();
                 broadcastFilterForAllUsers.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
                 mContext.registerReceiverAsUser(new ImmsBroadcastReceiverForAllUsers(),
@@ -3788,8 +3765,14 @@
             mVisibilityStateComputer.mShowForced = false;
         }
 
-        // cross-profile access is always allowed here to allow profile-switching.
-        if (!mSettings.isCurrentProfile(userId)) {
+        final int currentUserId = mSettings.getCurrentUserId();
+        if (userId != currentUserId) {
+            if (ArrayUtils.contains(
+                    mUserManagerInternal.getProfileIds(currentUserId, false), userId)) {
+                // cross-profile access is always allowed here to allow profile-switching.
+                scheduleSwitchUserTaskLocked(userId, cs.mClient);
+                return InputBindResult.USER_SWITCHING;
+            }
             Slog.w(TAG, "A background user is requesting window. Hiding IME.");
             Slog.w(TAG, "If you need to impersonate a foreground user/profile from"
                     + " a background user, use EditorInfo.targetInputMethodUser with"
@@ -3799,11 +3782,6 @@
             return InputBindResult.INVALID_USER;
         }
 
-        if (userId != mSettings.getCurrentUserId()) {
-            scheduleSwitchUserTaskLocked(userId, cs.mClient);
-            return InputBindResult.USER_SWITCHING;
-        }
-
         final boolean sameWindowFocused = mCurFocusedWindow == windowToken;
         final boolean isTextEditor = (startInputFlags & StartInputFlags.IS_TEXT_EDITOR) != 0;
         final boolean startInputByWinGainedFocus =
diff --git a/services/core/java/com/android/server/inputmethod/InputMethodUtils.java b/services/core/java/com/android/server/inputmethod/InputMethodUtils.java
index c661c86..b4338f6 100644
--- a/services/core/java/com/android/server/inputmethod/InputMethodUtils.java
+++ b/services/core/java/com/android/server/inputmethod/InputMethodUtils.java
@@ -21,7 +21,6 @@
 import android.annotation.UserHandleAware;
 import android.annotation.UserIdInt;
 import android.content.ComponentName;
-import android.content.ContentResolver;
 import android.content.Context;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
@@ -34,7 +33,6 @@
 import android.provider.Settings;
 import android.text.TextUtils;
 import android.util.ArrayMap;
-import android.util.ArraySet;
 import android.util.IntArray;
 import android.util.Pair;
 import android.util.Printer;
@@ -51,7 +49,6 @@
 
 import java.io.PrintWriter;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.List;
 import java.util.function.Predicate;
 
@@ -211,36 +208,15 @@
      */
     @UserHandleAware
     public static class InputMethodSettings {
-        private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
-                new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATOR);
-
-        private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
-                new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATOR);
-
         @NonNull
         private Context mUserAwareContext;
-        private ContentResolver mResolver;
         private final ArrayMap<String, InputMethodInfo> mMethodMap;
 
-        /**
-         * On-memory data store to emulate when {@link #mCopyOnWrite} is {@code true}.
-         */
-        private final ArrayMap<String, String> mCopyOnWriteDataStore = new ArrayMap<>();
-
-        private static final ArraySet<String> CLONE_TO_MANAGED_PROFILE = new ArraySet<>();
-        static {
-            Settings.Secure.getCloneToManagedProfileSettings(CLONE_TO_MANAGED_PROFILE);
-        }
-
-        private static final UserManagerInternal sUserManagerInternal =
-                LocalServices.getService(UserManagerInternal.class);
-
         private boolean mCopyOnWrite = false;
         @NonNull
         private String mEnabledInputMethodsStrCache = "";
         @UserIdInt
         private int mCurrentUserId;
-        private int[] mCurrentProfileIds = new int[0];
 
         private static void buildEnabledInputMethodsSettingString(
                 StringBuilder builder, Pair<String, ArrayList<String>> ime) {
@@ -281,7 +257,6 @@
             mUserAwareContext = context.getUserId() == userId
                     ? context
                     : context.createContextAsUser(UserHandle.of(userId), 0 /* flags */);
-            mResolver = mUserAwareContext.getContentResolver();
         }
 
         InputMethodSettings(@NonNull Context context,
@@ -305,79 +280,38 @@
                 Slog.d(TAG, "--- Switch the current user from " + mCurrentUserId + " to " + userId);
             }
             if (mCurrentUserId != userId || mCopyOnWrite != copyOnWrite) {
-                mCopyOnWriteDataStore.clear();
                 mEnabledInputMethodsStrCache = "";
-                // TODO: mCurrentProfileIds should be cleared here.
             }
             if (mUserAwareContext.getUserId() != userId) {
                 initContentWithUserContext(mUserAwareContext, userId);
             }
             mCurrentUserId = userId;
             mCopyOnWrite = copyOnWrite;
-            // TODO: mCurrentProfileIds should be updated here.
         }
 
         private void putString(@NonNull String key, @Nullable String str) {
-            if (mCopyOnWrite) {
-                mCopyOnWriteDataStore.put(key, str);
-            } else {
-                final int userId = CLONE_TO_MANAGED_PROFILE.contains(key)
-                        ? sUserManagerInternal.getProfileParentId(mCurrentUserId) : mCurrentUserId;
-                Settings.Secure.putStringForUser(mResolver, key, str, userId);
-            }
+            SecureSettingsWrapper.putString(key, str, mCurrentUserId);
         }
 
         @Nullable
         private String getString(@NonNull String key, @Nullable String defaultValue) {
-            final String result;
-            if (mCopyOnWrite && mCopyOnWriteDataStore.containsKey(key)) {
-                result = mCopyOnWriteDataStore.get(key);
-            } else {
-                result = Settings.Secure.getStringForUser(mResolver, key, mCurrentUserId);
-            }
-            return result != null ? result : defaultValue;
+            return SecureSettingsWrapper.getString(key, defaultValue, mCurrentUserId);
         }
 
         private void putInt(String key, int value) {
-            if (mCopyOnWrite) {
-                mCopyOnWriteDataStore.put(key, String.valueOf(value));
-            } else {
-                final int userId = CLONE_TO_MANAGED_PROFILE.contains(key)
-                        ? sUserManagerInternal.getProfileParentId(mCurrentUserId) : mCurrentUserId;
-                Settings.Secure.putIntForUser(mResolver, key, value, userId);
-            }
+            SecureSettingsWrapper.putInt(key, value, mCurrentUserId);
         }
 
         private int getInt(String key, int defaultValue) {
-            if (mCopyOnWrite && mCopyOnWriteDataStore.containsKey(key)) {
-                final String result = mCopyOnWriteDataStore.get(key);
-                return result != null ? Integer.parseInt(result) : defaultValue;
-            }
-            return Settings.Secure.getIntForUser(mResolver, key, defaultValue, mCurrentUserId);
+            return SecureSettingsWrapper.getInt(key, defaultValue, mCurrentUserId);
         }
 
         private void putBoolean(String key, boolean value) {
-            putInt(key, value ? 1 : 0);
+            SecureSettingsWrapper.putBoolean(key, value, mCurrentUserId);
         }
 
         private boolean getBoolean(String key, boolean defaultValue) {
-            return getInt(key, defaultValue ? 1 : 0) == 1;
-        }
-
-        public void setCurrentProfileIds(int[] currentProfileIds) {
-            synchronized (this) {
-                mCurrentProfileIds = currentProfileIds;
-            }
-        }
-
-        public boolean isCurrentProfile(int userId) {
-            synchronized (this) {
-                if (userId == mCurrentUserId) return true;
-                for (int i = 0; i < mCurrentProfileIds.length; i++) {
-                    if (userId == mCurrentProfileIds[i]) return true;
-                }
-                return false;
-            }
+            return SecureSettingsWrapper.getBoolean(key, defaultValue, mCurrentUserId);
         }
 
         ArrayList<InputMethodInfo> getEnabledInputMethodListLocked() {
@@ -428,8 +362,8 @@
 
         List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
             return buildInputMethodsAndSubtypeList(getEnabledInputMethodsStr(),
-                    mInputMethodSplitter,
-                    mSubtypeSplitter);
+                    new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATOR),
+                    new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATOR));
         }
 
         List<String> getEnabledInputMethodNames() {
@@ -700,16 +634,20 @@
             if (TextUtils.isEmpty(subtypeHistoryStr)) {
                 return imsList;
             }
-            mInputMethodSplitter.setString(subtypeHistoryStr);
-            while (mInputMethodSplitter.hasNext()) {
-                String nextImsStr = mInputMethodSplitter.next();
-                mSubtypeSplitter.setString(nextImsStr);
-                if (mSubtypeSplitter.hasNext()) {
+            final TextUtils.SimpleStringSplitter inputMethodSplitter =
+                    new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATOR);
+            final TextUtils.SimpleStringSplitter subtypeSplitter =
+                    new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATOR);
+            inputMethodSplitter.setString(subtypeHistoryStr);
+            while (inputMethodSplitter.hasNext()) {
+                String nextImsStr = inputMethodSplitter.next();
+                subtypeSplitter.setString(nextImsStr);
+                if (subtypeSplitter.hasNext()) {
                     String subtypeId = NOT_A_SUBTYPE_ID_STR;
                     // The first element is ime id.
-                    String imeId = mSubtypeSplitter.next();
-                    while (mSubtypeSplitter.hasNext()) {
-                        subtypeId = mSubtypeSplitter.next();
+                    String imeId = subtypeSplitter.next();
+                    while (subtypeSplitter.hasNext()) {
+                        subtypeId = subtypeSplitter.next();
                         break;
                     }
                     imsList.add(new Pair<>(imeId, subtypeId));
@@ -950,7 +888,6 @@
 
         public void dumpLocked(final Printer pw, final String prefix) {
             pw.println(prefix + "mCurrentUserId=" + mCurrentUserId);
-            pw.println(prefix + "mCurrentProfileIds=" + Arrays.toString(mCurrentProfileIds));
             pw.println(prefix + "mCopyOnWrite=" + mCopyOnWrite);
             pw.println(prefix + "mEnabledInputMethodsStrCache=" + mEnabledInputMethodsStrCache);
         }
@@ -1029,9 +966,7 @@
     static List<String> getEnabledInputMethodIdsForFiltering(@NonNull Context context,
             @UserIdInt int userId) {
         final String enabledInputMethodsStr = TextUtils.nullIfEmpty(
-                Settings.Secure.getStringForUser(
-                        context.getContentResolver(),
-                        Settings.Secure.ENABLED_INPUT_METHODS,
+                SecureSettingsWrapper.getString(Settings.Secure.ENABLED_INPUT_METHODS, null,
                         userId));
         if (enabledInputMethodsStr == null) {
             return List.of();
diff --git a/services/core/java/com/android/server/inputmethod/SecureSettingsWrapper.java b/services/core/java/com/android/server/inputmethod/SecureSettingsWrapper.java
new file mode 100644
index 0000000..559b625
--- /dev/null
+++ b/services/core/java/com/android/server/inputmethod/SecureSettingsWrapper.java
@@ -0,0 +1,371 @@
+/*
+ * Copyright (C) 2023 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.inputmethod;
+
+import android.annotation.AnyThread;
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.annotation.UserIdInt;
+import android.app.ActivityManagerInternal;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.content.pm.UserInfo;
+import android.provider.Settings;
+import android.util.ArrayMap;
+import android.util.ArraySet;
+import android.util.SparseArray;
+
+import com.android.internal.annotations.GuardedBy;
+import com.android.server.LocalServices;
+import com.android.server.pm.UserManagerInternal;
+
+/**
+ * A thread-safe utility class to encapsulate accesses to {@link Settings.Secure} that may need a
+ * special handling for direct-boot support.
+ *
+ * <p>Any changes made until the user storage is unlocked are non-persistent and will be reset
+ * to the persistent value when the user storage is unlocked.</p>
+ */
+final class SecureSettingsWrapper {
+    @Nullable
+    private static volatile ContentResolver sContentResolver = null;
+
+    /**
+     * Not intended to be instantiated.
+     */
+    private SecureSettingsWrapper() {
+    }
+
+    private static final ArraySet<String> CLONE_TO_MANAGED_PROFILE = new ArraySet<>();
+    static {
+        Settings.Secure.getCloneToManagedProfileSettings(CLONE_TO_MANAGED_PROFILE);
+    }
+
+    @AnyThread
+    @UserIdInt
+    private static int getUserIdForClonedSettings(@NonNull String key, @UserIdInt int userId) {
+        return CLONE_TO_MANAGED_PROFILE.contains(key)
+                ? LocalServices.getService(UserManagerInternal.class).getProfileParentId(userId)
+                : userId;
+    }
+
+    private interface ReaderWriter {
+        @AnyThread
+        void putString(@NonNull String key, @Nullable String value);
+
+        @AnyThread
+        @Nullable
+        String getString(@NonNull String key, @Nullable String defaultValue);
+
+        @AnyThread
+        void putInt(String key, int value);
+
+        @AnyThread
+        int getInt(String key, int defaultValue);
+    }
+
+    private static class UnlockedUserImpl implements ReaderWriter {
+        @UserIdInt
+        private final int mUserId;
+
+        private final ContentResolver mContentResolver;
+
+        UnlockedUserImpl(@UserIdInt int userId, @NonNull ContentResolver contentResolver) {
+            mUserId = userId;
+            mContentResolver = contentResolver;
+        }
+
+        @AnyThread
+        @Override
+        public void putString(String key, String value) {
+            final int userId = getUserIdForClonedSettings(key, mUserId);
+            Settings.Secure.putStringForUser(mContentResolver, key, value, userId);
+        }
+
+        @AnyThread
+        @Nullable
+        @Override
+        public String getString(String key, String defaultValue) {
+            final String result = Settings.Secure.getStringForUser(mContentResolver, key, mUserId);
+            return result != null ? result : defaultValue;
+        }
+
+        @AnyThread
+        @Override
+        public void putInt(String key, int value) {
+            final int userId = getUserIdForClonedSettings(key, mUserId);
+            Settings.Secure.putIntForUser(mContentResolver, key, value, userId);
+        }
+
+        @AnyThread
+        @Override
+        public int getInt(String key, int defaultValue) {
+            return Settings.Secure.getIntForUser(mContentResolver, key, defaultValue, mUserId);
+        }
+    }
+
+    /**
+     * For users whose storages are not unlocked yet, we do not want to update IME related Secure
+     * Settings. Any write operations will be forwarded to
+     * {@link LockedUserImpl#mNonPersistentKeyValues} so that we can return the volatile data until
+     * the user storage is unlocked.
+     */
+    private static final class LockedUserImpl extends UnlockedUserImpl {
+        @GuardedBy("mNonPersistentKeyValues")
+        private final ArrayMap<String, String> mNonPersistentKeyValues = new ArrayMap<>();
+
+        LockedUserImpl(@UserIdInt int userId, @NonNull ContentResolver contentResolver) {
+            super(userId, contentResolver);
+        }
+
+        @AnyThread
+        @Override
+        public void putString(String key, String value) {
+            synchronized (mNonPersistentKeyValues) {
+                mNonPersistentKeyValues.put(key, value);
+            }
+        }
+
+        @AnyThread
+        @Nullable
+        @Override
+        public String getString(String key, String defaultValue) {
+            synchronized (mNonPersistentKeyValues) {
+                if (mNonPersistentKeyValues.containsKey(key)) {
+                    final String result = mNonPersistentKeyValues.get(key);
+                    return result != null ? result : defaultValue;
+                }
+                return super.getString(key, defaultValue);
+            }
+        }
+
+        @AnyThread
+        @Override
+        public void putInt(String key, int value) {
+            synchronized (mNonPersistentKeyValues) {
+                mNonPersistentKeyValues.put(key, String.valueOf(value));
+            }
+        }
+
+        @AnyThread
+        @Override
+        public int getInt(String key, int defaultValue) {
+            synchronized (mNonPersistentKeyValues) {
+                if (mNonPersistentKeyValues.containsKey(key)) {
+                    final String result = mNonPersistentKeyValues.get(key);
+                    return result != null ? Integer.parseInt(result) : defaultValue;
+                }
+                return super.getInt(key, defaultValue);
+            }
+        }
+    }
+
+    @GuardedBy("sUserMap")
+    @NonNull
+    private static final SparseArray<ReaderWriter> sUserMap = new SparseArray<>();
+
+    private static final ReaderWriter NOOP = new ReaderWriter() {
+        @Override
+        public void putString(String key, String str) {
+        }
+
+        @Override
+        public String getString(String key, String defaultValue) {
+            return defaultValue;
+        }
+
+        @Override
+        public void putInt(String key, int value) {
+        }
+
+        @Override
+        public int getInt(String key, int defaultValue) {
+            return defaultValue;
+        }
+    };
+
+    private static ReaderWriter createImpl(@NonNull UserManagerInternal userManagerInternal,
+            @UserIdInt int userId) {
+        return userManagerInternal.isUserUnlockingOrUnlocked(userId)
+                ? new UnlockedUserImpl(userId, sContentResolver)
+                : new LockedUserImpl(userId, sContentResolver);
+    }
+
+    @NonNull
+    @AnyThread
+    private static ReaderWriter putOrGet(@UserIdInt int userId,
+            @NonNull ReaderWriter readerWriter) {
+        final boolean isUnlockedUserImpl = readerWriter instanceof UnlockedUserImpl;
+        synchronized (sUserMap) {
+            final ReaderWriter current = sUserMap.get(userId);
+            if (current == null) {
+                sUserMap.put(userId, readerWriter);
+                return readerWriter;
+            }
+            // Upgrading from CopyOnWriteImpl to DirectImpl is allowed.
+            if (current instanceof LockedUserImpl && isUnlockedUserImpl) {
+                sUserMap.put(userId, readerWriter);
+                return readerWriter;
+            }
+            return current;
+        }
+    }
+
+    @NonNull
+    @AnyThread
+    private static ReaderWriter get(@UserIdInt int userId) {
+        synchronized (sUserMap) {
+            final ReaderWriter readerWriter = sUserMap.get(userId);
+            if (readerWriter != null) {
+                return readerWriter;
+            }
+        }
+        final UserManagerInternal userManagerInternal =
+                LocalServices.getService(UserManagerInternal.class);
+        if (!userManagerInternal.exists(userId)) {
+            return NOOP;
+        }
+        return putOrGet(userId, createImpl(userManagerInternal, userId));
+    }
+
+    /**
+     * Called when {@link InputMethodManagerService} is starting.
+     *
+     * @param context the {@link Context} to be used.
+     */
+    @AnyThread
+    static void onStart(@NonNull Context context) {
+        sContentResolver = context.getContentResolver();
+
+        final int userId = LocalServices.getService(ActivityManagerInternal.class)
+                .getCurrentUserId();
+        final UserManagerInternal userManagerInternal =
+                LocalServices.getService(UserManagerInternal.class);
+        putOrGet(userId, createImpl(userManagerInternal, userId));
+
+        userManagerInternal.addUserLifecycleListener(
+                new UserManagerInternal.UserLifecycleListener() {
+                    @Override
+                    public void onUserRemoved(UserInfo user) {
+                        synchronized (sUserMap) {
+                            sUserMap.remove(userId);
+                        }
+                    }
+                }
+        );
+    }
+
+    /**
+     * Called when a user is starting.
+     *
+     * @param userId the ID of the user who is starting.
+     */
+    @AnyThread
+    static void onUserStarting(@UserIdInt int userId) {
+        putOrGet(userId, createImpl(LocalServices.getService(UserManagerInternal.class), userId));
+    }
+
+    /**
+     * Called when a user is being unlocked.
+     *
+     * @param userId the ID of the user whose storage is being unlocked.
+     */
+    @AnyThread
+    static void onUserUnlocking(@UserIdInt int userId) {
+        final ReaderWriter readerWriter = new UnlockedUserImpl(userId, sContentResolver);
+        putOrGet(userId, readerWriter);
+    }
+
+    /**
+     * Put the given string {@code value} to {@code key}.
+     *
+     * @param key a secure settings key.
+     * @param value a secure settings value.
+     * @param userId the ID of a user whose secure settings will be updated.
+     * @see Settings.Secure#putStringForUser(ContentResolver, String, String, int)
+     */
+    @AnyThread
+    static void putString(String key, String value, @UserIdInt int userId) {
+        get(userId).putString(key, value);
+    }
+
+    /**
+     * Get a string value with the given {@code key}
+     *
+     * @param key a secure settings key.
+     * @param defaultValue the default value when the value is not found.
+     * @param userId the ID of a user whose secure settings will be updated.
+     * @return The string value if it is found. {@code defaultValue} otherwise.
+     * @see Settings.Secure#getStringForUser(ContentResolver, String, int)
+     */
+    @AnyThread
+    @Nullable
+    static String getString(String key, String defaultValue, @UserIdInt int userId) {
+        return get(userId).getString(key, defaultValue);
+    }
+
+    /**
+     * Put the given integer {@code value} to {@code key}.
+     *
+     * @param key a secure settings key.
+     * @param value a secure settings value.
+     * @param userId the ID of a user whose secure settings will be updated.
+     * @see Settings.Secure#putIntForUser(ContentResolver, String, int, int)
+     */
+    @AnyThread
+    static void putInt(String key, int value, @UserIdInt int userId) {
+        get(userId).putInt(key, value);
+    }
+
+    /**
+     * Get an integer value with the given {@code key}
+     *
+     * @param key a secure settings key.
+     * @param defaultValue the default value when the value is not found.
+     * @param userId the ID of a user whose secure settings will be updated.
+     * @return The integer value if it is found. {@code defaultValue} otherwise.
+c     */
+    @AnyThread
+    static int getInt(String key, int defaultValue, @UserIdInt int userId) {
+        return get(userId).getInt(key, defaultValue);
+    }
+
+    /**
+     * Put the given boolean {@code value} to {@code key}.
+     *
+     * @param key a secure settings key.
+     * @param value a secure settings value.
+     * @param userId the ID of a user whose secure settings will be updated.
+     */
+    @AnyThread
+    static void putBoolean(String key, boolean value, @UserIdInt int userId) {
+        get(userId).putInt(key, value ? 1 : 0);
+    }
+
+    /**
+     * Get a boolean value with the given {@code key}
+     *
+     * @param key a secure settings key.
+     * @param defaultValue the default value when the value is not found.
+     * @param userId the ID of a user whose secure settings will be updated.
+     * @return The boolean value if it is found. {@code defaultValue} otherwise.
+     */
+    @AnyThread
+    static boolean getBoolean(String key, boolean defaultValue, @UserIdInt int userId) {
+        return get(userId).getInt(key, defaultValue ? 1 : 0) == 1;
+    }
+}
diff --git a/services/core/java/com/android/server/media/AudioAttributesUtils.java b/services/core/java/com/android/server/media/AudioAttributesUtils.java
deleted file mode 100644
index 8cb334d..0000000
--- a/services/core/java/com/android/server/media/AudioAttributesUtils.java
+++ /dev/null
@@ -1,125 +0,0 @@
-/*
- * Copyright (C) 2023 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.media;
-
-import android.annotation.NonNull;
-import android.annotation.Nullable;
-import android.media.AudioAttributes;
-import android.media.AudioDeviceAttributes;
-import android.media.AudioDeviceInfo;
-import android.media.MediaRoute2Info;
-
-import com.android.media.flags.Flags;
-
-/* package */ final class AudioAttributesUtils {
-
-    /* package */ static final AudioAttributes ATTRIBUTES_MEDIA = new AudioAttributes.Builder()
-            .setUsage(AudioAttributes.USAGE_MEDIA)
-            .build();
-
-    private AudioAttributesUtils() {
-        // no-op to prevent instantiation.
-    }
-
-    @MediaRoute2Info.Type
-    /* package */ static int mapToMediaRouteType(
-            @NonNull AudioDeviceAttributes audioDeviceAttributes) {
-        if (Flags.enableAudioPoliciesDeviceAndBluetoothController()) {
-            switch (audioDeviceAttributes.getType()) {
-                case AudioDeviceInfo.TYPE_HDMI_ARC:
-                    return MediaRoute2Info.TYPE_HDMI_ARC;
-                case AudioDeviceInfo.TYPE_HDMI_EARC:
-                    return MediaRoute2Info.TYPE_HDMI_EARC;
-            }
-        }
-        switch (audioDeviceAttributes.getType()) {
-            case AudioDeviceInfo.TYPE_BUILTIN_EARPIECE:
-            case AudioDeviceInfo.TYPE_BUILTIN_SPEAKER:
-                return MediaRoute2Info.TYPE_BUILTIN_SPEAKER;
-            case AudioDeviceInfo.TYPE_WIRED_HEADSET:
-                return MediaRoute2Info.TYPE_WIRED_HEADSET;
-            case AudioDeviceInfo.TYPE_WIRED_HEADPHONES:
-                return MediaRoute2Info.TYPE_WIRED_HEADPHONES;
-            case AudioDeviceInfo.TYPE_DOCK:
-            case AudioDeviceInfo.TYPE_DOCK_ANALOG:
-                return MediaRoute2Info.TYPE_DOCK;
-            case AudioDeviceInfo.TYPE_HDMI:
-            case AudioDeviceInfo.TYPE_HDMI_ARC:
-            case AudioDeviceInfo.TYPE_HDMI_EARC:
-                return MediaRoute2Info.TYPE_HDMI;
-            case AudioDeviceInfo.TYPE_USB_DEVICE:
-                return MediaRoute2Info.TYPE_USB_DEVICE;
-            case AudioDeviceInfo.TYPE_BLUETOOTH_A2DP:
-                return MediaRoute2Info.TYPE_BLUETOOTH_A2DP;
-            case AudioDeviceInfo.TYPE_BLE_HEADSET:
-                return MediaRoute2Info.TYPE_BLE_HEADSET;
-            case AudioDeviceInfo.TYPE_HEARING_AID:
-                return MediaRoute2Info.TYPE_HEARING_AID;
-            default:
-                return MediaRoute2Info.TYPE_UNKNOWN;
-        }
-    }
-
-    /* package */ static boolean isDeviceOutputAttributes(
-            @Nullable AudioDeviceAttributes audioDeviceAttributes) {
-        if (audioDeviceAttributes == null) {
-            return false;
-        }
-
-        if (audioDeviceAttributes.getRole() != AudioDeviceAttributes.ROLE_OUTPUT) {
-            return false;
-        }
-
-        switch (audioDeviceAttributes.getType()) {
-            case AudioDeviceInfo.TYPE_BUILTIN_EARPIECE:
-            case AudioDeviceInfo.TYPE_BUILTIN_SPEAKER:
-            case AudioDeviceInfo.TYPE_WIRED_HEADSET:
-            case AudioDeviceInfo.TYPE_WIRED_HEADPHONES:
-            case AudioDeviceInfo.TYPE_DOCK:
-            case AudioDeviceInfo.TYPE_DOCK_ANALOG:
-            case AudioDeviceInfo.TYPE_HDMI:
-            case AudioDeviceInfo.TYPE_HDMI_ARC:
-            case AudioDeviceInfo.TYPE_HDMI_EARC:
-            case AudioDeviceInfo.TYPE_USB_DEVICE:
-                return true;
-            default:
-                return false;
-        }
-    }
-
-    /* package */ static boolean isBluetoothOutputAttributes(
-            @Nullable AudioDeviceAttributes audioDeviceAttributes) {
-        if (audioDeviceAttributes == null) {
-            return false;
-        }
-
-        if (audioDeviceAttributes.getRole() != AudioDeviceAttributes.ROLE_OUTPUT) {
-            return false;
-        }
-
-        switch (audioDeviceAttributes.getType()) {
-            case AudioDeviceInfo.TYPE_BLUETOOTH_A2DP:
-            case AudioDeviceInfo.TYPE_BLE_HEADSET:
-            case AudioDeviceInfo.TYPE_BLE_SPEAKER:
-            case AudioDeviceInfo.TYPE_HEARING_AID:
-                return true;
-            default:
-                return false;
-        }
-    }
-
-}
diff --git a/services/core/java/com/android/server/media/AudioPoliciesBluetoothRouteController.java b/services/core/java/com/android/server/media/AudioPoliciesBluetoothRouteController.java
index 8bc69c2..a00999d 100644
--- a/services/core/java/com/android/server/media/AudioPoliciesBluetoothRouteController.java
+++ b/services/core/java/com/android/server/media/AudioPoliciesBluetoothRouteController.java
@@ -17,7 +17,6 @@
 package com.android.server.media;
 
 import static android.bluetooth.BluetoothAdapter.ACTIVE_DEVICE_AUDIO;
-import static android.bluetooth.BluetoothAdapter.STATE_CONNECTED;
 
 import android.annotation.NonNull;
 import android.annotation.Nullable;
@@ -31,38 +30,37 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
-import android.media.AudioManager;
-import android.media.AudioSystem;
 import android.media.MediaRoute2Info;
 import android.os.UserHandle;
 import android.text.TextUtils;
+import android.util.Log;
 import android.util.Slog;
 import android.util.SparseBooleanArray;
-import android.util.SparseIntArray;
 
 import com.android.internal.R;
 import com.android.internal.annotations.VisibleForTesting;
 
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
 import java.util.Set;
+import java.util.function.Function;
+import java.util.stream.Collectors;
 
 /**
- * Controls bluetooth routes and provides selected route override.
+ * Maintains a list of connected {@link BluetoothDevice bluetooth devices} and allows their
+ * activation.
  *
- * <p>The controller offers similar functionality to {@link LegacyBluetoothRouteController} but does
- * not support routes selection logic. Instead, relies on external clients to make a decision
- * about currently selected route.
- *
- * <p>Selected route override should be used by {@link AudioManager} which is aware of Audio
- * Policies.
+ * <p>This class also serves as ground truth for assigning {@link MediaRoute2Info#getId() route ids}
+ * for bluetooth routes via {@link #getRouteIdForBluetoothAddress}.
  */
-/* package */ class AudioPoliciesBluetoothRouteController
-        implements BluetoothRouteController {
-    private static final String TAG = "APBtRouteController";
+// TODO: b/305199571 - Rename this class to remove the RouteController suffix, which causes
+// confusion with the BluetoothRouteController interface.
+/* package */ class AudioPoliciesBluetoothRouteController {
+    private static final String TAG = SystemMediaRoute2Provider.TAG;
 
     private static final String HEARING_AID_ROUTE_ID_PREFIX = "HEARING_AID_";
     private static final String LE_AUDIO_ROUTE_ID_PREFIX = "LE_AUDIO_";
@@ -75,11 +73,8 @@
     private final DeviceStateChangedReceiver mDeviceStateChangedReceiver =
             new DeviceStateChangedReceiver();
 
-    @NonNull
-    private final Map<String, BluetoothRouteInfo> mBluetoothRoutes = new HashMap<>();
-
-    @NonNull
-    private final SparseIntArray mVolumeMap = new SparseIntArray();
+    @NonNull private Map<String, BluetoothDevice> mAddressToBondedDevice = new HashMap<>();
+    @NonNull private final Map<String, BluetoothRouteInfo> mBluetoothRoutes = new HashMap<>();
 
     @NonNull
     private final Context mContext;
@@ -89,11 +84,6 @@
     private final BluetoothRouteController.BluetoothRoutesUpdatedListener mListener;
     @NonNull
     private final BluetoothProfileMonitor mBluetoothProfileMonitor;
-    @NonNull
-    private final AudioManager mAudioManager;
-
-    @Nullable
-    private BluetoothRouteInfo mSelectedBluetoothRoute;
 
     AudioPoliciesBluetoothRouteController(@NonNull Context context,
             @NonNull BluetoothAdapter bluetoothAdapter,
@@ -107,21 +97,12 @@
             @NonNull BluetoothAdapter bluetoothAdapter,
             @NonNull BluetoothProfileMonitor bluetoothProfileMonitor,
             @NonNull BluetoothRouteController.BluetoothRoutesUpdatedListener listener) {
-        Objects.requireNonNull(context);
-        Objects.requireNonNull(bluetoothAdapter);
-        Objects.requireNonNull(bluetoothProfileMonitor);
-        Objects.requireNonNull(listener);
-
-        mContext = context;
-        mBluetoothAdapter = bluetoothAdapter;
-        mBluetoothProfileMonitor = bluetoothProfileMonitor;
-        mAudioManager = mContext.getSystemService(AudioManager.class);
-        mListener = listener;
-
-        updateBluetoothRoutes();
+        mContext = Objects.requireNonNull(context);
+        mBluetoothAdapter = Objects.requireNonNull(bluetoothAdapter);
+        mBluetoothProfileMonitor = Objects.requireNonNull(bluetoothProfileMonitor);
+        mListener = Objects.requireNonNull(listener);
     }
 
-    @Override
     public void start(UserHandle user) {
         mBluetoothProfileMonitor.start();
 
@@ -133,122 +114,63 @@
 
         IntentFilter deviceStateChangedIntentFilter = new IntentFilter();
 
-        deviceStateChangedIntentFilter.addAction(BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED);
         deviceStateChangedIntentFilter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
         deviceStateChangedIntentFilter.addAction(BluetoothHearingAid.ACTION_ACTIVE_DEVICE_CHANGED);
         deviceStateChangedIntentFilter.addAction(
                 BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED);
         deviceStateChangedIntentFilter.addAction(
                 BluetoothLeAudio.ACTION_LE_AUDIO_CONNECTION_STATE_CHANGED);
-        deviceStateChangedIntentFilter.addAction(
-                BluetoothLeAudio.ACTION_LE_AUDIO_ACTIVE_DEVICE_CHANGED);
 
         mContext.registerReceiverAsUser(mDeviceStateChangedReceiver, user,
                 deviceStateChangedIntentFilter, null, null);
+        updateBluetoothRoutes();
     }
 
-    @Override
     public void stop() {
         mContext.unregisterReceiver(mAdapterStateChangedReceiver);
         mContext.unregisterReceiver(mDeviceStateChangedReceiver);
     }
 
-    @Override
-    public boolean selectRoute(@Nullable String deviceAddress) {
-        synchronized (this) {
-            // Fetch all available devices in order to avoid race conditions with Bluetooth stack.
-            updateBluetoothRoutes();
-
-            if (deviceAddress == null) {
-                mSelectedBluetoothRoute = null;
-                return true;
-            }
-
-            BluetoothRouteInfo bluetoothRouteInfo = mBluetoothRoutes.get(deviceAddress);
-
-            if (bluetoothRouteInfo == null) {
-                Slog.w(TAG, "Cannot find bluetooth route for " + deviceAddress);
-                return false;
-            }
-
-            mSelectedBluetoothRoute = bluetoothRouteInfo;
-            setRouteConnectionState(mSelectedBluetoothRoute, STATE_CONNECTED);
-
-            updateConnectivityStateForDevicesInTheSameGroup();
-
-            return true;
-        }
+    @Nullable
+    public synchronized String getRouteIdForBluetoothAddress(@Nullable String address) {
+        BluetoothDevice bluetoothDevice = mAddressToBondedDevice.get(address);
+        // TODO: b/305199571 - Optimize the following statement to avoid creating the full
+        // MediaRoute2Info instance. We just need the id.
+        return bluetoothDevice != null
+                ? createBluetoothRoute(bluetoothDevice).mRoute.getId()
+                : null;
     }
 
-    /**
-     * Updates connectivity state for devices in the same devices group.
-     *
-     * <p>{@link BluetoothProfile#LE_AUDIO} and {@link BluetoothProfile#HEARING_AID} support
-     * grouping devices. Devices that belong to the same group should have the same routeId but
-     * different physical address.
-     *
-     * <p>In case one of the devices from the group is selected then other devices should also
-     * reflect this by changing their connectivity status to
-     * {@link MediaRoute2Info#CONNECTION_STATE_CONNECTED}.
-     */
-    private void updateConnectivityStateForDevicesInTheSameGroup() {
-        synchronized (this) {
-            for (BluetoothRouteInfo btRoute : mBluetoothRoutes.values()) {
-                if (TextUtils.equals(btRoute.mRoute.getId(), mSelectedBluetoothRoute.mRoute.getId())
-                        && !TextUtils.equals(btRoute.mBtDevice.getAddress(),
-                        mSelectedBluetoothRoute.mBtDevice.getAddress())) {
-                    setRouteConnectionState(btRoute, STATE_CONNECTED);
-                }
-            }
-        }
-    }
-
-    @Override
-    public void transferTo(@Nullable String routeId) {
-        if (routeId == null) {
-            mBluetoothAdapter.removeActiveDevice(ACTIVE_DEVICE_AUDIO);
-            return;
-        }
-
-        BluetoothRouteInfo btRouteInfo = findBluetoothRouteWithRouteId(routeId);
+    public synchronized void activateBluetoothDeviceWithAddress(String address) {
+        BluetoothRouteInfo btRouteInfo = mBluetoothRoutes.get(address);
 
         if (btRouteInfo == null) {
-            Slog.w(TAG, "transferTo: Unknown route. ID=" + routeId);
+            Slog.w(TAG, "activateBluetoothDeviceWithAddress: Ignoring unknown address " + address);
             return;
         }
-
         mBluetoothAdapter.setActiveDevice(btRouteInfo.mBtDevice, ACTIVE_DEVICE_AUDIO);
     }
 
-    @Nullable
-    private BluetoothRouteInfo findBluetoothRouteWithRouteId(@Nullable String routeId) {
-        if (routeId == null) {
-            return null;
-        }
-        synchronized (this) {
-            for (BluetoothRouteInfo btRouteInfo : mBluetoothRoutes.values()) {
-                if (TextUtils.equals(btRouteInfo.mRoute.getId(), routeId)) {
-                    return btRouteInfo;
-                }
-            }
-        }
-        return null;
-    }
-
     private void updateBluetoothRoutes() {
         Set<BluetoothDevice> bondedDevices = mBluetoothAdapter.getBondedDevices();
 
-        if (bondedDevices == null) {
-            return;
-        }
-
         synchronized (this) {
             mBluetoothRoutes.clear();
-
-            // We need to query all available to BT stack devices in order to avoid inconsistency
-            // between external services, like, AndroidManager, and BT stack.
+            if (bondedDevices == null) {
+                // Bonded devices is null upon running into a BluetoothAdapter error.
+                Log.w(TAG, "BluetoothAdapter.getBondedDevices returned null.");
+                return;
+            }
+            // We don't clear bonded devices if we receive a null getBondedDevices result, because
+            // that probably means that the bluetooth stack ran into an issue. Not that all devices
+            // have been unpaired.
+            mAddressToBondedDevice =
+                    bondedDevices.stream()
+                            .collect(
+                                    Collectors.toMap(
+                                            BluetoothDevice::getAddress, Function.identity()));
             for (BluetoothDevice device : bondedDevices) {
-                if (isDeviceConnected(device)) {
+                if (device.isConnected()) {
                     BluetoothRouteInfo newBtRoute = createBluetoothRoute(device);
                     if (newBtRoute.mConnectedProfiles.size() > 0) {
                         mBluetoothRoutes.put(device.getAddress(), newBtRoute);
@@ -258,106 +180,51 @@
         }
     }
 
-    @VisibleForTesting
-        /* package */ boolean isDeviceConnected(@NonNull BluetoothDevice device) {
-        return device.isConnected();
-    }
-
-    @Nullable
-    @Override
-    public MediaRoute2Info getSelectedRoute() {
-        synchronized (this) {
-            if (mSelectedBluetoothRoute == null) {
-                return null;
-            }
-
-            return mSelectedBluetoothRoute.mRoute;
-        }
-    }
-
     @NonNull
-    @Override
-    public List<MediaRoute2Info> getTransferableRoutes() {
-        List<MediaRoute2Info> routes = getAllBluetoothRoutes();
-        synchronized (this) {
-            if (mSelectedBluetoothRoute != null) {
-                routes.remove(mSelectedBluetoothRoute.mRoute);
-            }
-        }
-        return routes;
-    }
-
-    @NonNull
-    @Override
-    public List<MediaRoute2Info> getAllBluetoothRoutes() {
+    public List<MediaRoute2Info> getAvailableBluetoothRoutes() {
         List<MediaRoute2Info> routes = new ArrayList<>();
-        List<String> routeIds = new ArrayList<>();
-
-        MediaRoute2Info selectedRoute = getSelectedRoute();
-        if (selectedRoute != null) {
-            routes.add(selectedRoute);
-            routeIds.add(selectedRoute.getId());
-        }
+        Set<String> routeIds = new HashSet<>();
 
         synchronized (this) {
             for (BluetoothRouteInfo btRoute : mBluetoothRoutes.values()) {
-                // A pair of hearing aid devices or having the same hardware address
-                if (routeIds.contains(btRoute.mRoute.getId())) {
-                    continue;
+                // See createBluetoothRoute for info on why we do this.
+                if (routeIds.add(btRoute.mRoute.getId())) {
+                    routes.add(btRoute.mRoute);
                 }
-                routes.add(btRoute.mRoute);
-                routeIds.add(btRoute.mRoute.getId());
             }
         }
         return routes;
     }
 
-    @Override
-    public boolean updateVolumeForDevices(int devices, int volume) {
-        int routeType;
-        if ((devices & (AudioSystem.DEVICE_OUT_HEARING_AID)) != 0) {
-            routeType = MediaRoute2Info.TYPE_HEARING_AID;
-        } else if ((devices & (AudioManager.DEVICE_OUT_BLUETOOTH_A2DP
-                | AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES
-                | AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)) != 0) {
-            routeType = MediaRoute2Info.TYPE_BLUETOOTH_A2DP;
-        } else if ((devices & (AudioManager.DEVICE_OUT_BLE_HEADSET)) != 0) {
-            routeType = MediaRoute2Info.TYPE_BLE_HEADSET;
-        } else {
-            return false;
-        }
-
-        synchronized (this) {
-            mVolumeMap.put(routeType, volume);
-            if (mSelectedBluetoothRoute == null
-                    || mSelectedBluetoothRoute.mRoute.getType() != routeType) {
-                return false;
-            }
-
-            mSelectedBluetoothRoute.mRoute =
-                    new MediaRoute2Info.Builder(mSelectedBluetoothRoute.mRoute)
-                            .setVolume(volume)
-                            .build();
-        }
-
-        notifyBluetoothRoutesUpdated();
-        return true;
-    }
-
     private void notifyBluetoothRoutesUpdated() {
         mListener.onBluetoothRoutesUpdated();
     }
 
+    /**
+     * Creates a new {@link BluetoothRouteInfo}, including its member {@link
+     * BluetoothRouteInfo#mRoute}.
+     *
+     * <p>The most important logic in this method is around the {@link MediaRoute2Info#getId() route
+     * id} assignment. In some cases we want to group multiple {@link BluetoothDevice bluetooth
+     * devices} as a single media route. For example, the left and right hearing aids get exposed as
+     * two different BluetoothDevice instances, but we want to show them as a single route. In this
+     * case, we assign the same route id to all "group" bluetooth devices (like left and right
+     * hearing aids), so that a single route is exposed for both of them.
+     *
+     * <p>Deduplication by id happens downstream because we need to be able to refer to all
+     * bluetooth devices individually, since the audio stack refers to a bluetooth device group by
+     * any of its member devices.
+     */
     private BluetoothRouteInfo createBluetoothRoute(BluetoothDevice device) {
         BluetoothRouteInfo
                 newBtRoute = new BluetoothRouteInfo();
         newBtRoute.mBtDevice = device;
-
-        String routeId = device.getAddress();
         String deviceName = device.getName();
         if (TextUtils.isEmpty(deviceName)) {
             deviceName = mContext.getResources().getText(R.string.unknownName).toString();
         }
+
+        String routeId = device.getAddress();
         int type = MediaRoute2Info.TYPE_BLUETOOTH_A2DP;
         newBtRoute.mConnectedProfiles = new SparseBooleanArray();
         if (mBluetoothProfileMonitor.isProfileSupported(BluetoothProfile.A2DP, device)) {
@@ -365,7 +232,6 @@
         }
         if (mBluetoothProfileMonitor.isProfileSupported(BluetoothProfile.HEARING_AID, device)) {
             newBtRoute.mConnectedProfiles.put(BluetoothProfile.HEARING_AID, true);
-            // Intentionally assign the same ID for a pair of devices to publish only one of them.
             routeId = HEARING_AID_ROUTE_ID_PREFIX
                     + mBluetoothProfileMonitor.getGroupId(BluetoothProfile.HEARING_AID, device);
             type = MediaRoute2Info.TYPE_HEARING_AID;
@@ -377,66 +243,27 @@
             type = MediaRoute2Info.TYPE_BLE_HEADSET;
         }
 
-        // Current volume will be set when connected.
-        newBtRoute.mRoute = new MediaRoute2Info.Builder(routeId, deviceName)
-                .addFeature(MediaRoute2Info.FEATURE_LIVE_AUDIO)
-                .addFeature(MediaRoute2Info.FEATURE_LOCAL_PLAYBACK)
-                .setConnectionState(MediaRoute2Info.CONNECTION_STATE_DISCONNECTED)
-                .setDescription(mContext.getResources().getText(
-                        R.string.bluetooth_a2dp_audio_route_name).toString())
-                .setType(type)
-                .setVolumeHandling(MediaRoute2Info.PLAYBACK_VOLUME_VARIABLE)
-                .setVolumeMax(mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC))
-                .setAddress(device.getAddress())
-                .build();
+        // Note that volume is only relevant for active bluetooth routes, and those are managed via
+        // AudioManager.
+        newBtRoute.mRoute =
+                new MediaRoute2Info.Builder(routeId, deviceName)
+                        .addFeature(MediaRoute2Info.FEATURE_LIVE_AUDIO)
+                        .addFeature(MediaRoute2Info.FEATURE_LOCAL_PLAYBACK)
+                        .setConnectionState(MediaRoute2Info.CONNECTION_STATE_DISCONNECTED)
+                        .setDescription(
+                                mContext.getResources()
+                                        .getText(R.string.bluetooth_a2dp_audio_route_name)
+                                        .toString())
+                        .setType(type)
+                        .setAddress(device.getAddress())
+                        .build();
         return newBtRoute;
     }
 
-    private void setRouteConnectionState(@NonNull BluetoothRouteInfo btRoute,
-            @MediaRoute2Info.ConnectionState int state) {
-        if (btRoute == null) {
-            Slog.w(TAG, "setRouteConnectionState: route shouldn't be null");
-            return;
-        }
-        if (btRoute.mRoute.getConnectionState() == state) {
-            return;
-        }
-
-        MediaRoute2Info.Builder builder = new MediaRoute2Info.Builder(btRoute.mRoute)
-                .setConnectionState(state);
-        builder.setType(btRoute.getRouteType());
-
-
-
-        if (state == MediaRoute2Info.CONNECTION_STATE_CONNECTED) {
-            int currentVolume;
-            synchronized (this) {
-                currentVolume = mVolumeMap.get(btRoute.getRouteType(), 0);
-            }
-            builder.setVolume(currentVolume);
-        }
-
-        btRoute.mRoute = builder.build();
-    }
-
     private static class BluetoothRouteInfo {
         private BluetoothDevice mBtDevice;
         private MediaRoute2Info mRoute;
         private SparseBooleanArray mConnectedProfiles;
-
-        @MediaRoute2Info.Type
-        int getRouteType() {
-            // Let hearing aid profile have a priority.
-            if (mConnectedProfiles.get(BluetoothProfile.HEARING_AID, false)) {
-                return MediaRoute2Info.TYPE_HEARING_AID;
-            }
-
-            if (mConnectedProfiles.get(BluetoothProfile.LE_AUDIO, false)) {
-                return MediaRoute2Info.TYPE_BLE_HEADSET;
-            }
-
-            return MediaRoute2Info.TYPE_BLUETOOTH_A2DP;
-        }
     }
 
     private class AdapterStateChangedReceiver extends BroadcastReceiver {
@@ -468,9 +295,6 @@
         @Override
         public void onReceive(Context context, Intent intent) {
             switch (intent.getAction()) {
-                case BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED:
-                case BluetoothHearingAid.ACTION_ACTIVE_DEVICE_CHANGED:
-                case BluetoothLeAudio.ACTION_LE_AUDIO_ACTIVE_DEVICE_CHANGED:
                 case BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED:
                 case BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED:
                 case BluetoothLeAudio.ACTION_LE_AUDIO_CONNECTION_STATE_CHANGED:
diff --git a/services/core/java/com/android/server/media/AudioPoliciesDeviceRouteController.java b/services/core/java/com/android/server/media/AudioPoliciesDeviceRouteController.java
index 6bdfae2..173c452 100644
--- a/services/core/java/com/android/server/media/AudioPoliciesDeviceRouteController.java
+++ b/services/core/java/com/android/server/media/AudioPoliciesDeviceRouteController.java
@@ -17,228 +17,601 @@
 package com.android.server.media;
 
 import static android.media.MediaRoute2Info.FEATURE_LIVE_AUDIO;
-import static android.media.MediaRoute2Info.FEATURE_LIVE_VIDEO;
 import static android.media.MediaRoute2Info.FEATURE_LOCAL_PLAYBACK;
-import static android.media.MediaRoute2Info.TYPE_BUILTIN_SPEAKER;
-import static android.media.MediaRoute2Info.TYPE_DOCK;
-import static android.media.MediaRoute2Info.TYPE_HDMI;
-import static android.media.MediaRoute2Info.TYPE_HDMI_ARC;
-import static android.media.MediaRoute2Info.TYPE_HDMI_EARC;
-import static android.media.MediaRoute2Info.TYPE_USB_DEVICE;
-import static android.media.MediaRoute2Info.TYPE_WIRED_HEADPHONES;
-import static android.media.MediaRoute2Info.TYPE_WIRED_HEADSET;
 
+import android.Manifest;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
+import android.annotation.RequiresPermission;
+import android.bluetooth.BluetoothAdapter;
+import android.bluetooth.BluetoothDevice;
 import android.content.Context;
+import android.media.AudioAttributes;
+import android.media.AudioDeviceAttributes;
+import android.media.AudioDeviceCallback;
+import android.media.AudioDeviceInfo;
 import android.media.AudioManager;
-import android.media.AudioRoutesInfo;
-import android.media.IAudioRoutesObserver;
-import android.media.IAudioService;
 import android.media.MediaRoute2Info;
-import android.os.RemoteException;
+import android.media.audiopolicy.AudioProductStrategy;
+import android.os.Handler;
+import android.os.HandlerExecutor;
+import android.os.Looper;
+import android.os.UserHandle;
+import android.text.TextUtils;
 import android.util.Slog;
+import android.util.SparseArray;
 
 import com.android.internal.R;
-import com.android.internal.annotations.VisibleForTesting;
+import com.android.server.media.BluetoothRouteController.NoOpBluetoothRouteController;
 
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 import java.util.Objects;
 
+/**
+ * Maintains a list of all available routes and supports transfers to any of them.
+ *
+ * <p>This implementation is intended for use in conjunction with {@link
+ * NoOpBluetoothRouteController}, as it manages bluetooth devices directly.
+ *
+ * <p>This implementation obtains and manages all routes via {@link AudioManager}, with the
+ * exception of {@link AudioManager#handleBluetoothActiveDeviceChanged inactive bluetooth} routes
+ * which are managed by {@link AudioPoliciesBluetoothRouteController}, which depends on the
+ * bluetooth stack (for example {@link BluetoothAdapter}.
+ */
+// TODO: b/305199571 - Rename this class to avoid the AudioPolicies prefix, which has been flagged
+// by the audio team as a confusing name.
 /* package */ final class AudioPoliciesDeviceRouteController implements DeviceRouteController {
-
-    private static final String TAG = "APDeviceRoutesController";
+    private static final String TAG = SystemMediaRoute2Provider.TAG;
 
     @NonNull
-    private final Context mContext;
-    @NonNull
-    private final AudioManager mAudioManager;
-    @NonNull
-    private final IAudioService mAudioService;
+    private static final AudioAttributes MEDIA_USAGE_AUDIO_ATTRIBUTES =
+            new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).build();
 
     @NonNull
-    private final OnDeviceRouteChangedListener mOnDeviceRouteChangedListener;
-    @NonNull
-    private final AudioRoutesObserver mAudioRoutesObserver = new AudioRoutesObserver();
+    private static final SparseArray<SystemRouteInfo> AUDIO_DEVICE_INFO_TYPE_TO_ROUTE_INFO =
+            new SparseArray<>();
 
-    private int mDeviceVolume;
+    @NonNull private final Context mContext;
+    @NonNull private final AudioManager mAudioManager;
+    @NonNull private final Handler mHandler;
+    @NonNull private final OnDeviceRouteChangedListener mOnDeviceRouteChangedListener;
+    @NonNull private final AudioPoliciesBluetoothRouteController mBluetoothRouteController;
 
     @NonNull
-    private MediaRoute2Info mDeviceRoute;
-    @Nullable
-    private MediaRoute2Info mSelectedRoute;
+    private final Map<String, MediaRoute2InfoHolder> mRouteIdToAvailableDeviceRoutes =
+            new HashMap<>();
 
-    @VisibleForTesting
-    /* package */ AudioPoliciesDeviceRouteController(@NonNull Context context,
+    @NonNull private final AudioProductStrategy mStrategyForMedia;
+
+    @NonNull private final AudioDeviceCallback mAudioDeviceCallback = new AudioDeviceCallbackImpl();
+
+    @NonNull
+    private final AudioManager.OnDevicesForAttributesChangedListener
+            mOnDevicesForAttributesChangedListener = this::onDevicesForAttributesChangedListener;
+
+    @NonNull private MediaRoute2Info mSelectedRoute;
+
+    // TODO: b/305199571 - Support nullable btAdapter and strategyForMedia which, when null, means
+    // no support for transferring to inactive bluetooth routes and transferring to any routes
+    // respectively.
+    @RequiresPermission(
+            anyOf = {
+                Manifest.permission.MODIFY_AUDIO_ROUTING,
+                Manifest.permission.QUERY_AUDIO_STATE
+            })
+    /* package */ AudioPoliciesDeviceRouteController(
+            @NonNull Context context,
             @NonNull AudioManager audioManager,
-            @NonNull IAudioService audioService,
+            @NonNull Looper looper,
+            @NonNull AudioProductStrategy strategyForMedia,
+            @NonNull BluetoothAdapter btAdapter,
             @NonNull OnDeviceRouteChangedListener onDeviceRouteChangedListener) {
-        Objects.requireNonNull(context);
-        Objects.requireNonNull(audioManager);
-        Objects.requireNonNull(audioService);
-        Objects.requireNonNull(onDeviceRouteChangedListener);
-
-        mContext = context;
-        mOnDeviceRouteChangedListener = onDeviceRouteChangedListener;
-
-        mAudioManager = audioManager;
-        mAudioService = audioService;
-
-        AudioRoutesInfo newAudioRoutes = null;
-        try {
-            newAudioRoutes = mAudioService.startWatchingRoutes(mAudioRoutesObserver);
-        } catch (RemoteException e) {
-            Slog.w(TAG, "Cannot connect to audio service to start listen to routes", e);
-        }
-
-        mDeviceRoute = createRouteFromAudioInfo(newAudioRoutes);
+        mContext = Objects.requireNonNull(context);
+        mAudioManager = Objects.requireNonNull(audioManager);
+        mHandler = new Handler(Objects.requireNonNull(looper));
+        mStrategyForMedia = Objects.requireNonNull(strategyForMedia);
+        mOnDeviceRouteChangedListener = Objects.requireNonNull(onDeviceRouteChangedListener);
+        mBluetoothRouteController =
+                new AudioPoliciesBluetoothRouteController(
+                        mContext, btAdapter, this::rebuildAvailableRoutesAndNotify);
+        // Just build routes but don't notify. The caller may not expect the listener to be invoked
+        // before this constructor has finished executing.
+        rebuildAvailableRoutes();
     }
 
+    @RequiresPermission(
+            anyOf = {
+                Manifest.permission.MODIFY_AUDIO_ROUTING,
+                Manifest.permission.QUERY_AUDIO_STATE
+            })
     @Override
-    public synchronized boolean selectRoute(@Nullable Integer type) {
-        if (type == null) {
-            mSelectedRoute = null;
-            return true;
-        }
+    public void start(UserHandle mUser) {
+        mBluetoothRouteController.start(mUser);
+        mAudioManager.registerAudioDeviceCallback(mAudioDeviceCallback, mHandler);
+        mAudioManager.addOnDevicesForAttributesChangedListener(
+                AudioRoutingUtils.ATTRIBUTES_MEDIA,
+                new HandlerExecutor(mHandler),
+                mOnDevicesForAttributesChangedListener);
+    }
 
-        if (!isDeviceRouteType(type)) {
-            return false;
-        }
-
-        mSelectedRoute = createRouteFromAudioInfo(type);
-        return true;
+    @RequiresPermission(
+            anyOf = {
+                Manifest.permission.MODIFY_AUDIO_ROUTING,
+                Manifest.permission.QUERY_AUDIO_STATE
+            })
+    @Override
+    public void stop() {
+        mAudioManager.removeOnDevicesForAttributesChangedListener(
+                mOnDevicesForAttributesChangedListener);
+        mAudioManager.unregisterAudioDeviceCallback(mAudioDeviceCallback);
+        mBluetoothRouteController.stop();
+        mHandler.removeCallbacksAndMessages(/* token= */ null);
     }
 
     @Override
     @NonNull
     public synchronized MediaRoute2Info getSelectedRoute() {
-        if (mSelectedRoute != null) {
-            return mSelectedRoute;
-        }
-        return mDeviceRoute;
+        return mSelectedRoute;
     }
 
     @Override
+    @NonNull
+    public synchronized List<MediaRoute2Info> getAvailableRoutes() {
+        return mRouteIdToAvailableDeviceRoutes.values().stream()
+                .map(it -> it.mMediaRoute2Info)
+                .toList();
+    }
+
+    @RequiresPermission(Manifest.permission.MODIFY_AUDIO_ROUTING)
+    @Override
+    public synchronized void transferTo(@Nullable String routeId) {
+        if (routeId == null) {
+            // This should never happen: This branch should only execute when the matching bluetooth
+            // route controller is not the no-op one.
+            // TODO: b/305199571 - Make routeId non-null and remove this branch once we remove the
+            // legacy route controller implementations.
+            Slog.e(TAG, "Unexpected call to AudioPoliciesDeviceRouteController#transferTo(null)");
+            return;
+        }
+        MediaRoute2InfoHolder mediaRoute2InfoHolder = mRouteIdToAvailableDeviceRoutes.get(routeId);
+        if (mediaRoute2InfoHolder == null) {
+            Slog.w(TAG, "transferTo: Ignoring transfer request to unknown route id : " + routeId);
+            return;
+        }
+        if (mediaRoute2InfoHolder.mCorrespondsToInactiveBluetoothRoute) {
+            // By default, the last connected device is the active route so we don't need to apply a
+            // routing audio policy.
+            mBluetoothRouteController.activateBluetoothDeviceWithAddress(
+                    mediaRoute2InfoHolder.mMediaRoute2Info.getAddress());
+            mAudioManager.removePreferredDeviceForStrategy(mStrategyForMedia);
+        } else {
+            AudioDeviceAttributes attr =
+                    new AudioDeviceAttributes(
+                            AudioDeviceAttributes.ROLE_OUTPUT,
+                            mediaRoute2InfoHolder.mAudioDeviceInfoType,
+                            /* address= */ ""); // This is not a BT device, hence no address needed.
+            mAudioManager.setPreferredDeviceForStrategy(mStrategyForMedia, attr);
+        }
+    }
+
+    @RequiresPermission(
+            anyOf = {
+                Manifest.permission.MODIFY_AUDIO_ROUTING,
+                Manifest.permission.QUERY_AUDIO_STATE
+            })
+    @Override
     public synchronized boolean updateVolume(int volume) {
-        if (mDeviceVolume == volume) {
-            return false;
-        }
-
-        mDeviceVolume = volume;
-
-        if (mSelectedRoute != null) {
-            mSelectedRoute = new MediaRoute2Info.Builder(mSelectedRoute)
-                    .setVolume(volume)
-                    .build();
-        }
-
-        mDeviceRoute = new MediaRoute2Info.Builder(mDeviceRoute)
-                .setVolume(volume)
-                .build();
-
+        // TODO: b/305199571 - Optimize so that we only update the volume of the selected route. We
+        // don't need to rebuild all available routes.
+        rebuildAvailableRoutesAndNotify();
         return true;
     }
 
-    @NonNull
-    private MediaRoute2Info createRouteFromAudioInfo(@Nullable AudioRoutesInfo newRoutes) {
-        int type = TYPE_BUILTIN_SPEAKER;
+    @RequiresPermission(
+            anyOf = {
+                Manifest.permission.MODIFY_AUDIO_ROUTING,
+                Manifest.permission.QUERY_AUDIO_STATE
+            })
+    private void onDevicesForAttributesChangedListener(
+            AudioAttributes attributes, List<AudioDeviceAttributes> unusedAudioDeviceAttributes) {
+        if (attributes.getUsage() == AudioAttributes.USAGE_MEDIA) {
+            // We only care about the media usage. Ignore everything else.
+            rebuildAvailableRoutesAndNotify();
+        }
+    }
 
-        if (newRoutes != null) {
-            if ((newRoutes.mainType & AudioRoutesInfo.MAIN_HEADPHONES) != 0) {
-                type = TYPE_WIRED_HEADPHONES;
-            } else if ((newRoutes.mainType & AudioRoutesInfo.MAIN_HEADSET) != 0) {
-                type = TYPE_WIRED_HEADSET;
-            } else if ((newRoutes.mainType & AudioRoutesInfo.MAIN_DOCK_SPEAKERS) != 0) {
-                type = TYPE_DOCK;
-            } else if ((newRoutes.mainType & AudioRoutesInfo.MAIN_HDMI) != 0) {
-                type = TYPE_HDMI;
-            } else if ((newRoutes.mainType & AudioRoutesInfo.MAIN_USB) != 0) {
-                type = TYPE_USB_DEVICE;
+    @RequiresPermission(
+            anyOf = {
+                Manifest.permission.MODIFY_AUDIO_ROUTING,
+                Manifest.permission.QUERY_AUDIO_STATE
+            })
+    private synchronized void rebuildAvailableRoutesAndNotify() {
+        rebuildAvailableRoutes();
+        mOnDeviceRouteChangedListener.onDeviceRouteChanged();
+    }
+
+    @RequiresPermission(
+            anyOf = {
+                Manifest.permission.MODIFY_AUDIO_ROUTING,
+                Manifest.permission.QUERY_AUDIO_STATE
+            })
+    private synchronized void rebuildAvailableRoutes() {
+        List<AudioDeviceAttributes> attributesOfSelectedOutputDevices =
+                mAudioManager.getDevicesForAttributes(MEDIA_USAGE_AUDIO_ATTRIBUTES);
+        int selectedDeviceAttributesType;
+        if (attributesOfSelectedOutputDevices.isEmpty()) {
+            Slog.e(
+                    TAG,
+                    "Unexpected empty list of output devices for media. Using built-in speakers.");
+            selectedDeviceAttributesType = AudioDeviceInfo.TYPE_BUILTIN_SPEAKER;
+        } else {
+            if (attributesOfSelectedOutputDevices.size() > 1) {
+                Slog.w(
+                        TAG,
+                        "AudioManager.getDevicesForAttributes returned more than one element. Using"
+                                + " the first one.");
+            }
+            selectedDeviceAttributesType = attributesOfSelectedOutputDevices.get(0).getType();
+        }
+
+        AudioDeviceInfo[] audioDeviceInfos =
+                mAudioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);
+        mRouteIdToAvailableDeviceRoutes.clear();
+        MediaRoute2InfoHolder newSelectedRouteHolder = null;
+        for (AudioDeviceInfo audioDeviceInfo : audioDeviceInfos) {
+            MediaRoute2Info mediaRoute2Info =
+                    createMediaRoute2InfoFromAudioDeviceInfo(audioDeviceInfo);
+            // Null means audioDeviceInfo is not a supported media output, like a phone's builtin
+            // earpiece. We ignore those.
+            if (mediaRoute2Info != null) {
+                int audioDeviceInfoType = audioDeviceInfo.getType();
+                MediaRoute2InfoHolder newHolder =
+                        MediaRoute2InfoHolder.createForAudioManagerRoute(
+                                mediaRoute2Info, audioDeviceInfoType);
+                mRouteIdToAvailableDeviceRoutes.put(mediaRoute2Info.getId(), newHolder);
+                if (selectedDeviceAttributesType == audioDeviceInfoType) {
+                    newSelectedRouteHolder = newHolder;
+                }
             }
         }
 
-        return createRouteFromAudioInfo(type);
-    }
-
-    @NonNull
-    private MediaRoute2Info createRouteFromAudioInfo(@MediaRoute2Info.Type int type) {
-        int name = R.string.default_audio_route_name;
-        switch (type) {
-            case TYPE_WIRED_HEADPHONES:
-            case TYPE_WIRED_HEADSET:
-                name = R.string.default_audio_route_name_headphones;
-                break;
-            case TYPE_DOCK:
-                name = R.string.default_audio_route_name_dock_speakers;
-                break;
-            case TYPE_HDMI:
-            case TYPE_HDMI_ARC:
-            case TYPE_HDMI_EARC:
-                name = R.string.default_audio_route_name_external_device;
-                break;
-            case TYPE_USB_DEVICE:
-                name = R.string.default_audio_route_name_usb;
-                break;
+        if (mRouteIdToAvailableDeviceRoutes.isEmpty()) {
+            // Due to an unknown reason (possibly an audio server crash), we ended up with an empty
+            // list of routes. Our entire codebase assumes at least one system route always exists,
+            // so we create a placeholder route represented as a built-in speaker for
+            // user-presentation purposes.
+            Slog.e(TAG, "Ended up with an empty list of routes. Creating a placeholder route.");
+            MediaRoute2InfoHolder placeholderRouteHolder = createPlaceholderBuiltinSpeakerRoute();
+            String placeholderRouteId = placeholderRouteHolder.mMediaRoute2Info.getId();
+            mRouteIdToAvailableDeviceRoutes.put(placeholderRouteId, placeholderRouteHolder);
         }
 
-        synchronized (this) {
-            return new MediaRoute2Info.Builder(
-                            MediaRoute2Info.ROUTE_ID_DEVICE,
-                            mContext.getResources().getText(name).toString())
-                    .setVolumeHandling(
-                            mAudioManager.isVolumeFixed()
-                                    ? MediaRoute2Info.PLAYBACK_VOLUME_FIXED
-                                    : MediaRoute2Info.PLAYBACK_VOLUME_VARIABLE)
-                    .setVolume(mDeviceVolume)
-                    .setVolumeMax(mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC))
-                    .setType(type)
-                    .addFeature(FEATURE_LIVE_AUDIO)
-                    .addFeature(FEATURE_LIVE_VIDEO)
-                    .addFeature(FEATURE_LOCAL_PLAYBACK)
-                    .setConnectionState(MediaRoute2Info.CONNECTION_STATE_CONNECTED)
-                    .build();
+        if (newSelectedRouteHolder == null) {
+            Slog.e(
+                    TAG,
+                    "Could not map this selected device attribute type to an available route: "
+                            + selectedDeviceAttributesType);
+            // We know mRouteIdToAvailableDeviceRoutes is not empty.
+            newSelectedRouteHolder = mRouteIdToAvailableDeviceRoutes.values().iterator().next();
+        }
+        MediaRoute2InfoHolder selectedRouteHolderWithUpdatedVolumeInfo =
+                newSelectedRouteHolder.copyWithVolumeInfoFromAudioManager(mAudioManager);
+        mRouteIdToAvailableDeviceRoutes.put(
+                newSelectedRouteHolder.mMediaRoute2Info.getId(),
+                selectedRouteHolderWithUpdatedVolumeInfo);
+        mSelectedRoute = selectedRouteHolderWithUpdatedVolumeInfo.mMediaRoute2Info;
+
+        // We only add those BT routes that we have not already obtained from audio manager (which
+        // are active).
+        mBluetoothRouteController.getAvailableBluetoothRoutes().stream()
+                .filter(it -> !mRouteIdToAvailableDeviceRoutes.containsKey(it.getId()))
+                .map(MediaRoute2InfoHolder::createForInactiveBluetoothRoute)
+                .forEach(
+                        it -> mRouteIdToAvailableDeviceRoutes.put(it.mMediaRoute2Info.getId(), it));
+    }
+
+    private MediaRoute2InfoHolder createPlaceholderBuiltinSpeakerRoute() {
+        int type = AudioDeviceInfo.TYPE_BUILTIN_SPEAKER;
+        return MediaRoute2InfoHolder.createForAudioManagerRoute(
+                createMediaRoute2Info(
+                        /* routeId= */ null, type, /* productName= */ null, /* address= */ null),
+                type);
+    }
+
+    @Nullable
+    private MediaRoute2Info createMediaRoute2InfoFromAudioDeviceInfo(
+            AudioDeviceInfo audioDeviceInfo) {
+        String address = audioDeviceInfo.getAddress();
+        // Passing a null route id means we want to get the default id for the route. Generally, we
+        // only expect to pass null for non-Bluetooth routes.
+        String routeId =
+                TextUtils.isEmpty(address)
+                        ? null
+                        : mBluetoothRouteController.getRouteIdForBluetoothAddress(address);
+        return createMediaRoute2Info(
+                routeId, audioDeviceInfo.getType(), audioDeviceInfo.getProductName(), address);
+    }
+
+    /**
+     * Creates a new {@link MediaRoute2Info} using the provided information.
+     *
+     * @param routeId A route id, or null to use an id pre-defined for the given {@code type}.
+     * @param audioDeviceInfoType The type as obtained from {@link AudioDeviceInfo#getType}.
+     * @param productName The product name as obtained from {@link
+     *     AudioDeviceInfo#getProductName()}, or null to use a predefined name for the given {@code
+     *     type}.
+     * @param address The type as obtained from {@link AudioDeviceInfo#getAddress()} or {@link
+     *     BluetoothDevice#getAddress()}.
+     * @return The new {@link MediaRoute2Info}.
+     */
+    @Nullable
+    private MediaRoute2Info createMediaRoute2Info(
+            @Nullable String routeId,
+            int audioDeviceInfoType,
+            @Nullable CharSequence productName,
+            @Nullable String address) {
+        SystemRouteInfo systemRouteInfo =
+                AUDIO_DEVICE_INFO_TYPE_TO_ROUTE_INFO.get(audioDeviceInfoType);
+        if (systemRouteInfo == null) {
+            // Device type that's intentionally unsupported for media output, like the built-in
+            // earpiece.
+            return null;
+        }
+        CharSequence humanReadableName = productName;
+        if (TextUtils.isEmpty(humanReadableName)) {
+            humanReadableName = mContext.getResources().getText(systemRouteInfo.mNameResource);
+        }
+        if (routeId == null) {
+            // The caller hasn't provided an id, so we use a pre-defined one. This happens when we
+            // are creating a non-BT route, or we are creating a BT route but a race condition
+            // caused AudioManager to expose the BT route before BluetoothAdapter, preventing us
+            // from getting an id using BluetoothRouteController#getRouteIdForBluetoothAddress.
+            routeId = systemRouteInfo.mDefaultRouteId;
+        }
+        return new MediaRoute2Info.Builder(routeId, humanReadableName)
+                .setType(systemRouteInfo.mMediaRoute2InfoType)
+                .setAddress(address)
+                .setSystemRoute(true)
+                .addFeature(FEATURE_LIVE_AUDIO)
+                .addFeature(FEATURE_LOCAL_PLAYBACK)
+                .setConnectionState(MediaRoute2Info.CONNECTION_STATE_CONNECTED)
+                .build();
+    }
+
+    /**
+     * Holds a {@link MediaRoute2Info} and associated information that we don't want to put in the
+     * {@link MediaRoute2Info} class because it's solely necessary for the implementation of this
+     * class.
+     */
+    private static class MediaRoute2InfoHolder {
+
+        public final MediaRoute2Info mMediaRoute2Info;
+        public final int mAudioDeviceInfoType;
+        public final boolean mCorrespondsToInactiveBluetoothRoute;
+
+        public static MediaRoute2InfoHolder createForAudioManagerRoute(
+                MediaRoute2Info mediaRoute2Info, int audioDeviceInfoType) {
+            return new MediaRoute2InfoHolder(
+                    mediaRoute2Info,
+                    audioDeviceInfoType,
+                    /* correspondsToInactiveBluetoothRoute= */ false);
+        }
+
+        public static MediaRoute2InfoHolder createForInactiveBluetoothRoute(
+                MediaRoute2Info mediaRoute2Info) {
+            // There's no corresponding audio device info, hence the audio device info type is
+            // unknown.
+            return new MediaRoute2InfoHolder(
+                    mediaRoute2Info,
+                    /* audioDeviceInfoType= */ AudioDeviceInfo.TYPE_UNKNOWN,
+                    /* correspondsToInactiveBluetoothRoute= */ true);
+        }
+
+        private MediaRoute2InfoHolder(
+                MediaRoute2Info mediaRoute2Info,
+                int audioDeviceInfoType,
+                boolean correspondsToInactiveBluetoothRoute) {
+            mMediaRoute2Info = mediaRoute2Info;
+            mAudioDeviceInfoType = audioDeviceInfoType;
+            mCorrespondsToInactiveBluetoothRoute = correspondsToInactiveBluetoothRoute;
+        }
+
+        public MediaRoute2InfoHolder copyWithVolumeInfoFromAudioManager(
+                AudioManager mAudioManager) {
+            MediaRoute2Info routeInfoWithVolumeInfo =
+                    new MediaRoute2Info.Builder(mMediaRoute2Info)
+                            .setVolumeHandling(
+                                    mAudioManager.isVolumeFixed()
+                                            ? MediaRoute2Info.PLAYBACK_VOLUME_FIXED
+                                            : MediaRoute2Info.PLAYBACK_VOLUME_VARIABLE)
+                            .setVolume(mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC))
+                            .setVolumeMax(
+                                    mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC))
+                            .build();
+            return new MediaRoute2InfoHolder(
+                    routeInfoWithVolumeInfo,
+                    mAudioDeviceInfoType,
+                    mCorrespondsToInactiveBluetoothRoute);
         }
     }
 
     /**
-     * Checks if the given type is a device route.
-     *
-     * <p>Device route means a route which is either built-in or wired to the current device.
-     *
-     * @param type specifies the type of the device.
-     * @return {@code true} if the device is wired or built-in and {@code false} otherwise.
+     * Holds route information about an {@link AudioDeviceInfo#getType() audio device info type}.
      */
-    private boolean isDeviceRouteType(@MediaRoute2Info.Type int type) {
-        switch (type) {
-            case TYPE_BUILTIN_SPEAKER:
-            case TYPE_WIRED_HEADPHONES:
-            case TYPE_WIRED_HEADSET:
-            case TYPE_DOCK:
-            case TYPE_HDMI:
-            case TYPE_HDMI_ARC:
-            case TYPE_HDMI_EARC:
-            case TYPE_USB_DEVICE:
-                return true;
-            default:
-                return false;
+    private static class SystemRouteInfo {
+        /** The type to use for {@link MediaRoute2Info#getType()}. */
+        public final int mMediaRoute2InfoType;
+
+        /**
+         * Holds the route id to use if no other id is provided.
+         *
+         * <p>We only expect this id to be used for non-bluetooth routes. For bluetooth routes, in a
+         * normal scenario, the id is generated from the device information (like address, or
+         * hiSyncId), and this value is ignored. A non-normal scenario may occur when there's race
+         * condition between {@link BluetoothAdapter} and {@link AudioManager}, who are not
+         * synchronized.
+         */
+        public final String mDefaultRouteId;
+
+        /**
+         * The name to use for {@link MediaRoute2Info#getName()}.
+         *
+         * <p>Usually replaced by the UI layer with a localized string.
+         */
+        public final int mNameResource;
+
+        private SystemRouteInfo(int mediaRoute2InfoType, String defaultRouteId, int nameResource) {
+            mMediaRoute2InfoType = mediaRoute2InfoType;
+            mDefaultRouteId = defaultRouteId;
+            mNameResource = nameResource;
         }
     }
 
-    private class AudioRoutesObserver extends IAudioRoutesObserver.Stub {
-
+    private class AudioDeviceCallbackImpl extends AudioDeviceCallback {
+        @RequiresPermission(Manifest.permission.MODIFY_AUDIO_ROUTING)
         @Override
-        public void dispatchAudioRoutesChanged(AudioRoutesInfo newAudioRoutes) {
-            boolean isDeviceRouteChanged;
-            MediaRoute2Info deviceRoute = createRouteFromAudioInfo(newAudioRoutes);
-
-            synchronized (AudioPoliciesDeviceRouteController.this) {
-                mDeviceRoute = deviceRoute;
-                isDeviceRouteChanged = mSelectedRoute == null;
+        public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) {
+            for (AudioDeviceInfo deviceInfo : addedDevices) {
+                if (AUDIO_DEVICE_INFO_TYPE_TO_ROUTE_INFO.contains(deviceInfo.getType())) {
+                    // When a new valid media output is connected, we clear any routing policies so
+                    // that the default routing logic from the audio framework kicks in. As a result
+                    // of this, when the user connects a bluetooth device or a wired headset, the
+                    // new device becomes the active route, which is the traditional behavior.
+                    mAudioManager.removePreferredDeviceForStrategy(mStrategyForMedia);
+                    rebuildAvailableRoutesAndNotify();
+                    break;
+                }
             }
+        }
 
-            if (isDeviceRouteChanged) {
-                mOnDeviceRouteChangedListener.onDeviceRouteChanged();
+        @RequiresPermission(
+                anyOf = {
+                    Manifest.permission.MODIFY_AUDIO_ROUTING,
+                    Manifest.permission.QUERY_AUDIO_STATE
+                })
+        @Override
+        public void onAudioDevicesRemoved(AudioDeviceInfo[] removedDevices) {
+            for (AudioDeviceInfo deviceInfo : removedDevices) {
+                if (AUDIO_DEVICE_INFO_TYPE_TO_ROUTE_INFO.contains(deviceInfo.getType())) {
+                    rebuildAvailableRoutesAndNotify();
+                    break;
+                }
             }
         }
     }
 
+    static {
+        AUDIO_DEVICE_INFO_TYPE_TO_ROUTE_INFO.put(
+                AudioDeviceInfo.TYPE_BUILTIN_SPEAKER,
+                new SystemRouteInfo(
+                        MediaRoute2Info.TYPE_BUILTIN_SPEAKER,
+                        /* defaultRouteId= */ "ROUTE_ID_BUILTIN_SPEAKER",
+                        /* nameResource= */ R.string.default_audio_route_name));
+        AUDIO_DEVICE_INFO_TYPE_TO_ROUTE_INFO.put(
+                AudioDeviceInfo.TYPE_WIRED_HEADSET,
+                new SystemRouteInfo(
+                        MediaRoute2Info.TYPE_WIRED_HEADSET,
+                        /* defaultRouteId= */ "ROUTE_ID_WIRED_HEADSET",
+                        /* nameResource= */ R.string.default_audio_route_name_headphones));
+        AUDIO_DEVICE_INFO_TYPE_TO_ROUTE_INFO.put(
+                AudioDeviceInfo.TYPE_WIRED_HEADPHONES,
+                new SystemRouteInfo(
+                        MediaRoute2Info.TYPE_WIRED_HEADPHONES,
+                        /* defaultRouteId= */ "ROUTE_ID_WIRED_HEADPHONES",
+                        /* nameResource= */ R.string.default_audio_route_name_headphones));
+        AUDIO_DEVICE_INFO_TYPE_TO_ROUTE_INFO.put(
+                AudioDeviceInfo.TYPE_BLUETOOTH_A2DP,
+                new SystemRouteInfo(
+                        MediaRoute2Info.TYPE_BLUETOOTH_A2DP,
+                        /* defaultRouteId= */ "ROUTE_ID_BLUETOOTH_A2DP",
+                        /* nameResource= */ R.string.bluetooth_a2dp_audio_route_name));
+        AUDIO_DEVICE_INFO_TYPE_TO_ROUTE_INFO.put(
+                AudioDeviceInfo.TYPE_HDMI,
+                new SystemRouteInfo(
+                        MediaRoute2Info.TYPE_HDMI,
+                        /* defaultRouteId= */ "ROUTE_ID_HDMI",
+                        /* nameResource= */ R.string.default_audio_route_name_external_device));
+        AUDIO_DEVICE_INFO_TYPE_TO_ROUTE_INFO.put(
+                AudioDeviceInfo.TYPE_DOCK,
+                new SystemRouteInfo(
+                        MediaRoute2Info.TYPE_DOCK,
+                        /* defaultRouteId= */ "ROUTE_ID_DOCK",
+                        /* nameResource= */ R.string.default_audio_route_name_dock_speakers));
+        AUDIO_DEVICE_INFO_TYPE_TO_ROUTE_INFO.put(
+                AudioDeviceInfo.TYPE_USB_DEVICE,
+                new SystemRouteInfo(
+                        MediaRoute2Info.TYPE_USB_DEVICE,
+                        /* defaultRouteId= */ "ROUTE_ID_USB_DEVICE",
+                        /* nameResource= */ R.string.default_audio_route_name_usb));
+        AUDIO_DEVICE_INFO_TYPE_TO_ROUTE_INFO.put(
+                AudioDeviceInfo.TYPE_USB_HEADSET,
+                new SystemRouteInfo(
+                        MediaRoute2Info.TYPE_USB_HEADSET,
+                        /* defaultRouteId= */ "ROUTE_ID_USB_HEADSET",
+                        /* nameResource= */ R.string.default_audio_route_name_usb));
+        AUDIO_DEVICE_INFO_TYPE_TO_ROUTE_INFO.put(
+                AudioDeviceInfo.TYPE_HDMI_ARC,
+                new SystemRouteInfo(
+                        MediaRoute2Info.TYPE_HDMI_ARC,
+                        /* defaultRouteId= */ "ROUTE_ID_HDMI_ARC",
+                        /* nameResource= */ R.string.default_audio_route_name_external_device));
+        AUDIO_DEVICE_INFO_TYPE_TO_ROUTE_INFO.put(
+                AudioDeviceInfo.TYPE_HDMI_EARC,
+                new SystemRouteInfo(
+                        MediaRoute2Info.TYPE_HDMI_EARC,
+                        /* defaultRouteId= */ "ROUTE_ID_HDMI_EARC",
+                        /* nameResource= */ R.string.default_audio_route_name_external_device));
+        // TODO: b/305199571 - Add a proper type constants and human readable names for AUX_LINE,
+        // LINE_ANALOG, LINE_DIGITAL, BLE_BROADCAST, BLE_SPEAKER, BLE_HEADSET, and HEARING_AID.
+        AUDIO_DEVICE_INFO_TYPE_TO_ROUTE_INFO.put(
+                AudioDeviceInfo.TYPE_HEARING_AID,
+                new SystemRouteInfo(
+                        MediaRoute2Info.TYPE_HEARING_AID,
+                        /* defaultRouteId= */ "ROUTE_ID_HEARING_AID",
+                        /* nameResource= */ R.string.bluetooth_a2dp_audio_route_name));
+        AUDIO_DEVICE_INFO_TYPE_TO_ROUTE_INFO.put(
+                AudioDeviceInfo.TYPE_BLE_HEADSET,
+                new SystemRouteInfo(
+                        MediaRoute2Info.TYPE_BLE_HEADSET,
+                        /* defaultRouteId= */ "ROUTE_ID_BLE_HEADSET",
+                        /* nameResource= */ R.string.bluetooth_a2dp_audio_route_name));
+        AUDIO_DEVICE_INFO_TYPE_TO_ROUTE_INFO.put(
+                AudioDeviceInfo.TYPE_BLE_SPEAKER,
+                new SystemRouteInfo(
+                        MediaRoute2Info.TYPE_BLE_HEADSET, // TODO: b/305199571 - Make a new type.
+                        /* defaultRouteId= */ "ROUTE_ID_BLE_SPEAKER",
+                        /* nameResource= */ R.string.bluetooth_a2dp_audio_route_name));
+        AUDIO_DEVICE_INFO_TYPE_TO_ROUTE_INFO.put(
+                AudioDeviceInfo.TYPE_BLE_BROADCAST,
+                new SystemRouteInfo(
+                        MediaRoute2Info.TYPE_BLE_HEADSET,
+                        /* defaultRouteId= */ "ROUTE_ID_BLE_BROADCAST",
+                        /* nameResource= */ R.string.bluetooth_a2dp_audio_route_name));
+        AUDIO_DEVICE_INFO_TYPE_TO_ROUTE_INFO.put(
+                AudioDeviceInfo.TYPE_LINE_DIGITAL,
+                new SystemRouteInfo(
+                        MediaRoute2Info.TYPE_UNKNOWN,
+                        /* defaultRouteId= */ "ROUTE_ID_LINE_DIGITAL",
+                        /* nameResource= */ R.string.default_audio_route_name_external_device));
+        AUDIO_DEVICE_INFO_TYPE_TO_ROUTE_INFO.put(
+                AudioDeviceInfo.TYPE_LINE_ANALOG,
+                new SystemRouteInfo(
+                        MediaRoute2Info.TYPE_UNKNOWN,
+                        /* defaultRouteId= */ "ROUTE_ID_LINE_ANALOG",
+                        /* nameResource= */ R.string.default_audio_route_name_external_device));
+        AUDIO_DEVICE_INFO_TYPE_TO_ROUTE_INFO.put(
+                AudioDeviceInfo.TYPE_AUX_LINE,
+                new SystemRouteInfo(
+                        MediaRoute2Info.TYPE_UNKNOWN,
+                        /* defaultRouteId= */ "ROUTE_ID_AUX_LINE",
+                        /* nameResource= */ R.string.default_audio_route_name_external_device));
+        AUDIO_DEVICE_INFO_TYPE_TO_ROUTE_INFO.put(
+                AudioDeviceInfo.TYPE_DOCK_ANALOG,
+                new SystemRouteInfo(
+                        MediaRoute2Info.TYPE_DOCK,
+                        /* defaultRouteId= */ "ROUTE_ID_DOCK_ANALOG",
+                        /* nameResource= */ R.string.default_audio_route_name_dock_speakers));
+    }
 }
diff --git a/services/core/java/com/android/server/media/AudioRoutingUtils.java b/services/core/java/com/android/server/media/AudioRoutingUtils.java
new file mode 100644
index 0000000..13f11eb
--- /dev/null
+++ b/services/core/java/com/android/server/media/AudioRoutingUtils.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2023 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.media;
+
+import android.Manifest;
+import android.annotation.Nullable;
+import android.annotation.RequiresPermission;
+import android.media.AudioAttributes;
+import android.media.AudioManager;
+import android.media.audiopolicy.AudioProductStrategy;
+
+/** Holds utils related to routing in the audio framework. */
+/* package */ final class AudioRoutingUtils {
+
+    /* package */ static final AudioAttributes ATTRIBUTES_MEDIA =
+            new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).build();
+
+    @RequiresPermission(Manifest.permission.MODIFY_AUDIO_ROUTING)
+    @Nullable
+    /* package */ static AudioProductStrategy getMediaAudioProductStrategy() {
+        for (AudioProductStrategy strategy : AudioManager.getAudioProductStrategies()) {
+            if (strategy.supportsAudioAttributes(AudioRoutingUtils.ATTRIBUTES_MEDIA)) {
+                return strategy;
+            }
+        }
+        return null;
+    }
+
+    private AudioRoutingUtils() {
+        // no-op to prevent instantiation.
+    }
+}
diff --git a/services/core/java/com/android/server/media/BluetoothRouteController.java b/services/core/java/com/android/server/media/BluetoothRouteController.java
index 2b01001..74fdf6e 100644
--- a/services/core/java/com/android/server/media/BluetoothRouteController.java
+++ b/services/core/java/com/android/server/media/BluetoothRouteController.java
@@ -44,19 +44,11 @@
     @NonNull
     static BluetoothRouteController createInstance(@NonNull Context context,
             @NonNull BluetoothRouteController.BluetoothRoutesUpdatedListener listener) {
-        Objects.requireNonNull(context);
         Objects.requireNonNull(listener);
+        BluetoothAdapter btAdapter = context.getSystemService(BluetoothManager.class).getAdapter();
 
-        BluetoothManager bluetoothManager = (BluetoothManager)
-                context.getSystemService(Context.BLUETOOTH_SERVICE);
-        BluetoothAdapter btAdapter = bluetoothManager.getAdapter();
-
-        if (btAdapter == null) {
+        if (btAdapter == null || Flags.enableAudioPoliciesDeviceAndBluetoothController()) {
             return new NoOpBluetoothRouteController();
-        }
-
-        if (Flags.enableAudioPoliciesDeviceAndBluetoothController()) {
-            return new AudioPoliciesBluetoothRouteController(context, btAdapter, listener);
         } else {
             return new LegacyBluetoothRouteController(context, btAdapter, listener);
         }
@@ -74,17 +66,6 @@
      */
     void stop();
 
-
-    /**
-     * Selects the route with the given {@code deviceAddress}.
-     *
-     * @param deviceAddress The physical address of the device to select. May be null to unselect
-     *                      the currently selected device.
-     * @return Whether the selection succeeds. If the selection fails, the state of the instance
-     * remains unaltered.
-     */
-    boolean selectRoute(@Nullable String deviceAddress);
-
     /**
      * Transfers Bluetooth output to the given route.
      *
@@ -158,12 +139,6 @@
         }
 
         @Override
-        public boolean selectRoute(String deviceAddress) {
-            // no op
-            return false;
-        }
-
-        @Override
         public void transferTo(String routeId) {
             // no op
         }
diff --git a/services/core/java/com/android/server/media/DeviceRouteController.java b/services/core/java/com/android/server/media/DeviceRouteController.java
index 0fdaaa7..9f175a9 100644
--- a/services/core/java/com/android/server/media/DeviceRouteController.java
+++ b/services/core/java/com/android/server/media/DeviceRouteController.java
@@ -16,17 +16,25 @@
 
 package com.android.server.media;
 
+import android.Manifest;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
+import android.annotation.RequiresPermission;
+import android.bluetooth.BluetoothAdapter;
+import android.bluetooth.BluetoothManager;
 import android.content.Context;
 import android.media.AudioManager;
-import android.media.IAudioRoutesObserver;
 import android.media.IAudioService;
 import android.media.MediaRoute2Info;
+import android.media.audiopolicy.AudioProductStrategy;
+import android.os.Looper;
 import android.os.ServiceManager;
+import android.os.UserHandle;
 
 import com.android.media.flags.Flags;
 
+import java.util.List;
+
 /**
  * Controls device routes.
  *
@@ -37,46 +45,67 @@
  */
 /* package */ interface DeviceRouteController {
 
-    /**
-     * Returns a new instance of {@link DeviceRouteController}.
-     */
-    /* package */ static DeviceRouteController createInstance(@NonNull Context context,
+    /** Returns a new instance of {@link DeviceRouteController}. */
+    @RequiresPermission(Manifest.permission.MODIFY_AUDIO_ROUTING)
+    /* package */ static DeviceRouteController createInstance(
+            @NonNull Context context,
+            @NonNull Looper looper,
             @NonNull OnDeviceRouteChangedListener onDeviceRouteChangedListener) {
         AudioManager audioManager = context.getSystemService(AudioManager.class);
-        IAudioService audioService = IAudioService.Stub.asInterface(
-                ServiceManager.getService(Context.AUDIO_SERVICE));
+        AudioProductStrategy strategyForMedia = AudioRoutingUtils.getMediaAudioProductStrategy();
 
-        if (Flags.enableAudioPoliciesDeviceAndBluetoothController()) {
-            return new AudioPoliciesDeviceRouteController(context,
+        BluetoothManager bluetoothManager = context.getSystemService(BluetoothManager.class);
+        BluetoothAdapter btAdapter =
+                bluetoothManager != null ? bluetoothManager.getAdapter() : null;
+
+        // TODO: b/305199571 - Make the audio policies implementation work without the need for a
+        // bluetooth adapter or a strategy for media. If no strategy for media is available we can
+        // disallow media router transfers, and without a bluetooth adapter we can remove support
+        // for transfers to inactive bluetooth routes.
+        if (strategyForMedia != null
+                && btAdapter != null
+                && Flags.enableAudioPoliciesDeviceAndBluetoothController()) {
+            return new AudioPoliciesDeviceRouteController(
+                    context,
                     audioManager,
-                    audioService,
+                    looper,
+                    strategyForMedia,
+                    btAdapter,
                     onDeviceRouteChangedListener);
         } else {
-            return new LegacyDeviceRouteController(context,
-                    audioManager,
-                    audioService,
-                    onDeviceRouteChangedListener);
+            IAudioService audioService =
+                    IAudioService.Stub.asInterface(
+                            ServiceManager.getService(Context.AUDIO_SERVICE));
+            return new LegacyDeviceRouteController(
+                    context, audioManager, audioService, onDeviceRouteChangedListener);
         }
     }
 
-    /**
-     * Select the route with the given built-in or wired {@link MediaRoute2Info.Type}.
-     *
-     * <p>If the type is {@code null} then unselects the route and falls back to the default device
-     * route observed from
-     * {@link com.android.server.audio.AudioService#startWatchingRoutes(IAudioRoutesObserver)}.
-     *
-     * @param type device type. May be {@code null} to unselect currently selected route.
-     * @return whether the selection succeeds. If the selection fails the state of the controller
-     * remains intact.
-     */
-    boolean selectRoute(@Nullable @MediaRoute2Info.Type Integer type);
-
     /** Returns the currently selected device (built-in or wired) route. */
     @NonNull
     MediaRoute2Info getSelectedRoute();
 
     /**
+     * Returns all available routes.
+     *
+     * <p>Note that this method returns available routes including the selected route because (a)
+     * this interface doesn't guarantee that the internal state of the controller won't change
+     * between calls to {@link #getSelectedRoute()} and this method and (b) {@link
+     * #getSelectedRoute()} may be treated as a transferable route (not a selected route) if the
+     * selected route is from {@link BluetoothRouteController}.
+     */
+    List<MediaRoute2Info> getAvailableRoutes();
+
+    /**
+     * Transfers device output to the given route.
+     *
+     * <p>If the route is {@code null} then active route will be deactivated.
+     *
+     * @param routeId to switch to or {@code null} to unset the active device.
+     */
+    void transferTo(@Nullable String routeId);
+
+    /**
      * Updates device route volume.
      *
      * @param volume specifies a volume for the device route or 0 for unknown.
@@ -85,6 +114,18 @@
     boolean updateVolume(int volume);
 
     /**
+     * Starts listening for changes in the system to keep an up to date view of available and
+     * selected devices.
+     */
+    void start(UserHandle mUser);
+
+    /**
+     * Stops keeping the internal state up to date with the system, releasing any resources acquired
+     * in {@link #start}
+     */
+    void stop();
+
+    /**
      * Interface for receiving events when device route has changed.
      */
     interface OnDeviceRouteChangedListener {
diff --git a/services/core/java/com/android/server/media/LegacyBluetoothRouteController.java b/services/core/java/com/android/server/media/LegacyBluetoothRouteController.java
index ba3cecf..041fceaf 100644
--- a/services/core/java/com/android/server/media/LegacyBluetoothRouteController.java
+++ b/services/core/java/com/android/server/media/LegacyBluetoothRouteController.java
@@ -132,12 +132,6 @@
         mContext.unregisterReceiver(mDeviceStateChangedReceiver);
     }
 
-    @Override
-    public boolean selectRoute(String deviceAddress) {
-        // No-op as the class decides if a route is selected based on Bluetooth events.
-        return false;
-    }
-
     /**
      * Transfers to a given bluetooth route.
      * The dedicated BT device with the route would be activated.
diff --git a/services/core/java/com/android/server/media/LegacyDeviceRouteController.java b/services/core/java/com/android/server/media/LegacyDeviceRouteController.java
index 65874e2..c0f2834 100644
--- a/services/core/java/com/android/server/media/LegacyDeviceRouteController.java
+++ b/services/core/java/com/android/server/media/LegacyDeviceRouteController.java
@@ -35,11 +35,13 @@
 import android.media.IAudioService;
 import android.media.MediaRoute2Info;
 import android.os.RemoteException;
+import android.os.UserHandle;
 import android.util.Slog;
 
 import com.android.internal.R;
-import com.android.internal.annotations.VisibleForTesting;
 
+import java.util.Collections;
+import java.util.List;
 import java.util.Objects;
 
 /**
@@ -73,7 +75,6 @@
     private int mDeviceVolume;
     private MediaRoute2Info mDeviceRoute;
 
-    @VisibleForTesting
     /* package */ LegacyDeviceRouteController(@NonNull Context context,
             @NonNull AudioManager audioManager,
             @NonNull IAudioService audioService,
@@ -100,9 +101,13 @@
     }
 
     @Override
-    public boolean selectRoute(@Nullable Integer type) {
-        // No-op as the controller does not support selection from the outside of the class.
-        return false;
+    public void start(UserHandle mUser) {
+        // Nothing to do.
+    }
+
+    @Override
+    public void stop() {
+        // Nothing to do.
     }
 
     @Override
@@ -112,6 +117,17 @@
     }
 
     @Override
+    public synchronized List<MediaRoute2Info> getAvailableRoutes() {
+        return Collections.emptyList();
+    }
+
+    @Override
+    public synchronized void transferTo(@Nullable String routeId) {
+        // Unsupported. This implementation doesn't support transferable routes (always exposes a
+        // single non-bluetooth route).
+    }
+
+    @Override
     public synchronized boolean updateVolume(int volume) {
         if (mDeviceVolume == volume) {
             return false;
diff --git a/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java b/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java
index 4821fbe..90c4063 100644
--- a/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java
+++ b/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java
@@ -193,26 +193,6 @@
 
     // Start of methods that implement MediaRouter2 operations.
 
-    @RequiresPermission(Manifest.permission.MEDIA_CONTENT_CONTROL)
-    @NonNull
-    public boolean verifyPackageExists(@NonNull String clientPackageName) {
-        final int pid = Binder.getCallingPid();
-        final int uid = Binder.getCallingUid();
-        final long token = Binder.clearCallingIdentity();
-
-        try {
-            // TODO (b/305919655) - Handle revoking of MEDIA_ROUTING_CONTROL at runtime.
-            enforcePrivilegedRoutingPermissions(uid, pid, /* callerPackageName */ null);
-            PackageManager pm = mContext.getPackageManager();
-            pm.getPackageInfo(clientPackageName, PackageManager.PackageInfoFlags.of(0));
-            return true;
-        } catch (PackageManager.NameNotFoundException ex) {
-            return false;
-        } finally {
-            Binder.restoreCallingIdentity(token);
-        }
-    }
-
     @NonNull
     public List<MediaRoute2Info> getSystemRoutes() {
         final int uid = Binder.getCallingUid();
@@ -491,13 +471,65 @@
 
         final int callerUid = Binder.getCallingUid();
         final int callerPid = Binder.getCallingPid();
-        final int callerUserId = UserHandle.getUserHandleForUid(callerUid).getIdentifier();
+        final UserHandle callerUser = Binder.getCallingUserHandle();
+
+        // TODO (b/305919655) - Handle revoking of MEDIA_ROUTING_CONTROL at runtime.
+        enforcePrivilegedRoutingPermissions(callerUid, callerPid, callerPackageName);
 
         final long token = Binder.clearCallingIdentity();
         try {
             synchronized (mLock) {
                 registerManagerLocked(
-                        manager, callerUid, callerPid, callerPackageName, callerUserId);
+                        manager,
+                        callerUid,
+                        callerPid,
+                        callerPackageName,
+                        /* targetPackageName */ null,
+                        callerUser);
+            }
+        } finally {
+            Binder.restoreCallingIdentity(token);
+        }
+    }
+
+    @RequiresPermission(
+            anyOf = {
+                Manifest.permission.MEDIA_CONTENT_CONTROL,
+                Manifest.permission.MEDIA_ROUTING_CONTROL
+            })
+    public void registerProxyRouter(
+            @NonNull IMediaRouter2Manager manager,
+            @NonNull String callerPackageName,
+            @NonNull String targetPackageName,
+            @NonNull UserHandle targetUser) {
+        Objects.requireNonNull(manager, "manager must not be null");
+        Objects.requireNonNull(targetUser, "targetUser must not be null");
+
+        if (TextUtils.isEmpty(targetPackageName)) {
+            throw new IllegalArgumentException("targetPackageName must not be empty");
+        }
+
+        int callerUid = Binder.getCallingUid();
+        int callerPid = Binder.getCallingPid();
+        final long token = Binder.clearCallingIdentity();
+
+        try {
+            // TODO (b/305919655) - Handle revoking of MEDIA_ROUTING_CONTROL at runtime.
+            enforcePrivilegedRoutingPermissions(callerUid, callerPid, callerPackageName);
+            enforceCrossUserPermissions(callerUid, callerPid, targetUser);
+            if (!verifyPackageExistsForUser(targetPackageName, targetUser)) {
+                throw new IllegalArgumentException(
+                        "targetPackageName does not exist: " + targetPackageName);
+            }
+
+            synchronized (mLock) {
+                registerManagerLocked(
+                        manager,
+                        callerUid,
+                        callerPid,
+                        callerPackageName,
+                        targetPackageName,
+                        targetUser);
             }
         } finally {
             Binder.restoreCallingIdentity(token);
@@ -761,6 +793,37 @@
         }
     }
 
+    @RequiresPermission(value = Manifest.permission.INTERACT_ACROSS_USERS)
+    private boolean verifyPackageExistsForUser(
+            @NonNull String clientPackageName, @NonNull UserHandle user) {
+        try {
+            PackageManager pm = mContext.getPackageManager();
+            pm.getPackageInfoAsUser(
+                    clientPackageName, PackageManager.PackageInfoFlags.of(0), user.getIdentifier());
+            return true;
+        } catch (PackageManager.NameNotFoundException ex) {
+            return false;
+        }
+    }
+
+    /**
+     * Enforces the caller has {@link Manifest.permission#INTERACT_ACROSS_USERS_FULL} if the
+     * caller's user is different from the target user.
+     */
+    private void enforceCrossUserPermissions(
+            int callerUid, int callerPid, @NonNull UserHandle targetUser) {
+        int callerUserId = UserHandle.getUserId(callerUid);
+
+        if (targetUser.getIdentifier() != callerUserId) {
+            mContext.enforcePermission(
+                    Manifest.permission.INTERACT_ACROSS_USERS_FULL,
+                    callerPid,
+                    callerUid,
+                    "Must hold INTERACT_ACROSS_USERS_FULL to control an app in a different"
+                            + " userId.");
+        }
+    }
+
     // End of methods that implements operations for both MediaRouter2 and MediaRouter2Manager.
 
     public void dump(@NonNull PrintWriter pw, @NonNull String prefix) {
@@ -1203,7 +1266,8 @@
             int callerUid,
             int callerPid,
             @NonNull String callerPackageName,
-            int callerUserId) {
+            @Nullable String targetPackageName,
+            @NonNull UserHandle targetUser) {
         final IBinder binder = manager.asBinder();
         ManagerRecord managerRecord = mAllManagerRecords.get(binder);
 
@@ -1217,15 +1281,18 @@
                 TAG,
                 TextUtils.formatSimple(
                         "registerManager | callerUid: %d, callerPid: %d, callerPackage: %s,"
-                            + " callerUserId: %d",
-                        callerUid, callerPid, callerPackageName, callerUserId));
+                                + "targetPackageName: %s, targetUserId: %d",
+                        callerUid, callerPid, callerPackageName, targetPackageName, targetUser));
 
-        // TODO (b/305919655) - Handle revoking of MEDIA_ROUTING_CONTROL at runtime.
-        enforcePrivilegedRoutingPermissions(callerUid, callerPid, callerPackageName);
-
-        UserRecord userRecord = getOrCreateUserRecordLocked(callerUserId);
-        managerRecord = new ManagerRecord(
-                userRecord, manager, callerUid, callerPid, callerPackageName);
+        UserRecord userRecord = getOrCreateUserRecordLocked(targetUser.getIdentifier());
+        managerRecord =
+                new ManagerRecord(
+                        userRecord,
+                        manager,
+                        callerUid,
+                        callerPid,
+                        callerPackageName,
+                        targetPackageName);
         try {
             binder.linkToDeath(managerRecord, 0);
         } catch (RemoteException ex) {
@@ -1791,22 +1858,30 @@
     }
 
     final class ManagerRecord implements IBinder.DeathRecipient {
-        public final UserRecord mUserRecord;
-        public final IMediaRouter2Manager mManager;
+        @NonNull public final UserRecord mUserRecord;
+        @NonNull public final IMediaRouter2Manager mManager;
         public final int mOwnerUid;
         public final int mOwnerPid;
-        public final String mOwnerPackageName;
+        @NonNull public final String mOwnerPackageName;
         public final int mManagerId;
-        public SessionCreationRequest mLastSessionCreationRequest;
+        // TODO (b/281072508): Document behaviour around nullability for mTargetPackageName.
+        @Nullable public final String mTargetPackageName;
+        @Nullable public SessionCreationRequest mLastSessionCreationRequest;
         public boolean mIsScanning;
 
-        ManagerRecord(UserRecord userRecord, IMediaRouter2Manager manager,
-                int ownerUid, int ownerPid, String ownerPackageName) {
+        ManagerRecord(
+                @NonNull UserRecord userRecord,
+                @NonNull IMediaRouter2Manager manager,
+                int ownerUid,
+                int ownerPid,
+                @NonNull String ownerPackageName,
+                @Nullable String targetPackageName) {
             mUserRecord = userRecord;
             mManager = manager;
             mOwnerUid = ownerUid;
             mOwnerPid = ownerPid;
             mOwnerPackageName = ownerPackageName;
+            mTargetPackageName = targetPackageName;
             mManagerId = mNextRouterOrManagerId.getAndIncrement();
         }
 
diff --git a/services/core/java/com/android/server/media/MediaRouterService.java b/services/core/java/com/android/server/media/MediaRouterService.java
index 6df4a95..e562b3f 100644
--- a/services/core/java/com/android/server/media/MediaRouterService.java
+++ b/services/core/java/com/android/server/media/MediaRouterService.java
@@ -409,13 +409,6 @@
     }
 
     // Binder call
-    @RequiresPermission(Manifest.permission.MEDIA_CONTENT_CONTROL)
-    @Override
-    public boolean verifyPackageExists(String clientPackageName) {
-        return mService2.verifyPackageExists(clientPackageName);
-    }
-
-    // Binder call
     @Override
     public List<MediaRoute2Info> getSystemRoutes() {
         return mService2.getSystemRoutes();
@@ -547,6 +540,19 @@
         mService2.registerManager(manager, callerPackageName);
     }
 
+    @Override
+    public void registerProxyRouter(
+            @NonNull IMediaRouter2Manager manager,
+            @NonNull String callerPackageName,
+            @NonNull String targetPackageName,
+            @NonNull UserHandle targetUser) {
+        final int uid = Binder.getCallingUid();
+        if (!validatePackageName(uid, callerPackageName)) {
+            throw new SecurityException("callerPackageName must match the calling uid");
+        }
+        mService2.registerProxyRouter(manager, callerPackageName, targetPackageName, targetUser);
+    }
+
     // Binder call
     @Override
     public void unregisterManager(IMediaRouter2Manager manager) {
diff --git a/services/core/java/com/android/server/media/SystemMediaRoute2Provider.java b/services/core/java/com/android/server/media/SystemMediaRoute2Provider.java
index c8dba80..9d151c2 100644
--- a/services/core/java/com/android/server/media/SystemMediaRoute2Provider.java
+++ b/services/core/java/com/android/server/media/SystemMediaRoute2Provider.java
@@ -16,15 +16,12 @@
 
 package com.android.server.media;
 
-import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
-import android.media.AudioAttributes;
-import android.media.AudioDeviceAttributes;
 import android.media.AudioManager;
 import android.media.MediaRoute2Info;
 import android.media.MediaRoute2ProviderInfo;
@@ -51,7 +48,8 @@
  */
 // TODO: check thread safety. We may need to use lock to protect variables.
 class SystemMediaRoute2Provider extends MediaRoute2Provider {
-    private static final String TAG = "MR2SystemProvider";
+    // Package-visible to use this tag for all system routing logic (done across multiple classes).
+    /* package */ static final String TAG = "MR2SystemProvider";
     private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
 
     private static final ComponentName COMPONENT_NAME = new ComponentName(
@@ -77,26 +75,6 @@
     private final AudioManagerBroadcastReceiver mAudioReceiver =
             new AudioManagerBroadcastReceiver();
 
-    private final AudioManager.OnDevicesForAttributesChangedListener
-            mOnDevicesForAttributesChangedListener =
-            new AudioManager.OnDevicesForAttributesChangedListener() {
-                @Override
-                public void onDevicesForAttributesChanged(@NonNull AudioAttributes attributes,
-                        @NonNull List<AudioDeviceAttributes> devices) {
-                    if (attributes.getUsage() != AudioAttributes.USAGE_MEDIA) {
-                        return;
-                    }
-
-                    mHandler.post(() -> {
-                        updateSelectedAudioDevice(devices);
-                        notifyProviderState();
-                        if (updateSessionInfosIfNeeded()) {
-                            notifySessionInfoUpdated();
-                        }
-                    });
-                }
-            };
-
     private final Object mRequestLock = new Object();
     @GuardedBy("mRequestLock")
     private volatile SessionCreationRequest mPendingSessionCreationRequest;
@@ -106,7 +84,8 @@
         mIsSystemRouteProvider = true;
         mContext = context;
         mUser = user;
-        mHandler = new Handler(Looper.getMainLooper());
+        Looper looper = Looper.getMainLooper();
+        mHandler = new Handler(looper);
 
         mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
 
@@ -123,25 +102,15 @@
         mDeviceRouteController =
                 DeviceRouteController.createInstance(
                         context,
-                        () -> {
-                            mHandler.post(
-                                    () -> {
-                                        publishProviderState();
-                                        if (updateSessionInfosIfNeeded()) {
-                                            notifySessionInfoUpdated();
-                                        }
-                                    });
-                        });
-
-        mAudioManager.addOnDevicesForAttributesChangedListener(
-                AudioAttributesUtils.ATTRIBUTES_MEDIA, mContext.getMainExecutor(),
-                mOnDevicesForAttributesChangedListener);
-
-        // These methods below should be called after all fields are initialized, as they
-        // access the fields inside.
-        List<AudioDeviceAttributes> devices =
-                mAudioManager.getDevicesForAttributes(AudioAttributesUtils.ATTRIBUTES_MEDIA);
-        updateSelectedAudioDevice(devices);
+                        looper,
+                        () ->
+                                mHandler.post(
+                                        () -> {
+                                            publishProviderState();
+                                            if (updateSessionInfosIfNeeded()) {
+                                                notifySessionInfoUpdated();
+                                            }
+                                        }));
         updateProviderState();
         updateSessionInfosIfNeeded();
     }
@@ -151,20 +120,22 @@
         intentFilter.addAction(AudioManager.STREAM_DEVICES_CHANGED_ACTION);
         mContext.registerReceiverAsUser(mAudioReceiver, mUser,
                 intentFilter, null, null);
-
-        mHandler.post(() -> {
-            mBluetoothRouteController.start(mUser);
-            notifyProviderState();
-        });
+        mHandler.post(
+                () -> {
+                    mDeviceRouteController.start(mUser);
+                    mBluetoothRouteController.start(mUser);
+                });
         updateVolume();
     }
 
     public void stop() {
         mContext.unregisterReceiver(mAudioReceiver);
-        mHandler.post(() -> {
-            mBluetoothRouteController.stop();
-            notifyProviderState();
-        });
+        mHandler.post(
+                () -> {
+                    mBluetoothRouteController.stop();
+                    mDeviceRouteController.stop();
+                    notifyProviderState();
+                });
     }
 
     @Override
@@ -225,13 +196,26 @@
     public void transferToRoute(long requestId, String sessionId, String routeId) {
         if (TextUtils.equals(routeId, MediaRoute2Info.ROUTE_ID_DEFAULT)) {
             // The currently selected route is the default route.
+            Log.w(TAG, "Ignoring transfer to " + MediaRoute2Info.ROUTE_ID_DEFAULT);
             return;
         }
-
         MediaRoute2Info selectedDeviceRoute = mDeviceRouteController.getSelectedRoute();
-        if (TextUtils.equals(routeId, selectedDeviceRoute.getId())) {
+        boolean isAvailableDeviceRoute =
+                mDeviceRouteController.getAvailableRoutes().stream()
+                        .anyMatch(it -> it.getId().equals(routeId));
+        boolean isSelectedDeviceRoute = TextUtils.equals(routeId, selectedDeviceRoute.getId());
+
+        if (isSelectedDeviceRoute || isAvailableDeviceRoute) {
+            // The requested route is managed by the device route controller. Note that the selected
+            // device route doesn't necessarily match mSelectedRouteId (which is the selected route
+            // of the routing session). If the selected device route is transferred to, we need to
+            // make the bluetooth routes inactive so that the device route becomes the selected
+            // route of the routing session.
+            mDeviceRouteController.transferTo(routeId);
             mBluetoothRouteController.transferTo(null);
         } else {
+            // The requested route is managed by the bluetooth route controller.
+            mDeviceRouteController.transferTo(null);
             mBluetoothRouteController.transferTo(routeId);
         }
     }
@@ -280,41 +264,38 @@
 
             MediaRoute2Info selectedDeviceRoute = mDeviceRouteController.getSelectedRoute();
 
-            RoutingSessionInfo.Builder builder = new RoutingSessionInfo.Builder(
-                    SYSTEM_SESSION_ID, packageName).setSystemSession(true);
+            RoutingSessionInfo.Builder builder =
+                    new RoutingSessionInfo.Builder(SYSTEM_SESSION_ID, packageName)
+                            .setSystemSession(true);
             builder.addSelectedRoute(selectedDeviceRoute.getId());
             for (MediaRoute2Info route : mBluetoothRouteController.getAllBluetoothRoutes()) {
                 builder.addTransferableRoute(route.getId());
             }
+
+            if (Flags.enableAudioPoliciesDeviceAndBluetoothController()) {
+                for (MediaRoute2Info route : mDeviceRouteController.getAvailableRoutes()) {
+                    if (!TextUtils.equals(selectedDeviceRoute.getId(), route.getId())) {
+                        builder.addTransferableRoute(route.getId());
+                    }
+                }
+            }
             return builder.setProviderId(mUniqueId).build();
         }
     }
 
-    private void updateSelectedAudioDevice(@NonNull List<AudioDeviceAttributes> devices) {
-        if (devices.isEmpty()) {
-            Slog.w(TAG, "The list of preferred devices was empty.");
-            return;
-        }
-
-        AudioDeviceAttributes audioDeviceAttributes = devices.get(0);
-
-        if (AudioAttributesUtils.isDeviceOutputAttributes(audioDeviceAttributes)) {
-            mDeviceRouteController.selectRoute(
-                    AudioAttributesUtils.mapToMediaRouteType(audioDeviceAttributes));
-            mBluetoothRouteController.selectRoute(null);
-        } else if (AudioAttributesUtils.isBluetoothOutputAttributes(audioDeviceAttributes)) {
-            mDeviceRouteController.selectRoute(null);
-            mBluetoothRouteController.selectRoute(audioDeviceAttributes.getAddress());
-        } else {
-            Slog.w(TAG, "Unknown audio attributes: " + audioDeviceAttributes);
-        }
-    }
-
     private void updateProviderState() {
         MediaRoute2ProviderInfo.Builder builder = new MediaRoute2ProviderInfo.Builder();
 
         // We must have a device route in the provider info.
-        builder.addRoute(mDeviceRouteController.getSelectedRoute());
+        if (Flags.enableAudioPoliciesDeviceAndBluetoothController()) {
+            List<MediaRoute2Info> deviceRoutes = mDeviceRouteController.getAvailableRoutes();
+            for (MediaRoute2Info route : deviceRoutes) {
+                builder.addRoute(route);
+            }
+            setProviderState(builder.build());
+        } else {
+            builder.addRoute(mDeviceRouteController.getSelectedRoute());
+        }
 
         for (MediaRoute2Info route : mBluetoothRouteController.getAllBluetoothRoutes()) {
             builder.addRoute(route);
@@ -352,7 +333,14 @@
                             .setProviderId(mUniqueId)
                             .build();
             builder.addSelectedRoute(mSelectedRouteId);
-
+            if (Flags.enableAudioPoliciesDeviceAndBluetoothController()) {
+                for (MediaRoute2Info route : mDeviceRouteController.getAvailableRoutes()) {
+                    String routeId = route.getId();
+                    if (!mSelectedRouteId.equals(routeId)) {
+                        builder.addTransferableRoute(routeId);
+                    }
+                }
+            }
             for (MediaRoute2Info route : mBluetoothRouteController.getTransferableRoutes()) {
                 builder.addTransferableRoute(route.getId());
             }
diff --git a/services/core/java/com/android/server/notification/DefaultDeviceEffectsApplier.java b/services/core/java/com/android/server/notification/DefaultDeviceEffectsApplier.java
new file mode 100644
index 0000000..9fdeda4
--- /dev/null
+++ b/services/core/java/com/android/server/notification/DefaultDeviceEffectsApplier.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright (C) 2023 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.notification;
+
+import android.app.UiModeManager;
+import android.app.WallpaperManager;
+import android.content.Context;
+import android.hardware.display.ColorDisplayManager;
+import android.os.Binder;
+import android.os.PowerManager;
+import android.service.notification.DeviceEffectsApplier;
+import android.service.notification.ZenDeviceEffects;
+
+/** Default implementation for {@link DeviceEffectsApplier}. */
+class DefaultDeviceEffectsApplier implements DeviceEffectsApplier {
+
+    private static final String SUPPRESS_AMBIENT_DISPLAY_TOKEN =
+            "DefaultDeviceEffectsApplier:SuppressAmbientDisplay";
+    private static final int SATURATION_LEVEL_GRAYSCALE = 0;
+    private static final int SATURATION_LEVEL_FULL_COLOR = 100;
+    private static final float WALLPAPER_DIM_AMOUNT_DIMMED = 0.6f;
+    private static final float WALLPAPER_DIM_AMOUNT_NORMAL = 0f;
+
+    private final ColorDisplayManager mColorDisplayManager;
+    private final PowerManager mPowerManager;
+    private final UiModeManager mUiModeManager;
+    private final WallpaperManager mWallpaperManager;
+
+    DefaultDeviceEffectsApplier(Context context) {
+        mColorDisplayManager = context.getSystemService(ColorDisplayManager.class);
+        mPowerManager = context.getSystemService(PowerManager.class);
+        mUiModeManager = context.getSystemService(UiModeManager.class);
+        mWallpaperManager = context.getSystemService(WallpaperManager.class);
+    }
+
+    @Override
+    public void apply(ZenDeviceEffects effects) {
+        Binder.withCleanCallingIdentity(() -> {
+            mPowerManager.suppressAmbientDisplay(SUPPRESS_AMBIENT_DISPLAY_TOKEN,
+                    effects.shouldSuppressAmbientDisplay());
+
+            if (mColorDisplayManager != null) {
+                mColorDisplayManager.setSaturationLevel(
+                        effects.shouldDisplayGrayscale() ? SATURATION_LEVEL_GRAYSCALE
+                                : SATURATION_LEVEL_FULL_COLOR);
+            }
+
+            if (mWallpaperManager != null) {
+                mWallpaperManager.setWallpaperDimAmount(
+                        effects.shouldDimWallpaper() ? WALLPAPER_DIM_AMOUNT_DIMMED
+                                : WALLPAPER_DIM_AMOUNT_NORMAL);
+            }
+
+            // TODO: b/308673343 - Apply dark theme (via UiModeManager) when screen is off.
+        });
+    }
+}
diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java
index 3c6887c1..02845fb 100755
--- a/services/core/java/com/android/server/notification/NotificationManagerService.java
+++ b/services/core/java/com/android/server/notification/NotificationManagerService.java
@@ -2941,6 +2941,12 @@
             registerDeviceConfigChange();
             migrateDefaultNAS();
             maybeShowInitialReviewPermissionsNotification();
+
+            if (android.app.Flags.modesApi()) {
+                // Cannot be done earlier, as some services aren't ready until this point.
+                mZenModeHelper.setDeviceEffectsApplier(
+                        new DefaultDeviceEffectsApplier(getContext()));
+            }
         } else if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
             mSnoozeHelper.scheduleRepostsForPersistedNotifications(System.currentTimeMillis());
         } else if (phase == SystemService.PHASE_DEVICE_SPECIFIC_SERVICES_READY) {
diff --git a/services/core/java/com/android/server/notification/ZenModeHelper.java b/services/core/java/com/android/server/notification/ZenModeHelper.java
index 5c37eea..218519f 100644
--- a/services/core/java/com/android/server/notification/ZenModeHelper.java
+++ b/services/core/java/com/android/server/notification/ZenModeHelper.java
@@ -27,8 +27,8 @@
 
 import static com.android.internal.util.FrameworkStatsLog.DND_MODE_RULE;
 
-import android.annotation.IntDef;
 import android.annotation.DrawableRes;
+import android.annotation.IntDef;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.annotation.SuppressLint;
@@ -76,6 +76,7 @@
 import android.provider.Settings.Global;
 import android.service.notification.Condition;
 import android.service.notification.ConditionProviderService;
+import android.service.notification.DeviceEffectsApplier;
 import android.service.notification.ZenDeviceEffects;
 import android.service.notification.ZenModeConfig;
 import android.service.notification.ZenModeConfig.ZenRule;
@@ -175,6 +176,8 @@
 
     @VisibleForTesting protected int mZenMode;
     @VisibleForTesting protected NotificationManager.Policy mConsolidatedPolicy;
+    @GuardedBy("mConfigLock")
+    private ZenDeviceEffects mConsolidatedDeviceEffects = new ZenDeviceEffects.Builder().build();
     private int mUser = UserHandle.USER_SYSTEM;
 
     private final Object mConfigLock = new Object();
@@ -182,6 +185,8 @@
     @VisibleForTesting protected ZenModeConfig mConfig;
     @VisibleForTesting protected AudioManagerInternal mAudioManager;
     protected PackageManager mPm;
+    @GuardedBy("mConfigLock")
+    private DeviceEffectsApplier mDeviceEffectsApplier;
     private long mSuppressedEffects;
 
     public static final long SUPPRESSED_EFFECT_NOTIFICATIONS = 1;
@@ -189,7 +194,7 @@
     public static final long SUPPRESSED_EFFECT_ALL = SUPPRESSED_EFFECT_CALLS
             | SUPPRESSED_EFFECT_NOTIFICATIONS;
 
-    @VisibleForTesting protected boolean mIsBootComplete;
+    @VisibleForTesting protected boolean mIsSystemServicesReady;
 
     private String[] mPriorityOnlyDndExemptPackages;
 
@@ -285,10 +290,33 @@
         mPm = mContext.getPackageManager();
         mHandler.postMetricsTimer();
         cleanUpZenRules();
-        mIsBootComplete = true;
+        mIsSystemServicesReady = true;
         showZenUpgradeNotification(mZenMode);
     }
 
+    /**
+     * Set the {@link DeviceEffectsApplier} used to apply the consolidated effects.
+     *
+     * <p>If effects were calculated previously (for example, when we loaded a {@link ZenModeConfig}
+     * that includes activated rules), they will be applied immediately.
+     */
+    void setDeviceEffectsApplier(@NonNull DeviceEffectsApplier deviceEffectsApplier) {
+        if (!Flags.modesApi()) {
+            return;
+        }
+        ZenDeviceEffects consolidatedDeviceEffects;
+        synchronized (mConfigLock) {
+            if (mDeviceEffectsApplier != null) {
+                throw new IllegalStateException("Already set up a DeviceEffectsApplier!");
+            }
+            mDeviceEffectsApplier = deviceEffectsApplier;
+            consolidatedDeviceEffects = mConsolidatedDeviceEffects;
+        }
+        if (consolidatedDeviceEffects.hasEffects()) {
+            applyConsolidatedDeviceEffects();
+        }
+    }
+
     public void onUserSwitched(int user) {
         loadConfigForUser(user, "onUserSwitched");
     }
@@ -1349,7 +1377,7 @@
 
             mConfig = config;
             dispatchOnConfigChanged();
-            updateConsolidatedPolicy(reason);
+            updateAndApplyConsolidatedPolicyAndDeviceEffects(reason);
         }
         final String val = Integer.toString(config.hashCode());
         Global.putString(mContext.getContentResolver(), Global.ZEN_MODE_CONFIG_ETAG, val);
@@ -1398,7 +1426,7 @@
         ZenLog.traceSetZenMode(zen, reason);
         mZenMode = zen;
         setZenModeSetting(mZenMode);
-        updateConsolidatedPolicy(reason);
+        updateAndApplyConsolidatedPolicyAndDeviceEffects(reason);
         boolean shouldApplyToRinger = setRingerMode && (zen != zenBefore || (
                 zen == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS
                         && policyHashBefore != mConsolidatedPolicy.hashCode()));
@@ -1459,25 +1487,56 @@
         }
     }
 
-    private void updateConsolidatedPolicy(String reason) {
+    private void updateAndApplyConsolidatedPolicyAndDeviceEffects(String reason) {
         synchronized (mConfigLock) {
             if (mConfig == null) return;
             ZenPolicy policy = new ZenPolicy();
+            ZenDeviceEffects.Builder deviceEffectsBuilder = new ZenDeviceEffects.Builder();
             if (mConfig.manualRule != null) {
                 applyCustomPolicy(policy, mConfig.manualRule);
+                if (Flags.modesApi()) {
+                    deviceEffectsBuilder.add(mConfig.manualRule.zenDeviceEffects);
+                }
             }
 
             for (ZenRule automaticRule : mConfig.automaticRules.values()) {
                 if (automaticRule.isAutomaticActive()) {
                     applyCustomPolicy(policy, automaticRule);
+                    if (Flags.modesApi()) {
+                        deviceEffectsBuilder.add(automaticRule.zenDeviceEffects);
+                    }
                 }
             }
+
             Policy newPolicy = mConfig.toNotificationPolicy(policy);
             if (!Objects.equals(mConsolidatedPolicy, newPolicy)) {
                 mConsolidatedPolicy = newPolicy;
                 dispatchOnConsolidatedPolicyChanged();
                 ZenLog.traceSetConsolidatedZenPolicy(mConsolidatedPolicy, reason);
             }
+
+            if (Flags.modesApi()) {
+                ZenDeviceEffects deviceEffects = deviceEffectsBuilder.build();
+                if (!deviceEffects.equals(mConsolidatedDeviceEffects)) {
+                    mConsolidatedDeviceEffects = deviceEffects;
+                    mHandler.postApplyDeviceEffects();
+                }
+            }
+        }
+    }
+
+    private void applyConsolidatedDeviceEffects() {
+        if (!Flags.modesApi()) {
+            return;
+        }
+        DeviceEffectsApplier applier;
+        ZenDeviceEffects effects;
+        synchronized (mConfigLock) {
+            applier = mDeviceEffectsApplier;
+            effects = mConsolidatedDeviceEffects;
+        }
+        if (applier != null) {
+            applier.apply(effects);
         }
     }
 
@@ -1893,7 +1952,7 @@
     private void showZenUpgradeNotification(int zen) {
         final boolean isWatch = mContext.getPackageManager().hasSystemFeature(
             PackageManager.FEATURE_WATCH);
-        final boolean showNotification = mIsBootComplete
+        final boolean showNotification = mIsSystemServicesReady
                 && zen != Global.ZEN_MODE_OFF
                 && !isWatch
                 && Settings.Secure.getInt(mContext.getContentResolver(),
@@ -2067,6 +2126,7 @@
         private static final int MSG_DISPATCH = 1;
         private static final int MSG_METRICS = 2;
         private static final int MSG_RINGER_AUDIO = 5;
+        private static final int MSG_APPLY_EFFECTS = 6;
 
         private static final long METRICS_PERIOD_MS = 6 * 60 * 60 * 1000;
 
@@ -2089,6 +2149,11 @@
             sendMessage(obtainMessage(MSG_RINGER_AUDIO, shouldApplyToRinger));
         }
 
+        private void postApplyDeviceEffects() {
+            removeMessages(MSG_APPLY_EFFECTS);
+            sendEmptyMessage(MSG_APPLY_EFFECTS);
+        }
+
         @Override
         public void handleMessage(Message msg) {
             switch (msg.what) {
@@ -2101,6 +2166,10 @@
                 case MSG_RINGER_AUDIO:
                     boolean shouldApplyToRinger = (boolean) msg.obj;
                     updateRingerAndAudio(shouldApplyToRinger);
+                    break;
+                case MSG_APPLY_EFFECTS:
+                    applyConsolidatedDeviceEffects();
+                    break;
             }
         }
     }
diff --git a/services/core/java/com/android/server/om/OverlayManagerShellCommand.java b/services/core/java/com/android/server/om/OverlayManagerShellCommand.java
index f77d7898..d9c8ec6 100644
--- a/services/core/java/com/android/server/om/OverlayManagerShellCommand.java
+++ b/services/core/java/com/android/server/om/OverlayManagerShellCommand.java
@@ -317,11 +317,11 @@
             return 1;
         }
         final String overlayPackageName = "com.android.shell";
-        FabricatedOverlay.Builder overlayBuilder = new FabricatedOverlay.Builder(
-                overlayPackageName, name, targetPackage)
-                .setTargetOverlayable(targetOverlayable);
+        FabricatedOverlay overlay = new FabricatedOverlay(name, targetPackage);
+        overlay.setTargetOverlayable(targetOverlayable);
+        overlay.setOwningPackage(overlayPackageName);
         if (filename != null) {
-            int result = addOverlayValuesFromXml(overlayBuilder, targetPackage, filename);
+            int result = addOverlayValuesFromXml(overlay, targetPackage, filename);
             if (result != 0) {
                 return result;
             }
@@ -329,18 +329,18 @@
             final String resourceName = getNextArgRequired();
             final String typeStr = getNextArgRequired();
             final String strData = String.join(" ", peekRemainingArgs());
-            if (addOverlayValue(overlayBuilder, resourceName, typeStr, strData, config) != 0) {
+            if (addOverlayValue(overlay, resourceName, typeStr, strData, config) != 0) {
                 return 1;
             }
         }
 
         mInterface.commit(new OverlayManagerTransaction.Builder()
-                .registerFabricatedOverlay(overlayBuilder.build()).build());
+                .registerFabricatedOverlay(overlay).build());
         return 0;
     }
 
     private int addOverlayValuesFromXml(
-            FabricatedOverlay.Builder overlayBuilder, String targetPackage, String filename) {
+            FabricatedOverlay overlay, String targetPackage, String filename) {
         final PrintWriter err = getErrPrintWriter();
         File file = new File(filename);
         if (!file.exists()) {
@@ -388,7 +388,7 @@
                             return 1;
                         }
                         String config = parser.getAttributeValue(null, "config");
-                        if (addOverlayValue(overlayBuilder, targetPackage + ':' + target,
+                        if (addOverlayValue(overlay, targetPackage + ':' + target,
                                   overlayType, value, config) != 0) {
                             return 1;
                         }
@@ -405,8 +405,8 @@
         return 0;
     }
 
-    private int addOverlayValue(FabricatedOverlay.Builder overlayBuilder,
-            String resourceName, String typeString, String valueString, String configuration) {
+    private int addOverlayValue(FabricatedOverlay overlay, String resourceName, String typeString,
+                                String valueString, String configuration) {
         final int type;
         typeString = typeString.toLowerCase(Locale.getDefault());
         if (TYPE_MAP.containsKey(typeString)) {
@@ -419,10 +419,14 @@
             }
         }
         if (type == TypedValue.TYPE_STRING) {
-            overlayBuilder.setResourceValue(resourceName, type, valueString, configuration);
+            overlay.setResourceValue(resourceName, type, valueString, configuration);
         } else if (type < 0) {
             ParcelFileDescriptor pfd =  openFileForSystem(valueString, "r");
-            overlayBuilder.setResourceValue(resourceName, pfd, configuration);
+            if (valueString.endsWith(".9.png")) {
+                overlay.setNinePatchResourceValue(resourceName, pfd, configuration);
+            } else {
+                overlay.setResourceValue(resourceName, pfd, configuration);
+            }
         } else {
             final int intData;
             if (valueString.startsWith("0x")) {
@@ -430,7 +434,7 @@
             } else {
                 intData = Integer.parseUnsignedInt(valueString);
             }
-            overlayBuilder.setResourceValue(resourceName, type, intData, configuration);
+            overlay.setResourceValue(resourceName, type, intData, configuration);
         }
         return 0;
     }
diff --git a/services/core/java/com/android/server/pdb/TEST_MAPPING b/services/core/java/com/android/server/pdb/TEST_MAPPING
new file mode 100644
index 0000000..1aa8601
--- /dev/null
+++ b/services/core/java/com/android/server/pdb/TEST_MAPPING
@@ -0,0 +1,12 @@
+{
+    "postsubmit": [
+        {
+            "name": "FrameworksServicesTests",
+            "options": [
+                {
+                    "include-filter": "com.android.server.pdb.PersistentDataBlockServiceTest"
+                }
+            ]
+        }
+    ]
+}
diff --git a/services/core/java/com/android/server/pm/ComputerEngine.java b/services/core/java/com/android/server/pm/ComputerEngine.java
index b2d4a2c..7c425b82 100644
--- a/services/core/java/com/android/server/pm/ComputerEngine.java
+++ b/services/core/java/com/android/server/pm/ComputerEngine.java
@@ -1532,7 +1532,9 @@
                     ai, flags, state, userId);
             pi.signingInfo = ps.getSigningInfo();
             pi.signatures = getDeprecatedSignatures(pi.signingInfo.getSigningDetails(), flags);
-            pi.setArchiveTimeMillis(state.getArchiveTimeMillis());
+            if (state.getArchiveState() != null) {
+                pi.setArchiveTimeMillis(state.getArchiveState().getArchiveTimeMillis());
+            }
 
             if (DEBUG_PACKAGE_INFO) {
                 Log.v(TAG, "ps.pkg is n/a for ["
diff --git a/services/core/java/com/android/server/pm/DeletePackageHelper.java b/services/core/java/com/android/server/pm/DeletePackageHelper.java
index 07e0ddf..80e6c83 100644
--- a/services/core/java/com/android/server/pm/DeletePackageHelper.java
+++ b/services/core/java/com/android/server/pm/DeletePackageHelper.java
@@ -21,7 +21,6 @@
 import static android.content.pm.Flags.sdkLibIndependence;
 import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
 import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
-import static android.content.pm.PackageManager.DELETE_ARCHIVE;
 import static android.content.pm.PackageManager.DELETE_KEEP_DATA;
 import static android.content.pm.PackageManager.DELETE_SUCCEEDED;
 import static android.content.pm.PackageManager.MATCH_KNOWN_PACKAGES;
@@ -613,10 +612,6 @@
                     firstInstallTime,
                     PackageManager.USER_MIN_ASPECT_RATIO_UNSET,
                     archiveState);
-
-            if ((flags & DELETE_ARCHIVE) != 0) {
-                ps.modifyUserState(nextUserId).setArchiveTimeMillis(System.currentTimeMillis());
-            }
         }
         mPm.mSettings.writeKernelMappingLPr(ps);
     }
diff --git a/services/core/java/com/android/server/pm/InstallPackageHelper.java b/services/core/java/com/android/server/pm/InstallPackageHelper.java
index 3e7c8c4..8270481 100644
--- a/services/core/java/com/android/server/pm/InstallPackageHelper.java
+++ b/services/core/java/com/android/server/pm/InstallPackageHelper.java
@@ -691,7 +691,6 @@
                     pkgSetting.setUninstallReason(PackageManager.UNINSTALL_REASON_UNKNOWN, userId);
                     pkgSetting.setFirstInstallTime(System.currentTimeMillis(), userId);
                     // Clear any existing archive state.
-                    pkgSetting.setArchiveTimeMillis(0, userId);
                     pkgSetting.setArchiveState(null, userId);
                     mPm.mSettings.writePackageRestrictionsLPr(userId);
                     mPm.mSettings.writeKernelMappingLPr(pkgSetting);
@@ -2272,7 +2271,6 @@
                     }
                     // Clear any existing archive state.
                     ps.setArchiveState(null, userId);
-                    ps.setArchiveTimeMillis(0, userId);
                 } else if (allUsers != null) {
                     // The caller explicitly specified INSTALL_ALL_USERS flag.
                     // Thus, updating the settings to install the app for all users.
@@ -2297,7 +2295,6 @@
                             }
                             // Clear any existing archive state.
                             ps.setArchiveState(null, currentUserId);
-                            ps.setArchiveTimeMillis(0, currentUserId);
                         } else {
                             ps.setInstalled(false, currentUserId);
                         }
diff --git a/services/core/java/com/android/server/pm/PackageSetting.java b/services/core/java/com/android/server/pm/PackageSetting.java
index 26dc576..174df44 100644
--- a/services/core/java/com/android/server/pm/PackageSetting.java
+++ b/services/core/java/com/android/server/pm/PackageSetting.java
@@ -775,11 +775,6 @@
         onChanged();
     }
 
-    void setArchiveTimeMillis(long value, int userId) {
-        modifyUserState(userId).setArchiveTimeMillis(value);
-        onChanged();
-    }
-
     boolean getInstalled(int userId) {
         return readUserState(userId).isInstalled();
     }
diff --git a/services/core/java/com/android/server/pm/RemovePackageHelper.java b/services/core/java/com/android/server/pm/RemovePackageHelper.java
index 80f69a4..7ee32fb 100644
--- a/services/core/java/com/android/server/pm/RemovePackageHelper.java
+++ b/services/core/java/com/android/server/pm/RemovePackageHelper.java
@@ -419,9 +419,6 @@
                     Slog.d(TAG, "    user " + userId + ": " + wasInstalled + " => " + false);
                 }
                 deletedPs.setInstalled(/* installed= */ false, userId);
-                if (isArchive) {
-                    deletedPs.modifyUserState(userId).setArchiveTimeMillis(currentTimeMillis);
-                }
             }
         }
         // make sure to preserve per-user installed state if this removal was just
diff --git a/services/core/java/com/android/server/pm/Settings.java b/services/core/java/com/android/server/pm/Settings.java
index 107dc76..2cbf714 100644
--- a/services/core/java/com/android/server/pm/Settings.java
+++ b/services/core/java/com/android/server/pm/Settings.java
@@ -372,7 +372,6 @@
     private static final String ATTR_ARCHIVE_INSTALLER_TITLE = "installer-title";
     private static final String ATTR_ARCHIVE_ICON_PATH = "icon-path";
     private static final String ATTR_ARCHIVE_MONOCHROME_ICON_PATH = "monochrome-icon-path";
-
     private static final String ATTR_ARCHIVE_TIME = "archive-time";
 
     private final Handler mHandler;
@@ -1933,8 +1932,6 @@
                                 ATTR_SPLASH_SCREEN_THEME);
                         final long firstInstallTime = parser.getAttributeLongHex(null,
                                 ATTR_FIRST_INSTALL_TIME, 0);
-                        final long archiveTime = parser.getAttributeLongHex(null,
-                                ATTR_ARCHIVE_TIME, 0);
                         final int minAspectRatio = parser.getAttributeInt(null,
                                 ATTR_MIN_ASPECT_RATIO,
                                 PackageManager.USER_MIN_ASPECT_RATIO_UNSET);
@@ -2022,7 +2019,6 @@
                                 firstInstallTime != 0 ? firstInstallTime
                                         : origFirstInstallTimes.getOrDefault(name, 0L),
                                 minAspectRatio, archiveState);
-                        ps.setArchiveTimeMillis(archiveTime, userId);
                         mDomainVerificationManager.setLegacyUserState(name, userId, verifState);
                     } else if (tagName.equals("preferred-activities")) {
                         readPreferredActivitiesLPw(parser, userId);
@@ -2054,6 +2050,7 @@
             throws XmlPullParserException, IOException {
         String installerTitle = parser.getAttributeValue(null,
                 ATTR_ARCHIVE_INSTALLER_TITLE);
+        final long archiveTimeMillis = parser.getAttributeLongHex(null, ATTR_ARCHIVE_TIME, 0);
         List<ArchiveState.ArchiveActivityInfo> activityInfos =
                 parseArchiveActivityInfos(parser);
 
@@ -2067,7 +2064,7 @@
             return null;
         }
 
-        return new ArchiveState(activityInfos, installerTitle);
+        return new ArchiveState(activityInfos, installerTitle, archiveTimeMillis);
     }
 
     private static List<ArchiveState.ArchiveActivityInfo> parseArchiveActivityInfos(
@@ -2385,8 +2382,6 @@
                         }
                         serializer.attributeLongHex(null, ATTR_FIRST_INSTALL_TIME,
                                 ustate.getFirstInstallTimeMillis());
-                        serializer.attributeLongHex(null, ATTR_ARCHIVE_TIME,
-                                ustate.getArchiveTimeMillis());
                         if (ustate.getUninstallReason()
                                 != PackageManager.UNINSTALL_REASON_UNKNOWN) {
                             serializer.attributeInt(null, ATTR_UNINSTALL_REASON,
@@ -2488,6 +2483,7 @@
 
         serializer.startTag(null, TAG_ARCHIVE_STATE);
         serializer.attribute(null, ATTR_ARCHIVE_INSTALLER_TITLE, archiveState.getInstallerTitle());
+        serializer.attributeLongHex(null, ATTR_ARCHIVE_TIME, archiveState.getArchiveTimeMillis());
         for (ArchiveState.ArchiveActivityInfo activityInfo : archiveState.getActivityInfos()) {
             serializer.startTag(null, TAG_ARCHIVE_ACTIVITY_INFO);
             serializer.attribute(null, ATTR_ARCHIVE_ACTIVITY_TITLE, activityInfo.getTitle());
@@ -5293,9 +5289,11 @@
             date.setTime(pus.getFirstInstallTimeMillis());
             pw.println(sdf.format(date));
 
-            pw.print("      archiveTime=");
-            date.setTime(pus.getArchiveTimeMillis());
-            pw.println(sdf.format(date));
+            if (pus.getArchiveState() != null) {
+                pw.print("      archiveTime=");
+                date.setTime(pus.getArchiveState().getArchiveTimeMillis());
+                pw.println(sdf.format(date));
+            }
 
             pw.print("      uninstallReason=");
             pw.println(userState.getUninstallReason());
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 7910edc..d642018 100644
--- a/services/core/java/com/android/server/pm/parsing/PackageInfoUtils.java
+++ b/services/core/java/com/android/server/pm/parsing/PackageInfoUtils.java
@@ -152,7 +152,9 @@
         info.compileSdkVersionCodename = pkg.getCompileSdkVersionCodeName();
         info.firstInstallTime = firstInstallTime;
         info.lastUpdateTime = lastUpdateTime;
-        info.setArchiveTimeMillis(state.getArchiveTimeMillis());
+        if (state.getArchiveState() != null) {
+            info.setArchiveTimeMillis(state.getArchiveState().getArchiveTimeMillis());
+        }
         if ((flags & PackageManager.GET_GIDS) != 0) {
             info.gids = gids;
         }
@@ -346,7 +348,7 @@
     }
 
     /**
-     *  Retrieve the deprecated {@link PackageInfo.signatures} field of signing certificates
+     * Retrieve the deprecated {@link PackageInfo.signatures} field of signing certificates
      */
     public static Signature[] getDeprecatedSignatures(SigningDetails signingDetails, long flags) {
         if ((flags & PackageManager.GET_SIGNATURES) == 0) {
diff --git a/services/core/java/com/android/server/pm/pkg/ArchiveState.java b/services/core/java/com/android/server/pm/pkg/ArchiveState.java
index 1e40d44..009bb9f 100644
--- a/services/core/java/com/android/server/pm/pkg/ArchiveState.java
+++ b/services/core/java/com/android/server/pm/pkg/ArchiveState.java
@@ -16,9 +16,10 @@
 
 package com.android.server.pm.pkg;
 
-import android.content.ComponentName;
+import android.annotation.CurrentTimeMillisLong;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
+import android.content.ComponentName;
 
 import com.android.internal.util.AnnotationValidations;
 import com.android.internal.util.DataClass;
@@ -51,14 +52,45 @@
     @NonNull
     private final String mInstallerTitle;
 
-    /** Information about a main activity of an archived app. */
+    /**
+     * The time at which the app was archived for the user.  Units are as per
+     * {@link System#currentTimeMillis()}.
+     */
+    private final @CurrentTimeMillisLong long mArchiveTimeMillis;
+
+    /**
+     * Creates a new ArchiveState.
+     *
+     * @param activityInfos
+     *   Information about main activities.
+     *
+     *   <p> This list has at least one entry. In the vast majority of cases, this list has only one
+     *   entry.
+     * @param installerTitle
+     *   Corresponds to android:label of the installer responsible for the unarchival of the app.
+     *   Stored in the installer's locale .*
+     */
+    public ArchiveState(
+            @NonNull List<ArchiveActivityInfo> activityInfos,
+            @NonNull String installerTitle) {
+        this(activityInfos, installerTitle, System.currentTimeMillis());
+    }
+
+
+    /**
+     * Information about a main activity of an archived app.
+     */
     @DataClass(genEqualsHashCode = true, genToString = true)
     public static class ArchiveActivityInfo {
-        /** Corresponds to the activity's android:label in the app's locale. */
+        /**
+         * Corresponds to the activity's android:label in the app's locale.
+         */
         @NonNull
         private final String mTitle;
 
-        /** The component name of the original activity (pre-archival). */
+        /**
+         * The component name of the original activity (pre-archival).
+         */
         @NonNull
         private final ComponentName mOriginalComponentName;
 
@@ -69,7 +101,9 @@
         @Nullable
         private final Path mIconBitmap;
 
-        /** See {@link #mIconBitmap}. Only set if the app defined a monochrome icon. */
+        /**
+         * See {@link #mIconBitmap}. Only set if the app defined a monochrome icon.
+         */
         @Nullable
         private final Path mMonochromeIconBitmap;
 
@@ -93,6 +127,8 @@
          *
          * @param title
          *   Corresponds to the activity's android:label in the app's locale.
+         * @param originalComponentName
+         *   The component name of the original activity (pre-archival).
          * @param iconBitmap
          *   The path to the stored icon of the activity in the app's locale. Null if the app does
          *   not define any icon (default icon would be shown on the launcher).
@@ -106,9 +142,11 @@
                 @Nullable Path iconBitmap,
                 @Nullable Path monochromeIconBitmap) {
             this.mTitle = title;
+            AnnotationValidations.validate(
+                    NonNull.class, null, mTitle);
             this.mOriginalComponentName = originalComponentName;
-            AnnotationValidations.validate(NonNull.class, null, mTitle);
-            AnnotationValidations.validate(NonNull.class, null, mOriginalComponentName);
+            AnnotationValidations.validate(
+                    NonNull.class, null, mOriginalComponentName);
             this.mIconBitmap = iconBitmap;
             this.mMonochromeIconBitmap = monochromeIconBitmap;
 
@@ -125,7 +163,7 @@
 
         /**
          * The component name of the original activity (pre-archival).
-            */
+         */
         @DataClass.Generated.Member
         public @NonNull ComponentName getOriginalComponentName() {
             return mOriginalComponentName;
@@ -189,18 +227,17 @@
 
             int _hash = 1;
             _hash = 31 * _hash + java.util.Objects.hashCode(mTitle);
-            _hash = 31* _hash + java.util.Objects.hashCode(mOriginalComponentName);
+            _hash = 31 * _hash + java.util.Objects.hashCode(mOriginalComponentName);
             _hash = 31 * _hash + java.util.Objects.hashCode(mIconBitmap);
             _hash = 31 * _hash + java.util.Objects.hashCode(mMonochromeIconBitmap);
             return _hash;
         }
 
         @DataClass.Generated(
-                time = 1693590309015L,
+                time = 1701471309832L,
                 codegenVersion = "1.0.23",
                 sourceFile = "frameworks/base/services/core/java/com/android/server/pm/pkg/ArchiveState.java",
-                inputSignatures =
-                        "private final @android.annotation.NonNull java.lang.String mTitle\nprivate final @android.annotation.NonNull android.content.ComponentName mOriginalComponentName\nprivate final @android.annotation.Nullable java.nio.file.Path mIconBitmap\nprivate final @android.annotation.Nullable java.nio.file.Path mMonochromeIconBitmap\nclass ArchiveActivityInfo extends java.lang.Object implements []\n@com.android.internal.util.DataClass(genEqualsHashCode=true, genToString=true)")
+                inputSignatures = "private final @android.annotation.NonNull java.lang.String mTitle\nprivate final @android.annotation.NonNull android.content.ComponentName mOriginalComponentName\nprivate final @android.annotation.Nullable java.nio.file.Path mIconBitmap\nprivate final @android.annotation.Nullable java.nio.file.Path mMonochromeIconBitmap\nclass ArchiveActivityInfo extends java.lang.Object implements []\n@com.android.internal.util.DataClass(genEqualsHashCode=true, genToString=true)")
         @Deprecated
         private void __metadata() {}
 
@@ -238,15 +275,24 @@
      * @param installerTitle
      *   Corresponds to android:label of the installer responsible for the unarchival of the app.
      *   Stored in the installer's locale .
+     * @param archiveTimeMillis
+     *   The time at which the app was archived for the user.  Units are as per
+     *   {@link System#currentTimeMillis()}.
      */
     @DataClass.Generated.Member
     public ArchiveState(
             @NonNull List<ArchiveActivityInfo> activityInfos,
-            @NonNull String installerTitle) {
+            @NonNull String installerTitle,
+            @CurrentTimeMillisLong long archiveTimeMillis) {
         this.mActivityInfos = activityInfos;
-        AnnotationValidations.validate(NonNull.class, null, mActivityInfos);
+        AnnotationValidations.validate(
+                NonNull.class, null, mActivityInfos);
         this.mInstallerTitle = installerTitle;
-        AnnotationValidations.validate(NonNull.class, null, mInstallerTitle);
+        AnnotationValidations.validate(
+                NonNull.class, null, mInstallerTitle);
+        this.mArchiveTimeMillis = archiveTimeMillis;
+        AnnotationValidations.validate(
+                CurrentTimeMillisLong.class, null, mArchiveTimeMillis);
 
         // onConstructed(); // You can define this method to get a callback
     }
@@ -271,6 +317,15 @@
         return mInstallerTitle;
     }
 
+    /**
+     * The time at which the app was archived for the user.  Units are as per
+     * {@link System#currentTimeMillis()}.
+     */
+    @DataClass.Generated.Member
+    public @CurrentTimeMillisLong long getArchiveTimeMillis() {
+        return mArchiveTimeMillis;
+    }
+
     @Override
     @DataClass.Generated.Member
     public String toString() {
@@ -279,7 +334,8 @@
 
         return "ArchiveState { " +
                 "activityInfos = " + mActivityInfos + ", " +
-                "installerTitle = " + mInstallerTitle +
+                "installerTitle = " + mInstallerTitle + ", " +
+                "archiveTimeMillis = " + mArchiveTimeMillis +
         " }";
     }
 
@@ -297,7 +353,8 @@
         //noinspection PointlessBooleanExpression
         return true
                 && java.util.Objects.equals(mActivityInfos, that.mActivityInfos)
-                && java.util.Objects.equals(mInstallerTitle, that.mInstallerTitle);
+                && java.util.Objects.equals(mInstallerTitle, that.mInstallerTitle)
+                && mArchiveTimeMillis == that.mArchiveTimeMillis;
     }
 
     @Override
@@ -309,14 +366,15 @@
         int _hash = 1;
         _hash = 31 * _hash + java.util.Objects.hashCode(mActivityInfos);
         _hash = 31 * _hash + java.util.Objects.hashCode(mInstallerTitle);
+        _hash = 31 * _hash + Long.hashCode(mArchiveTimeMillis);
         return _hash;
     }
 
     @DataClass.Generated(
-            time = 1693590309027L,
+            time = 1701471309853L,
             codegenVersion = "1.0.23",
             sourceFile = "frameworks/base/services/core/java/com/android/server/pm/pkg/ArchiveState.java",
-            inputSignatures = "private final @android.annotation.NonNull java.util.List<com.android.server.pm.pkg.ArchiveActivityInfo> mActivityInfos\nprivate final @android.annotation.NonNull java.lang.String mInstallerTitle\nclass ArchiveState extends java.lang.Object implements []\n@com.android.internal.util.DataClass(genEqualsHashCode=true, genToString=true)")
+            inputSignatures = "private final @android.annotation.NonNull java.util.List<com.android.server.pm.pkg.ArchiveActivityInfo> mActivityInfos\nprivate final @android.annotation.NonNull java.lang.String mInstallerTitle\nprivate final @android.annotation.CurrentTimeMillisLong long mArchiveTimeMillis\nclass ArchiveState extends java.lang.Object implements []\n@com.android.internal.util.DataClass(genEqualsHashCode=true, genToString=true)")
     @Deprecated
     private void __metadata() {}
 
diff --git a/services/core/java/com/android/server/pm/pkg/PackageUserState.java b/services/core/java/com/android/server/pm/pkg/PackageUserState.java
index 8eb3466..2a81a86 100644
--- a/services/core/java/com/android/server/pm/pkg/PackageUserState.java
+++ b/services/core/java/com/android/server/pm/pkg/PackageUserState.java
@@ -256,10 +256,4 @@
      * @hide
      */
     boolean dataExists();
-
-    /**
-     * Timestamp of when the app is archived on the user.
-     * @hide
-     */
-    long getArchiveTimeMillis();
 }
diff --git a/services/core/java/com/android/server/pm/pkg/PackageUserStateDefault.java b/services/core/java/com/android/server/pm/pkg/PackageUserStateDefault.java
index defd343..2f4ad2d8 100644
--- a/services/core/java/com/android/server/pm/pkg/PackageUserStateDefault.java
+++ b/services/core/java/com/android/server/pm/pkg/PackageUserStateDefault.java
@@ -206,9 +206,4 @@
     public boolean dataExists() {
         return true;
     }
-
-    @Override
-    public long getArchiveTimeMillis() {
-        return 0;
-    }
 }
diff --git a/services/core/java/com/android/server/pm/pkg/PackageUserStateImpl.java b/services/core/java/com/android/server/pm/pkg/PackageUserStateImpl.java
index c0ea7cc..c5ef525 100644
--- a/services/core/java/com/android/server/pm/pkg/PackageUserStateImpl.java
+++ b/services/core/java/com/android/server/pm/pkg/PackageUserStateImpl.java
@@ -135,8 +135,6 @@
     @Nullable
     private ArchiveState mArchiveState;
 
-    private @CurrentTimeMillisLong long mArchiveTimeMillis;
-
     @NonNull
     final SnapshotCache<PackageUserStateImpl> mSnapshot;
 
@@ -189,7 +187,6 @@
                 ? null : other.mComponentLabelIconOverrideMap.snapshot();
         mFirstInstallTimeMillis = other.mFirstInstallTimeMillis;
         mArchiveState = other.mArchiveState;
-        mArchiveTimeMillis = other.mArchiveTimeMillis;
         mSnapshot = new SnapshotCache.Sealed<>();
     }
 
@@ -613,16 +610,6 @@
         return this;
     }
 
-    /**
-     * Sets the timestamp when the app is archived on this user.
-     */
-    @NonNull
-    public PackageUserStateImpl setArchiveTimeMillis(@CurrentTimeMillisLong long value) {
-        mArchiveTimeMillis = value;
-        onChanged();
-        return this;
-    }
-
     @NonNull
     @Override
     public Map<String, OverlayPaths> getSharedLibraryOverlayPaths() {
@@ -811,11 +798,6 @@
     }
 
     @DataClass.Generated.Member
-    public @CurrentTimeMillisLong long getArchiveTimeMillis() {
-        return mArchiveTimeMillis;
-    }
-
-    @DataClass.Generated.Member
     public @NonNull SnapshotCache<PackageUserStateImpl> getSnapshot() {
         return mSnapshot;
     }
@@ -892,7 +874,6 @@
                 && mFirstInstallTimeMillis == that.mFirstInstallTimeMillis
                 && watchableEquals(that.mWatchable)
                 && Objects.equals(mArchiveState, that.mArchiveState)
-                && mArchiveTimeMillis == that.mArchiveTimeMillis
                 && snapshotEquals(that.mSnapshot);
     }
 
@@ -923,16 +904,15 @@
         _hash = 31 * _hash + Long.hashCode(mFirstInstallTimeMillis);
         _hash = 31 * _hash + watchableHashCode();
         _hash = 31 * _hash + Objects.hashCode(mArchiveState);
-        _hash = 31 * _hash + Long.hashCode(mArchiveTimeMillis);
         _hash = 31 * _hash + snapshotHashCode();
         return _hash;
     }
 
     @DataClass.Generated(
-            time = 1699917927942L,
+            time = 1701470095849L,
             codegenVersion = "1.0.23",
             sourceFile = "frameworks/base/services/core/java/com/android/server/pm/pkg/PackageUserStateImpl.java",
-            inputSignatures = "private  int mBooleans\nprotected @android.annotation.Nullable com.android.server.utils.WatchedArraySet<java.lang.String> mDisabledComponentsWatched\nprotected @android.annotation.Nullable com.android.server.utils.WatchedArraySet<java.lang.String> mEnabledComponentsWatched\nprivate  long mCeDataInode\nprivate  long mDeDataInode\nprivate  int mDistractionFlags\nprivate @android.content.pm.PackageManager.EnabledState int mEnabledState\nprivate @android.content.pm.PackageManager.InstallReason int mInstallReason\nprivate @android.content.pm.PackageManager.UninstallReason int mUninstallReason\nprivate @android.annotation.Nullable java.lang.String mHarmfulAppWarning\nprivate @android.annotation.Nullable java.lang.String mLastDisableAppCaller\nprivate @android.annotation.Nullable android.content.pm.overlay.OverlayPaths mOverlayPaths\nprotected @android.annotation.Nullable com.android.server.utils.WatchedArrayMap<java.lang.String,android.content.pm.overlay.OverlayPaths> mSharedLibraryOverlayPaths\nprivate @android.annotation.Nullable java.lang.String mSplashScreenTheme\nprivate @android.content.pm.PackageManager.UserMinAspectRatio int mMinAspectRatio\nprivate @android.annotation.Nullable com.android.server.utils.WatchedArrayMap<java.lang.String,com.android.server.pm.pkg.SuspendParams> mSuspendParams\nprivate @android.annotation.Nullable com.android.server.utils.WatchedArrayMap<android.content.ComponentName,android.util.Pair<java.lang.String,java.lang.Integer>> mComponentLabelIconOverrideMap\nprivate @android.annotation.CurrentTimeMillisLong long mFirstInstallTimeMillis\nprivate @android.annotation.Nullable com.android.server.utils.Watchable mWatchable\nprivate @android.annotation.Nullable com.android.server.pm.pkg.ArchiveState mArchiveState\nprivate @android.annotation.CurrentTimeMillisLong long mArchiveTimeMillis\nfinal @android.annotation.NonNull com.android.server.utils.SnapshotCache<com.android.server.pm.pkg.PackageUserStateImpl> mSnapshot\nprivate  void setBoolean(int,boolean)\nprivate  boolean getBoolean(int)\nprivate  com.android.server.utils.SnapshotCache<com.android.server.pm.pkg.PackageUserStateImpl> makeCache()\nprivate  void onChanged()\npublic @android.annotation.NonNull @java.lang.Override com.android.server.pm.pkg.PackageUserStateImpl snapshot()\npublic @android.annotation.Nullable boolean setOverlayPaths(android.content.pm.overlay.OverlayPaths)\npublic  boolean setSharedLibraryOverlayPaths(java.lang.String,android.content.pm.overlay.OverlayPaths)\npublic @android.annotation.Nullable @java.lang.Override com.android.server.utils.WatchedArraySet<java.lang.String> getDisabledComponentsNoCopy()\npublic @android.annotation.Nullable @java.lang.Override com.android.server.utils.WatchedArraySet<java.lang.String> getEnabledComponentsNoCopy()\npublic @android.annotation.NonNull @java.lang.Override android.util.ArraySet<java.lang.String> getDisabledComponents()\npublic @android.annotation.NonNull @java.lang.Override android.util.ArraySet<java.lang.String> getEnabledComponents()\npublic @java.lang.Override boolean isComponentEnabled(java.lang.String)\npublic @java.lang.Override boolean isComponentDisabled(java.lang.String)\npublic @java.lang.Override android.content.pm.overlay.OverlayPaths getAllOverlayPaths()\npublic @com.android.internal.annotations.VisibleForTesting boolean overrideLabelAndIcon(android.content.ComponentName,java.lang.String,java.lang.Integer)\npublic  void resetOverrideComponentLabelIcon()\npublic @android.annotation.Nullable android.util.Pair<java.lang.String,java.lang.Integer> getOverrideLabelIconForComponent(android.content.ComponentName)\npublic @java.lang.Override boolean isSuspended()\npublic  com.android.server.pm.pkg.PackageUserStateImpl putSuspendParams(java.lang.String,com.android.server.pm.pkg.SuspendParams)\npublic  com.android.server.pm.pkg.PackageUserStateImpl removeSuspension(java.lang.String)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setDisabledComponents(android.util.ArraySet<java.lang.String>)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setEnabledComponents(android.util.ArraySet<java.lang.String>)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setEnabledComponents(com.android.server.utils.WatchedArraySet<java.lang.String>)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setDisabledComponents(com.android.server.utils.WatchedArraySet<java.lang.String>)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setCeDataInode(long)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setDeDataInode(long)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setInstalled(boolean)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setStopped(boolean)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setNotLaunched(boolean)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setHidden(boolean)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setDistractionFlags(int)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setInstantApp(boolean)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setVirtualPreload(boolean)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setEnabledState(int)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setInstallReason(int)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setUninstallReason(int)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setHarmfulAppWarning(java.lang.String)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setLastDisableAppCaller(java.lang.String)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setSharedLibraryOverlayPaths(android.util.ArrayMap<java.lang.String,android.content.pm.overlay.OverlayPaths>)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setSplashScreenTheme(java.lang.String)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setMinAspectRatio(int)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setSuspendParams(android.util.ArrayMap<java.lang.String,com.android.server.pm.pkg.SuspendParams>)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setComponentLabelIconOverrideMap(android.util.ArrayMap<android.content.ComponentName,android.util.Pair<java.lang.String,java.lang.Integer>>)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setFirstInstallTimeMillis(long)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setArchiveState(com.android.server.pm.pkg.ArchiveState)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setArchiveTimeMillis(long)\npublic @android.annotation.NonNull @java.lang.Override java.util.Map<java.lang.String,android.content.pm.overlay.OverlayPaths> getSharedLibraryOverlayPaths()\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setWatchable(com.android.server.utils.Watchable)\nprivate  boolean watchableEquals(com.android.server.utils.Watchable)\nprivate  int watchableHashCode()\nprivate  boolean snapshotEquals(com.android.server.utils.SnapshotCache<com.android.server.pm.pkg.PackageUserStateImpl>)\nprivate  int snapshotHashCode()\npublic @java.lang.Override boolean isInstalled()\npublic @java.lang.Override boolean isStopped()\npublic @java.lang.Override boolean isNotLaunched()\npublic @java.lang.Override boolean isHidden()\npublic @java.lang.Override boolean isInstantApp()\npublic @java.lang.Override boolean isVirtualPreload()\npublic @java.lang.Override boolean isQuarantined()\npublic @java.lang.Override boolean dataExists()\nclass PackageUserStateImpl extends com.android.server.utils.WatchableImpl implements [com.android.server.pm.pkg.PackageUserStateInternal, com.android.server.utils.Snappable]\nprivate static final  int INSTALLED\nprivate static final  int STOPPED\nprivate static final  int NOT_LAUNCHED\nprivate static final  int HIDDEN\nprivate static final  int INSTANT_APP\nprivate static final  int VIRTUAL_PRELOADED\nclass Booleans extends java.lang.Object implements []\n@com.android.internal.util.DataClass(genConstructor=false, genBuilder=false, genEqualsHashCode=true)")
+            inputSignatures = "private  int mBooleans\nprotected @android.annotation.Nullable com.android.server.utils.WatchedArraySet<java.lang.String> mDisabledComponentsWatched\nprotected @android.annotation.Nullable com.android.server.utils.WatchedArraySet<java.lang.String> mEnabledComponentsWatched\nprivate  long mCeDataInode\nprivate  long mDeDataInode\nprivate  int mDistractionFlags\nprivate @android.content.pm.PackageManager.EnabledState int mEnabledState\nprivate @android.content.pm.PackageManager.InstallReason int mInstallReason\nprivate @android.content.pm.PackageManager.UninstallReason int mUninstallReason\nprivate @android.annotation.Nullable java.lang.String mHarmfulAppWarning\nprivate @android.annotation.Nullable java.lang.String mLastDisableAppCaller\nprivate @android.annotation.Nullable android.content.pm.overlay.OverlayPaths mOverlayPaths\nprotected @android.annotation.Nullable com.android.server.utils.WatchedArrayMap<java.lang.String,android.content.pm.overlay.OverlayPaths> mSharedLibraryOverlayPaths\nprivate @android.annotation.Nullable java.lang.String mSplashScreenTheme\nprivate @android.content.pm.PackageManager.UserMinAspectRatio int mMinAspectRatio\nprivate @android.annotation.Nullable com.android.server.utils.WatchedArrayMap<java.lang.String,com.android.server.pm.pkg.SuspendParams> mSuspendParams\nprivate @android.annotation.Nullable com.android.server.utils.WatchedArrayMap<android.content.ComponentName,android.util.Pair<java.lang.String,java.lang.Integer>> mComponentLabelIconOverrideMap\nprivate @android.annotation.CurrentTimeMillisLong long mFirstInstallTimeMillis\nprivate @android.annotation.Nullable com.android.server.utils.Watchable mWatchable\nprivate @android.annotation.Nullable com.android.server.pm.pkg.ArchiveState mArchiveState\nfinal @android.annotation.NonNull com.android.server.utils.SnapshotCache<com.android.server.pm.pkg.PackageUserStateImpl> mSnapshot\nprivate  void setBoolean(int,boolean)\nprivate  boolean getBoolean(int)\nprivate  com.android.server.utils.SnapshotCache<com.android.server.pm.pkg.PackageUserStateImpl> makeCache()\nprivate  void onChanged()\npublic @android.annotation.NonNull @java.lang.Override com.android.server.pm.pkg.PackageUserStateImpl snapshot()\npublic @android.annotation.Nullable boolean setOverlayPaths(android.content.pm.overlay.OverlayPaths)\npublic  boolean setSharedLibraryOverlayPaths(java.lang.String,android.content.pm.overlay.OverlayPaths)\npublic @android.annotation.Nullable @java.lang.Override com.android.server.utils.WatchedArraySet<java.lang.String> getDisabledComponentsNoCopy()\npublic @android.annotation.Nullable @java.lang.Override com.android.server.utils.WatchedArraySet<java.lang.String> getEnabledComponentsNoCopy()\npublic @android.annotation.NonNull @java.lang.Override android.util.ArraySet<java.lang.String> getDisabledComponents()\npublic @android.annotation.NonNull @java.lang.Override android.util.ArraySet<java.lang.String> getEnabledComponents()\npublic @java.lang.Override boolean isComponentEnabled(java.lang.String)\npublic @java.lang.Override boolean isComponentDisabled(java.lang.String)\npublic @java.lang.Override android.content.pm.overlay.OverlayPaths getAllOverlayPaths()\npublic @com.android.internal.annotations.VisibleForTesting boolean overrideLabelAndIcon(android.content.ComponentName,java.lang.String,java.lang.Integer)\npublic  void resetOverrideComponentLabelIcon()\npublic @android.annotation.Nullable android.util.Pair<java.lang.String,java.lang.Integer> getOverrideLabelIconForComponent(android.content.ComponentName)\npublic @java.lang.Override boolean isSuspended()\npublic  com.android.server.pm.pkg.PackageUserStateImpl putSuspendParams(java.lang.String,com.android.server.pm.pkg.SuspendParams)\npublic  com.android.server.pm.pkg.PackageUserStateImpl removeSuspension(java.lang.String)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setDisabledComponents(android.util.ArraySet<java.lang.String>)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setEnabledComponents(android.util.ArraySet<java.lang.String>)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setEnabledComponents(com.android.server.utils.WatchedArraySet<java.lang.String>)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setDisabledComponents(com.android.server.utils.WatchedArraySet<java.lang.String>)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setCeDataInode(long)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setDeDataInode(long)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setInstalled(boolean)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setStopped(boolean)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setNotLaunched(boolean)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setHidden(boolean)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setDistractionFlags(int)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setInstantApp(boolean)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setVirtualPreload(boolean)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setEnabledState(int)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setInstallReason(int)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setUninstallReason(int)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setHarmfulAppWarning(java.lang.String)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setLastDisableAppCaller(java.lang.String)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setSharedLibraryOverlayPaths(android.util.ArrayMap<java.lang.String,android.content.pm.overlay.OverlayPaths>)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setSplashScreenTheme(java.lang.String)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setMinAspectRatio(int)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setSuspendParams(android.util.ArrayMap<java.lang.String,com.android.server.pm.pkg.SuspendParams>)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setComponentLabelIconOverrideMap(android.util.ArrayMap<android.content.ComponentName,android.util.Pair<java.lang.String,java.lang.Integer>>)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setFirstInstallTimeMillis(long)\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setArchiveState(com.android.server.pm.pkg.ArchiveState)\npublic @android.annotation.NonNull @java.lang.Override java.util.Map<java.lang.String,android.content.pm.overlay.OverlayPaths> getSharedLibraryOverlayPaths()\npublic @android.annotation.NonNull com.android.server.pm.pkg.PackageUserStateImpl setWatchable(com.android.server.utils.Watchable)\nprivate  boolean watchableEquals(com.android.server.utils.Watchable)\nprivate  int watchableHashCode()\nprivate  boolean snapshotEquals(com.android.server.utils.SnapshotCache<com.android.server.pm.pkg.PackageUserStateImpl>)\nprivate  int snapshotHashCode()\npublic @java.lang.Override boolean isInstalled()\npublic @java.lang.Override boolean isStopped()\npublic @java.lang.Override boolean isNotLaunched()\npublic @java.lang.Override boolean isHidden()\npublic @java.lang.Override boolean isInstantApp()\npublic @java.lang.Override boolean isVirtualPreload()\npublic @java.lang.Override boolean isQuarantined()\npublic @java.lang.Override boolean dataExists()\nclass PackageUserStateImpl extends com.android.server.utils.WatchableImpl implements [com.android.server.pm.pkg.PackageUserStateInternal, com.android.server.utils.Snappable]\nprivate static final  int INSTALLED\nprivate static final  int STOPPED\nprivate static final  int NOT_LAUNCHED\nprivate static final  int HIDDEN\nprivate static final  int INSTANT_APP\nprivate static final  int VIRTUAL_PRELOADED\nclass Booleans extends java.lang.Object implements []\n@com.android.internal.util.DataClass(genConstructor=false, genBuilder=false, genEqualsHashCode=true)")
     @Deprecated
     private void __metadata() {}
 
diff --git a/services/core/java/com/android/server/power/ShutdownThread.java b/services/core/java/com/android/server/power/ShutdownThread.java
index 27811e9..871e98b 100644
--- a/services/core/java/com/android/server/power/ShutdownThread.java
+++ b/services/core/java/com/android/server/power/ShutdownThread.java
@@ -19,6 +19,7 @@
 
 import android.app.ActivityManagerInternal;
 import android.app.AlertDialog;
+import android.app.BroadcastOptions;
 import android.app.Dialog;
 import android.app.IActivityManager;
 import android.app.ProgressDialog;
@@ -493,6 +494,9 @@
         mActionDone = false;
         Intent intent = new Intent(Intent.ACTION_SHUTDOWN);
         intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND | Intent.FLAG_RECEIVER_REGISTERED_ONLY);
+        final Bundle opts = BroadcastOptions.makeBasic()
+                .setDeferralPolicy(BroadcastOptions.DEFERRAL_POLICY_UNTIL_ACTIVE)
+                .toBundle();
         final ActivityManagerInternal activityManagerInternal = LocalServices.getService(
                 ActivityManagerInternal.class);
         activityManagerInternal.broadcastIntentWithCallback(intent,
@@ -502,7 +506,7 @@
                             Bundle extras, boolean ordered, boolean sticky, int sendingUser) {
                         mHandler.post(ShutdownThread.this::actionDone);
                     }
-                }, null, UserHandle.USER_ALL, null, null, null);
+                }, null, UserHandle.USER_ALL, null, null, opts);
 
         final long endTime = SystemClock.elapsedRealtime() + MAX_BROADCAST_TIME;
         synchronized (mActionDoneSync) {
diff --git a/services/core/java/com/android/server/utils/Android.bp b/services/core/java/com/android/server/utils/Android.bp
new file mode 100644
index 0000000..3a334be
--- /dev/null
+++ b/services/core/java/com/android/server/utils/Android.bp
@@ -0,0 +1,10 @@
+aconfig_declarations {
+    name: "com.android.server.utils-aconfig",
+    package: "com.android.server.utils",
+    srcs: ["*.aconfig"],
+}
+
+java_aconfig_library {
+    name: "com.android.server.utils_aconfig-java",
+    aconfig_declarations: "com.android.server.utils-aconfig",
+}
diff --git a/services/core/java/com/android/server/am/AnrTimer.java b/services/core/java/com/android/server/utils/AnrTimer.java
similarity index 98%
rename from services/core/java/com/android/server/am/AnrTimer.java
rename to services/core/java/com/android/server/utils/AnrTimer.java
index 3e17930..2b6dffb 100644
--- a/services/core/java/com/android/server/am/AnrTimer.java
+++ b/services/core/java/com/android/server/utils/AnrTimer.java
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package com.android.server.am;
+package com.android.server.utils;
 
 import static android.text.TextUtils.formatSimple;
 
@@ -77,7 +77,7 @@
  *
  * @hide
  */
-class AnrTimer<V> {
+public class AnrTimer<V> {
 
     /**
      * The log tag.
@@ -568,7 +568,7 @@
      * @param label A name for this instance.
      * @param extend A flag to indicate if expired timers can be granted extensions.
      */
-    AnrTimer(@NonNull Handler handler, int what, @NonNull String label, boolean extend) {
+    public AnrTimer(@NonNull Handler handler, int what, @NonNull String label, boolean extend) {
         this(handler, what, label, extend, new Injector(handler));
     }
 
@@ -580,7 +580,7 @@
      * @param what The "what" parameter for the expiration message.
      * @param label A name for this instance.
      */
-    AnrTimer(@NonNull Handler handler, int what, @NonNull String label) {
+    public AnrTimer(@NonNull Handler handler, int what, @NonNull String label) {
         this(handler, what, label, false);
     }
 
@@ -591,7 +591,7 @@
      *
      * @return true if the service is flag-enabled.
      */
-    boolean serviceEnabled() {
+    public boolean serviceEnabled() {
         return mFeature.enabled();
     }
 
@@ -856,7 +856,7 @@
      * @param timeoutMs The timer timeout, in milliseconds.
      * @return true if the timer was successfully created.
      */
-    boolean start(@NonNull V arg, int pid, int uid, long timeoutMs) {
+    public boolean start(@NonNull V arg, int pid, int uid, long timeoutMs) {
         return mFeature.start(arg, pid, uid, timeoutMs);
     }
 
@@ -867,7 +867,7 @@
      *
      * @return true if the timer was found and was running.
      */
-    boolean cancel(@NonNull V arg) {
+    public boolean cancel(@NonNull V arg) {
         return mFeature.cancel(arg);
     }
 
@@ -878,7 +878,7 @@
      *
      * @return true if the timer was found and was expired.
      */
-    boolean accept(@NonNull V arg) {
+    public boolean accept(@NonNull V arg) {
         return mFeature.accept(arg);
     }
 
@@ -892,7 +892,7 @@
      *
      * @return true if the timer was found and was expired.
      */
-    boolean discard(@NonNull V arg) {
+    public boolean discard(@NonNull V arg) {
         return mFeature.discard(arg);
     }
 
@@ -1010,7 +1010,7 @@
     /**
      * Dumpsys output.
      */
-    static void dump(@NonNull PrintWriter pw, boolean verbose) {
+    public static void dump(@NonNull PrintWriter pw, boolean verbose) {
         final IndentingPrintWriter ipw = new IndentingPrintWriter(pw);
         ipw.println("AnrTimer statistics");
         ipw.increaseIndent();
diff --git a/services/core/java/com/android/server/utils/flags.aconfig b/services/core/java/com/android/server/utils/flags.aconfig
new file mode 100644
index 0000000..489e21a
--- /dev/null
+++ b/services/core/java/com/android/server/utils/flags.aconfig
@@ -0,0 +1,9 @@
+package: "com.android.server.utils"
+
+flag {
+     name: "anr_timer_service_enabled"
+     namespace: "system_performance"
+     is_fixed_read_only: true
+     description: "Feature flag for the ANR timer service"
+     bug: "282428924"
+}
diff --git a/services/core/java/com/android/server/wallpaper/WallpaperManagerService.java b/services/core/java/com/android/server/wallpaper/WallpaperManagerService.java
index e088d9a..1485b96 100644
--- a/services/core/java/com/android/server/wallpaper/WallpaperManagerService.java
+++ b/services/core/java/com/android/server/wallpaper/WallpaperManagerService.java
@@ -40,6 +40,7 @@
 import static com.android.server.wallpaper.WallpaperUtils.WALLPAPER_LOCK_ORIG;
 import static com.android.server.wallpaper.WallpaperUtils.getWallpaperDir;
 import static com.android.server.wallpaper.WallpaperUtils.makeWallpaperIdLocked;
+import static com.android.window.flags.Flags.multiCrop;
 
 import android.annotation.NonNull;
 import android.app.ActivityManager;
@@ -93,7 +94,6 @@
 import android.os.SELinux;
 import android.os.ShellCallback;
 import android.os.SystemClock;
-import android.os.SystemProperties;
 import android.os.UserHandle;
 import android.os.UserManager;
 import android.os.storage.StorageManager;
@@ -1516,8 +1516,7 @@
         mColorsChangedListeners = new SparseArray<>();
         mWallpaperDataParser = new WallpaperDataParser(mContext, mWallpaperDisplayHelper,
                 mWallpaperCropper);
-        mIsMultiCropEnabled =
-                SystemProperties.getBoolean("persist.wm.debug.wallpaper_multi_crop", false);
+        mIsMultiCropEnabled = multiCrop();
         LocalServices.addService(WallpaperManagerInternal.class, new LocalService());
     }
 
diff --git a/services/core/java/com/android/server/wm/ActivityTaskManagerService.java b/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
index b8b102f..91f45a7 100644
--- a/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
+++ b/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
@@ -76,6 +76,7 @@
 import static com.android.internal.protolog.ProtoLogGroup.WM_DEBUG_IMMERSIVE;
 import static com.android.internal.protolog.ProtoLogGroup.WM_DEBUG_LOCKTASK;
 import static com.android.internal.protolog.ProtoLogGroup.WM_DEBUG_TASKS;
+import static com.android.sdksandbox.flags.Flags.sandboxActivitySdkBasedContext;
 import static com.android.server.am.ActivityManagerService.STOCK_PM_FLAGS;
 import static com.android.server.am.ActivityManagerServiceDumpActivitiesProto.ROOT_WINDOW_CONTAINER;
 import static com.android.server.am.ActivityManagerServiceDumpProcessesProto.CONFIG_WILL_CHANGE;
@@ -125,7 +126,6 @@
 import static com.android.server.wm.Task.REPARENT_KEEP_ROOT_TASK_AT_FRONT;
 import static com.android.server.wm.WindowManagerService.MY_PID;
 import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_NORMAL;
-import static com.android.sdksandbox.flags.Flags.sandboxActivitySdkBasedContext;
 
 import android.Manifest;
 import android.annotation.IntDef;
@@ -1261,10 +1261,10 @@
                 true /*validateIncomingUser*/);
     }
 
-    static boolean isSdkSandboxActivity(Context context, Intent intent) {
+    static boolean isSdkSandboxActivityIntent(Context context, Intent intent) {
         return intent != null
                 && (sandboxActivitySdkBasedContext()
-                        ? SdkSandboxActivityAuthority.isSdkSandboxActivity(context, intent)
+                        ? SdkSandboxActivityAuthority.isSdkSandboxActivityIntent(context, intent)
                         : intent.isSandboxActivity(context));
     }
 
@@ -1278,7 +1278,7 @@
         assertPackageMatchesCallingUid(callingPackage);
         enforceNotIsolatedCaller("startActivityAsUser");
 
-        if (isSdkSandboxActivity(mContext, intent)) {
+        if (isSdkSandboxActivityIntent(mContext, intent)) {
             SdkSandboxManagerLocal sdkSandboxManagerLocal = LocalManagerRegistry.getManager(
                     SdkSandboxManagerLocal.class);
             sdkSandboxManagerLocal.enforceAllowedToHostSandboxedActivity(
diff --git a/services/core/java/com/android/server/wm/ActivityTaskSupervisor.java b/services/core/java/com/android/server/wm/ActivityTaskSupervisor.java
index a21b9b4..4a479aa 100644
--- a/services/core/java/com/android/server/wm/ActivityTaskSupervisor.java
+++ b/services/core/java/com/android/server/wm/ActivityTaskSupervisor.java
@@ -1085,7 +1085,8 @@
             // Remove the process record so it won't be considered as alive.
             mService.mProcessNames.remove(wpc.mName, wpc.mUid);
             mService.mProcessMap.remove(wpc.getPid());
-        } else if (ActivityTaskManagerService.isSdkSandboxActivity(mService.mContext, r.intent)) {
+        } else if (ActivityTaskManagerService.isSdkSandboxActivityIntent(
+                mService.mContext, r.intent)) {
             Slog.e(TAG, "Abort sandbox activity launching as no sandbox process to host it.");
             r.finishIfPossible("No sandbox process for the activity", false /* oomAdj */);
             r.launchFailed = true;
diff --git a/services/core/java/com/android/server/wm/BackNavigationController.java b/services/core/java/com/android/server/wm/BackNavigationController.java
index be7b855..c5902c9 100644
--- a/services/core/java/com/android/server/wm/BackNavigationController.java
+++ b/services/core/java/com/android/server/wm/BackNavigationController.java
@@ -395,7 +395,8 @@
      *
      * @return false if unable to predict what will happen
      */
-    private static boolean getAnimatablePrevActivities(@NonNull Task currentTask,
+    @VisibleForTesting
+    static boolean getAnimatablePrevActivities(@NonNull Task currentTask,
             @NonNull ActivityRecord currentActivity,
             @NonNull ArrayList<ActivityRecord> outPrevActivities) {
         if (currentActivity.mAtmService
@@ -413,45 +414,86 @@
         // Searching previous
         final ActivityRecord prevActivity = currentTask.getActivity((below) -> !below.finishing,
                 currentActivity, false /*includeBoundary*/, true /*traverseTopToBottom*/);
-        if (prevActivity == null) {
-            // No previous activity in this task, can still predict if previous task exists.
-            return true;
-        }
-        if (currentTask.getActivity((above) -> !above.finishing, currentActivity,
-                false /*includeBoundary*/, false /*traverseTopToBottom*/) != null) {
-            // another activity is above this activity, don't know what will happen
-            return false;
-        }
 
         final TaskFragment currTF = currentActivity.getTaskFragment();
-        final TaskFragment prevTF = prevActivity.getTaskFragment();
-        if (currTF != prevTF && prevTF != null) {
-            final TaskFragment prevTFAdjacent = prevTF.getAdjacentTaskFragment();
-            if (prevTFAdjacent != null) {
-                if (prevTFAdjacent == currTF) {
-                    outPrevActivities.clear();
-                    // No more activity in task, so it can predict if previous task exists.
-                    // Otherwise, unable to predict what will happen when app receive
-                    // back key, skip animation.
-                    return currentTask.getActivity((below) -> !below.finishing, prevActivity,
+        if (currTF != null && currTF.asTask() == null) {
+            // The currentActivity is embedded, search for the candidate previous activities.
+            if (prevActivity != null && currTF.hasChild(prevActivity)) {
+                // PrevActivity is under the same task fragment, that's it.
+                outPrevActivities.add(prevActivity);
+                return true;
+            }
+            if (currTF.getAdjacentTaskFragment() != null) {
+                // The two TFs are adjacent (visually displayed side-by-side), search if any
+                // activity below the lowest one
+                // If companion, those two TF will be closed together.
+                if (currTF.getCompanionTaskFragment() != null) {
+                    final WindowContainer commonParent = currTF.getParent();
+                    final TaskFragment adjacentTF = currTF.getAdjacentTaskFragment();
+                    final TaskFragment lowerTF = commonParent.mChildren.indexOf(currTF)
+                            < commonParent.mChildren.indexOf(adjacentTF)
+                            ? currTF : adjacentTF;
+                    final ActivityRecord lowerActivity = lowerTF.getTopNonFinishingActivity();
+                    // TODO (b/274997067) close currTF + companionTF, open next activities if any.
+                    // Allow to predict next task if no more activity in task. Or return previous
+                    // activities for cross-activity animation.
+                    return currentTask.getActivity((below) -> !below.finishing, lowerActivity,
                             false /*includeBoundary*/, true /*traverseTopToBottom*/) == null;
-                } else {
-                    final ActivityRecord prevActivityAdjacent =
-                            prevTFAdjacent.getTopNonFinishingActivity();
-                    if (prevActivityAdjacent != null) {
-                        outPrevActivities.add(prevActivityAdjacent);
-                    } else {
-                        // Don't know what will happen.
-                        outPrevActivities.clear();
-                        return false;
-                    }
                 }
+                // Unable to predict if no companion, it can only close current activity and make
+                // prev Activity full screened.
+                return false;
+            } else if (currTF.getCompanionTaskFragment() != null) {
+                // TF is isStacked, search bottom activity from companion TF.
+                //
+                // Sample hierarchy: search for underPrevious if any.
+                //     Current TF
+                //     Companion TF (bottomActivityInCompanion)
+                //     Bottom Activity not inside companion TF (underPrevious)
+                final TaskFragment companionTF = currTF.getCompanionTaskFragment();
+                // find bottom activity in Companion TF.
+                final ActivityRecord bottomActivityInCompanion = companionTF.getActivity(
+                        (below) -> !below.finishing, false /* traverseTopToBottom */);
+                final ActivityRecord underPrevious = currentTask.getActivity(
+                        (below) -> !below.finishing, bottomActivityInCompanion,
+                        false /*includeBoundary*/, true /*traverseTopToBottom*/);
+                if (underPrevious != null) {
+                    outPrevActivities.add(underPrevious);
+                    addPreviousAdjacentActivityIfExist(underPrevious, outPrevActivities);
+                }
+                return true;
             }
         }
+
+        if (prevActivity == null) {
+            // No previous activity in this Task nor TaskFragment, it can still predict if previous
+            // task exists.
+            return true;
+        }
+        // Add possible adjacent activity if prevActivity is embedded
+        addPreviousAdjacentActivityIfExist(prevActivity, outPrevActivities);
         outPrevActivities.add(prevActivity);
         return true;
     }
 
+    private static void addPreviousAdjacentActivityIfExist(@NonNull ActivityRecord prevActivity,
+            @NonNull ArrayList<ActivityRecord> outPrevActivities) {
+        final TaskFragment prevTF = prevActivity.getTaskFragment();
+        if (prevTF == null || prevTF.asTask() != null) {
+            return;
+        }
+
+        final TaskFragment prevTFAdjacent = prevTF.getAdjacentTaskFragment();
+        if (prevTFAdjacent == null || prevTFAdjacent.asTask() != null) {
+            return;
+        }
+        final ActivityRecord prevActivityAdjacent =
+                prevTFAdjacent.getTopNonFinishingActivity();
+        if (prevActivityAdjacent != null) {
+            outPrevActivities.add(prevActivityAdjacent);
+        }
+    }
+
     private static void findAdjacentActivityIfExist(@NonNull ActivityRecord mainActivity,
             @NonNull ArrayList<ActivityRecord> outList) {
         final TaskFragment mainTF = mainActivity.getTaskFragment();
diff --git a/services/core/java/com/android/server/wm/BackgroundActivityStartController.java b/services/core/java/com/android/server/wm/BackgroundActivityStartController.java
index 4625b4fe..f8b22c9 100644
--- a/services/core/java/com/android/server/wm/BackgroundActivityStartController.java
+++ b/services/core/java/com/android/server/wm/BackgroundActivityStartController.java
@@ -614,6 +614,15 @@
                 == ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_SYSTEM_DEFINED;
         if (callerCanAllow && realCallerCanAllow) {
             // Both caller and real caller allow with system defined behavior
+            if (state.mBalAllowedByPiCreatorWithHardening.allowsBackgroundActivityStarts()) {
+                // Will be allowed even with BAL hardening.
+                if (DEBUG_ACTIVITY_STARTS) {
+                    Slog.d(TAG, "Activity start allowed by caller. "
+                            + state.dump(resultForCaller, resultForRealCaller));
+                }
+                // return the realCaller result for backwards compatibility
+                return statsLog(resultForRealCaller, state);
+            }
             if (state.mBalAllowedByPiCreator.allowsBackgroundActivityStarts()) {
                 Slog.wtf(TAG,
                         "With Android 15 BAL hardening this activity start may be blocked"
@@ -632,6 +641,14 @@
         }
         if (callerCanAllow) {
             // Allowed before V by creator
+            if (state.mBalAllowedByPiCreatorWithHardening.allowsBackgroundActivityStarts()) {
+                // Will be allowed even with BAL hardening.
+                if (DEBUG_ACTIVITY_STARTS) {
+                    Slog.d(TAG, "Activity start allowed by caller. "
+                            + state.dump(resultForCaller, resultForRealCaller));
+                }
+                return statsLog(resultForCaller, state);
+            }
             if (state.mBalAllowedByPiCreator.allowsBackgroundActivityStarts()) {
                 Slog.wtf(TAG,
                         "With Android 15 BAL hardening this activity start may be blocked"
diff --git a/services/core/java/com/android/server/wm/DisplayArea.java b/services/core/java/com/android/server/wm/DisplayArea.java
index f51bf7f..0006bd2 100644
--- a/services/core/java/com/android/server/wm/DisplayArea.java
+++ b/services/core/java/com/android/server/wm/DisplayArea.java
@@ -420,7 +420,7 @@
     @Override
     ActivityRecord getActivity(Predicate<ActivityRecord> callback, boolean traverseTopToBottom,
             ActivityRecord boundary) {
-        if (mType == Type.ABOVE_TASKS || mType == Type.BELOW_TASKS) {
+        if (mType == Type.ABOVE_TASKS) {
             return null;
         }
         return super.getActivity(callback, traverseTopToBottom, boundary);
@@ -428,23 +428,39 @@
 
     @Override
     Task getTask(Predicate<Task> callback, boolean traverseTopToBottom) {
-        if (mType == Type.ABOVE_TASKS || mType == Type.BELOW_TASKS) {
+        if (mType == Type.ABOVE_TASKS) {
             return null;
         }
         return super.getTask(callback, traverseTopToBottom);
     }
 
     @Override
+    Task getRootTask(Predicate<Task> callback, boolean traverseTopToBottom) {
+        if (mType == Type.ABOVE_TASKS) {
+            return null;
+        }
+        return super.getRootTask(callback, traverseTopToBottom);
+    }
+
+    @Override
     boolean forAllActivities(Predicate<ActivityRecord> callback, boolean traverseTopToBottom) {
-        if (mType == Type.ABOVE_TASKS || mType == Type.BELOW_TASKS) {
+        if (mType == Type.ABOVE_TASKS) {
             return false;
         }
         return super.forAllActivities(callback, traverseTopToBottom);
     }
 
     @Override
+    void forAllActivities(Consumer<ActivityRecord> callback, boolean traverseTopToBottom) {
+        if (mType == Type.ABOVE_TASKS) {
+            return;
+        }
+        super.forAllActivities(callback, traverseTopToBottom);
+    }
+
+    @Override
     boolean forAllRootTasks(Predicate<Task> callback, boolean traverseTopToBottom) {
-        if (mType == Type.ABOVE_TASKS || mType == Type.BELOW_TASKS) {
+        if (mType == Type.ABOVE_TASKS) {
             return false;
         }
         return super.forAllRootTasks(callback, traverseTopToBottom);
@@ -452,7 +468,7 @@
 
     @Override
     boolean forAllTasks(Predicate<Task> callback) {
-        if (mType == Type.ABOVE_TASKS || mType == Type.BELOW_TASKS) {
+        if (mType == Type.ABOVE_TASKS) {
             return false;
         }
         return super.forAllTasks(callback);
@@ -460,13 +476,29 @@
 
     @Override
     boolean forAllLeafTasks(Predicate<Task> callback) {
-        if (mType == Type.ABOVE_TASKS || mType == Type.BELOW_TASKS) {
+        if (mType == Type.ABOVE_TASKS) {
             return false;
         }
         return super.forAllLeafTasks(callback);
     }
 
     @Override
+    void forAllLeafTasks(Consumer<Task> callback, boolean traverseTopToBottom) {
+        if (mType == Type.ABOVE_TASKS) {
+            return;
+        }
+        super.forAllLeafTasks(callback, traverseTopToBottom);
+    }
+
+    @Override
+    boolean forAllLeafTaskFragments(Predicate<TaskFragment> callback) {
+        if (mType == Type.ABOVE_TASKS) {
+            return false;
+        }
+        return super.forAllLeafTaskFragments(callback);
+    }
+
+    @Override
     void forAllDisplayAreas(Consumer<DisplayArea> callback) {
         super.forAllDisplayAreas(callback);
         callback.accept(this);
diff --git a/services/core/java/com/android/server/wm/TrustedPresentationListenerController.java b/services/core/java/com/android/server/wm/TrustedPresentationListenerController.java
deleted file mode 100644
index e82dc37..0000000
--- a/services/core/java/com/android/server/wm/TrustedPresentationListenerController.java
+++ /dev/null
@@ -1,448 +0,0 @@
-/*
- * Copyright 2023 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.wm;
-
-import static android.graphics.Matrix.MSCALE_X;
-import static android.graphics.Matrix.MSCALE_Y;
-import static android.graphics.Matrix.MSKEW_X;
-import static android.graphics.Matrix.MSKEW_Y;
-
-import static com.android.internal.protolog.ProtoLogGroup.WM_DEBUG_TPL;
-
-import android.graphics.Matrix;
-import android.graphics.Rect;
-import android.graphics.RectF;
-import android.graphics.Region;
-import android.os.Handler;
-import android.os.HandlerThread;
-import android.os.IBinder;
-import android.os.RemoteException;
-import android.util.ArrayMap;
-import android.util.ArraySet;
-import android.util.IntArray;
-import android.util.Pair;
-import android.util.Size;
-import android.view.InputWindowHandle;
-import android.window.ITrustedPresentationListener;
-import android.window.TrustedPresentationThresholds;
-import android.window.WindowInfosListener;
-
-import com.android.internal.protolog.common.ProtoLog;
-import com.android.server.wm.utils.RegionUtils;
-
-import java.io.PrintWriter;
-import java.util.ArrayList;
-import java.util.Optional;
-
-/**
- * Class to handle TrustedPresentationListener registrations in a thread safe manner. This class
- * also takes care of cleaning up listeners when the remote process dies.
- */
-public class TrustedPresentationListenerController {
-
-    // Should only be accessed by the posting to the handler
-    private class Listeners {
-        private final class ListenerDeathRecipient implements IBinder.DeathRecipient {
-            IBinder mListenerBinder;
-            int mInstances;
-
-            ListenerDeathRecipient(IBinder listenerBinder) {
-                mListenerBinder = listenerBinder;
-                mInstances = 0;
-                try {
-                    mListenerBinder.linkToDeath(this, 0);
-                } catch (RemoteException ignore) {
-                }
-            }
-
-            void addInstance() {
-                mInstances++;
-            }
-
-            // return true if there are no instances alive
-            boolean removeInstance() {
-                mInstances--;
-                if (mInstances > 0) {
-                    return false;
-                }
-                mListenerBinder.unlinkToDeath(this, 0);
-                return true;
-            }
-
-            public void binderDied() {
-                mHandler.post(() -> {
-                    mUniqueListeners.remove(mListenerBinder);
-                    removeListeners(mListenerBinder, Optional.empty());
-                });
-            }
-        }
-
-        // tracks binder deaths for cleanup
-        ArrayMap<IBinder, ListenerDeathRecipient> mUniqueListeners = new ArrayMap<>();
-        ArrayMap<IBinder /*window*/, ArrayList<TrustedPresentationInfo>> mWindowToListeners =
-                new ArrayMap<>();
-
-        void register(IBinder window, ITrustedPresentationListener listener,
-                TrustedPresentationThresholds thresholds, int id) {
-            var listenersForWindow = mWindowToListeners.computeIfAbsent(window,
-                    iBinder -> new ArrayList<>());
-            listenersForWindow.add(new TrustedPresentationInfo(thresholds, id, listener));
-
-            // register death listener
-            var listenerBinder = listener.asBinder();
-            var deathRecipient = mUniqueListeners.computeIfAbsent(listenerBinder,
-                    ListenerDeathRecipient::new);
-            deathRecipient.addInstance();
-        }
-
-        void unregister(ITrustedPresentationListener trustedPresentationListener, int id) {
-            var listenerBinder = trustedPresentationListener.asBinder();
-            var deathRecipient = mUniqueListeners.get(listenerBinder);
-            if (deathRecipient == null) {
-                ProtoLog.e(WM_DEBUG_TPL, "unregister failed, couldn't find"
-                        + " deathRecipient for %s with id=%d", trustedPresentationListener, id);
-                return;
-            }
-
-            if (deathRecipient.removeInstance()) {
-                mUniqueListeners.remove(listenerBinder);
-            }
-            removeListeners(listenerBinder, Optional.of(id));
-        }
-
-        boolean isEmpty() {
-            return mWindowToListeners.isEmpty();
-        }
-
-        ArrayList<TrustedPresentationInfo> get(IBinder windowToken) {
-            return mWindowToListeners.get(windowToken);
-        }
-
-        private void removeListeners(IBinder listenerBinder, Optional<Integer> id) {
-            for (int i = mWindowToListeners.size() - 1; i >= 0; i--) {
-                var listeners = mWindowToListeners.valueAt(i);
-                for (int j = listeners.size() - 1; j >= 0; j--) {
-                    var listener = listeners.get(j);
-                    if (listener.mListener.asBinder() == listenerBinder && (id.isEmpty()
-                            || listener.mId == id.get())) {
-                        listeners.remove(j);
-                    }
-                }
-                if (listeners.isEmpty()) {
-                    mWindowToListeners.removeAt(i);
-                }
-            }
-        }
-    }
-
-    private final Object mHandlerThreadLock = new Object();
-    private HandlerThread mHandlerThread;
-    private Handler mHandler;
-
-    private WindowInfosListener mWindowInfosListener;
-
-    Listeners mRegisteredListeners = new Listeners();
-
-    private InputWindowHandle[] mLastWindowHandles;
-
-    private final Object mIgnoredWindowTokensLock = new Object();
-
-    private final ArraySet<IBinder> mIgnoredWindowTokens = new ArraySet<>();
-
-    private void startHandlerThreadIfNeeded() {
-        synchronized (mHandlerThreadLock) {
-            if (mHandler == null) {
-                mHandlerThread = new HandlerThread("WindowInfosListenerForTpl");
-                mHandlerThread.start();
-                mHandler = new Handler(mHandlerThread.getLooper());
-            }
-        }
-    }
-
-    void addIgnoredWindowTokens(IBinder token) {
-        synchronized (mIgnoredWindowTokensLock) {
-            mIgnoredWindowTokens.add(token);
-        }
-    }
-
-    void removeIgnoredWindowTokens(IBinder token) {
-        synchronized (mIgnoredWindowTokensLock) {
-            mIgnoredWindowTokens.remove(token);
-        }
-    }
-
-    void registerListener(IBinder window, ITrustedPresentationListener listener,
-            TrustedPresentationThresholds thresholds, int id) {
-        startHandlerThreadIfNeeded();
-        mHandler.post(() -> {
-            ProtoLog.d(WM_DEBUG_TPL, "Registering listener=%s with id=%d for window=%s with %s",
-                    listener, id, window, thresholds);
-
-            mRegisteredListeners.register(window, listener, thresholds, id);
-            registerWindowInfosListener();
-            // Update the initial state for the new registered listener
-            computeTpl(mLastWindowHandles);
-        });
-    }
-
-    void unregisterListener(ITrustedPresentationListener listener, int id) {
-        startHandlerThreadIfNeeded();
-        mHandler.post(() -> {
-            ProtoLog.d(WM_DEBUG_TPL, "Unregistering listener=%s with id=%d",
-                    listener, id);
-
-            mRegisteredListeners.unregister(listener, id);
-            if (mRegisteredListeners.isEmpty()) {
-                unregisterWindowInfosListener();
-            }
-        });
-    }
-
-    void dump(PrintWriter pw) {
-        final String innerPrefix = "  ";
-        pw.println("TrustedPresentationListenerController:");
-        pw.println(innerPrefix + "Active unique listeners ("
-                + mRegisteredListeners.mUniqueListeners.size() + "):");
-        for (int i = 0; i < mRegisteredListeners.mWindowToListeners.size(); i++) {
-            pw.println(
-                    innerPrefix + "  window=" + mRegisteredListeners.mWindowToListeners.keyAt(i));
-            final var listeners = mRegisteredListeners.mWindowToListeners.valueAt(i);
-            for (int j = 0; j < listeners.size(); j++) {
-                final var listener = listeners.get(j);
-                pw.println(innerPrefix + innerPrefix + "  listener=" + listener.mListener.asBinder()
-                        + " id=" + listener.mId
-                        + " thresholds=" + listener.mThresholds);
-            }
-        }
-    }
-
-    private void registerWindowInfosListener() {
-        if (mWindowInfosListener != null) {
-            return;
-        }
-
-        mWindowInfosListener = new WindowInfosListener() {
-            @Override
-            public void onWindowInfosChanged(InputWindowHandle[] windowHandles,
-                    DisplayInfo[] displayInfos) {
-                mHandler.post(() -> computeTpl(windowHandles));
-            }
-        };
-        mLastWindowHandles = mWindowInfosListener.register().first;
-    }
-
-    private void unregisterWindowInfosListener() {
-        if (mWindowInfosListener == null) {
-            return;
-        }
-
-        mWindowInfosListener.unregister();
-        mWindowInfosListener = null;
-        mLastWindowHandles = null;
-    }
-
-    private void computeTpl(InputWindowHandle[] windowHandles) {
-        mLastWindowHandles = windowHandles;
-        if (mLastWindowHandles == null || mLastWindowHandles.length == 0
-                || mRegisteredListeners.isEmpty()) {
-            return;
-        }
-
-        Rect tmpRect = new Rect();
-        Matrix tmpInverseMatrix = new Matrix();
-        float[] tmpMatrix = new float[9];
-        Region coveredRegionsAbove = new Region();
-        long currTimeMs = System.currentTimeMillis();
-        ProtoLog.v(WM_DEBUG_TPL, "Checking %d windows", mLastWindowHandles.length);
-
-        ArrayMap<ITrustedPresentationListener, Pair<IntArray, IntArray>> listenerUpdates =
-                new ArrayMap<>();
-        ArraySet<IBinder> ignoredWindowTokens;
-        synchronized (mIgnoredWindowTokensLock) {
-            ignoredWindowTokens = new ArraySet<>(mIgnoredWindowTokens);
-        }
-        for (var windowHandle : mLastWindowHandles) {
-            if (ignoredWindowTokens.contains(windowHandle.getWindowToken())) {
-                ProtoLog.v(WM_DEBUG_TPL, "Skipping %s", windowHandle.name);
-                continue;
-            }
-            tmpRect.set(windowHandle.frame);
-            var listeners = mRegisteredListeners.get(windowHandle.getWindowToken());
-            if (listeners != null) {
-                Region region = new Region();
-                region.op(tmpRect, coveredRegionsAbove, Region.Op.DIFFERENCE);
-                windowHandle.transform.invert(tmpInverseMatrix);
-                tmpInverseMatrix.getValues(tmpMatrix);
-                float scaleX = (float) Math.sqrt(tmpMatrix[MSCALE_X] * tmpMatrix[MSCALE_X]
-                        + tmpMatrix[MSKEW_X] * tmpMatrix[MSKEW_X]);
-                float scaleY = (float) Math.sqrt(tmpMatrix[MSCALE_Y] * tmpMatrix[MSCALE_Y]
-                        + tmpMatrix[MSKEW_Y] * tmpMatrix[MSKEW_Y]);
-
-                float fractionRendered = computeFractionRendered(region, new RectF(tmpRect),
-                        windowHandle.contentSize,
-                        scaleX, scaleY);
-
-                checkIfInThreshold(listeners, listenerUpdates, fractionRendered, windowHandle.alpha,
-                        currTimeMs);
-            }
-
-            coveredRegionsAbove.op(tmpRect, Region.Op.UNION);
-            ProtoLog.v(WM_DEBUG_TPL, "coveredRegionsAbove updated with %s frame:%s region:%s",
-                    windowHandle.name, tmpRect.toShortString(), coveredRegionsAbove);
-        }
-
-        for (int i = 0; i < listenerUpdates.size(); i++) {
-            var updates = listenerUpdates.valueAt(i);
-            var listener = listenerUpdates.keyAt(i);
-            try {
-                listener.onTrustedPresentationChanged(updates.first.toArray(),
-                        updates.second.toArray());
-            } catch (RemoteException ignore) {
-            }
-        }
-    }
-
-    private void addListenerUpdate(
-            ArrayMap<ITrustedPresentationListener, Pair<IntArray, IntArray>> listenerUpdates,
-            ITrustedPresentationListener listener, int id, boolean presentationState) {
-        var updates = listenerUpdates.get(listener);
-        if (updates == null) {
-            updates = new Pair<>(new IntArray(), new IntArray());
-            listenerUpdates.put(listener, updates);
-        }
-        if (presentationState) {
-            updates.first.add(id);
-        } else {
-            updates.second.add(id);
-        }
-    }
-
-
-    private void checkIfInThreshold(
-            ArrayList<TrustedPresentationInfo> listeners,
-            ArrayMap<ITrustedPresentationListener, Pair<IntArray, IntArray>> listenerUpdates,
-            float fractionRendered, float alpha, long currTimeMs) {
-        ProtoLog.v(WM_DEBUG_TPL, "checkIfInThreshold fractionRendered=%f alpha=%f currTimeMs=%d",
-                fractionRendered, alpha, currTimeMs);
-        for (int i = 0; i < listeners.size(); i++) {
-            var trustedPresentationInfo = listeners.get(i);
-            var listener = trustedPresentationInfo.mListener;
-            boolean lastState = trustedPresentationInfo.mLastComputedTrustedPresentationState;
-            boolean newState =
-                    (alpha >= trustedPresentationInfo.mThresholds.mMinAlpha) && (fractionRendered
-                            >= trustedPresentationInfo.mThresholds.mMinFractionRendered);
-            trustedPresentationInfo.mLastComputedTrustedPresentationState = newState;
-
-            ProtoLog.v(WM_DEBUG_TPL,
-                    "lastState=%s newState=%s alpha=%f minAlpha=%f fractionRendered=%f "
-                            + "minFractionRendered=%f",
-                    lastState, newState, alpha, trustedPresentationInfo.mThresholds.mMinAlpha,
-                    fractionRendered, trustedPresentationInfo.mThresholds.mMinFractionRendered);
-
-            if (lastState && !newState) {
-                // We were in the trusted presentation state, but now we left it,
-                // emit the callback if needed
-                if (trustedPresentationInfo.mLastReportedTrustedPresentationState) {
-                    trustedPresentationInfo.mLastReportedTrustedPresentationState = false;
-                    addListenerUpdate(listenerUpdates, listener,
-                            trustedPresentationInfo.mId, /*presentationState*/ false);
-                    ProtoLog.d(WM_DEBUG_TPL, "Adding untrusted state listener=%s with id=%d",
-                            listener, trustedPresentationInfo.mId);
-                }
-                // Reset the timer
-                trustedPresentationInfo.mEnteredTrustedPresentationStateTime = -1;
-            } else if (!lastState && newState) {
-                // We were not in the trusted presentation state, but we entered it, begin the timer
-                // and make sure this gets called at least once more!
-                trustedPresentationInfo.mEnteredTrustedPresentationStateTime = currTimeMs;
-                mHandler.postDelayed(() -> {
-                    computeTpl(mLastWindowHandles);
-                }, (long) (trustedPresentationInfo.mThresholds.mStabilityRequirementMs * 1.5));
-            }
-
-            // Has the timer elapsed, but we are still in the state? Emit a callback if needed
-            if (!trustedPresentationInfo.mLastReportedTrustedPresentationState && newState && (
-                    currTimeMs - trustedPresentationInfo.mEnteredTrustedPresentationStateTime
-                            > trustedPresentationInfo.mThresholds.mStabilityRequirementMs)) {
-                trustedPresentationInfo.mLastReportedTrustedPresentationState = true;
-                addListenerUpdate(listenerUpdates, listener,
-                        trustedPresentationInfo.mId, /*presentationState*/ true);
-                ProtoLog.d(WM_DEBUG_TPL, "Adding trusted state listener=%s with id=%d",
-                        listener, trustedPresentationInfo.mId);
-            }
-        }
-    }
-
-    private float computeFractionRendered(Region visibleRegion, RectF screenBounds,
-            Size contentSize,
-            float sx, float sy) {
-        ProtoLog.v(WM_DEBUG_TPL,
-                "computeFractionRendered: visibleRegion=%s screenBounds=%s contentSize=%s "
-                        + "scale=%f,%f",
-                visibleRegion, screenBounds, contentSize, sx, sy);
-
-        if (contentSize.getWidth() == 0 || contentSize.getHeight() == 0) {
-            return -1;
-        }
-        if (screenBounds.width() == 0 || screenBounds.height() == 0) {
-            return -1;
-        }
-
-        float fractionRendered = Math.min(sx * sy, 1.0f);
-        ProtoLog.v(WM_DEBUG_TPL, "fractionRendered scale=%f", fractionRendered);
-
-        float boundsOverSourceW = screenBounds.width() / (float) contentSize.getWidth();
-        float boundsOverSourceH = screenBounds.height() / (float) contentSize.getHeight();
-        fractionRendered *= boundsOverSourceW * boundsOverSourceH;
-        ProtoLog.v(WM_DEBUG_TPL, "fractionRendered boundsOverSource=%f", fractionRendered);
-        // Compute the size of all the rects since they may be disconnected.
-        float[] visibleSize = new float[1];
-        RegionUtils.forEachRect(visibleRegion, rect -> {
-            float size = rect.width() * rect.height();
-            visibleSize[0] += size;
-        });
-
-        fractionRendered *= visibleSize[0] / (screenBounds.width() * screenBounds.height());
-        return fractionRendered;
-    }
-
-    private static class TrustedPresentationInfo {
-        boolean mLastComputedTrustedPresentationState = false;
-        boolean mLastReportedTrustedPresentationState = false;
-        long mEnteredTrustedPresentationStateTime = -1;
-        final TrustedPresentationThresholds mThresholds;
-
-        final ITrustedPresentationListener mListener;
-        final int mId;
-
-        private TrustedPresentationInfo(TrustedPresentationThresholds thresholds, int id,
-                ITrustedPresentationListener listener) {
-            mThresholds = thresholds;
-            mId = id;
-            mListener = listener;
-            checkValid(thresholds);
-        }
-
-        private void checkValid(TrustedPresentationThresholds thresholds) {
-            if (thresholds.mMinAlpha <= 0 || thresholds.mMinFractionRendered <= 0
-                    || thresholds.mStabilityRequirementMs < 1) {
-                throw new IllegalArgumentException(
-                        "TrustedPresentationThresholds values are invalid");
-            }
-        }
-    }
-}
diff --git a/services/core/java/com/android/server/wm/WindowManagerService.java b/services/core/java/com/android/server/wm/WindowManagerService.java
index 0c57036..0d2c94d 100644
--- a/services/core/java/com/android/server/wm/WindowManagerService.java
+++ b/services/core/java/com/android/server/wm/WindowManagerService.java
@@ -303,11 +303,9 @@
 import android.window.ClientWindowFrames;
 import android.window.ISurfaceSyncGroupCompletedListener;
 import android.window.ITaskFpsCallback;
-import android.window.ITrustedPresentationListener;
 import android.window.ScreenCapture;
 import android.window.SystemPerformanceHinter;
 import android.window.TaskSnapshot;
-import android.window.TrustedPresentationThresholds;
 import android.window.WindowContainerToken;
 import android.window.WindowContextInfo;
 
@@ -766,9 +764,6 @@
     private final SurfaceSyncGroupController mSurfaceSyncGroupController =
             new SurfaceSyncGroupController();
 
-    final TrustedPresentationListenerController mTrustedPresentationListenerController =
-            new TrustedPresentationListenerController();
-
     @VisibleForTesting
     final class SettingsObserver extends ContentObserver {
         private final Uri mDisplayInversionEnabledUri =
@@ -7176,7 +7171,6 @@
                 pw.println(separator);
             }
             mSystemPerformanceHinter.dump(pw, "");
-            mTrustedPresentationListenerController.dump(pw);
         }
     }
 
@@ -9777,17 +9771,4 @@
             Binder.restoreCallingIdentity(origId);
         }
     }
-
-    @Override
-    public void registerTrustedPresentationListener(IBinder window,
-            ITrustedPresentationListener listener,
-            TrustedPresentationThresholds thresholds, int id) {
-        mTrustedPresentationListenerController.registerListener(window, listener, thresholds, id);
-    }
-
-    @Override
-    public void unregisterTrustedPresentationListener(ITrustedPresentationListener listener,
-            int id) {
-        mTrustedPresentationListenerController.unregisterListener(listener, id);
-    }
 }
diff --git a/services/core/java/com/android/server/wm/WindowState.java b/services/core/java/com/android/server/wm/WindowState.java
index 7bc7e2c..e1f1f66 100644
--- a/services/core/java/com/android/server/wm/WindowState.java
+++ b/services/core/java/com/android/server/wm/WindowState.java
@@ -1189,11 +1189,6 @@
             ProtoLog.v(WM_DEBUG_ADD_REMOVE, "Adding %s to %s", this, parentWindow);
             parentWindow.addChild(this, sWindowSubLayerComparator);
         }
-
-        if (token.mRoundedCornerOverlay) {
-            mWmService.mTrustedPresentationListenerController.addIgnoredWindowTokens(
-                    getWindowToken());
-        }
     }
 
     @Override
@@ -2398,9 +2393,6 @@
         }
 
         mWmService.postWindowRemoveCleanupLocked(this);
-
-        mWmService.mTrustedPresentationListenerController.removeIgnoredWindowTokens(
-                getWindowToken());
     }
 
     @Override
diff --git a/services/robotests/src/com/android/server/media/AudioPoliciesBluetoothRouteControllerTest.java b/services/robotests/src/com/android/server/media/AudioPoliciesBluetoothRouteControllerTest.java
deleted file mode 100644
index 0ad4184..0000000
--- a/services/robotests/src/com/android/server/media/AudioPoliciesBluetoothRouteControllerTest.java
+++ /dev/null
@@ -1,293 +0,0 @@
-/*
- * Copyright (C) 2023 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.media;
-
-import static com.google.common.truth.Truth.assertThat;
-
-import static org.mockito.Mockito.any;
-import static org.mockito.Mockito.eq;
-import static org.mockito.Mockito.when;
-
-import android.app.Application;
-import android.bluetooth.BluetoothA2dp;
-import android.bluetooth.BluetoothAdapter;
-import android.bluetooth.BluetoothDevice;
-import android.bluetooth.BluetoothManager;
-import android.bluetooth.BluetoothProfile;
-import android.content.Context;
-import android.content.Intent;
-import android.media.AudioManager;
-import android.media.MediaRoute2Info;
-import android.os.UserHandle;
-
-import androidx.test.core.app.ApplicationProvider;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.robolectric.RobolectricTestRunner;
-import org.robolectric.Shadows;
-import org.robolectric.shadows.ShadowBluetoothAdapter;
-import org.robolectric.shadows.ShadowBluetoothDevice;
-
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Set;
-
-@RunWith(RobolectricTestRunner.class)
-public class AudioPoliciesBluetoothRouteControllerTest {
-
-    private static final String DEVICE_ADDRESS_UNKNOWN = ":unknown:ip:address:";
-    private static final String DEVICE_ADDRESS_SAMPLE_1 = "30:59:8B:E4:C6:35";
-    private static final String DEVICE_ADDRESS_SAMPLE_2 = "0D:0D:A6:FF:8D:B6";
-    private static final String DEVICE_ADDRESS_SAMPLE_3 = "2D:9B:0C:C2:6F:78";
-    private static final String DEVICE_ADDRESS_SAMPLE_4 = "66:88:F9:2D:A8:1E";
-
-    private Context mContext;
-
-    private ShadowBluetoothAdapter mShadowBluetoothAdapter;
-
-    @Mock
-    private BluetoothRouteController.BluetoothRoutesUpdatedListener mListener;
-
-    @Mock
-    private BluetoothProfileMonitor mBluetoothProfileMonitor;
-
-    private AudioPoliciesBluetoothRouteController mAudioPoliciesBluetoothRouteController;
-
-    @Before
-    public void setUp() {
-        MockitoAnnotations.initMocks(this);
-
-        Application application = ApplicationProvider.getApplicationContext();
-        mContext = application;
-
-        BluetoothManager bluetoothManager = (BluetoothManager)
-                mContext.getSystemService(Context.BLUETOOTH_SERVICE);
-
-        BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
-        mShadowBluetoothAdapter = Shadows.shadowOf(bluetoothAdapter);
-
-        mAudioPoliciesBluetoothRouteController =
-                new AudioPoliciesBluetoothRouteController(mContext, bluetoothAdapter,
-                        mBluetoothProfileMonitor, mListener) {
-                    @Override
-                    boolean isDeviceConnected(BluetoothDevice device) {
-                        return true;
-                    }
-                };
-
-        // Enable A2DP profile.
-        when(mBluetoothProfileMonitor.isProfileSupported(eq(BluetoothProfile.A2DP), any()))
-                .thenReturn(true);
-        mShadowBluetoothAdapter.setProfileConnectionState(BluetoothProfile.A2DP,
-                BluetoothProfile.STATE_CONNECTED);
-
-        mAudioPoliciesBluetoothRouteController.start(UserHandle.of(0));
-    }
-
-    @Test
-    public void getSelectedRoute_noBluetoothRoutesAvailable_returnsNull() {
-        assertThat(mAudioPoliciesBluetoothRouteController.getSelectedRoute()).isNull();
-    }
-
-    @Test
-    public void selectRoute_noBluetoothRoutesAvailable_returnsFalse() {
-        assertThat(mAudioPoliciesBluetoothRouteController
-                .selectRoute(DEVICE_ADDRESS_UNKNOWN)).isFalse();
-    }
-
-    @Test
-    public void selectRoute_noDeviceWithGivenAddress_returnsFalse() {
-        Set<BluetoothDevice> devices = generateFakeBluetoothDevicesSet(
-                DEVICE_ADDRESS_SAMPLE_1, DEVICE_ADDRESS_SAMPLE_3);
-
-        mShadowBluetoothAdapter.setBondedDevices(devices);
-
-        assertThat(mAudioPoliciesBluetoothRouteController
-                .selectRoute(DEVICE_ADDRESS_SAMPLE_2)).isFalse();
-    }
-
-    @Test
-    public void selectRoute_deviceIsInDevicesSet_returnsTrue() {
-        Set<BluetoothDevice> devices = generateFakeBluetoothDevicesSet(
-                DEVICE_ADDRESS_SAMPLE_1, DEVICE_ADDRESS_SAMPLE_2);
-
-        mShadowBluetoothAdapter.setBondedDevices(devices);
-
-        assertThat(mAudioPoliciesBluetoothRouteController
-                .selectRoute(DEVICE_ADDRESS_SAMPLE_1)).isTrue();
-    }
-
-    @Test
-    public void selectRoute_resetSelectedDevice_returnsTrue() {
-        Set<BluetoothDevice> devices = generateFakeBluetoothDevicesSet(
-                DEVICE_ADDRESS_SAMPLE_1, DEVICE_ADDRESS_SAMPLE_2);
-
-        mShadowBluetoothAdapter.setBondedDevices(devices);
-
-        mAudioPoliciesBluetoothRouteController.selectRoute(DEVICE_ADDRESS_SAMPLE_1);
-        assertThat(mAudioPoliciesBluetoothRouteController.selectRoute(null)).isTrue();
-    }
-
-    @Test
-    public void selectRoute_noSelectedDevice_returnsTrue() {
-        Set<BluetoothDevice> devices = generateFakeBluetoothDevicesSet(
-                DEVICE_ADDRESS_SAMPLE_1, DEVICE_ADDRESS_SAMPLE_2);
-
-        mShadowBluetoothAdapter.setBondedDevices(devices);
-
-        assertThat(mAudioPoliciesBluetoothRouteController.selectRoute(null)).isTrue();
-    }
-
-    @Test
-    public void getSelectedRoute_updateRouteFailed_returnsNull() {
-        Set<BluetoothDevice> devices = generateFakeBluetoothDevicesSet(
-                DEVICE_ADDRESS_SAMPLE_1, DEVICE_ADDRESS_SAMPLE_2);
-
-        mShadowBluetoothAdapter.setBondedDevices(devices);
-        mAudioPoliciesBluetoothRouteController
-                .selectRoute(DEVICE_ADDRESS_SAMPLE_3);
-
-        assertThat(mAudioPoliciesBluetoothRouteController.getSelectedRoute()).isNull();
-    }
-
-    @Test
-    public void getSelectedRoute_updateRouteSuccessful_returnsUpdateDevice() {
-        Set<BluetoothDevice> devices = generateFakeBluetoothDevicesSet(
-                DEVICE_ADDRESS_SAMPLE_1, DEVICE_ADDRESS_SAMPLE_2, DEVICE_ADDRESS_SAMPLE_4);
-
-        assertThat(mAudioPoliciesBluetoothRouteController.getSelectedRoute()).isNull();
-
-        mShadowBluetoothAdapter.setBondedDevices(devices);
-
-        assertThat(mAudioPoliciesBluetoothRouteController
-                .selectRoute(DEVICE_ADDRESS_SAMPLE_4)).isTrue();
-
-        MediaRoute2Info selectedRoute = mAudioPoliciesBluetoothRouteController.getSelectedRoute();
-        assertThat(selectedRoute.getAddress()).isEqualTo(DEVICE_ADDRESS_SAMPLE_4);
-    }
-
-    @Test
-    public void getSelectedRoute_resetSelectedRoute_returnsNull() {
-        Set<BluetoothDevice> devices = generateFakeBluetoothDevicesSet(
-                DEVICE_ADDRESS_SAMPLE_1, DEVICE_ADDRESS_SAMPLE_2, DEVICE_ADDRESS_SAMPLE_4);
-
-        mShadowBluetoothAdapter.setBondedDevices(devices);
-
-        // Device is not null now.
-        mAudioPoliciesBluetoothRouteController.selectRoute(DEVICE_ADDRESS_SAMPLE_4);
-        // Rest the device.
-        mAudioPoliciesBluetoothRouteController.selectRoute(null);
-
-        assertThat(mAudioPoliciesBluetoothRouteController.getSelectedRoute())
-                .isNull();
-    }
-
-    @Test
-    public void getTransferableRoutes_noSelectedRoute_returnsAllBluetoothDevices() {
-        String[] addresses = new String[] { DEVICE_ADDRESS_SAMPLE_1,
-                DEVICE_ADDRESS_SAMPLE_2, DEVICE_ADDRESS_SAMPLE_4 };
-        Set<BluetoothDevice> devices = generateFakeBluetoothDevicesSet(addresses);
-        mShadowBluetoothAdapter.setBondedDevices(devices);
-
-        // Force route controller to update bluetooth devices list.
-        sendBluetoothDevicesChangedBroadcast();
-
-        Set<String> transferableDevices = extractAddressesListFrom(
-                mAudioPoliciesBluetoothRouteController.getTransferableRoutes());
-        assertThat(transferableDevices).containsExactlyElementsIn(addresses);
-    }
-
-    @Test
-    public void getTransferableRoutes_hasSelectedRoute_returnsRoutesWithoutSelectedDevice() {
-        String[] addresses = new String[] { DEVICE_ADDRESS_SAMPLE_1,
-                DEVICE_ADDRESS_SAMPLE_2, DEVICE_ADDRESS_SAMPLE_4 };
-        Set<BluetoothDevice> devices = generateFakeBluetoothDevicesSet(addresses);
-        mShadowBluetoothAdapter.setBondedDevices(devices);
-
-        // Force route controller to update bluetooth devices list.
-        sendBluetoothDevicesChangedBroadcast();
-        mAudioPoliciesBluetoothRouteController.selectRoute(DEVICE_ADDRESS_SAMPLE_4);
-
-        Set<String> transferableDevices = extractAddressesListFrom(
-                mAudioPoliciesBluetoothRouteController.getTransferableRoutes());
-        assertThat(transferableDevices).containsExactly(DEVICE_ADDRESS_SAMPLE_1,
-                DEVICE_ADDRESS_SAMPLE_2);
-    }
-
-    @Test
-    public void getAllBluetoothRoutes_hasSelectedRoute_returnsAllRoutes() {
-        String[] addresses = new String[] { DEVICE_ADDRESS_SAMPLE_1,
-                DEVICE_ADDRESS_SAMPLE_2, DEVICE_ADDRESS_SAMPLE_4 };
-        Set<BluetoothDevice> devices = generateFakeBluetoothDevicesSet(addresses);
-        mShadowBluetoothAdapter.setBondedDevices(devices);
-
-        // Force route controller to update bluetooth devices list.
-        sendBluetoothDevicesChangedBroadcast();
-        mAudioPoliciesBluetoothRouteController.selectRoute(DEVICE_ADDRESS_SAMPLE_4);
-
-        Set<String> bluetoothDevices = extractAddressesListFrom(
-                mAudioPoliciesBluetoothRouteController.getAllBluetoothRoutes());
-        assertThat(bluetoothDevices).containsExactlyElementsIn(addresses);
-    }
-
-    @Test
-    public void updateVolumeForDevice_setVolumeForA2DPTo25_selectedRouteVolumeIsUpdated() {
-        String[] addresses = new String[] { DEVICE_ADDRESS_SAMPLE_1,
-                DEVICE_ADDRESS_SAMPLE_2, DEVICE_ADDRESS_SAMPLE_4 };
-        Set<BluetoothDevice> devices = generateFakeBluetoothDevicesSet(addresses);
-        mShadowBluetoothAdapter.setBondedDevices(devices);
-
-        // Force route controller to update bluetooth devices list.
-        sendBluetoothDevicesChangedBroadcast();
-        mAudioPoliciesBluetoothRouteController.selectRoute(DEVICE_ADDRESS_SAMPLE_4);
-
-        mAudioPoliciesBluetoothRouteController.updateVolumeForDevices(
-                AudioManager.DEVICE_OUT_BLUETOOTH_A2DP, 25);
-
-        MediaRoute2Info selectedRoute = mAudioPoliciesBluetoothRouteController.getSelectedRoute();
-        assertThat(selectedRoute.getVolume()).isEqualTo(25);
-    }
-
-    private void sendBluetoothDevicesChangedBroadcast() {
-        Intent intent = new Intent(BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED);
-        mContext.sendBroadcast(intent);
-    }
-
-    private static Set<String> extractAddressesListFrom(Collection<MediaRoute2Info> routes) {
-        Set<String> addresses = new HashSet<>();
-
-        for (MediaRoute2Info route: routes) {
-            addresses.add(route.getAddress());
-        }
-
-        return addresses;
-    }
-
-    private static Set<BluetoothDevice> generateFakeBluetoothDevicesSet(String... addresses) {
-        Set<BluetoothDevice> devices = new HashSet<>();
-
-        for (String address: addresses) {
-            devices.add(ShadowBluetoothDevice.newInstance(address));
-        }
-
-        return devices;
-    }
-}
diff --git a/services/tests/PackageManagerServiceTests/server/src/com/android/server/pm/PackageUserStateTest.java b/services/tests/PackageManagerServiceTests/server/src/com/android/server/pm/PackageUserStateTest.java
index 87a297b..c0c7032 100644
--- a/services/tests/PackageManagerServiceTests/server/src/com/android/server/pm/PackageUserStateTest.java
+++ b/services/tests/PackageManagerServiceTests/server/src/com/android/server/pm/PackageUserStateTest.java
@@ -404,6 +404,7 @@
 
     @Test
     public void archiveState() {
+        final long currentTimeMillis = System.currentTimeMillis();
         PackageUserStateImpl packageUserState = new PackageUserStateImpl();
         ArchiveState.ArchiveActivityInfo archiveActivityInfo =
                 new ArchiveState.ArchiveActivityInfo(
@@ -415,5 +416,23 @@
                 "installerTitle");
         packageUserState.setArchiveState(archiveState);
         assertEquals(archiveState, packageUserState.getArchiveState());
+        assertTrue(archiveState.getArchiveTimeMillis() > currentTimeMillis);
+    }
+
+    @Test
+    public void archiveStateWithTimestamp() {
+        final long currentTimeMillis = System.currentTimeMillis();
+        PackageUserStateImpl packageUserState = new PackageUserStateImpl();
+        ArchiveState.ArchiveActivityInfo archiveActivityInfo =
+                new ArchiveState.ArchiveActivityInfo(
+                        "appTitle",
+                        new ComponentName("pkg", "class"),
+                        Path.of("/path1"),
+                        Path.of("/path2"));
+        ArchiveState archiveState = new ArchiveState(List.of(archiveActivityInfo),
+                "installerTitle", currentTimeMillis);
+        packageUserState.setArchiveState(archiveState);
+        assertEquals(archiveState, packageUserState.getArchiveState());
+        assertEquals(archiveState.getArchiveTimeMillis(), currentTimeMillis);
     }
 }
diff --git a/services/tests/mockingservicestests/src/com/android/server/job/controllers/BackgroundJobsControllerTest.java b/services/tests/mockingservicestests/src/com/android/server/job/controllers/BackgroundJobsControllerTest.java
new file mode 100644
index 0000000..cdae8c6
--- /dev/null
+++ b/services/tests/mockingservicestests/src/com/android/server/job/controllers/BackgroundJobsControllerTest.java
@@ -0,0 +1,291 @@
+/*
+ * Copyright (C) 2018 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.job.controllers;
+
+import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
+import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock;
+import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession;
+import static com.android.dx.mockito.inline.extended.ExtendedMockito.when;
+import static com.android.server.job.JobSchedulerService.FREQUENT_INDEX;
+import static com.android.server.job.controllers.JobStatus.CONSTRAINT_BACKGROUND_NOT_RESTRICTED;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+
+import android.app.ActivityManagerInternal;
+import android.app.AppGlobals;
+import android.app.job.JobInfo;
+import android.content.BroadcastReceiver;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.pm.IPackageManager;
+import android.content.pm.PackageManager;
+import android.content.pm.PackageManagerInternal;
+import android.net.Uri;
+import android.os.UserHandle;
+import android.platform.test.flag.junit.SetFlagsRule;
+import android.util.ArraySet;
+
+import androidx.test.runner.AndroidJUnit4;
+
+import com.android.server.AppStateTracker;
+import com.android.server.AppStateTrackerImpl;
+import com.android.server.LocalServices;
+import com.android.server.job.JobSchedulerService;
+import com.android.server.job.JobStore;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.ArgumentMatchers;
+import org.mockito.Mock;
+import org.mockito.MockitoSession;
+import org.mockito.quality.Strictness;
+
+@RunWith(AndroidJUnit4.class)
+public class BackgroundJobsControllerTest {
+    private static final int CALLING_UID = 1000;
+    private static final String CALLING_PACKAGE = "com.test.calling.package";
+    private static final String SOURCE_PACKAGE = "com.android.frameworks.mockingservicestests";
+    private static final int SOURCE_UID = 10001;
+    private static final int ALTERNATE_UID = 12345;
+    private static final String ALTERNATE_SOURCE_PACKAGE = "com.test.alternate.package";
+    private static final int SOURCE_USER_ID = 0;
+
+    private BackgroundJobsController mBackgroundJobsController;
+    private BroadcastReceiver mStoppedReceiver;
+    private JobStore mJobStore;
+
+    private MockitoSession mMockingSession;
+    @Mock
+    private Context mContext;
+    @Mock
+    private AppStateTrackerImpl mAppStateTrackerImpl;
+    @Mock
+    private IPackageManager mIPackageManager;
+    @Mock
+    private JobSchedulerService mJobSchedulerService;
+    @Mock
+    private PackageManagerInternal mPackageManagerInternal;
+    @Mock
+    private PackageManager mPackageManager;
+
+    @Rule
+    public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
+
+    @Before
+    public void setUp() throws Exception {
+        mMockingSession = mockitoSession()
+                .initMocks(this)
+                .mockStatic(AppGlobals.class)
+                .mockStatic(LocalServices.class)
+                .strictness(Strictness.LENIENT)
+                .startMocking();
+
+        // Called in StateController constructor.
+        when(mJobSchedulerService.getTestableContext()).thenReturn(mContext);
+        when(mJobSchedulerService.getLock()).thenReturn(mJobSchedulerService);
+        // Called in BackgroundJobsController constructor.
+        doReturn(mock(ActivityManagerInternal.class))
+                .when(() -> LocalServices.getService(ActivityManagerInternal.class));
+        doReturn(mAppStateTrackerImpl)
+                .when(() -> LocalServices.getService(AppStateTracker.class));
+        doReturn(mPackageManagerInternal)
+                .when(() -> LocalServices.getService(PackageManagerInternal.class));
+        mJobStore = JobStore.initAndGetForTesting(mContext, mContext.getFilesDir());
+        when(mJobSchedulerService.getJobStore()).thenReturn(mJobStore);
+        // Called in JobStatus constructor.
+        doReturn(mIPackageManager).when(AppGlobals::getPackageManager);
+
+        doReturn(false).when(mAppStateTrackerImpl)
+                .areJobsRestricted(anyInt(), anyString(), anyBoolean());
+        doReturn(true).when(mAppStateTrackerImpl)
+                .isRunAnyInBackgroundAppOpsAllowed(anyInt(), anyString());
+
+        // Initialize real objects.
+        // Capture the listeners.
+        ArgumentCaptor<BroadcastReceiver> receiverCaptor =
+                ArgumentCaptor.forClass(BroadcastReceiver.class);
+
+        when(mContext.getPackageManager()).thenReturn(mPackageManager);
+        mBackgroundJobsController = new BackgroundJobsController(mJobSchedulerService);
+        mBackgroundJobsController.startTrackingLocked();
+
+        verify(mContext).registerReceiverAsUser(receiverCaptor.capture(), any(),
+                ArgumentMatchers.argThat(filter ->
+                        filter.hasAction(Intent.ACTION_PACKAGE_RESTARTED)
+                                && filter.hasAction(Intent.ACTION_PACKAGE_UNSTOPPED)),
+                any(), any());
+        mStoppedReceiver = receiverCaptor.getValue();
+
+        // Need to do this since we're using a mock JS and not a real object.
+        doReturn(new ArraySet<>(new String[]{SOURCE_PACKAGE}))
+                .when(mJobSchedulerService).getPackagesForUidLocked(SOURCE_UID);
+        doReturn(new ArraySet<>(new String[]{ALTERNATE_SOURCE_PACKAGE}))
+                .when(mJobSchedulerService).getPackagesForUidLocked(ALTERNATE_UID);
+        setPackageUid(ALTERNATE_UID, ALTERNATE_SOURCE_PACKAGE);
+        setPackageUid(SOURCE_UID, SOURCE_PACKAGE);
+    }
+
+    @After
+    public void tearDown() {
+        if (mMockingSession != null) {
+            mMockingSession.finishMocking();
+        }
+    }
+
+    private void setPackageUid(final int uid, final String pkgName) throws Exception {
+        doReturn(uid).when(mIPackageManager)
+                .getPackageUid(eq(pkgName), anyLong(), eq(UserHandle.getUserId(uid)));
+    }
+
+    private void setStoppedState(int uid, String pkgName, boolean stopped) {
+        Intent intent = new Intent(
+                stopped ? Intent.ACTION_PACKAGE_RESTARTED : Intent.ACTION_PACKAGE_UNSTOPPED);
+        intent.putExtra(Intent.EXTRA_UID, uid);
+        intent.setData(Uri.fromParts(IntentFilter.SCHEME_PACKAGE, pkgName, null));
+        mStoppedReceiver.onReceive(mContext, intent);
+    }
+
+    private void setUidBias(int uid, int bias) {
+        int prevBias = mJobSchedulerService.getUidBias(uid);
+        doReturn(bias).when(mJobSchedulerService).getUidBias(uid);
+        synchronized (mBackgroundJobsController.mLock) {
+            mBackgroundJobsController.onUidBiasChangedLocked(uid, prevBias, bias);
+        }
+    }
+
+    private void trackJobs(JobStatus... jobs) {
+        for (JobStatus job : jobs) {
+            mJobStore.add(job);
+            synchronized (mBackgroundJobsController.mLock) {
+                mBackgroundJobsController.maybeStartTrackingJobLocked(job, null);
+            }
+        }
+    }
+
+    private JobInfo.Builder createBaseJobInfoBuilder(String pkgName, int jobId) {
+        final ComponentName cn = spy(new ComponentName(pkgName, "TestBJCJobService"));
+        doReturn("TestBJCJobService").when(cn).flattenToShortString();
+        return new JobInfo.Builder(jobId, cn);
+    }
+
+    private JobStatus createJobStatus(String testTag, String packageName, int callingUid,
+            JobInfo jobInfo) {
+        JobStatus js = JobStatus.createFromJobInfo(
+                jobInfo, callingUid, packageName, SOURCE_USER_ID, "BJCTest", testTag);
+        js.serviceProcessName = "testProcess";
+        // Make sure tests aren't passing just because the default bucket is likely ACTIVE.
+        js.setStandbyBucket(FREQUENT_INDEX);
+        return js;
+    }
+
+    @Test
+    public void testStopped_disabled() {
+        mSetFlagsRule.disableFlags(android.content.pm.Flags.FLAG_STAY_STOPPED);
+        // Scheduled by SOURCE_UID:SOURCE_PACKAGE for itself.
+        JobStatus directJob1 = createJobStatus("testStopped", SOURCE_PACKAGE, SOURCE_UID,
+                createBaseJobInfoBuilder(SOURCE_PACKAGE, 1).build());
+        // Scheduled by ALTERNATE_UID:ALTERNATE_SOURCE_PACKAGE for itself.
+        JobStatus directJob2 = createJobStatus("testStopped",
+                ALTERNATE_SOURCE_PACKAGE, ALTERNATE_UID,
+                createBaseJobInfoBuilder(ALTERNATE_SOURCE_PACKAGE, 2).build());
+        // Scheduled by CALLING_PACKAGE for SOURCE_PACKAGE.
+        JobStatus proxyJob1 = createJobStatus("testStopped", SOURCE_PACKAGE, CALLING_UID,
+                createBaseJobInfoBuilder(CALLING_PACKAGE, 3).build());
+        // Scheduled by CALLING_PACKAGE for ALTERNATE_SOURCE_PACKAGE.
+        JobStatus proxyJob2 = createJobStatus("testStopped",
+                ALTERNATE_SOURCE_PACKAGE, CALLING_UID,
+                createBaseJobInfoBuilder(CALLING_PACKAGE, 4).build());
+
+        trackJobs(directJob1, directJob2, proxyJob1, proxyJob2);
+
+        setStoppedState(ALTERNATE_UID, ALTERNATE_SOURCE_PACKAGE, true);
+        assertTrue(directJob1.isConstraintSatisfied(CONSTRAINT_BACKGROUND_NOT_RESTRICTED));
+        assertFalse(directJob1.isUserBgRestricted());
+        assertTrue(directJob2.isConstraintSatisfied(CONSTRAINT_BACKGROUND_NOT_RESTRICTED));
+        assertFalse(directJob2.isUserBgRestricted());
+        assertTrue(proxyJob1.isConstraintSatisfied(CONSTRAINT_BACKGROUND_NOT_RESTRICTED));
+        assertFalse(proxyJob1.isUserBgRestricted());
+        assertTrue(proxyJob2.isConstraintSatisfied(CONSTRAINT_BACKGROUND_NOT_RESTRICTED));
+        assertFalse(proxyJob2.isUserBgRestricted());
+
+        setStoppedState(ALTERNATE_UID, ALTERNATE_SOURCE_PACKAGE, false);
+        assertTrue(directJob1.isConstraintSatisfied(CONSTRAINT_BACKGROUND_NOT_RESTRICTED));
+        assertFalse(directJob1.isUserBgRestricted());
+        assertTrue(directJob2.isConstraintSatisfied(CONSTRAINT_BACKGROUND_NOT_RESTRICTED));
+        assertFalse(directJob2.isUserBgRestricted());
+        assertTrue(proxyJob1.isConstraintSatisfied(CONSTRAINT_BACKGROUND_NOT_RESTRICTED));
+        assertFalse(proxyJob1.isUserBgRestricted());
+        assertTrue(proxyJob2.isConstraintSatisfied(CONSTRAINT_BACKGROUND_NOT_RESTRICTED));
+        assertFalse(proxyJob2.isUserBgRestricted());
+    }
+
+    @Test
+    public void testStopped_enabled() {
+        mSetFlagsRule.enableFlags(android.content.pm.Flags.FLAG_STAY_STOPPED);
+        // Scheduled by SOURCE_UID:SOURCE_PACKAGE for itself.
+        JobStatus directJob1 = createJobStatus("testStopped", SOURCE_PACKAGE, SOURCE_UID,
+                createBaseJobInfoBuilder(SOURCE_PACKAGE, 1).build());
+        // Scheduled by ALTERNATE_UID:ALTERNATE_SOURCE_PACKAGE for itself.
+        JobStatus directJob2 = createJobStatus("testStopped",
+                ALTERNATE_SOURCE_PACKAGE, ALTERNATE_UID,
+                createBaseJobInfoBuilder(ALTERNATE_SOURCE_PACKAGE, 2).build());
+        // Scheduled by CALLING_PACKAGE for SOURCE_PACKAGE.
+        JobStatus proxyJob1 = createJobStatus("testStopped", SOURCE_PACKAGE, CALLING_UID,
+                createBaseJobInfoBuilder(CALLING_PACKAGE, 3).build());
+        // Scheduled by CALLING_PACKAGE for ALTERNATE_SOURCE_PACKAGE.
+        JobStatus proxyJob2 = createJobStatus("testStopped",
+                ALTERNATE_SOURCE_PACKAGE, CALLING_UID,
+                createBaseJobInfoBuilder(CALLING_PACKAGE, 4).build());
+
+        trackJobs(directJob1, directJob2, proxyJob1, proxyJob2);
+
+        setStoppedState(ALTERNATE_UID, ALTERNATE_SOURCE_PACKAGE, true);
+        assertTrue(directJob1.isConstraintSatisfied(CONSTRAINT_BACKGROUND_NOT_RESTRICTED));
+        assertFalse(directJob1.isUserBgRestricted());
+        assertFalse(directJob2.isConstraintSatisfied(CONSTRAINT_BACKGROUND_NOT_RESTRICTED));
+        assertTrue(directJob2.isUserBgRestricted());
+        assertTrue(proxyJob1.isConstraintSatisfied(CONSTRAINT_BACKGROUND_NOT_RESTRICTED));
+        assertFalse(proxyJob1.isUserBgRestricted());
+        assertFalse(proxyJob2.isConstraintSatisfied(CONSTRAINT_BACKGROUND_NOT_RESTRICTED));
+        assertTrue(proxyJob2.isUserBgRestricted());
+
+        setStoppedState(ALTERNATE_UID, ALTERNATE_SOURCE_PACKAGE, false);
+        assertTrue(directJob1.isConstraintSatisfied(CONSTRAINT_BACKGROUND_NOT_RESTRICTED));
+        assertFalse(directJob1.isUserBgRestricted());
+        assertTrue(directJob2.isConstraintSatisfied(CONSTRAINT_BACKGROUND_NOT_RESTRICTED));
+        assertFalse(directJob2.isUserBgRestricted());
+        assertTrue(proxyJob1.isConstraintSatisfied(CONSTRAINT_BACKGROUND_NOT_RESTRICTED));
+        assertFalse(proxyJob1.isUserBgRestricted());
+        assertTrue(proxyJob2.isConstraintSatisfied(CONSTRAINT_BACKGROUND_NOT_RESTRICTED));
+        assertFalse(proxyJob2.isUserBgRestricted());
+    }
+}
diff --git a/services/tests/mockingservicestests/src/com/android/server/pm/PackageArchiverTest.java b/services/tests/mockingservicestests/src/com/android/server/pm/PackageArchiverTest.java
index 3e73aa3..2332988 100644
--- a/services/tests/mockingservicestests/src/com/android/server/pm/PackageArchiverTest.java
+++ b/services/tests/mockingservicestests/src/com/android/server/pm/PackageArchiverTest.java
@@ -366,9 +366,17 @@
                 eq(new VersionedPackage(PACKAGE, PackageManager.VERSION_CODE_HIGHEST)),
                 eq(CALLER_PACKAGE), eq(DELETE_ARCHIVE | DELETE_KEEP_DATA), eq(mIntentSender),
                 eq(UserHandle.CURRENT.getIdentifier()), anyInt());
-        assertThat(mPackageSetting.readUserState(
-                UserHandle.CURRENT.getIdentifier()).getArchiveState()).isEqualTo(
-                createArchiveState());
+
+        ArchiveState expectedArchiveState = createArchiveState();
+        ArchiveState actualArchiveState = mPackageSetting.readUserState(
+                UserHandle.CURRENT.getIdentifier()).getArchiveState();
+        assertThat(actualArchiveState.getActivityInfos())
+                .isEqualTo(expectedArchiveState.getActivityInfos());
+        assertThat(actualArchiveState.getInstallerTitle())
+                .isEqualTo(expectedArchiveState.getInstallerTitle());
+        // The timestamps are expected to be different
+        assertThat(actualArchiveState.getArchiveTimeMillis())
+                .isNotEqualTo(expectedArchiveState.getArchiveTimeMillis());
     }
 
     @Test
@@ -383,9 +391,17 @@
                 eq(DELETE_ARCHIVE | DELETE_KEEP_DATA | PackageManager.DELETE_SHOW_DIALOG),
                 eq(mIntentSender),
                 eq(UserHandle.CURRENT.getIdentifier()), anyInt());
-        assertThat(mPackageSetting.readUserState(
-                UserHandle.CURRENT.getIdentifier()).getArchiveState()).isEqualTo(
-                createArchiveState());
+
+        ArchiveState expectedArchiveState = createArchiveState();
+        ArchiveState actualArchiveState = mPackageSetting.readUserState(
+                UserHandle.CURRENT.getIdentifier()).getArchiveState();
+        assertThat(actualArchiveState.getActivityInfos())
+                .isEqualTo(expectedArchiveState.getActivityInfos());
+        assertThat(actualArchiveState.getInstallerTitle())
+                .isEqualTo(expectedArchiveState.getInstallerTitle());
+        // The timestamps are expected to be different
+        assertThat(actualArchiveState.getArchiveTimeMillis())
+                .isNotEqualTo(expectedArchiveState.getArchiveTimeMillis());
     }
 
     @Test
diff --git a/services/tests/servicestests/src/com/android/server/am/AnrTimerTest.java b/services/tests/servicestests/src/com/android/server/am/AnrTimerTest.java
deleted file mode 100644
index 44d6760..0000000
--- a/services/tests/servicestests/src/com/android/server/am/AnrTimerTest.java
+++ /dev/null
@@ -1,389 +0,0 @@
-/*
- * Copyright (C) 2023 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.am;
-
-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.junit.Assert.fail;
-
-import android.platform.test.annotations.Presubmit;
-
-import android.os.Handler;
-import android.os.Looper;
-import android.os.Message;
-import android.os.SystemClock;
-
-import android.util.Log;
-
-import androidx.test.filters.SmallTest;
-
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-
-import java.util.ArrayList;
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.TimeUnit;
-
-/**
- * Build/Install/Run:
- *  atest FrameworksServicesTests:AnrTimerTest
- */
-@SmallTest
-@Presubmit
-public class AnrTimerTest {
-
-    /**
-     * A handler that allows control over when to dispatch messages and callbacks. Because most
-     * Handler methods are final, the only thing this handler can intercept is sending messages.
-     * This handler allows unit tests to be written without a need to sleep (which leads to flaky
-     * tests).
-     *
-     * This code was cloned from {@link com.android.systemui.utils.os.FakeHandler}.
-     */
-    static class TestHandler extends Handler {
-
-        private boolean mImmediate = true;
-        private ArrayList<Message> mQueuedMessages = new ArrayList<>();
-
-        ArrayList<Long> mDelays = new ArrayList<>();
-
-        TestHandler(Looper looper, Callback callback, boolean immediate) {
-            super(looper, callback);
-            mImmediate = immediate;
-        }
-
-        TestHandler(Looper looper, Callback callback) {
-            this(looper, callback, true);
-        }
-
-        /**
-         * Override sendMessageAtTime.  In immediate mode, the message is immediately dispatched.
-         * In non-immediate mode, the message is enqueued to the real handler.  In both cases, the
-         * original delay is computed by comparing the target dispatch time with 'now'.  This
-         * computation is prone to errors if the code experiences delays.  The computed time is
-         * captured in the mDelays list.
-         */
-        @Override
-        public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
-            long delay = uptimeMillis - SystemClock.uptimeMillis();
-            mDelays.add(delay);
-            if (mImmediate) {
-                mQueuedMessages.add(msg);
-                dispatchQueuedMessages();
-            } else {
-                super.sendMessageAtTime(msg, uptimeMillis);
-            }
-            return true;
-        }
-
-        void setImmediate(boolean immediate) {
-            mImmediate = immediate;
-        }
-
-        /** Dispatch any messages that have been queued on the calling thread. */
-        void dispatchQueuedMessages() {
-            ArrayList<Message> messages = new ArrayList<>(mQueuedMessages);
-            mQueuedMessages.clear();
-            for (Message msg : messages) {
-                dispatchMessage(msg);
-            }
-        }
-
-        /**
-         * Compare the captured delays with the input array.  The comparison is fuzzy because the
-         * captured delay (see sendMessageAtTime) is affected by process delays.
-         */
-        void verifyDelays(long[] r) {
-            final long FUZZ = 10;
-            assertEquals(r.length, mDelays.size());
-            for (int i = 0; i < mDelays.size(); i++) {
-                long t = r[i];
-                long v = mDelays.get(i);
-                assertTrue(v >= t - FUZZ && v <= t + FUZZ);
-            }
-        }
-    }
-
-    private Handler mHandler;
-    private CountDownLatch mLatch = null;
-    private ArrayList<Message> mMessages;
-
-    // The commonly used message timeout key.
-    private static final int MSG_TIMEOUT = 1;
-
-    @Before
-    public void setUp() {
-        mHandler = new Handler(Looper.getMainLooper(), this::expirationHandler);
-        mMessages = new ArrayList<>();
-        mLatch = new CountDownLatch(1);
-        AnrTimer.resetTimerListForHermeticTest();
-    }
-
-    @After
-    public void tearDown() {
-        mHandler = null;
-        mMessages = null;
-    }
-
-    // When a timer expires, set the expiration time in the message and add it to the queue.
-    private boolean expirationHandler(Message msg) {
-        mMessages.add(Message.obtain(msg));
-        mLatch.countDown();
-        return false;
-    }
-
-    // The test argument includes a pid and uid, and a tag.  The tag is used to distinguish
-    // different message instances.
-    private static class TestArg {
-        final int pid;
-        final int uid;
-        final int tag;
-
-        TestArg(int pid, int uid, int tag) {
-            this.pid = pid;
-            this.uid = uid;
-            this.tag = tag;
-        }
-        @Override
-        public String toString() {
-            return String.format("pid=%d uid=%d tag=%d", pid, uid, tag);
-        }
-    }
-
-    /**
-     * An instrumented AnrTimer.
-     */
-    private class TestAnrTimer extends AnrTimer {
-        // A local copy of 'what'.  The field in AnrTimer is private.
-        final int mWhat;
-
-        TestAnrTimer(Handler h, int key, String tag) {
-            super(h, key, tag);
-            mWhat = key;
-        }
-
-        TestAnrTimer() {
-            this(mHandler, MSG_TIMEOUT, caller());
-        }
-
-        TestAnrTimer(Handler h, int key, String tag, boolean extend, TestInjector injector) {
-            super(h, key, tag, extend, injector);
-            mWhat = key;
-        }
-
-        TestAnrTimer(boolean extend, TestInjector injector) {
-            this(mHandler, MSG_TIMEOUT, caller(), extend, injector);
-        }
-
-        // Return the name of method that called the constructor, assuming that this function is
-        // called from inside the constructor.  The calling method is used to name the AnrTimer
-        // instance so that logs are easier to understand.
-        private static String caller() {
-            final int n = 4;
-            StackTraceElement[] stack = Thread.currentThread().getStackTrace();
-            if (stack.length < n+1) return "test";
-            return stack[n].getMethodName();
-        }
-
-        boolean start(TestArg arg, long millis) {
-            return start(arg, arg.pid, arg.uid, millis);
-        }
-
-        int what() {
-            return mWhat;
-        }
-    }
-
-    private static class TestTracker extends AnrTimer.CpuTracker {
-        long index = 0;
-        final int skip;
-        TestTracker(int skip) {
-            this.skip = skip;
-        }
-        long delay(int pid) {
-            return index++ * skip;
-        }
-    }
-
-    private class TestInjector extends AnrTimer.Injector {
-        final boolean mImmediate;
-        final AnrTimer.CpuTracker mTracker;
-        TestHandler mTestHandler;
-
-        TestInjector(int skip, boolean immediate) {
-            super(mHandler);
-            mTracker = new TestTracker(skip);
-            mImmediate = immediate;
-        }
-
-        TestInjector(int skip) {
-            this(skip, true);
-        }
-
-        @Override
-        Handler newHandler(Handler.Callback callback) {
-            if (mTestHandler == null) {
-                mTestHandler = new TestHandler(mHandler.getLooper(), callback, mImmediate);
-            }
-            return mTestHandler;
-        }
-
-        /** Fetch the allocated handle. This does not check for nulls. */
-        TestHandler getHandler() {
-            return mTestHandler;
-        }
-
-        /**
-         * This override returns the tracker supplied in the constructor.  It does not create a
-         * new one.
-         */
-        @Override
-        AnrTimer.CpuTracker newTracker() {
-            return mTracker;
-        }
-
-        /** For test purposes, always enable the feature. */
-        @Override
-        boolean isFeatureEnabled() {
-            return true;
-        }
-    }
-
-    // Tests
-    // 1. Start a timer and wait for expiration.
-    // 2. Start a timer and cancel it.  Verify no expiration.
-    // 3. Start a timer.  Shortly thereafter, restart it.  Verify only one expiration.
-    // 4. Start a couple of timers.  Verify max active timers.  Discard one and verify the active
-    //    count drops by 1.  Accept one and verify the active count drops by 1.
-
-    @Test
-    public void testSimpleTimeout() throws Exception {
-        // Create an immediate TestHandler.
-        TestInjector injector = new TestInjector(0);
-        TestAnrTimer timer = new TestAnrTimer(false, injector);
-        TestArg t = new TestArg(1, 1, 3);
-        assertTrue(timer.start(t, 10));
-        // Delivery is immediate but occurs on a different thread.
-        assertTrue(mLatch.await(100, TimeUnit.MILLISECONDS));
-        assertEquals(1, mMessages.size());
-        Message m = mMessages.get(0);
-        assertEquals(timer.what(), m.what);
-        assertEquals(t, m.obj);
-
-        // Verify that the timer is still present.
-        assertEquals(1, AnrTimer.sizeOfTimerList());
-        assertTrue(timer.accept(t));
-        assertEquals(0, AnrTimer.sizeOfTimerList());
-
-        // Verify that the timer no longer exists.
-        assertFalse(timer.accept(t));
-    }
-
-    @Test
-    public void testCancel() throws Exception {
-        // Create an non-immediate TestHandler.
-        TestInjector injector = new TestInjector(0, false);
-        TestAnrTimer timer = new TestAnrTimer(false, injector);
-
-        Handler handler = injector.getHandler();
-        assertNotNull(handler);
-        assertTrue(handler instanceof TestHandler);
-
-        // The tests that follow check for a 'what' of 0 (zero), which is the message key used
-        // by AnrTimer internally.
-        TestArg t = new TestArg(1, 1, 3);
-        assertFalse(handler.hasMessages(0));
-        assertTrue(timer.start(t, 100));
-        assertTrue(handler.hasMessages(0));
-        assertTrue(timer.cancel(t));
-        assertFalse(handler.hasMessages(0));
-
-        // Verify that no expiration messages were delivered.
-        assertEquals(0, mMessages.size());
-        assertEquals(0, AnrTimer.sizeOfTimerList());
-    }
-
-    @Test
-    public void testRestart() throws Exception {
-        // Create an non-immediate TestHandler.
-        TestInjector injector = new TestInjector(0, false);
-        TestAnrTimer timer = new TestAnrTimer(false, injector);
-
-        TestArg t = new TestArg(1, 1, 3);
-        assertTrue(timer.start(t, 2500));
-        assertTrue(timer.start(t, 1000));
-
-        // Verify that the test handler saw two timeouts.
-        injector.getHandler().verifyDelays(new long[] { 2500, 1000 });
-
-        // Verify that there is a single timer.  Then cancel it.
-        assertEquals(1, AnrTimer.sizeOfTimerList());
-        assertTrue(timer.cancel(t));
-        assertEquals(0, AnrTimer.sizeOfTimerList());
-    }
-
-    @Test
-    public void testExtendNormal() throws Exception {
-        // Create an immediate TestHandler.
-        TestInjector injector = new TestInjector(5);
-        TestAnrTimer timer = new TestAnrTimer(true, injector);
-        TestArg t = new TestArg(1, 1, 3);
-        assertTrue(timer.start(t, 10));
-
-        assertTrue(mLatch.await(100, TimeUnit.MILLISECONDS));
-        assertEquals(1, mMessages.size());
-        Message m = mMessages.get(0);
-        assertEquals(timer.what(), m.what);
-        assertEquals(t, m.obj);
-
-        // Verify that the test handler saw two timeouts: one of 10ms and one of 5ms.
-        injector.getHandler().verifyDelays(new long[] { 10, 5 });
-
-        // Verify that the timer is still present. Then remove it and verify that the list is
-        // empty.
-        assertEquals(1, AnrTimer.sizeOfTimerList());
-        assertTrue(timer.accept(t));
-        assertEquals(0, AnrTimer.sizeOfTimerList());
-    }
-
-    @Test
-    public void testExtendOversize() throws Exception {
-        // Create an immediate TestHandler.
-        TestInjector injector = new TestInjector(25);
-        TestAnrTimer timer = new TestAnrTimer(true, injector);
-        TestArg t = new TestArg(1, 1, 3);
-        assertTrue(timer.start(t, 10));
-
-        assertTrue(mLatch.await(100, TimeUnit.MILLISECONDS));
-        assertEquals(1, mMessages.size());
-        Message m = mMessages.get(0);
-        assertEquals(timer.what(), m.what);
-        assertEquals(t, m.obj);
-
-        // Verify that the test handler saw two timeouts: one of 10ms and one of 10ms.
-        injector.getHandler().verifyDelays(new long[] { 10, 10 });
-
-        // Verify that the timer is still present. Then remove it and verify that the list is
-        // empty.
-        assertEquals(1, AnrTimer.sizeOfTimerList());
-        assertTrue(timer.accept(t));
-        assertEquals(0, AnrTimer.sizeOfTimerList());
-    }
-}
diff --git a/services/tests/servicestests/src/com/android/server/hdmi/HdmiCecLocalDevicePlaybackTest.java b/services/tests/servicestests/src/com/android/server/hdmi/HdmiCecLocalDevicePlaybackTest.java
index dec89d9..543fa57 100644
--- a/services/tests/servicestests/src/com/android/server/hdmi/HdmiCecLocalDevicePlaybackTest.java
+++ b/services/tests/servicestests/src/com/android/server/hdmi/HdmiCecLocalDevicePlaybackTest.java
@@ -1774,6 +1774,18 @@
     }
 
     @Test
+    public void wakeUp_hotPlugIn_invokesDeviceDiscoveryOnce() {
+        mNativeWrapper.setPollAddressResponse(Constants.ADDR_PLAYBACK_2, SendMessageResult.SUCCESS);
+        mHdmiControlService.onWakeUp(HdmiControlService.WAKE_UP_SCREEN_ON);
+        mTestLooper.dispatchAll();
+
+        mNativeWrapper.onHotplugEvent(1, true);
+        mTestLooper.dispatchAll();
+
+        assertThat(mHdmiCecLocalDevicePlayback.getActions(DeviceDiscoveryAction.class)).hasSize(1);
+    }
+
+    @Test
     public void hotplugDetectionAction_addDevice() {
         int otherPlaybackLogicalAddress = mPlaybackLogicalAddress == Constants.ADDR_PLAYBACK_2
                 ? Constants.ADDR_PLAYBACK_1 : Constants.ADDR_PLAYBACK_2;
diff --git a/services/tests/servicestests/src/com/android/server/media/AudioPoliciesDeviceRouteControllerTest.java b/services/tests/servicestests/src/com/android/server/media/AudioPoliciesDeviceRouteControllerTest.java
deleted file mode 100644
index 5aef7a3..0000000
--- a/services/tests/servicestests/src/com/android/server/media/AudioPoliciesDeviceRouteControllerTest.java
+++ /dev/null
@@ -1,247 +0,0 @@
-/*
- * Copyright (C) 2023 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.media;
-
-import static com.google.common.truth.Truth.assertThat;
-import static com.google.common.truth.Truth.assertWithMessage;
-
-import static org.mockito.Mockito.anyInt;
-import static org.mockito.Mockito.when;
-
-import android.content.Context;
-import android.content.res.Resources;
-import android.media.AudioManager;
-import android.media.AudioRoutesInfo;
-import android.media.IAudioRoutesObserver;
-import android.media.MediaRoute2Info;
-import android.os.RemoteException;
-
-import com.android.internal.R;
-import com.android.server.audio.AudioService;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.JUnit4;
-import org.mockito.ArgumentCaptor;
-import org.mockito.Captor;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-
-@RunWith(JUnit4.class)
-public class AudioPoliciesDeviceRouteControllerTest {
-
-    private static final String ROUTE_NAME_DEFAULT = "default";
-    private static final String ROUTE_NAME_DOCK = "dock";
-    private static final String ROUTE_NAME_HEADPHONES = "headphones";
-
-    private static final int VOLUME_SAMPLE_1 = 25;
-
-    @Mock
-    private Context mContext;
-    @Mock
-    private Resources mResources;
-    @Mock
-    private AudioManager mAudioManager;
-    @Mock
-    private AudioService mAudioService;
-    @Mock
-    private DeviceRouteController.OnDeviceRouteChangedListener mOnDeviceRouteChangedListener;
-
-    @Captor
-    private ArgumentCaptor<IAudioRoutesObserver.Stub> mAudioRoutesObserverCaptor;
-
-    private AudioPoliciesDeviceRouteController mController;
-
-    private IAudioRoutesObserver.Stub mAudioRoutesObserver;
-
-    @Before
-    public void setUp() {
-        MockitoAnnotations.initMocks(this);
-
-        when(mContext.getResources()).thenReturn(mResources);
-        when(mResources.getText(anyInt())).thenReturn(ROUTE_NAME_DEFAULT);
-
-        // Setting built-in speaker as default speaker.
-        AudioRoutesInfo audioRoutesInfo = new AudioRoutesInfo();
-        audioRoutesInfo.mainType = AudioRoutesInfo.MAIN_SPEAKER;
-        when(mAudioService.startWatchingRoutes(mAudioRoutesObserverCaptor.capture()))
-                .thenReturn(audioRoutesInfo);
-
-        mController = new AudioPoliciesDeviceRouteController(
-                mContext, mAudioManager, mAudioService, mOnDeviceRouteChangedListener);
-
-        mAudioRoutesObserver = mAudioRoutesObserverCaptor.getValue();
-    }
-
-    @Test
-    public void getDeviceRoute_noSelectedRoutes_returnsDefaultDevice() {
-        MediaRoute2Info route2Info = mController.getSelectedRoute();
-
-        assertThat(route2Info.getName()).isEqualTo(ROUTE_NAME_DEFAULT);
-        assertThat(route2Info.getType()).isEqualTo(MediaRoute2Info.TYPE_BUILTIN_SPEAKER);
-    }
-
-    @Test
-    public void getDeviceRoute_audioRouteHasChanged_returnsRouteFromAudioService() {
-        when(mResources.getText(R.string.default_audio_route_name_headphones))
-                .thenReturn(ROUTE_NAME_HEADPHONES);
-
-        AudioRoutesInfo audioRoutesInfo = new AudioRoutesInfo();
-        audioRoutesInfo.mainType = AudioRoutesInfo.MAIN_HEADPHONES;
-        callAudioRoutesObserver(audioRoutesInfo);
-
-        MediaRoute2Info route2Info = mController.getSelectedRoute();
-        assertThat(route2Info.getName()).isEqualTo(ROUTE_NAME_HEADPHONES);
-        assertThat(route2Info.getType()).isEqualTo(MediaRoute2Info.TYPE_WIRED_HEADPHONES);
-    }
-
-    @Test
-    public void getDeviceRoute_selectDevice_returnsSelectedRoute() {
-        when(mResources.getText(R.string.default_audio_route_name_dock_speakers))
-                .thenReturn(ROUTE_NAME_DOCK);
-
-        mController.selectRoute(MediaRoute2Info.TYPE_DOCK);
-
-        MediaRoute2Info route2Info = mController.getSelectedRoute();
-        assertThat(route2Info.getName()).isEqualTo(ROUTE_NAME_DOCK);
-        assertThat(route2Info.getType()).isEqualTo(MediaRoute2Info.TYPE_DOCK);
-    }
-
-    @Test
-    public void getDeviceRoute_hasSelectedAndAudioServiceRoutes_returnsSelectedRoute() {
-        when(mResources.getText(R.string.default_audio_route_name_headphones))
-                .thenReturn(ROUTE_NAME_HEADPHONES);
-        when(mResources.getText(R.string.default_audio_route_name_dock_speakers))
-                .thenReturn(ROUTE_NAME_DOCK);
-
-        AudioRoutesInfo audioRoutesInfo = new AudioRoutesInfo();
-        audioRoutesInfo.mainType = AudioRoutesInfo.MAIN_HEADPHONES;
-        callAudioRoutesObserver(audioRoutesInfo);
-
-        mController.selectRoute(MediaRoute2Info.TYPE_DOCK);
-
-        MediaRoute2Info route2Info = mController.getSelectedRoute();
-        assertThat(route2Info.getName()).isEqualTo(ROUTE_NAME_DOCK);
-        assertThat(route2Info.getType()).isEqualTo(MediaRoute2Info.TYPE_DOCK);
-    }
-
-    @Test
-    public void getDeviceRoute_unselectRoute_returnsAudioServiceRoute() {
-        when(mResources.getText(R.string.default_audio_route_name_headphones))
-                .thenReturn(ROUTE_NAME_HEADPHONES);
-        when(mResources.getText(R.string.default_audio_route_name_dock_speakers))
-                .thenReturn(ROUTE_NAME_DOCK);
-
-        mController.selectRoute(MediaRoute2Info.TYPE_DOCK);
-
-        AudioRoutesInfo audioRoutesInfo = new AudioRoutesInfo();
-        audioRoutesInfo.mainType = AudioRoutesInfo.MAIN_HEADPHONES;
-        callAudioRoutesObserver(audioRoutesInfo);
-
-        mController.selectRoute(null);
-
-        MediaRoute2Info route2Info = mController.getSelectedRoute();
-        assertThat(route2Info.getName()).isEqualTo(ROUTE_NAME_HEADPHONES);
-        assertThat(route2Info.getType()).isEqualTo(MediaRoute2Info.TYPE_WIRED_HEADPHONES);
-    }
-
-    @Test
-    public void getDeviceRoute_selectRouteFails_returnsAudioServiceRoute() {
-        when(mResources.getText(R.string.default_audio_route_name_headphones))
-                .thenReturn(ROUTE_NAME_HEADPHONES);
-
-        AudioRoutesInfo audioRoutesInfo = new AudioRoutesInfo();
-        audioRoutesInfo.mainType = AudioRoutesInfo.MAIN_HEADPHONES;
-        callAudioRoutesObserver(audioRoutesInfo);
-
-        mController.selectRoute(MediaRoute2Info.TYPE_BLUETOOTH_A2DP);
-
-        MediaRoute2Info route2Info = mController.getSelectedRoute();
-        assertThat(route2Info.getName()).isEqualTo(ROUTE_NAME_HEADPHONES);
-        assertThat(route2Info.getType()).isEqualTo(MediaRoute2Info.TYPE_WIRED_HEADPHONES);
-    }
-
-    @Test
-    public void selectRoute_selectWiredRoute_returnsTrue() {
-        assertThat(mController.selectRoute(MediaRoute2Info.TYPE_HDMI)).isTrue();
-    }
-
-    @Test
-    public void selectRoute_selectBluetoothRoute_returnsFalse() {
-        assertThat(mController.selectRoute(MediaRoute2Info.TYPE_BLUETOOTH_A2DP)).isFalse();
-    }
-
-    @Test
-    public void selectRoute_unselectRoute_returnsTrue() {
-        assertThat(mController.selectRoute(null)).isTrue();
-    }
-
-    @Test
-    public void updateVolume_noSelectedRoute_deviceRouteVolumeChanged() {
-        when(mResources.getText(R.string.default_audio_route_name_headphones))
-                .thenReturn(ROUTE_NAME_HEADPHONES);
-
-        AudioRoutesInfo audioRoutesInfo = new AudioRoutesInfo();
-        audioRoutesInfo.mainType = AudioRoutesInfo.MAIN_HEADPHONES;
-        callAudioRoutesObserver(audioRoutesInfo);
-
-        mController.updateVolume(VOLUME_SAMPLE_1);
-
-        MediaRoute2Info route2Info = mController.getSelectedRoute();
-        assertThat(route2Info.getType()).isEqualTo(MediaRoute2Info.TYPE_WIRED_HEADPHONES);
-        assertThat(route2Info.getVolume()).isEqualTo(VOLUME_SAMPLE_1);
-    }
-
-    @Test
-    public void updateVolume_connectSelectedRouteLater_selectedRouteVolumeChanged() {
-        when(mResources.getText(R.string.default_audio_route_name_headphones))
-                .thenReturn(ROUTE_NAME_HEADPHONES);
-        when(mResources.getText(R.string.default_audio_route_name_dock_speakers))
-                .thenReturn(ROUTE_NAME_DOCK);
-
-        AudioRoutesInfo audioRoutesInfo = new AudioRoutesInfo();
-        audioRoutesInfo.mainType = AudioRoutesInfo.MAIN_HEADPHONES;
-        callAudioRoutesObserver(audioRoutesInfo);
-
-        mController.updateVolume(VOLUME_SAMPLE_1);
-
-        mController.selectRoute(MediaRoute2Info.TYPE_DOCK);
-
-        MediaRoute2Info route2Info = mController.getSelectedRoute();
-        assertThat(route2Info.getType()).isEqualTo(MediaRoute2Info.TYPE_DOCK);
-        assertThat(route2Info.getVolume()).isEqualTo(VOLUME_SAMPLE_1);
-    }
-
-    /**
-     * Simulates {@link IAudioRoutesObserver.Stub#dispatchAudioRoutesChanged(AudioRoutesInfo)}
-     * from {@link AudioService}. This happens when there is a wired route change,
-     * like a wired headset being connected.
-     *
-     * @param audioRoutesInfo updated state of connected wired device
-     */
-    private void callAudioRoutesObserver(AudioRoutesInfo audioRoutesInfo) {
-        try {
-            // this is a captured observer implementation
-            // from WiredRoutesController's AudioService#startWatchingRoutes call
-            mAudioRoutesObserver.dispatchAudioRoutesChanged(audioRoutesInfo);
-        } catch (RemoteException exception) {
-            // Should not happen since the object is mocked.
-            assertWithMessage("An unexpected RemoteException happened.").fail();
-        }
-    }
-}
diff --git a/services/tests/servicestests/src/com/android/server/media/DeviceRouteControllerTest.java b/services/tests/servicestests/src/com/android/server/media/DeviceRouteControllerTest.java
index 14b121d..0961b7d 100644
--- a/services/tests/servicestests/src/com/android/server/media/DeviceRouteControllerTest.java
+++ b/services/tests/servicestests/src/com/android/server/media/DeviceRouteControllerTest.java
@@ -19,6 +19,7 @@
 import static com.android.media.flags.Flags.FLAG_ENABLE_AUDIO_POLICIES_DEVICE_AND_BLUETOOTH_CONTROLLER;
 
 import android.content.Context;
+import android.os.Looper;
 import android.platform.test.annotations.RequiresFlagsDisabled;
 import android.platform.test.annotations.RequiresFlagsEnabled;
 import android.platform.test.flag.junit.CheckFlagsRule;
@@ -56,7 +57,8 @@
     @RequiresFlagsDisabled(FLAG_ENABLE_AUDIO_POLICIES_DEVICE_AND_BLUETOOTH_CONTROLLER)
     public void createInstance_audioPoliciesFlagIsDisabled_createsLegacyController() {
         DeviceRouteController deviceRouteController =
-                DeviceRouteController.createInstance(mContext, mOnDeviceRouteChangedListener);
+                DeviceRouteController.createInstance(
+                        mContext, Looper.getMainLooper(), mOnDeviceRouteChangedListener);
 
         Truth.assertThat(deviceRouteController).isInstanceOf(LegacyDeviceRouteController.class);
     }
@@ -65,7 +67,8 @@
     @RequiresFlagsEnabled(FLAG_ENABLE_AUDIO_POLICIES_DEVICE_AND_BLUETOOTH_CONTROLLER)
     public void createInstance_audioPoliciesFlagIsEnabled_createsAudioPoliciesController() {
         DeviceRouteController deviceRouteController =
-                DeviceRouteController.createInstance(mContext, mOnDeviceRouteChangedListener);
+                DeviceRouteController.createInstance(
+                        mContext, Looper.getMainLooper(), mOnDeviceRouteChangedListener);
 
         Truth.assertThat(deviceRouteController)
                 .isInstanceOf(AudioPoliciesDeviceRouteController.class);
diff --git a/services/tests/servicestests/src/com/android/server/utils/AnrTimerTest.java b/services/tests/servicestests/src/com/android/server/utils/AnrTimerTest.java
new file mode 100644
index 0000000..330dbb8
--- /dev/null
+++ b/services/tests/servicestests/src/com/android/server/utils/AnrTimerTest.java
@@ -0,0 +1,203 @@
+/*
+ * Copyright (C) 2023 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.utils;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import android.os.Handler;
+import android.os.Looper;
+import android.os.Message;
+
+import android.platform.test.annotations.Presubmit;
+import androidx.test.filters.SmallTest;
+
+import com.android.internal.annotations.GuardedBy;
+
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+@SmallTest
+@Presubmit
+public class AnrTimerTest {
+
+    // The commonly used message timeout key.
+    private static final int MSG_TIMEOUT = 1;
+
+    // The test argument includes a pid and uid, and a tag.  The tag is used to distinguish
+    // different message instances.  Additional fields (like what) capture delivery information
+    // that is checked by the test.
+    private static class TestArg {
+        final int pid;
+        final int uid;
+        int what;
+
+        TestArg(int pid, int uid) {
+            this.pid = pid;
+            this.uid = uid;
+            this.what = 0;
+        }
+    }
+
+    /**
+     * The test handler is a self-contained object for a single test.
+     */
+    private static class Helper {
+        final Object mLock = new Object();
+
+        final Handler mHandler;
+        final CountDownLatch mLatch;
+        @GuardedBy("mLock")
+        final ArrayList<TestArg> mMessages;
+
+        Helper(int expect) {
+            mHandler = new Handler(Looper.getMainLooper(), this::expirationHandler);
+            mMessages = new ArrayList<>();
+            mLatch = new CountDownLatch(expect);
+        }
+
+        /**
+         * When a timer expires, the object must be a TestArg.  Update the TestArg with
+         * expiration metadata and save it.
+         */
+        private boolean expirationHandler(Message msg) {
+            synchronized (mLock) {
+                TestArg arg = (TestArg) msg.obj;
+                arg.what = msg.what;
+                mMessages.add(arg);
+                mLatch.countDown();
+                return false;
+            }
+        }
+
+        boolean await(long timeout) throws InterruptedException {
+            // No need to synchronize, as the CountDownLatch is already thread-safe.
+            return mLatch.await(timeout, TimeUnit.MILLISECONDS);
+        }
+
+        /**
+         * Fetch the received messages.  Fail if the count of received messages is other than the
+         * expected count.
+         */
+        TestArg[] messages(int expected) {
+            synchronized (mLock) {
+                assertEquals(expected, mMessages.size());
+                return mMessages.toArray(new TestArg[expected]);
+            }
+        }
+    }
+
+    /**
+     * An instrumented AnrTimer.
+     */
+    private static class TestAnrTimer extends AnrTimer<TestArg> {
+        private TestAnrTimer(Handler h, int key, String tag) {
+            super(h, key, tag);
+        }
+
+        TestAnrTimer(Helper helper) {
+            this(helper.mHandler, MSG_TIMEOUT, caller());
+        }
+
+        void start(TestArg arg, long millis) {
+            start(arg, arg.pid, arg.uid, millis);
+        }
+
+        // Return the name of method that called the constructor, assuming that this function is
+        // called from inside the constructor.  The calling method is used to name the AnrTimer
+        // instance so that logs are easier to understand.
+        private static String caller() {
+            final int n = 4;
+            StackTraceElement[] stack = Thread.currentThread().getStackTrace();
+            if (stack.length < n+1) return "test";
+            return stack[n].getMethodName();
+        }
+    }
+
+    void validate(TestArg expected, TestArg actual) {
+        assertEquals(expected, actual);
+        assertEquals(actual.what, MSG_TIMEOUT);
+    }
+
+
+    /**
+     * Verify that a simple expiration succeeds.  The timer is started for 10ms.  The test
+     * procedure waits 5s for the expiration message, but under correct operation, the test will
+     * only take 10ms
+     */
+    @Test
+    public void testSimpleTimeout() throws Exception {
+        Helper helper = new Helper(1);
+        TestAnrTimer timer = new TestAnrTimer(helper);
+        TestArg t = new TestArg(1, 1);
+        timer.start(t, 10);
+        // Delivery is immediate but occurs on a different thread.
+        assertTrue(helper.await(5000));
+        TestArg[] result = helper.messages(1);
+        validate(t, result[0]);
+    }
+
+    /**
+     * Verify that if three timers are scheduled, they are delivered in time order.
+     */
+    @Test
+    public void testMultipleTimers() throws Exception {
+        // Expect three messages.
+        Helper helper = new Helper(3);
+        TestAnrTimer timer = new TestAnrTimer(helper);
+        TestArg t1 = new TestArg(1, 1);
+        TestArg t2 = new TestArg(1, 2);
+        TestArg t3 = new TestArg(1, 3);
+        timer.start(t1, 50);
+        timer.start(t2, 60);
+        timer.start(t3, 40);
+        // Delivery is immediate but occurs on a different thread.
+        assertTrue(helper.await(5000));
+        TestArg[] result = helper.messages(3);
+        validate(t3, result[0]);
+        validate(t1, result[1]);
+        validate(t2, result[2]);
+    }
+
+    /**
+     * Verify that a canceled timer is not delivered.
+     */
+    @Test
+    public void testCancelTimer() throws Exception {
+        // Expect two messages.
+        Helper helper = new Helper(2);
+        TestAnrTimer timer = new TestAnrTimer(helper);
+        TestArg t1 = new TestArg(1, 1);
+        TestArg t2 = new TestArg(1, 2);
+        TestArg t3 = new TestArg(1, 3);
+        timer.start(t1, 50);
+        timer.start(t2, 60);
+        timer.start(t3, 40);
+        // Briefly pause.
+        assertFalse(helper.await(10));
+        timer.cancel(t1);
+        // Delivery is immediate but occurs on a different thread.
+        assertTrue(helper.await(5000));
+        TestArg[] result = helper.messages(2);
+        validate(t3, result[0]);
+        validate(t2, result[1]);
+    }
+}
diff --git a/services/tests/uiservicestests/src/com/android/server/notification/DefaultDeviceEffectsApplierTest.java b/services/tests/uiservicestests/src/com/android/server/notification/DefaultDeviceEffectsApplierTest.java
new file mode 100644
index 0000000..5febd02
--- /dev/null
+++ b/services/tests/uiservicestests/src/com/android/server/notification/DefaultDeviceEffectsApplierTest.java
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2023 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.notification;
+
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyZeroInteractions;
+
+import android.app.UiModeManager;
+import android.app.WallpaperManager;
+import android.hardware.display.ColorDisplayManager;
+import android.os.PowerManager;
+import android.platform.test.flag.junit.SetFlagsRule;
+import android.service.notification.ZenDeviceEffects;
+import android.testing.TestableContext;
+
+import androidx.test.InstrumentationRegistry;
+import androidx.test.runner.AndroidJUnit4;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+@RunWith(AndroidJUnit4.class)
+public class DefaultDeviceEffectsApplierTest {
+
+    @Rule
+    public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
+
+    private TestableContext mContext;
+    private DefaultDeviceEffectsApplier mApplier;
+    @Mock PowerManager mPowerManager;
+    @Mock ColorDisplayManager mColorDisplayManager;
+    @Mock UiModeManager mUiModeManager;
+    @Mock WallpaperManager mWallpaperManager;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mContext = new TestableContext(InstrumentationRegistry.getContext(), null);
+        mContext.addMockSystemService(PowerManager.class, mPowerManager);
+        mContext.addMockSystemService(ColorDisplayManager.class, mColorDisplayManager);
+        mContext.addMockSystemService(UiModeManager.class, mUiModeManager);
+        mContext.addMockSystemService(WallpaperManager.class, mWallpaperManager);
+
+        mApplier = new DefaultDeviceEffectsApplier(mContext);
+    }
+
+    @Test
+    public void apply_appliesEffects() {
+        mSetFlagsRule.enableFlags(android.app.Flags.FLAG_MODES_API);
+
+        ZenDeviceEffects effects = new ZenDeviceEffects.Builder()
+                .setShouldSuppressAmbientDisplay(true)
+                .setShouldDimWallpaper(true)
+                .setShouldDisplayGrayscale(true)
+                .setShouldUseNightMode(true)
+                .build();
+        mApplier.apply(effects);
+
+        verify(mPowerManager).suppressAmbientDisplay(anyString(), eq(true));
+        verify(mColorDisplayManager).setSaturationLevel(eq(0));
+        verify(mWallpaperManager).setWallpaperDimAmount(eq(0.6f));
+        verifyZeroInteractions(mUiModeManager); // Coming later; adding now so test fails then. :)
+    }
+
+    @Test
+    public void apply_removesEffects() {
+        mSetFlagsRule.enableFlags(android.app.Flags.FLAG_MODES_API);
+
+        ZenDeviceEffects noEffects = new ZenDeviceEffects.Builder().build();
+        mApplier.apply(noEffects);
+
+        verify(mPowerManager).suppressAmbientDisplay(anyString(), eq(false));
+        verify(mColorDisplayManager).setSaturationLevel(eq(100));
+        verify(mWallpaperManager).setWallpaperDimAmount(eq(0.0f));
+        verifyZeroInteractions(mUiModeManager);
+    }
+
+    @Test
+    public void apply_missingSomeServices_okay() {
+        mSetFlagsRule.enableFlags(android.app.Flags.FLAG_MODES_API);
+        mContext.addMockSystemService(ColorDisplayManager.class, null);
+        mContext.addMockSystemService(WallpaperManager.class, null);
+
+        ZenDeviceEffects effects = new ZenDeviceEffects.Builder()
+                .setShouldSuppressAmbientDisplay(true)
+                .setShouldDimWallpaper(true)
+                .setShouldDisplayGrayscale(true)
+                .setShouldUseNightMode(true)
+                .build();
+        mApplier.apply(effects);
+
+        verify(mPowerManager).suppressAmbientDisplay(anyString(), eq(true));
+        // (And no crash from missing services).
+    }
+}
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 b1fdec9..ef6fced 100644
--- a/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java
+++ b/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java
@@ -77,9 +77,9 @@
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.notNull;
 import static org.mockito.Mockito.reset;
-import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
 import static org.mockito.Mockito.when;
 import static org.mockito.Mockito.withSettings;
 
@@ -112,6 +112,7 @@
 import android.provider.Settings;
 import android.provider.Settings.Global;
 import android.service.notification.Condition;
+import android.service.notification.DeviceEffectsApplier;
 import android.service.notification.ZenDeviceEffects;
 import android.service.notification.ZenModeConfig;
 import android.service.notification.ZenModeConfig.ScheduleInfo;
@@ -188,8 +189,10 @@
             .appendPath("test")
             .build();
 
-    private static final Condition CONDITION = new Condition(CONDITION_ID, "",
+    private static final Condition CONDITION_TRUE = new Condition(CONDITION_ID, "",
             Condition.STATE_TRUE);
+    private static final Condition CONDITION_FALSE = new Condition(CONDITION_ID, "",
+            Condition.STATE_FALSE);
     private static final String TRIGGER_DESC = "Every Night, 10pm to 6am";
     private static final int TYPE = TYPE_BEDTIME;
     private static final boolean ALLOW_MANUAL = true;
@@ -201,6 +204,7 @@
             = NotificationManager.INTERRUPTION_FILTER_ALARMS;
     private static final boolean ENABLED = true;
     private static final int CREATION_TIME = 123;
+    private static final ZenDeviceEffects NO_EFFECTS = new ZenDeviceEffects.Builder().build();
 
     @Rule
     public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
@@ -212,6 +216,7 @@
     private TestableLooper mTestableLooper;
     private ZenModeHelper mZenModeHelper;
     private ContentResolver mContentResolver;
+    @Mock DeviceEffectsApplier mDeviceEffectsApplier;
     @Mock AppOpsManager mAppOps;
     TestableFlagResolver mTestFlagResolver = new TestableFlagResolver();
     ZenModeEventLoggerFake mZenModeEventLogger;
@@ -238,8 +243,9 @@
         when(mPackageManager.getResourcesForApplication(anyString())).thenReturn(
                 mResources);
 
-        when(mContext.getSystemService(AppOpsManager.class)).thenReturn(mAppOps);
-        when(mContext.getSystemService(NotificationManager.class)).thenReturn(mNotificationManager);
+        mContext.addMockSystemService(AppOpsManager.class, mAppOps);
+        mContext.addMockSystemService(NotificationManager.class, mNotificationManager);
+
         mConditionProviders = new ConditionProviders(mContext, new UserProfiles(),
                 AppGlobals.getPackageManager());
         mConditionProviders.addSystemProvider(new CountdownConditionProvider());
@@ -609,7 +615,7 @@
         // and we're setting zen mode on
         Settings.Secure.putInt(mContentResolver, Settings.Secure.SHOW_ZEN_UPGRADE_NOTIFICATION, 1);
         Settings.Secure.putInt(mContentResolver, Settings.Secure.ZEN_SETTINGS_UPDATED, 0);
-        mZenModeHelper.mIsBootComplete = true;
+        mZenModeHelper.mIsSystemServicesReady = true;
         mZenModeHelper.mConsolidatedPolicy = new Policy(0, 0, 0, 0, 0, 0);
         mZenModeHelper.setZenModeSetting(ZEN_MODE_IMPORTANT_INTERRUPTIONS);
 
@@ -624,7 +630,7 @@
         // doesn't show upgrade notification if stored settings says don't show
         Settings.Secure.putInt(mContentResolver, Settings.Secure.SHOW_ZEN_UPGRADE_NOTIFICATION, 0);
         Settings.Secure.putInt(mContentResolver, Settings.Secure.ZEN_SETTINGS_UPDATED, 0);
-        mZenModeHelper.mIsBootComplete = true;
+        mZenModeHelper.mIsSystemServicesReady = true;
         mZenModeHelper.setZenModeSetting(ZEN_MODE_IMPORTANT_INTERRUPTIONS);
 
         verify(mNotificationManager, never()).notify(eq(ZenModeHelper.TAG),
@@ -636,7 +642,7 @@
         // doesn't show upgrade notification since zen was already updated
         Settings.Secure.putInt(mContentResolver, Settings.Secure.SHOW_ZEN_UPGRADE_NOTIFICATION, 0);
         Settings.Secure.putInt(mContentResolver, Settings.Secure.ZEN_SETTINGS_UPDATED, 1);
-        mZenModeHelper.mIsBootComplete = true;
+        mZenModeHelper.mIsSystemServicesReady = true;
         mZenModeHelper.setZenModeSetting(ZEN_MODE_IMPORTANT_INTERRUPTIONS);
 
         verify(mNotificationManager, never()).notify(eq(ZenModeHelper.TAG),
@@ -3060,7 +3066,7 @@
         rule.configurationActivity = CONFIG_ACTIVITY;
         rule.component = OWNER;
         rule.conditionId = CONDITION_ID;
-        rule.condition = CONDITION;
+        rule.condition = CONDITION_TRUE;
         rule.enabled = ENABLED;
         rule.creationTime = 123;
         rule.id = "id";
@@ -3350,6 +3356,84 @@
     }
 
     @Test
+    public void testDeviceEffects_applied() {
+        mSetFlagsRule.enableFlags(android.app.Flags.FLAG_MODES_API);
+        mZenModeHelper.setDeviceEffectsApplier(mDeviceEffectsApplier);
+
+        ZenDeviceEffects effects = new ZenDeviceEffects.Builder()
+                .setShouldSuppressAmbientDisplay(true)
+                .setShouldDimWallpaper(true)
+                .build();
+        String ruleId = addRuleWithEffects(effects);
+        verify(mDeviceEffectsApplier, never()).apply(any());
+
+        mZenModeHelper.setAutomaticZenRuleState(ruleId, CONDITION_TRUE, CUSTOM_PKG_UID, false);
+        mTestableLooper.processAllMessages();
+
+        verify(mDeviceEffectsApplier).apply(eq(effects));
+    }
+
+    @Test
+    public void testDeviceEffects_onDeactivateRule_applied() {
+        mSetFlagsRule.enableFlags(android.app.Flags.FLAG_MODES_API);
+        mZenModeHelper.setDeviceEffectsApplier(mDeviceEffectsApplier);
+
+        ZenDeviceEffects zde = new ZenDeviceEffects.Builder().setShouldUseNightMode(true).build();
+        String ruleId = addRuleWithEffects(zde);
+        mZenModeHelper.setAutomaticZenRuleState(ruleId, CONDITION_TRUE, CUSTOM_PKG_UID, false);
+        mTestableLooper.processAllMessages();
+        verify(mDeviceEffectsApplier).apply(eq(zde));
+
+        mZenModeHelper.setAutomaticZenRuleState(ruleId, CONDITION_FALSE, CUSTOM_PKG_UID, false);
+        mTestableLooper.processAllMessages();
+
+        verify(mDeviceEffectsApplier).apply(eq(NO_EFFECTS));
+    }
+
+    @Test
+    public void testDeviceEffects_noChangeToConsolidatedEffects_notApplied() {
+        mSetFlagsRule.enableFlags(android.app.Flags.FLAG_MODES_API);
+        mZenModeHelper.setDeviceEffectsApplier(mDeviceEffectsApplier);
+
+        ZenDeviceEffects zde = new ZenDeviceEffects.Builder().setShouldUseNightMode(true).build();
+        String ruleId = addRuleWithEffects(zde);
+        mZenModeHelper.setAutomaticZenRuleState(ruleId, CONDITION_TRUE, CUSTOM_PKG_UID, false);
+        mTestableLooper.processAllMessages();
+        verify(mDeviceEffectsApplier).apply(eq(zde));
+
+        // Now create and activate a second rule that doesn't add any more effects.
+        String secondRuleId = addRuleWithEffects(zde);
+        mZenModeHelper.setAutomaticZenRuleState(secondRuleId, CONDITION_TRUE, CUSTOM_PKG_UID,
+                false);
+        mTestableLooper.processAllMessages();
+
+        verifyNoMoreInteractions(mDeviceEffectsApplier);
+    }
+
+    @Test
+    public void testDeviceEffects_activeBeforeApplierProvided_appliedWhenProvided() {
+        mSetFlagsRule.enableFlags(android.app.Flags.FLAG_MODES_API);
+
+        ZenDeviceEffects zde = new ZenDeviceEffects.Builder().setShouldUseNightMode(true).build();
+        String ruleId = addRuleWithEffects(zde);
+        verify(mDeviceEffectsApplier, never()).apply(any());
+
+        mZenModeHelper.setAutomaticZenRuleState(ruleId, CONDITION_TRUE, CUSTOM_PKG_UID, false);
+        mTestableLooper.processAllMessages();
+        verify(mDeviceEffectsApplier, never()).apply(any());
+
+        mZenModeHelper.setDeviceEffectsApplier(mDeviceEffectsApplier);
+        verify(mDeviceEffectsApplier).apply(eq(zde));
+    }
+
+    private String addRuleWithEffects(ZenDeviceEffects effects) {
+        AutomaticZenRule rule = new AutomaticZenRule.Builder("Test", CONDITION_ID)
+                .setDeviceEffects(effects)
+                .build();
+        return mZenModeHelper.addAutomaticZenRule("pkg", rule, "", CUSTOM_PKG_UID, FROM_APP);
+    }
+
+    @Test
     public void applyGlobalZenModeAsImplicitZenRule_createsImplicitRuleAndActivatesIt() {
         mSetFlagsRule.enableFlags(android.app.Flags.FLAG_MODES_API);
         mZenModeHelper.mConfig.automaticRules.clear();
diff --git a/services/tests/wmtests/AndroidManifest.xml b/services/tests/wmtests/AndroidManifest.xml
index a8d3fa1..c3074bb 100644
--- a/services/tests/wmtests/AndroidManifest.xml
+++ b/services/tests/wmtests/AndroidManifest.xml
@@ -99,7 +99,7 @@
             android:theme="@style/WhiteBackgroundTheme"
             android:exported="true"/>
 
-        <activity android:name="com.android.server.wm.TrustedPresentationListenerTest$TestActivity"
+        <activity android:name="com.android.server.wm.TrustedPresentationCallbackTest$TestActivity"
             android:exported="true"
             android:showWhenLocked="true"
             android:turnScreenOn="true" />
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 afea811..4d4d397 100644
--- a/services/tests/wmtests/src/com/android/server/wm/BackNavigationControllerTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/BackNavigationControllerTests.java
@@ -242,6 +242,110 @@
     }
 
     @Test
+    public void backTypeCrossActivityInTaskFragment() {
+        final Task task = createTask(mDefaultDisplay);
+        final TaskFragment tf1 = createTaskFragmentWithActivity(task);
+        final TaskFragment tf2 = createTaskFragmentWithActivity(task);
+        final ArrayList<ActivityRecord> outPrevActivities = new ArrayList<>();
+
+        ActivityRecord prevAr = tf1.getTopMostActivity();
+        ActivityRecord topAr = tf2.getTopMostActivity();
+        boolean predictable;
+
+        // Stacked + no Companion => predict for previous activity.
+        // TF2
+        // TF1
+        predictable = BackNavigationController.getAnimatablePrevActivities(task, topAr,
+                outPrevActivities);
+        assertTrue(outPrevActivities.contains(prevAr));
+        assertTrue(predictable);
+        outPrevActivities.clear();
+
+        // Stacked + companion => predict for previous task
+        tf2.setCompanionTaskFragment(tf1);
+        predictable = BackNavigationController.getAnimatablePrevActivities(task, topAr,
+                outPrevActivities);
+        assertTrue(outPrevActivities.isEmpty());
+        assertTrue(predictable);
+        tf2.setCompanionTaskFragment(null);
+
+        // Adjacent + no companion => unable to predict
+        // TF1 | TF2
+        tf1.setAdjacentTaskFragment(tf2);
+        tf2.setAdjacentTaskFragment(tf1);
+        predictable = BackNavigationController.getAnimatablePrevActivities(task, topAr,
+                outPrevActivities);
+        assertTrue(outPrevActivities.isEmpty());
+        assertFalse(predictable);
+        predictable = BackNavigationController.getAnimatablePrevActivities(task, prevAr,
+                outPrevActivities);
+        assertTrue(outPrevActivities.isEmpty());
+        assertFalse(predictable);
+
+        // Adjacent + companion => predict for previous task
+        tf1.setCompanionTaskFragment(tf2);
+        tf2.setCompanionTaskFragment(tf1);
+        predictable = BackNavigationController.getAnimatablePrevActivities(task, topAr,
+                outPrevActivities);
+        assertTrue(outPrevActivities.isEmpty());
+        assertTrue(predictable);
+        predictable = BackNavigationController.getAnimatablePrevActivities(task, prevAr,
+                outPrevActivities);
+        assertTrue(outPrevActivities.isEmpty());
+        assertTrue(predictable);
+        // reset
+        tf1.setAdjacentTaskFragment(null);
+        tf2.setAdjacentTaskFragment(null);
+        tf1.setCompanionTaskFragment(null);
+        tf2.setCompanionTaskFragment(null);
+
+        final TaskFragment tf3 = new TaskFragmentBuilder(mAtm)
+                .createActivityCount(2)
+                .setParentTask(task)
+                .build();
+        topAr = tf3.getTopMostActivity();
+        prevAr = tf3.getBottomMostActivity();
+        // Stacked => predict for previous activity.
+        // TF3
+        // TF2
+        // TF1
+        predictable = BackNavigationController.getAnimatablePrevActivities(task, topAr,
+                outPrevActivities);
+        assertTrue(outPrevActivities.contains(prevAr));
+        assertTrue(predictable);
+        // reset
+        outPrevActivities.clear();
+
+        // Adjacent => predict for previous activity.
+        // TF2 | TF3
+        // TF1
+        tf2.setAdjacentTaskFragment(tf3);
+        tf3.setAdjacentTaskFragment(tf2);
+        predictable = BackNavigationController.getAnimatablePrevActivities(task, topAr,
+                outPrevActivities);
+        assertTrue(outPrevActivities.contains(prevAr));
+        assertTrue(predictable);
+        // reset
+        outPrevActivities.clear();
+        tf2.setAdjacentTaskFragment(null);
+        tf3.setAdjacentTaskFragment(null);
+
+        final TaskFragment tf4 = createTaskFragmentWithActivity(task);
+        // Stacked + companion => predict for previous activity below companion.
+        // Tf4
+        // TF3
+        // TF2
+        // TF1
+        tf4.setCompanionTaskFragment(tf3);
+        tf3.setCompanionTaskFragment(tf4);
+        topAr = tf4.getTopMostActivity();
+        predictable = BackNavigationController.getAnimatablePrevActivities(task, topAr,
+                outPrevActivities);
+        assertTrue(outPrevActivities.contains(tf2.getTopMostActivity()));
+        assertTrue(predictable);
+    }
+
+    @Test
     public void backTypeDialogCloseWhenBackFromDialog() {
         DialogCloseTestCase testCase = createTopTaskWithActivityAndDialog();
         IOnBackInvokedCallback callback = withSystemCallback(testCase.task);
diff --git a/services/tests/wmtests/src/com/android/server/wm/TrustedPresentationCallbackTest.java b/services/tests/wmtests/src/com/android/server/wm/TrustedPresentationCallbackTest.java
new file mode 100644
index 0000000..c5dd447
--- /dev/null
+++ b/services/tests/wmtests/src/com/android/server/wm/TrustedPresentationCallbackTest.java
@@ -0,0 +1,154 @@
+/*
+ * Copyright (C) 2023 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.wm;
+
+import static android.server.wm.ActivityManagerTestBase.createFullscreenActivityScenarioRule;
+import static android.server.wm.BuildUtils.HW_TIMEOUT_MULTIPLIER;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import android.app.Activity;
+import android.platform.test.annotations.Presubmit;
+import android.server.wm.CtsWindowInfoUtils;
+import android.view.SurfaceControl;
+import android.view.SurfaceControl.TrustedPresentationThresholds;
+
+import androidx.annotation.GuardedBy;
+import androidx.test.ext.junit.rules.ActivityScenarioRule;
+
+import com.android.server.wm.utils.CommonUtils;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TestName;
+
+import java.util.function.Consumer;
+
+/**
+ * TODO (b/287076178): Move these tests to
+ * {@link android.view.surfacecontrol.cts.TrustedPresentationCallbackTest} when API is made public
+ */
+@Presubmit
+public class TrustedPresentationCallbackTest {
+    private static final String TAG = "TrustedPresentationCallbackTest";
+    private static final int STABILITY_REQUIREMENT_MS = 500;
+    private static final long WAIT_TIME_MS = HW_TIMEOUT_MULTIPLIER * 2000L;
+
+    private static final float FRACTION_VISIBLE = 0.1f;
+
+    private final Object mResultsLock = new Object();
+    @GuardedBy("mResultsLock")
+    private boolean mResult;
+    @GuardedBy("mResultsLock")
+    private boolean mReceivedResults;
+
+    @Rule
+    public TestName mName = new TestName();
+
+    @Rule
+    public ActivityScenarioRule<TestActivity> mActivityRule = createFullscreenActivityScenarioRule(
+            TestActivity.class);
+
+    private TestActivity mActivity;
+
+    @Before
+    public void setup() {
+        mActivityRule.getScenario().onActivity(activity -> mActivity = activity);
+    }
+
+    @After
+    public void tearDown() {
+        CommonUtils.waitUntilActivityRemoved(mActivity);
+    }
+
+    @Test
+    public void testAddTrustedPresentationListenerOnWindow() throws InterruptedException {
+        TrustedPresentationThresholds thresholds = new TrustedPresentationThresholds(
+                1 /* minAlpha */, FRACTION_VISIBLE, STABILITY_REQUIREMENT_MS);
+        SurfaceControl.Transaction t = new SurfaceControl.Transaction();
+        mActivity.getWindow().getRootSurfaceControl().addTrustedPresentationCallback(t, thresholds,
+                Runnable::run, inTrustedPresentationState -> {
+                    synchronized (mResultsLock) {
+                        mResult = inTrustedPresentationState;
+                        mReceivedResults = true;
+                        mResultsLock.notify();
+                    }
+                });
+        t.apply();
+        synchronized (mResultsLock) {
+            assertResults();
+        }
+    }
+
+    @Test
+    public void testRemoveTrustedPresentationListenerOnWindow() throws InterruptedException {
+        TrustedPresentationThresholds thresholds = new TrustedPresentationThresholds(
+                1 /* minAlpha */, FRACTION_VISIBLE, STABILITY_REQUIREMENT_MS);
+        Consumer<Boolean> trustedPresentationCallback = inTrustedPresentationState -> {
+            synchronized (mResultsLock) {
+                mResult = inTrustedPresentationState;
+                mReceivedResults = true;
+                mResultsLock.notify();
+            }
+        };
+        SurfaceControl.Transaction t = new SurfaceControl.Transaction();
+        mActivity.getWindow().getRootSurfaceControl().addTrustedPresentationCallback(t, thresholds,
+                Runnable::run, trustedPresentationCallback);
+        t.apply();
+
+        synchronized (mResultsLock) {
+            if (!mReceivedResults) {
+                mResultsLock.wait(WAIT_TIME_MS);
+            }
+            assertResults();
+            // reset the state
+            mReceivedResults = false;
+        }
+
+        mActivity.getWindow().getRootSurfaceControl().removeTrustedPresentationCallback(t,
+                trustedPresentationCallback);
+        t.apply();
+
+        synchronized (mResultsLock) {
+            if (!mReceivedResults) {
+                mResultsLock.wait(WAIT_TIME_MS);
+            }
+            // Ensure we waited the full time and never received a notify on the result from the
+            // callback.
+            assertFalse("Should never have received a callback", mReceivedResults);
+            // results shouldn't have changed.
+            assertTrue(mResult);
+        }
+    }
+
+    @GuardedBy("mResultsLock")
+    private void assertResults() throws InterruptedException {
+        mResultsLock.wait(WAIT_TIME_MS);
+
+        if (!mReceivedResults) {
+            CtsWindowInfoUtils.dumpWindowsOnScreen(TAG, "test " + mName.getMethodName());
+        }
+        // Make sure we received the results and not just timed out
+        assertTrue("Timed out waiting for results", mReceivedResults);
+        assertTrue(mResult);
+    }
+
+    public static class TestActivity extends Activity {
+    }
+}
diff --git a/services/tests/wmtests/src/com/android/server/wm/TrustedPresentationListenerTest.java b/services/tests/wmtests/src/com/android/server/wm/TrustedPresentationListenerTest.java
deleted file mode 100644
index 96b66bf..0000000
--- a/services/tests/wmtests/src/com/android/server/wm/TrustedPresentationListenerTest.java
+++ /dev/null
@@ -1,267 +0,0 @@
-/*
- * Copyright (C) 2023 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.wm;
-
-import static android.server.wm.ActivityManagerTestBase.createFullscreenActivityScenarioRule;
-import static android.server.wm.BuildUtils.HW_TIMEOUT_MULTIPLIER;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertThrows;
-import static org.junit.Assert.fail;
-
-import android.app.Activity;
-import android.os.SystemClock;
-import android.platform.test.annotations.Presubmit;
-import android.server.wm.CtsWindowInfoUtils;
-import android.util.AndroidRuntimeException;
-import android.util.Log;
-import android.view.SurfaceControl;
-import android.view.SurfaceControlViewHost;
-import android.view.View;
-import android.view.WindowManager;
-import android.window.TrustedPresentationThresholds;
-
-import androidx.annotation.GuardedBy;
-import androidx.annotation.NonNull;
-import androidx.test.ext.junit.rules.ActivityScenarioRule;
-
-import com.android.server.wm.utils.CommonUtils;
-
-import junit.framework.Assert;
-
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TestName;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.TimeUnit;
-import java.util.function.Consumer;
-
-/**
- * TODO (b/287076178): Move these tests to
- * {@link android.view.surfacecontrol.cts.TrustedPresentationListenerTest} when API is made public
- */
-@Presubmit
-public class TrustedPresentationListenerTest {
-    private static final String TAG = "TrustedPresentationListenerTest";
-    private static final int STABILITY_REQUIREMENT_MS = 500;
-    private static final long WAIT_TIME_MS = HW_TIMEOUT_MULTIPLIER * 2000L;
-
-    private static final float FRACTION_VISIBLE = 0.1f;
-
-    private final List<Boolean> mResults = Collections.synchronizedList(new ArrayList<>());
-    private CountDownLatch mReceivedResults = new CountDownLatch(1);
-
-    private TrustedPresentationThresholds mThresholds = new TrustedPresentationThresholds(
-            1 /* minAlpha */, FRACTION_VISIBLE, STABILITY_REQUIREMENT_MS);
-
-    @Rule
-    public TestName mName = new TestName();
-
-    @Rule
-    public ActivityScenarioRule<TestActivity> mActivityRule = createFullscreenActivityScenarioRule(
-            TestActivity.class);
-
-    private TestActivity mActivity;
-
-    private SurfaceControlViewHost.SurfacePackage mSurfacePackage = null;
-
-    @Before
-    public void setup() {
-        mActivityRule.getScenario().onActivity(activity -> mActivity = activity);
-        mDefaultListener = new Listener(mReceivedResults);
-    }
-
-    @After
-    public void tearDown() {
-        if (mSurfacePackage != null) {
-            new SurfaceControl.Transaction().remove(mSurfacePackage.getSurfaceControl()).apply(
-                    true);
-            mSurfacePackage.release();
-        }
-        CommonUtils.waitUntilActivityRemoved(mActivity);
-
-    }
-
-    private class Listener implements Consumer<Boolean> {
-        final CountDownLatch mLatch;
-
-        Listener(CountDownLatch latch) {
-            mLatch = latch;
-        }
-
-        @Override
-        public void accept(Boolean inTrustedPresentationState) {
-            Log.d(TAG, "onTrustedPresentationChanged " + inTrustedPresentationState);
-            mResults.add(inTrustedPresentationState);
-            mLatch.countDown();
-        }
-    }
-
-    private Consumer<Boolean> mDefaultListener;
-
-    @Test
-    public void testAddTrustedPresentationListenerOnWindow() {
-        WindowManager windowManager = mActivity.getSystemService(WindowManager.class);
-        windowManager.registerTrustedPresentationListener(
-                mActivity.getWindow().getDecorView().getWindowToken(), mThresholds, Runnable::run,
-                mDefaultListener);
-        assertResults(List.of(true));
-    }
-
-    @Test
-    public void testRemoveTrustedPresentationListenerOnWindow() throws InterruptedException {
-        WindowManager windowManager = mActivity.getSystemService(WindowManager.class);
-        windowManager.registerTrustedPresentationListener(
-                mActivity.getWindow().getDecorView().getWindowToken(), mThresholds, Runnable::run,
-                mDefaultListener);
-        assertResults(List.of(true));
-        // reset the latch
-        mReceivedResults = new CountDownLatch(1);
-
-        windowManager.unregisterTrustedPresentationListener(mDefaultListener);
-        mReceivedResults.await(WAIT_TIME_MS, TimeUnit.MILLISECONDS);
-        // Ensure we waited the full time and never received a notify on the result from the
-        // callback.
-        assertEquals("Should never have received a callback", mReceivedResults.getCount(), 1);
-        // results shouldn't have changed.
-        assertEquals(mResults, List.of(true));
-    }
-
-    @Test
-    public void testRemovingUnknownListenerIsANoop() {
-        WindowManager windowManager = mActivity.getSystemService(WindowManager.class);
-        assertNotNull(windowManager);
-        windowManager.unregisterTrustedPresentationListener(mDefaultListener);
-    }
-
-    @Test
-    public void testAddDuplicateListenerThrowsException() {
-        WindowManager windowManager = mActivity.getSystemService(WindowManager.class);
-        assertNotNull(windowManager);
-        windowManager.registerTrustedPresentationListener(
-                mActivity.getWindow().getDecorView().getWindowToken(), mThresholds,
-                Runnable::run, mDefaultListener);
-        assertThrows(AndroidRuntimeException.class,
-                () -> windowManager.registerTrustedPresentationListener(
-                        mActivity.getWindow().getDecorView().getWindowToken(), mThresholds,
-                        Runnable::run, mDefaultListener));
-    }
-
-    @Test
-    public void testAddDuplicateThresholds() {
-        mReceivedResults = new CountDownLatch(2);
-        mDefaultListener = new Listener(mReceivedResults);
-        WindowManager windowManager = mActivity.getSystemService(WindowManager.class);
-        windowManager.registerTrustedPresentationListener(
-                mActivity.getWindow().getDecorView().getWindowToken(), mThresholds,
-                Runnable::run, mDefaultListener);
-
-        Consumer<Boolean> mNewListener = new Listener(mReceivedResults);
-
-        windowManager.registerTrustedPresentationListener(
-                mActivity.getWindow().getDecorView().getWindowToken(), mThresholds,
-                Runnable::run, mNewListener);
-        assertResults(List.of(true, true));
-    }
-
-    private void waitForViewAttach(View view) {
-        final CountDownLatch viewAttached = new CountDownLatch(1);
-        view.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
-            @Override
-            public void onViewAttachedToWindow(@NonNull View v) {
-                viewAttached.countDown();
-            }
-
-            @Override
-            public void onViewDetachedFromWindow(@NonNull View v) {
-
-            }
-        });
-        try {
-            viewAttached.await(2000, TimeUnit.MILLISECONDS);
-        } catch (InterruptedException e) {
-            throw new RuntimeException(e);
-        }
-        if (!wait(viewAttached, 2000 /* waitTimeMs */)) {
-            fail("Couldn't attach view=" + view);
-        }
-    }
-
-    @Test
-    public void testAddListenerToScvh() {
-        WindowManager windowManager = mActivity.getSystemService(WindowManager.class);
-
-        var embeddedView = new View(mActivity);
-        mActivityRule.getScenario().onActivity(activity -> {
-            var attachedSurfaceControl =
-                    mActivity.getWindow().getDecorView().getRootSurfaceControl();
-            var scvh = new SurfaceControlViewHost(mActivity, mActivity.getDisplay(),
-                    attachedSurfaceControl.getHostToken());
-            mSurfacePackage = scvh.getSurfacePackage();
-            scvh.setView(embeddedView, mActivity.getWindow().getDecorView().getWidth(),
-                    mActivity.getWindow().getDecorView().getHeight());
-            attachedSurfaceControl.buildReparentTransaction(
-                    mSurfacePackage.getSurfaceControl());
-        });
-
-        waitForViewAttach(embeddedView);
-        windowManager.registerTrustedPresentationListener(embeddedView.getWindowToken(),
-                mThresholds,
-                Runnable::run, mDefaultListener);
-
-        assertResults(List.of(true));
-    }
-
-    private boolean wait(CountDownLatch latch, long waitTimeMs) {
-        while (true) {
-            long now = SystemClock.uptimeMillis();
-            try {
-                return latch.await(waitTimeMs, TimeUnit.MILLISECONDS);
-            } catch (InterruptedException e) {
-                long elapsedTime = SystemClock.uptimeMillis() - now;
-                waitTimeMs = Math.max(0, waitTimeMs - elapsedTime);
-            }
-        }
-
-    }
-
-    @GuardedBy("mResultsLock")
-    private void assertResults(List<Boolean> results) {
-        if (!wait(mReceivedResults, WAIT_TIME_MS)) {
-            try {
-                CtsWindowInfoUtils.dumpWindowsOnScreen(TAG, "test " + mName.getMethodName());
-            } catch (InterruptedException e) {
-                Log.d(TAG, "Couldn't dump windows", e);
-            }
-            Assert.fail("Timed out waiting for results mReceivedResults.count="
-                    + mReceivedResults.getCount() + "mReceivedResults=" + mReceivedResults);
-        }
-
-        // Make sure we received the results
-        assertEquals(results.toArray(), mResults.toArray());
-    }
-
-    public static class TestActivity extends Activity {
-    }
-}
diff --git a/services/usb/java/com/android/server/usb/UsbHostManager.java b/services/usb/java/com/android/server/usb/UsbHostManager.java
index b3eb285..8b44579 100644
--- a/services/usb/java/com/android/server/usb/UsbHostManager.java
+++ b/services/usb/java/com/android/server/usb/UsbHostManager.java
@@ -221,9 +221,7 @@
                 for (int line = 0; line < length / kDumpBytesPerLine; line++) {
                     StringBuilder sb = new StringBuilder();
                     for (int offset = 0; offset < kDumpBytesPerLine; offset++) {
-                        sb.append("0x")
-                            .append(String.format("0x%02X", mDescriptors[dataOffset++]))
-                            .append(" ");
+                        sb.append(String.format("0x%02X", mDescriptors[dataOffset++])).append(" ");
                     }
                     pw.println(sb.toString());
                 }
@@ -231,9 +229,7 @@
                 // remainder
                 StringBuilder sb = new StringBuilder();
                 while (dataOffset < length) {
-                    sb.append("0x")
-                        .append(String.format("0x%02X", mDescriptors[dataOffset++]))
-                        .append(" ");
+                    sb.append(String.format("0x%02X", mDescriptors[dataOffset++])).append(" ");
                 }
                 pw.println(sb.toString());
             } else {
diff --git a/tests/FlickerTests/ActivityEmbedding/src/com/android/server/wm/flicker/activityembedding/rotation/RotateSplitNoChangeTest.kt b/tests/FlickerTests/ActivityEmbedding/src/com/android/server/wm/flicker/activityembedding/rotation/RotateSplitNoChangeTest.kt
index e8389d19..0dce870 100644
--- a/tests/FlickerTests/ActivityEmbedding/src/com/android/server/wm/flicker/activityembedding/rotation/RotateSplitNoChangeTest.kt
+++ b/tests/FlickerTests/ActivityEmbedding/src/com/android/server/wm/flicker/activityembedding/rotation/RotateSplitNoChangeTest.kt
@@ -43,8 +43,6 @@
 @Parameterized.UseParametersRunnerFactory(FlickerParametersRunnerFactory::class)
 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
 open class RotateSplitNoChangeTest(flicker: LegacyFlickerTest) : RotationTransition(flicker) {
-
-    override val testApp = ActivityEmbeddingAppHelper(instrumentation)
     override val transition: FlickerBuilder.() -> Unit
         get() = {
             super.transition(this)
diff --git a/tests/FlickerTests/ActivityEmbedding/src/com/android/server/wm/flicker/activityembedding/rotation/RotationTransition.kt b/tests/FlickerTests/ActivityEmbedding/src/com/android/server/wm/flicker/activityembedding/rotation/RotationTransition.kt
index 1123c5b..f6d51f9 100644
--- a/tests/FlickerTests/ActivityEmbedding/src/com/android/server/wm/flicker/activityembedding/rotation/RotationTransition.kt
+++ b/tests/FlickerTests/ActivityEmbedding/src/com/android/server/wm/flicker/activityembedding/rotation/RotationTransition.kt
@@ -18,17 +18,14 @@
 
 import android.platform.test.annotations.Presubmit
 import android.tools.common.traces.component.ComponentNameMatcher
-import android.tools.device.apphelpers.StandardAppHelper
 import android.tools.device.flicker.legacy.FlickerBuilder
 import android.tools.device.flicker.legacy.LegacyFlickerTest
-import com.android.server.wm.flicker.BaseTest
+import com.android.server.wm.flicker.activityembedding.ActivityEmbeddingTestBase
 import com.android.server.wm.flicker.helpers.setRotation
 import org.junit.Test
 
 /** Base class for app rotation tests */
-abstract class RotationTransition(flicker: LegacyFlickerTest) : BaseTest(flicker) {
-    protected abstract val testApp: StandardAppHelper
-
+abstract class RotationTransition(flicker: LegacyFlickerTest) : ActivityEmbeddingTestBase(flicker) {
     /** {@inheritDoc} */
     override val transition: FlickerBuilder.() -> Unit = {
         setup { this.setRotation(flicker.scenario.startRotation) }
diff --git a/tests/FlickerTests/src/com/android/server/wm/flicker/BaseTest.kt b/tests/FlickerTests/src/com/android/server/wm/flicker/BaseTest.kt
index ad272a0..ce92eac 100644
--- a/tests/FlickerTests/src/com/android/server/wm/flicker/BaseTest.kt
+++ b/tests/FlickerTests/src/com/android/server/wm/flicker/BaseTest.kt
@@ -40,10 +40,9 @@
 constructor(
     protected val flicker: LegacyFlickerTest,
     protected val instrumentation: Instrumentation = InstrumentationRegistry.getInstrumentation(),
-    protected val tapl: LauncherInstrumentation = LauncherInstrumentation()
 ) {
-    init {
-        tapl.setExpectedRotationCheckEnabled(true)
+    protected val tapl: LauncherInstrumentation by lazy {
+        LauncherInstrumentation().also { it.expectedRotationCheckEnabled = true }
     }
 
     private val logTag = this::class.java.simpleName
diff --git a/tests/InputScreenshotTest/robotests/assets/phone/light_landscape_layout-preview.png b/tests/InputScreenshotTest/robotests/assets/phone/light_landscape_layout-preview.png
index baf204a..a117599d 100644
--- a/tests/InputScreenshotTest/robotests/assets/phone/light_landscape_layout-preview.png
+++ b/tests/InputScreenshotTest/robotests/assets/phone/light_landscape_layout-preview.png
Binary files differ
diff --git a/tests/InputScreenshotTest/robotests/assets/phone/light_portrait_layout-preview.png b/tests/InputScreenshotTest/robotests/assets/phone/light_portrait_layout-preview.png
index deb3cee..538abe8 100644
--- a/tests/InputScreenshotTest/robotests/assets/phone/light_portrait_layout-preview.png
+++ b/tests/InputScreenshotTest/robotests/assets/phone/light_portrait_layout-preview.png
Binary files differ
diff --git a/tests/InputScreenshotTest/robotests/assets/tablet/dark_portrait_layout-preview.png b/tests/InputScreenshotTest/robotests/assets/tablet/dark_portrait_layout-preview.png
index 34e25f7..79a1d6b 100644
--- a/tests/InputScreenshotTest/robotests/assets/tablet/dark_portrait_layout-preview.png
+++ b/tests/InputScreenshotTest/robotests/assets/tablet/dark_portrait_layout-preview.png
Binary files differ