Merge "Correctly support restoring SIM specific settings" into main
diff --git a/apct-tests/perftests/core/src/android/os/OWNERS b/apct-tests/perftests/core/src/android/os/OWNERS
index a1719c9..76ab303 100644
--- a/apct-tests/perftests/core/src/android/os/OWNERS
+++ b/apct-tests/perftests/core/src/android/os/OWNERS
@@ -1 +1,4 @@
-per-file PackageParsingPerfTest.kt = file:/services/core/java/com/android/server/pm/OWNERS
\ No newline at end of file
+per-file PackageParsingPerfTest.kt = file:/services/core/java/com/android/server/pm/OWNERS
+
+# Bug component: 345036
+per-file VibratorPerfTest.java = file:/services/core/java/com/android/server/vibrator/OWNERS
\ No newline at end of file
diff --git a/api/Android.bp b/api/Android.bp
index f40f0c9..6aef5ff 100644
--- a/api/Android.bp
+++ b/api/Android.bp
@@ -60,14 +60,14 @@
 metalava_cmd = "$(location metalava)"
 // Silence reflection warnings. See b/168689341
 metalava_cmd += " -J--add-opens=java.base/java.util=ALL-UNNAMED "
-metalava_cmd += " --quiet --no-banner --format=v2 "
+metalava_cmd += " --quiet "
 
 genrule {
     name: "current-api-xml",
     tools: ["metalava"],
     srcs: [":frameworks-base-api-current.txt"],
     out: ["current.api"],
-    cmd: metalava_cmd + "-convert2xmlnostrip $(in) $(out)",
+    cmd: metalava_cmd + "signature-to-jdiff $(in) $(out)",
     visibility: ["//visibility:public"],
 }
 
diff --git a/core/api/current.txt b/core/api/current.txt
index 44865b5..abf3a2f0 100644
--- a/core/api/current.txt
+++ b/core/api/current.txt
@@ -12037,6 +12037,7 @@
     field public static final String FEATURE_TELEPHONY_RADIO_ACCESS = "android.hardware.telephony.radio.access";
     field public static final String FEATURE_TELEPHONY_SUBSCRIPTION = "android.hardware.telephony.subscription";
     field @Deprecated public static final String FEATURE_TELEVISION = "android.hardware.type.television";
+    field public static final String FEATURE_THREADNETWORK = "android.hardware.threadnetwork";
     field public static final String FEATURE_TOUCHSCREEN = "android.hardware.touchscreen";
     field public static final String FEATURE_TOUCHSCREEN_MULTITOUCH = "android.hardware.touchscreen.multitouch";
     field public static final String FEATURE_TOUCHSCREEN_MULTITOUCH_DISTINCT = "android.hardware.touchscreen.multitouch.distinct";
diff --git a/core/java/android/app/ActivityOptions.java b/core/java/android/app/ActivityOptions.java
index 53e0a05..6706091 100644
--- a/core/java/android/app/ActivityOptions.java
+++ b/core/java/android/app/ActivityOptions.java
@@ -21,6 +21,8 @@
 import static android.Manifest.permission.START_TASKS_FROM_RECENTS;
 import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
 import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
+import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
+import static android.content.Intent.FLAG_RECEIVER_FOREGROUND;
 import static android.view.Display.INVALID_DISPLAY;
 import static android.window.DisplayAreaOrganizer.FEATURE_UNDEFINED;
 
@@ -1756,7 +1758,9 @@
      * @hide
      */
     public int getPendingIntentLaunchFlags() {
-        return mPendingIntentLaunchFlags;
+        // b/243794108: Ignore all flags except the new task flag, to be reconsidered in b/254490217
+        return mPendingIntentLaunchFlags &
+                (FLAG_ACTIVITY_NEW_TASK | FLAG_RECEIVER_FOREGROUND);
     }
 
     /**
diff --git a/core/java/android/app/Notification.java b/core/java/android/app/Notification.java
index 90e3e9d..a4a3fdb 100644
--- a/core/java/android/app/Notification.java
+++ b/core/java/android/app/Notification.java
@@ -2857,6 +2857,17 @@
             if (person != null) {
                 visitor.accept(person.getIconUri());
             }
+
+            final RemoteInputHistoryItem[] history = (RemoteInputHistoryItem[])
+                    extras.getParcelableArray(Notification.EXTRA_REMOTE_INPUT_HISTORY_ITEMS);
+            if (history != null) {
+                for (int i = 0; i < history.length; i++) {
+                    RemoteInputHistoryItem item = history[i];
+                    if (item.getUri() != null) {
+                        visitor.accept(item.getUri());
+                    }
+                }
+            }
         }
 
         if (isStyle(MessagingStyle.class) && extras != null) {
@@ -2887,6 +2898,14 @@
             }
         }
 
+        if (isStyle(CallStyle.class) & extras != null) {
+            Person callPerson = extras.getParcelable(EXTRA_CALL_PERSON);
+            if (callPerson != null) {
+                visitor.accept(callPerson.getIconUri());
+            }
+            visitIconUri(visitor, extras.getParcelable(EXTRA_VERIFICATION_ICON));
+        }
+
         if (mBubbleMetadata != null) {
             visitIconUri(visitor, mBubbleMetadata.getIcon());
         }
diff --git a/core/java/android/app/OWNERS b/core/java/android/app/OWNERS
index 5894803..1e5a5fb 100644
--- a/core/java/android/app/OWNERS
+++ b/core/java/android/app/OWNERS
@@ -63,10 +63,11 @@
 # Multiuser
 per-file *User* = file:/MULTIUSER_OWNERS
 
-# Notification, DND, Status bar
+# Notification, DND, Status bar, UiModeManager
 per-file *Notification* = file:/packages/SystemUI/OWNERS
 per-file *Zen* = file:/packages/SystemUI/OWNERS
 per-file *StatusBar* = file:/packages/SystemUI/OWNERS
+per-file *UiModeManager* = file:/packages/SystemUI/OWNERS
 
 # PackageManager
 per-file ApplicationPackageManager.java = file:/services/core/java/com/android/server/pm/OWNERS
diff --git a/core/java/android/content/om/IOverlayManager.aidl b/core/java/android/content/om/IOverlayManager.aidl
index a0f3d7a..122ab48 100644
--- a/core/java/android/content/om/IOverlayManager.aidl
+++ b/core/java/android/content/om/IOverlayManager.aidl
@@ -190,4 +190,15 @@
      * @throws SecurityException if the transaction failed
      */
     void commit(in OverlayManagerTransaction transaction);
+
+    /**
+     * Returns a String of a list of partitions from low priority to high.
+     */
+    String getPartitionOrder();
+
+    /**
+     * Returns a boolean which represent whether the partition list is sorted by default.
+     * If not then it should be sorted by /product/overlay/partition_order.xml.
+     */
+    boolean isDefaultPartitionOrder();
 }
diff --git a/core/java/android/content/pm/PackageManager.java b/core/java/android/content/pm/PackageManager.java
index 4677026..d738d9e 100644
--- a/core/java/android/content/pm/PackageManager.java
+++ b/core/java/android/content/pm/PackageManager.java
@@ -3448,6 +3448,14 @@
     /**
      * Feature for {@link #getSystemAvailableFeatures} and
      * {@link #hasSystemFeature}: The device is capable of communicating with
+     * other devices via Thread network.
+     */
+    @SdkConstant(SdkConstantType.FEATURE)
+    public static final String FEATURE_THREADNETWORK = "android.hardware.threadnetwork";
+
+    /**
+     * Feature for {@link #getSystemAvailableFeatures} and
+     * {@link #hasSystemFeature}: The device is capable of communicating with
      * other devices via ultra wideband.
      */
     @SdkConstant(SdkConstantType.FEATURE)
diff --git a/core/java/android/content/pm/ShortcutInfo.java b/core/java/android/content/pm/ShortcutInfo.java
index 295df5c..b85ea36 100644
--- a/core/java/android/content/pm/ShortcutInfo.java
+++ b/core/java/android/content/pm/ShortcutInfo.java
@@ -285,6 +285,12 @@
      */
     public static final int DISABLED_REASON_OTHER_RESTORE_ISSUE = 103;
 
+    /**
+     * The maximum length of Shortcut ID. IDs will be truncated at this limit.
+     * @hide
+     */
+    public static final int MAX_ID_LENGTH = 1000;
+
     /** @hide */
     @IntDef(prefix = { "DISABLED_REASON_" }, value = {
             DISABLED_REASON_NOT_DISABLED,
@@ -477,8 +483,7 @@
 
     private ShortcutInfo(Builder b) {
         mUserId = b.mContext.getUserId();
-
-        mId = Preconditions.checkStringNotEmpty(b.mId, "Shortcut ID must be provided");
+        mId = getSafeId(Preconditions.checkStringNotEmpty(b.mId, "Shortcut ID must be provided"));
 
         // Note we can't do other null checks here because SM.updateShortcuts() takes partial
         // information.
@@ -584,6 +589,14 @@
         return ret;
     }
 
+    @NonNull
+    private static String getSafeId(@NonNull String id) {
+        if (id.length() > MAX_ID_LENGTH) {
+            return id.substring(0, MAX_ID_LENGTH);
+        }
+        return id;
+    }
+
     /**
      * Throws if any of the mandatory fields is not set.
      *
@@ -2342,7 +2355,8 @@
         final ClassLoader cl = getClass().getClassLoader();
 
         mUserId = source.readInt();
-        mId = source.readString8();
+        mId = getSafeId(Preconditions.checkStringNotEmpty(source.readString8(),
+                "Shortcut ID must be provided"));
         mPackageName = source.readString8();
         mActivity = source.readParcelable(cl, android.content.ComponentName.class);
         mFlags = source.readInt();
diff --git a/core/java/android/database/OWNERS b/core/java/android/database/OWNERS
index 7e19942..53f5bb0 100644
--- a/core/java/android/database/OWNERS
+++ b/core/java/android/database/OWNERS
@@ -1,3 +1,6 @@
+include /SQLITE_OWNERS
+
 omakoto@google.com
 jsharkey@android.com
 yamasani@google.com
+
diff --git a/core/java/android/nfc/cardemulation/CardEmulation.java b/core/java/android/nfc/cardemulation/CardEmulation.java
index ac3344e..4909b08 100644
--- a/core/java/android/nfc/cardemulation/CardEmulation.java
+++ b/core/java/android/nfc/cardemulation/CardEmulation.java
@@ -594,9 +594,6 @@
         if (activity == null || service == null) {
             throw new NullPointerException("activity or service or category is null");
         }
-        if (!activity.isResumed()) {
-            throw new IllegalArgumentException("Activity must be resumed.");
-        }
         try {
             return sService.setPreferredService(service);
         } catch (RemoteException e) {
@@ -629,9 +626,6 @@
         if (activity == null) {
             throw new NullPointerException("activity is null");
         }
-        if (!activity.isResumed()) {
-            throw new IllegalArgumentException("Activity must be resumed.");
-        }
         try {
             return sService.unsetPreferredService();
         } catch (RemoteException e) {
diff --git a/core/java/android/os/Binder.java b/core/java/android/os/Binder.java
index 5d0a723..3f04ca4 100644
--- a/core/java/android/os/Binder.java
+++ b/core/java/android/os/Binder.java
@@ -943,16 +943,19 @@
      * @hide
      */
     @VisibleForTesting
-    public final @NonNull String getTransactionTraceName(int transactionCode) {
+    public final @Nullable String getTransactionTraceName(int transactionCode) {
+        final boolean isInterfaceUserDefined = getMaxTransactionId() == 0;
         if (mTransactionTraceNames == null) {
-            final int highestId = Math.min(getMaxTransactionId(), TRANSACTION_TRACE_NAME_ID_LIMIT);
+            final int highestId = isInterfaceUserDefined ? TRANSACTION_TRACE_NAME_ID_LIMIT
+                    : Math.min(getMaxTransactionId(), TRANSACTION_TRACE_NAME_ID_LIMIT);
             mSimpleDescriptor = getSimpleDescriptor();
             mTransactionTraceNames = new AtomicReferenceArray(highestId + 1);
         }
 
-        final int index = transactionCode - FIRST_CALL_TRANSACTION;
-        if (index < 0 || index >= mTransactionTraceNames.length()) {
-            return mSimpleDescriptor + "#" + transactionCode;
+        final int index = isInterfaceUserDefined
+                ? transactionCode : transactionCode - FIRST_CALL_TRANSACTION;
+        if (index >= mTransactionTraceNames.length() || index < 0) {
+            return null;
         }
 
         String transactionTraceName = mTransactionTraceNames.getAcquire(index);
@@ -1317,19 +1320,9 @@
         final boolean hasFullyQualifiedName = getMaxTransactionId() > 0;
         final String transactionTraceName;
 
-        if (tagEnabled && hasFullyQualifiedName) {
+        if (tagEnabled) {
             // If tracing enabled and we have a fully qualified name, fetch the name
             transactionTraceName = getTransactionTraceName(code);
-        } else if (tagEnabled && isStackTrackingEnabled()) {
-            // If tracing is enabled and we *don't* have a fully qualified name, fetch the
-            // 'best effort' name only for stack tracking. This works around noticeable perf impact
-            // on low latency binder calls (<100us). The tracing call itself is between (1-10us) and
-            // the perf impact can be quite noticeable while benchmarking such binder calls.
-            // The primary culprits are ContentProviders and Cursors which convenienty don't
-            // autogenerate their AIDL and hence will not have a fully qualified name.
-            //
-            // TODO(b/253426478): Relax this constraint after a more robust fix
-            transactionTraceName = getTransactionTraceName(code);
         } else {
             transactionTraceName = null;
         }
diff --git a/core/java/android/os/Handler.java b/core/java/android/os/Handler.java
index 834867c..55b7a7d 100644
--- a/core/java/android/os/Handler.java
+++ b/core/java/android/os/Handler.java
@@ -119,7 +119,7 @@
      *   crashes (if a handler is sometimes created on a thread without a Looper active), or race
      *   conditions, where the thread a handler is associated with is not what the author
      *   anticipated. Instead, use an {@link java.util.concurrent.Executor} or specify the Looper
-     *   explicitly, using {@link Looper#getMainLooper}, {link android.view.View#getHandler}, or
+     *   explicitly, using {@link Looper#getMainLooper}, {@link android.view.View#getHandler}, or
      *   similar. If the implicit thread local behavior is required for compatibility, use
      *   {@code new Handler(Looper.myLooper())} to make it clear to readers.
      *
@@ -144,7 +144,7 @@
      *   crashes (if a handler is sometimes created on a thread without a Looper active), or race
      *   conditions, where the thread a handler is associated with is not what the author
      *   anticipated. Instead, use an {@link java.util.concurrent.Executor} or specify the Looper
-     *   explicitly, using {@link Looper#getMainLooper}, {link android.view.View#getHandler}, or
+     *   explicitly, using {@link Looper#getMainLooper}, {@link android.view.View#getHandler}, or
      *   similar. If the implicit thread local behavior is required for compatibility, use
      *   {@code new Handler(Looper.myLooper(), callback)} to make it clear to readers.
      */
diff --git a/core/java/android/permission/IPermissionManager.aidl b/core/java/android/permission/IPermissionManager.aidl
index 6a93b35..45dad98 100644
--- a/core/java/android/permission/IPermissionManager.aidl
+++ b/core/java/android/permission/IPermissionManager.aidl
@@ -77,8 +77,7 @@
     List<SplitPermissionInfoParcelable> getSplitPermissions();
 
     void startOneTimePermissionSession(String packageName, int userId, long timeout,
-            long revokeAfterKilledDelay, int importanceToResetTimer,
-            int importanceToKeepSessionAlive);
+            long revokeAfterKilledDelay);
 
     void stopOneTimePermissionSession(String packageName, int userId);
 
diff --git a/core/java/android/permission/PermissionManager.java b/core/java/android/permission/PermissionManager.java
index 6b540d7..6769954 100644
--- a/core/java/android/permission/PermissionManager.java
+++ b/core/java/android/permission/PermissionManager.java
@@ -1371,8 +1371,7 @@
             @ActivityManager.RunningAppProcessInfo.Importance int importanceToKeepSessionAlive) {
         try {
             mPermissionManager.startOneTimePermissionSession(packageName, mContext.getUserId(),
-                    timeoutMillis, revokeAfterKilledDelayMillis, importanceToResetTimer,
-                    importanceToKeepSessionAlive);
+                    timeoutMillis, revokeAfterKilledDelayMillis);
         } catch (RemoteException e) {
             e.rethrowFromSystemServer();
         }
diff --git a/core/java/android/print/OWNERS b/core/java/android/print/OWNERS
index 28a24203..0809de2 100644
--- a/core/java/android/print/OWNERS
+++ b/core/java/android/print/OWNERS
@@ -1,4 +1,4 @@
 # Bug component: 47273
 
-svetoslavganov@android.com
-svetoslavganov@google.com
+anothermark@google.com
+kumarashishg@google.com
diff --git a/core/java/android/print/pdf/OWNERS b/core/java/android/print/pdf/OWNERS
deleted file mode 100644
index 28a24203..0000000
--- a/core/java/android/print/pdf/OWNERS
+++ /dev/null
@@ -1,4 +0,0 @@
-# Bug component: 47273
-
-svetoslavganov@android.com
-svetoslavganov@google.com
diff --git a/core/java/android/printservice/OWNERS b/core/java/android/printservice/OWNERS
index 28a24203..0809de2 100644
--- a/core/java/android/printservice/OWNERS
+++ b/core/java/android/printservice/OWNERS
@@ -1,4 +1,4 @@
 # Bug component: 47273
 
-svetoslavganov@android.com
-svetoslavganov@google.com
+anothermark@google.com
+kumarashishg@google.com
diff --git a/core/java/android/printservice/recommendation/OWNERS b/core/java/android/printservice/recommendation/OWNERS
deleted file mode 100644
index 28a24203..0000000
--- a/core/java/android/printservice/recommendation/OWNERS
+++ /dev/null
@@ -1,4 +0,0 @@
-# Bug component: 47273
-
-svetoslavganov@android.com
-svetoslavganov@google.com
diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java
index 483c39d..f50dc8d 100644
--- a/core/java/android/view/View.java
+++ b/core/java/android/view/View.java
@@ -21206,6 +21206,8 @@
         mCurrentAnimation = null;
 
         if ((mViewFlags & TOOLTIP) == TOOLTIP) {
+            removeCallbacks(mTooltipInfo.mShowTooltipRunnable);
+            removeCallbacks(mTooltipInfo.mHideTooltipRunnable);
             hideTooltip();
         }
 
diff --git a/core/java/android/view/WindowManager.java b/core/java/android/view/WindowManager.java
index 033f726..d67a476 100644
--- a/core/java/android/view/WindowManager.java
+++ b/core/java/android/view/WindowManager.java
@@ -877,7 +877,7 @@
      * &lt;application&gt;
      *   &lt;property
      *     android:name=
-     *       "android.window.PROPERTY_COMPAT_IGNORE_ORIENTATION_REQUEST_WHEN_LOOP_DETECTED"
+     *       "android.window.PROPERTY_COMPAT_ALLOW_IGNORING_ORIENTATION_REQUEST_WHEN_LOOP_DETECTED"
      *     android:value="false"/&gt;
      * &lt;/application&gt;
      * </pre>
@@ -885,8 +885,8 @@
      * @hide
      */
     // TODO(b/274924641): Make this public API.
-    String PROPERTY_COMPAT_IGNORE_ORIENTATION_REQUEST_WHEN_LOOP_DETECTED =
-            "android.window.PROPERTY_COMPAT_IGNORE_ORIENTATION_REQUEST_WHEN_LOOP_DETECTED";
+    String PROPERTY_COMPAT_ALLOW_IGNORING_ORIENTATION_REQUEST_WHEN_LOOP_DETECTED =
+            "android.window.PROPERTY_COMPAT_ALLOW_IGNORING_ORIENTATION_REQUEST_WHEN_LOOP_DETECTED";
 
     /**
      * Application level {@link android.content.pm.PackageManager.Property PackageManager
diff --git a/core/java/android/widget/RemoteViews.java b/core/java/android/widget/RemoteViews.java
index a339062..c985ae8 100644
--- a/core/java/android/widget/RemoteViews.java
+++ b/core/java/android/widget/RemoteViews.java
@@ -726,6 +726,12 @@
                 mActions.get(i).visitUris(visitor);
             }
         }
+        if (mLandscape != null) {
+            mLandscape.visitUris(visitor);
+        }
+        if (mPortrait != null) {
+            mPortrait.visitUris(visitor);
+        }
     }
 
     private static void visitIconUri(Icon icon, @NonNull Consumer<Uri> visitor) {
diff --git a/core/java/com/android/internal/content/om/OverlayConfig.java b/core/java/com/android/internal/content/om/OverlayConfig.java
index fc0943b..e8bdd1d 100644
--- a/core/java/com/android/internal/content/om/OverlayConfig.java
+++ b/core/java/com/android/internal/content/om/OverlayConfig.java
@@ -34,8 +34,15 @@
 import com.android.internal.util.Preconditions;
 import com.android.internal.util.function.TriConsumer;
 
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
+
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -46,6 +53,10 @@
 import java.util.Map;
 import java.util.function.Supplier;
 
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
 /**
  * Responsible for reading overlay configuration files and handling queries of overlay mutability,
  * default-enabled state, and priority.
@@ -61,6 +72,8 @@
     @VisibleForTesting
     public static final int DEFAULT_PRIORITY = Integer.MAX_VALUE;
 
+    public static final String PARTITION_ORDER_FILE_PATH = "/product/overlay/partition_order.xml";
+
     @VisibleForTesting
     public static final class Configuration {
         @Nullable
@@ -119,6 +132,10 @@
     // Singleton instance only assigned in system server
     private static OverlayConfig sInstance;
 
+    private final String mPartitionOrder;
+
+    private final boolean mIsDefaultPartitionOrder;
+
     @VisibleForTesting
     public OverlayConfig(@Nullable File rootDirectory,
             @Nullable Supplier<OverlayScanner> scannerFactory,
@@ -137,6 +154,8 @@
                             new File(rootDirectory, p.getNonConicalFolder().getPath()),
                             p)));
         }
+        mIsDefaultPartitionOrder = !sortPartitions(PARTITION_ORDER_FILE_PATH, partitions);
+        mPartitionOrder = generatePartitionOrderString(partitions);
 
         ArrayMap<Integer, List<String>> activeApexesPerPartition = getActiveApexes(partitions);
 
@@ -198,6 +217,96 @@
         }
     }
 
+    private static String generatePartitionOrderString(List<OverlayPartition> partitions) {
+        if (partitions == null || partitions.size() == 0) {
+            return "";
+        }
+        StringBuilder partitionOrder = new StringBuilder();
+        partitionOrder.append(partitions.get(0).getName());
+        for (int i = 1; i < partitions.size(); i++) {
+            partitionOrder.append(", ").append(partitions.get(i).getName());
+        }
+        return partitionOrder.toString();
+    }
+
+    private static boolean parseAndValidatePartitionsOrderXml(String partitionOrderFilePath,
+            Map<String, Integer> orderMap, List<OverlayPartition> partitions) {
+        try {
+            File file = new File(partitionOrderFilePath);
+            if (!file.exists()) {
+                Log.w(TAG, "partition_order.xml does not exist.");
+                return false;
+            }
+            var dbFactory = DocumentBuilderFactory.newInstance();
+            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
+            Document doc = dBuilder.parse(file);
+            doc.getDocumentElement().normalize();
+
+            Element root = doc.getDocumentElement();
+            if (!root.getNodeName().equals("partition-order")) {
+                Log.w(TAG, "Invalid partition_order.xml, "
+                        + "xml root element is not partition-order");
+                return false;
+            }
+
+            NodeList partitionList = doc.getElementsByTagName("partition");
+            for (int order = 0; order < partitionList.getLength(); order++) {
+                Node partitionNode = partitionList.item(order);
+                if (partitionNode.getNodeType() == Node.ELEMENT_NODE) {
+                    Element partitionElement = (Element) partitionNode;
+                    String partitionName = partitionElement.getAttribute("name");
+                    if (orderMap.containsKey(partitionName)) {
+                        Log.w(TAG, "Invalid partition_order.xml, "
+                                + "it has duplicate partition: " + partitionName);
+                        return false;
+                    }
+                    orderMap.put(partitionName, order);
+                }
+            }
+
+            if (orderMap.keySet().size() != partitions.size()) {
+                Log.w(TAG, "Invalid partition_order.xml, partition_order.xml has "
+                        + orderMap.keySet().size() + " partitions, "
+                        + "which is different from SYSTEM_PARTITIONS");
+                return false;
+            }
+            for (int i = 0; i < partitions.size(); i++) {
+                if (!orderMap.keySet().contains(partitions.get(i).getName())) {
+                    Log.w(TAG, "Invalid Parsing partition_order.xml, "
+                            + "partition_order.xml does not have partition: "
+                            + partitions.get(i).getName());
+                    return false;
+                }
+            }
+        } catch (ParserConfigurationException | SAXException | IOException e) {
+            Log.w(TAG, "Parsing or validating partition_order.xml failed, "
+                    + "exception thrown: " + e.getMessage());
+            return false;
+        }
+        Log.i(TAG, "Sorting partitions in the specified order from partitions_order.xml");
+        return true;
+    }
+
+    /**
+     * Sort partitions by order in partition_order.xml if the file exists.
+     *
+     * @hide
+     */
+    @VisibleForTesting
+    public static boolean sortPartitions(String partitionOrderFilePath,
+            List<OverlayPartition> partitions) {
+        Map<String, Integer> orderMap = new HashMap<>();
+        if (!parseAndValidatePartitionsOrderXml(partitionOrderFilePath, orderMap, partitions)) {
+            return false;
+        }
+
+        Comparator<OverlayPartition> partitionComparator = Comparator.comparingInt(
+                o -> orderMap.get(o.getName()));
+        Collections.sort(partitions, partitionComparator);
+
+        return true;
+    }
+
     /**
      * Creates an instance of OverlayConfig for use in the zygote process.
      * This instance will not include information of static overlays existing outside of a partition
@@ -476,4 +585,19 @@
      */
     private static native String[] createIdmap(@NonNull String targetPath,
             @NonNull String[] overlayPath, @NonNull String[] policies, boolean enforceOverlayable);
+
+    /**
+     * @hide
+     */
+    public boolean isDefaultPartitionOrder() {
+        return mIsDefaultPartitionOrder;
+    }
+
+    /**
+     * @hide
+     */
+    public String getPartitionOrder() {
+        return mPartitionOrder;
+    }
+
 }
diff --git a/core/java/com/android/internal/content/om/OverlayConfigParser.java b/core/java/com/android/internal/content/om/OverlayConfigParser.java
index 0ab7b3d..5a86b93 100644
--- a/core/java/com/android/internal/content/om/OverlayConfigParser.java
+++ b/core/java/com/android/internal/content/om/OverlayConfigParser.java
@@ -27,6 +27,7 @@
 import android.util.Log;
 import android.util.Xml;
 
+import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.content.om.OverlayScanner.ParsedOverlayInfo;
 import com.android.internal.util.Preconditions;
 import com.android.internal.util.XmlUtils;
@@ -53,8 +54,11 @@
  *
  * @see #parseOverlay(File, XmlPullParser, OverlayScanner, ParsingContext)
  * @see #parseMerge(File, XmlPullParser, OverlayScanner, ParsingContext)
+ *
+ * @hide
  **/
-final class OverlayConfigParser {
+@VisibleForTesting
+public final class OverlayConfigParser {
 
     // Default values for overlay configurations.
     static final boolean DEFAULT_ENABLED_STATE = false;
@@ -115,7 +119,11 @@
         }
     }
 
-    static class OverlayPartition extends SystemPartition {
+    /**
+     * @hide
+     **/
+    @VisibleForTesting
+    public static class OverlayPartition extends SystemPartition {
         // Policies passed to idmap2 during idmap creation.
         // Keep partition policy constants in sync with f/b/cmds/idmap2/include/idmap2/Policies.h.
         static final String POLICY_ODM = "odm";
@@ -128,7 +136,11 @@
         @NonNull
         public final String policy;
 
-        OverlayPartition(@NonNull SystemPartition partition) {
+        /**
+         * @hide
+         **/
+        @VisibleForTesting
+        public OverlayPartition(@NonNull SystemPartition partition) {
             super(partition);
             this.policy = policyForPartition(partition);
         }
diff --git a/core/java/com/android/internal/os/Zygote.java b/core/java/com/android/internal/os/Zygote.java
index d8aeb51..0786d6f 100644
--- a/core/java/com/android/internal/os/Zygote.java
+++ b/core/java/com/android/internal/os/Zygote.java
@@ -195,6 +195,11 @@
      */
     public static final int PROFILEABLE = 1 << 24;
 
+    /**
+     * Enable ptrace.  This is enabled on eng or userdebug builds, or if the app is debuggable.
+     */
+    public static final int DEBUG_ENABLE_PTRACE = 1 << 25;
+
     /** No external storage should be mounted. */
     public static final int MOUNT_EXTERNAL_NONE = IVold.REMOUNT_MODE_NONE;
     /** Default external storage should be mounted. */
@@ -1028,6 +1033,9 @@
         if (Build.IS_ENG || ENABLE_JDWP) {
             args.mRuntimeFlags |= Zygote.DEBUG_ENABLE_JDWP;
         }
+        if (RoSystemProperties.DEBUGGABLE) {
+            args.mRuntimeFlags |= Zygote.DEBUG_ENABLE_PTRACE;
+        }
     }
 
     /**
diff --git a/core/java/com/android/internal/widget/OWNERS b/core/java/com/android/internal/widget/OWNERS
index d068a3a..e2672f5 100644
--- a/core/java/com/android/internal/widget/OWNERS
+++ b/core/java/com/android/internal/widget/OWNERS
@@ -21,3 +21,6 @@
 per-file ObservableTextView.java = file:/services/core/java/com/android/server/notification/OWNERS
 per-file RemeasuringLinearLayout.java = file:/services/core/java/com/android/server/notification/OWNERS
 per-file ViewClippingUtil.java = file:/services/core/java/com/android/server/notification/OWNERS
+
+# Appwidget related
+per-file *RemoteViews* = file:/services/appwidget/java/com/android/server/appwidget/OWNERS
diff --git a/core/java/com/android/server/SystemConfig.java b/core/java/com/android/server/SystemConfig.java
index 088c001..21da321 100644
--- a/core/java/com/android/server/SystemConfig.java
+++ b/core/java/com/android/server/SystemConfig.java
@@ -1848,6 +1848,9 @@
                         soname, soname, new String[0], true);
                 mSharedLibraries.put(entry.name, entry);
             }
+        } catch (FileNotFoundException e) {
+            // Expected for /vendor/etc/public.libraries.txt on some devices
+            Slog.d(TAG, listFile + " does not exist");
         } catch (IOException e) {
             Slog.w(TAG, "Failed to read public libraries file " + listFile, e);
         }
diff --git a/core/jni/android_media_AudioSystem.cpp b/core/jni/android_media_AudioSystem.cpp
index 56c2a86..b7a30e5 100644
--- a/core/jni/android_media_AudioSystem.cpp
+++ b/core/jni/android_media_AudioSystem.cpp
@@ -29,6 +29,8 @@
 #include <media/AudioSystem.h>
 #include <nativehelper/JNIHelp.h>
 #include <nativehelper/ScopedLocalRef.h>
+#include <nativehelper/ScopedPrimitiveArray.h>
+#include <nativehelper/jni_macros.h>
 #include <system/audio.h>
 #include <system/audio_policy.h>
 #include <utils/Log.h>
@@ -265,7 +267,7 @@
         ALOGE("Can't find class %s", kEventHandlerClassPathName);
         return;
     }
-    mClass = (jclass)env->NewGlobalRef(clazz);
+    mClass = static_cast<jclass>(env->NewGlobalRef(clazz));
 
     // We use a weak reference so the AudioPortEventHandler object can be garbage collected.
     // The reference is only used as a proxy for callbacks.
@@ -317,15 +319,16 @@
                                        const sp<JNIAudioPortCallback>& callback)
 {
     Mutex::Autolock l(gLock);
-    sp<JNIAudioPortCallback> old =
-            (JNIAudioPortCallback*)env->GetLongField(thiz, gEventHandlerFields.mJniCallback);
+    sp<JNIAudioPortCallback> old = reinterpret_cast<JNIAudioPortCallback *>(
+            env->GetLongField(thiz, gEventHandlerFields.mJniCallback));
     if (callback.get()) {
-        callback->incStrong((void*)setJniCallback);
+        callback->incStrong(reinterpret_cast<void *>(setJniCallback));
     }
     if (old != 0) {
-        old->decStrong((void*)setJniCallback);
+        old->decStrong(reinterpret_cast<void *>(setJniCallback));
     }
-    env->SetLongField(thiz, gEventHandlerFields.mJniCallback, (jlong)callback.get());
+    env->SetLongField(thiz, gEventHandlerFields.mJniCallback,
+                      reinterpret_cast<jlong>(callback.get()));
     return old;
 }
 
@@ -354,43 +357,44 @@
                                            jobjectArray deviceAddresses,
                                            AudioDeviceTypeAddrVector &audioDeviceTypeAddrVector) {
     if (deviceTypes == nullptr || deviceAddresses == nullptr) {
-        return (jint)AUDIO_JAVA_BAD_VALUE;
+    return AUDIO_JAVA_BAD_VALUE;
     }
     jsize deviceCount = env->GetArrayLength(deviceTypes);
     if (deviceCount == 0 || deviceCount != env->GetArrayLength(deviceAddresses)) {
-        return (jint)AUDIO_JAVA_BAD_VALUE;
+    return AUDIO_JAVA_BAD_VALUE;
     }
     // retrieve all device types
     std::vector<audio_devices_t> deviceTypesVector;
     jint *typesPtr = nullptr;
     typesPtr = env->GetIntArrayElements(deviceTypes, 0);
     if (typesPtr == nullptr) {
-        return (jint)AUDIO_JAVA_BAD_VALUE;
+    return AUDIO_JAVA_BAD_VALUE;
     }
     for (jint i = 0; i < deviceCount; i++) {
-        deviceTypesVector.push_back((audio_devices_t)typesPtr[i]);
+    deviceTypesVector.push_back(static_cast<audio_devices_t>(typesPtr[i]));
     }
     // check each address is a string and add device type/address to list
     jclass stringClass = FindClassOrDie(env, "java/lang/String");
     for (jint i = 0; i < deviceCount; i++) {
         jobject addrJobj = env->GetObjectArrayElement(deviceAddresses, i);
         if (!env->IsInstanceOf(addrJobj, stringClass)) {
-            return (jint)AUDIO_JAVA_BAD_VALUE;
+        return AUDIO_JAVA_BAD_VALUE;
         }
-        const char *address = env->GetStringUTFChars((jstring)addrJobj, NULL);
-        AudioDeviceTypeAddr dev = AudioDeviceTypeAddr((audio_devices_t)typesPtr[i], address);
+        const char *address = env->GetStringUTFChars(static_cast<jstring>(addrJobj), NULL);
+        AudioDeviceTypeAddr dev =
+                AudioDeviceTypeAddr(static_cast<audio_devices_t>(typesPtr[i]), address);
         audioDeviceTypeAddrVector.push_back(dev);
-        env->ReleaseStringUTFChars((jstring)addrJobj, address);
+        env->ReleaseStringUTFChars(static_cast<jstring>(addrJobj), address);
     }
     env->ReleaseIntArrayElements(deviceTypes, typesPtr, 0);
 
-    return (jint)NO_ERROR;
+    return NO_ERROR;
 }
 
 static jint
 android_media_AudioSystem_muteMicrophone(JNIEnv *env, jobject thiz, jboolean on)
 {
-    return (jint) check_AudioSystem_Command(AudioSystem::muteMicrophone(on));
+    return check_AudioSystem_Command(AudioSystem::muteMicrophone(on));
 }
 
 static jboolean
@@ -405,7 +409,7 @@
 android_media_AudioSystem_isStreamActive(JNIEnv *env, jobject thiz, jint stream, jint inPastMs)
 {
     bool state = false;
-    AudioSystem::isStreamActive((audio_stream_type_t) stream, &state, inPastMs);
+    AudioSystem::isStreamActive(static_cast<audio_stream_type_t>(stream), &state, inPastMs);
     return state;
 }
 
@@ -414,7 +418,7 @@
         jint inPastMs)
 {
     bool state = false;
-    AudioSystem::isStreamActiveRemotely((audio_stream_type_t) stream, &state, inPastMs);
+    AudioSystem::isStreamActiveRemotely(static_cast<audio_stream_type_t>(stream), &state, inPastMs);
     return state;
 }
 
@@ -422,7 +426,7 @@
 android_media_AudioSystem_isSourceActive(JNIEnv *env, jobject thiz, jint source)
 {
     bool state = false;
-    AudioSystem::isSourceActive((audio_source_t) source, &state);
+    AudioSystem::isSourceActive(static_cast<audio_source_t>(source), &state);
     return state;
 }
 
@@ -457,8 +461,7 @@
             env->GetStringLength(keyValuePairs));
         env->ReleaseStringCritical(keyValuePairs, c_keyValuePairs);
     }
-    int status = check_AudioSystem_Command(AudioSystem::setParameters(c_keyValuePairs8));
-    return (jint) status;
+    return check_AudioSystem_Command(AudioSystem::setParameters(c_keyValuePairs8));
 }
 
 static jstring
@@ -538,15 +541,15 @@
         return;
     }
     jint recParamData[REC_PARAM_SIZE];
-    recParamData[0] = (jint) audioFormatFromNative(clientConfig->format);
+    recParamData[0] = audioFormatFromNative(clientConfig->format);
     // FIXME this doesn't support index-based masks
-    recParamData[1] = (jint) inChannelMaskFromNative(clientConfig->channel_mask);
-    recParamData[2] = (jint) clientConfig->sample_rate;
-    recParamData[3] = (jint) audioFormatFromNative(deviceConfig->format);
+    recParamData[1] = inChannelMaskFromNative(clientConfig->channel_mask);
+    recParamData[2] = clientConfig->sample_rate;
+    recParamData[3] = audioFormatFromNative(deviceConfig->format);
     // FIXME this doesn't support index-based masks
-    recParamData[4] = (jint) inChannelMaskFromNative(deviceConfig->channel_mask);
-    recParamData[5] = (jint) deviceConfig->sample_rate;
-    recParamData[6] = (jint) patchHandle;
+    recParamData[4] = inChannelMaskFromNative(deviceConfig->channel_mask);
+    recParamData[5] = deviceConfig->sample_rate;
+    recParamData[6] = patchHandle;
     env->SetIntArrayRegion(recParamArray, 0, REC_PARAM_SIZE, recParamData);
 
     jobjectArray jClientEffects;
@@ -560,10 +563,9 @@
 
     env->CallStaticVoidMethod(clazz,
                               gAudioPolicyEventHandlerMethods.postRecordConfigEventFromNative,
-                              event, (jint) clientInfo->riid, (jint) clientInfo->uid,
-                              clientInfo->session, clientInfo->source, clientInfo->port_id,
-                              clientInfo->silenced, recParamArray, jClientEffects, jEffects,
-                              source);
+                              event, clientInfo->riid, clientInfo->uid, clientInfo->session,
+                              clientInfo->source, clientInfo->port_id, clientInfo->silenced,
+                              recParamArray, jClientEffects, jEffects, source);
     env->DeleteLocalRef(clazz);
     env->DeleteLocalRef(recParamArray);
     env->DeleteLocalRef(jClientEffects);
@@ -606,11 +608,9 @@
     if (Parcel *parcel = parcelForJavaObject(env, jParcel); parcel != nullptr) {
         android::media::audio::common::AudioPort port{};
         if (status_t statusOfParcel = port.readFromParcel(parcel); statusOfParcel == OK) {
-            status = check_AudioSystem_Command(
-                    AudioSystem::setDeviceConnectionState(static_cast<audio_policy_dev_state_t>(
-                                                                  state),
-                                                          port,
-                                                          static_cast<audio_format_t>(codec)));
+        status = check_AudioSystem_Command(
+                AudioSystem::setDeviceConnectionState(static_cast<audio_policy_dev_state_t>(state),
+                                                      port, static_cast<audio_format_t>(codec)));
         } else {
             ALOGE("Failed to read from parcel: %s", statusToString(statusOfParcel).c_str());
             status = kAudioStatusError;
@@ -619,17 +619,17 @@
         ALOGE("Failed to retrieve the native parcel from Java parcel");
         status = kAudioStatusError;
     }
-    return (jint) status;
+    return status;
 }
 
 static jint
 android_media_AudioSystem_getDeviceConnectionState(JNIEnv *env, jobject thiz, jint device, jstring device_address)
 {
     const char *c_address = env->GetStringUTFChars(device_address, NULL);
-    int state = static_cast <int>(AudioSystem::getDeviceConnectionState(static_cast <audio_devices_t>(device),
-                                          c_address));
+    int state = static_cast<int>(
+            AudioSystem::getDeviceConnectionState(static_cast<audio_devices_t>(device), c_address));
     env->ReleaseStringUTFChars(device_address, c_address);
-    return (jint) state;
+    return state;
 }
 
 static jint
@@ -638,38 +638,41 @@
 {
     const char *c_address = env->GetStringUTFChars(device_address, NULL);
     const char *c_name = env->GetStringUTFChars(device_name, NULL);
-    int status = check_AudioSystem_Command(AudioSystem::handleDeviceConfigChange(static_cast <audio_devices_t>(device),
-                                          c_address, c_name, static_cast <audio_format_t>(codec)));
+    int status = check_AudioSystem_Command(
+            AudioSystem::handleDeviceConfigChange(static_cast<audio_devices_t>(device), c_address,
+                                                  c_name, static_cast<audio_format_t>(codec)));
     env->ReleaseStringUTFChars(device_address, c_address);
     env->ReleaseStringUTFChars(device_name, c_name);
-    return (jint) status;
+    return status;
 }
 
 static jint android_media_AudioSystem_setPhoneState(JNIEnv *env, jobject thiz, jint state,
                                                     jint uid) {
-    return (jint)check_AudioSystem_Command(
-            AudioSystem::setPhoneState((audio_mode_t)state, (uid_t)uid));
+    return check_AudioSystem_Command(
+            AudioSystem::setPhoneState(static_cast<audio_mode_t>(state), static_cast<uid_t>(uid)));
 }
 
 static jint
 android_media_AudioSystem_setForceUse(JNIEnv *env, jobject thiz, jint usage, jint config)
 {
-    return (jint) check_AudioSystem_Command(AudioSystem::setForceUse(static_cast <audio_policy_force_use_t>(usage),
-                                                           static_cast <audio_policy_forced_cfg_t>(config)));
+    return check_AudioSystem_Command(
+            AudioSystem::setForceUse(static_cast<audio_policy_force_use_t>(usage),
+                                     static_cast<audio_policy_forced_cfg_t>(config)));
 }
 
 static jint
 android_media_AudioSystem_getForceUse(JNIEnv *env, jobject thiz, jint usage)
 {
-    return static_cast <jint>(AudioSystem::getForceUse(static_cast <audio_policy_force_use_t>(usage)));
+    return static_cast<jint>(
+            AudioSystem::getForceUse(static_cast<audio_policy_force_use_t>(usage)));
 }
 
 static jint
 android_media_AudioSystem_initStreamVolume(JNIEnv *env, jobject thiz, jint stream, jint indexMin, jint indexMax)
 {
-    return (jint) check_AudioSystem_Command(AudioSystem::initStreamVolume(static_cast <audio_stream_type_t>(stream),
-                                                                   indexMin,
-                                                                   indexMax));
+    return check_AudioSystem_Command(
+            AudioSystem::initStreamVolume(static_cast<audio_stream_type_t>(stream), indexMin,
+                                          indexMax));
 }
 
 static jint
@@ -679,10 +682,9 @@
                                                jint index,
                                                jint device)
 {
-    return (jint) check_AudioSystem_Command(
-            AudioSystem::setStreamVolumeIndex(static_cast <audio_stream_type_t>(stream),
-                                              index,
-                                              (audio_devices_t)device));
+    return check_AudioSystem_Command(
+            AudioSystem::setStreamVolumeIndex(static_cast<audio_stream_type_t>(stream), index,
+                                              static_cast<audio_devices_t>(device)));
 }
 
 static jint
@@ -692,13 +694,11 @@
                                                jint device)
 {
     int index;
-    if (AudioSystem::getStreamVolumeIndex(static_cast <audio_stream_type_t>(stream),
-                                          &index,
-                                          (audio_devices_t)device)
-            != NO_ERROR) {
+    if (AudioSystem::getStreamVolumeIndex(static_cast<audio_stream_type_t>(stream), &index,
+                                          static_cast<audio_devices_t>(device)) != NO_ERROR) {
         index = -1;
     }
-    return (jint) index;
+    return index;
 }
 
 static jint
@@ -711,11 +711,12 @@
     // read the AudioAttributes values
     JNIAudioAttributeHelper::UniqueAaPtr paa = JNIAudioAttributeHelper::makeUnique();
     jint jStatus = JNIAudioAttributeHelper::nativeFromJava(env, jaa, paa.get());
-    if (jStatus != (jint)AUDIO_JAVA_SUCCESS) {
+    if (jStatus != AUDIO_JAVA_SUCCESS) {
         return jStatus;
     }
-    return (jint) check_AudioSystem_Command(
-            AudioSystem::setVolumeIndexForAttributes(*(paa.get()), index, (audio_devices_t)device));
+    return check_AudioSystem_Command(
+            AudioSystem::setVolumeIndexForAttributes(*(paa.get()), index,
+                                                     static_cast<audio_devices_t>(device)));
 }
 
 static jint
@@ -727,15 +728,16 @@
     // read the AudioAttributes values
     JNIAudioAttributeHelper::UniqueAaPtr paa = JNIAudioAttributeHelper::makeUnique();
     jint jStatus = JNIAudioAttributeHelper::nativeFromJava(env, jaa, paa.get());
-    if (jStatus != (jint)AUDIO_JAVA_SUCCESS) {
+    if (jStatus != AUDIO_JAVA_SUCCESS) {
         return jStatus;
     }
     int index;
-    if (AudioSystem::getVolumeIndexForAttributes(*(paa.get()), index, (audio_devices_t)device)
-            != NO_ERROR) {
+    if (AudioSystem::getVolumeIndexForAttributes(*(paa.get()), index,
+                                                 static_cast<audio_devices_t>(device)) !=
+        NO_ERROR) {
         index = -1;
     }
-    return (jint) index;
+    return index;
 }
 
 static jint
@@ -746,7 +748,7 @@
     // read the AudioAttributes values
     JNIAudioAttributeHelper::UniqueAaPtr paa = JNIAudioAttributeHelper::makeUnique();
     jint jStatus = JNIAudioAttributeHelper::nativeFromJava(env, jaa, paa.get());
-    if (jStatus != (jint)AUDIO_JAVA_SUCCESS) {
+    if (jStatus != AUDIO_JAVA_SUCCESS) {
         return jStatus;
     }
     int index;
@@ -754,7 +756,7 @@
             != NO_ERROR) {
         index = -1;
     }
-    return (jint) index;
+    return index;
 }
 
 static jint
@@ -765,7 +767,7 @@
     // read the AudioAttributes values
     JNIAudioAttributeHelper::UniqueAaPtr paa = JNIAudioAttributeHelper::makeUnique();
     jint jStatus = JNIAudioAttributeHelper::nativeFromJava(env, jaa, paa.get());
-    if (jStatus != (jint)AUDIO_JAVA_SUCCESS) {
+    if (jStatus != AUDIO_JAVA_SUCCESS) {
         return jStatus;
     }
     int index;
@@ -773,13 +775,13 @@
             != NO_ERROR) {
         index = -1;
     }
-    return (jint) index;
+    return index;
 }
 
 static jint
 android_media_AudioSystem_setMasterVolume(JNIEnv *env, jobject thiz, jfloat value)
 {
-    return (jint) check_AudioSystem_Command(AudioSystem::setMasterVolume(value));
+    return check_AudioSystem_Command(AudioSystem::setMasterVolume(value));
 }
 
 static jfloat
@@ -795,7 +797,7 @@
 static jint
 android_media_AudioSystem_setMasterMute(JNIEnv *env, jobject thiz, jboolean mute)
 {
-    return (jint) check_AudioSystem_Command(AudioSystem::setMasterMute(mute));
+    return check_AudioSystem_Command(AudioSystem::setMasterMute(mute));
 }
 
 static jboolean
@@ -811,7 +813,7 @@
 static jint
 android_media_AudioSystem_setMasterMono(JNIEnv *env, jobject thiz, jboolean mono)
 {
-    return (jint) check_AudioSystem_Command(AudioSystem::setMasterMono(mono));
+    return check_AudioSystem_Command(AudioSystem::setMasterMono(mono));
 }
 
 static jboolean
@@ -827,7 +829,7 @@
 static jint
 android_media_AudioSystem_setMasterBalance(JNIEnv *env, jobject thiz, jfloat balance)
 {
-    return (jint) check_AudioSystem_Command(AudioSystem::setMasterBalance(balance));
+    return check_AudioSystem_Command(AudioSystem::setMasterBalance(balance));
 }
 
 static jfloat
@@ -845,37 +847,37 @@
 static jint
 android_media_AudioSystem_getPrimaryOutputSamplingRate(JNIEnv *env, jobject clazz)
 {
-    return (jint) AudioSystem::getPrimaryOutputSamplingRate();
+    return AudioSystem::getPrimaryOutputSamplingRate();
 }
 
 static jint
 android_media_AudioSystem_getPrimaryOutputFrameCount(JNIEnv *env, jobject clazz)
 {
-    return (jint) AudioSystem::getPrimaryOutputFrameCount();
+    return AudioSystem::getPrimaryOutputFrameCount();
 }
 
 static jint
 android_media_AudioSystem_getOutputLatency(JNIEnv *env, jobject clazz, jint stream)
 {
     uint32_t afLatency;
-    if (AudioSystem::getOutputLatency(&afLatency, static_cast <audio_stream_type_t>(stream))
-            != NO_ERROR) {
+    if (AudioSystem::getOutputLatency(&afLatency, static_cast<audio_stream_type_t>(stream)) !=
+        NO_ERROR) {
         afLatency = -1;
     }
-    return (jint) afLatency;
+    return afLatency;
 }
 
 static jint
 android_media_AudioSystem_setLowRamDevice(
         JNIEnv *env, jobject clazz, jboolean isLowRamDevice, jlong totalMemory)
 {
-    return (jint) AudioSystem::setLowRamDevice((bool) isLowRamDevice, (int64_t) totalMemory);
+    return AudioSystem::setLowRamDevice(isLowRamDevice, totalMemory);
 }
 
 static jint
 android_media_AudioSystem_checkAudioFlinger(JNIEnv *env, jobject clazz)
 {
-    return (jint) check_AudioSystem_Command(AudioSystem::checkAudioFlinger());
+    return check_AudioSystem_Command(AudioSystem::checkAudioFlinger());
 }
 
 static void android_media_AudioSystem_setAudioFlingerBinder(JNIEnv *env, jobject clazz,
@@ -889,8 +891,8 @@
                                                bool useInMask)
 {
     nAudioGainConfig->index = env->GetIntField(jAudioGainConfig, gAudioGainConfigFields.mIndex);
-    nAudioGainConfig->mode =
-            (audio_gain_mode_t)env->GetIntField(jAudioGainConfig, gAudioGainConfigFields.mMode);
+    nAudioGainConfig->mode = static_cast<audio_gain_mode_t>(
+            env->GetIntField(jAudioGainConfig, gAudioGainConfigFields.mMode));
     ALOGV("convertAudioGainConfigToNative got gain index %d", nAudioGainConfig->index);
     jint jMask = env->GetIntField(jAudioGainConfig, gAudioGainConfigFields.mChannelMask);
     audio_channel_mask_t nMask;
@@ -904,8 +906,8 @@
     nAudioGainConfig->channel_mask = nMask;
     nAudioGainConfig->ramp_duration_ms = env->GetIntField(jAudioGainConfig,
                                                        gAudioGainConfigFields.mRampDurationMs);
-    jintArray jValues = (jintArray)env->GetObjectField(jAudioGainConfig,
-                                                       gAudioGainConfigFields.mValues);
+    jintArray jValues = static_cast<jintArray>(
+            env->GetObjectField(jAudioGainConfig, gAudioGainConfigFields.mValues));
     int *nValues = env->GetIntArrayElements(jValues, NULL);
     size_t size = env->GetArrayLength(jValues);
     memcpy(nAudioGainConfig->values, nValues, size * sizeof(int));
@@ -920,8 +922,8 @@
     jobject jAudioPort = env->GetObjectField(jAudioPortConfig, gAudioPortConfigFields.mPort);
     jobject jHandle = env->GetObjectField(jAudioPort, gAudioPortFields.mHandle);
     nAudioPortConfig->id = env->GetIntField(jHandle, gAudioHandleFields.mId);
-    nAudioPortConfig->role = (audio_port_role_t)env->GetIntField(jAudioPort,
-                                                                 gAudioPortFields.mRole);
+    nAudioPortConfig->role =
+            static_cast<audio_port_role_t>(env->GetIntField(jAudioPort, gAudioPortFields.mRole));
     if (env->IsInstanceOf(jAudioPort, gAudioDevicePortClass)) {
         nAudioPortConfig->type = AUDIO_PORT_TYPE_DEVICE;
     } else if (env->IsInstanceOf(jAudioPort, gAudioMixPortClass)) {
@@ -929,7 +931,7 @@
     } else {
         env->DeleteLocalRef(jAudioPort);
         env->DeleteLocalRef(jHandle);
-        return (jint)AUDIO_JAVA_ERROR;
+        return AUDIO_JAVA_ERROR;
     }
     ALOGV("convertAudioPortConfigToNative handle %d role %d type %d",
           nAudioPortConfig->id, nAudioPortConfig->role, nAudioPortConfig->type);
@@ -984,7 +986,7 @@
     }
     env->DeleteLocalRef(jAudioPort);
     env->DeleteLocalRef(jHandle);
-    return (jint)AUDIO_JAVA_SUCCESS;
+    return AUDIO_JAVA_SUCCESS;
 }
 
 /**
@@ -1005,15 +1007,15 @@
     }
     // Supports AUDIO_PORT_TYPE_DEVICE only
     if (nAudioPortConfig->type != AUDIO_PORT_TYPE_DEVICE) {
-        return (jint)AUDIO_JAVA_BAD_VALUE;
+        return AUDIO_JAVA_BAD_VALUE;
     }
 
     jobject jAudioDevicePort = env->GetObjectField(jAudioPortConfig,
             gAudioPortConfigFields.mPort);
-    nAudioPortConfig->ext.device.type =
-            (audio_devices_t)env->GetIntField(jAudioDevicePort, gAudioPortFields.mType);
-    jstring jDeviceAddress = (jstring)env->GetObjectField(jAudioDevicePort,
-            gAudioPortFields.mAddress);
+    nAudioPortConfig->ext.device.type = static_cast<audio_devices_t>(
+            env->GetIntField(jAudioDevicePort, gAudioPortFields.mType));
+    jstring jDeviceAddress =
+            static_cast<jstring>(env->GetObjectField(jAudioDevicePort, gAudioPortFields.mAddress));
     const char *nDeviceAddress = env->GetStringUTFChars(jDeviceAddress, NULL);
     strncpy(nAudioPortConfig->ext.device.address,
             nDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN - 1);
@@ -1023,45 +1025,41 @@
     return jStatus;
 }
 
-static jint convertAudioPortConfigFromNative(JNIEnv *env,
-                                                 jobject jAudioPort,
-                                                 jobject *jAudioPortConfig,
-                                                 const struct audio_port_config *nAudioPortConfig)
-{
-    jint jStatus = AUDIO_JAVA_SUCCESS;
-    jobject jAudioGainConfig = NULL;
-    jobject jAudioGain = NULL;
+static jint convertAudioPortConfigFromNative(JNIEnv *env, ScopedLocalRef<jobject> *jAudioPort,
+                                             ScopedLocalRef<jobject> *jAudioPortConfig,
+                                             const struct audio_port_config *nAudioPortConfig) {
     jintArray jGainValues;
     bool audioportCreated = false;
 
     ALOGV("convertAudioPortConfigFromNative jAudioPort %p", jAudioPort);
 
-    if (jAudioPort == NULL) {
-        jobject jHandle = env->NewObject(gAudioHandleClass, gAudioHandleCstor,
-                                                 nAudioPortConfig->id);
+    if (*jAudioPort == nullptr) {
+        ScopedLocalRef<jobject> jHandle(env,
+                                        env->NewObject(gAudioHandleClass, gAudioHandleCstor,
+                                                       nAudioPortConfig->id));
 
         ALOGV("convertAudioPortConfigFromNative handle %d is a %s", nAudioPortConfig->id,
               nAudioPortConfig->type == AUDIO_PORT_TYPE_DEVICE ? "device" : "mix");
 
         if (jHandle == NULL) {
-            return (jint)AUDIO_JAVA_ERROR;
+            return AUDIO_JAVA_ERROR;
         }
         // create placeholder port and port config objects with just the correct handle
         // and configuration data. The actual AudioPortConfig objects will be
         // constructed by java code with correct class type (device, mix etc...)
         // and reference to AudioPort instance in this client
-        jAudioPort = env->NewObject(gAudioPortClass, gAudioPortCstor,
-                                           jHandle, // handle
-                                           0,       // role
-                                           NULL,    // name
-                                           NULL,    // samplingRates
-                                           NULL,    // channelMasks
-                                           NULL,    // channelIndexMasks
-                                           NULL,    // formats
-                                           NULL);   // gains
-        env->DeleteLocalRef(jHandle);
-        if (jAudioPort == NULL) {
-            return (jint)AUDIO_JAVA_ERROR;
+        jAudioPort->reset(env->NewObject(gAudioPortClass, gAudioPortCstor,
+                                         jHandle.get(), // handle
+                                         0,             // role
+                                         nullptr,       // name
+                                         nullptr,       // samplingRates
+                                         nullptr,       // channelMasks
+                                         nullptr,       // channelIndexMasks
+                                         nullptr,       // formats
+                                         nullptr));     // gains
+
+        if (*jAudioPort == nullptr) {
+            return AUDIO_JAVA_ERROR;
         }
         ALOGV("convertAudioPortConfigFromNative jAudioPort created for handle %d",
               nAudioPortConfig->id);
@@ -1069,6 +1067,9 @@
         audioportCreated = true;
     }
 
+    ScopedLocalRef<jobject> jAudioGainConfig(env, nullptr);
+    ScopedLocalRef<jobject> jAudioGain(env, nullptr);
+
     bool useInMask = audio_port_config_has_input_direction(nAudioPortConfig);
 
     audio_channel_mask_t nMask;
@@ -1082,36 +1083,28 @@
               gainIndex, nAudioPortConfig->gain.mode);
         if (audioportCreated) {
             ALOGV("convertAudioPortConfigFromNative creating gain");
-            jAudioGain = env->NewObject(gAudioGainClass, gAudioGainCstor,
-                                               gainIndex,
-                                               0,
-                                               0,
-                                               0,
-                                               0,
-                                               0,
-                                               0,
-                                               0,
-                                               0);
+            jAudioGain.reset(env->NewObject(gAudioGainClass, gAudioGainCstor, gainIndex, 0 /*mode*/,
+                                            0 /*channelMask*/, 0 /*minValue*/, 0 /*maxValue*/,
+                                            0 /*defaultValue*/, 0 /*stepValue*/,
+                                            0 /*rampDurationMinMs*/, 0 /*rampDurationMaxMs*/));
             if (jAudioGain == NULL) {
                 ALOGV("convertAudioPortConfigFromNative creating gain FAILED");
-                jStatus = (jint)AUDIO_JAVA_ERROR;
-                goto exit;
+                return AUDIO_JAVA_ERROR;
             }
         } else {
             ALOGV("convertAudioPortConfigFromNative reading gain from port");
-            jobjectArray jGains = (jobjectArray)env->GetObjectField(jAudioPort,
-                                                                      gAudioPortFields.mGains);
+            ScopedLocalRef<jobjectArray>
+                    jGains(env,
+                           static_cast<jobjectArray>(env->GetObjectField(jAudioPort->get(),
+                                                                         gAudioPortFields.mGains)));
             if (jGains == NULL) {
                 ALOGV("convertAudioPortConfigFromNative could not get gains from port");
-                jStatus = (jint)AUDIO_JAVA_ERROR;
-                goto exit;
+                return AUDIO_JAVA_ERROR;
             }
-            jAudioGain = env->GetObjectArrayElement(jGains, gainIndex);
-            env->DeleteLocalRef(jGains);
+            jAudioGain.reset(env->GetObjectArrayElement(jGains.get(), gainIndex));
             if (jAudioGain == NULL) {
                 ALOGV("convertAudioPortConfigFromNative could not get gain at index %d", gainIndex);
-                jStatus = (jint)AUDIO_JAVA_ERROR;
-                goto exit;
+                return AUDIO_JAVA_ERROR;
             }
         }
         int numValues;
@@ -1123,8 +1116,7 @@
         jGainValues = env->NewIntArray(numValues);
         if (jGainValues == NULL) {
             ALOGV("convertAudioPortConfigFromNative could not create gain values %d", numValues);
-            jStatus = (jint)AUDIO_JAVA_ERROR;
-            goto exit;
+            return AUDIO_JAVA_ERROR;
         }
         env->SetIntArrayRegion(jGainValues, 0, numValues,
                                nAudioPortConfig->gain.values);
@@ -1138,19 +1130,14 @@
             ALOGV("convertAudioPortConfigFromNative OUT mask java %x native %x", jMask, nMask);
         }
 
-        jAudioGainConfig = env->NewObject(gAudioGainConfigClass,
-                                        gAudioGainConfigCstor,
-                                        gainIndex,
-                                        jAudioGain,
-                                        nAudioPortConfig->gain.mode,
-                                        jMask,
-                                        jGainValues,
-                                        nAudioPortConfig->gain.ramp_duration_ms);
+        jAudioGainConfig.reset(env->NewObject(gAudioGainConfigClass, gAudioGainConfigCstor,
+                                              gainIndex, jAudioGain.get(),
+                                              nAudioPortConfig->gain.mode, jMask, jGainValues,
+                                              nAudioPortConfig->gain.ramp_duration_ms));
         env->DeleteLocalRef(jGainValues);
         if (jAudioGainConfig == NULL) {
             ALOGV("convertAudioPortConfigFromNative could not create gain config");
-            jStatus = (jint)AUDIO_JAVA_ERROR;
-            goto exit;
+            return AUDIO_JAVA_ERROR;
         }
     }
     jclass clazz;
@@ -1160,17 +1147,16 @@
         methodID = gAudioPortConfigCstor;
         ALOGV("convertAudioPortConfigFromNative building a generic port config");
     } else {
-        if (env->IsInstanceOf(jAudioPort, gAudioDevicePortClass)) {
+        if (env->IsInstanceOf(jAudioPort->get(), gAudioDevicePortClass)) {
             clazz = gAudioDevicePortConfigClass;
             methodID = gAudioDevicePortConfigCstor;
             ALOGV("convertAudioPortConfigFromNative building a device config");
-        } else if (env->IsInstanceOf(jAudioPort, gAudioMixPortClass)) {
+        } else if (env->IsInstanceOf(jAudioPort->get(), gAudioMixPortClass)) {
             clazz = gAudioMixPortConfigClass;
             methodID = gAudioMixPortConfigCstor;
             ALOGV("convertAudioPortConfigFromNative building a mix config");
         } else {
-            jStatus = (jint)AUDIO_JAVA_ERROR;
-            goto exit;
+            return AUDIO_JAVA_ERROR;
         }
     }
     nMask = (nAudioPortConfig->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK)
@@ -1184,8 +1170,8 @@
         ALOGV("convertAudioPortConfigFromNative OUT mask java %x native %x", jMask, nMask);
     }
 
-    *jAudioPortConfig =
-            env->NewObject(clazz, methodID, jAudioPort,
+    jAudioPortConfig->reset(
+            env->NewObject(clazz, methodID, jAudioPort->get(),
                            (nAudioPortConfig->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE)
                                    ? nAudioPortConfig->sample_rate
                                    : AUDIO_CONFIG_BASE_INITIALIZER.sample_rate,
@@ -1194,31 +1180,14 @@
                                    (nAudioPortConfig->config_mask & AUDIO_PORT_CONFIG_FORMAT)
                                            ? nAudioPortConfig->format
                                            : AUDIO_CONFIG_BASE_INITIALIZER.format),
-                           jAudioGainConfig);
+                           jAudioGainConfig.get()));
     if (*jAudioPortConfig == NULL) {
         ALOGV("convertAudioPortConfigFromNative could not create new port config");
-        jStatus = (jint)AUDIO_JAVA_ERROR;
+        return AUDIO_JAVA_ERROR;
     } else {
         ALOGV("convertAudioPortConfigFromNative OK");
     }
-
-exit:
-    if (audioportCreated) {
-        env->DeleteLocalRef(jAudioPort);
-        if (jAudioGain != NULL) {
-            env->DeleteLocalRef(jAudioGain);
-        }
-    }
-    if (jAudioGainConfig != NULL) {
-        env->DeleteLocalRef(jAudioGainConfig);
-    }
-    return jStatus;
-}
-
-// TODO: pull out to separate file
-template <typename T, size_t N>
-static constexpr size_t array_size(const T (&)[N]) {
-    return N;
+    return AUDIO_JAVA_SUCCESS;
 }
 
 static jintArray convertEncapsulationInfoFromNative(JNIEnv *env, uint32_t encapsulationInfo) {
@@ -1232,7 +1201,8 @@
         }
     }
     jintArray result = env->NewIntArray(encapsulation.size());
-    env->SetIntArrayRegion(result, 0, encapsulation.size(), (jint *)encapsulation.data());
+    env->SetIntArrayRegion(result, 0, encapsulation.size(),
+                           reinterpret_cast<jint *>(encapsulation.data()));
     return result;
 }
 
@@ -1240,8 +1210,8 @@
                                              std::stringstream &ss) {
     ss << " num_audio_profiles " << nAudioPort->num_audio_profiles << " num_gains "
        << nAudioPort->num_gains;
-    if (nAudioPort->num_audio_profiles > array_size(nAudioPort->audio_profiles) ||
-        nAudioPort->num_gains > array_size(nAudioPort->gains)) {
+    if (nAudioPort->num_audio_profiles > std::size(nAudioPort->audio_profiles) ||
+        nAudioPort->num_gains > std::size(nAudioPort->gains)) {
         return true;
     }
     for (size_t i = 0; i < nAudioPort->num_audio_profiles; ++i) {
@@ -1249,16 +1219,16 @@
            << " num_sample_rates " << nAudioPort->audio_profiles[i].num_sample_rates
            << " num_channel_masks " << nAudioPort->audio_profiles[i].num_channel_masks;
         if (nAudioPort->audio_profiles[i].num_sample_rates >
-                    array_size(nAudioPort->audio_profiles[i].sample_rates) ||
+                    std::size(nAudioPort->audio_profiles[i].sample_rates) ||
             nAudioPort->audio_profiles[i].num_channel_masks >
-                    array_size(nAudioPort->audio_profiles[i].channel_masks)) {
+                    std::size(nAudioPort->audio_profiles[i].channel_masks)) {
             return true;
         }
     }
     return false;
 }
 
-static jint convertAudioProfileFromNative(JNIEnv *env, jobject *jAudioProfile,
+static jint convertAudioProfileFromNative(JNIEnv *env, ScopedLocalRef<jobject> *jAudioProfile,
                                           const audio_profile *nAudioProfile, bool useInMask) {
     size_t numPositionMasks = 0;
     size_t numIndexMasks = 0;
@@ -1289,7 +1259,8 @@
 
     if (nAudioProfile->num_sample_rates) {
         env->SetIntArrayRegion(jSamplingRates.get(), 0 /*start*/, nAudioProfile->num_sample_rates,
-                               (jint *)nAudioProfile->sample_rates);
+                               const_cast<jint *>(reinterpret_cast<const jint *>(
+                                       nAudioProfile->sample_rates)));
     }
 
     // put the masks in the output arrays
@@ -1311,10 +1282,9 @@
         ALOGW("Unknown encapsulation type for JAVA API: %u", nAudioProfile->encapsulation_type);
     }
 
-    *jAudioProfile = env->NewObject(gAudioProfileClass, gAudioProfileCstor, audioFormat,
-                                    jSamplingRates.get(), jChannelMasks.get(),
-                                    jChannelIndexMasks.get(), encapsulationType);
-
+    jAudioProfile->reset(env->NewObject(gAudioProfileClass, gAudioProfileCstor, audioFormat,
+                                        jSamplingRates.get(), jChannelMasks.get(),
+                                        jChannelIndexMasks.get(), encapsulationType));
     if (*jAudioProfile == nullptr) {
         return AUDIO_JAVA_ERROR;
     }
@@ -1322,18 +1292,8 @@
     return AUDIO_JAVA_SUCCESS;
 }
 
-static jint convertAudioPortFromNative(JNIEnv *env, jobject *jAudioPort,
+static jint convertAudioPortFromNative(JNIEnv *env, ScopedLocalRef<jobject> *jAudioPort,
                                        const struct audio_port_v7 *nAudioPort) {
-    jint jStatus = (jint)AUDIO_JAVA_SUCCESS;
-    jintArray jEncapsulationModes = NULL;
-    jintArray jEncapsulationMetadataTypes = NULL;
-    jobjectArray jGains = NULL;
-    jobject jHandle = NULL;
-    jobject jAudioPortConfig = NULL;
-    jstring jDeviceName = NULL;
-    jobject jAudioProfiles = NULL;
-    jobject jAudioDescriptors = nullptr;
-    ScopedLocalRef<jobject> jPcmFloatProfileFromExtendedInteger(env, nullptr);
     bool hasFloat = false;
     bool useInMask;
 
@@ -1357,30 +1317,30 @@
         } else {
             ALOGE("%s", s.c_str());
         }
-        jStatus = (jint)AUDIO_JAVA_ERROR;
-        goto exit;
+        return AUDIO_JAVA_ERROR;
     }
 
     useInMask = audio_has_input_direction(nAudioPort->type, nAudioPort->role);
-    jAudioProfiles = env->NewObject(gArrayListClass, gArrayListMethods.cstor);
+    ScopedLocalRef<jobject> jAudioProfiles(env,
+                                           env->NewObject(gArrayListClass,
+                                                          gArrayListMethods.cstor));
     if (jAudioProfiles == nullptr) {
-        jStatus = (jint)AUDIO_JAVA_ERROR;
-        goto exit;
+        return AUDIO_JAVA_ERROR;
     }
 
+    ScopedLocalRef<jobject> jPcmFloatProfileFromExtendedInteger(env, nullptr);
     for (size_t i = 0; i < nAudioPort->num_audio_profiles; ++i) {
-        jobject jAudioProfile = nullptr;
-        jStatus = convertAudioProfileFromNative(env, &jAudioProfile, &nAudioPort->audio_profiles[i],
-                                                useInMask);
+        ScopedLocalRef<jobject> jAudioProfile(env);
+        jint jStatus = convertAudioProfileFromNative(env, &jAudioProfile,
+                                                     &nAudioPort->audio_profiles[i], useInMask);
         if (jStatus == AUDIO_JAVA_BAD_VALUE) {
             // skipping Java layer unsupported audio formats
             continue;
         }
         if (jStatus != NO_ERROR) {
-            jStatus = (jint)AUDIO_JAVA_ERROR;
-            goto exit;
+            return AUDIO_JAVA_ERROR;
         }
-        env->CallBooleanMethod(jAudioProfiles, gArrayListMethods.add, jAudioProfile);
+        env->CallBooleanMethod(jAudioProfiles.get(), gArrayListMethods.add, jAudioProfile.get());
 
         if (nAudioPort->audio_profiles[i].format == AUDIO_FORMAT_PCM_FLOAT) {
             hasFloat = true;
@@ -1389,21 +1349,23 @@
                    audio_bytes_per_sample(nAudioPort->audio_profiles[i].format) > 2) {
             ScopedLocalRef<jintArray>
                     jSamplingRates(env,
-                                   (jintArray)
-                                           env->GetObjectField(jAudioProfile,
-                                                               gAudioProfileFields.mSamplingRates));
+                                   static_cast<jintArray>(
+                                           env->GetObjectField(jAudioProfile.get(),
+                                                               gAudioProfileFields
+                                                                       .mSamplingRates)));
             ScopedLocalRef<jintArray>
                     jChannelMasks(env,
-                                  (jintArray)
-                                          env->GetObjectField(jAudioProfile,
-                                                              gAudioProfileFields.mChannelMasks));
+                                  static_cast<jintArray>(
+                                          env->GetObjectField(jAudioProfile.get(),
+                                                              gAudioProfileFields.mChannelMasks)));
             ScopedLocalRef<jintArray>
                     jChannelIndexMasks(env,
-                                       (jintArray)env->GetObjectField(jAudioProfile,
-                                                                      gAudioProfileFields
-                                                                              .mChannelIndexMasks));
+                                       static_cast<jintArray>(
+                                               env->GetObjectField(jAudioProfile.get(),
+                                                                   gAudioProfileFields
+                                                                           .mChannelIndexMasks)));
             int encapsulationType =
-                    env->GetIntField(jAudioProfile, gAudioProfileFields.mEncapsulationType);
+                    env->GetIntField(jAudioProfile.get(), gAudioProfileFields.mEncapsulationType);
 
             jPcmFloatProfileFromExtendedInteger.reset(
                     env->NewObject(gAudioProfileClass, gAudioProfileCstor,
@@ -1411,24 +1373,21 @@
                                    jSamplingRates.get(), jChannelMasks.get(),
                                    jChannelIndexMasks.get(), encapsulationType));
         }
-
-        if (jAudioProfile != nullptr) {
-            env->DeleteLocalRef(jAudioProfile);
-        }
     }
     if (!hasFloat && jPcmFloatProfileFromExtendedInteger.get() != nullptr) {
         // R and earlier compatibility - add ENCODING_PCM_FLOAT to the end
         // (replacing the zero pad). This ensures pre-S apps that look
         // for ENCODING_PCM_FLOAT continue to see that encoding if the device supports
         // extended precision integers.
-        env->CallBooleanMethod(jAudioProfiles, gArrayListMethods.add,
+        env->CallBooleanMethod(jAudioProfiles.get(), gArrayListMethods.add,
                                jPcmFloatProfileFromExtendedInteger.get());
     }
 
-    jAudioDescriptors = env->NewObject(gArrayListClass, gArrayListMethods.cstor);
+    ScopedLocalRef<jobject> jAudioDescriptors(env,
+                                              env->NewObject(gArrayListClass,
+                                                             gArrayListMethods.cstor));
     if (jAudioDescriptors == nullptr) {
-        jStatus = (jint)AUDIO_JAVA_ERROR;
-        goto exit;
+        return AUDIO_JAVA_ERROR;
     }
     for (size_t i = 0; i < nAudioPort->num_extra_audio_descriptors; ++i) {
         const auto &extraAudioDescriptor = nAudioPort->extra_audio_descriptors[i];
@@ -1458,15 +1417,16 @@
                                         env->NewObject(gAudioDescriptorClass, gAudioDescriptorCstor,
                                                        standard, encapsulationType,
                                                        jDescriptor.get()));
-        env->CallBooleanMethod(jAudioDescriptors, gArrayListMethods.add, jAudioDescriptor.get());
+        env->CallBooleanMethod(jAudioDescriptors.get(), gArrayListMethods.add,
+                               jAudioDescriptor.get());
     }
 
     // gains
-    jGains = env->NewObjectArray(nAudioPort->num_gains,
-                                          gAudioGainClass, NULL);
-    if (jGains == NULL) {
-        jStatus = (jint)AUDIO_JAVA_ERROR;
-        goto exit;
+    ScopedLocalRef<jobjectArray> jGains(env,
+                                        env->NewObjectArray(nAudioPort->num_gains, gAudioGainClass,
+                                                            nullptr));
+    if (jGains == nullptr) {
+        return AUDIO_JAVA_ERROR;
     }
 
     for (size_t j = 0; j < nAudioPort->num_gains; j++) {
@@ -1491,88 +1451,71 @@
                                                  nAudioPort->gains[j].min_ramp_ms,
                                                  nAudioPort->gains[j].max_ramp_ms);
         if (jGain == NULL) {
-            jStatus = (jint)AUDIO_JAVA_ERROR;
-            goto exit;
+            return AUDIO_JAVA_ERROR;
         }
-        env->SetObjectArrayElement(jGains, j, jGain);
+        env->SetObjectArrayElement(jGains.get(), j, jGain);
         env->DeleteLocalRef(jGain);
     }
 
-    jHandle = env->NewObject(gAudioHandleClass, gAudioHandleCstor,
-                                             nAudioPort->id);
-    if (jHandle == NULL) {
-        jStatus = (jint)AUDIO_JAVA_ERROR;
-        goto exit;
+    ScopedLocalRef<jobject> jHandle(env,
+                                    env->NewObject(gAudioHandleClass, gAudioHandleCstor,
+                                                   nAudioPort->id));
+    if (jHandle == nullptr) {
+        return AUDIO_JAVA_ERROR;
     }
 
-    jDeviceName = env->NewStringUTF(nAudioPort->name);
-
+    ScopedLocalRef<jstring> jDeviceName(env, env->NewStringUTF(nAudioPort->name));
     if (nAudioPort->type == AUDIO_PORT_TYPE_DEVICE) {
-        ALOGV("convertAudioPortFromNative is a device %08x", nAudioPort->ext.device.type);
-        jstring jAddress = env->NewStringUTF(nAudioPort->ext.device.address);
-        jEncapsulationModes =
-                convertEncapsulationInfoFromNative(env, nAudioPort->ext.device.encapsulation_modes);
-        jEncapsulationMetadataTypes =
+        ScopedLocalRef<jintArray> jEncapsulationModes(
+                env,
+                convertEncapsulationInfoFromNative(env,
+                                                   nAudioPort->ext.device.encapsulation_modes));
+        ScopedLocalRef<jintArray> jEncapsulationMetadataTypes(
+                env,
                 convertEncapsulationInfoFromNative(env,
                                                    nAudioPort->ext.device
-                                                           .encapsulation_metadata_types);
-        *jAudioPort =
-                env->NewObject(gAudioDevicePortClass, gAudioDevicePortCstor, jHandle, jDeviceName,
-                               jAudioProfiles, jGains, nAudioPort->ext.device.type, jAddress,
-                               jEncapsulationModes, jEncapsulationMetadataTypes, jAudioDescriptors);
-        env->DeleteLocalRef(jAddress);
+                                                           .encapsulation_metadata_types));
+        ALOGV("convertAudioPortFromNative is a device %08x", nAudioPort->ext.device.type);
+        ScopedLocalRef<jstring> jAddress(env, env->NewStringUTF(nAudioPort->ext.device.address));
+        jAudioPort->reset(env->NewObject(gAudioDevicePortClass, gAudioDevicePortCstor,
+                                         jHandle.get(), jDeviceName.get(), jAudioProfiles.get(),
+                                         jGains.get(), nAudioPort->ext.device.type, jAddress.get(),
+                                         jEncapsulationModes.get(),
+                                         jEncapsulationMetadataTypes.get(),
+                                         jAudioDescriptors.get()));
     } else if (nAudioPort->type == AUDIO_PORT_TYPE_MIX) {
         ALOGV("convertAudioPortFromNative is a mix");
-        *jAudioPort = env->NewObject(gAudioMixPortClass, gAudioMixPortCstor, jHandle,
-                                     nAudioPort->ext.mix.handle, nAudioPort->role, jDeviceName,
-                                     jAudioProfiles, jGains);
+        jAudioPort->reset(env->NewObject(gAudioMixPortClass, gAudioMixPortCstor, jHandle.get(),
+                                         nAudioPort->ext.mix.handle, nAudioPort->role,
+                                         jDeviceName.get(), jAudioProfiles.get(), jGains.get()));
     } else {
         ALOGE("convertAudioPortFromNative unknown nAudioPort type %d", nAudioPort->type);
-        jStatus = (jint)AUDIO_JAVA_ERROR;
-        goto exit;
+        return AUDIO_JAVA_ERROR;
     }
     if (*jAudioPort == NULL) {
-        jStatus = (jint)AUDIO_JAVA_ERROR;
-        goto exit;
+        return AUDIO_JAVA_ERROR;
     }
 
-    jStatus = convertAudioPortConfigFromNative(env,
-                                                       *jAudioPort,
-                                                       &jAudioPortConfig,
+    ScopedLocalRef<jobject> jAudioPortConfig(env, nullptr);
+
+    if (int jStatus = convertAudioPortConfigFromNative(env, jAudioPort, &jAudioPortConfig,
                                                        &nAudioPort->active_config);
-    if (jStatus != AUDIO_JAVA_SUCCESS) {
-        goto exit;
+        jStatus != AUDIO_JAVA_SUCCESS) {
+        return jStatus;
     }
 
-    env->SetObjectField(*jAudioPort, gAudioPortFields.mActiveConfig, jAudioPortConfig);
+    env->SetObjectField(jAudioPort->get(), gAudioPortFields.mActiveConfig, jAudioPortConfig.get());
+    return AUDIO_JAVA_SUCCESS;
+}
 
-exit:
-    if (jDeviceName != NULL) {
-        env->DeleteLocalRef(jDeviceName);
+static bool setGeneration(JNIEnv *env, jintArray jGeneration, unsigned int generation1) {
+    ScopedIntArrayRW nGeneration(env, jGeneration);
+    if (nGeneration.get() == nullptr) {
+        return false;
+    } else {
+        nGeneration[0] = generation1;
+        return true;
     }
-    if (jEncapsulationModes != NULL) {
-        env->DeleteLocalRef(jEncapsulationModes);
-    }
-    if (jEncapsulationMetadataTypes != NULL) {
-        env->DeleteLocalRef(jEncapsulationMetadataTypes);
-    }
-    if (jAudioProfiles != NULL) {
-        env->DeleteLocalRef(jAudioProfiles);
-    }
-    if (jGains != NULL) {
-        env->DeleteLocalRef(jGains);
-    }
-    if (jHandle != NULL) {
-        env->DeleteLocalRef(jHandle);
-    }
-    if (jAudioPortConfig != NULL) {
-        env->DeleteLocalRef(jAudioPortConfig);
-    }
-    if (jAudioDescriptors != nullptr) {
-        env->DeleteLocalRef(jAudioDescriptors);
-    }
-
-    return jStatus;
 }
 
 static jint
@@ -1583,23 +1526,22 @@
 
     if (jPorts == NULL) {
         ALOGE("listAudioPorts NULL AudioPort ArrayList");
-        return (jint)AUDIO_JAVA_BAD_VALUE;
+        return AUDIO_JAVA_BAD_VALUE;
     }
     if (!env->IsInstanceOf(jPorts, gArrayListClass)) {
         ALOGE("listAudioPorts not an arraylist");
-        return (jint)AUDIO_JAVA_BAD_VALUE;
+        return AUDIO_JAVA_BAD_VALUE;
     }
 
     if (jGeneration == NULL || env->GetArrayLength(jGeneration) != 1) {
-        return (jint)AUDIO_JAVA_BAD_VALUE;
+        return AUDIO_JAVA_BAD_VALUE;
     }
 
     status_t status;
-    unsigned int generation1;
+    unsigned int generation1 = 0;
     unsigned int generation;
     unsigned int numPorts;
-    jint *nGeneration;
-    struct audio_port_v7 *nPorts = nullptr;
+    std::vector<audio_port_v7> nPorts;
     int attempts = MAX_PORT_GENERATION_SYNC_ATTEMPTS;
     jint jStatus;
 
@@ -1618,43 +1560,29 @@
             break;
         }
         if (numPorts == 0) {
-            jStatus = (jint)AUDIO_JAVA_SUCCESS;
-            goto exit;
+            return setGeneration(env, jGeneration, generation1) ? AUDIO_JAVA_SUCCESS
+                                                                : AUDIO_JAVA_ERROR;
         }
-        nPorts = (struct audio_port_v7 *)realloc(nPorts, numPorts * sizeof(struct audio_port_v7));
+        nPorts.resize(numPorts);
 
         status = AudioSystem::listAudioPorts(AUDIO_PORT_ROLE_NONE, AUDIO_PORT_TYPE_NONE, &numPorts,
-                                             nPorts, &generation);
+                                             &nPorts[0], &generation);
         ALOGV("listAudioPorts AudioSystem::listAudioPorts numPorts %d generation %d generation1 %d",
               numPorts, generation, generation1);
     } while (generation1 != generation && status == NO_ERROR);
 
     jStatus = nativeToJavaStatus(status);
-    if (jStatus != AUDIO_JAVA_SUCCESS) {
-        goto exit;
-    }
-
-    for (size_t i = 0; i < numPorts; i++) {
-        jobject jAudioPort = NULL;
-        jStatus = convertAudioPortFromNative(env, &jAudioPort, &nPorts[i]);
-        if (jStatus != AUDIO_JAVA_SUCCESS) {
-            goto exit;
-        }
-        env->CallBooleanMethod(jPorts, gArrayListMethods.add, jAudioPort);
-        if (jAudioPort != NULL) {
-            env->DeleteLocalRef(jAudioPort);
+    if (jStatus == AUDIO_JAVA_SUCCESS) {
+        for (size_t i = 0; i < numPorts; i++) {
+            ScopedLocalRef<jobject> jAudioPort(env, nullptr);
+            jStatus = convertAudioPortFromNative(env, &jAudioPort, &nPorts[i]);
+            if (jStatus != AUDIO_JAVA_SUCCESS) break;
+            env->CallBooleanMethod(jPorts, gArrayListMethods.add, jAudioPort.get());
         }
     }
-
-exit:
-    nGeneration = env->GetIntArrayElements(jGeneration, NULL);
-    if (nGeneration == NULL) {
-        jStatus = (jint)AUDIO_JAVA_ERROR;
-    } else {
-        nGeneration[0] = generation1;
-        env->ReleaseIntArrayElements(jGeneration, nGeneration, 0);
+    if (!setGeneration(env, jGeneration, generation1)) {
+        jStatus = AUDIO_JAVA_ERROR;
     }
-    free(nPorts);
     return jStatus;
 }
 
@@ -1667,64 +1595,56 @@
 
     ALOGV("createAudioPatch");
     if (jPatches == NULL || jSources == NULL || jSinks == NULL) {
-        return (jint)AUDIO_JAVA_BAD_VALUE;
+        return AUDIO_JAVA_BAD_VALUE;
     }
 
     if (env->GetArrayLength(jPatches) != 1) {
-        return (jint)AUDIO_JAVA_BAD_VALUE;
+        return AUDIO_JAVA_BAD_VALUE;
     }
     jint numSources = env->GetArrayLength(jSources);
     if (numSources == 0 || numSources > AUDIO_PATCH_PORTS_MAX) {
-        return (jint)AUDIO_JAVA_BAD_VALUE;
+        return AUDIO_JAVA_BAD_VALUE;
     }
 
     jint numSinks = env->GetArrayLength(jSinks);
     if (numSinks == 0 || numSinks > AUDIO_PATCH_PORTS_MAX) {
-        return (jint)AUDIO_JAVA_BAD_VALUE;
+        return AUDIO_JAVA_BAD_VALUE;
     }
 
-    audio_patch_handle_t handle = (audio_patch_handle_t)0;
-    jobject jPatch = env->GetObjectArrayElement(jPatches, 0);
-    jobject jPatchHandle = NULL;
-    if (jPatch != NULL) {
-        if (!env->IsInstanceOf(jPatch, gAudioPatchClass)) {
-            return (jint)AUDIO_JAVA_BAD_VALUE;
+    audio_patch_handle_t handle = static_cast<audio_patch_handle_t>(AUDIO_PATCH_HANDLE_NONE);
+    ScopedLocalRef<jobject> jPatch(env, env->GetObjectArrayElement(jPatches, 0));
+    ScopedLocalRef<jobject> jPatchHandle(env, nullptr);
+    if (jPatch != nullptr) {
+        if (!env->IsInstanceOf(jPatch.get(), gAudioPatchClass)) {
+            return AUDIO_JAVA_BAD_VALUE;
         }
-        jPatchHandle = env->GetObjectField(jPatch, gAudioPatchFields.mHandle);
-        handle = (audio_patch_handle_t)env->GetIntField(jPatchHandle, gAudioHandleFields.mId);
+        jPatchHandle.reset(env->GetObjectField(jPatch.get(), gAudioPatchFields.mHandle));
+        handle = static_cast<audio_patch_handle_t>(
+                env->GetIntField(jPatchHandle.get(), gAudioHandleFields.mId));
     }
 
     struct audio_patch nPatch = { .id = handle };
 
-    jobject jSource = NULL;
-    jobject jSink = NULL;
-
     for (jint i = 0; i < numSources; i++) {
-        jSource = env->GetObjectArrayElement(jSources, i);
-        if (!env->IsInstanceOf(jSource, gAudioPortConfigClass)) {
-            jStatus = (jint)AUDIO_JAVA_BAD_VALUE;
-            goto exit;
+        ScopedLocalRef<jobject> jSource(env, env->GetObjectArrayElement(jSources, i));
+        if (!env->IsInstanceOf(jSource.get(), gAudioPortConfigClass)) {
+            return AUDIO_JAVA_BAD_VALUE;
         }
-        jStatus = convertAudioPortConfigToNative(env, &nPatch.sources[i], jSource, false);
-        env->DeleteLocalRef(jSource);
-        jSource = NULL;
+        jStatus = convertAudioPortConfigToNative(env, &nPatch.sources[i], jSource.get(), false);
         if (jStatus != AUDIO_JAVA_SUCCESS) {
-            goto exit;
+            return jStatus;
         }
         nPatch.num_sources++;
     }
 
     for (jint i = 0; i < numSinks; i++) {
-        jSink = env->GetObjectArrayElement(jSinks, i);
-        if (!env->IsInstanceOf(jSink, gAudioPortConfigClass)) {
-            jStatus = (jint)AUDIO_JAVA_BAD_VALUE;
-            goto exit;
+        ScopedLocalRef<jobject> jSink(env, env->GetObjectArrayElement(jSinks, i));
+        if (!env->IsInstanceOf(jSink.get(), gAudioPortConfigClass)) {
+            return AUDIO_JAVA_BAD_VALUE;
         }
-        jStatus = convertAudioPortConfigToNative(env, &nPatch.sinks[i], jSink, false);
-        env->DeleteLocalRef(jSink);
-        jSink = NULL;
+        jStatus = convertAudioPortConfigToNative(env, &nPatch.sinks[i], jSink.get(), false);
         if (jStatus != AUDIO_JAVA_SUCCESS) {
-            goto exit;
+            return jStatus;
         }
         nPatch.num_sinks++;
     }
@@ -1735,38 +1655,22 @@
 
     jStatus = nativeToJavaStatus(status);
     if (jStatus != AUDIO_JAVA_SUCCESS) {
-        goto exit;
+        return jStatus;
     }
 
-    if (jPatchHandle == NULL) {
-        jPatchHandle = env->NewObject(gAudioHandleClass, gAudioHandleCstor,
-                                           handle);
-        if (jPatchHandle == NULL) {
-            jStatus = (jint)AUDIO_JAVA_ERROR;
-            goto exit;
+    if (jPatchHandle == nullptr) {
+        jPatchHandle.reset(env->NewObject(gAudioHandleClass, gAudioHandleCstor, handle));
+        if (jPatchHandle == nullptr) {
+            return AUDIO_JAVA_ERROR;
         }
-        jPatch = env->NewObject(gAudioPatchClass, gAudioPatchCstor, jPatchHandle, jSources, jSinks);
-        if (jPatch == NULL) {
-            jStatus = (jint)AUDIO_JAVA_ERROR;
-            goto exit;
+        jPatch.reset(env->NewObject(gAudioPatchClass, gAudioPatchCstor, jPatchHandle.get(),
+                                    jSources, jSinks));
+        if (jPatch == nullptr) {
+            return AUDIO_JAVA_ERROR;
         }
-        env->SetObjectArrayElement(jPatches, 0, jPatch);
+        env->SetObjectArrayElement(jPatches, 0, jPatch.get());
     } else {
-        env->SetIntField(jPatchHandle, gAudioHandleFields.mId, handle);
-    }
-
-exit:
-    if (jPatchHandle != NULL) {
-        env->DeleteLocalRef(jPatchHandle);
-    }
-    if (jPatch != NULL) {
-        env->DeleteLocalRef(jPatch);
-    }
-    if (jSource != NULL) {
-        env->DeleteLocalRef(jSource);
-    }
-    if (jSink != NULL) {
-        env->DeleteLocalRef(jSink);
+        env->SetIntField(jPatchHandle.get(), gAudioHandleFields.mId, handle);
     }
     return jStatus;
 }
@@ -1777,16 +1681,17 @@
 {
     ALOGV("releaseAudioPatch");
     if (jPatch == NULL) {
-        return (jint)AUDIO_JAVA_BAD_VALUE;
+        return AUDIO_JAVA_BAD_VALUE;
     }
 
-    audio_patch_handle_t handle = (audio_patch_handle_t)0;
+    audio_patch_handle_t handle = static_cast<audio_patch_handle_t>(AUDIO_PATCH_HANDLE_NONE);
     jobject jPatchHandle = NULL;
     if (!env->IsInstanceOf(jPatch, gAudioPatchClass)) {
-        return (jint)AUDIO_JAVA_BAD_VALUE;
+        return AUDIO_JAVA_BAD_VALUE;
     }
     jPatchHandle = env->GetObjectField(jPatch, gAudioPatchFields.mHandle);
-    handle = (audio_patch_handle_t)env->GetIntField(jPatchHandle, gAudioHandleFields.mId);
+    handle = static_cast<audio_patch_handle_t>(
+            env->GetIntField(jPatchHandle, gAudioHandleFields.mId));
     env->DeleteLocalRef(jPatchHandle);
 
     ALOGV("AudioSystem::releaseAudioPatch");
@@ -1803,28 +1708,22 @@
     ALOGV("listAudioPatches");
     if (jPatches == NULL) {
         ALOGE("listAudioPatches NULL AudioPatch ArrayList");
-        return (jint)AUDIO_JAVA_BAD_VALUE;
+        return AUDIO_JAVA_BAD_VALUE;
     }
     if (!env->IsInstanceOf(jPatches, gArrayListClass)) {
         ALOGE("listAudioPatches not an arraylist");
-        return (jint)AUDIO_JAVA_BAD_VALUE;
+        return AUDIO_JAVA_BAD_VALUE;
     }
 
     if (jGeneration == NULL || env->GetArrayLength(jGeneration) != 1) {
-        return (jint)AUDIO_JAVA_BAD_VALUE;
+        return AUDIO_JAVA_BAD_VALUE;
     }
 
     status_t status;
     unsigned int generation1;
     unsigned int generation;
     unsigned int numPatches;
-    jint *nGeneration;
-    struct audio_patch *nPatches = NULL;
-    jobjectArray jSources = NULL;
-    jobject jSource = NULL;
-    jobjectArray jSinks = NULL;
-    jobject jSink = NULL;
-    jobject jPatch = NULL;
+    std::vector<audio_patch> nPatches;
     int attempts = MAX_PORT_GENERATION_SYNC_ATTEMPTS;
     jint jStatus;
 
@@ -1845,15 +1744,13 @@
             break;
         }
         if (numPatches == 0) {
-            jStatus = (jint)AUDIO_JAVA_SUCCESS;
-            goto exit;
+            return setGeneration(env, jGeneration, generation1) ? AUDIO_JAVA_SUCCESS
+                                                                : AUDIO_JAVA_ERROR;
         }
 
-        nPatches = (struct audio_patch *)realloc(nPatches, numPatches * sizeof(struct audio_patch));
+        nPatches.resize(numPatches);
 
-        status = AudioSystem::listAudioPatches(&numPatches,
-                                               nPatches,
-                                               &generation);
+        status = AudioSystem::listAudioPatches(&numPatches, nPatches.data(), &generation);
         ALOGV("listAudioPatches AudioSystem::listAudioPatches numPatches %d generation %d generation1 %d",
               numPatches, generation, generation1);
 
@@ -1861,15 +1758,21 @@
 
     jStatus = nativeToJavaStatus(status);
     if (jStatus != AUDIO_JAVA_SUCCESS) {
-        goto exit;
+        if (!setGeneration(env, jGeneration, generation1)) {
+            jStatus = AUDIO_JAVA_ERROR;
+        }
+        return jStatus;
     }
 
     for (size_t i = 0; i < numPatches; i++) {
+        ScopedLocalRef<jobject> jPatch(env, nullptr);
+        ScopedLocalRef<jobjectArray> jSources(env, nullptr);
+        ScopedLocalRef<jobjectArray> jSinks(env, nullptr);
         jobject patchHandle = env->NewObject(gAudioHandleClass, gAudioHandleCstor,
                                                  nPatches[i].id);
         if (patchHandle == NULL) {
-            jStatus = AUDIO_JAVA_ERROR;
-            goto exit;
+            setGeneration(env, jGeneration, generation1);
+            return AUDIO_JAVA_ERROR;
         }
         ALOGV("listAudioPatches patch %zu num_sources %d num_sinks %d",
               i, nPatches[i].num_sources, nPatches[i].num_sinks);
@@ -1877,96 +1780,66 @@
         env->SetIntField(patchHandle, gAudioHandleFields.mId, nPatches[i].id);
 
         // load sources
-        jSources = env->NewObjectArray(nPatches[i].num_sources,
-                                       gAudioPortConfigClass, NULL);
-        if (jSources == NULL) {
-            jStatus = AUDIO_JAVA_ERROR;
-            goto exit;
+        jSources.reset(env->NewObjectArray(nPatches[i].num_sources, gAudioPortConfigClass, NULL));
+        if (jSources == nullptr) {
+            setGeneration(env, jGeneration, generation1);
+            return AUDIO_JAVA_ERROR;
         }
 
         for (size_t j = 0; j < nPatches[i].num_sources; j++) {
-            jStatus = convertAudioPortConfigFromNative(env,
-                                                      NULL,
-                                                      &jSource,
-                                                      &nPatches[i].sources[j]);
+            ScopedLocalRef<jobject> jSource(env, nullptr);
+            ScopedLocalRef<jobject> jAudioPort(env, nullptr);
+            jStatus = convertAudioPortConfigFromNative(env, &jAudioPort, &jSource,
+                                                       &nPatches[i].sources[j]);
             if (jStatus != AUDIO_JAVA_SUCCESS) {
-                goto exit;
+                if (!setGeneration(env, jGeneration, generation1)) {
+                    jStatus = AUDIO_JAVA_ERROR;
+                }
+                return jStatus;
             }
-            env->SetObjectArrayElement(jSources, j, jSource);
-            env->DeleteLocalRef(jSource);
-            jSource = NULL;
+            env->SetObjectArrayElement(jSources.get(), j, jSource.get());
             ALOGV("listAudioPatches patch %zu source %zu is a %s handle %d",
                   i, j,
                   nPatches[i].sources[j].type == AUDIO_PORT_TYPE_DEVICE ? "device" : "mix",
                   nPatches[i].sources[j].id);
         }
         // load sinks
-        jSinks = env->NewObjectArray(nPatches[i].num_sinks,
-                                     gAudioPortConfigClass, NULL);
-        if (jSinks == NULL) {
-            jStatus = AUDIO_JAVA_ERROR;
-            goto exit;
+        jSinks.reset(env->NewObjectArray(nPatches[i].num_sinks, gAudioPortConfigClass, NULL));
+        if (jSinks == nullptr) {
+            setGeneration(env, jGeneration, generation1);
+            return AUDIO_JAVA_ERROR;
         }
 
         for (size_t j = 0; j < nPatches[i].num_sinks; j++) {
-            jStatus = convertAudioPortConfigFromNative(env,
-                                                      NULL,
-                                                      &jSink,
-                                                      &nPatches[i].sinks[j]);
+            ScopedLocalRef<jobject> jSink(env, nullptr);
+            ScopedLocalRef<jobject> jAudioPort(env, nullptr);
+            jStatus = convertAudioPortConfigFromNative(env, &jAudioPort, &jSink,
+                                                       &nPatches[i].sinks[j]);
 
             if (jStatus != AUDIO_JAVA_SUCCESS) {
-                goto exit;
+                if (!setGeneration(env, jGeneration, generation1)) {
+                    jStatus = AUDIO_JAVA_ERROR;
+                }
+                return jStatus;
             }
-            env->SetObjectArrayElement(jSinks, j, jSink);
-            env->DeleteLocalRef(jSink);
-            jSink = NULL;
+            env->SetObjectArrayElement(jSinks.get(), j, jSink.get());
             ALOGV("listAudioPatches patch %zu sink %zu is a %s handle %d",
                   i, j,
                   nPatches[i].sinks[j].type == AUDIO_PORT_TYPE_DEVICE ? "device" : "mix",
                   nPatches[i].sinks[j].id);
         }
 
-        jPatch = env->NewObject(gAudioPatchClass, gAudioPatchCstor,
-                                       patchHandle, jSources, jSinks);
-        env->DeleteLocalRef(jSources);
-        jSources = NULL;
-        env->DeleteLocalRef(jSinks);
-        jSinks = NULL;
-        if (jPatch == NULL) {
-            jStatus = AUDIO_JAVA_ERROR;
-            goto exit;
+        jPatch.reset(env->NewObject(gAudioPatchClass, gAudioPatchCstor, patchHandle, jSources.get(),
+                                    jSinks.get()));
+        if (jPatch == nullptr) {
+            setGeneration(env, jGeneration, generation1);
+            return AUDIO_JAVA_ERROR;
         }
-        env->CallBooleanMethod(jPatches, gArrayListMethods.add, jPatch);
-        env->DeleteLocalRef(jPatch);
-        jPatch = NULL;
+        env->CallBooleanMethod(jPatches, gArrayListMethods.add, jPatch.get());
     }
-
-exit:
-
-    nGeneration = env->GetIntArrayElements(jGeneration, NULL);
-    if (nGeneration == NULL) {
+    if (!setGeneration(env, jGeneration, generation1)) {
         jStatus = AUDIO_JAVA_ERROR;
-    } else {
-        nGeneration[0] = generation1;
-        env->ReleaseIntArrayElements(jGeneration, nGeneration, 0);
     }
-
-    if (jSources != NULL) {
-        env->DeleteLocalRef(jSources);
-    }
-    if (jSource != NULL) {
-        env->DeleteLocalRef(jSource);
-    }
-    if (jSinks != NULL) {
-        env->DeleteLocalRef(jSinks);
-    }
-    if (jSink != NULL) {
-        env->DeleteLocalRef(jSink);
-    }
-    if (jPatch != NULL) {
-        env->DeleteLocalRef(jPatch);
-    }
-    free(nPatches);
     return jStatus;
 }
 
@@ -2015,7 +1888,7 @@
     }
     auto paa = JNIAudioAttributeHelper::makeUnique();
     jStatus = JNIAudioAttributeHelper::nativeFromJava(env, jAudioAttributes, paa.get());
-    if (jStatus != (jint)AUDIO_JAVA_SUCCESS) {
+    if (jStatus != AUDIO_JAVA_SUCCESS) {
         return jStatus;
     }
     audio_port_handle_t handle;
@@ -2032,8 +1905,7 @@
 android_media_AudioSystem_stopAudioSource(JNIEnv *env, jobject clazz, jint handle)
 {
     ALOGV("stopAudioSource");
-    status_t status = AudioSystem::stopAudioSource(
-            static_cast <audio_port_handle_t>(handle));
+    status_t status = AudioSystem::stopAudioSource(static_cast<audio_port_handle_t>(handle));
     ALOGV("AudioSystem::stopAudioSource() returned %d", status);
     return nativeToJavaStatus(status);
 }
@@ -2065,7 +1937,7 @@
 static jint
 android_media_AudioSystem_getAudioHwSyncForSession(JNIEnv *env, jobject thiz, jint sessionId)
 {
-    return (jint) AudioSystem::getAudioHwSyncForSession((audio_session_t) sessionId);
+    return AudioSystem::getAudioHwSyncForSession(static_cast<audio_session_t>(sessionId));
 }
 
 static void
@@ -2110,11 +1982,11 @@
 {
     nAudioMix->mMixType = env->GetIntField(jAudioMix, gAudioMixFields.mMixType);
     nAudioMix->mRouteFlags = env->GetIntField(jAudioMix, gAudioMixFields.mRouteFlags);
-    nAudioMix->mDeviceType = (audio_devices_t)
-            env->GetIntField(jAudioMix, gAudioMixFields.mDeviceType);
+    nAudioMix->mDeviceType =
+            static_cast<audio_devices_t>(env->GetIntField(jAudioMix, gAudioMixFields.mDeviceType));
 
-    jstring jDeviceAddress = (jstring)env->GetObjectField(jAudioMix,
-                                                           gAudioMixFields.mDeviceAddress);
+    jstring jDeviceAddress =
+            static_cast<jstring>(env->GetObjectField(jAudioMix, gAudioMixFields.mDeviceAddress));
     const char *nDeviceAddress = env->GetStringUTFChars(jDeviceAddress, NULL);
     nAudioMix->mDeviceAddress = String8(nDeviceAddress);
     env->ReleaseStringUTFChars(jDeviceAddress, nDeviceAddress);
@@ -2133,8 +2005,8 @@
     nAudioMix->mVoiceCommunicationCaptureAllowed =
             env->GetBooleanField(jRule, gAudioMixingRuleFields.mVoiceCommunicationCaptureAllowed);
     env->DeleteLocalRef(jRule);
-    jobjectArray jCriteria = (jobjectArray)env->CallObjectMethod(jRuleCriteria,
-                                                                 gArrayListMethods.toArray);
+    jobjectArray jCriteria = static_cast<jobjectArray>(
+            env->CallObjectMethod(jRuleCriteria, gArrayListMethods.toArray));
     env->DeleteLocalRef(jRuleCriteria);
 
     jint numCriteria = env->GetArrayLength(jCriteria);
@@ -2165,8 +2037,8 @@
 
             auto paa = JNIAudioAttributeHelper::makeUnique();
             jint jStatus = JNIAudioAttributeHelper::nativeFromJava(env, jAttributes, paa.get());
-            if (jStatus != (jint)AUDIO_JAVA_SUCCESS) {
-                return jStatus;
+            if (jStatus != AUDIO_JAVA_SUCCESS) {
+                    return jStatus;
             }
             if (match_rule == RULE_MATCH_ATTRIBUTE_USAGE) {
                 nCriterion.mValue.mUsage = paa->usage;
@@ -2184,7 +2056,7 @@
 
     env->DeleteLocalRef(jCriteria);
 
-    return (jint)AUDIO_JAVA_SUCCESS;
+    return AUDIO_JAVA_SUCCESS;
 }
 
 static jint
@@ -2194,34 +2066,29 @@
     ALOGV("registerPolicyMixes");
 
     if (jMixesList == NULL) {
-        return (jint)AUDIO_JAVA_BAD_VALUE;
+        return AUDIO_JAVA_BAD_VALUE;
     }
     if (!env->IsInstanceOf(jMixesList, gArrayListClass)) {
-        return (jint)AUDIO_JAVA_BAD_VALUE;
+        return AUDIO_JAVA_BAD_VALUE;
     }
-    jobjectArray jMixes = (jobjectArray)env->CallObjectMethod(jMixesList,
-                                                              gArrayListMethods.toArray);
+    jobjectArray jMixes =
+            static_cast<jobjectArray>(env->CallObjectMethod(jMixesList, gArrayListMethods.toArray));
     jint numMixes = env->GetArrayLength(jMixes);
     if (numMixes > MAX_MIXES_PER_POLICY) {
         numMixes = MAX_MIXES_PER_POLICY;
     }
 
     status_t status;
-    jint jStatus;
-    jobject jAudioMix = NULL;
     Vector <AudioMix> mixes;
     for (jint i = 0; i < numMixes; i++) {
-        jAudioMix = env->GetObjectArrayElement(jMixes, i);
-        if (!env->IsInstanceOf(jAudioMix, gAudioMixClass)) {
-            jStatus = (jint)AUDIO_JAVA_BAD_VALUE;
-            goto exit;
+        ScopedLocalRef<jobject> jAudioMix(env, env->GetObjectArrayElement(jMixes, i));
+        if (!env->IsInstanceOf(jAudioMix.get(), gAudioMixClass)) {
+            return AUDIO_JAVA_BAD_VALUE;
         }
         AudioMix mix;
-        jStatus = convertAudioMixToNative(env, &mix, jAudioMix);
-        env->DeleteLocalRef(jAudioMix);
-        jAudioMix = NULL;
-        if (jStatus != AUDIO_JAVA_SUCCESS) {
-            goto exit;
+        if (jint jStatus = convertAudioMixToNative(env, &mix, jAudioMix.get());
+            jStatus != AUDIO_JAVA_SUCCESS) {
+            return jStatus;
         }
         mixes.add(mix);
     }
@@ -2230,16 +2097,7 @@
     status = AudioSystem::registerPolicyMixes(mixes, registration);
     ALOGV("AudioSystem::registerPolicyMixes() returned %d", status);
 
-    jStatus = nativeToJavaStatus(status);
-    if (jStatus != AUDIO_JAVA_SUCCESS) {
-        goto exit;
-    }
-
-exit:
-    if (jAudioMix != NULL) {
-        env->DeleteLocalRef(jAudioMix);
-    }
-    return jStatus;
+    return nativeToJavaStatus(status);
 }
 
 static jint android_media_AudioSystem_setUidDeviceAffinities(JNIEnv *env, jobject clazz,
@@ -2249,14 +2107,14 @@
     if (results != NO_ERROR) {
         return results;
     }
-    status_t status = AudioSystem::setUidDeviceAffinities((uid_t) uid, deviceVector);
-    return (jint) nativeToJavaStatus(status);
+    status_t status = AudioSystem::setUidDeviceAffinities(uid, deviceVector);
+    return nativeToJavaStatus(status);
 }
 
 static jint android_media_AudioSystem_removeUidDeviceAffinities(JNIEnv *env, jobject clazz,
         jint uid) {
-    status_t status = AudioSystem::removeUidDeviceAffinities((uid_t) uid);
-    return (jint) nativeToJavaStatus(status);
+    status_t status = AudioSystem::removeUidDeviceAffinities(static_cast<uid_t>(uid));
+    return nativeToJavaStatus(status);
 }
 
 static jint android_media_AudioSystem_setUserIdDeviceAffinities(JNIEnv *env, jobject clazz,
@@ -2267,14 +2125,14 @@
     if (results != NO_ERROR) {
         return results;
     }
-    status_t status = AudioSystem::setUserIdDeviceAffinities((int)userId, deviceVector);
-    return (jint)nativeToJavaStatus(status);
+    status_t status = AudioSystem::setUserIdDeviceAffinities(userId, deviceVector);
+    return nativeToJavaStatus(status);
 }
 
 static jint android_media_AudioSystem_removeUserIdDeviceAffinities(JNIEnv *env, jobject clazz,
                                                                    jint userId) {
-    status_t status = AudioSystem::removeUserIdDeviceAffinities((int)userId);
-    return (jint)nativeToJavaStatus(status);
+    status_t status = AudioSystem::removeUserIdDeviceAffinities(userId);
+    return nativeToJavaStatus(status);
 }
 
 static jint
@@ -2287,19 +2145,18 @@
 android_media_AudioSystem_getStreamVolumeDB(JNIEnv *env, jobject thiz,
                                             jint stream, jint index, jint device)
 {
-    return (jfloat)AudioSystem::getStreamVolumeDB((audio_stream_type_t)stream,
-                                                  (int)index,
-                                                  (audio_devices_t)device);
+    return AudioSystem::getStreamVolumeDB(static_cast<audio_stream_type_t>(stream), index,
+                                          static_cast<audio_devices_t>(device));
 }
 
 static jint android_media_AudioSystem_getOffloadSupport(JNIEnv *env, jobject thiz, jint encoding,
                                                         jint sampleRate, jint channelMask,
                                                         jint channelIndexMask, jint streamType) {
     audio_offload_info_t format = AUDIO_INFO_INITIALIZER;
-    format.format = (audio_format_t) audioFormatToNative(encoding);
-    format.sample_rate = (uint32_t) sampleRate;
+    format.format = static_cast<audio_format_t>(audioFormatToNative(encoding));
+    format.sample_rate = sampleRate;
     format.channel_mask = nativeChannelMaskFromJavaChannelMasks(channelMask, channelIndexMask);
-    format.stream_type = (audio_stream_type_t) streamType;
+    format.stream_type = static_cast<audio_stream_type_t>(streamType);
     format.has_video = false;
     format.is_streaming = false;
     // offload duration unknown at this point:
@@ -2316,11 +2173,11 @@
 
     if (jMicrophonesInfo == NULL) {
         ALOGE("jMicrophonesInfo NULL MicrophoneInfo ArrayList");
-        return (jint)AUDIO_JAVA_BAD_VALUE;
+        return AUDIO_JAVA_BAD_VALUE;
     }
     if (!env->IsInstanceOf(jMicrophonesInfo, gArrayListClass)) {
         ALOGE("getMicrophones not an arraylist");
-        return (jint)AUDIO_JAVA_BAD_VALUE;
+        return AUDIO_JAVA_BAD_VALUE;
     }
 
     jint jStatus;
@@ -2332,7 +2189,7 @@
         return jStatus;
     }
     if (microphones.size() == 0) {
-        jStatus = (jint)AUDIO_JAVA_SUCCESS;
+        jStatus = AUDIO_JAVA_SUCCESS;
         return jStatus;
     }
     for (size_t i = 0; i < microphones.size(); i++) {
@@ -2354,7 +2211,7 @@
     jint jStatus = AUDIO_JAVA_SUCCESS;
     if (!env->IsInstanceOf(jEncodingFormatList, gArrayListClass)) {
         ALOGE("%s: jEncodingFormatList not an ArrayList", __FUNCTION__);
-        return (jint)AUDIO_JAVA_BAD_VALUE;
+        return AUDIO_JAVA_BAD_VALUE;
     }
     std::vector<audio_format_t> encodingFormats;
     status_t status =
@@ -2473,12 +2330,10 @@
 android_media_AudioSystem_setSurroundFormatEnabled(JNIEnv *env, jobject thiz,
                                                    jint audioFormat, jboolean enabled)
 {
-    status_t status = AudioSystem::setSurroundFormatEnabled(audioFormatToNative(audioFormat),
-                                                            (bool)enabled);
-    if (status != NO_ERROR) {
-        ALOGE_IF(status != NO_ERROR, "AudioSystem::setSurroundFormatEnabled error %d", status);
-    }
-    return (jint)nativeToJavaStatus(status);
+    status_t status =
+            AudioSystem::setSurroundFormatEnabled(audioFormatToNative(audioFormat), enabled);
+    ALOGE_IF(status != NO_ERROR, "AudioSystem::setSurroundFormatEnabled error %d", status);
+    return nativeToJavaStatus(status);
 }
 
 static jint android_media_AudioSystem_getMaxChannelCount(JNIEnv *env, jobject thiz) {
@@ -2519,7 +2374,7 @@
 
     status_t status = AudioSystem::setAssistantServicesUids(nativeUidsVector);
 
-    return (jint)nativeToJavaStatus(status);
+    return nativeToJavaStatus(status);
 }
 
 static jint android_media_AudioSystem_setActiveAssistantServicesUids(JNIEnv *env, jobject thiz,
@@ -2528,7 +2383,7 @@
 
     status_t status = AudioSystem::setActiveAssistantServicesUids(nativeActiveUidsVector);
 
-    return (jint)nativeToJavaStatus(status);
+    return nativeToJavaStatus(status);
 }
 
 static jint
@@ -2536,12 +2391,12 @@
     std::vector<uid_t> nativeUidsVector = convertJIntArrayToUidVector(env, uids);
 
     status_t status = AudioSystem::setA11yServicesUids(nativeUidsVector);
-    return (jint)nativeToJavaStatus(status);
+    return nativeToJavaStatus(status);
 }
 
 static jint android_media_AudioSystem_setCurrentImeUid(JNIEnv *env, jobject thiz, jint uid) {
     status_t status = AudioSystem::setCurrentImeUid(uid);
-    return (jint)nativeToJavaStatus(status);
+    return nativeToJavaStatus(status);
 }
 
 static jboolean
@@ -2559,7 +2414,7 @@
     std::vector<audio_usage_t> nativeSystemUsagesVector;
 
     if (systemUsages == nullptr) {
-        return (jint) AUDIO_JAVA_BAD_VALUE;
+        return AUDIO_JAVA_BAD_VALUE;
     }
 
     int *nativeSystemUsages = nullptr;
@@ -2576,7 +2431,7 @@
     }
 
     status_t status = AudioSystem::setSupportedSystemUsages(nativeSystemUsagesVector);
-    return (jint)nativeToJavaStatus(status);
+    return nativeToJavaStatus(status);
 }
 
 static jint
@@ -2587,16 +2442,16 @@
 static jint
 android_media_AudioSystem_setRttEnabled(JNIEnv *env, jobject thiz, jboolean enabled)
 {
-    return (jint) check_AudioSystem_Command(AudioSystem::setRttEnabled(enabled));
+    return check_AudioSystem_Command(AudioSystem::setRttEnabled(enabled));
 }
 
 static jint
 android_media_AudioSystem_setAudioHalPids(JNIEnv *env, jobject clazz, jintArray jPids)
 {
     if (jPids == NULL) {
-        return (jint) AUDIO_JAVA_BAD_VALUE;
+        return AUDIO_JAVA_BAD_VALUE;
     }
-    pid_t *nPidsArray = (pid_t *) env->GetIntArrayElements(jPids, NULL);
+    pid_t *nPidsArray = reinterpret_cast<pid_t *>(env->GetIntArrayElements(jPids, nullptr));
     std::vector<pid_t> nPids(nPidsArray, nPidsArray + env->GetArrayLength(jPids));
     status_t status = AudioSystem::setAudioHalPids(nPids);
     env->ReleaseIntArrayElements(jPids, nPidsArray, 0);
@@ -2620,19 +2475,17 @@
         return results;
     }
     int status = check_AudioSystem_Command(
-            AudioSystem::setDevicesRoleForStrategy((product_strategy_t)strategy,
-                                                   (device_role_t)role, nDevices));
-    return (jint) status;
+            AudioSystem::setDevicesRoleForStrategy(static_cast<product_strategy_t>(strategy),
+                                                   static_cast<device_role_t>(role), nDevices));
+    return status;
 }
 
 static jint android_media_AudioSystem_removeDevicesRoleForStrategy(JNIEnv *env, jobject thiz,
                                                                    jint strategy, jint role) {
-    return (jint)
-            check_AudioSystem_Command(AudioSystem::removeDevicesRoleForStrategy((product_strategy_t)
-                                                                                        strategy,
-                                                                                (device_role_t)
-                                                                                        role),
-                                      {NAME_NOT_FOUND});
+    return check_AudioSystem_Command(
+            AudioSystem::removeDevicesRoleForStrategy(static_cast<product_strategy_t>(strategy),
+                                                      static_cast<device_role_t>(role)),
+            {NAME_NOT_FOUND});
 }
 
 static jint android_media_AudioSystem_getDevicesForRoleAndStrategy(JNIEnv *env, jobject thiz,
@@ -2640,10 +2493,10 @@
                                                                    jobject jDevices) {
     AudioDeviceTypeAddrVector nDevices;
     status_t status = check_AudioSystem_Command(
-            AudioSystem::getDevicesForRoleAndStrategy((product_strategy_t)strategy,
-                                                      (device_role_t)role, nDevices));
+            AudioSystem::getDevicesForRoleAndStrategy(static_cast<product_strategy_t>(strategy),
+                                                      static_cast<device_role_t>(role), nDevices));
     if (status != NO_ERROR) {
-        return (jint) status;
+        return status;
     }
     for (const auto &device : nDevices) {
         jobject jAudioDeviceAttributes = NULL;
@@ -2666,9 +2519,10 @@
         return results;
     }
     int status = check_AudioSystem_Command(
-            AudioSystem::setDevicesRoleForCapturePreset((audio_source_t)capturePreset,
-                                                        (device_role_t)role, nDevices));
-    return (jint)status;
+            AudioSystem::setDevicesRoleForCapturePreset(static_cast<audio_source_t>(capturePreset),
+                                                        static_cast<device_role_t>(role),
+                                                        nDevices));
+    return status;
 }
 
 static jint android_media_AudioSystem_addDevicesRoleForCapturePreset(
@@ -2680,9 +2534,10 @@
         return results;
     }
     int status = check_AudioSystem_Command(
-            AudioSystem::addDevicesRoleForCapturePreset((audio_source_t)capturePreset,
-                                                        (device_role_t)role, nDevices));
-    return (jint)status;
+            AudioSystem::addDevicesRoleForCapturePreset(static_cast<audio_source_t>(capturePreset),
+                                                        static_cast<device_role_t>(role),
+                                                        nDevices));
+    return status;
 }
 
 static jint android_media_AudioSystem_removeDevicesRoleForCapturePreset(
@@ -2694,17 +2549,20 @@
         return results;
     }
     int status = check_AudioSystem_Command(
-            AudioSystem::removeDevicesRoleForCapturePreset((audio_source_t)capturePreset,
-                                                           (device_role_t)role, nDevices));
-    return (jint)status;
+            AudioSystem::removeDevicesRoleForCapturePreset(static_cast<audio_source_t>(
+                                                                   capturePreset),
+                                                           static_cast<device_role_t>(role),
+                                                           nDevices));
+    return status;
 }
 
 static jint android_media_AudioSystem_clearDevicesRoleForCapturePreset(JNIEnv *env, jobject thiz,
                                                                        jint capturePreset,
                                                                        jint role) {
-    return (jint)check_AudioSystem_Command(
-            AudioSystem::clearDevicesRoleForCapturePreset((audio_source_t)capturePreset,
-                                                          (device_role_t)role));
+    return static_cast<jint>(check_AudioSystem_Command(
+            AudioSystem::clearDevicesRoleForCapturePreset(static_cast<audio_source_t>(
+                                                                  capturePreset),
+                                                          static_cast<device_role_t>(role))));
 }
 
 static jint android_media_AudioSystem_getDevicesForRoleAndCapturePreset(JNIEnv *env, jobject thiz,
@@ -2713,10 +2571,12 @@
                                                                         jobject jDevices) {
     AudioDeviceTypeAddrVector nDevices;
     status_t status = check_AudioSystem_Command(
-            AudioSystem::getDevicesForRoleAndCapturePreset((audio_source_t)capturePreset,
-                                                           (device_role_t)role, nDevices));
+            AudioSystem::getDevicesForRoleAndCapturePreset(static_cast<audio_source_t>(
+                                                                   capturePreset),
+                                                           static_cast<device_role_t>(role),
+                                                           nDevices));
     if (status != NO_ERROR) {
-        return (jint)status;
+        return status;
     }
     for (const auto &device : nDevices) {
         jobject jAudioDeviceAttributes = NULL;
@@ -2741,12 +2601,12 @@
     // components call this method often
     if (jDeviceArray == nullptr || maxResultSize == 0) {
         ALOGE("%s invalid array to store AudioDeviceAttributes", __FUNCTION__);
-        return (jint)AUDIO_JAVA_BAD_VALUE;
+        return AUDIO_JAVA_BAD_VALUE;
     }
 
     JNIAudioAttributeHelper::UniqueAaPtr paa = JNIAudioAttributeHelper::makeUnique();
     jint jStatus = JNIAudioAttributeHelper::nativeFromJava(env, jaa, paa.get());
-    if (jStatus != (jint) AUDIO_JAVA_SUCCESS) {
+    if (jStatus != AUDIO_JAVA_SUCCESS) {
         return jStatus;
     }
 
@@ -2775,7 +2635,7 @@
 static jint android_media_AudioSystem_setVibratorInfos(JNIEnv *env, jobject thiz,
                                                        jobject jVibrators) {
     if (!env->IsInstanceOf(jVibrators, gListClass)) {
-        return (jint)AUDIO_JAVA_BAD_VALUE;
+        return AUDIO_JAVA_BAD_VALUE;
     }
     const jint size = env->CallIntMethod(jVibrators, gListMethods.size);
     std::vector<media::AudioVibratorInfo> vibratorInfos;
@@ -2783,7 +2643,7 @@
         ScopedLocalRef<jobject> jVibrator(env,
                                           env->CallObjectMethod(jVibrators, gListMethods.get, i));
         if (!env->IsInstanceOf(jVibrator.get(), gVibratorClass)) {
-            return (jint)AUDIO_JAVA_BAD_VALUE;
+            return AUDIO_JAVA_BAD_VALUE;
         }
         media::AudioVibratorInfo vibratorInfo;
         vibratorInfo.id = env->CallIntMethod(jVibrator.get(), gVibratorMethods.getId);
@@ -2794,7 +2654,7 @@
                 env->CallFloatMethod(jVibrator.get(), gVibratorMethods.getMaxAmplitude);
         vibratorInfos.push_back(vibratorInfo);
     }
-    return (jint)check_AudioSystem_Command(AudioSystem::setVibratorInfos(vibratorInfos));
+    return check_AudioSystem_Command(AudioSystem::setVibratorInfos(vibratorInfos));
 }
 
 static jobject android_media_AudioSystem_getSpatializer(JNIEnv *env, jobject thiz,
@@ -2816,8 +2676,8 @@
                                                        jobjectArray jDeviceArray) {
     JNIAudioAttributeHelper::UniqueAaPtr paa = JNIAudioAttributeHelper::makeUnique();
     jint jStatus = JNIAudioAttributeHelper::nativeFromJava(env, jaa, paa.get());
-    if (jStatus != (jint)AUDIO_JAVA_SUCCESS) {
-       return false;
+    if (jStatus != AUDIO_JAVA_SUCCESS) {
+        return false;
     }
 
     AudioDeviceTypeAddrVector nDevices;
@@ -2830,7 +2690,7 @@
             return false;
         }
         jStatus = createAudioDeviceTypeAddrFromJava(env, &device, jDevice);
-        if (jStatus != (jint)AUDIO_JAVA_SUCCESS) {
+        if (jStatus != AUDIO_JAVA_SUCCESS) {
             return false;
         }
         nDevices.push_back(device);
@@ -2873,7 +2733,7 @@
                                                                jobject jFormat, jobject jaa) {
     JNIAudioAttributeHelper::UniqueAaPtr paa = JNIAudioAttributeHelper::makeUnique();
     jint jStatus = JNIAudioAttributeHelper::nativeFromJava(env, jaa, paa.get());
-    if (jStatus != (jint)AUDIO_JAVA_SUCCESS) {
+    if (jStatus != AUDIO_JAVA_SUCCESS) {
         return DIRECT_NOT_SUPPORTED;
     }
 
@@ -2896,20 +2756,20 @@
 
     if (jAudioAttributes == nullptr) {
         ALOGE("jAudioAttributes is NULL");
-        return (jint)AUDIO_JAVA_BAD_VALUE;
+        return AUDIO_JAVA_BAD_VALUE;
     }
     if (jAudioProfilesList == nullptr) {
         ALOGE("jAudioProfilesList is NULL");
-        return (jint)AUDIO_JAVA_BAD_VALUE;
+        return AUDIO_JAVA_BAD_VALUE;
     }
     if (!env->IsInstanceOf(jAudioProfilesList, gArrayListClass)) {
         ALOGE("jAudioProfilesList not an ArrayList");
-        return (jint)AUDIO_JAVA_BAD_VALUE;
+        return AUDIO_JAVA_BAD_VALUE;
     }
 
     JNIAudioAttributeHelper::UniqueAaPtr paa = JNIAudioAttributeHelper::makeUnique();
     jint jStatus = JNIAudioAttributeHelper::nativeFromJava(env, jAudioAttributes, paa.get());
-    if (jStatus != (jint)AUDIO_JAVA_SUCCESS) {
+    if (jStatus != AUDIO_JAVA_SUCCESS) {
         return jStatus;
     }
 
@@ -2922,7 +2782,7 @@
     }
 
     for (const auto &audioProfile : audioProfiles) {
-        jobject jAudioProfile;
+        ScopedLocalRef<jobject> jAudioProfile(env);
         jint jConvertProfileStatus = convertAudioProfileFromNative(
                                         env, &jAudioProfile, &audioProfile, false);
         if (jConvertProfileStatus == AUDIO_JAVA_BAD_VALUE) {
@@ -2932,8 +2792,7 @@
         if (jConvertProfileStatus != AUDIO_JAVA_SUCCESS) {
             return jConvertProfileStatus;
         }
-        env->CallBooleanMethod(jAudioProfilesList, gArrayListMethods.add, jAudioProfile);
-        env->DeleteLocalRef(jAudioProfile);
+        env->CallBooleanMethod(jAudioProfilesList, gArrayListMethods.add, jAudioProfile.get());
     }
     return jStatus;
 }
@@ -2949,8 +2808,7 @@
 
 static int android_media_AudioSystem_setBluetoothVariableLatencyEnabled(JNIEnv *env, jobject thiz,
                                                                         jboolean enabled) {
-    return (jint)check_AudioSystem_Command(
-            AudioSystem::setBluetoothVariableLatencyEnabled(enabled));
+    return check_AudioSystem_Command(AudioSystem::setBluetoothVariableLatencyEnabled(enabled));
 }
 
 static jboolean android_media_AudioSystem_isBluetoothVariableLatencyEnabled(JNIEnv *env,
@@ -2964,178 +2822,165 @@
 
 // ----------------------------------------------------------------------------
 
+#define MAKE_AUDIO_SYSTEM_METHOD(x) \
+    MAKE_JNI_NATIVE_METHOD_AUTOSIG(#x, android_media_AudioSystem_##x)
+
 static const JNINativeMethod gMethods[] =
-        {{"setParameters", "(Ljava/lang/String;)I",
-          (void *)android_media_AudioSystem_setParameters},
-         {"getParameters", "(Ljava/lang/String;)Ljava/lang/String;",
-          (void *)android_media_AudioSystem_getParameters},
-         {"muteMicrophone", "(Z)I", (void *)android_media_AudioSystem_muteMicrophone},
-         {"isMicrophoneMuted", "()Z", (void *)android_media_AudioSystem_isMicrophoneMuted},
-         {"isStreamActive", "(II)Z", (void *)android_media_AudioSystem_isStreamActive},
-         {"isStreamActiveRemotely", "(II)Z",
-          (void *)android_media_AudioSystem_isStreamActiveRemotely},
-         {"isSourceActive", "(I)Z", (void *)android_media_AudioSystem_isSourceActive},
-         {"newAudioSessionId", "()I", (void *)android_media_AudioSystem_newAudioSessionId},
-         {"newAudioPlayerId", "()I", (void *)android_media_AudioSystem_newAudioPlayerId},
-         {"newAudioRecorderId", "()I", (void *)android_media_AudioSystem_newAudioRecorderId},
-         {"setDeviceConnectionState", "(ILandroid/os/Parcel;I)I",
-          (void *)android_media_AudioSystem_setDeviceConnectionState},
-         {"getDeviceConnectionState", "(ILjava/lang/String;)I",
-          (void *)android_media_AudioSystem_getDeviceConnectionState},
-         {"handleDeviceConfigChange", "(ILjava/lang/String;Ljava/lang/String;I)I",
-          (void *)android_media_AudioSystem_handleDeviceConfigChange},
-         {"setPhoneState", "(II)I", (void *)android_media_AudioSystem_setPhoneState},
-         {"setForceUse", "(II)I", (void *)android_media_AudioSystem_setForceUse},
-         {"getForceUse", "(I)I", (void *)android_media_AudioSystem_getForceUse},
-         {"initStreamVolume", "(III)I", (void *)android_media_AudioSystem_initStreamVolume},
-         {"setStreamVolumeIndex", "(III)I", (void *)android_media_AudioSystem_setStreamVolumeIndex},
-         {"getStreamVolumeIndex", "(II)I", (void *)android_media_AudioSystem_getStreamVolumeIndex},
-         {"setVolumeIndexForAttributes", "(Landroid/media/AudioAttributes;II)I",
-          (void *)android_media_AudioSystem_setVolumeIndexForAttributes},
-         {"getVolumeIndexForAttributes", "(Landroid/media/AudioAttributes;I)I",
-          (void *)android_media_AudioSystem_getVolumeIndexForAttributes},
-         {"getMinVolumeIndexForAttributes", "(Landroid/media/AudioAttributes;)I",
-          (void *)android_media_AudioSystem_getMinVolumeIndexForAttributes},
-         {"getMaxVolumeIndexForAttributes", "(Landroid/media/AudioAttributes;)I",
-          (void *)android_media_AudioSystem_getMaxVolumeIndexForAttributes},
-         {"setMasterVolume", "(F)I", (void *)android_media_AudioSystem_setMasterVolume},
-         {"getMasterVolume", "()F", (void *)android_media_AudioSystem_getMasterVolume},
-         {"setMasterMute", "(Z)I", (void *)android_media_AudioSystem_setMasterMute},
-         {"getMasterMute", "()Z", (void *)android_media_AudioSystem_getMasterMute},
-         {"setMasterMono", "(Z)I", (void *)android_media_AudioSystem_setMasterMono},
-         {"getMasterMono", "()Z", (void *)android_media_AudioSystem_getMasterMono},
-         {"setMasterBalance", "(F)I", (void *)android_media_AudioSystem_setMasterBalance},
-         {"getMasterBalance", "()F", (void *)android_media_AudioSystem_getMasterBalance},
-         {"getPrimaryOutputSamplingRate", "()I",
-          (void *)android_media_AudioSystem_getPrimaryOutputSamplingRate},
-         {"getPrimaryOutputFrameCount", "()I",
-          (void *)android_media_AudioSystem_getPrimaryOutputFrameCount},
-         {"getOutputLatency", "(I)I", (void *)android_media_AudioSystem_getOutputLatency},
-         {"setLowRamDevice", "(ZJ)I", (void *)android_media_AudioSystem_setLowRamDevice},
-         {"checkAudioFlinger", "()I", (void *)android_media_AudioSystem_checkAudioFlinger},
-         {"setAudioFlingerBinder", "(Landroid/os/IBinder;)V",
-          (void *)android_media_AudioSystem_setAudioFlingerBinder},
-         {"listAudioPorts", "(Ljava/util/ArrayList;[I)I",
-          (void *)android_media_AudioSystem_listAudioPorts},
-         {"createAudioPatch",
-          "([Landroid/media/AudioPatch;[Landroid/media/AudioPortConfig;[Landroid/media/"
-          "AudioPortConfig;)I",
-          (void *)android_media_AudioSystem_createAudioPatch},
-         {"releaseAudioPatch", "(Landroid/media/AudioPatch;)I",
-          (void *)android_media_AudioSystem_releaseAudioPatch},
-         {"listAudioPatches", "(Ljava/util/ArrayList;[I)I",
-          (void *)android_media_AudioSystem_listAudioPatches},
-         {"setAudioPortConfig", "(Landroid/media/AudioPortConfig;)I",
-          (void *)android_media_AudioSystem_setAudioPortConfig},
-         {"startAudioSource", "(Landroid/media/AudioPortConfig;Landroid/media/AudioAttributes;)I",
-          (void *)android_media_AudioSystem_startAudioSource},
-         {"stopAudioSource", "(I)I", (void *)android_media_AudioSystem_stopAudioSource},
-         {"getAudioHwSyncForSession", "(I)I",
-          (void *)android_media_AudioSystem_getAudioHwSyncForSession},
-         {"registerPolicyMixes", "(Ljava/util/ArrayList;Z)I",
-          (void *)android_media_AudioSystem_registerPolicyMixes},
-         {"setUidDeviceAffinities", "(I[I[Ljava/lang/String;)I",
-          (void *)android_media_AudioSystem_setUidDeviceAffinities},
-         {"removeUidDeviceAffinities", "(I)I",
-          (void *)android_media_AudioSystem_removeUidDeviceAffinities},
-         {"native_register_dynamic_policy_callback", "()V",
-          (void *)android_media_AudioSystem_registerDynPolicyCallback},
-         {"native_register_recording_callback", "()V",
-          (void *)android_media_AudioSystem_registerRecordingCallback},
-         {"native_register_routing_callback", "()V",
-          (void *)android_media_AudioSystem_registerRoutingCallback},
-         {"native_register_vol_range_init_req_callback", "()V",
-          (void *)android_media_AudioSystem_registerVolRangeInitReqCallback},
-         {"systemReady", "()I", (void *)android_media_AudioSystem_systemReady},
-         {"getStreamVolumeDB", "(III)F", (void *)android_media_AudioSystem_getStreamVolumeDB},
-         {"native_get_offload_support", "(IIIII)I",
-          (void *)android_media_AudioSystem_getOffloadSupport},
-         {"getMicrophones", "(Ljava/util/ArrayList;)I",
-          (void *)android_media_AudioSystem_getMicrophones},
-         {"getSurroundFormats", "(Ljava/util/Map;)I",
-          (void *)android_media_AudioSystem_getSurroundFormats},
-         {"getReportedSurroundFormats", "(Ljava/util/ArrayList;)I",
-          (void *)android_media_AudioSystem_getReportedSurroundFormats},
-         {"setSurroundFormatEnabled", "(IZ)I",
-          (void *)android_media_AudioSystem_setSurroundFormatEnabled},
-         {"setAssistantServicesUids", "([I)I",
-          (void *)android_media_AudioSystem_setAssistantServicesUids},
-         {"setActiveAssistantServicesUids", "([I)I",
-          (void *)android_media_AudioSystem_setActiveAssistantServicesUids},
-         {"setA11yServicesUids", "([I)I", (void *)android_media_AudioSystem_setA11yServicesUids},
-         {"isHapticPlaybackSupported", "()Z",
-          (void *)android_media_AudioSystem_isHapticPlaybackSupported},
-         {"isUltrasoundSupported", "()Z", (void *)android_media_AudioSystem_isUltrasoundSupported},
-         {"getHwOffloadFormatsSupportedForBluetoothMedia", "(ILjava/util/ArrayList;)I",
-          (void *)android_media_AudioSystem_getHwOffloadFormatsSupportedForBluetoothMedia},
-         {"setSupportedSystemUsages", "([I)I",
-          (void *)android_media_AudioSystem_setSupportedSystemUsages},
-         {"setAllowedCapturePolicy", "(II)I",
-          (void *)android_media_AudioSystem_setAllowedCapturePolicy},
-         {"setRttEnabled", "(Z)I", (void *)android_media_AudioSystem_setRttEnabled},
-         {"setAudioHalPids", "([I)I", (void *)android_media_AudioSystem_setAudioHalPids},
-         {"isCallScreeningModeSupported", "()Z",
-          (void *)android_media_AudioSystem_isCallScreeningModeSupported},
-         {"setDevicesRoleForStrategy", "(II[I[Ljava/lang/String;)I",
-          (void *)android_media_AudioSystem_setDevicesRoleForStrategy},
-         {"removeDevicesRoleForStrategy", "(II)I",
-          (void *)android_media_AudioSystem_removeDevicesRoleForStrategy},
-         {"getDevicesForRoleAndStrategy", "(IILjava/util/List;)I",
-          (void *)android_media_AudioSystem_getDevicesForRoleAndStrategy},
-         {"setDevicesRoleForCapturePreset", "(II[I[Ljava/lang/String;)I",
-          (void *)android_media_AudioSystem_setDevicesRoleForCapturePreset},
-         {"addDevicesRoleForCapturePreset", "(II[I[Ljava/lang/String;)I",
-          (void *)android_media_AudioSystem_addDevicesRoleForCapturePreset},
-         {"removeDevicesRoleForCapturePreset", "(II[I[Ljava/lang/String;)I",
-          (void *)android_media_AudioSystem_removeDevicesRoleForCapturePreset},
-         {"clearDevicesRoleForCapturePreset", "(II)I",
-          (void *)android_media_AudioSystem_clearDevicesRoleForCapturePreset},
-         {"getDevicesForRoleAndCapturePreset", "(IILjava/util/List;)I",
-          (void *)android_media_AudioSystem_getDevicesForRoleAndCapturePreset},
-         {"getDevicesForAttributes",
-          "(Landroid/media/AudioAttributes;[Landroid/media/AudioDeviceAttributes;Z)I",
-          (void *)android_media_AudioSystem_getDevicesForAttributes},
-         {"setUserIdDeviceAffinities", "(I[I[Ljava/lang/String;)I",
-          (void *)android_media_AudioSystem_setUserIdDeviceAffinities},
-         {"removeUserIdDeviceAffinities", "(I)I",
-          (void *)android_media_AudioSystem_removeUserIdDeviceAffinities},
-         {"setCurrentImeUid", "(I)I", (void *)android_media_AudioSystem_setCurrentImeUid},
-         {"setVibratorInfos", "(Ljava/util/List;)I",
-          (void *)android_media_AudioSystem_setVibratorInfos},
-         {"nativeGetSpatializer",
-          "(Landroid/media/INativeSpatializerCallback;)Landroid/os/IBinder;",
-          (void *)android_media_AudioSystem_getSpatializer},
-         {"canBeSpatialized",
-          "(Landroid/media/AudioAttributes;Landroid/media/AudioFormat;"
-          "[Landroid/media/AudioDeviceAttributes;)Z",
-          (void *)android_media_AudioSystem_canBeSpatialized},
-         {"getDirectPlaybackSupport",
-          "(Landroid/media/AudioFormat;Landroid/media/AudioAttributes;)I",
-          (void *)android_media_AudioSystem_getDirectPlaybackSupport},
-         {"getDirectProfilesForAttributes",
-          "(Landroid/media/AudioAttributes;Ljava/util/ArrayList;)I",
-          (void *)android_media_AudioSystem_getDirectProfilesForAttributes},
-         {"supportsBluetoothVariableLatency", "()Z",
-          (void *)android_media_AudioSystem_supportsBluetoothVariableLatency},
-         {"setBluetoothVariableLatencyEnabled", "(Z)I",
-          (void *)android_media_AudioSystem_setBluetoothVariableLatencyEnabled},
-         {"isBluetoothVariableLatencyEnabled", "()Z",
-          (void *)android_media_AudioSystem_isBluetoothVariableLatencyEnabled}};
+        {MAKE_AUDIO_SYSTEM_METHOD(setParameters),
+         MAKE_AUDIO_SYSTEM_METHOD(getParameters),
+         MAKE_AUDIO_SYSTEM_METHOD(muteMicrophone),
+         MAKE_AUDIO_SYSTEM_METHOD(isMicrophoneMuted),
+         MAKE_AUDIO_SYSTEM_METHOD(isStreamActive),
+         MAKE_AUDIO_SYSTEM_METHOD(isStreamActiveRemotely),
+         MAKE_AUDIO_SYSTEM_METHOD(isSourceActive),
+         MAKE_AUDIO_SYSTEM_METHOD(newAudioSessionId),
+         MAKE_AUDIO_SYSTEM_METHOD(newAudioPlayerId),
+         MAKE_AUDIO_SYSTEM_METHOD(newAudioRecorderId),
+         MAKE_JNI_NATIVE_METHOD("setDeviceConnectionState", "(ILandroid/os/Parcel;I)I",
+                                android_media_AudioSystem_setDeviceConnectionState),
+         MAKE_AUDIO_SYSTEM_METHOD(getDeviceConnectionState),
+         MAKE_AUDIO_SYSTEM_METHOD(handleDeviceConfigChange),
+         MAKE_AUDIO_SYSTEM_METHOD(setPhoneState),
+         MAKE_AUDIO_SYSTEM_METHOD(setForceUse),
+         MAKE_AUDIO_SYSTEM_METHOD(getForceUse),
+         MAKE_AUDIO_SYSTEM_METHOD(initStreamVolume),
+         MAKE_AUDIO_SYSTEM_METHOD(setStreamVolumeIndex),
+         MAKE_AUDIO_SYSTEM_METHOD(getStreamVolumeIndex),
+         MAKE_JNI_NATIVE_METHOD("setVolumeIndexForAttributes",
+                                "(Landroid/media/AudioAttributes;II)I",
+                                android_media_AudioSystem_setVolumeIndexForAttributes),
+         MAKE_JNI_NATIVE_METHOD("getVolumeIndexForAttributes",
+                                "(Landroid/media/AudioAttributes;I)I",
+                                android_media_AudioSystem_getVolumeIndexForAttributes),
+         MAKE_JNI_NATIVE_METHOD("getMinVolumeIndexForAttributes",
+                                "(Landroid/media/AudioAttributes;)I",
+                                android_media_AudioSystem_getMinVolumeIndexForAttributes),
+         MAKE_JNI_NATIVE_METHOD("getMaxVolumeIndexForAttributes",
+                                "(Landroid/media/AudioAttributes;)I",
+                                android_media_AudioSystem_getMaxVolumeIndexForAttributes),
+         MAKE_AUDIO_SYSTEM_METHOD(setMasterVolume),
+         MAKE_AUDIO_SYSTEM_METHOD(getMasterVolume),
+         MAKE_AUDIO_SYSTEM_METHOD(setMasterMute),
+         MAKE_AUDIO_SYSTEM_METHOD(getMasterMute),
+         MAKE_AUDIO_SYSTEM_METHOD(setMasterMono),
+         MAKE_AUDIO_SYSTEM_METHOD(getMasterMono),
+         MAKE_AUDIO_SYSTEM_METHOD(setMasterBalance),
+         MAKE_AUDIO_SYSTEM_METHOD(getMasterBalance),
+         MAKE_AUDIO_SYSTEM_METHOD(getPrimaryOutputSamplingRate),
+         MAKE_AUDIO_SYSTEM_METHOD(getPrimaryOutputFrameCount),
+         MAKE_AUDIO_SYSTEM_METHOD(getOutputLatency),
+         MAKE_AUDIO_SYSTEM_METHOD(setLowRamDevice),
+         MAKE_AUDIO_SYSTEM_METHOD(checkAudioFlinger),
+         MAKE_JNI_NATIVE_METHOD("setAudioFlingerBinder", "(Landroid/os/IBinder;)V",
+                                android_media_AudioSystem_setAudioFlingerBinder),
+         MAKE_JNI_NATIVE_METHOD("listAudioPorts", "(Ljava/util/ArrayList;[I)I",
+                                android_media_AudioSystem_listAudioPorts),
+         MAKE_JNI_NATIVE_METHOD("createAudioPatch",
+                                "([Landroid/media/AudioPatch;[Landroid/media/"
+                                "AudioPortConfig;[Landroid/media/AudioPortConfig;)I",
+                                android_media_AudioSystem_createAudioPatch),
+         MAKE_JNI_NATIVE_METHOD("releaseAudioPatch", "(Landroid/media/AudioPatch;)I",
+                                android_media_AudioSystem_releaseAudioPatch),
+         MAKE_JNI_NATIVE_METHOD("listAudioPatches", "(Ljava/util/ArrayList;[I)I",
+                                android_media_AudioSystem_listAudioPatches),
+         MAKE_JNI_NATIVE_METHOD("setAudioPortConfig", "(Landroid/media/AudioPortConfig;)I",
+                                android_media_AudioSystem_setAudioPortConfig),
+         MAKE_JNI_NATIVE_METHOD("startAudioSource",
+                                "(Landroid/media/AudioPortConfig;Landroid/media/AudioAttributes;)I",
+                                android_media_AudioSystem_startAudioSource),
+         MAKE_AUDIO_SYSTEM_METHOD(stopAudioSource),
+         MAKE_AUDIO_SYSTEM_METHOD(getAudioHwSyncForSession),
+         MAKE_JNI_NATIVE_METHOD("registerPolicyMixes", "(Ljava/util/ArrayList;Z)I",
+                                android_media_AudioSystem_registerPolicyMixes),
+         MAKE_JNI_NATIVE_METHOD("setUidDeviceAffinities", "(I[I[Ljava/lang/String;)I",
+                                android_media_AudioSystem_setUidDeviceAffinities),
+         MAKE_AUDIO_SYSTEM_METHOD(removeUidDeviceAffinities),
+         MAKE_JNI_NATIVE_METHOD_AUTOSIG("native_register_dynamic_policy_callback",
+                                        android_media_AudioSystem_registerDynPolicyCallback),
+         MAKE_JNI_NATIVE_METHOD_AUTOSIG("native_register_recording_callback",
+                                        android_media_AudioSystem_registerRecordingCallback),
+         MAKE_JNI_NATIVE_METHOD_AUTOSIG("native_register_routing_callback",
+                                        android_media_AudioSystem_registerRoutingCallback),
+         MAKE_JNI_NATIVE_METHOD_AUTOSIG("native_register_vol_range_init_req_callback",
+                                        android_media_AudioSystem_registerVolRangeInitReqCallback),
+         MAKE_AUDIO_SYSTEM_METHOD(systemReady),
+         MAKE_AUDIO_SYSTEM_METHOD(getStreamVolumeDB),
+         MAKE_JNI_NATIVE_METHOD_AUTOSIG("native_get_offload_support",
+                                        android_media_AudioSystem_getOffloadSupport),
+         MAKE_JNI_NATIVE_METHOD("getMicrophones", "(Ljava/util/ArrayList;)I",
+                                android_media_AudioSystem_getMicrophones),
+         MAKE_JNI_NATIVE_METHOD("getSurroundFormats", "(Ljava/util/Map;)I",
+                                android_media_AudioSystem_getSurroundFormats),
+         MAKE_JNI_NATIVE_METHOD("getReportedSurroundFormats", "(Ljava/util/ArrayList;)I",
+                                android_media_AudioSystem_getReportedSurroundFormats),
+         MAKE_AUDIO_SYSTEM_METHOD(setSurroundFormatEnabled),
+         MAKE_AUDIO_SYSTEM_METHOD(setAssistantServicesUids),
+         MAKE_AUDIO_SYSTEM_METHOD(setActiveAssistantServicesUids),
+         MAKE_AUDIO_SYSTEM_METHOD(setA11yServicesUids),
+         MAKE_AUDIO_SYSTEM_METHOD(isHapticPlaybackSupported),
+         MAKE_AUDIO_SYSTEM_METHOD(isUltrasoundSupported),
+         MAKE_JNI_NATIVE_METHOD(
+                 "getHwOffloadFormatsSupportedForBluetoothMedia", "(ILjava/util/ArrayList;)I",
+                 android_media_AudioSystem_getHwOffloadFormatsSupportedForBluetoothMedia),
+         MAKE_AUDIO_SYSTEM_METHOD(setSupportedSystemUsages),
+         MAKE_AUDIO_SYSTEM_METHOD(setAllowedCapturePolicy),
+         MAKE_AUDIO_SYSTEM_METHOD(setRttEnabled),
+         MAKE_AUDIO_SYSTEM_METHOD(setAudioHalPids),
+         MAKE_AUDIO_SYSTEM_METHOD(isCallScreeningModeSupported),
+         MAKE_JNI_NATIVE_METHOD("setDevicesRoleForStrategy", "(II[I[Ljava/lang/String;)I",
+                                android_media_AudioSystem_setDevicesRoleForStrategy),
+         MAKE_AUDIO_SYSTEM_METHOD(removeDevicesRoleForStrategy),
+         MAKE_JNI_NATIVE_METHOD("getDevicesForRoleAndStrategy", "(IILjava/util/List;)I",
+                                android_media_AudioSystem_getDevicesForRoleAndStrategy),
+         MAKE_JNI_NATIVE_METHOD("setDevicesRoleForCapturePreset", "(II[I[Ljava/lang/String;)I",
+                                android_media_AudioSystem_setDevicesRoleForCapturePreset),
+         MAKE_JNI_NATIVE_METHOD("addDevicesRoleForCapturePreset", "(II[I[Ljava/lang/String;)I",
+                                android_media_AudioSystem_addDevicesRoleForCapturePreset),
+         MAKE_JNI_NATIVE_METHOD("removeDevicesRoleForCapturePreset", "(II[I[Ljava/lang/String;)I",
+                                android_media_AudioSystem_removeDevicesRoleForCapturePreset),
+         MAKE_AUDIO_SYSTEM_METHOD(clearDevicesRoleForCapturePreset),
+         MAKE_JNI_NATIVE_METHOD("getDevicesForRoleAndCapturePreset", "(IILjava/util/List;)I",
+                                android_media_AudioSystem_getDevicesForRoleAndCapturePreset),
+         MAKE_JNI_NATIVE_METHOD("getDevicesForAttributes",
+                                "(Landroid/media/AudioAttributes;[Landroid/media/"
+                                "AudioDeviceAttributes;Z)I",
+                                android_media_AudioSystem_getDevicesForAttributes),
+         MAKE_JNI_NATIVE_METHOD("setUserIdDeviceAffinities", "(I[I[Ljava/lang/String;)I",
+                                android_media_AudioSystem_setUserIdDeviceAffinities),
+         MAKE_AUDIO_SYSTEM_METHOD(removeUserIdDeviceAffinities),
+         MAKE_AUDIO_SYSTEM_METHOD(setCurrentImeUid),
+         MAKE_JNI_NATIVE_METHOD("setVibratorInfos", "(Ljava/util/List;)I",
+                                android_media_AudioSystem_setVibratorInfos),
+         MAKE_JNI_NATIVE_METHOD("nativeGetSpatializer",
+                                "(Landroid/media/INativeSpatializerCallback;)Landroid/os/IBinder;",
+                                android_media_AudioSystem_getSpatializer),
+         MAKE_JNI_NATIVE_METHOD("canBeSpatialized",
+                                "(Landroid/media/AudioAttributes;Landroid/media/AudioFormat;"
+                                "[Landroid/media/AudioDeviceAttributes;)Z",
+                                android_media_AudioSystem_canBeSpatialized),
+         MAKE_JNI_NATIVE_METHOD("getDirectPlaybackSupport",
+                                "(Landroid/media/AudioFormat;Landroid/media/AudioAttributes;)I",
+                                android_media_AudioSystem_getDirectPlaybackSupport),
+         MAKE_JNI_NATIVE_METHOD("getDirectProfilesForAttributes",
+                                "(Landroid/media/AudioAttributes;Ljava/util/ArrayList;)I",
+                                android_media_AudioSystem_getDirectProfilesForAttributes),
+         MAKE_AUDIO_SYSTEM_METHOD(supportsBluetoothVariableLatency),
+         MAKE_AUDIO_SYSTEM_METHOD(setBluetoothVariableLatencyEnabled),
+         MAKE_AUDIO_SYSTEM_METHOD(isBluetoothVariableLatencyEnabled)};
 
-static const JNINativeMethod gEventHandlerMethods[] = {
-    {"native_setup",
-        "(Ljava/lang/Object;)V",
-        (void *)android_media_AudioSystem_eventHandlerSetup},
-    {"native_finalize",
-        "()V",
-        (void *)android_media_AudioSystem_eventHandlerFinalize},
-};
+static const JNINativeMethod gEventHandlerMethods[] =
+        {MAKE_JNI_NATIVE_METHOD("native_setup", "(Ljava/lang/Object;)V",
+                                android_media_AudioSystem_eventHandlerSetup),
+         MAKE_JNI_NATIVE_METHOD_AUTOSIG("native_finalize",
+                                        android_media_AudioSystem_eventHandlerFinalize)};
 
-static const JNINativeMethod gFrameworkCapabilities[] = {
-        {"native_getMaxChannelCount", "()I", (void *)android_media_AudioSystem_getMaxChannelCount},
-        {"native_getMaxSampleRate", "()I", (void *)android_media_AudioSystem_getMaxSampleRate},
-        {"native_getMinSampleRate", "()I", (void *)android_media_AudioSystem_getMinSampleRate},
-};
+static const JNINativeMethod gFrameworkCapabilities[] =
+        {MAKE_JNI_NATIVE_METHOD_AUTOSIG("native_getMaxChannelCount",
+                                        android_media_AudioSystem_getMaxChannelCount),
+         MAKE_JNI_NATIVE_METHOD_AUTOSIG("native_getMaxSampleRate",
+                                        android_media_AudioSystem_getMaxSampleRate),
+         MAKE_JNI_NATIVE_METHOD_AUTOSIG("native_getMinSampleRate",
+                                        android_media_AudioSystem_getMinSampleRate)};
 
 int register_android_media_AudioSystem(JNIEnv *env)
 {
@@ -3310,7 +3155,7 @@
     gClsAudioTrackRoutingProxy =
             android::FindClassOrDie(env, "android/media/AudioTrackRoutingProxy");
     // make sure this reference doesn't get deleted
-    gClsAudioTrackRoutingProxy = (jclass)env->NewGlobalRef(gClsAudioTrackRoutingProxy);
+    gClsAudioTrackRoutingProxy = static_cast<jclass>(env->NewGlobalRef(gClsAudioTrackRoutingProxy));
 
     gMidAudioTrackRoutingProxy_ctor =
             android::GetMethodIDOrDie(env, gClsAudioTrackRoutingProxy, "<init>", "(J)V");
@@ -3321,7 +3166,8 @@
     gClsAudioRecordRoutingProxy =
             android::FindClassOrDie(env, "android/media/AudioRecordRoutingProxy");
     // make sure this reference doesn't get deleted
-    gClsAudioRecordRoutingProxy = (jclass)env->NewGlobalRef(gClsAudioRecordRoutingProxy);
+    gClsAudioRecordRoutingProxy =
+            static_cast<jclass>(env->NewGlobalRef(gClsAudioRecordRoutingProxy));
 
     gMidAudioRecordRoutingProxy_ctor =
             android::GetMethodIDOrDie(env, gClsAudioRecordRoutingProxy, "<init>", "(J)V");
diff --git a/core/jni/android_util_Binder.cpp b/core/jni/android_util_Binder.cpp
index 8bc52b8..c198797 100644
--- a/core/jni/android_util_Binder.cpp
+++ b/core/jni/android_util_Binder.cpp
@@ -1305,103 +1305,6 @@
     return alive ? JNI_TRUE : JNI_FALSE;
 }
 
-static int getprocname(pid_t pid, char *buf, size_t len) {
-    char filename[32];
-    FILE *f;
-
-    snprintf(filename, sizeof(filename), "/proc/%d/cmdline", pid);
-    f = fopen(filename, "re");
-    if (!f) {
-        *buf = '\0';
-        return 1;
-    }
-    if (!fgets(buf, len, f)) {
-        *buf = '\0';
-        fclose(f);
-        return 2;
-    }
-    fclose(f);
-    return 0;
-}
-
-static bool push_eventlog_string(char** pos, const char* end, const char* str) {
-    jint len = strlen(str);
-    int space_needed = 1 + sizeof(len) + len;
-    if (end - *pos < space_needed) {
-        ALOGW("not enough space for string. remain=%" PRIdPTR "; needed=%d",
-             end - *pos, space_needed);
-        return false;
-    }
-    **pos = EVENT_TYPE_STRING;
-    (*pos)++;
-    memcpy(*pos, &len, sizeof(len));
-    *pos += sizeof(len);
-    memcpy(*pos, str, len);
-    *pos += len;
-    return true;
-}
-
-static bool push_eventlog_int(char** pos, const char* end, jint val) {
-    int space_needed = 1 + sizeof(val);
-    if (end - *pos < space_needed) {
-        ALOGW("not enough space for int.  remain=%" PRIdPTR "; needed=%d",
-             end - *pos, space_needed);
-        return false;
-    }
-    **pos = EVENT_TYPE_INT;
-    (*pos)++;
-    memcpy(*pos, &val, sizeof(val));
-    *pos += sizeof(val);
-    return true;
-}
-
-// From frameworks/base/core/java/android/content/EventLogTags.logtags:
-
-static const bool kEnableBinderSample = false;
-
-#define LOGTAG_BINDER_OPERATION 52004
-
-static void conditionally_log_binder_call(int64_t start_millis,
-                                          IBinder* target, jint code) {
-    int duration_ms = static_cast<int>(uptimeMillis() - start_millis);
-
-    int sample_percent;
-    if (duration_ms >= 500) {
-        sample_percent = 100;
-    } else {
-        sample_percent = 100 * duration_ms / 500;
-        if (sample_percent == 0) {
-            return;
-        }
-        if (sample_percent < (random() % 100 + 1)) {
-            return;
-        }
-    }
-
-    char process_name[40];
-    getprocname(getpid(), process_name, sizeof(process_name));
-    String8 desc(target->getInterfaceDescriptor());
-
-    char buf[LOGGER_ENTRY_MAX_PAYLOAD];
-    buf[0] = EVENT_TYPE_LIST;
-    buf[1] = 5;
-    char* pos = &buf[2];
-    char* end = &buf[LOGGER_ENTRY_MAX_PAYLOAD - 1];  // leave room for final \n
-    if (!push_eventlog_string(&pos, end, desc.string())) return;
-    if (!push_eventlog_int(&pos, end, code)) return;
-    if (!push_eventlog_int(&pos, end, duration_ms)) return;
-    if (!push_eventlog_string(&pos, end, process_name)) return;
-    if (!push_eventlog_int(&pos, end, sample_percent)) return;
-    *(pos++) = '\n';   // conventional with EVENT_TYPE_LIST apparently.
-    android_bWriteLog(LOGTAG_BINDER_OPERATION, buf, pos - buf);
-}
-
-// We only measure binder call durations to potentially log them if
-// we're on the main thread.
-static bool should_time_binder_calls() {
-  return (getpid() == gettid());
-}
-
 static jboolean android_os_BinderProxy_transact(JNIEnv* env, jobject obj,
         jint code, jobject dataObj, jobject replyObj, jint flags) // throws RemoteException
 {
@@ -1428,29 +1331,10 @@
     ALOGV("Java code calling transact on %p in Java object %p with code %" PRId32 "\n",
             target, obj, code);
 
-
-    bool time_binder_calls;
-    int64_t start_millis;
-    if (kEnableBinderSample) {
-        // Only log the binder call duration for things on the Java-level main thread.
-        // But if we don't
-        time_binder_calls = should_time_binder_calls();
-
-        if (time_binder_calls) {
-            start_millis = uptimeMillis();
-        }
-    }
-
     //printf("Transact from Java code to %p sending: ", target); data->print();
     status_t err = target->transact(code, *data, reply, flags);
     //if (reply) printf("Transact from Java code to %p received: ", target); reply->print();
 
-    if (kEnableBinderSample) {
-        if (time_binder_calls) {
-            conditionally_log_binder_call(start_millis, target, code);
-        }
-    }
-
     if (err == NO_ERROR) {
         return JNI_TRUE;
     } else if (err == UNKNOWN_TRANSACTION) {
diff --git a/core/jni/com_android_internal_os_Zygote.cpp b/core/jni/com_android_internal_os_Zygote.cpp
index a5d979c..bbaea0a 100644
--- a/core/jni/com_android_internal_os_Zygote.cpp
+++ b/core/jni/com_android_internal_os_Zygote.cpp
@@ -356,6 +356,7 @@
     GWP_ASAN_LEVEL_DEFAULT = 3 << 21,
     NATIVE_HEAP_ZERO_INIT_ENABLED = 1 << 23,
     PROFILEABLE = 1 << 24,
+    DEBUG_ENABLE_PTRACE = 1 << 25,
 };
 
 enum UnsolicitedZygoteMessageTypes : uint32_t {
@@ -1887,8 +1888,10 @@
     }
 
     // Set process properties to enable debugging if required.
-    if ((runtime_flags & RuntimeFlags::DEBUG_ENABLE_JDWP) != 0) {
+    if ((runtime_flags & RuntimeFlags::DEBUG_ENABLE_PTRACE) != 0) {
         EnableDebugger();
+        // Don't pass unknown flag to the ART runtime.
+        runtime_flags &= ~RuntimeFlags::DEBUG_ENABLE_PTRACE;
     }
     if ((runtime_flags & RuntimeFlags::PROFILE_FROM_SHELL) != 0) {
         // simpleperf needs the process to be dumpable to profile it.
diff --git a/core/jni/fd_utils.cpp b/core/jni/fd_utils.cpp
index 40f6e4f..8e4addd 100644
--- a/core/jni/fd_utils.cpp
+++ b/core/jni/fd_utils.cpp
@@ -17,6 +17,7 @@
 #include "fd_utils.h"
 
 #include <algorithm>
+#include <utility>
 
 #include <fcntl.h>
 #include <grp.h>
@@ -174,7 +175,7 @@
 class FileDescriptorInfo {
  public:
   // Create a FileDescriptorInfo for a given file descriptor.
-  static FileDescriptorInfo* CreateFromFd(int fd, fail_fn_t fail_fn);
+  static std::unique_ptr<FileDescriptorInfo> CreateFromFd(int fd, fail_fn_t fail_fn);
 
   // Checks whether the file descriptor associated with this object refers to
   // the same description.
@@ -213,7 +214,7 @@
   DISALLOW_COPY_AND_ASSIGN(FileDescriptorInfo);
 };
 
-FileDescriptorInfo* FileDescriptorInfo::CreateFromFd(int fd, fail_fn_t fail_fn) {
+std::unique_ptr<FileDescriptorInfo> FileDescriptorInfo::CreateFromFd(int fd, fail_fn_t fail_fn) {
   struct stat f_stat;
   // This should never happen; the zygote should always have the right set
   // of permissions required to stat all its open files.
@@ -234,7 +235,7 @@
                                             socket_name.c_str(), fd));
     }
 
-    return new FileDescriptorInfo(fd);
+    return std::unique_ptr<FileDescriptorInfo>(new FileDescriptorInfo(fd));
   }
 
   // We only handle allowlisted regular files and character devices. Allowlisted
@@ -315,7 +316,8 @@
   int open_flags = fs_flags & (kOpenFlags);
   fs_flags = fs_flags & (~(kOpenFlags));
 
-  return new FileDescriptorInfo(f_stat, file_path, fd, open_flags, fd_flags, fs_flags, offset);
+  return std::unique_ptr<FileDescriptorInfo>(
+    new FileDescriptorInfo(f_stat, file_path, fd, open_flags, fd_flags, fs_flags, offset));
 }
 
 bool FileDescriptorInfo::RefersToSameFile() const {
@@ -482,11 +484,11 @@
 FileDescriptorTable* FileDescriptorTable::Create(const std::vector<int>& fds_to_ignore,
                                                  fail_fn_t fail_fn) {
   std::unique_ptr<std::set<int>> open_fds = GetOpenFdsIgnoring(fds_to_ignore, fail_fn);
-  std::unordered_map<int, FileDescriptorInfo*> open_fd_map;
+  std::unordered_map<int, std::unique_ptr<FileDescriptorInfo>> open_fd_map;
   for (auto fd : *open_fds) {
     open_fd_map[fd] = FileDescriptorInfo::CreateFromFd(fd, fail_fn);
   }
-  return new FileDescriptorTable(open_fd_map);
+  return new FileDescriptorTable(std::move(open_fd_map));
 }
 
 static std::unique_ptr<std::set<int>> GetOpenFdsIgnoring(const std::vector<int>& fds_to_ignore,
@@ -535,9 +537,9 @@
 
 // Reopens all file descriptors that are contained in the table.
 void FileDescriptorTable::ReopenOrDetach(fail_fn_t fail_fn) {
-  std::unordered_map<int, FileDescriptorInfo*>::const_iterator it;
+  std::unordered_map<int, std::unique_ptr<FileDescriptorInfo>>::const_iterator it;
   for (it = open_fd_map_.begin(); it != open_fd_map_.end(); ++it) {
-    const FileDescriptorInfo* info = it->second;
+    const FileDescriptorInfo* info = it->second.get();
     if (info == nullptr) {
       return;
     } else {
@@ -547,15 +549,11 @@
 }
 
 FileDescriptorTable::FileDescriptorTable(
-    const std::unordered_map<int, FileDescriptorInfo*>& map)
-    : open_fd_map_(map) {
+  std::unordered_map<int, std::unique_ptr<FileDescriptorInfo>> map)
+  : open_fd_map_(std::move(map)) {
 }
 
-FileDescriptorTable::~FileDescriptorTable() {
-    for (auto& it : open_fd_map_) {
-        delete it.second;
-    }
-}
+FileDescriptorTable::~FileDescriptorTable() {}
 
 void FileDescriptorTable::RestatInternal(std::set<int>& open_fds, fail_fn_t fail_fn) {
   // ART creates a file through memfd for optimization purposes. We make sure
@@ -569,7 +567,7 @@
   // (b) they refer to the same file.
   //
   // We'll only store the last error message.
-  std::unordered_map<int, FileDescriptorInfo*>::iterator it = open_fd_map_.begin();
+  std::unordered_map<int, std::unique_ptr<FileDescriptorInfo>>::iterator it = open_fd_map_.begin();
   while (it != open_fd_map_.end()) {
     std::set<int>::const_iterator element = open_fds.find(it->first);
     if (element == open_fds.end()) {
@@ -587,7 +585,6 @@
       if (!it->second->RefersToSameFile()) {
         // The file descriptor refers to a different description. We must
         // update our entry in the table.
-        delete it->second;
         it->second = FileDescriptorInfo::CreateFromFd(*element, fail_fn);
       } else {
         // It's the same file. Nothing to do here. Move on to the next open
diff --git a/core/jni/fd_utils.h b/core/jni/fd_utils.h
index a28ebf1..ac2d2a4 100644
--- a/core/jni/fd_utils.h
+++ b/core/jni/fd_utils.h
@@ -17,6 +17,7 @@
 #ifndef FRAMEWORKS_BASE_CORE_JNI_FD_UTILS_H_
 #define FRAMEWORKS_BASE_CORE_JNI_FD_UTILS_H_
 
+#include <memory>
 #include <set>
 #include <string>
 #include <unordered_map>
@@ -98,12 +99,12 @@
   void ReopenOrDetach(fail_fn_t fail_fn);
 
  private:
-  explicit FileDescriptorTable(const std::unordered_map<int, FileDescriptorInfo*>& map);
+  explicit FileDescriptorTable(std::unordered_map<int, std::unique_ptr<FileDescriptorInfo>> map);
 
   void RestatInternal(std::set<int>& open_fds, fail_fn_t fail_fn);
 
   // Invariant: All values in this unordered_map are non-NULL.
-  std::unordered_map<int, FileDescriptorInfo*> open_fd_map_;
+  std::unordered_map<int, std::unique_ptr<FileDescriptorInfo>> open_fd_map_;
 
   DISALLOW_COPY_AND_ASSIGN(FileDescriptorTable);
 };
diff --git a/core/proto/android/os/system_properties.proto b/core/proto/android/os/system_properties.proto
index 3cedba0..5a3539a 100644
--- a/core/proto/android/os/system_properties.proto
+++ b/core/proto/android/os/system_properties.proto
@@ -172,7 +172,7 @@
         optional Status tombstoned = 29;
         optional Status ueventd = 30;
         optional Status update_engine = 31;
-        optional Status update_verifier_nonencrypted = 32;
+        optional Status update_verifier = 32;
         optional Status virtual_touchpad = 33;
         optional Status vndservicemanager = 34;
         optional Status vold = 35;
diff --git a/core/tests/coretests/src/android/os/AidlTest.java b/core/tests/coretests/src/android/os/AidlTest.java
index 5f54b09..d0c3470 100644
--- a/core/tests/coretests/src/android/os/AidlTest.java
+++ b/core/tests/coretests/src/android/os/AidlTest.java
@@ -28,12 +28,14 @@
 
     private IAidlTest mRemote;
     private AidlObject mLocal;
+    private NonAutoGeneratedObject mNonAutoGenerated;
 
     @Override
     protected void setUp() throws Exception {
         super.setUp();
         mLocal = new AidlObject();
         mRemote = IAidlTest.Stub.asInterface(mLocal);
+        mNonAutoGenerated = new NonAutoGeneratedObject("NonAutoGeneratedObject");
     }
 
     private static boolean check(TestParcelable p, int n, String s) {
@@ -84,6 +86,12 @@
         }
     }
 
+    private static class NonAutoGeneratedObject extends Binder {
+        NonAutoGeneratedObject(String descriptor) {
+            super(descriptor);
+        }
+    }
+
     private static class AidlObject extends IAidlTest.Stub {
         public IInterface queryLocalInterface(String descriptor) {
             // overriding this to return null makes asInterface always
@@ -194,7 +202,7 @@
                 TestParcelable[] a1, TestParcelable[] a2) {
             return null;
         }
-        
+
         public void voidSecurityException() {
             throw new SecurityException("gotcha!");
         }
@@ -396,7 +404,7 @@
         assertEquals("s2[1]", s2[1]);
         assertEquals("s2[2]", s2[2]);
     }
-    
+
     @SmallTest
     public void testVoidSecurityException() throws Exception {
         boolean good = false;
@@ -407,7 +415,7 @@
         }
         assertEquals(good, true);
     }
-    
+
     @SmallTest
     public void testIntSecurityException() throws Exception {
         boolean good = false;
@@ -420,7 +428,7 @@
     }
 
     @SmallTest
-    public void testGetTransactionName() throws Exception {
+    public void testGetTransactionNameAutoGenerated() throws Exception {
         assertEquals(15, mLocal.getMaxTransactionId());
 
         assertEquals("booleanArray",
@@ -430,12 +438,21 @@
         assertEquals("parcelableIn",
                 mLocal.getTransactionName(IAidlTest.Stub.TRANSACTION_parcelableIn));
 
-        assertEquals("IAidlTest:booleanArray",
+        assertEquals("AIDL::java::IAidlTest::booleanArray::server",
                 mLocal.getTransactionTraceName(IAidlTest.Stub.TRANSACTION_booleanArray));
-        assertEquals("IAidlTest:voidSecurityException",
+        assertEquals("AIDL::java::IAidlTest::voidSecurityException::server",
                 mLocal.getTransactionTraceName(IAidlTest.Stub.TRANSACTION_voidSecurityException));
-        assertEquals("IAidlTest:parcelableIn",
+        assertEquals("AIDL::java::IAidlTest::parcelableIn::server",
                 mLocal.getTransactionTraceName(IAidlTest.Stub.TRANSACTION_parcelableIn));
     }
-}
 
+    @SmallTest
+    public void testGetTransactionNameNonAutoGenerated() throws Exception {
+        assertEquals(0, mNonAutoGenerated.getMaxTransactionId());
+
+        assertEquals("AIDL::java::NonAutoGeneratedObject::#0::server",
+                mNonAutoGenerated.getTransactionTraceName(0));
+        assertEquals("AIDL::java::NonAutoGeneratedObject::#1::server",
+                mNonAutoGenerated.getTransactionTraceName(1));
+    }
+}
diff --git a/core/tests/coretests/src/android/widget/RemoteViewsTest.java b/core/tests/coretests/src/android/widget/RemoteViewsTest.java
index bbf9f3c..ed2a32b 100644
--- a/core/tests/coretests/src/android/widget/RemoteViewsTest.java
+++ b/core/tests/coretests/src/android/widget/RemoteViewsTest.java
@@ -24,6 +24,10 @@
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
 
 import android.app.ActivityOptions;
 import android.app.PendingIntent;
@@ -33,6 +37,8 @@
 import android.graphics.Bitmap;
 import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
+import android.graphics.drawable.Icon;
+import android.net.Uri;
 import android.os.AsyncTask;
 import android.os.Binder;
 import android.os.Looper;
@@ -58,6 +64,7 @@
 import java.util.Arrays;
 import java.util.Map;
 import java.util.concurrent.CountDownLatch;
+import java.util.function.Consumer;
 
 /**
  * Tests for RemoteViews.
@@ -693,4 +700,61 @@
             return null;
         }
     }
+
+    @Test
+    public void visitUris() {
+        RemoteViews views = new RemoteViews(mPackage, R.layout.remote_views_test);
+
+        final Uri imageUri = Uri.parse("content://media/image");
+        final Icon icon1 = Icon.createWithContentUri("content://media/icon1");
+        final Icon icon2 = Icon.createWithContentUri("content://media/icon2");
+        final Icon icon3 = Icon.createWithContentUri("content://media/icon3");
+        final Icon icon4 = Icon.createWithContentUri("content://media/icon4");
+        views.setImageViewUri(R.id.image, imageUri);
+        views.setTextViewCompoundDrawables(R.id.text, icon1, icon2, icon3, icon4);
+
+        Consumer<Uri> visitor = (Consumer<Uri>) spy(Consumer.class);
+        views.visitUris(visitor);
+        verify(visitor, times(1)).accept(eq(imageUri));
+        verify(visitor, times(1)).accept(eq(icon1.getUri()));
+        verify(visitor, times(1)).accept(eq(icon2.getUri()));
+        verify(visitor, times(1)).accept(eq(icon3.getUri()));
+        verify(visitor, times(1)).accept(eq(icon4.getUri()));
+    }
+
+    @Test
+    public void visitUris_separateOrientation() {
+        final RemoteViews landscape = new RemoteViews(mPackage, R.layout.remote_views_test);
+        final Uri imageUriL = Uri.parse("content://landscape/image");
+        final Icon icon1L = Icon.createWithContentUri("content://landscape/icon1");
+        final Icon icon2L = Icon.createWithContentUri("content://landscape/icon2");
+        final Icon icon3L = Icon.createWithContentUri("content://landscape/icon3");
+        final Icon icon4L = Icon.createWithContentUri("content://landscape/icon4");
+        landscape.setImageViewUri(R.id.image, imageUriL);
+        landscape.setTextViewCompoundDrawables(R.id.text, icon1L, icon2L, icon3L, icon4L);
+
+        final RemoteViews portrait = new RemoteViews(mPackage, 33);
+        final Uri imageUriP = Uri.parse("content://portrait/image");
+        final Icon icon1P = Icon.createWithContentUri("content://portrait/icon1");
+        final Icon icon2P = Icon.createWithContentUri("content://portrait/icon2");
+        final Icon icon3P = Icon.createWithContentUri("content://portrait/icon3");
+        final Icon icon4P = Icon.createWithContentUri("content://portrait/icon4");
+        portrait.setImageViewUri(R.id.image, imageUriP);
+        portrait.setTextViewCompoundDrawables(R.id.text, icon1P, icon2P, icon3P, icon4P);
+
+        RemoteViews views = new RemoteViews(landscape, portrait);
+
+        Consumer<Uri> visitor = (Consumer<Uri>) spy(Consumer.class);
+        views.visitUris(visitor);
+        verify(visitor, times(1)).accept(eq(imageUriL));
+        verify(visitor, times(1)).accept(eq(icon1L.getUri()));
+        verify(visitor, times(1)).accept(eq(icon2L.getUri()));
+        verify(visitor, times(1)).accept(eq(icon3L.getUri()));
+        verify(visitor, times(1)).accept(eq(icon4L.getUri()));
+        verify(visitor, times(1)).accept(eq(imageUriP));
+        verify(visitor, times(1)).accept(eq(icon1P.getUri()));
+        verify(visitor, times(1)).accept(eq(icon2P.getUri()));
+        verify(visitor, times(1)).accept(eq(icon3P.getUri()));
+        verify(visitor, times(1)).accept(eq(icon4P.getUri()));
+    }
 }
diff --git a/core/tests/coretests/src/com/android/internal/content/res/OverlayConfigTest.java b/core/tests/coretests/src/com/android/internal/content/res/OverlayConfigTest.java
index 0f30cfe..246a1e7 100644
--- a/core/tests/coretests/src/com/android/internal/content/res/OverlayConfigTest.java
+++ b/core/tests/coretests/src/com/android/internal/content/res/OverlayConfigTest.java
@@ -22,6 +22,7 @@
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
+import android.content.pm.PackagePartitions;
 import android.os.FileUtils;
 import android.os.SystemProperties;
 import android.platform.test.annotations.Presubmit;
@@ -32,6 +33,7 @@
 import com.android.frameworks.coretests.R;
 import com.android.internal.content.om.OverlayConfig;
 import com.android.internal.content.om.OverlayConfig.IdmapInvocation;
+import com.android.internal.content.om.OverlayConfigParser.OverlayPartition;
 import com.android.internal.content.om.OverlayScanner;
 
 import org.junit.Rule;
@@ -46,6 +48,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.List;
 
 @Presubmit
 @RunWith(AndroidJUnit4.class)
@@ -88,6 +91,17 @@
         assertEquals(configIndex, config.configIndex);
     }
 
+    private String generatePartitionOrderString(List<OverlayPartition> partitions) {
+        StringBuilder partitionOrder = new StringBuilder();
+        for (int i = 0; i < partitions.size(); i++) {
+            partitionOrder.append(partitions.get(i).getName());
+            if (i < partitions.size() - 1) {
+                partitionOrder.append(", ");
+            }
+        }
+        return partitionOrder.toString();
+    }
+
     @Test
     public void testImmutableAfterNonImmutableFails() throws IOException {
         mExpectedException.expect(IllegalStateException.class);
@@ -685,4 +699,122 @@
         OverlayConfig.Configuration o3 = overlayConfig.getConfiguration("three");
         assertNotNull(o3);
     }
+
+    @Test
+    public void testSortPartitionsWithoutXml() throws IOException {
+        ArrayList<OverlayPartition> partitions = new ArrayList<>(
+                PackagePartitions.getOrderedPartitions(OverlayPartition::new));
+
+        final OverlayConfig overlayConfig = createConfigImpl();
+        String partitionOrderFilePath = String.format("%s/%s", mTestFolder.getRoot(),
+                "/product/overlay/partition_order.xml");
+        assertEquals(false, overlayConfig.sortPartitions(partitionOrderFilePath, partitions));
+        assertEquals("system, vendor, odm, oem, product, system_ext",
+                generatePartitionOrderString(partitions));
+    }
+
+    @Test
+    public void testSortPartitionsWithInvalidXmlRootElement() throws IOException {
+        ArrayList<OverlayPartition> partitions = new ArrayList<>(
+                PackagePartitions.getOrderedPartitions(OverlayPartition::new));
+        createFile("/product/overlay/partition_order.xml",
+                "<partition-list>\n"
+                        + "  <partition name=\"system_ext\"/>\n"
+                        + "  <partition name=\"vendor\"/>\n"
+                        + "  <partition name=\"oem\"/>\n"
+                        + "  <partition name=\"odm\"/>\n"
+                        + "  <partition name=\"product\"/>\n"
+                        + "  <partition name=\"system\"/>\n"
+                        + "</partition-list>\n");
+        final OverlayConfig overlayConfig = createConfigImpl();
+        String partitionOrderFilePath = String.format("%s/%s", mTestFolder.getRoot(),
+                "/product/overlay/partition_order.xml");
+        assertEquals(false, overlayConfig.sortPartitions(partitionOrderFilePath, partitions));
+        assertEquals("system, vendor, odm, oem, product, system_ext",
+                generatePartitionOrderString(partitions));
+    }
+
+    @Test
+    public void testSortPartitionsWithInvalidPartition() throws IOException {
+        ArrayList<OverlayPartition> partitions = new ArrayList<>(
+                PackagePartitions.getOrderedPartitions(OverlayPartition::new));
+        createFile("/product/overlay/partition_order.xml",
+                "<partition-order>\n"
+                        + "  <partition name=\"INVALID\"/>\n"
+                        + "  <partition name=\"vendor\"/>\n"
+                        + "  <partition name=\"oem\"/>\n"
+                        + "  <partition name=\"odm\"/>\n"
+                        + "  <partition name=\"product\"/>\n"
+                        + "  <partition name=\"system\"/>\n"
+                        + "</partition-order>\n");
+        final OverlayConfig overlayConfig = createConfigImpl();
+        String partitionOrderFilePath = String.format("%s/%s", mTestFolder.getRoot(),
+                "/product/overlay/partition_order.xml");
+        assertEquals(false, overlayConfig.sortPartitions(partitionOrderFilePath, partitions));
+        assertEquals("system, vendor, odm, oem, product, system_ext",
+                generatePartitionOrderString(partitions));
+    }
+
+    @Test
+    public void testSortPartitionsWithDuplicatePartition() throws IOException {
+        ArrayList<OverlayPartition> partitions = new ArrayList<>(
+                PackagePartitions.getOrderedPartitions(OverlayPartition::new));
+        createFile("/product/overlay/partition_order.xml",
+                "<partition-order>\n"
+                        + "  <partition name=\"system_ext\"/>\n"
+                        + "  <partition name=\"system\"/>\n"
+                        + "  <partition name=\"vendor\"/>\n"
+                        + "  <partition name=\"oem\"/>\n"
+                        + "  <partition name=\"odm\"/>\n"
+                        + "  <partition name=\"product\"/>\n"
+                        + "  <partition name=\"system\"/>\n"
+                        + "</partition-order>\n");
+        final OverlayConfig overlayConfig = createConfigImpl();
+        String partitionOrderFilePath = String.format("%s/%s", mTestFolder.getRoot(),
+                "/product/overlay/partition_order.xml");
+        assertEquals(false, overlayConfig.sortPartitions(partitionOrderFilePath, partitions));
+        assertEquals("system, vendor, odm, oem, product, system_ext",
+                generatePartitionOrderString(partitions));
+    }
+
+    @Test
+    public void testSortPartitionsWithMissingPartition() throws IOException {
+        ArrayList<OverlayPartition> partitions = new ArrayList<>(
+                PackagePartitions.getOrderedPartitions(OverlayPartition::new));
+        createFile("/product/overlay/partition_order.xml",
+                "<partition-order>\n"
+                        + "  <partition name=\"vendor\"/>\n"
+                        + "  <partition name=\"oem\"/>\n"
+                        + "  <partition name=\"odm\"/>\n"
+                        + "  <partition name=\"product\"/>\n"
+                        + "  <partition name=\"system\"/>\n"
+                        + "</partition-order>\n");
+        final OverlayConfig overlayConfig = createConfigImpl();
+        String partitionOrderFilePath = String.format("%s/%s", mTestFolder.getRoot(),
+                "/product/overlay/partition_order.xml");
+        assertEquals(false, overlayConfig.sortPartitions(partitionOrderFilePath, partitions));
+        assertEquals("system, vendor, odm, oem, product, system_ext",
+                generatePartitionOrderString(partitions));
+    }
+
+    @Test
+    public void testSortPartitionsWithCorrectPartitionOrderXml() throws IOException {
+        ArrayList<OverlayPartition> partitions = new ArrayList<>(
+                PackagePartitions.getOrderedPartitions(OverlayPartition::new));
+        createFile("/product/overlay/partition_order.xml",
+                "<partition-order>\n"
+                        + "  <partition name=\"system_ext\"/>\n"
+                        + "  <partition name=\"vendor\"/>\n"
+                        + "  <partition name=\"oem\"/>\n"
+                        + "  <partition name=\"odm\"/>\n"
+                        + "  <partition name=\"product\"/>\n"
+                        + "  <partition name=\"system\"/>\n"
+                        + "</partition-order>\n");
+        final OverlayConfig overlayConfig = createConfigImpl();
+        String partitionOrderFilePath = String.format("%s/%s", mTestFolder.getRoot(),
+                "/product/overlay/partition_order.xml");
+        assertEquals(true, overlayConfig.sortPartitions(partitionOrderFilePath, partitions));
+        assertEquals("system_ext, vendor, oem, odm, product, system",
+                generatePartitionOrderString(partitions));
+    }
 }
diff --git a/libs/WindowManager/Shell/Android.bp b/libs/WindowManager/Shell/Android.bp
index b3108fe..c00a749 100644
--- a/libs/WindowManager/Shell/Android.bp
+++ b/libs/WindowManager/Shell/Android.bp
@@ -110,8 +110,7 @@
     name: "protolog.json.gz",
     srcs: [":generate-wm_shell_protolog.json"],
     out: ["wmshell.protolog.json.gz"],
-    cmd: "$(location minigzip) -c < $(in) > $(out)",
-    tools: ["minigzip"],
+    cmd: "gzip -c < $(in) > $(out)",
 }
 
 prebuilt_etc {
@@ -162,7 +161,7 @@
         // *.kt sources are inside a filegroup.
         "kotlin-annotations",
     ],
-    kotlincflags: ["-Xjvm-default=enable"],
+    kotlincflags: ["-Xjvm-default=all"],
     manifest: "AndroidManifest.xml",
     plugins: ["dagger2-compiler"],
 }
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/bubbles/BubbleController.java b/libs/WindowManager/Shell/src/com/android/wm/shell/bubbles/BubbleController.java
index 541c0f0..2b3ee76 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/bubbles/BubbleController.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/bubbles/BubbleController.java
@@ -307,16 +307,20 @@
                 new OneHandedTransitionCallback() {
                     @Override
                     public void onStartFinished(Rect bounds) {
-                        if (mStackView != null) {
-                            mStackView.onVerticalOffsetChanged(bounds.top);
-                        }
+                        mMainExecutor.execute(() -> {
+                            if (mStackView != null) {
+                                mStackView.onVerticalOffsetChanged(bounds.top);
+                            }
+                        });
                     }
 
                     @Override
                     public void onStopFinished(Rect bounds) {
-                        if (mStackView != null) {
-                            mStackView.onVerticalOffsetChanged(bounds.top);
-                        }
+                        mMainExecutor.execute(() -> {
+                            if (mStackView != null) {
+                                mStackView.onVerticalOffsetChanged(bounds.top);
+                            }
+                        });
                     }
                 });
     }
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/common/FloatingContentCoordinator.kt b/libs/WindowManager/Shell/src/com/android/wm/shell/common/FloatingContentCoordinator.kt
index d5d072a..122dcbb 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/common/FloatingContentCoordinator.kt
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/common/FloatingContentCoordinator.kt
@@ -94,7 +94,6 @@
          * non-overlapping.
          * @return The new bounds for this content.
          */
-        @JvmDefault
         fun calculateNewBoundsOnOverlap(
             overlappingContentBounds: Rect,
             otherContentBounds: List<Rect>
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/common/split/SplitDecorManager.java b/libs/WindowManager/Shell/src/com/android/wm/shell/common/split/SplitDecorManager.java
index bdf0ac2..4c2e0da 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/common/split/SplitDecorManager.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/common/split/SplitDecorManager.java
@@ -164,6 +164,10 @@
             t.remove(mGapBackgroundLeash);
             mGapBackgroundLeash = null;
         }
+        if (mScreenshot != null) {
+            t.remove(mScreenshot);
+            mScreenshot = null;
+        }
         mHostLeash = null;
         mIcon = null;
         mResizingIconView = null;
@@ -323,6 +327,8 @@
         if (!mShown && mIsResizing && !mOldBounds.equals(mResizingBounds)) {
             if (mScreenshotAnimator != null && mScreenshotAnimator.isRunning()) {
                 mScreenshotAnimator.cancel();
+            } else if (mScreenshot != null) {
+                t.remove(mScreenshot);
             }
 
             mTempRect.set(mOldBounds);
@@ -339,6 +345,8 @@
         if (!mShown && mIsResizing && !mOldBounds.equals(mResizingBounds)) {
             if (mScreenshotAnimator != null && mScreenshotAnimator.isRunning()) {
                 mScreenshotAnimator.cancel();
+            } else if (mScreenshot != null) {
+                t.remove(mScreenshot);
             }
 
             mScreenshot = screenshot;
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode/DesktopModeTaskRepository.kt b/libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode/DesktopModeTaskRepository.kt
index 47342c9..935a588 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode/DesktopModeTaskRepository.kt
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode/DesktopModeTaskRepository.kt
@@ -173,7 +173,6 @@
         /**
          * Called when the active tasks change in desktop mode.
          */
-        @JvmDefault
         fun onActiveTasksChanged() {}
     }
 
@@ -184,7 +183,6 @@
         /**
          * Called when the desktop starts or stops showing freeform tasks.
          */
-        @JvmDefault
         fun onVisibilityChanged(hasVisibleFreeformTasks: Boolean) {}
     }
 }
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipTaskOrganizer.java b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipTaskOrganizer.java
index c4b5470..7da7502 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipTaskOrganizer.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipTaskOrganizer.java
@@ -73,7 +73,6 @@
 import android.window.TaskSnapshot;
 import android.window.WindowContainerToken;
 import android.window.WindowContainerTransaction;
-import android.window.WindowContainerTransactionCallback;
 
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.protolog.common.ProtoLog;
@@ -145,23 +144,6 @@
     protected final ShellTaskOrganizer mTaskOrganizer;
     protected final ShellExecutor mMainExecutor;
 
-    // the runnable to execute after WindowContainerTransactions is applied to finish resizing pip
-    private Runnable mPipFinishResizeWCTRunnable;
-
-    private final WindowContainerTransactionCallback mPipFinishResizeWCTCallback =
-            new WindowContainerTransactionCallback() {
-        @Override
-        public void onTransactionReady(int id, SurfaceControl.Transaction t) {
-            t.apply();
-
-            // execute the runnable if non-null after WCT is applied to finish resizing pip
-            if (mPipFinishResizeWCTRunnable != null) {
-                mPipFinishResizeWCTRunnable.run();
-                mPipFinishResizeWCTRunnable = null;
-            }
-        }
-    };
-
     // These callbacks are called on the update thread
     private final PipAnimationController.PipAnimationCallback mPipAnimationCallback =
             new PipAnimationController.PipAnimationCallback() {
@@ -1278,23 +1260,8 @@
     /**
      * Animates resizing of the pinned stack given the duration and start bounds.
      * This is used when the starting bounds is not the current PiP bounds.
-     *
-     * @param pipFinishResizeWCTRunnable callback to run after window updates are complete
      */
     public void scheduleAnimateResizePip(Rect fromBounds, Rect toBounds, int duration,
-            float startingAngle, Consumer<Rect> updateBoundsCallback,
-            Runnable pipFinishResizeWCTRunnable) {
-        mPipFinishResizeWCTRunnable = pipFinishResizeWCTRunnable;
-        if (mPipFinishResizeWCTRunnable != null) {
-            ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
-                    "mPipFinishResizeWCTRunnable is set to be called once window updates");
-        }
-
-        scheduleAnimateResizePip(fromBounds, toBounds, duration, startingAngle,
-                updateBoundsCallback);
-    }
-
-    private void scheduleAnimateResizePip(Rect fromBounds, Rect toBounds, int duration,
             float startingAngle, Consumer<Rect> updateBoundsCallback) {
         if (mWaitForFixedRotation) {
             ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
@@ -1599,7 +1566,7 @@
             mSplitScreenOptional.ifPresent(splitScreenController ->
                     splitScreenController.enterSplitScreen(mTaskInfo.taskId, wasPipTopLeft, wct));
         } else {
-            mTaskOrganizer.applySyncTransaction(wct, mPipFinishResizeWCTCallback);
+            mTaskOrganizer.applyTransaction(wct);
         }
     }
 
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipResizeGestureHandler.java b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipResizeGestureHandler.java
index 3e83c5f..41ff0b3 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipResizeGestureHandler.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipResizeGestureHandler.java
@@ -580,16 +580,8 @@
                 final float snapFraction = mPipBoundsAlgorithm.getSnapFraction(
                         mLastResizeBounds, movementBounds);
                 mPipBoundsAlgorithm.applySnapFraction(mLastResizeBounds, snapFraction);
-
-                // disable the pinch resizing until the final bounds are updated
-                final boolean prevEnablePinchResize = mEnablePinchResize;
-                mEnablePinchResize = false;
-
                 mPipTaskOrganizer.scheduleAnimateResizePip(startBounds, mLastResizeBounds,
-                        PINCH_RESIZE_SNAP_DURATION, mAngle, mUpdateResizeBoundsCallback, () -> {
-                            // reset the pinch resizing to its default state
-                            mEnablePinchResize = prevEnablePinchResize;
-                        });
+                        PINCH_RESIZE_SNAP_DURATION, mAngle, mUpdateResizeBoundsCallback);
             } else {
                 mPipTaskOrganizer.scheduleFinishResizePip(mLastResizeBounds,
                         PipAnimationController.TRANSITION_DIRECTION_USER_RESIZE,
diff --git a/libs/WindowManager/Shell/tests/unittest/Android.bp b/libs/WindowManager/Shell/tests/unittest/Android.bp
index 2ac1dc0..64bb481 100644
--- a/libs/WindowManager/Shell/tests/unittest/Android.bp
+++ b/libs/WindowManager/Shell/tests/unittest/Android.bp
@@ -61,7 +61,7 @@
         "libstaticjvmtiagent",
     ],
 
-    kotlincflags: ["-Xjvm-default=enable"],
+    kotlincflags: ["-Xjvm-default=all"],
 
     plugins: ["dagger2-compiler"],
 
diff --git a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/pip/phone/PipResizeGestureHandlerTest.java b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/pip/phone/PipResizeGestureHandlerTest.java
index 5f356c9..c7b9eb3 100644
--- a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/pip/phone/PipResizeGestureHandlerTest.java
+++ b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/pip/phone/PipResizeGestureHandlerTest.java
@@ -155,7 +155,7 @@
         mPipResizeGestureHandler.onPinchResize(upEvent);
 
         verify(mPipTaskOrganizer, times(1))
-                .scheduleAnimateResizePip(any(), any(), anyInt(), anyFloat(), any(), any());
+                .scheduleAnimateResizePip(any(), any(), anyInt(), anyFloat(), any());
 
         assertTrue("The new size should be bigger than the original PiP size.",
                 mPipResizeGestureHandler.getLastResizeBounds().width()
@@ -194,7 +194,7 @@
         mPipResizeGestureHandler.onPinchResize(upEvent);
 
         verify(mPipTaskOrganizer, times(1))
-                .scheduleAnimateResizePip(any(), any(), anyInt(), anyFloat(), any(), any());
+                .scheduleAnimateResizePip(any(), any(), anyInt(), anyFloat(), any());
 
         assertTrue("The new size should be smaller than the original PiP size.",
                 mPipResizeGestureHandler.getLastResizeBounds().width()
diff --git a/media/java/android/media/OWNERS b/media/java/android/media/OWNERS
index 6d6a9f8..bbe5e06 100644
--- a/media/java/android/media/OWNERS
+++ b/media/java/android/media/OWNERS
@@ -10,5 +10,8 @@
 
 per-file *Image* = file:/graphics/java/android/graphics/OWNERS
 
+per-file ExifInterface.java,ExifInterfaceUtils.java,IMediaHTTPConnection.aidl,IMediaHTTPService.aidl,JetPlayer.java,MediaDataSource.java,MediaExtractor.java,MediaHTTPConnection.java,MediaHTTPService.java,MediaPlayer.java=set noparent
+per-file ExifInterface.java,ExifInterfaceUtils.java,IMediaHTTPConnection.aidl,IMediaHTTPService.aidl,JetPlayer.java,MediaDataSource.java,MediaExtractor.java,MediaHTTPConnection.java,MediaHTTPService.java,MediaPlayer.java=file:platform/frameworks/av:/media/janitors/media_solutions_OWNERS
+
 # Haptics team also works on Ringtone
 per-file *Ringtone* = file:/services/core/java/com/android/server/vibrator/OWNERS
diff --git a/media/java/android/media/projection/OWNERS b/media/java/android/media/projection/OWNERS
index 2273f81..cc9be9c 100644
--- a/media/java/android/media/projection/OWNERS
+++ b/media/java/android/media/projection/OWNERS
@@ -1,3 +1,5 @@
+# Bug component: 1345447
+
 michaelwr@google.com
 santoscordon@google.com
 chaviw@google.com
diff --git a/media/jni/OWNERS b/media/jni/OWNERS
index 96894d1..e12d828 100644
--- a/media/jni/OWNERS
+++ b/media/jni/OWNERS
@@ -3,3 +3,6 @@
 
 # extra for TV related files
 per-file android_media_tv_*=hgchen@google.com,quxiangfang@google.com
+
+per-file android_media_JetPlayer.cpp,android_media_MediaDataSource.cpp,android_media_MediaDataSource.h,android_media_MediaPlayer.java=set noparent
+per-file android_media_JetPlayer.cpp,android_media_MediaDataSource.cpp,android_media_MediaDataSource.h,android_media_MediaPlayer.java=file:platform/frameworks/av:/media/janitors/media_solutions_OWNERS
diff --git a/media/jni/android_media_MediaCodec.cpp b/media/jni/android_media_MediaCodec.cpp
index b878bcf5..53c0266 100644
--- a/media/jni/android_media_MediaCodec.cpp
+++ b/media/jni/android_media_MediaCodec.cpp
@@ -2491,7 +2491,7 @@
             return;
         }
         auto cryptoInfo =
-                cryptoInfoObj ? NativeCryptoInfo{size} : NativeCryptoInfo{env, cryptoInfoObj};
+                cryptoInfoObj ? NativeCryptoInfo{env, cryptoInfoObj} : NativeCryptoInfo{size};
         if (env->ExceptionCheck()) {
             // Creation of cryptoInfo failed. Let the exception bubble up.
             return;
diff --git a/packages/DynamicSystemInstallationService/src/com/android/dynsystem/DynamicSystemInstallationService.java b/packages/DynamicSystemInstallationService/src/com/android/dynsystem/DynamicSystemInstallationService.java
index 3b56016..c390f31 100644
--- a/packages/DynamicSystemInstallationService/src/com/android/dynsystem/DynamicSystemInstallationService.java
+++ b/packages/DynamicSystemInstallationService/src/com/android/dynsystem/DynamicSystemInstallationService.java
@@ -419,12 +419,20 @@
         mDynSystem.remove();
     }
 
+    private boolean isDsuSlotLocked() {
+        // Slot names ending with ".lock" are a customized installation.
+        // We expect the client app to provide custom UI to enter/exit DSU mode.
+        // We will ignore the ACTION_REBOOT_TO_NORMAL command and will not show
+        // notifications in this case.
+        return mDynSystem.getActiveDsuSlot().endsWith(".lock");
+    }
+
     private void executeRebootToNormalCommand() {
         if (!isInDynamicSystem()) {
             Log.e(TAG, "It's already running in normal system.");
             return;
         }
-        if (mDynSystem.getActiveDsuSlot().endsWith(".lock")) {
+        if (isDsuSlotLocked()) {
             Log.e(TAG, "Ignore the reboot intent for a locked DSU slot");
             return;
         }
@@ -449,13 +457,13 @@
     private void executeNotifyIfInUseCommand() {
         switch (getStatus()) {
             case STATUS_IN_USE:
-                if (!mHideNotification) {
+                if (!mHideNotification && !isDsuSlotLocked()) {
                     startForeground(NOTIFICATION_ID,
                             buildNotification(STATUS_IN_USE, CAUSE_NOT_SPECIFIED));
                 }
                 break;
             case STATUS_READY:
-                if (!mHideNotification) {
+                if (!mHideNotification && !isDsuSlotLocked()) {
                     startForeground(NOTIFICATION_ID,
                             buildNotification(STATUS_READY, CAUSE_NOT_SPECIFIED));
                 }
diff --git a/packages/EasterEgg/Android.bp b/packages/EasterEgg/Android.bp
index e88410c..6d4e801 100644
--- a/packages/EasterEgg/Android.bp
+++ b/packages/EasterEgg/Android.bp
@@ -26,7 +26,10 @@
 android_app {
     // the build system in pi-dev can't quite handle R.java in kt
     // so we will have a mix of java and kotlin files
-    srcs: ["src/**/*.java", "src/**/*.kt"],
+    srcs: [
+        "src/**/*.java",
+        "src/**/*.kt",
+    ],
 
     resource_dirs: ["res"],
 
@@ -39,16 +42,16 @@
         proguard_flags_files: ["proguard.flags"],
     },
 
-	static_libs: [
-		"androidx.core_core",
-		"androidx.recyclerview_recyclerview",
+    static_libs: [
+        "androidx.core_core",
+        "androidx.recyclerview_recyclerview",
         "androidx.annotation_annotation",
-		"kotlinx-coroutines-android",
-		"kotlinx-coroutines-core",
-		//"kotlinx-coroutines-reactive",
-	],
+        "kotlinx-coroutines-android",
+        "kotlinx-coroutines-core",
+        //"kotlinx-coroutines-reactive",
+    ],
 
     manifest: "AndroidManifest.xml",
 
-    kotlincflags: ["-Xjvm-default=enable"],
+    kotlincflags: ["-Xjvm-default=all"],
 }
diff --git a/packages/SystemUI/Android.bp b/packages/SystemUI/Android.bp
index eb5a15f..eb30ad9 100644
--- a/packages/SystemUI/Android.bp
+++ b/packages/SystemUI/Android.bp
@@ -180,7 +180,7 @@
     ],
     manifest: "AndroidManifest.xml",
 
-    kotlincflags: ["-Xjvm-default=enable"],
+    kotlincflags: ["-Xjvm-default=all"],
 
     plugins: ["dagger2-compiler"],
 
@@ -341,7 +341,7 @@
         "android.test.base",
         "android.test.mock",
     ],
-    kotlincflags: ["-Xjvm-default=enable"],
+    kotlincflags: ["-Xjvm-default=all"],
     aaptflags: [
         "--extra-packages",
         "com.android.systemui",
@@ -457,7 +457,7 @@
     certificate: "platform",
     privileged: true,
 
-    kotlincflags: ["-Xjvm-default=enable"],
+    kotlincflags: ["-Xjvm-default=all"],
 
     dxflags: ["--multi-dex"],
     required: [
diff --git a/packages/SystemUI/OWNERS b/packages/SystemUI/OWNERS
index 811b409..306031d 100644
--- a/packages/SystemUI/OWNERS
+++ b/packages/SystemUI/OWNERS
@@ -23,6 +23,7 @@
 brycelee@google.com
 brzezinski@google.com
 caitlinshk@google.com
+cameronyee@google.com
 chandruis@google.com
 chrisgollner@google.com
 cinek@google.com
@@ -44,6 +45,7 @@
 jjaggi@google.com
 jonmiranda@google.com
 joshtrask@google.com
+juansmartinez@google.com
 juliacr@google.com
 juliatuttle@google.com
 justinkoh@google.com
diff --git a/packages/SystemUI/animation/src/com/android/systemui/animation/ActivityLaunchAnimator.kt b/packages/SystemUI/animation/src/com/android/systemui/animation/ActivityLaunchAnimator.kt
index 296c2ae..32085ce 100644
--- a/packages/SystemUI/animation/src/com/android/systemui/animation/ActivityLaunchAnimator.kt
+++ b/packages/SystemUI/animation/src/com/android/systemui/animation/ActivityLaunchAnimator.kt
@@ -302,10 +302,9 @@
 
     interface Callback {
         /** Whether we are currently on the keyguard or not. */
-        @JvmDefault fun isOnKeyguard(): Boolean = false
+        fun isOnKeyguard(): Boolean = false
 
         /** Hide the keyguard and animate using [runner]. */
-        @JvmDefault
         fun hideKeyguardWithAnimation(runner: IRemoteAnimationRunner) {
             throw UnsupportedOperationException()
         }
@@ -316,16 +315,16 @@
 
     interface Listener {
         /** Called when an activity launch animation started. */
-        @JvmDefault fun onLaunchAnimationStart() {}
+        fun onLaunchAnimationStart() {}
 
         /**
          * Called when an activity launch animation is finished. This will be called if and only if
          * [onLaunchAnimationStart] was called earlier.
          */
-        @JvmDefault fun onLaunchAnimationEnd() {}
+        fun onLaunchAnimationEnd() {}
 
         /** Called when an activity launch animation made progress. */
-        @JvmDefault fun onLaunchAnimationProgress(linearProgress: Float) {}
+        fun onLaunchAnimationProgress(linearProgress: Float) {}
     }
 
     /**
diff --git a/packages/SystemUI/animation/src/com/android/systemui/animation/FontInterpolator.kt b/packages/SystemUI/animation/src/com/android/systemui/animation/FontInterpolator.kt
index f0a8211..83e44b6 100644
--- a/packages/SystemUI/animation/src/com/android/systemui/animation/FontInterpolator.kt
+++ b/packages/SystemUI/animation/src/com/android/systemui/animation/FontInterpolator.kt
@@ -18,8 +18,10 @@
 
 import android.graphics.fonts.Font
 import android.graphics.fonts.FontVariationAxis
+import android.util.LruCache
 import android.util.MathUtils
 import android.util.MathUtils.abs
+import androidx.annotation.VisibleForTesting
 import java.lang.Float.max
 import java.lang.Float.min
 
@@ -34,6 +36,10 @@
 private const val FONT_ITALIC_ANIMATION_STEP = 0.1f
 private const val FONT_ITALIC_DEFAULT_VALUE = 0f
 
+// Benchmarked via Perfetto, difference between 10 and 50 entries is about 0.3ms in
+// frame draw time on a Pixel 6.
+@VisibleForTesting const val FONT_CACHE_MAX_ENTRIES = 10
+
 /** Provide interpolation of two fonts by adjusting font variation settings. */
 class FontInterpolator {
 
@@ -81,8 +87,8 @@
     // Font interpolator has two level caches: one for input and one for font with different
     // variation settings. No synchronization is needed since FontInterpolator is not designed to be
     // thread-safe and can be used only on UI thread.
-    private val interpCache = hashMapOf<InterpKey, Font>()
-    private val verFontCache = hashMapOf<VarFontKey, Font>()
+    private val interpCache = LruCache<InterpKey, Font>(FONT_CACHE_MAX_ENTRIES)
+    private val verFontCache = LruCache<VarFontKey, Font>(FONT_CACHE_MAX_ENTRIES)
 
     // Mutable keys for recycling.
     private val tmpInterpKey = InterpKey(null, null, 0f)
@@ -152,7 +158,7 @@
         tmpVarFontKey.set(start, newAxes)
         val axesCachedFont = verFontCache[tmpVarFontKey]
         if (axesCachedFont != null) {
-            interpCache[InterpKey(start, end, progress)] = axesCachedFont
+            interpCache.put(InterpKey(start, end, progress), axesCachedFont)
             return axesCachedFont
         }
 
@@ -160,8 +166,8 @@
         // Font.Builder#build won't throw IOException since creating fonts from existing fonts will
         // not do any IO work.
         val newFont = Font.Builder(start).setFontVariationSettings(newAxes.toTypedArray()).build()
-        interpCache[InterpKey(start, end, progress)] = newFont
-        verFontCache[VarFontKey(start, newAxes)] = newFont
+        interpCache.put(InterpKey(start, end, progress), newFont)
+        verFontCache.put(VarFontKey(start, newAxes), newFont)
         return newFont
     }
 
diff --git a/packages/SystemUI/animation/src/com/android/systemui/animation/TextAnimator.kt b/packages/SystemUI/animation/src/com/android/systemui/animation/TextAnimator.kt
index 9e9929e..3ee97be 100644
--- a/packages/SystemUI/animation/src/com/android/systemui/animation/TextAnimator.kt
+++ b/packages/SystemUI/animation/src/com/android/systemui/animation/TextAnimator.kt
@@ -24,8 +24,10 @@
 import android.graphics.Typeface
 import android.graphics.fonts.Font
 import android.text.Layout
+import android.util.LruCache
 
 private const val DEFAULT_ANIMATION_DURATION: Long = 300
+private const val TYPEFACE_CACHE_MAX_ENTRIES = 5
 
 typealias GlyphCallback = (TextAnimator.PositionedGlyph, Float) -> Unit
 /**
@@ -114,7 +116,7 @@
 
     private val fontVariationUtils = FontVariationUtils()
 
-    private val typefaceCache = HashMap<String, Typeface?>()
+    private val typefaceCache = LruCache<String, Typeface>(TYPEFACE_CACHE_MAX_ENTRIES)
 
     fun updateLayout(layout: Layout) {
         textInterpolator.layout = layout
@@ -218,12 +220,12 @@
         }
 
         if (!fvar.isNullOrBlank()) {
-            textInterpolator.targetPaint.typeface =
-                typefaceCache.getOrElse(fvar) {
-                    textInterpolator.targetPaint.fontVariationSettings = fvar
+            textInterpolator.targetPaint.typeface = typefaceCache.get(fvar) ?: run {
+                textInterpolator.targetPaint.fontVariationSettings = fvar
+                textInterpolator.targetPaint.typeface?.also {
                     typefaceCache.put(fvar, textInterpolator.targetPaint.typeface)
-                    textInterpolator.targetPaint.typeface
                 }
+            }
         }
 
         if (color != null) {
diff --git a/packages/SystemUI/compose/core/tests/Android.bp b/packages/SystemUI/compose/core/tests/Android.bp
index 6119e96..06d94ac 100644
--- a/packages/SystemUI/compose/core/tests/Android.bp
+++ b/packages/SystemUI/compose/core/tests/Android.bp
@@ -44,5 +44,5 @@
         "androidx.compose.ui_ui-test-manifest",
     ],
 
-    kotlincflags: ["-Xjvm-default=enable"],
+    kotlincflags: ["-Xjvm-default=all"],
 }
diff --git a/packages/SystemUI/compose/features/tests/Android.bp b/packages/SystemUI/compose/features/tests/Android.bp
index ff534bd..c7c9140 100644
--- a/packages/SystemUI/compose/features/tests/Android.bp
+++ b/packages/SystemUI/compose/features/tests/Android.bp
@@ -44,5 +44,5 @@
         "androidx.compose.ui_ui-test-manifest",
     ],
 
-    kotlincflags: ["-Xjvm-default=enable"],
+    kotlincflags: ["-Xjvm-default=all"],
 }
diff --git a/packages/SystemUI/customization/Android.bp b/packages/SystemUI/customization/Android.bp
index dc450bb..fc37b355 100644
--- a/packages/SystemUI/customization/Android.bp
+++ b/packages/SystemUI/customization/Android.bp
@@ -48,5 +48,5 @@
     ],
     min_sdk_version: "current",
     plugins: ["dagger2-compiler"],
-    kotlincflags: ["-Xjvm-default=enable"],
+    kotlincflags: ["-Xjvm-default=all"],
 }
diff --git a/packages/SystemUI/res/values/dimens.xml b/packages/SystemUI/res/values/dimens.xml
index 3faacf2..adc4c55 100644
--- a/packages/SystemUI/res/values/dimens.xml
+++ b/packages/SystemUI/res/values/dimens.xml
@@ -396,7 +396,7 @@
     <dimen name="navigation_key_width">70dp</dimen>
 
     <!-- The width/height of the icon of a navigation button -->
-    <dimen name="navigation_icon_size">32dp</dimen>
+    <dimen name="navigation_icon_size">24dp</dimen>
 
     <!-- The padding on the side of the navigation bar. Must be greater than or equal to
          navigation_extra_key_width -->
diff --git a/packages/SystemUI/shared/Android.bp b/packages/SystemUI/shared/Android.bp
index 28e786b..ca30e15 100644
--- a/packages/SystemUI/shared/Android.bp
+++ b/packages/SystemUI/shared/Android.bp
@@ -65,7 +65,7 @@
     ],
     min_sdk_version: "current",
     plugins: ["dagger2-compiler"],
-    kotlincflags: ["-Xjvm-default=enable"],
+    kotlincflags: ["-Xjvm-default=all"],
 }
 
 java_library {
diff --git a/packages/SystemUI/src/com/android/keyguard/KeyguardSecurityContainerController.java b/packages/SystemUI/src/com/android/keyguard/KeyguardSecurityContainerController.java
index 061bab8..16299c7 100644
--- a/packages/SystemUI/src/com/android/keyguard/KeyguardSecurityContainerController.java
+++ b/packages/SystemUI/src/com/android/keyguard/KeyguardSecurityContainerController.java
@@ -752,7 +752,7 @@
                 case SimPuk:
                     // Shortcut for SIM PIN/PUK to go to directly to user's security screen or home
                     SecurityMode securityMode = mSecurityModel.getSecurityMode(targetUserId);
-                    if (securityMode == SecurityMode.None && mLockPatternUtils.isLockScreenDisabled(
+                    if (securityMode == SecurityMode.None || mLockPatternUtils.isLockScreenDisabled(
                             KeyguardUpdateMonitor.getCurrentUser())) {
                         finish = true;
                         eventSubtype = BOUNCER_DISMISS_SIM;
diff --git a/packages/SystemUI/src/com/android/systemui/dreams/DreamOverlayStateController.java b/packages/SystemUI/src/com/android/systemui/dreams/DreamOverlayStateController.java
index a475653..a2872e3 100644
--- a/packages/SystemUI/src/com/android/systemui/dreams/DreamOverlayStateController.java
+++ b/packages/SystemUI/src/com/android/systemui/dreams/DreamOverlayStateController.java
@@ -182,6 +182,10 @@
      * Returns collection of present {@link Complication}.
      */
     public Collection<Complication> getComplications(boolean filterByAvailability) {
+        if (isLowLightActive()) {
+            // Don't show complications on low light.
+            return Collections.emptyList();
+        }
         return Collections.unmodifiableCollection(filterByAvailability
                 ? mComplications
                 .stream()
@@ -193,7 +197,8 @@
                     if (mShouldShowComplications) {
                         return (requiredTypes & getAvailableComplicationTypes()) == requiredTypes;
                     }
-                    return (requiredTypes & mSupportedTypes) == requiredTypes;
+                    final int typesToAlwaysShow = mSupportedTypes & getAvailableComplicationTypes();
+                    return (requiredTypes & typesToAlwaysShow) == requiredTypes;
                 })
                 .collect(Collectors.toCollection(HashSet::new))
                 : mComplications);
diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardUnlockAnimationController.kt b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardUnlockAnimationController.kt
index 90562dc..7992a34 100644
--- a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardUnlockAnimationController.kt
+++ b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardUnlockAnimationController.kt
@@ -167,7 +167,6 @@
          * for the canned animation (if applicable) so interested parties can sync with it. If no
          * canned animation is playing, these are both 0.
          */
-        @JvmDefault
         fun onUnlockAnimationStarted(
             playingCannedAnimation: Boolean,
             fromWakeAndUnlock: Boolean,
@@ -180,7 +179,6 @@
          * The keyguard is no longer visible in this state and the app/launcher behind the keyguard
          * is now completely visible.
          */
-        @JvmDefault
         fun onUnlockAnimationFinished() {}
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/ui/viewmodel/OccludedToLockscreenTransitionViewModel.kt b/packages/SystemUI/src/com/android/systemui/keyguard/ui/viewmodel/OccludedToLockscreenTransitionViewModel.kt
index 5770f3e..ddce516 100644
--- a/packages/SystemUI/src/com/android/systemui/keyguard/ui/viewmodel/OccludedToLockscreenTransitionViewModel.kt
+++ b/packages/SystemUI/src/com/android/systemui/keyguard/ui/viewmodel/OccludedToLockscreenTransitionViewModel.kt
@@ -47,6 +47,7 @@
             duration = TO_LOCKSCREEN_DURATION,
             onStep = { value -> -translatePx + value * translatePx },
             interpolator = EMPHASIZED_DECELERATE,
+            onCancel = { 0f },
         )
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/privacy/PrivacyConfig.kt b/packages/SystemUI/src/com/android/systemui/privacy/PrivacyConfig.kt
index d652889..d949a2a 100644
--- a/packages/SystemUI/src/com/android/systemui/privacy/PrivacyConfig.kt
+++ b/packages/SystemUI/src/com/android/systemui/privacy/PrivacyConfig.kt
@@ -145,13 +145,10 @@
     }
 
     interface Callback {
-        @JvmDefault
         fun onFlagMicCameraChanged(flag: Boolean) {}
 
-        @JvmDefault
         fun onFlagLocationChanged(flag: Boolean) {}
 
-        @JvmDefault
         fun onFlagMediaProjectionChanged(flag: Boolean) {}
     }
 }
\ No newline at end of file
diff --git a/packages/SystemUI/src/com/android/systemui/privacy/PrivacyItemController.kt b/packages/SystemUI/src/com/android/systemui/privacy/PrivacyItemController.kt
index a676150..eb8ef9b 100644
--- a/packages/SystemUI/src/com/android/systemui/privacy/PrivacyItemController.kt
+++ b/packages/SystemUI/src/com/android/systemui/privacy/PrivacyItemController.kt
@@ -216,7 +216,6 @@
     interface Callback : PrivacyConfig.Callback {
         fun onPrivacyItemsChanged(privacyItems: List<PrivacyItem>)
 
-        @JvmDefault
         fun onFlagAllChanged(flag: Boolean) {}
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/screenshot/ScrollCaptureClient.java b/packages/SystemUI/src/com/android/systemui/screenshot/ScrollCaptureClient.java
index 93e5021..e93f737 100644
--- a/packages/SystemUI/src/com/android/systemui/screenshot/ScrollCaptureClient.java
+++ b/packages/SystemUI/src/com/android/systemui/screenshot/ScrollCaptureClient.java
@@ -365,6 +365,9 @@
         @Override
         public void onImageAvailable(ImageReader reader) {
             synchronized (mLock) {
+                if (mCapturedImage != null) {
+                    mCapturedImage.close();
+                }
                 mCapturedImage = mReader.acquireLatestImage();
                 if (mCapturedArea != null) {
                     completeCaptureRequest();
diff --git a/packages/SystemUI/src/com/android/systemui/settings/DisplayTracker.kt b/packages/SystemUI/src/com/android/systemui/settings/DisplayTracker.kt
index bb7f721..468a75d 100644
--- a/packages/SystemUI/src/com/android/systemui/settings/DisplayTracker.kt
+++ b/packages/SystemUI/src/com/android/systemui/settings/DisplayTracker.kt
@@ -52,12 +52,12 @@
     interface Callback {
 
         /** Notifies that a display has been added. */
-        @JvmDefault fun onDisplayAdded(displayId: Int) {}
+        fun onDisplayAdded(displayId: Int) {}
 
         /** Notifies that a display has been removed. */
-        @JvmDefault fun onDisplayRemoved(displayId: Int) {}
+        fun onDisplayRemoved(displayId: Int) {}
 
         /** Notifies a display has been changed */
-        @JvmDefault fun onDisplayChanged(displayId: Int) {}
+        fun onDisplayChanged(displayId: Int) {}
     }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/settings/UserTracker.kt b/packages/SystemUI/src/com/android/systemui/settings/UserTracker.kt
index 33a3125..93a3e90 100644
--- a/packages/SystemUI/src/com/android/systemui/settings/UserTracker.kt
+++ b/packages/SystemUI/src/com/android/systemui/settings/UserTracker.kt
@@ -71,7 +71,6 @@
          * Same as {@link onUserChanging(Int, Context, CountDownLatch)} but the latch will be
          * auto-decremented after the completion of this method.
          */
-        @JvmDefault
         fun onUserChanging(newUser: Int, userContext: Context) {}
 
         /**
@@ -82,7 +81,6 @@
          * user switch duration. When overriding this method, countDown() MUST be called on the
          * latch once execution is complete.
          */
-        @JvmDefault
         fun onUserChanging(newUser: Int, userContext: Context, latch: CountDownLatch) {
             onUserChanging(newUser, userContext)
             latch.countDown()
@@ -93,13 +91,11 @@
          * Override this method to run things after the screen is unfrozen for the user switch.
          * Please see {@link #onUserChanging} if you need to hide jank.
          */
-        @JvmDefault
         fun onUserChanged(newUser: Int, userContext: Context) {}
 
         /**
          * Notifies that the current user's profiles have changed.
          */
-        @JvmDefault
         fun onProfilesChanged(profiles: List<@JvmSuppressWildcards UserInfo>) {}
     }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/shade/ShadeStateEvents.kt b/packages/SystemUI/src/com/android/systemui/shade/ShadeStateEvents.kt
index 56bb1a6..5804040 100644
--- a/packages/SystemUI/src/com/android/systemui/shade/ShadeStateEvents.kt
+++ b/packages/SystemUI/src/com/android/systemui/shade/ShadeStateEvents.kt
@@ -29,12 +29,12 @@
     interface ShadeStateEventsListener {
 
         /** Invoked when the notification panel starts or stops collapsing. */
-        @JvmDefault fun onPanelCollapsingChanged(isCollapsing: Boolean) {}
+        fun onPanelCollapsingChanged(isCollapsing: Boolean) {}
 
         /**
          * Invoked when the notification panel starts or stops launching an [android.app.Activity].
          */
-        @JvmDefault fun onLaunchingActivityChanged(isLaunchingActivity: Boolean) {}
+        fun onLaunchingActivityChanged(isLaunchingActivity: Boolean) {}
 
         /**
          * Invoked when the "expand immediate" attribute changes.
@@ -45,6 +45,6 @@
          * Another example is when full QS is showing, and we swipe up from the bottom. Instead of
          * going to QQS, the panel fully collapses.
          */
-        @JvmDefault fun onExpandImmediateChanged(isExpandImmediateEnabled: Boolean) {}
+        fun onExpandImmediateChanged(isExpandImmediateEnabled: Boolean) {}
     }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/NotificationShadeDepthController.kt b/packages/SystemUI/src/com/android/systemui/statusbar/NotificationShadeDepthController.kt
index 8dc7842..a23e3f29 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/NotificationShadeDepthController.kt
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/NotificationShadeDepthController.kt
@@ -546,7 +546,6 @@
          */
         fun onWallpaperZoomOutChanged(zoomOut: Float)
 
-        @JvmDefault
         fun onBlurRadiusChanged(blurRadius: Int) {}
     }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/connectivity/SignalCallback.kt b/packages/SystemUI/src/com/android/systemui/statusbar/connectivity/SignalCallback.kt
index 599beec..6be407a 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/connectivity/SignalCallback.kt
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/connectivity/SignalCallback.kt
@@ -29,7 +29,6 @@
      *
      * @param wifiIndicators a box type containing enough information to properly draw a Wi-Fi icon
      */
-    @JvmDefault
     fun setWifiIndicators(wifiIndicators: WifiIndicators) {}
 
     /**
@@ -42,7 +41,6 @@
      * NOTE: phones can have multiple subscriptions, so this [mobileDataIndicators] object should be
      * indexed based on its [subId][MobileDataIndicators.subId]
      */
-    @JvmDefault
     fun setMobileDataIndicators(mobileDataIndicators: MobileDataIndicators) {}
 
     /**
@@ -51,7 +49,6 @@
      *
      * @param subs a [SubscriptionInfo] for each subscription that we know about
      */
-    @JvmDefault
     fun setSubs(subs: List<@JvmSuppressWildcards SubscriptionInfo>) {}
 
     /**
@@ -63,7 +60,6 @@
      * @param show whether or not to show a "no sim" view
      * @param simDetected whether any SIM is detected or not
      */
-    @JvmDefault
     fun setNoSims(show: Boolean, simDetected: Boolean) {}
 
     /**
@@ -72,7 +68,6 @@
      *
      * @param icon an [IconState] for the current ethernet status
      */
-    @JvmDefault
     fun setEthernetIndicators(icon: IconState) {}
 
     /**
@@ -80,7 +75,6 @@
      *
      * @param icon an [IconState] for the current airplane mode status
      */
-    @JvmDefault
     fun setIsAirplaneMode(icon: IconState) {}
 
     /**
@@ -88,7 +82,6 @@
      *
      * @param enabled the current mobile data feature ennabled state
      */
-    @JvmDefault
     fun setMobileDataEnabled(enabled: Boolean) {}
 
     /**
@@ -97,7 +90,6 @@
      * @param noValidatedNetwork whether there is any validated network.
      * @param noNetworksAvailable whether there is any WiFi networks available.
      */
-    @JvmDefault
     fun setConnectivityStatus(
         noDefaultNetwork: Boolean,
         noValidatedNetwork: Boolean,
@@ -109,7 +101,6 @@
      * @param statusIcon the icon for the call indicator
      * @param subId subscription ID for which to update the UI
      */
-    @JvmDefault
     fun setCallIndicator(statusIcon: IconState, subId: Int) {}
 }
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/events/SystemStatusAnimationScheduler.kt b/packages/SystemUI/src/com/android/systemui/statusbar/events/SystemStatusAnimationScheduler.kt
index 2a18f1f..ef90890 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/events/SystemStatusAnimationScheduler.kt
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/events/SystemStatusAnimationScheduler.kt
@@ -49,11 +49,10 @@
     fun onSystemEventAnimationFinish(hasPersistentDot: Boolean): Animator? { return null }
 
     // Best method name, change my mind
-    @JvmDefault
     fun onSystemStatusAnimationTransitionToPersistentDot(contentDescription: String?): Animator? {
         return null
     }
-    @JvmDefault fun onHidePersistentDot(): Animator? { return null }
+    fun onHidePersistentDot(): Animator? { return null }
 }
 
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationWakeUpCoordinator.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationWakeUpCoordinator.kt
index 20af6ca..31a9f96 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationWakeUpCoordinator.kt
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationWakeUpCoordinator.kt
@@ -613,20 +613,20 @@
 
     interface WakeUpListener {
         /** Called whenever the notifications are fully hidden or shown */
-        @JvmDefault fun onFullyHiddenChanged(isFullyHidden: Boolean) {}
+        fun onFullyHiddenChanged(isFullyHidden: Boolean) {}
 
         /**
          * Called whenever the pulseExpansion changes
          *
          * @param expandingChanged if the user has started or stopped expanding
          */
-        @JvmDefault fun onPulseExpansionChanged(expandingChanged: Boolean) {}
+        fun onPulseExpansionChanged(expandingChanged: Boolean) {}
 
         /**
          * Called when the animator started by [scheduleDelayedDozeAmountAnimation] begins running
          * after the start delay, or after it ends/is cancelled.
          */
-        @JvmDefault fun onDelayedDozeAmountAnimationRunning(running: Boolean) {}
+        fun onDelayedDozeAmountAnimationRunning(running: Boolean) {}
     }
 
     companion object {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/Roundable.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/Roundable.kt
index 6deef2e..9675b43 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/Roundable.kt
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/Roundable.kt
@@ -22,33 +22,27 @@
 
     /** Current top roundness */
     @get:FloatRange(from = 0.0, to = 1.0)
-    @JvmDefault
     val topRoundness: Float
         get() = roundableState.topRoundness
 
     /** Current bottom roundness */
     @get:FloatRange(from = 0.0, to = 1.0)
-    @JvmDefault
     val bottomRoundness: Float
         get() = roundableState.bottomRoundness
 
     /** Max radius in pixel */
-    @JvmDefault
     val maxRadius: Float
         get() = roundableState.maxRadius
 
     /** Current top corner in pixel, based on [topRoundness] and [maxRadius] */
-    @JvmDefault
     val topCornerRadius: Float
         get() = topRoundness * maxRadius
 
     /** Current bottom corner in pixel, based on [bottomRoundness] and [maxRadius] */
-    @JvmDefault
     val bottomCornerRadius: Float
         get() = bottomRoundness * maxRadius
 
     /** Get and update the current radii */
-    @JvmDefault
     val updatedRadii: FloatArray
         get() =
             roundableState.radiiBuffer.also { radii ->
@@ -71,7 +65,6 @@
      * @param sourceType the source from which the request for roundness comes.
      * @return Whether the roundness was changed.
      */
-    @JvmDefault
     fun requestTopRoundness(
         @FloatRange(from = 0.0, to = 1.0) value: Float,
         sourceType: SourceType,
@@ -116,7 +109,6 @@
      * @param sourceType the source from which the request for roundness comes.
      * @return Whether the roundness was changed.
      */
-    @JvmDefault
     fun requestTopRoundness(
         @FloatRange(from = 0.0, to = 1.0) value: Float,
         sourceType: SourceType,
@@ -140,7 +132,6 @@
      * @param sourceType the source from which the request for roundness comes.
      * @return Whether the roundness was changed.
      */
-    @JvmDefault
     fun requestBottomRoundness(
         @FloatRange(from = 0.0, to = 1.0) value: Float,
         sourceType: SourceType,
@@ -185,7 +176,6 @@
      * @param sourceType the source from which the request for roundness comes.
      * @return Whether the roundness was changed.
      */
-    @JvmDefault
     fun requestBottomRoundness(
         @FloatRange(from = 0.0, to = 1.0) value: Float,
         sourceType: SourceType,
@@ -210,7 +200,6 @@
      * @param animate true if it should animate to that value.
      * @return Whether the roundness was changed.
      */
-    @JvmDefault
     fun requestRoundness(
         @FloatRange(from = 0.0, to = 1.0) top: Float,
         @FloatRange(from = 0.0, to = 1.0) bottom: Float,
@@ -237,7 +226,6 @@
      * @param sourceType the source from which the request for roundness comes.
      * @return Whether the roundness was changed.
      */
-    @JvmDefault
     fun requestRoundness(
         @FloatRange(from = 0.0, to = 1.0) top: Float,
         @FloatRange(from = 0.0, to = 1.0) bottom: Float,
@@ -261,7 +249,6 @@
      * @param sourceType the source from which the request for roundness comes.
      * @param animate true if it should animate to that value.
      */
-    @JvmDefault
     fun requestRoundnessReset(sourceType: SourceType, animate: Boolean) {
         requestRoundness(top = 0f, bottom = 0f, sourceType = sourceType, animate = animate)
     }
@@ -275,19 +262,16 @@
      *
      * @param sourceType the source from which the request for roundness comes.
      */
-    @JvmDefault
     fun requestRoundnessReset(sourceType: SourceType) {
         requestRoundnessReset(sourceType = sourceType, animate = roundableState.targetView.isShown)
     }
 
     /** Apply the roundness changes, usually means invalidate the [RoundableState.targetView]. */
-    @JvmDefault
     fun applyRoundnessAndInvalidate() {
         roundableState.targetView.invalidate()
     }
 
     /** @return true if top or bottom roundness is not zero. */
-    @JvmDefault
     fun hasRoundedCorner(): Boolean {
         return topRoundness != 0f || bottomRoundness != 0f
     }
@@ -298,7 +282,6 @@
      *
      * This method reuses the previous [radii] for performance reasons.
      */
-    @JvmDefault
     fun updateRadii(
         topCornerRadius: Float,
         bottomCornerRadius: Float,
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/interruption/KeyguardNotificationVisibilityProvider.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/interruption/KeyguardNotificationVisibilityProvider.kt
index 7513aa7..7b65454 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/interruption/KeyguardNotificationVisibilityProvider.kt
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/interruption/KeyguardNotificationVisibilityProvider.kt
@@ -87,6 +87,7 @@
 
     private val userTrackerCallback = object : UserTracker.Callback {
         override fun onUserChanged(newUser: Int, userContext: Context) {
+            readShowSilentNotificationSetting()
             if (isLockedOrLocking) {
                 // maybe public mode changed
                 notifyStateChanged("onUserSwitched")
diff --git a/packages/SystemUI/src/com/android/systemui/touch/TouchInsetManager.java b/packages/SystemUI/src/com/android/systemui/touch/TouchInsetManager.java
index a4f1ef4f..ef0fa01 100644
--- a/packages/SystemUI/src/com/android/systemui/touch/TouchInsetManager.java
+++ b/packages/SystemUI/src/com/android/systemui/touch/TouchInsetManager.java
@@ -90,6 +90,9 @@
             final Region cumulativeRegion = Region.obtain();
 
             mTrackedViews.stream().forEach(view -> {
+                if (!view.isAttachedToWindow()) {
+                    return;
+                }
                 final Rect boundaries = new Rect();
                 view.getDrawingRect(boundaries);
                 ((ViewGroup) view.getRootView()).offsetDescendantRectToMyCoords(view, boundaries);
diff --git a/packages/SystemUI/src/com/android/systemui/util/UserAwareController.kt b/packages/SystemUI/src/com/android/systemui/util/UserAwareController.kt
index 693c270..5582ced 100644
--- a/packages/SystemUI/src/com/android/systemui/util/UserAwareController.kt
+++ b/packages/SystemUI/src/com/android/systemui/util/UserAwareController.kt
@@ -23,7 +23,6 @@
  * changes.
  */
 interface UserAwareController {
-    @JvmDefault
     fun changeUser(newUser: UserHandle) {}
 
     val currentUserId: Int
diff --git a/packages/SystemUI/tests/src/com/android/keyguard/KeyguardSecurityContainerControllerTest.java b/packages/SystemUI/tests/src/com/android/keyguard/KeyguardSecurityContainerControllerTest.java
index d492758..3b3df23 100644
--- a/packages/SystemUI/tests/src/com/android/keyguard/KeyguardSecurityContainerControllerTest.java
+++ b/packages/SystemUI/tests/src/com/android/keyguard/KeyguardSecurityContainerControllerTest.java
@@ -163,8 +163,6 @@
     @Captor
     private ArgumentCaptor<KeyguardSecurityContainer.SwipeListener> mSwipeListenerArgumentCaptor;
 
-    private Configuration mConfiguration;
-
     private KeyguardSecurityContainerController mKeyguardSecurityContainerController;
     private KeyguardPasswordViewController mKeyguardPasswordViewController;
     private KeyguardPasswordView mKeyguardPasswordView;
@@ -172,12 +170,12 @@
 
     @Before
     public void setup() {
-        mConfiguration = new Configuration();
-        mConfiguration.setToDefaults(); // Defaults to ORIENTATION_UNDEFINED.
         mTestableResources = mContext.getOrCreateTestableResources();
+        mTestableResources.getResources().getConfiguration().orientation =
+                Configuration.ORIENTATION_UNDEFINED;
 
         when(mView.getContext()).thenReturn(mContext);
-        when(mView.getResources()).thenReturn(mContext.getResources());
+        when(mView.getResources()).thenReturn(mTestableResources.getResources());
         FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(/* width=  */ 0, /* height= */
                 0);
         lp.gravity = 0;
@@ -254,6 +252,8 @@
 
     @Test
     public void onResourcesUpdate_callsThroughOnRotationChange() {
+        clearInvocations(mView);
+
         // Rotation is the same, shouldn't cause an update
         mKeyguardSecurityContainerController.updateResources();
         verify(mView, never()).initMode(eq(MODE_DEFAULT), eq(mGlobalSettings), eq(mFalsingManager),
@@ -579,12 +579,12 @@
         // Set initial gravity
         mTestableResources.addOverride(R.integer.keyguard_host_view_gravity,
                 Gravity.CENTER);
+        mTestableResources.addOverride(
+                R.bool.can_use_one_handed_bouncer, false);
 
         // Kick off the initial pass...
         mKeyguardSecurityContainerController.onInit();
-        verify(mView).setLayoutParams(argThat(
-                (ArgumentMatcher<FrameLayout.LayoutParams>) argument ->
-                        argument.gravity == Gravity.CENTER));
+        verify(mView).setLayoutParams(any());
         clearInvocations(mView);
 
         // Now simulate a config change
@@ -592,9 +592,7 @@
                 Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
 
         mKeyguardSecurityContainerController.updateResources();
-        verify(mView).setLayoutParams(argThat(
-                (ArgumentMatcher<FrameLayout.LayoutParams>) argument ->
-                        argument.gravity == (Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM)));
+        verify(mView).setLayoutParams(any());
     }
 
     @Test
diff --git a/packages/SystemUI/tests/src/com/android/systemui/animation/FontInterpolatorTest.kt b/packages/SystemUI/tests/src/com/android/systemui/animation/FontInterpolatorTest.kt
index 8a5c5b5..57a355f 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/animation/FontInterpolatorTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/animation/FontInterpolatorTest.kt
@@ -106,4 +106,29 @@
         val reversedFont = interp.lerp(endFont, startFont, 0.5f)
         assertThat(resultFont).isSameInstanceAs(reversedFont)
     }
+
+    @Test
+    fun testCacheMaxSize() {
+        val interp = FontInterpolator()
+
+        val startFont = Font.Builder(sFont)
+                .setFontVariationSettings("'wght' 100")
+                .build()
+        val endFont = Font.Builder(sFont)
+                .setFontVariationSettings("'wght' 1")
+                .build()
+        val resultFont = interp.lerp(startFont, endFont, 0.5f)
+        for (i in 0..FONT_CACHE_MAX_ENTRIES + 1) {
+            val f1 = Font.Builder(sFont)
+                    .setFontVariationSettings("'wght' ${i * 100}")
+                    .build()
+            val f2 = Font.Builder(sFont)
+                    .setFontVariationSettings("'wght' $i")
+                    .build()
+            interp.lerp(f1, f2, 0.5f)
+        }
+
+        val cachedFont = interp.lerp(startFont, endFont, 0.5f)
+        assertThat(resultFont).isNotSameInstanceAs(cachedFont)
+    }
 }
diff --git a/packages/SystemUI/tests/src/com/android/systemui/dreams/DreamOverlayStateControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/dreams/DreamOverlayStateControllerTest.java
index 34fa76f..d0c1c4d 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/dreams/DreamOverlayStateControllerTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/dreams/DreamOverlayStateControllerTest.java
@@ -235,6 +235,23 @@
     }
 
     @Test
+    public void testComplicationsNotShownForLowLight() {
+        final Complication complication = Mockito.mock(Complication.class);
+        final DreamOverlayStateController stateController = getDreamOverlayStateController(true);
+
+        // Add a complication and verify it's returned in getComplications.
+        stateController.addComplication(complication);
+        mExecutor.runAllReady();
+        assertThat(stateController.getComplications().contains(complication))
+                .isTrue();
+
+        stateController.setLowLightActive(true);
+        mExecutor.runAllReady();
+
+        assertThat(stateController.getComplications()).isEmpty();
+    }
+
+    @Test
     public void testNotifyLowLightChanged() {
         final DreamOverlayStateController stateController = getDreamOverlayStateController(true);
 
@@ -336,6 +353,34 @@
         }
     }
 
+    @Test
+    public void testHomeControlsDoNotShowIfNotAvailable_featureEnabled() {
+        when(mFeatureFlags.isEnabled(Flags.ALWAYS_SHOW_HOME_CONTROLS_ON_DREAMS)).thenReturn(true);
+
+        final DreamOverlayStateController stateController = getDreamOverlayStateController(true);
+        stateController.setShouldShowComplications(true);
+
+        final Complication homeControlsComplication = Mockito.mock(Complication.class);
+        when(homeControlsComplication.getRequiredTypeAvailability())
+                .thenReturn(Complication.COMPLICATION_TYPE_HOME_CONTROLS);
+
+        stateController.addComplication(homeControlsComplication);
+
+        final DreamOverlayStateController.Callback callback =
+                Mockito.mock(DreamOverlayStateController.Callback.class);
+
+        stateController.addCallback(callback);
+        mExecutor.runAllReady();
+
+        // No home controls since it is not available.
+        assertThat(stateController.getComplications()).doesNotContain(homeControlsComplication);
+
+        stateController.setAvailableComplicationTypes(Complication.COMPLICATION_TYPE_HOME_CONTROLS
+                | Complication.COMPLICATION_TYPE_WEATHER);
+        mExecutor.runAllReady();
+        assertThat(stateController.getComplications()).contains(homeControlsComplication);
+    }
+
     private DreamOverlayStateController getDreamOverlayStateController(boolean overlayEnabled) {
         return new DreamOverlayStateController(mExecutor, overlayEnabled, mFeatureFlags);
     }
diff --git a/packages/SystemUI/tests/src/com/android/systemui/keyguard/ui/viewmodel/OccludedToLockscreenTransitionViewModelTest.kt b/packages/SystemUI/tests/src/com/android/systemui/keyguard/ui/viewmodel/OccludedToLockscreenTransitionViewModelTest.kt
index 0c4e845..efa5f0c 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/keyguard/ui/viewmodel/OccludedToLockscreenTransitionViewModelTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/keyguard/ui/viewmodel/OccludedToLockscreenTransitionViewModelTest.kt
@@ -92,6 +92,21 @@
             job.cancel()
         }
 
+    @Test
+    fun lockscreenTranslationYResettedAfterJobCancelled() =
+        runTest(UnconfinedTestDispatcher()) {
+            val values = mutableListOf<Float>()
+
+            val pixels = 100
+            val job =
+                underTest.lockscreenTranslationY(pixels).onEach { values.add(it) }.launchIn(this)
+            repository.sendTransitionStep(step(0.5f, TransitionState.CANCELED))
+
+            assertThat(values.last()).isEqualTo(0f)
+
+            job.cancel()
+        }
+
     private fun step(
         value: Float,
         state: TransitionState = TransitionState.RUNNING
diff --git a/packages/SystemUI/unfold/Android.bp b/packages/SystemUI/unfold/Android.bp
index 2e0a946..1f0181f 100644
--- a/packages/SystemUI/unfold/Android.bp
+++ b/packages/SystemUI/unfold/Android.bp
@@ -33,7 +33,7 @@
         "dagger2",
         "jsr330",
     ],
-    kotlincflags: ["-Xjvm-default=enable"],
+    kotlincflags: ["-Xjvm-default=all"],
     java_version: "1.8",
     sdk_version: "current",
     min_sdk_version: "current",
diff --git a/packages/SystemUI/unfold/src/com/android/systemui/unfold/UnfoldTransitionProgressProvider.kt b/packages/SystemUI/unfold/src/com/android/systemui/unfold/UnfoldTransitionProgressProvider.kt
index fee485d..896444d 100644
--- a/packages/SystemUI/unfold/src/com/android/systemui/unfold/UnfoldTransitionProgressProvider.kt
+++ b/packages/SystemUI/unfold/src/com/android/systemui/unfold/UnfoldTransitionProgressProvider.kt
@@ -35,14 +35,12 @@
 
     interface TransitionProgressListener {
         /** Called when transition is started */
-        @JvmDefault
         fun onTransitionStarted() {}
 
         /**
          * Called whenever transition progress is updated, [progress] is a value of the animation
          * where 0 is fully folded, 1 is fully unfolded
          */
-        @JvmDefault
         fun onTransitionProgress(@FloatRange(from = 0.0, to = 1.0) progress: Float) {}
 
         /**
@@ -51,11 +49,9 @@
          * For example, in [PhysicsBasedUnfoldTransitionProgressProvider] this could happen when the
          * animation is not tied to the hinge angle anymore and it is about to run fixed animation.
          */
-        @JvmDefault
         fun onTransitionFinishing() {}
 
         /** Called when transition is completely finished */
-        @JvmDefault
         fun onTransitionFinished() {}
     }
 }
diff --git a/packages/SystemUI/unfold/src/com/android/systemui/unfold/updates/FoldStateProvider.kt b/packages/SystemUI/unfold/src/com/android/systemui/unfold/updates/FoldStateProvider.kt
index 0af372f..bce7e88 100644
--- a/packages/SystemUI/unfold/src/com/android/systemui/unfold/updates/FoldStateProvider.kt
+++ b/packages/SystemUI/unfold/src/com/android/systemui/unfold/updates/FoldStateProvider.kt
@@ -31,9 +31,9 @@
     val isFinishedOpening: Boolean
 
     interface FoldUpdatesListener {
-        @JvmDefault fun onHingeAngleUpdate(@FloatRange(from = 0.0, to = 180.0) angle: Float) {}
-        @JvmDefault fun onFoldUpdate(@FoldUpdate update: Int) {}
-        @JvmDefault fun onUnfoldedScreenAvailable() {}
+        fun onHingeAngleUpdate(@FloatRange(from = 0.0, to = 180.0) angle: Float) {}
+        fun onFoldUpdate(@FoldUpdate update: Int) {}
+        fun onUnfoldedScreenAvailable() {}
     }
 
     @IntDef(
diff --git a/services/companion/java/com/android/server/companion/virtual/OWNERS b/services/companion/java/com/android/server/companion/virtual/OWNERS
index 5e8291f..83143a4 100644
--- a/services/companion/java/com/android/server/companion/virtual/OWNERS
+++ b/services/companion/java/com/android/server/companion/virtual/OWNERS
@@ -2,4 +2,5 @@
 
 ogunwale@google.com
 michaelwr@google.com
-vladokom@google.com
\ No newline at end of file
+vladokom@google.com
+marvinramin@google.com
\ No newline at end of file
diff --git a/services/core/Android.bp b/services/core/Android.bp
index 81f600e..e687aa9 100644
--- a/services/core/Android.bp
+++ b/services/core/Android.bp
@@ -215,8 +215,7 @@
     name: "services.core.json.gz",
     srcs: [":checked-protolog.json"],
     out: ["services.core.protolog.json.gz"],
-    cmd: "$(location minigzip) -c < $(in) > $(out)",
-    tools: ["minigzip"],
+    cmd: "gzip -c < $(in) > $(out)",
 }
 
 prebuilt_etc {
diff --git a/services/core/java/com/android/server/StorageManagerService.java b/services/core/java/com/android/server/StorageManagerService.java
index a3137a6..af0e61e 100644
--- a/services/core/java/com/android/server/StorageManagerService.java
+++ b/services/core/java/com/android/server/StorageManagerService.java
@@ -1357,6 +1357,21 @@
         }
     }
 
+    /**
+     * This method checks if the volume is public and the volume is visible and the volume it is
+     * trying to mount doesn't have the same mount user id as the current user being maintained by
+     * StorageManagerService and change the mount Id. The checks are same as
+     * {@link StorageManagerService#maybeRemountVolumes(int)}
+     * @param VolumeInfo object to consider for changing the mountId
+     */
+    private void updateVolumeMountIdIfRequired(VolumeInfo vol) {
+        synchronized (mLock) {
+            if (!vol.isPrimary() && vol.isVisible() && vol.getMountUserId() != mCurrentUserId) {
+                vol.mountUserId = mCurrentUserId;
+            }
+        }
+    }
+
     private boolean supportsBlockCheckpoint() throws RemoteException {
         enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
         return mVold.supportsBlockCheckpoint();
@@ -1468,13 +1483,14 @@
         }
 
         @Override
-        public void onVolumeStateChanged(String volId, final int newState) {
+        public void onVolumeStateChanged(String volId, final int newState, final int userId) {
             synchronized (mLock) {
                 final VolumeInfo vol = mVolumes.get(volId);
                 if (vol != null) {
                     final int oldState = vol.state;
                     vol.state = newState;
                     final VolumeInfo vInfo = new VolumeInfo(vol);
+                    vInfo.mountUserId = userId;
                     final SomeArgs args = SomeArgs.obtain();
                     args.arg1 = vInfo;
                     args.argi1 = oldState;
@@ -2322,7 +2338,7 @@
         if (isMountDisallowed(vol)) {
             throw new SecurityException("Mounting " + volId + " restricted by policy");
         }
-
+        updateVolumeMountIdIfRequired(vol);
         mount(vol);
     }
 
diff --git a/services/core/java/com/android/server/accounts/AccountManagerService.java b/services/core/java/com/android/server/accounts/AccountManagerService.java
index 2a9dc52..f1bcb2b 100644
--- a/services/core/java/com/android/server/accounts/AccountManagerService.java
+++ b/services/core/java/com/android/server/accounts/AccountManagerService.java
@@ -4854,7 +4854,6 @@
             if (accountType == null) throw new IllegalArgumentException("accountType is null");
             mAccounts = accounts;
             mStripAuthTokenFromResult = stripAuthTokenFromResult;
-            mResponse = response;
             mAccountType = accountType;
             mExpectActivityLaunch = expectActivityLaunch;
             mCreationTime = SystemClock.elapsedRealtime();
@@ -4868,8 +4867,8 @@
             if (response != null) {
                 try {
                     response.asBinder().linkToDeath(this, 0 /* flags */);
+                    mResponse = response;
                 } catch (RemoteException e) {
-                    mResponse = null;
                     binderDied();
                 }
             }
diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java
index a5662ed..b837688 100644
--- a/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -5176,6 +5176,9 @@
             // Tell anyone interested that we are done booting!
             SystemProperties.set("sys.boot_completed", "1");
 
+            // Start PSI monitoring in LMKD if it was skipped earlier.
+            ProcessList.startPsiMonitoringAfterBoot();
+
             // And trigger dev.bootcomplete if we are not showing encryption progress
             if (!"trigger_restart_min_framework".equals(VoldProperties.decrypt().orElse(""))
                     || "".equals(VoldProperties.encrypt_progress().orElse(""))) {
diff --git a/services/core/java/com/android/server/am/ProcessList.java b/services/core/java/com/android/server/am/ProcessList.java
index 5efaafe..bce3173 100644
--- a/services/core/java/com/android/server/am/ProcessList.java
+++ b/services/core/java/com/android/server/am/ProcessList.java
@@ -350,6 +350,7 @@
     static final byte LMK_UPDATE_PROPS = 7;
     static final byte LMK_KILL_OCCURRED = 8; // Msg to subscribed clients on kill occurred event
     static final byte LMK_STATE_CHANGED = 9; // Msg to subscribed clients on state changed
+    static final byte LMK_START_MONITORING = 9; // Start monitoring if delayed earlier
 
     // Low Memory Killer Daemon command codes.
     // These must be kept in sync with async_event_type definitions in lmkd.h
@@ -1483,6 +1484,15 @@
         return true;
     }
 
+    /**
+     * {@hide}
+     */
+    public static void startPsiMonitoringAfterBoot() {
+        ByteBuffer buf = ByteBuffer.allocate(4);
+        buf.putInt(LMK_START_MONITORING);
+        writeLmkd(buf, null);
+    }
+
     private static boolean writeLmkd(ByteBuffer buf, ByteBuffer repl) {
         if (!sLmkdConnection.isConnected()) {
             // try to connect immediately and then keep retrying
@@ -1741,6 +1751,7 @@
 
             if (debuggableFlag) {
                 runtimeFlags |= Zygote.DEBUG_ENABLE_JDWP;
+                runtimeFlags |= Zygote.DEBUG_ENABLE_PTRACE;
                 runtimeFlags |= Zygote.DEBUG_JAVA_DEBUGGABLE;
                 // Also turn on CheckJNI for debuggable apps. It's quite
                 // awkward to turn on otherwise.
diff --git a/services/core/java/com/android/server/audio/AudioService.java b/services/core/java/com/android/server/audio/AudioService.java
index 47a98e8..4c234a0 100644
--- a/services/core/java/com/android/server/audio/AudioService.java
+++ b/services/core/java/com/android/server/audio/AudioService.java
@@ -2254,8 +2254,8 @@
                 synchronized (VolumeStreamState.class) {
                     mStreamStates[AudioSystem.STREAM_DTMF]
                             .setAllIndexes(mStreamStates[dtmfStreamAlias], caller);
-                    mStreamStates[AudioSystem.STREAM_ACCESSIBILITY].mVolumeIndexSettingName =
-                            System.VOLUME_SETTINGS_INT[a11yStreamAlias];
+                    mStreamStates[AudioSystem.STREAM_ACCESSIBILITY].setSettingName(
+                            System.VOLUME_SETTINGS_INT[a11yStreamAlias]);
                     mStreamStates[AudioSystem.STREAM_ACCESSIBILITY].setAllIndexes(
                             mStreamStates[a11yStreamAlias], caller);
                 }
@@ -3449,8 +3449,14 @@
                         hdmiClient = mHdmiTvClient;
                     }
 
-                    if (((mHdmiPlaybackClient != null && isFullVolumeDevice(device))
-                            || (mHdmiTvClient != null && mHdmiSystemAudioSupported))
+                    boolean playbackDeviceConditions = mHdmiPlaybackClient != null
+                            && isFullVolumeDevice(device);
+                    boolean tvConditions = mHdmiTvClient != null
+                            && mHdmiSystemAudioSupported
+                            && !isAbsoluteVolumeDevice(device)
+                            && !isA2dpAbsoluteVolumeDevice(device);
+
+                    if ((playbackDeviceConditions || tvConditions)
                             && mHdmiCecVolumeControlEnabled
                             && streamTypeAlias == AudioSystem.STREAM_MUSIC) {
                         int keyCode = KeyEvent.KEYCODE_UNKNOWN;
@@ -7512,7 +7518,7 @@
         private int mPublicStreamType = AudioSystem.STREAM_MUSIC;
         private AudioAttributes mAudioAttributes = AudioProductStrategy.getDefaultAttributes();
         private boolean mIsMuted = false;
-        private final String mSettingName;
+        private String mSettingName;
 
         // No API in AudioSystem to get a device from strategy or from attributes.
         // Need a valid public stream type to use current API getDeviceForStream
@@ -7841,15 +7847,19 @@
         }
 
         private void persistVolumeGroup(int device) {
-            if (mUseFixedVolume) {
+            // No need to persist the index if the volume group is backed up
+            // by a public stream type as this is redundant
+            if (mUseFixedVolume || mHasValidStreamType) {
                 return;
             }
             if (DEBUG_VOL) {
                 Log.v(TAG, "persistVolumeGroup: storing index " + getIndex(device) + " for group "
                         + mAudioVolumeGroup.name()
                         + ", device " + AudioSystem.getOutputDeviceName(device)
-                        + " and User=" + getCurrentUserId());
+                        + " and User=" + getCurrentUserId()
+                        + " mSettingName: " + mSettingName);
             }
+
             boolean success = mSettings.putSystemIntForUser(mContentResolver,
                     getSettingNameForDevice(device),
                     getIndex(device),
@@ -7912,6 +7922,14 @@
             return mSettingName + "_" + AudioSystem.getOutputDeviceName(device);
         }
 
+        void setSettingName(String settingName) {
+            mSettingName = settingName;
+        }
+
+        String getSettingName() {
+            return mSettingName;
+        }
+
         private void dump(PrintWriter pw) {
             pw.println("- VOLUME GROUP " + mAudioVolumeGroup.name() + ":");
             pw.print("   Muted: ");
@@ -8036,6 +8054,9 @@
          */
         public void setVolumeGroupState(VolumeGroupState volumeGroupState) {
             mVolumeGroupState = volumeGroupState;
+            if (mVolumeGroupState != null) {
+                mVolumeGroupState.setSettingName(mVolumeIndexSettingName);
+            }
         }
         /**
          * Update the minimum index that can be used without MODIFY_AUDIO_SETTINGS permission
@@ -8106,6 +8127,17 @@
             return (mVolumeIndexSettingName != null && !mVolumeIndexSettingName.isEmpty());
         }
 
+        void setSettingName(String settingName) {
+            mVolumeIndexSettingName = settingName;
+            if (mVolumeGroupState != null) {
+                mVolumeGroupState.setSettingName(mVolumeIndexSettingName);
+            }
+        }
+
+        String getSettingName() {
+            return mVolumeIndexSettingName;
+        }
+
         public void readSettings() {
             synchronized (mSettingsLock) {
                 synchronized (VolumeStreamState.class) {
@@ -8754,7 +8786,7 @@
             if (streamState.hasValidSettingsName()) {
                 mSettings.putSystemIntForUser(mContentResolver,
                         streamState.getSettingNameForDevice(device),
-                        (streamState.getIndex(device) + 5)/ 10,
+                        (streamState.getIndex(device) + 5) / 10,
                         UserHandle.USER_CURRENT);
             }
         }
diff --git a/services/core/java/com/android/server/camera/CameraServiceProxy.java b/services/core/java/com/android/server/camera/CameraServiceProxy.java
index 5a3a3df..2f354b3 100644
--- a/services/core/java/com/android/server/camera/CameraServiceProxy.java
+++ b/services/core/java/com/android/server/camera/CameraServiceProxy.java
@@ -595,16 +595,26 @@
 
         @Override
         public boolean isCameraDisabled(int userId) {
-            DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
-            if (dpm == null) {
-                Slog.e(TAG, "Failed to get the device policy manager service");
+            if (Binder.getCallingUid() != Process.CAMERASERVER_UID) {
+                Slog.e(TAG, "Calling UID: " + Binder.getCallingUid()
+                        + " doesn't match expected camera service UID!");
                 return false;
             }
+            final long ident = Binder.clearCallingIdentity();
             try {
-                return dpm.getCameraDisabled(null, userId);
-            } catch (Exception e) {
-                e.printStackTrace();
-                return false;
+                DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
+                if (dpm == null) {
+                    Slog.e(TAG, "Failed to get the device policy manager service");
+                    return false;
+                }
+                try {
+                    return dpm.getCameraDisabled(null, userId);
+                } catch (Exception e) {
+                    e.printStackTrace();
+                    return false;
+                }
+            } finally {
+                Binder.restoreCallingIdentity(ident);
             }
         }
     };
diff --git a/services/core/java/com/android/server/connectivity/Vpn.java b/services/core/java/com/android/server/connectivity/Vpn.java
index 63bb026..cba5039 100644
--- a/services/core/java/com/android/server/connectivity/Vpn.java
+++ b/services/core/java/com/android/server/connectivity/Vpn.java
@@ -3346,7 +3346,7 @@
                 // Transforms do not need to be persisted; the IkeSession will keep
                 // them alive for us
                 mIpSecManager.applyTunnelModeTransform(mTunnelIface, direction, transform);
-            } catch (IOException e) {
+            } catch (IOException | IllegalArgumentException e) {
                 Log.d(TAG, "Transform application failed for token " + token, e);
                 onSessionLost(token, e);
             }
@@ -3440,7 +3440,7 @@
                         mTunnelIface, IpSecManager.DIRECTION_IN, inTransform);
                 mIpSecManager.applyTunnelModeTransform(
                         mTunnelIface, IpSecManager.DIRECTION_OUT, outTransform);
-            } catch (IOException e) {
+            } catch (IOException | IllegalArgumentException e) {
                 Log.d(TAG, "Transform application failed for token " + token, e);
                 onSessionLost(token, e);
             }
diff --git a/services/core/java/com/android/server/locksettings/OWNERS b/services/core/java/com/android/server/locksettings/OWNERS
index 55b0cff..5d49863 100644
--- a/services/core/java/com/android/server/locksettings/OWNERS
+++ b/services/core/java/com/android/server/locksettings/OWNERS
@@ -1,3 +1,4 @@
+# Bug component: 1333694
 ebiggers@google.com
 jaggies@google.com
 rubinxu@google.com
diff --git a/services/core/java/com/android/server/om/OverlayManagerService.java b/services/core/java/com/android/server/om/OverlayManagerService.java
index 362b26e..bdab341 100644
--- a/services/core/java/com/android/server/om/OverlayManagerService.java
+++ b/services/core/java/com/android/server/om/OverlayManagerService.java
@@ -1109,6 +1109,21 @@
             int callingUid = Binder.getCallingUid();
             mActorEnforcer.enforceActor(overlayInfo, methodName, callingUid, realUserId);
         }
+
+        /**
+         * @hide
+         */
+        public String getPartitionOrder() {
+            return mImpl.getOverlayConfig().getPartitionOrder();
+        }
+
+        /**
+         * @hide
+         */
+        public boolean isDefaultPartitionOrder() {
+            return mImpl.getOverlayConfig().isDefaultPartitionOrder();
+        }
+
     };
 
     private static final class PackageManagerHelperImpl implements PackageManagerHelper {
diff --git a/services/core/java/com/android/server/om/OverlayManagerServiceImpl.java b/services/core/java/com/android/server/om/OverlayManagerServiceImpl.java
index 38781fa..bd0f657 100644
--- a/services/core/java/com/android/server/om/OverlayManagerServiceImpl.java
+++ b/services/core/java/com/android/server/om/OverlayManagerServiceImpl.java
@@ -876,4 +876,8 @@
             super(message, cause);
         }
     }
+
+    OverlayConfig getOverlayConfig() {
+        return mOverlayConfig;
+    }
 }
diff --git a/services/core/java/com/android/server/om/OverlayManagerShellCommand.java b/services/core/java/com/android/server/om/OverlayManagerShellCommand.java
index 89939a3..4beb391 100644
--- a/services/core/java/com/android/server/om/OverlayManagerShellCommand.java
+++ b/services/core/java/com/android/server/om/OverlayManagerShellCommand.java
@@ -16,6 +16,8 @@
 
 package com.android.server.om;
 
+import static com.android.internal.content.om.OverlayConfig.PARTITION_ORDER_FILE_PATH;
+
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.content.Context;
@@ -79,6 +81,8 @@
                     return runLookup();
                 case "fabricate":
                     return runFabricate();
+                case "partition-order":
+                    return runPartitionOrder();
                 default:
                     return handleDefaultCommands(cmd);
             }
@@ -130,6 +134,9 @@
         out.println("    Create an overlay from a single resource. Caller must be root. Example:");
         out.println("      fabricate --target android --name LighterGray \\");
         out.println("                android:color/lighter_gray 0x1c 0xffeeeeee");
+        out.println("  partition-order");
+        out.println("    Print the partition order from overlay config and how this order");
+        out.println("    got established, by default or by " + PARTITION_ORDER_FILE_PATH);
     }
 
     private int runList() throws RemoteException {
@@ -230,6 +237,14 @@
         return 0;
     }
 
+    private int runPartitionOrder() throws RemoteException {
+        final PrintWriter out = getOutPrintWriter();
+        out.println("Partition order (low to high priority): " + mInterface.getPartitionOrder());
+        out.println("Established by " + (mInterface.isDefaultPartitionOrder() ? "default"
+                : PARTITION_ORDER_FILE_PATH));
+        return 0;
+    }
+
     private int runFabricate() throws RemoteException {
         final PrintWriter err = getErrPrintWriter();
         if (Binder.getCallingUid() != Process.ROOT_UID) {
diff --git a/services/core/java/com/android/server/os/NativeTombstoneManager.java b/services/core/java/com/android/server/os/NativeTombstoneManager.java
index 45192b7..88f70cf 100644
--- a/services/core/java/com/android/server/os/NativeTombstoneManager.java
+++ b/services/core/java/com/android/server/os/NativeTombstoneManager.java
@@ -363,7 +363,7 @@
                 return false;
             }
 
-            if (Math.abs(exitInfo.getTimestamp() - mTimestampMs) > 5000) {
+            if (Math.abs(exitInfo.getTimestamp() - mTimestampMs) > 10000) {
                 return false;
             }
 
diff --git a/services/core/java/com/android/server/pm/InstallPackageHelper.java b/services/core/java/com/android/server/pm/InstallPackageHelper.java
index 3dac305..293f7e9 100644
--- a/services/core/java/com/android/server/pm/InstallPackageHelper.java
+++ b/services/core/java/com/android/server/pm/InstallPackageHelper.java
@@ -2093,9 +2093,25 @@
                     // The caller explicitly specified INSTALL_ALL_USERS flag.
                     // Thus, updating the settings to install the app for all users.
                     for (int currentUserId : allUsers) {
-                        ps.setInstalled(true, currentUserId);
-                        ps.setEnabled(COMPONENT_ENABLED_STATE_DEFAULT, userId,
-                                installerPackageName);
+                        // If the app is already installed for the currentUser,
+                        // keep it as installed as we might be updating the app at this place.
+                        // If not currently installed, check if the currentUser is restricted by
+                        // DISALLOW_INSTALL_APPS or DISALLOW_DEBUGGING_FEATURES device policy.
+                        // Install / update the app if the user isn't restricted. Skip otherwise.
+                        final boolean installedForCurrentUser = ArrayUtils.contains(
+                                installedForUsers, currentUserId);
+                        final boolean restrictedByPolicy =
+                                mPm.isUserRestricted(currentUserId,
+                                        UserManager.DISALLOW_INSTALL_APPS)
+                                || mPm.isUserRestricted(currentUserId,
+                                        UserManager.DISALLOW_DEBUGGING_FEATURES);
+                        if (installedForCurrentUser || !restrictedByPolicy) {
+                            ps.setInstalled(true, currentUserId);
+                            ps.setEnabled(COMPONENT_ENABLED_STATE_DEFAULT, currentUserId,
+                                    installerPackageName);
+                        } else {
+                            ps.setInstalled(false, currentUserId);
+                        }
                     }
                 }
 
diff --git a/services/core/java/com/android/server/pm/PackageInstallerService.java b/services/core/java/com/android/server/pm/PackageInstallerService.java
index 02cf433..3036e0b 100644
--- a/services/core/java/com/android/server/pm/PackageInstallerService.java
+++ b/services/core/java/com/android/server/pm/PackageInstallerService.java
@@ -482,7 +482,7 @@
             }
         } catch (FileNotFoundException e) {
             // Missing sessions are okay, probably first boot
-        } catch (IOException | XmlPullParserException e) {
+        } catch (IOException | XmlPullParserException | ArrayIndexOutOfBoundsException e) {
             Slog.wtf(TAG, "Failed reading install sessions", e);
         } finally {
             IoUtils.closeQuietly(fis);
diff --git a/services/core/java/com/android/server/pm/SELinuxMMAC.java b/services/core/java/com/android/server/pm/SELinuxMMAC.java
index c0e191f..5a941fe 100644
--- a/services/core/java/com/android/server/pm/SELinuxMMAC.java
+++ b/services/core/java/com/android/server/pm/SELinuxMMAC.java
@@ -80,6 +80,8 @@
     // Append targetSdkVersion=n to existing seinfo label where n is the app's targetSdkVersion
     private static final String TARGETSDKVERSION_STR = ":targetSdkVersion=";
 
+    private static final String PARTITION_STR = ":partition=";
+
     /**
      * Allows opt-in to the latest targetSdkVersion enforced changes without changing target SDK.
      * Turning this change on for an app targeting the latest SDK or higher is a no-op.
@@ -370,6 +372,23 @@
         return pkg.getTargetSdkVersion();
     }
 
+    private static String getPartition(AndroidPackage pkg) {
+        if (pkg.isSystemExt()) {
+            return "system_ext";
+        } else if (pkg.isProduct()) {
+            return "product";
+        } else if (pkg.isVendor()) {
+            return "vendor";
+        } else if (pkg.isOem()) {
+            return "oem";
+        } else if (pkg.isOdm()) {
+            return "odm";
+        } else if (pkg.isSystem()) {
+            return "system";
+        }
+        return "";
+    }
+
     /**
      * Selects a security label to a package based on input parameters and the seinfo tag taken
      * from a matched policy. All signature based policy stanzas are consulted and, if no match
@@ -433,6 +452,11 @@
 
         seInfo += TARGETSDKVERSION_STR + targetSdkVersion;
 
+        String partition = getPartition(pkg);
+        if (!partition.isEmpty()) {
+            seInfo += PARTITION_STR + partition;
+        }
+
         if (DEBUG_POLICY_INSTALL) {
             Slog.i(TAG, "package (" + pkg.getPackageName() + ") labeled with "
                     + "seinfo=" + seInfo);
diff --git a/services/core/java/com/android/server/pm/permission/OneTimePermissionUserManager.java b/services/core/java/com/android/server/pm/permission/OneTimePermissionUserManager.java
index 881f870..d28048c 100644
--- a/services/core/java/com/android/server/pm/permission/OneTimePermissionUserManager.java
+++ b/services/core/java/com/android/server/pm/permission/OneTimePermissionUserManager.java
@@ -16,23 +16,26 @@
 
 package com.android.server.pm.permission;
 
-import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_CACHED;
-
 import android.annotation.NonNull;
 import android.app.ActivityManager;
+import android.app.ActivityManagerInternal;
 import android.app.AlarmManager;
+import android.app.IActivityManager;
+import android.app.IUidObserver;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.pm.PackageManager;
 import android.os.Handler;
+import android.os.RemoteException;
 import android.permission.PermissionControllerManager;
 import android.provider.DeviceConfig;
 import android.util.Log;
 import android.util.SparseArray;
 
 import com.android.internal.annotations.GuardedBy;
+import com.android.server.LocalServices;
 
 /**
  * Class that handles one-time permissions for a user
@@ -47,7 +50,8 @@
             "one_time_permissions_killed_delay_millis";
 
     private final @NonNull Context mContext;
-    private final @NonNull ActivityManager mActivityManager;
+    private final @NonNull IActivityManager mIActivityManager;
+    private final @NonNull ActivityManagerInternal mActivityManagerInternal;
     private final @NonNull AlarmManager mAlarmManager;
     private final @NonNull PermissionControllerManager mPermissionControllerManager;
 
@@ -77,49 +81,15 @@
 
     OneTimePermissionUserManager(@NonNull Context context) {
         mContext = context;
-        mActivityManager = context.getSystemService(ActivityManager.class);
+        mIActivityManager = ActivityManager.getService();
+        mActivityManagerInternal = LocalServices.getService(ActivityManagerInternal.class);
         mAlarmManager = context.getSystemService(AlarmManager.class);
         mPermissionControllerManager = context.getSystemService(PermissionControllerManager.class);
         mHandler = context.getMainThreadHandler();
     }
 
-    /**
-     * Starts a one-time permission session for a given package. A one-time permission session is
-     * ended if app becomes inactive. Inactivity is defined as the package's uid importance level
-     * staying > importanceToResetTimer for timeoutMillis milliseconds. If the package's uid
-     * importance level goes <= importanceToResetTimer then the timer is reset and doesn't start
-     * until going > importanceToResetTimer.
-     * <p>
-     * When this timeoutMillis is reached if the importance level is <= importanceToKeepSessionAlive
-     * then the session is extended until either the importance goes above
-     * importanceToKeepSessionAlive which will end the session or <= importanceToResetTimer which
-     * will continue the session and reset the timer.
-     * </p>
-     * <p>
-     * Importance levels are defined in {@link android.app.ActivityManager.RunningAppProcessInfo}.
-     * </p>
-     * <p>
-     * Once the session ends PermissionControllerService#onNotifyOneTimePermissionSessionTimeout
-     * is invoked.
-     * </p>
-     * <p>
-     * Note that if there is currently an active session for a package a new one isn't created and
-     * the existing one isn't changed.
-     * </p>
-     * @param packageName The package to start a one-time permission session for
-     * @param timeoutMillis Number of milliseconds for an app to be in an inactive state
-     * @param revokeAfterKilledDelayMillis Number of milliseconds to wait after the process dies
-     *                                     before ending the session. Set to -1 to use default value
-     *                                     for the device.
-     * @param importanceToResetTimer The least important level to uid must be to reset the timer
-     * @param importanceToKeepSessionAlive The least important level the uid must be to keep the
-     *                                     session alive
-     *
-     * @hide
-     */
     void startPackageOneTimeSession(@NonNull String packageName, long timeoutMillis,
-            long revokeAfterKilledDelayMillis, int importanceToResetTimer,
-            int importanceToKeepSessionAlive) {
+            long revokeAfterKilledDelayMillis) {
         int uid;
         try {
             uid = mContext.getPackageManager().getPackageUid(packageName, 0);
@@ -131,13 +101,11 @@
         synchronized (mLock) {
             PackageInactivityListener listener = mListeners.get(uid);
             if (listener != null) {
-                listener.updateSessionParameters(timeoutMillis, revokeAfterKilledDelayMillis,
-                        importanceToResetTimer, importanceToKeepSessionAlive);
+                listener.updateSessionParameters(timeoutMillis, revokeAfterKilledDelayMillis);
                 return;
             }
             listener = new PackageInactivityListener(uid, packageName, timeoutMillis,
-                    revokeAfterKilledDelayMillis, importanceToResetTimer,
-                    importanceToKeepSessionAlive);
+                    revokeAfterKilledDelayMillis);
             mListeners.put(uid, listener);
         }
     }
@@ -182,34 +150,58 @@
 
         private static final long TIMER_INACTIVE = -1;
 
+        private static final int STATE_GONE = 0;
+        private static final int STATE_TIMER = 1;
+        private static final int STATE_ACTIVE = 2;
+
         private final int mUid;
         private final @NonNull String mPackageName;
         private long mTimeout;
         private long mRevokeAfterKilledDelay;
-        private int mImportanceToResetTimer;
-        private int mImportanceToKeepSessionAlive;
 
         private boolean mIsAlarmSet;
         private boolean mIsFinished;
 
         private long mTimerStart = TIMER_INACTIVE;
 
-        private final ActivityManager.OnUidImportanceListener mStartTimerListener;
-        private final ActivityManager.OnUidImportanceListener mSessionKillableListener;
-        private final ActivityManager.OnUidImportanceListener mGoneListener;
-
         private final Object mInnerLock = new Object();
         private final Object mToken = new Object();
+        private final IUidObserver.Stub mObserver = new IUidObserver.Stub() {
+            @Override
+            public void onUidGone(int uid, boolean disabled) {
+                if (uid == mUid) {
+                    PackageInactivityListener.this.updateUidState(STATE_GONE);
+                }
+            }
+
+            @Override
+            public void onUidStateChanged(int uid, int procState, long procStateSeq,
+                    int capability) {
+                if (uid == mUid) {
+                    if (procState > ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE
+                            && procState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
+                        PackageInactivityListener.this.updateUidState(STATE_TIMER);
+                    } else {
+                        PackageInactivityListener.this.updateUidState(STATE_ACTIVE);
+                    }
+                }
+            }
+
+            public void onUidActive(int uid) {
+            }
+            public void onUidIdle(int uid, boolean disabled) {
+            }
+            public void onUidProcAdjChanged(int uid) {
+            }
+            public void onUidCachedChanged(int uid, boolean cached) {
+            }
+        };
 
         private PackageInactivityListener(int uid, @NonNull String packageName, long timeout,
-                long revokeAfterkilledDelay, int importanceToResetTimer,
-                int importanceToKeepSessionAlive) {
-
+                long revokeAfterkilledDelay) {
             Log.i(LOG_TAG,
                     "Start tracking " + packageName + ". uid=" + uid + " timeout=" + timeout
-                            + " killedDelay=" + revokeAfterkilledDelay
-                            + " importanceToResetTimer=" + importanceToResetTimer
-                            + " importanceToKeepSessionAlive=" + importanceToKeepSessionAlive);
+                            + " killedDelay=" + revokeAfterkilledDelay);
 
             mUid = uid;
             mPackageName = packageName;
@@ -219,27 +211,24 @@
                             DeviceConfig.NAMESPACE_PERMISSIONS, PROPERTY_KILLED_DELAY_CONFIG_KEY,
                             DEFAULT_KILLED_DELAY_MILLIS)
                     : revokeAfterkilledDelay;
-            mImportanceToResetTimer = importanceToResetTimer;
-            mImportanceToKeepSessionAlive = importanceToKeepSessionAlive;
 
-            mStartTimerListener =
-                    (changingUid, importance) -> onImportanceChanged(changingUid, importance);
-            mSessionKillableListener =
-                    (changingUid, importance) -> onImportanceChanged(changingUid, importance);
-            mGoneListener =
-                    (changingUid, importance) -> onImportanceChanged(changingUid, importance);
+            try {
+                mIActivityManager.registerUidObserver(mObserver,
+                        ActivityManager.UID_OBSERVER_GONE | ActivityManager.UID_OBSERVER_PROCSTATE,
+                        ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE,
+                        null);
+            } catch (RemoteException e) {
+                Log.e(LOG_TAG, "Couldn't check uid proc state", e);
+                // Can't register uid observer, just revoke immediately
+                synchronized (mInnerLock) {
+                    onPackageInactiveLocked();
+                }
+            }
 
-            mActivityManager.addOnUidImportanceListener(mStartTimerListener,
-                    importanceToResetTimer);
-            mActivityManager.addOnUidImportanceListener(mSessionKillableListener,
-                    importanceToKeepSessionAlive);
-            mActivityManager.addOnUidImportanceListener(mGoneListener, IMPORTANCE_CACHED);
-
-            onImportanceChanged(mUid, mActivityManager.getPackageImportance(packageName));
+            updateUidState();
         }
 
-        public void updateSessionParameters(long timeoutMillis, long revokeAfterKilledDelayMillis,
-                int importanceToResetTimer, int importanceToKeepSessionAlive) {
+        public void updateSessionParameters(long timeoutMillis, long revokeAfterKilledDelayMillis) {
             synchronized (mInnerLock) {
                 mTimeout = Math.min(mTimeout, timeoutMillis);
                 mRevokeAfterKilledDelay = Math.min(mRevokeAfterKilledDelay,
@@ -248,63 +237,74 @@
                                 DeviceConfig.NAMESPACE_PERMISSIONS,
                                 PROPERTY_KILLED_DELAY_CONFIG_KEY, DEFAULT_KILLED_DELAY_MILLIS)
                                 : revokeAfterKilledDelayMillis);
-                mImportanceToResetTimer = Math.min(importanceToResetTimer, mImportanceToResetTimer);
-                mImportanceToKeepSessionAlive = Math.min(importanceToKeepSessionAlive,
-                        mImportanceToKeepSessionAlive);
                 Log.v(LOG_TAG,
                         "Updated params for " + mPackageName + ". timeout=" + mTimeout
-                                + " killedDelay=" + mRevokeAfterKilledDelay
-                                + " importanceToResetTimer=" + mImportanceToResetTimer
-                                + " importanceToKeepSessionAlive=" + mImportanceToKeepSessionAlive);
-                onImportanceChanged(mUid, mActivityManager.getPackageImportance(mPackageName));
+                                + " killedDelay=" + mRevokeAfterKilledDelay);
+                updateUidState();
             }
         }
 
-        private void onImportanceChanged(int uid, int importance) {
-            if (uid != mUid) {
-                return;
-            }
+        private int getCurrentState() {
+            return getStateFromProcState(mActivityManagerInternal.getUidProcessState(mUid));
+        }
 
-            Log.v(LOG_TAG, "Importance changed for " + mPackageName + " (" + mUid + ")."
-                    + " importance=" + importance);
+        private int getStateFromProcState(int procState) {
+            if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
+                return STATE_GONE;
+            } else {
+                if (procState > ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
+                    return STATE_TIMER;
+                } else {
+                    return STATE_ACTIVE;
+                }
+            }
+        }
+
+        private void updateUidState() {
+            updateUidState(getCurrentState());
+        }
+
+        private void updateUidState(int state) {
+            Log.v(LOG_TAG, "Updating state for " + mPackageName + " (" + mUid + ")."
+                    + " state=" + state);
             synchronized (mInnerLock) {
                 // Remove any pending inactivity callback
                 mHandler.removeCallbacksAndMessages(mToken);
 
-                if (importance > IMPORTANCE_CACHED) {
+                if (state == STATE_GONE) {
                     if (mRevokeAfterKilledDelay == 0) {
                         onPackageInactiveLocked();
                         return;
                     }
                     // Delay revocation in case app is restarting
                     mHandler.postDelayed(() -> {
-                        int imp = mActivityManager.getUidImportance(mUid);
-                        if (imp > IMPORTANCE_CACHED) {
-                            onPackageInactiveLocked();
-                        } else {
-                            if (DEBUG) {
-                                Log.d(LOG_TAG, "No longer gone after delayed revocation. "
-                                        + "Rechecking for " + mPackageName + " (" + mUid + ").");
+                        int currentState;
+                        synchronized (mInnerLock) {
+                            currentState = getCurrentState();
+                            if (currentState == STATE_GONE) {
+                                onPackageInactiveLocked();
+                                return;
                             }
-                            onImportanceChanged(mUid, imp);
                         }
+                        if (DEBUG) {
+                            Log.d(LOG_TAG, "No longer gone after delayed revocation. "
+                                    + "Rechecking for " + mPackageName + " (" + mUid
+                                    + ").");
+                        }
+                        updateUidState(currentState);
                     }, mToken, mRevokeAfterKilledDelay);
                     return;
-                }
-                if (importance > mImportanceToResetTimer) {
+                } else if (state == STATE_TIMER) {
                     if (mTimerStart == TIMER_INACTIVE) {
                         if (DEBUG) {
                             Log.d(LOG_TAG, "Start the timer for "
                                     + mPackageName + " (" + mUid + ").");
                         }
                         mTimerStart = System.currentTimeMillis();
+                        setAlarmLocked();
                     }
-                } else {
+                } else if (state == STATE_ACTIVE) {
                     mTimerStart = TIMER_INACTIVE;
-                }
-                if (importance > mImportanceToKeepSessionAlive) {
-                    setAlarmLocked();
-                } else {
                     cancelAlarmLocked();
                 }
             }
@@ -318,19 +318,9 @@
                 mIsFinished = true;
                 cancelAlarmLocked();
                 try {
-                    mActivityManager.removeOnUidImportanceListener(mStartTimerListener);
-                } catch (IllegalArgumentException e) {
-                    Log.e(LOG_TAG, "Could not remove start timer listener", e);
-                }
-                try {
-                    mActivityManager.removeOnUidImportanceListener(mSessionKillableListener);
-                } catch (IllegalArgumentException e) {
-                    Log.e(LOG_TAG, "Could not remove session killable listener", e);
-                }
-                try {
-                    mActivityManager.removeOnUidImportanceListener(mGoneListener);
-                } catch (IllegalArgumentException e) {
-                    Log.e(LOG_TAG, "Could not remove gone listener", e);
+                    mIActivityManager.unregisterUidObserver(mObserver);
+                } catch (RemoteException e) {
+                    Log.e(LOG_TAG, "Unable to unregister uid observer.", e);
                 }
             }
         }
@@ -394,9 +384,11 @@
                         mPermissionControllerManager.notifyOneTimePermissionSessionTimeout(
                                 mPackageName);
                     });
-            mActivityManager.removeOnUidImportanceListener(mStartTimerListener);
-            mActivityManager.removeOnUidImportanceListener(mSessionKillableListener);
-            mActivityManager.removeOnUidImportanceListener(mGoneListener);
+            try {
+                mIActivityManager.unregisterUidObserver(mObserver);
+            } catch (RemoteException e) {
+                Log.e(LOG_TAG, "Unable to unregister uid observer.", e);
+            }
             synchronized (mLock) {
                 mListeners.remove(mUid);
             }
diff --git a/services/core/java/com/android/server/pm/permission/PermissionManagerService.java b/services/core/java/com/android/server/pm/permission/PermissionManagerService.java
index 1c2603c..9ebe6b4 100644
--- a/services/core/java/com/android/server/pm/permission/PermissionManagerService.java
+++ b/services/core/java/com/android/server/pm/permission/PermissionManagerService.java
@@ -386,8 +386,7 @@
 
     @Override
     public void startOneTimePermissionSession(String packageName, @UserIdInt int userId,
-            long timeoutMillis, long revokeAfterKilledDelayMillis, int importanceToResetTimer,
-            int importanceToKeepSessionAlive) {
+            long timeoutMillis, long revokeAfterKilledDelayMillis) {
         mContext.enforceCallingOrSelfPermission(
                 Manifest.permission.MANAGE_ONE_TIME_PERMISSION_SESSIONS,
                 "Must hold " + Manifest.permission.MANAGE_ONE_TIME_PERMISSION_SESSIONS
@@ -397,8 +396,7 @@
         final long token = Binder.clearCallingIdentity();
         try {
             getOneTimePermissionUserManager(userId).startPackageOneTimeSession(packageName,
-                    timeoutMillis, revokeAfterKilledDelayMillis, importanceToResetTimer,
-                    importanceToKeepSessionAlive);
+                    timeoutMillis, revokeAfterKilledDelayMillis);
         } finally {
             Binder.restoreCallingIdentity(token);
         }
diff --git a/services/core/java/com/android/server/pm/permission/PermissionManagerServiceImpl.java b/services/core/java/com/android/server/pm/permission/PermissionManagerServiceImpl.java
index 9ed5aa7..5221cfa 100644
--- a/services/core/java/com/android/server/pm/permission/PermissionManagerServiceImpl.java
+++ b/services/core/java/com/android/server/pm/permission/PermissionManagerServiceImpl.java
@@ -2116,6 +2116,12 @@
                 if (!isStorageOrMedia) {
                     continue;
                 }
+                boolean isSystemOrPolicyFixed = (getPermissionFlags(newPackage.getPackageName(),
+                        permInfo.name, userId) & (FLAG_PERMISSION_SYSTEM_FIXED
+                        | FLAG_PERMISSION_POLICY_FIXED)) != 0;
+                if (isSystemOrPolicyFixed) {
+                    continue;
+                }
 
                 EventLog.writeEvent(0x534e4554, "171430330", newPackage.getUid(),
                         "Revoking permission " + permInfo.name + " from package "
diff --git a/services/core/java/com/android/server/policy/PhoneWindowManager.java b/services/core/java/com/android/server/policy/PhoneWindowManager.java
index 95ab464..fe31bf0 100644
--- a/services/core/java/com/android/server/policy/PhoneWindowManager.java
+++ b/services/core/java/com/android/server/policy/PhoneWindowManager.java
@@ -5857,10 +5857,10 @@
                 pw.println(endcallBehaviorToString(mEndcallBehavior));
         pw.print(prefix);
         // TODO(b/117479243): handle it in InputPolicy
-        pw.print("mDisplayHomeButtonHandlers=");
+        pw.println("mDisplayHomeButtonHandlers=");
         for (int i = 0; i < mDisplayHomeButtonHandlers.size(); i++) {
             final int key = mDisplayHomeButtonHandlers.keyAt(i);
-            pw.println(mDisplayHomeButtonHandlers.get(key));
+            pw.print(prefix); pw.print("  "); pw.println(mDisplayHomeButtonHandlers.get(key));
         }
         pw.print(prefix); pw.print("mKeyguardOccluded="); pw.print(isKeyguardOccluded());
                 pw.print(" mKeyguardOccludedChanged="); pw.print(mKeyguardOccludedChanged);
@@ -6131,6 +6131,7 @@
 
     private class HdmiVideoExtconUEventObserver extends ExtconStateObserver<Boolean> {
         private static final String HDMI_EXIST = "HDMI=1";
+        private static final String DP_EXIST = "DP=1";
         private static final String NAME = "hdmi";
 
         private boolean init(ExtconInfo hdmi) {
@@ -6161,7 +6162,8 @@
         public Boolean parseState(ExtconInfo extconIfno, String state) {
             // extcon event state changes from kernel4.9
             // new state will be like STATE=HDMI=1
-            return state.contains(HDMI_EXIST);
+            // or like STATE=DP=1 for newer kernel
+            return state.contains(HDMI_EXIST) || state.contains(DP_EXIST);
         }
     }
 
diff --git a/services/core/java/com/android/server/security/FileIntegrityService.java b/services/core/java/com/android/server/security/FileIntegrityService.java
index 466ac74..bdeb9a8 100644
--- a/services/core/java/com/android/server/security/FileIntegrityService.java
+++ b/services/core/java/com/android/server/security/FileIntegrityService.java
@@ -125,13 +125,7 @@
     }
 
     private void loadAllCertificates() {
-        // A better alternative to load certificates would be to read from .fs-verity kernel
-        // keyring, which fsverity_init loads to during earlier boot time from the same sources
-        // below. But since the read operation from keyring is not provided in kernel, we need to
-        // duplicate the same loading logic here.
-
         // Load certificates trusted by the device manufacturer.
-        // NB: Directories need to be synced with system/security/fsverity_init/fsverity_init.cpp.
         final String relativeDir = "etc/security/fsverity";
         loadCertificatesFromDirectory(Environment.getRootDirectory().toPath()
                 .resolve(relativeDir));
diff --git a/services/core/java/com/android/server/vibrator/SetAmplitudeVibratorStep.java b/services/core/java/com/android/server/vibrator/SetAmplitudeVibratorStep.java
index 6fb9111..959b94b 100644
--- a/services/core/java/com/android/server/vibrator/SetAmplitudeVibratorStep.java
+++ b/services/core/java/com/android/server/vibrator/SetAmplitudeVibratorStep.java
@@ -167,7 +167,9 @@
         while (i < segmentCount) {
             VibrationEffectSegment segment = segments.get(i);
             if (!(segment instanceof StepSegment)
-                    || ((StepSegment) segment).getAmplitude() == 0) {
+                    // play() will ignore segments with zero duration, so it's important that
+                    // zero-duration segments don't affect this method.
+                    || (segment.getDuration() > 0 && ((StepSegment) segment).getAmplitude() == 0)) {
                 break;
             }
             timing += segment.getDuration();
diff --git a/services/core/java/com/android/server/wm/DisplayContent.java b/services/core/java/com/android/server/wm/DisplayContent.java
index f5a41c9..b12d8bd 100644
--- a/services/core/java/com/android/server/wm/DisplayContent.java
+++ b/services/core/java/com/android/server/wm/DisplayContent.java
@@ -6302,9 +6302,9 @@
             // Don't do recursive work.
             return;
         }
-        mInEnsureActivitiesVisible = true;
         mAtmService.mTaskSupervisor.beginActivityVisibilityUpdate();
         try {
+            mInEnsureActivitiesVisible = true;
             forAllRootTasks(rootTask -> {
                 rootTask.ensureActivitiesVisible(starting, configChanges, preserveWindows,
                         notifyClients);
diff --git a/services/core/java/com/android/server/wm/LetterboxUiController.java b/services/core/java/com/android/server/wm/LetterboxUiController.java
index 4b2cb7d..684e787 100644
--- a/services/core/java/com/android/server/wm/LetterboxUiController.java
+++ b/services/core/java/com/android/server/wm/LetterboxUiController.java
@@ -48,9 +48,9 @@
 import static android.view.WindowManager.PROPERTY_CAMERA_COMPAT_ALLOW_REFRESH;
 import static android.view.WindowManager.PROPERTY_CAMERA_COMPAT_ENABLE_REFRESH_VIA_PAUSE;
 import static android.view.WindowManager.PROPERTY_COMPAT_ALLOW_DISPLAY_ORIENTATION_OVERRIDE;
+import static android.view.WindowManager.PROPERTY_COMPAT_ALLOW_IGNORING_ORIENTATION_REQUEST_WHEN_LOOP_DETECTED;
 import static android.view.WindowManager.PROPERTY_COMPAT_ALLOW_ORIENTATION_OVERRIDE;
 import static android.view.WindowManager.PROPERTY_COMPAT_ENABLE_FAKE_FOCUS;
-import static android.view.WindowManager.PROPERTY_COMPAT_IGNORE_ORIENTATION_REQUEST_WHEN_LOOP_DETECTED;
 import static android.view.WindowManager.PROPERTY_COMPAT_IGNORE_REQUESTED_ORIENTATION;
 
 import static com.android.internal.util.FrameworkStatsLog.APP_COMPAT_STATE_CHANGED__LETTERBOX_POSITION__BOTTOM;
@@ -241,7 +241,7 @@
     private final Boolean mBooleanPropertyIgnoreRequestedOrientation;
 
     @Nullable
-    private final Boolean mBooleanPropertyIgnoreOrientationRequestWhenLoopDetected;
+    private final Boolean mBooleanPropertyAllowIgnoringOrientationRequestWhenLoopDetected;
 
     @Nullable
     private final Boolean mBooleanPropertyFakeFocus;
@@ -264,10 +264,10 @@
                 readComponentProperty(packageManager, mActivityRecord.packageName,
                         mLetterboxConfiguration::isPolicyForIgnoringRequestedOrientationEnabled,
                         PROPERTY_COMPAT_IGNORE_REQUESTED_ORIENTATION);
-        mBooleanPropertyIgnoreOrientationRequestWhenLoopDetected =
+        mBooleanPropertyAllowIgnoringOrientationRequestWhenLoopDetected =
                 readComponentProperty(packageManager, mActivityRecord.packageName,
                         mLetterboxConfiguration::isPolicyForIgnoringRequestedOrientationEnabled,
-                        PROPERTY_COMPAT_IGNORE_ORIENTATION_REQUEST_WHEN_LOOP_DETECTED);
+                        PROPERTY_COMPAT_ALLOW_IGNORING_ORIENTATION_REQUEST_WHEN_LOOP_DETECTED);
         mBooleanPropertyFakeFocus =
                 readComponentProperty(packageManager, mActivityRecord.packageName,
                         mLetterboxConfiguration::isCompatFakeFocusEnabled,
@@ -453,7 +453,7 @@
                 /* gatingCondition */ mLetterboxConfiguration
                     ::isPolicyForIgnoringRequestedOrientationEnabled,
                 mIsOverrideEnableCompatIgnoreOrientationRequestWhenLoopDetectedEnabled,
-                mBooleanPropertyIgnoreOrientationRequestWhenLoopDetected)) {
+                mBooleanPropertyAllowIgnoringOrientationRequestWhenLoopDetected)) {
             return false;
         }
 
diff --git a/services/core/java/com/android/server/wm/RootWindowContainer.java b/services/core/java/com/android/server/wm/RootWindowContainer.java
index 10673a3..23ea6d7 100644
--- a/services/core/java/com/android/server/wm/RootWindowContainer.java
+++ b/services/core/java/com/android/server/wm/RootWindowContainer.java
@@ -1833,9 +1833,8 @@
             // Don't do recursive work.
             return;
         }
-
+        mTaskSupervisor.beginActivityVisibilityUpdate();
         try {
-            mTaskSupervisor.beginActivityVisibilityUpdate();
             // First the front root tasks. In case any are not fullscreen and are in front of home.
             for (int displayNdx = getChildCount() - 1; displayNdx >= 0; --displayNdx) {
                 final DisplayContent display = getChildAt(displayNdx);
diff --git a/services/core/java/com/android/server/wm/SurfaceAnimationRunner.java b/services/core/java/com/android/server/wm/SurfaceAnimationRunner.java
index 1ae7816..b93dbce 100644
--- a/services/core/java/com/android/server/wm/SurfaceAnimationRunner.java
+++ b/services/core/java/com/android/server/wm/SurfaceAnimationRunner.java
@@ -62,6 +62,8 @@
  */
 class SurfaceAnimationRunner {
 
+    private static final String TAG = SurfaceAnimationRunner.class.getSimpleName();
+
     private final Object mLock = new Object();
 
     /**
@@ -185,6 +187,16 @@
                 // We must wait for t to be committed since otherwise the leash doesn't have the
                 // windows we want to screenshot and extend as children.
                 t.addTransactionCommittedListener(mEdgeExtensionExecutor, () -> {
+                    if (!animationLeash.isValid()) {
+                        Log.e(TAG, "Animation leash is not valid");
+                        synchronized (mEdgeExtensionLock) {
+                            mEdgeExtensions.remove(animationLeash);
+                        }
+                        synchronized (mLock) {
+                            mPreProcessingAnimations.remove(animationLeash);
+                        }
+                        return;
+                    }
                     final WindowAnimationSpec animationSpec = a.asWindowAnimationSpec();
 
                     final Transaction edgeExtensionCreationTransaction = new Transaction();
@@ -449,8 +461,7 @@
             // The leash we are trying to screenshot may have been removed by this point, which is
             // likely the reason for ending up with a null edgeBuffer, in which case we just want to
             // return and do nothing.
-            Log.e("SurfaceAnimationRunner", "Failed to create edge extension - "
-                    + "edge buffer is null");
+            Log.e(TAG, "Failed to create edge extension - edge buffer is null");
             return;
         }
 
diff --git a/services/core/java/com/android/server/wm/TaskFragment.java b/services/core/java/com/android/server/wm/TaskFragment.java
index d947168..15bc60c 100644
--- a/services/core/java/com/android/server/wm/TaskFragment.java
+++ b/services/core/java/com/android/server/wm/TaskFragment.java
@@ -1089,21 +1089,13 @@
             }
 
             final int otherWindowingMode = other.getWindowingMode();
-            if (otherWindowingMode == WINDOWING_MODE_FULLSCREEN) {
-                if (isTranslucent(other, starting)) {
-                    // Can be visible behind a translucent fullscreen TaskFragment.
-                    gotTranslucentFullscreen = true;
-                    continue;
-                }
-                return TASK_FRAGMENT_VISIBILITY_INVISIBLE;
-            } else if (otherWindowingMode == WINDOWING_MODE_MULTI_WINDOW
-                    && other.matchParentBounds()) {
+            if (otherWindowingMode == WINDOWING_MODE_FULLSCREEN
+                    || (otherWindowingMode != WINDOWING_MODE_PINNED && other.matchParentBounds())) {
                 if (isTranslucent(other, starting)) {
                     // Can be visible behind a translucent TaskFragment.
                     gotTranslucentFullscreen = true;
                     continue;
                 }
-                // Multi-window TaskFragment that matches parent bounds would occlude other children
                 return TASK_FRAGMENT_VISIBILITY_INVISIBLE;
             }
 
diff --git a/services/core/java/com/android/server/wm/WindowState.java b/services/core/java/com/android/server/wm/WindowState.java
index 2d21e71..95fea0e 100644
--- a/services/core/java/com/android/server/wm/WindowState.java
+++ b/services/core/java/com/android/server/wm/WindowState.java
@@ -2053,19 +2053,16 @@
 
     /**
      * Like isOnScreen(), but we don't return true if the window is part
-     * of a transition but has not yet started animating.
+     * of a transition that has not yet been started.
      */
     boolean isReadyForDisplay() {
-        if (!mHasSurface || mDestroying || !isVisibleByPolicy()) {
-            return false;
-        }
-        if (mToken.waitingToShow && getDisplayContent().mAppTransition.isTransitionSet()
-                && !isAnimating(TRANSITION | PARENTS, ANIMATION_TYPE_APP_TRANSITION)) {
+        if (mToken.waitingToShow && getDisplayContent().mAppTransition.isTransitionSet()) {
             return false;
         }
         final boolean parentAndClientVisible = !isParentWindowHidden()
                 && mViewVisibility == View.VISIBLE && mToken.isVisible();
-        return parentAndClientVisible || isAnimating(TRANSITION | PARENTS, ANIMATION_TYPE_ALL);
+        return mHasSurface && isVisibleByPolicy() && !mDestroying
+                && (parentAndClientVisible || isAnimating(TRANSITION | PARENTS));
     }
 
     boolean isFullyTransparent() {
diff --git a/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest2.java b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest2.java
index c786784..86d4655 100644
--- a/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest2.java
+++ b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest2.java
@@ -228,6 +228,15 @@
                 });
     }
 
+    public void testShortcutIdTruncated() {
+        ShortcutInfo si = new ShortcutInfo.Builder(getTestContext(),
+                "s".repeat(Short.MAX_VALUE)).build();
+
+        assertTrue(
+                "id must be truncated to MAX_ID_LENGTH",
+                si.getId().length() <= ShortcutInfo.MAX_ID_LENGTH);
+    }
+
     public void testShortcutInfoParcel() {
         setCaller(CALLING_PACKAGE_1, USER_10);
         ShortcutInfo si = parceled(new ShortcutInfo.Builder(mClientContext)
diff --git a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java
index 874846d..8f8b1c5 100755
--- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java
+++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java
@@ -128,6 +128,7 @@
 import android.app.PendingIntent;
 import android.app.Person;
 import android.app.RemoteInput;
+import android.app.RemoteInputHistoryItem;
 import android.app.StatsManager;
 import android.app.admin.DevicePolicyManagerInternal;
 import android.app.usage.UsageStatsManagerInternal;
@@ -5373,10 +5374,36 @@
     public void testVisitUris() throws Exception {
         final Uri audioContents = Uri.parse("content://com.example/audio");
         final Uri backgroundImage = Uri.parse("content://com.example/background");
+        final Icon personIcon1 = Icon.createWithContentUri("content://media/person1");
+        final Icon personIcon2 = Icon.createWithContentUri("content://media/person2");
+        final Icon personIcon3 = Icon.createWithContentUri("content://media/person3");
+        final Person person1 = new Person.Builder()
+                .setName("Messaging Person")
+                .setIcon(personIcon1)
+                .build();
+        final Person person2 = new Person.Builder()
+                .setName("People List Person 1")
+                .setIcon(personIcon2)
+                .build();
+        final Person person3 = new Person.Builder()
+                .setName("People List Person 2")
+                .setIcon(personIcon3)
+                .build();
+        final Uri historyUri1 = Uri.parse("content://com.example/history1");
+        final Uri historyUri2 = Uri.parse("content://com.example/history2");
+        final RemoteInputHistoryItem historyItem1 = new RemoteInputHistoryItem(null, historyUri1,
+                "a");
+        final RemoteInputHistoryItem historyItem2 = new RemoteInputHistoryItem(null, historyUri2,
+                "b");
 
         Bundle extras = new Bundle();
         extras.putParcelable(Notification.EXTRA_AUDIO_CONTENTS_URI, audioContents);
         extras.putString(Notification.EXTRA_BACKGROUND_IMAGE_URI, backgroundImage.toString());
+        extras.putParcelable(Notification.EXTRA_MESSAGING_PERSON, person1);
+        extras.putParcelableArrayList(Notification.EXTRA_PEOPLE_LIST,
+                new ArrayList<>(Arrays.asList(person2, person3)));
+        extras.putParcelableArray(Notification.EXTRA_REMOTE_INPUT_HISTORY_ITEMS,
+                new RemoteInputHistoryItem[]{historyItem1, historyItem2});
 
         Notification n = new Notification.Builder(mContext, "a")
                 .setContentTitle("notification with uris")
@@ -5388,6 +5415,34 @@
         n.visitUris(visitor);
         verify(visitor, times(1)).accept(eq(audioContents));
         verify(visitor, times(1)).accept(eq(backgroundImage));
+        verify(visitor, times(1)).accept(eq(personIcon1.getUri()));
+        verify(visitor, times(1)).accept(eq(personIcon2.getUri()));
+        verify(visitor, times(1)).accept(eq(personIcon3.getUri()));
+        verify(visitor, times(1)).accept(eq(historyUri1));
+        verify(visitor, times(1)).accept(eq(historyUri2));
+    }
+
+    @Test
+    public void testVisitUris_callStyle() {
+        Icon personIcon = Icon.createWithContentUri("content://media/person");
+        Icon verificationIcon = Icon.createWithContentUri("content://media/verification");
+        Person callingPerson = new Person.Builder().setName("Someone")
+                .setIcon(personIcon)
+                .build();
+        PendingIntent hangUpIntent = PendingIntent.getActivity(mContext, 0, new Intent(),
+                PendingIntent.FLAG_IMMUTABLE);
+        Notification n = new Notification.Builder(mContext, "a")
+                .setStyle(Notification.CallStyle.forOngoingCall(callingPerson, hangUpIntent)
+                        .setVerificationIcon(verificationIcon))
+                .setContentTitle("Calling...")
+                .setSmallIcon(android.R.drawable.sym_def_app_icon)
+                .build();
+
+        Consumer<Uri> visitor = (Consumer<Uri>) spy(Consumer.class);
+        n.visitUris(visitor);
+
+        verify(visitor, times(1)).accept(eq(personIcon.getUri()));
+        verify(visitor, times(1)).accept(eq(verificationIcon.getUri()));
     }
 
     @Test
diff --git a/services/tests/wmtests/OWNERS b/services/tests/wmtests/OWNERS
index 7a128fc..cece37f 100644
--- a/services/tests/wmtests/OWNERS
+++ b/services/tests/wmtests/OWNERS
@@ -1,3 +1,4 @@
+# Bug template url: https://b.corp.google.com/issues/new?component=316125&template=1018199
 include /services/core/java/com/android/server/wm/OWNERS
 
 # Voice Interaction
diff --git a/services/tests/wmtests/src/com/android/server/wm/LetterboxUiControllerTest.java b/services/tests/wmtests/src/com/android/server/wm/LetterboxUiControllerTest.java
index b277283..4ffd18a 100644
--- a/services/tests/wmtests/src/com/android/server/wm/LetterboxUiControllerTest.java
+++ b/services/tests/wmtests/src/com/android/server/wm/LetterboxUiControllerTest.java
@@ -39,9 +39,9 @@
 import static android.view.WindowManager.PROPERTY_CAMERA_COMPAT_ALLOW_REFRESH;
 import static android.view.WindowManager.PROPERTY_CAMERA_COMPAT_ENABLE_REFRESH_VIA_PAUSE;
 import static android.view.WindowManager.PROPERTY_COMPAT_ALLOW_DISPLAY_ORIENTATION_OVERRIDE;
+import static android.view.WindowManager.PROPERTY_COMPAT_ALLOW_IGNORING_ORIENTATION_REQUEST_WHEN_LOOP_DETECTED;
 import static android.view.WindowManager.PROPERTY_COMPAT_ALLOW_ORIENTATION_OVERRIDE;
 import static android.view.WindowManager.PROPERTY_COMPAT_ENABLE_FAKE_FOCUS;
-import static android.view.WindowManager.PROPERTY_COMPAT_IGNORE_ORIENTATION_REQUEST_WHEN_LOOP_DETECTED;
 import static android.view.WindowManager.PROPERTY_COMPAT_IGNORE_REQUESTED_ORIENTATION;
 
 import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
@@ -206,7 +206,7 @@
             throws Exception {
         doReturn(true).when(mLetterboxConfiguration)
                 .isPolicyForIgnoringRequestedOrientationEnabled();
-        mockThatProperty(PROPERTY_COMPAT_IGNORE_ORIENTATION_REQUEST_WHEN_LOOP_DETECTED,
+        mockThatProperty(PROPERTY_COMPAT_ALLOW_IGNORING_ORIENTATION_REQUEST_WHEN_LOOP_DETECTED,
                 /* value */ false);
         doReturn(false).when(mActivity).isLetterboxedForFixedOrientationAndAspectRatio();
 
diff --git a/tests/Input/OWNERS b/tests/Input/OWNERS
index d701f23..3cffce9 100644
--- a/tests/Input/OWNERS
+++ b/tests/Input/OWNERS
@@ -1 +1,2 @@
+# Bug component: 136048
 include /core/java/android/hardware/input/OWNERS
diff --git a/tests/StagedInstallTest/OWNERS b/tests/StagedInstallTest/OWNERS
index aac68e9..d7301dc 100644
--- a/tests/StagedInstallTest/OWNERS
+++ b/tests/StagedInstallTest/OWNERS
@@ -1,3 +1,5 @@
+# Bug component: 36137
+
 include /services/core/java/com/android/server/pm/OWNERS
 
 dariofreni@google.com