Merge "Initialize Display White Balance using SurfaceControl"
diff --git a/Android.bp b/Android.bp
index e19ca84..c6cf75d 100644
--- a/Android.bp
+++ b/Android.bp
@@ -49,8 +49,6 @@
         "rs/java/**/*.java",
 
         ":framework-javastream-protos",
-        // TODO: Resolve circular library dependency and remove media1-srcs
-        ":media1-srcs",
 
         "core/java/android/accessibilityservice/IAccessibilityServiceConnection.aidl",
         "core/java/android/accessibilityservice/IAccessibilityServiceClient.aidl",
@@ -249,6 +247,7 @@
         "core/java/android/os/ICancellationSignal.aidl",
         "core/java/android/os/IDeviceIdentifiersPolicyService.aidl",
         "core/java/android/os/IDeviceIdleController.aidl",
+        "core/java/android/os/IDynamicAndroidService.aidl",
         "core/java/android/os/IHardwarePropertiesManager.aidl",
         ":libincident_aidl",
         "core/java/android/os/IMaintenanceActivityListener.aidl",
@@ -505,7 +504,11 @@
         "media/java/android/media/session/ICallback.aidl",
         "media/java/android/media/session/IOnMediaKeyListener.aidl",
         "media/java/android/media/session/IOnVolumeKeyLongPressListener.aidl",
+        "media/java/android/media/session/ISession.aidl",
         "media/java/android/media/session/ISession2TokensListener.aidl",
+        "media/java/android/media/session/ISessionCallback.aidl",
+        "media/java/android/media/session/ISessionController.aidl",
+        "media/java/android/media/session/ISessionControllerCallback.aidl",
         "media/java/android/media/session/ISessionManager.aidl",
         "media/java/android/media/soundtrigger/ISoundTriggerDetectionService.aidl",
         "media/java/android/media/soundtrigger/ISoundTriggerDetectionServiceClient.aidl",
@@ -520,6 +523,8 @@
         "media/java/android/media/tv/ITvInputSessionCallback.aidl",
         "media/java/android/media/tv/ITvRemoteProvider.aidl",
         "media/java/android/media/tv/ITvRemoteServiceInput.aidl",
+        "media/java/android/service/media/IMediaBrowserService.aidl",
+        "media/java/android/service/media/IMediaBrowserServiceCallbacks.aidl",
         "telecomm/java/com/android/internal/telecom/ICallRedirectionAdapter.aidl",
         "telecomm/java/com/android/internal/telecom/ICallRedirectionService.aidl",
         "telecomm/java/com/android/internal/telecom/ICallScreeningAdapter.aidl",
@@ -654,6 +659,7 @@
 
         ":storaged_aidl",
         ":vold_aidl",
+        ":gsiservice_aidl",
         ":installd_aidl",
         ":dumpstate_aidl",
         ":incidentcompanion_aidl",
@@ -713,6 +719,7 @@
             "frameworks/native/aidl/gui",
             "system/core/storaged/binder",
             "system/vold/binder",
+            "system/gsid/aidl",
             "system/bt/binder",
             "system/security/keystore/binder",
         ],
diff --git a/Android.mk b/Android.mk
index e405345..65d4d24 100644
--- a/Android.mk
+++ b/Android.mk
@@ -78,6 +78,7 @@
 update-api: doc-comment-check-docs
 
 # ==== hiddenapi lists =======================================
+ifneq ($(UNSAFE_DISABLE_HIDDENAPI_FLAGS),true)
 .KATI_RESTAT: $(INTERNAL_PLATFORM_HIDDENAPI_FLAGS)
 $(INTERNAL_PLATFORM_HIDDENAPI_FLAGS): \
     PRIVATE_FLAGS_INPUTS := $(PRIVATE_FLAGS_INPUTS) $(SOONG_HIDDENAPI_FLAGS)
@@ -108,6 +109,7 @@
 
 $(call dist-for-goals,droidcore,$(INTERNAL_PLATFORM_HIDDENAPI_FLAGS))
 $(call dist-for-goals,droidcore,$(INTERNAL_PLATFORM_HIDDENAPI_GREYLIST_METADATA))
+endif  # UNSAFE_DISABLE_HIDDENAPI_FLAGS
 
 # Include subdirectory makefiles
 # ============================================================
diff --git a/api/current.txt b/api/current.txt
index 3b6cfe3..cb62ffc 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -1622,6 +1622,7 @@
     field @Deprecated public static final int yearListSelectorColor = 16843930; // 0x101049a
     field public static final int yesNoPreferenceStyle = 16842896; // 0x1010090
     field public static final int zAdjustment = 16843201; // 0x10101c1
+    field public static final int zygotePreloadName = 16844195; // 0x10105a3
   }
 
   public static final class R.bool {
@@ -6176,6 +6177,7 @@
     method public void onTrimMemory(int);
     method public boolean onUnbind(android.content.Intent);
     method public final void startForeground(int, android.app.Notification);
+    method public final void startForeground(int, @NonNull android.app.Notification, int);
     method public final void stopForeground(boolean);
     method public final void stopForeground(int);
     method public final void stopSelf();
@@ -6483,6 +6485,10 @@
     method public void onColorsChanged(android.app.WallpaperColors, int);
   }
 
+  public interface ZygotePreload {
+    method public void doPreload(android.content.pm.ApplicationInfo);
+  }
+
 }
 
 package android.app.admin {
@@ -11233,6 +11239,7 @@
 
   public class LauncherApps {
     method public java.util.List<android.content.pm.LauncherActivityInfo> getActivityList(String, android.os.UserHandle);
+    method @NonNull public java.util.List<android.content.pm.PackageInstaller.SessionInfo> getAllPackageInstallerSessions();
     method @Nullable public android.content.pm.LauncherApps.AppUsageLimit getAppUsageLimit(String, android.os.UserHandle);
     method public android.content.pm.ApplicationInfo getApplicationInfo(@NonNull String, int, @NonNull android.os.UserHandle) throws android.content.pm.PackageManager.NameNotFoundException;
     method public android.content.pm.LauncherApps.PinItemRequest getPinItemRequest(android.content.Intent);
@@ -11249,13 +11256,16 @@
     method public void pinShortcuts(@NonNull String, @NonNull java.util.List<java.lang.String>, @NonNull android.os.UserHandle);
     method public void registerCallback(android.content.pm.LauncherApps.Callback);
     method public void registerCallback(android.content.pm.LauncherApps.Callback, android.os.Handler);
+    method public void registerPackageInstallerSessionCallback(@NonNull java.util.concurrent.Executor, @NonNull android.content.pm.PackageInstaller.SessionCallback);
     method public android.content.pm.LauncherActivityInfo resolveActivity(android.content.Intent, android.os.UserHandle);
     method public boolean shouldHideFromSuggestions(@NonNull String, @NonNull android.os.UserHandle);
     method public void startAppDetailsActivity(android.content.ComponentName, android.os.UserHandle, android.graphics.Rect, android.os.Bundle);
     method public void startMainActivity(android.content.ComponentName, android.os.UserHandle, android.graphics.Rect, android.os.Bundle);
+    method public void startPackageInstallerSessionDetailsActivity(android.content.pm.PackageInstaller.SessionInfo, android.graphics.Rect, android.os.Bundle);
     method public void startShortcut(@NonNull String, @NonNull String, @Nullable android.graphics.Rect, @Nullable android.os.Bundle, @NonNull android.os.UserHandle);
     method public void startShortcut(@NonNull android.content.pm.ShortcutInfo, @Nullable android.graphics.Rect, @Nullable android.os.Bundle);
     method public void unregisterCallback(android.content.pm.LauncherApps.Callback);
+    method public void unregisterPackageInstallerSessionCallback(android.content.pm.PackageInstaller.SessionCallback);
     field public static final String ACTION_CONFIRM_PIN_APPWIDGET = "android.content.pm.action.CONFIRM_PIN_APPWIDGET";
     field public static final String ACTION_CONFIRM_PIN_SHORTCUT = "android.content.pm.action.CONFIRM_PIN_SHORTCUT";
     field public static final String EXTRA_PIN_ITEM_REQUEST = "android.content.pm.extra.PIN_ITEM_REQUEST";
@@ -11445,6 +11455,7 @@
     method public long getSize();
     method public int getStagedSessionErrorCode();
     method public String getStagedSessionErrorMessage();
+    method public android.os.UserHandle getUser();
     method public boolean isActive();
     method public boolean isMultiPackage();
     method public boolean isSealed();
@@ -11919,13 +11930,13 @@
     field public static final int FLAG_SINGLE_USER = 1073741824; // 0x40000000
     field public static final int FLAG_STOP_WITH_TASK = 1; // 0x1
     field public static final int FLAG_USE_APP_ZYGOTE = 8; // 0x8
-    field public static final int FOREGROUND_SERVICE_TYPE_DEVICE_COMPANION = 5; // 0x5
-    field public static final int FOREGROUND_SERVICE_TYPE_LOCATION = 4; // 0x4
-    field public static final int FOREGROUND_SERVICE_TYPE_MEDIA_PLAY = 2; // 0x2
-    field public static final int FOREGROUND_SERVICE_TYPE_ONGOING_PROCESS = 6; // 0x6
-    field public static final int FOREGROUND_SERVICE_TYPE_PHONE_CALL = 3; // 0x3
-    field public static final int FOREGROUND_SERVICE_TYPE_SYNC = 1; // 0x1
-    field public static final int FOREGROUND_SERVICE_TYPE_UNSPECIFIED = 0; // 0x0
+    field public static final int FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE = 16; // 0x10
+    field public static final int FOREGROUND_SERVICE_TYPE_DATA_SYNC = 1; // 0x1
+    field public static final int FOREGROUND_SERVICE_TYPE_LOCATION = 8; // 0x8
+    field public static final int FOREGROUND_SERVICE_TYPE_MANIFEST = -1; // 0xffffffff
+    field public static final int FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK = 2; // 0x2
+    field public static final int FOREGROUND_SERVICE_TYPE_NONE = 0; // 0x0
+    field public static final int FOREGROUND_SERVICE_TYPE_PHONE_CALL = 4; // 0x4
     field public int flags;
     field public String permission;
   }
@@ -13552,10 +13563,12 @@
     method public static android.graphics.Bitmap createScaledBitmap(@NonNull android.graphics.Bitmap, int, int, boolean);
     method public int describeContents();
     method public void eraseColor(@ColorInt int);
+    method public void eraseColor(@ColorLong long);
     method @CheckResult public android.graphics.Bitmap extractAlpha();
     method @CheckResult public android.graphics.Bitmap extractAlpha(android.graphics.Paint, int[]);
     method public int getAllocationByteCount();
     method public int getByteCount();
+    method public android.graphics.Color getColor(int, int);
     method @Nullable public android.graphics.ColorSpace getColorSpace();
     method public android.graphics.Bitmap.Config getConfig();
     method public int getDensity();
@@ -13581,6 +13594,7 @@
     method public void reconfigure(int, int, android.graphics.Bitmap.Config);
     method public void recycle();
     method public boolean sameAs(android.graphics.Bitmap);
+    method public void setColorSpace(@NonNull android.graphics.ColorSpace);
     method public void setConfig(android.graphics.Bitmap.Config);
     method public void setDensity(int);
     method public void setHasAlpha(boolean);
@@ -14241,7 +14255,9 @@
 
   public class LinearGradient extends android.graphics.Shader {
     ctor public LinearGradient(float, float, float, float, @NonNull @ColorInt int[], @Nullable float[], @NonNull android.graphics.Shader.TileMode);
+    ctor public LinearGradient(float, float, float, float, @NonNull @ColorLong long[], @Nullable float[], @NonNull android.graphics.Shader.TileMode);
     ctor public LinearGradient(float, float, float, float, @ColorInt int, @ColorInt int, @NonNull android.graphics.Shader.TileMode);
+    ctor public LinearGradient(float, float, float, float, @ColorLong long, @ColorLong long, @NonNull android.graphics.Shader.TileMode);
   }
 
   public class MaskFilter {
@@ -14382,6 +14398,7 @@
     method @Nullable public android.graphics.BlendMode getBlendMode();
     method @ColorInt public int getColor();
     method public android.graphics.ColorFilter getColorFilter();
+    method @ColorLong public long getColorLong();
     method public boolean getFillPath(android.graphics.Path, android.graphics.Path);
     method public int getFlags();
     method public String getFontFeatureSettings();
@@ -14402,6 +14419,7 @@
     method public float getRunAdvance(CharSequence, int, int, int, int, boolean, int);
     method public android.graphics.Shader getShader();
     method @ColorInt public int getShadowLayerColor();
+    method @ColorLong public long getShadowLayerColorLong();
     method public float getShadowLayerDx();
     method public float getShadowLayerDy();
     method public float getShadowLayerRadius();
@@ -14456,6 +14474,7 @@
     method public void setAntiAlias(boolean);
     method public void setBlendMode(@Nullable android.graphics.BlendMode);
     method public void setColor(@ColorInt int);
+    method public void setColor(@ColorLong long);
     method public android.graphics.ColorFilter setColorFilter(android.graphics.ColorFilter);
     method public void setDither(boolean);
     method public void setElegantTextHeight(boolean);
@@ -14472,6 +14491,7 @@
     method public android.graphics.PathEffect setPathEffect(android.graphics.PathEffect);
     method public android.graphics.Shader setShader(android.graphics.Shader);
     method public void setShadowLayer(float, float, float, @ColorInt int);
+    method public void setShadowLayer(float, float, float, @ColorLong long);
     method public void setStrikeThruText(boolean);
     method public void setStrokeCap(android.graphics.Paint.Cap);
     method public void setStrokeJoin(android.graphics.Paint.Join);
@@ -14766,7 +14786,9 @@
 
   public class RadialGradient extends android.graphics.Shader {
     ctor public RadialGradient(float, float, float, @NonNull @ColorInt int[], @Nullable float[], @NonNull android.graphics.Shader.TileMode);
+    ctor public RadialGradient(float, float, float, @NonNull @ColorLong long[], @Nullable float[], @NonNull android.graphics.Shader.TileMode);
     ctor public RadialGradient(float, float, float, @ColorInt int, @ColorInt int, @NonNull android.graphics.Shader.TileMode);
+    ctor public RadialGradient(float, float, float, @ColorLong long, @ColorLong long, @NonNull android.graphics.Shader.TileMode);
   }
 
   public final class RecordingCanvas extends android.graphics.Canvas {
@@ -15026,7 +15048,9 @@
 
   public class SweepGradient extends android.graphics.Shader {
     ctor public SweepGradient(float, float, @NonNull @ColorInt int[], @Nullable float[]);
+    ctor public SweepGradient(float, float, @NonNull @ColorLong long[], @Nullable float[]);
     ctor public SweepGradient(float, float, @ColorInt int, @ColorInt int);
+    ctor public SweepGradient(float, float, @ColorLong long, @ColorLong long);
   }
 
   public class Typeface {
@@ -24720,9 +24744,9 @@
     field public static final int KEY_TYPE_OFFLINE = 2; // 0x2
     field public static final int KEY_TYPE_RELEASE = 3; // 0x3
     field public static final int KEY_TYPE_STREAMING = 1; // 0x1
-    field public static final int OFFLINE_LICENSE_INACTIVE = 2; // 0x2
+    field public static final int OFFLINE_LICENSE_STATE_RELEASED = 2; // 0x2
     field public static final int OFFLINE_LICENSE_STATE_UNKNOWN = 0; // 0x0
-    field public static final int OFFLINE_LICENSE_USABLE = 1; // 0x1
+    field public static final int OFFLINE_LICENSE_STATE_USABLE = 1; // 0x1
     field public static final String PROPERTY_ALGORITHMS = "algorithms";
     field public static final String PROPERTY_DESCRIPTION = "description";
     field public static final String PROPERTY_DEVICE_UNIQUE_ID = "deviceUniqueId";
@@ -25483,7 +25507,7 @@
     method @Nullable public android.media.MediaTimestamp getTimestamp();
     method @NonNull public java.util.List<android.media.MediaPlayer2.TrackInfo> getTrackInfo();
     method @NonNull public java.util.List<android.media.MediaPlayer2.TrackInfo> getTrackInfo(@NonNull android.media.DataSourceDesc);
-    method public android.media.VideoSize getVideoSize();
+    method public android.util.Size getVideoSize();
     method public boolean isLooping();
     method public Object loopCurrent(boolean);
     method public Object notifyWhenCommandLabelReached(@NonNull Object);
@@ -25575,6 +25599,14 @@
     field public static final int PLAYER_STATE_PAUSED = 1003; // 0x3eb
     field public static final int PLAYER_STATE_PLAYING = 1004; // 0x3ec
     field public static final int PLAYER_STATE_PREPARED = 1002; // 0x3ea
+    field public static final int PREPARE_DRM_STATUS_KEY_EXCHANGE_ERROR = 7; // 0x7
+    field public static final int PREPARE_DRM_STATUS_PREPARATION_ERROR = 3; // 0x3
+    field public static final int PREPARE_DRM_STATUS_PROVISIONING_NETWORK_ERROR = 1; // 0x1
+    field public static final int PREPARE_DRM_STATUS_PROVISIONING_SERVER_ERROR = 2; // 0x2
+    field public static final int PREPARE_DRM_STATUS_RESOURCE_BUSY = 5; // 0x5
+    field public static final int PREPARE_DRM_STATUS_RESTORE_ERROR = 6; // 0x6
+    field public static final int PREPARE_DRM_STATUS_SUCCESS = 0; // 0x0
+    field public static final int PREPARE_DRM_STATUS_UNSUPPORTED_SCHEME = 4; // 0x4
     field public static final int SEEK_CLOSEST = 3; // 0x3
     field public static final int SEEK_CLOSEST_SYNC = 2; // 0x2
     field public static final int SEEK_NEXT_SYNC = 1; // 0x1
@@ -25617,7 +25649,7 @@
     method public void onMediaTimeDiscontinuity(@NonNull android.media.MediaPlayer2, @NonNull android.media.DataSourceDesc, @NonNull android.media.MediaTimestamp);
     method public void onSubtitleData(@NonNull android.media.MediaPlayer2, @NonNull android.media.DataSourceDesc, @NonNull android.media.SubtitleData);
     method public void onTimedMetaDataAvailable(@NonNull android.media.MediaPlayer2, @NonNull android.media.DataSourceDesc, @NonNull android.media.TimedMetaData);
-    method public void onVideoSizeChanged(@NonNull android.media.MediaPlayer2, @NonNull android.media.DataSourceDesc, @NonNull android.media.VideoSize);
+    method public void onVideoSizeChanged(@NonNull android.media.MediaPlayer2, @NonNull android.media.DataSourceDesc, @NonNull android.util.Size);
   }
 
   public static final class MediaPlayer2.MetricsConstants {
@@ -26522,11 +26554,6 @@
     method @NonNull public android.media.UriDataSourceDesc.Builder setDataSource(@NonNull android.content.Context, @NonNull android.net.Uri, @Nullable java.util.Map<java.lang.String,java.lang.String>, @Nullable java.util.List<java.net.HttpCookie>);
   }
 
-  public final class VideoSize {
-    method public int getHeight();
-    method public int getWidth();
-  }
-
   public interface VolumeAutomation {
     method @NonNull public android.media.VolumeShaper createVolumeShaper(@NonNull android.media.VolumeShaper.Configuration);
   }
@@ -27366,7 +27393,6 @@
     method public int getRatingType();
     method @Nullable public android.app.PendingIntent getSessionActivity();
     method @NonNull public android.media.session.MediaSession.Token getSessionToken();
-    method public String getTag();
     method @NonNull public android.media.session.MediaController.TransportControls getTransportControls();
     method public void registerCallback(@NonNull android.media.session.MediaController.Callback);
     method public void registerCallback(@NonNull android.media.session.MediaController.Callback, @Nullable android.os.Handler);
@@ -29261,6 +29287,7 @@
     method public android.net.VpnService.Builder setBlocking(boolean);
     method public android.net.VpnService.Builder setConfigureIntent(android.app.PendingIntent);
     method public android.net.VpnService.Builder setHttpProxy(android.net.ProxyInfo);
+    method public android.net.VpnService.Builder setMetered(boolean);
     method public android.net.VpnService.Builder setMtu(int);
     method public android.net.VpnService.Builder setSession(String);
     method public android.net.VpnService.Builder setUnderlyingNetworks(android.net.Network[]);
@@ -38490,13 +38517,13 @@
     field public static final String BUCKET_ID = "bucket_id";
     field public static final String DATE_TAKEN = "datetaken";
     field public static final String DESCRIPTION = "description";
+    field public static final String GROUP_ID = "group_id";
     field public static final String IS_PRIVATE = "isprivate";
     field @Deprecated public static final String LATITUDE = "latitude";
     field @Deprecated public static final String LONGITUDE = "longitude";
     field @Deprecated public static final String MINI_THUMB_MAGIC = "mini_thumb_magic";
     field public static final String ORIENTATION = "orientation";
     field @Deprecated public static final String PICASA_ID = "picasa_id";
-    field public static final String SECONDARY_BUCKET_ID = "secondary_bucket_id";
   }
 
   public static final class MediaStore.Images.Media implements android.provider.MediaStore.Images.ImageColumns {
@@ -38550,6 +38577,8 @@
     field public static final String IS_TRASHED = "is_trashed";
     field public static final String MIME_TYPE = "mime_type";
     field public static final String OWNER_PACKAGE_NAME = "owner_package_name";
+    field public static final String PRIMARY_DIRECTORY = "primary_directory";
+    field public static final String SECONDARY_DIRECTORY = "secondary_directory";
     field public static final String SIZE = "_size";
     field public static final String TITLE = "title";
     field public static final String WIDTH = "width";
@@ -38617,13 +38646,13 @@
     field public static final String DATE_TAKEN = "datetaken";
     field public static final String DESCRIPTION = "description";
     field public static final String DURATION = "duration";
+    field public static final String GROUP_ID = "group_id";
     field public static final String IS_PRIVATE = "isprivate";
     field public static final String LANGUAGE = "language";
     field @Deprecated public static final String LATITUDE = "latitude";
     field @Deprecated public static final String LONGITUDE = "longitude";
     field @Deprecated public static final String MINI_THUMB_MAGIC = "mini_thumb_magic";
     field public static final String RESOLUTION = "resolution";
-    field public static final String SECONDARY_BUCKET_ID = "secondary_bucket_id";
     field public static final String TAGS = "tags";
   }
 
@@ -38847,6 +38876,7 @@
     field @Deprecated public static final int LOCATION_MODE_BATTERY_SAVING = 2; // 0x2
     field @Deprecated public static final int LOCATION_MODE_HIGH_ACCURACY = 3; // 0x3
     field @Deprecated public static final int LOCATION_MODE_OFF = 0; // 0x0
+    field @Deprecated public static final int LOCATION_MODE_ON = 3; // 0x3
     field @Deprecated public static final int LOCATION_MODE_SENSORS_ONLY = 1; // 0x1
     field @Deprecated public static final String LOCATION_PROVIDERS_ALLOWED = "location_providers_allowed";
     field @Deprecated public static final String LOCK_PATTERN_ENABLED = "lock_pattern_autolock";
@@ -41871,10 +41901,6 @@
     method public void showSession(android.os.Bundle, int);
     field public static final String SERVICE_INTERFACE = "android.service.voice.VoiceInteractionService";
     field public static final String SERVICE_META_DATA = "android.voice_interaction";
-    field public static final int VOICE_STATE_CONDITIONAL_LISTENING = 1; // 0x1
-    field public static final int VOICE_STATE_FULFILLING = 3; // 0x3
-    field public static final int VOICE_STATE_LISTENING = 2; // 0x2
-    field public static final int VOICE_STATE_NONE = 0; // 0x0
   }
 
   public class VoiceInteractionSession implements android.content.ComponentCallbacks2 android.view.KeyEvent.Callback {
@@ -49784,6 +49810,7 @@
     field public static final int EDGE_RIGHT = 8; // 0x8
     field public static final int EDGE_TOP = 1; // 0x1
     field public static final int FLAG_WINDOW_IS_OBSCURED = 1; // 0x1
+    field public static final int FLAG_WINDOW_IS_PARTIALLY_OBSCURED = 2; // 0x2
     field public static final int INVALID_POINTER_ID = -1; // 0xffffffff
     field public static final int TOOL_TYPE_ERASER = 4; // 0x4
     field public static final int TOOL_TYPE_FINGER = 1; // 0x1
@@ -50336,6 +50363,7 @@
     method public final int getScrollX();
     method public final int getScrollY();
     method @android.view.ViewDebug.ExportedProperty(category="drawing") @ColorInt public int getSolidColor();
+    method @LayoutRes public int getSourceLayoutResId();
     method public android.animation.StateListAnimator getStateListAnimator();
     method protected int getSuggestedMinimumHeight();
     method protected int getSuggestedMinimumWidth();
@@ -56014,8 +56042,7 @@
     method public void show(@FloatRange(from=0) float, @FloatRange(from=0) float, float, float);
     method public void update();
     field public static final int SOURCE_BOUND_MAX_IN_SURFACE = 0; // 0x0
-    field public static final int SOURCE_BOUND_MAX_IN_VIEW = 1; // 0x1
-    field public static final int SOURCE_BOUND_MAX_VISIBLE = 2; // 0x2
+    field public static final int SOURCE_BOUND_MAX_VISIBLE = 1; // 0x1
   }
 
   public static class Magnifier.Builder {
diff --git a/api/system-current.txt b/api/system-current.txt
index 1fd8495..43d055b 100644
--- a/api/system-current.txt
+++ b/api/system-current.txt
@@ -122,6 +122,7 @@
     field public static final String OBSERVE_ROLE_HOLDERS = "android.permission.OBSERVE_ROLE_HOLDERS";
     field public static final String OVERRIDE_WIFI_CONFIG = "android.permission.OVERRIDE_WIFI_CONFIG";
     field public static final String PACKAGE_VERIFICATION_AGENT = "android.permission.PACKAGE_VERIFICATION_AGENT";
+    field public static final String PACKET_KEEPALIVE_OFFLOAD = "android.permission.PACKET_KEEPALIVE_OFFLOAD";
     field public static final String PEERS_MAC_ADDRESS = "android.permission.PEERS_MAC_ADDRESS";
     field public static final String PERFORM_CDMA_PROVISIONING = "android.permission.PERFORM_CDMA_PROVISIONING";
     field public static final String PERFORM_SIM_ACTIVATION = "android.permission.PERFORM_SIM_ACTIVATION";
@@ -990,6 +991,7 @@
   }
 
   public static final class AppPredictionContext.Builder {
+    ctor public AppPredictionContext.Builder(@NonNull android.content.Context);
     method public android.app.prediction.AppPredictionContext build();
     method public android.app.prediction.AppPredictionContext.Builder setExtras(@Nullable android.os.Bundle);
     method public android.app.prediction.AppPredictionContext.Builder setPredictedTargetCount(int);
@@ -1021,6 +1023,8 @@
   }
 
   public final class AppTarget implements android.os.Parcelable {
+    ctor public AppTarget(@NonNull android.app.prediction.AppTargetId, @NonNull String, @Nullable String, @NonNull android.os.UserHandle);
+    ctor public AppTarget(@NonNull android.app.prediction.AppTargetId, @NonNull android.content.pm.ShortcutInfo, @Nullable String);
     method public int describeContents();
     method @Nullable public String getClassName();
     method @NonNull public android.app.prediction.AppTargetId getId();
@@ -1051,6 +1055,7 @@
   }
 
   public final class AppTargetId implements android.os.Parcelable {
+    ctor public AppTargetId(@NonNull String);
     method public int describeContents();
     method public void writeToParcel(android.os.Parcel, int);
     field public static final android.os.Parcelable.Creator<android.app.prediction.AppTargetId> CREATOR;
@@ -1286,6 +1291,7 @@
     field public static final String BACKUP_SERVICE = "backup";
     field public static final String CONTENT_SUGGESTIONS_SERVICE = "content_suggestions";
     field public static final String CONTEXTHUB_SERVICE = "contexthub";
+    field public static final String DYNAMIC_ANDROID_SERVICE = "dynamic_android";
     field public static final String EUICC_CARD_SERVICE = "euicc_card";
     field public static final String HDMI_CONTROL_SERVICE = "hdmi_control";
     field public static final String NETD_SERVICE = "netd";
@@ -3478,15 +3484,6 @@
     method @NonNull public android.media.TimedMetaData.Builder setTimedMetaData(long, @NonNull byte[]);
   }
 
-  public abstract class VolumeProvider {
-    method public void setCallback(android.media.VolumeProvider.Callback);
-  }
-
-  public abstract static class VolumeProvider.Callback {
-    ctor public VolumeProvider.Callback();
-    method public abstract void onVolumeChanged(android.media.VolumeProvider);
-  }
-
 }
 
 package android.media.audiopolicy {
@@ -3575,167 +3572,10 @@
 
 package android.media.session {
 
-  public final class ControllerCallbackLink implements android.os.Parcelable {
-    ctor public ControllerCallbackLink(@NonNull android.content.Context, @NonNull android.media.session.ControllerCallbackLink.CallbackStub);
-    ctor public ControllerCallbackLink(android.os.IBinder);
-    method public int describeContents();
-    method @NonNull public android.os.IBinder getBinder();
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifyEvent(@NonNull String, @Nullable android.os.Bundle);
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifyExtrasChanged(@Nullable android.os.Bundle);
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifyMetadataChanged(@Nullable android.media.MediaMetadata);
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifyPlaybackStateChanged(@Nullable android.media.session.PlaybackState);
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifyQueueChanged(@Nullable java.util.List<android.media.session.MediaSession.QueueItem>);
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifyQueueTitleChanged(@Nullable CharSequence);
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifySessionDestroyed();
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifyVolumeInfoChanged(@NonNull android.media.session.MediaController.PlaybackInfo);
-    method public void writeToParcel(android.os.Parcel, int);
-    field public static final android.os.Parcelable.Creator<android.media.session.ControllerCallbackLink> CREATOR;
-  }
-
-  public abstract static class ControllerCallbackLink.CallbackStub {
-    ctor public ControllerCallbackLink.CallbackStub();
-    method public void onEvent(@NonNull String, @Nullable android.os.Bundle);
-    method public void onExtrasChanged(@Nullable android.os.Bundle);
-    method public void onMetadataChanged(@Nullable android.media.MediaMetadata);
-    method public void onPlaybackStateChanged(@Nullable android.media.session.PlaybackState);
-    method public void onQueueChanged(@Nullable java.util.List<android.media.session.MediaSession.QueueItem>);
-    method public void onQueueTitleChanged(@Nullable CharSequence);
-    method public void onSessionDestroyed();
-    method public void onVolumeInfoChanged(@NonNull android.media.session.MediaController.PlaybackInfo);
-  }
-
-  public final class ControllerLink implements android.os.Parcelable {
-    ctor public ControllerLink(@NonNull android.media.session.ControllerLink.ControllerStub);
-    ctor public ControllerLink(android.os.IBinder);
-    method public int describeContents();
-    method @NonNull public android.os.IBinder getBinder();
-    method @Nullable public android.os.Bundle getExtras();
-    method @Nullable public android.media.MediaMetadata getMetadata();
-    method @Nullable public android.media.session.PlaybackState getPlaybackState();
-    method @Nullable public java.util.List<android.media.session.MediaSession.QueueItem> getQueue();
-    method @Nullable public CharSequence getQueueTitle();
-    method public int getRatingType();
-    method public void writeToParcel(android.os.Parcel, int);
-    field public static final android.os.Parcelable.Creator<android.media.session.ControllerLink> CREATOR;
-  }
-
-  public abstract static class ControllerLink.ControllerStub {
-    ctor public ControllerLink.ControllerStub();
-    method public void adjustVolume(@NonNull String, @NonNull String, @NonNull android.media.session.ControllerCallbackLink, int, int);
-    method public void fastForward(@NonNull String, @NonNull android.media.session.ControllerCallbackLink);
-    method @Nullable public android.os.Bundle getExtras();
-    method public long getFlags();
-    method @Nullable public android.app.PendingIntent getLaunchPendingIntent();
-    method @Nullable public android.media.MediaMetadata getMetadata();
-    method @NonNull public String getPackageName();
-    method @Nullable public android.media.session.PlaybackState getPlaybackState();
-    method @Nullable public java.util.List<android.media.session.MediaSession.QueueItem> getQueue();
-    method @Nullable public CharSequence getQueueTitle();
-    method public int getRatingType();
-    method @NonNull public String getTag();
-    method @NonNull public android.media.session.MediaController.PlaybackInfo getVolumeAttributes();
-    method public void next(@NonNull String, @NonNull android.media.session.ControllerCallbackLink);
-    method public void pause(@NonNull String, @NonNull android.media.session.ControllerCallbackLink);
-    method public void play(@NonNull String, @NonNull android.media.session.ControllerCallbackLink);
-    method public void playFromMediaId(@NonNull String, @NonNull android.media.session.ControllerCallbackLink, @NonNull String, @Nullable android.os.Bundle);
-    method public void playFromSearch(@NonNull String, @NonNull android.media.session.ControllerCallbackLink, @NonNull String, @Nullable android.os.Bundle);
-    method public void playFromUri(@NonNull String, @NonNull android.media.session.ControllerCallbackLink, @NonNull android.net.Uri, @Nullable android.os.Bundle);
-    method public void prepare(@NonNull String, @NonNull android.media.session.ControllerCallbackLink);
-    method public void prepareFromMediaId(@NonNull String, @NonNull android.media.session.ControllerCallbackLink, @NonNull String, @Nullable android.os.Bundle);
-    method public void prepareFromSearch(@NonNull String, @NonNull android.media.session.ControllerCallbackLink, @NonNull String, @Nullable android.os.Bundle);
-    method public void prepareFromUri(@NonNull String, @NonNull android.media.session.ControllerCallbackLink, @NonNull android.net.Uri, @Nullable android.os.Bundle);
-    method public void previous(@NonNull String, @NonNull android.media.session.ControllerCallbackLink);
-    method public void rate(@NonNull String, @NonNull android.media.session.ControllerCallbackLink, @NonNull android.media.Rating);
-    method public void registerCallback(@NonNull String, @NonNull android.media.session.ControllerCallbackLink);
-    method public void rewind(@NonNull String, @NonNull android.media.session.ControllerCallbackLink);
-    method public void seekTo(@NonNull String, @NonNull android.media.session.ControllerCallbackLink, long);
-    method public void sendCommand(@NonNull String, @NonNull android.media.session.ControllerCallbackLink, @NonNull String, @Nullable android.os.Bundle, @Nullable android.os.ResultReceiver);
-    method public void sendCustomAction(@NonNull String, @NonNull android.media.session.ControllerCallbackLink, @NonNull String, @Nullable android.os.Bundle);
-    method public boolean sendMediaButton(@NonNull String, @NonNull android.media.session.ControllerCallbackLink, @NonNull android.view.KeyEvent);
-    method public void setVolumeTo(@NonNull String, @NonNull String, @NonNull android.media.session.ControllerCallbackLink, int, int);
-    method public void skipToQueueItem(@NonNull String, @NonNull android.media.session.ControllerCallbackLink, long);
-    method public void stop(@NonNull String, @NonNull android.media.session.ControllerCallbackLink);
-    method public void unregisterCallback(@NonNull android.media.session.ControllerCallbackLink);
-  }
-
   public static final class MediaController.PlaybackInfo implements android.os.Parcelable {
     ctor public MediaController.PlaybackInfo(int, int, int, int, android.media.AudioAttributes);
   }
 
-  public abstract static class MediaSession.Callback {
-    method public void onSetMediaButtonEventDelegate(@NonNull android.media.session.MediaSessionEngine.MediaButtonEventDelegate);
-  }
-
-  public static final class MediaSession.Token implements android.os.Parcelable {
-    method public android.media.session.ControllerLink getControllerLink();
-  }
-
-  public final class MediaSessionEngine implements java.lang.AutoCloseable {
-    ctor public MediaSessionEngine(@NonNull android.content.Context, @NonNull android.media.session.SessionLink, @NonNull android.media.session.SessionCallbackLink, @NonNull android.media.session.MediaSessionEngine.CallbackStub);
-    method public void close();
-    method public String getCallingPackage();
-    method @NonNull public android.media.session.MediaController getController();
-    method @NonNull public android.media.session.MediaSessionManager.RemoteUserInfo getCurrentControllerInfo();
-    method @NonNull public android.media.session.MediaSession.Token getSessionToken();
-    method public boolean isActive();
-    method public static boolean isActiveState(int);
-    method public void sendSessionEvent(@NonNull String, @Nullable android.os.Bundle);
-    method public void setActive(boolean);
-    method public void setCallback(@Nullable android.media.session.MediaSession.Callback);
-    method public void setCallback(@Nullable android.media.session.MediaSession.Callback, @NonNull android.os.Handler);
-    method public void setExtras(@Nullable android.os.Bundle);
-    method public void setFlags(int);
-    method public void setMediaButtonReceiver(@Nullable android.app.PendingIntent);
-    method public void setMetadata(@Nullable android.media.MediaMetadata);
-    method public void setPlaybackState(@Nullable android.media.session.PlaybackState);
-    method public void setPlaybackToLocal(android.media.AudioAttributes);
-    method public void setPlaybackToRemote(@NonNull android.media.VolumeProvider);
-    method public void setQueue(@Nullable java.util.List<android.media.session.MediaSession.QueueItem>);
-    method public void setQueueTitle(@Nullable CharSequence);
-    method public void setRatingType(int);
-    method public void setSessionActivity(@Nullable android.app.PendingIntent);
-  }
-
-  public static final class MediaSessionEngine.CallbackStub {
-    ctor public MediaSessionEngine.CallbackStub();
-    method public void onAdjustVolume(String, int, int, android.media.session.ControllerCallbackLink, int);
-    method public void onCommand(String, int, int, android.media.session.ControllerCallbackLink, String, android.os.Bundle, android.os.ResultReceiver);
-    method public void onCustomAction(String, int, int, android.media.session.ControllerCallbackLink, String, android.os.Bundle);
-    method public void onFastForward(String, int, int, android.media.session.ControllerCallbackLink);
-    method public void onMediaButton(String, int, int, android.content.Intent, int, android.os.ResultReceiver);
-    method public void onMediaButtonFromController(String, int, int, android.media.session.ControllerCallbackLink, android.content.Intent);
-    method public void onNext(String, int, int, android.media.session.ControllerCallbackLink);
-    method public void onPause(String, int, int, android.media.session.ControllerCallbackLink);
-    method public void onPlay(String, int, int, android.media.session.ControllerCallbackLink);
-    method public void onPlayFromMediaId(String, int, int, android.media.session.ControllerCallbackLink, String, android.os.Bundle);
-    method public void onPlayFromSearch(String, int, int, android.media.session.ControllerCallbackLink, String, android.os.Bundle);
-    method public void onPlayFromUri(String, int, int, android.media.session.ControllerCallbackLink, android.net.Uri, android.os.Bundle);
-    method public void onPrepare(String, int, int, android.media.session.ControllerCallbackLink);
-    method public void onPrepareFromMediaId(String, int, int, android.media.session.ControllerCallbackLink, String, android.os.Bundle);
-    method public void onPrepareFromSearch(String, int, int, android.media.session.ControllerCallbackLink, String, android.os.Bundle);
-    method public void onPrepareFromUri(String, int, int, android.media.session.ControllerCallbackLink, android.net.Uri, android.os.Bundle);
-    method public void onPrevious(String, int, int, android.media.session.ControllerCallbackLink);
-    method public void onRate(String, int, int, android.media.session.ControllerCallbackLink, android.media.Rating);
-    method public void onRewind(String, int, int, android.media.session.ControllerCallbackLink);
-    method public void onSeekTo(String, int, int, android.media.session.ControllerCallbackLink, long);
-    method public void onSetVolumeTo(String, int, int, android.media.session.ControllerCallbackLink, int);
-    method public void onSkipToTrack(String, int, int, android.media.session.ControllerCallbackLink, long);
-    method public void onStop(String, int, int, android.media.session.ControllerCallbackLink);
-  }
-
-  public static interface MediaSessionEngine.MediaButtonEventDelegate {
-    method public boolean onMediaButtonIntent(android.content.Intent);
-  }
-
-  public static final class MediaSessionEngine.QueueItem {
-    ctor public MediaSessionEngine.QueueItem(android.media.MediaDescription, long);
-    ctor public MediaSessionEngine.QueueItem(android.os.Parcel);
-    method public android.media.MediaDescription getDescription();
-    method public long getQueueId();
-    method public void writeToParcel(android.os.Parcel, int);
-    field public static final int UNKNOWN_ID = -1; // 0xffffffff
-  }
-
   public final class MediaSessionManager {
     method @RequiresPermission(android.Manifest.permission.SET_MEDIA_KEY_LISTENER) public void setOnMediaKeyListener(android.media.session.MediaSessionManager.OnMediaKeyListener, @Nullable android.os.Handler);
     method @RequiresPermission(android.Manifest.permission.SET_VOLUME_KEY_LONG_PRESS_LISTENER) public void setOnVolumeKeyLongPressListener(android.media.session.MediaSessionManager.OnVolumeKeyLongPressListener, @Nullable android.os.Handler);
@@ -3749,66 +3589,6 @@
     method public void onVolumeKeyLongPress(android.view.KeyEvent);
   }
 
-  public final class SessionCallbackLink implements android.os.Parcelable {
-    ctor public SessionCallbackLink(android.os.IBinder);
-    method public int describeContents();
-    method @NonNull public android.os.IBinder getBinder();
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifyAdjustVolume(@NonNull String, int, int, @NonNull android.media.session.ControllerCallbackLink, int);
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifyCommand(@NonNull String, int, int, @NonNull android.media.session.ControllerCallbackLink, @NonNull String, @Nullable android.os.Bundle, @Nullable android.os.ResultReceiver);
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifyCustomAction(@NonNull String, int, int, @NonNull android.media.session.ControllerCallbackLink, @NonNull String, @Nullable android.os.Bundle);
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifyFastForward(@NonNull String, int, int, @NonNull android.media.session.ControllerCallbackLink);
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifyMediaButton(@NonNull String, int, int, @NonNull android.content.Intent, int, @Nullable android.os.ResultReceiver);
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifyMediaButtonFromController(@NonNull String, int, int, @NonNull android.media.session.ControllerCallbackLink, @NonNull android.content.Intent);
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifyNext(@NonNull String, int, int, @NonNull android.media.session.ControllerCallbackLink);
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifyPause(@NonNull String, int, int, @NonNull android.media.session.ControllerCallbackLink);
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifyPlay(@NonNull String, int, int, @NonNull android.media.session.ControllerCallbackLink);
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifyPlayFromMediaId(@NonNull String, int, int, @NonNull android.media.session.ControllerCallbackLink, @NonNull String, @Nullable android.os.Bundle);
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifyPlayFromSearch(@NonNull String, int, int, @NonNull android.media.session.ControllerCallbackLink, @NonNull String, @Nullable android.os.Bundle);
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifyPlayFromUri(@NonNull String, int, int, @NonNull android.media.session.ControllerCallbackLink, @NonNull android.net.Uri, @Nullable android.os.Bundle);
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifyPrepare(@NonNull String, int, int, @NonNull android.media.session.ControllerCallbackLink);
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifyPrepareFromMediaId(@NonNull String, int, int, @NonNull android.media.session.ControllerCallbackLink, @NonNull String, @Nullable android.os.Bundle);
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifyPrepareFromSearch(@NonNull String, int, int, @NonNull android.media.session.ControllerCallbackLink, @NonNull String, @Nullable android.os.Bundle);
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifyPrepareFromUri(@NonNull String, int, int, @NonNull android.media.session.ControllerCallbackLink, @NonNull android.net.Uri, @Nullable android.os.Bundle);
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifyPrevious(@NonNull String, int, int, @NonNull android.media.session.ControllerCallbackLink);
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifyRate(@NonNull String, int, int, @NonNull android.media.session.ControllerCallbackLink, @NonNull android.media.Rating);
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifyRewind(@NonNull String, int, int, @NonNull android.media.session.ControllerCallbackLink);
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifySeekTo(@NonNull String, int, int, @NonNull android.media.session.ControllerCallbackLink, long);
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifySetVolumeTo(@NonNull String, int, int, @NonNull android.media.session.ControllerCallbackLink, int);
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifySkipToTrack(@NonNull String, int, int, @NonNull android.media.session.ControllerCallbackLink, long);
-    method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void notifyStop(@NonNull String, int, int, @NonNull android.media.session.ControllerCallbackLink);
-    method public void writeToParcel(android.os.Parcel, int);
-    field public static final android.os.Parcelable.Creator<android.media.session.SessionCallbackLink> CREATOR;
-  }
-
-  public final class SessionLink implements android.os.Parcelable {
-    ctor public SessionLink(@NonNull android.media.session.SessionLink.SessionStub);
-    ctor public SessionLink(android.os.IBinder);
-    method public int describeContents();
-    method @NonNull public android.os.IBinder getBinder();
-    method public void writeToParcel(android.os.Parcel, int);
-    field public static final android.os.Parcelable.Creator<android.media.session.SessionLink> CREATOR;
-  }
-
-  public abstract static class SessionLink.SessionStub {
-    ctor public SessionLink.SessionStub();
-    method public void destroySession();
-    method @NonNull public android.media.session.ControllerLink getController();
-    method public void sendEvent(@NonNull String, @Nullable android.os.Bundle);
-    method public void setActive(boolean);
-    method public void setCurrentVolume(int);
-    method public void setExtras(@Nullable android.os.Bundle);
-    method public void setFlags(int);
-    method public void setLaunchPendingIntent(@Nullable android.app.PendingIntent);
-    method public void setMediaButtonReceiver(@Nullable android.app.PendingIntent);
-    method public void setMetadata(@Nullable android.media.MediaMetadata, long, @Nullable String);
-    method public void setPlaybackState(@Nullable android.media.session.PlaybackState);
-    method public void setPlaybackToLocal(@NonNull android.media.AudioAttributes);
-    method public void setPlaybackToRemote(int, int);
-    method public void setQueue(@Nullable java.util.List<android.media.session.MediaSession.QueueItem>);
-    method public void setQueueTitle(@Nullable CharSequence);
-    method public void setRatingType(int);
-  }
-
 }
 
 package android.media.soundtrigger {
@@ -4110,9 +3890,10 @@
   }
 
   public class ConnectivityManager {
-    method @RequiresPermission("android.permission.PACKET_KEEPALIVE_OFFLOAD") public android.net.SocketKeepalive createNattKeepalive(@NonNull android.net.Network, @NonNull java.io.FileDescriptor, @NonNull java.net.InetAddress, @NonNull java.net.InetAddress, @NonNull java.util.concurrent.Executor, @NonNull android.net.SocketKeepalive.Callback);
+    method @RequiresPermission(android.Manifest.permission.PACKET_KEEPALIVE_OFFLOAD) public android.net.SocketKeepalive createNattKeepalive(@NonNull android.net.Network, @NonNull java.io.FileDescriptor, @NonNull java.net.InetAddress, @NonNull java.net.InetAddress, @NonNull java.util.concurrent.Executor, @NonNull android.net.SocketKeepalive.Callback);
     method public boolean getAvoidBadWifi();
     method @RequiresPermission(android.Manifest.permission.LOCAL_MAC_ADDRESS) public String getCaptivePortalServerUrl();
+    method @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) public void getLatestTetheringEntitlementValue(int, boolean, @NonNull android.net.ConnectivityManager.TetheringEntitlementValueListener, @Nullable android.os.Handler);
     method @RequiresPermission(anyOf={android.Manifest.permission.TETHER_PRIVILEGED, android.Manifest.permission.WRITE_SETTINGS}) public boolean isTetheringSupported();
     method @RequiresPermission(anyOf={"android.permission.NETWORK_SETTINGS", android.Manifest.permission.NETWORK_SETUP_WIZARD, "android.permission.NETWORK_STACK"}) public void setAirplaneMode(boolean);
     method @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) public void startTethering(int, boolean, android.net.ConnectivityManager.OnStartTetheringCallback);
@@ -4123,6 +3904,9 @@
     field public static final int TETHERING_BLUETOOTH = 2; // 0x2
     field public static final int TETHERING_USB = 1; // 0x1
     field public static final int TETHERING_WIFI = 0; // 0x0
+    field public static final int TETHER_ERROR_ENTITLEMENT_UNKONWN = 13; // 0xd
+    field public static final int TETHER_ERROR_NO_ERROR = 0; // 0x0
+    field public static final int TETHER_ERROR_PROVISION_FAILED = 11; // 0xb
   }
 
   public abstract static class ConnectivityManager.OnStartTetheringCallback {
@@ -4131,6 +3915,11 @@
     method public void onTetheringStarted();
   }
 
+  public abstract static class ConnectivityManager.TetheringEntitlementValueListener {
+    ctor public ConnectivityManager.TetheringEntitlementValueListener();
+    method public void onEntitlementResult(int);
+  }
+
   public final class IpPrefix implements android.os.Parcelable {
     ctor public IpPrefix(java.net.InetAddress, int);
     ctor public IpPrefix(String);
@@ -4246,6 +4035,10 @@
     field public static final String EXTRA_PACKAGE_NAME = "packageName";
   }
 
+  public class NetworkStack {
+    field public static final String PERMISSION_MAINLINE_NETWORK_STACK = "android.permission.MAINLINE_NETWORK_STACK";
+  }
+
   public final class RouteInfo implements android.os.Parcelable {
     ctor public RouteInfo(android.net.IpPrefix, java.net.InetAddress, String, int);
     method public int getType();
@@ -5165,6 +4958,53 @@
     field public static final String EXTRA_EVENT_TIMESTAMP = "android.os.extra.EVENT_TIMESTAMP";
   }
 
+  public final class BatterySaverPolicyConfig implements android.os.Parcelable {
+    method public int describeContents();
+    method public float getAdjustBrightnessFactor();
+    method public boolean getAdvertiseIsEnabled();
+    method public boolean getDeferFullBackup();
+    method public boolean getDeferKeyValueBackup();
+    method @NonNull public java.util.Map<java.lang.String,java.lang.String> getDeviceSpecificSettings();
+    method public boolean getDisableAnimation();
+    method public boolean getDisableAod();
+    method public boolean getDisableLaunchBoost();
+    method public boolean getDisableOptionalSensors();
+    method public boolean getDisableSoundTrigger();
+    method public boolean getDisableVibration();
+    method public boolean getEnableAdjustBrightness();
+    method public boolean getEnableDataSaver();
+    method public boolean getEnableFirewall();
+    method public boolean getEnableQuickDoze();
+    method public boolean getForceAllAppsStandby();
+    method public boolean getForceBackgroundCheck();
+    method public int getGpsMode();
+    method public void writeToParcel(android.os.Parcel, int);
+    field public static final android.os.Parcelable.Creator<android.os.BatterySaverPolicyConfig> CREATOR;
+  }
+
+  public static final class BatterySaverPolicyConfig.Builder {
+    ctor public BatterySaverPolicyConfig.Builder();
+    method @NonNull public android.os.BatterySaverPolicyConfig.Builder addDeviceSpecificSetting(@NonNull String, @NonNull String);
+    method @NonNull public android.os.BatterySaverPolicyConfig build();
+    method @NonNull public android.os.BatterySaverPolicyConfig.Builder setAdjustBrightnessFactor(float);
+    method @NonNull public android.os.BatterySaverPolicyConfig.Builder setAdvertiseIsEnabled(boolean);
+    method @NonNull public android.os.BatterySaverPolicyConfig.Builder setDeferFullBackup(boolean);
+    method @NonNull public android.os.BatterySaverPolicyConfig.Builder setDeferKeyValueBackup(boolean);
+    method @NonNull public android.os.BatterySaverPolicyConfig.Builder setDisableAnimation(boolean);
+    method @NonNull public android.os.BatterySaverPolicyConfig.Builder setDisableAod(boolean);
+    method @NonNull public android.os.BatterySaverPolicyConfig.Builder setDisableLaunchBoost(boolean);
+    method @NonNull public android.os.BatterySaverPolicyConfig.Builder setDisableOptionalSensors(boolean);
+    method @NonNull public android.os.BatterySaverPolicyConfig.Builder setDisableSoundTrigger(boolean);
+    method @NonNull public android.os.BatterySaverPolicyConfig.Builder setDisableVibration(boolean);
+    method @NonNull public android.os.BatterySaverPolicyConfig.Builder setEnableAdjustBrightness(boolean);
+    method @NonNull public android.os.BatterySaverPolicyConfig.Builder setEnableDataSaver(boolean);
+    method @NonNull public android.os.BatterySaverPolicyConfig.Builder setEnableFirewall(boolean);
+    method @NonNull public android.os.BatterySaverPolicyConfig.Builder setEnableQuickDoze(boolean);
+    method @NonNull public android.os.BatterySaverPolicyConfig.Builder setForceAllAppsStandby(boolean);
+    method @NonNull public android.os.BatterySaverPolicyConfig.Builder setForceBackgroundCheck(boolean);
+    method @NonNull public android.os.BatterySaverPolicyConfig.Builder setGpsMode(int);
+  }
+
   public class Binder implements android.os.IBinder {
     method public static final long clearCallingWorkSource();
     method public static final int getCallingWorkSourceUid();
@@ -5440,6 +5280,8 @@
   public final class PowerManager {
     method @RequiresPermission(allOf={android.Manifest.permission.READ_DREAM_STATE, android.Manifest.permission.WRITE_DREAM_STATE}) public void dream(long);
     method @RequiresPermission(android.Manifest.permission.POWER_SAVER) public int getPowerSaveMode();
+    method @RequiresPermission(anyOf={android.Manifest.permission.DEVICE_POWER, android.Manifest.permission.POWER_SAVER}) public boolean setAdaptivePowerSaveEnabled(boolean);
+    method @RequiresPermission(anyOf={android.Manifest.permission.DEVICE_POWER, android.Manifest.permission.POWER_SAVER}) public boolean setAdaptivePowerSavePolicy(@NonNull android.os.BatterySaverPolicyConfig);
     method @RequiresPermission(android.Manifest.permission.POWER_SAVER) public boolean setDynamicPowerSavings(boolean, int);
     method @RequiresPermission(anyOf={android.Manifest.permission.DEVICE_POWER, android.Manifest.permission.POWER_SAVER}) public boolean setPowerSaveMode(boolean);
     method @RequiresPermission(anyOf={android.Manifest.permission.DEVICE_POWER, android.Manifest.permission.USER_ACTIVITY}) public void userActivity(long, int, int);
@@ -5578,6 +5420,7 @@
   }
 
   public final class UserHandle implements android.os.Parcelable {
+    method public static int getAppId(int);
     method public int getIdentifier();
     method @Deprecated public boolean isOwner();
     method public boolean isSystem();
@@ -5840,6 +5683,7 @@
   public static interface DeviceConfig.ActivityManager {
     field public static final String KEY_COMPACT_ACTION_1 = "compact_action_1";
     field public static final String KEY_COMPACT_ACTION_2 = "compact_action_2";
+    field public static final String KEY_COMPACT_STATSD_SAMPLE_RATE = "compact_statsd_sample_rate";
     field public static final String KEY_COMPACT_THROTTLE_1 = "compact_throttle_1";
     field public static final String KEY_COMPACT_THROTTLE_2 = "compact_throttle_2";
     field public static final String KEY_COMPACT_THROTTLE_3 = "compact_throttle_3";
@@ -5855,9 +5699,9 @@
   }
 
   public static interface DeviceConfig.AttentionManagerService {
+    field public static final String COMPONENT_NAME = "component_name";
     field public static final String NAMESPACE = "attention_manager_service";
-    field public static final String PROPERTY_COMPONENT_NAME = "component_name";
-    field public static final String PROPERTY_SERVICE_ENABLED = "service_enabled";
+    field public static final String SERVICE_ENABLED = "service_enabled";
   }
 
   public static interface DeviceConfig.ContentCapture {
@@ -5871,9 +5715,9 @@
   }
 
   public static interface DeviceConfig.IntelligenceAttention {
+    field public static final String ATTENTION_ENABLED = "attention_enabled";
+    field public static final String ATTENTION_SETTINGS = "attention_settings";
     field public static final String NAMESPACE = "intelligence_attention";
-    field public static final String PROPERTY_ATTENTION_ENABLED = "attention_enabled";
-    field public static final String PROPERTY_ATTENTION_SETTINGS = "attention_settings";
   }
 
   public static interface DeviceConfig.NotificationAssistant {
@@ -5888,8 +5732,8 @@
 
   public static interface DeviceConfig.Privacy {
     field public static final String NAMESPACE = "privacy";
-    field public static final String PROPERTY_LOCATION_ACCESS_CHECK_ENABLED = "enable_location_access_check";
-    field public static final String PROPERTY_PERMISSIONS_HUB_ENABLED = "enable_permissions_hub";
+    field public static final String PROPERTY_LOCATION_ACCESS_CHECK_ENABLED = "location_access_check_enabled";
+    field public static final String PROPERTY_PERMISSIONS_HUB_ENABLED = "permissions_hub_enabled";
   }
 
   public static interface DeviceConfig.RuntimeNative {
@@ -5903,8 +5747,9 @@
 
   public static interface DeviceConfig.Telephony {
     field public static final String NAMESPACE = "telephony";
-    field public static final String PROPERTY_ENABLE_RAMPING_RINGER = "enable_ramping_ringer";
-    field public static final String PROPERTY_RAMPING_RINGER_DURATION = "ramping_duration";
+    field public static final String RAMPING_RINGER_DURATION = "ramping_ringer_duration";
+    field public static final String RAMPING_RINGER_ENABLED = "ramping_ringer_enabled";
+    field public static final String RAMPING_RINGER_VIBRATION_DURATION = "ramping_ringer_vibration_duration";
   }
 
   public final class DocumentsContract {
@@ -7927,6 +7772,7 @@
     method @RequiresPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE) public boolean getEmergencyCallbackMode();
     method @RequiresPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE) public String getIsimDomain();
     method @RequiresPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE) public String getIsimIst();
+    method @NonNull @RequiresPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE) public java.util.List<android.util.Pair<java.lang.Integer,java.lang.Integer>> getLogicalToPhysicalSlotMapping();
     method @RequiresPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE) public long getPreferredNetworkTypeBitmap();
     method @RequiresPermission(anyOf={android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, android.Manifest.permission.READ_PHONE_STATE}) public int getRadioPowerState();
     method public int getSimApplicationState();
diff --git a/api/test-current.txt b/api/test-current.txt
index 91d0019..9023a85 100644
--- a/api/test-current.txt
+++ b/api/test-current.txt
@@ -242,6 +242,7 @@
 
   public class NotificationManager {
     method public android.content.ComponentName getEffectsSuppressor();
+    method public boolean matchesCallFilter(android.os.Bundle);
   }
 
   public final class PictureInPictureParams implements android.os.Parcelable {
@@ -332,6 +333,91 @@
 
 }
 
+package android.app.prediction {
+
+  public final class AppPredictionContext implements android.os.Parcelable {
+    method public int describeContents();
+    method @Nullable public android.os.Bundle getExtras();
+    method @NonNull public String getPackageName();
+    method public int getPredictedTargetCount();
+    method public String getUiSurface();
+    method public void writeToParcel(android.os.Parcel, int);
+    field public static final android.os.Parcelable.Creator<android.app.prediction.AppPredictionContext> CREATOR;
+  }
+
+  public static final class AppPredictionContext.Builder {
+    ctor public AppPredictionContext.Builder(@NonNull android.content.Context);
+    method public android.app.prediction.AppPredictionContext build();
+    method public android.app.prediction.AppPredictionContext.Builder setExtras(@Nullable android.os.Bundle);
+    method public android.app.prediction.AppPredictionContext.Builder setPredictedTargetCount(int);
+    method public android.app.prediction.AppPredictionContext.Builder setUiSurface(@Nullable String);
+  }
+
+  public final class AppPredictionManager {
+    method public android.app.prediction.AppPredictor createAppPredictionSession(@NonNull android.app.prediction.AppPredictionContext);
+  }
+
+  public final class AppPredictionSessionId implements android.os.Parcelable {
+    method public int describeContents();
+    method public void writeToParcel(android.os.Parcel, int);
+    field public static final android.os.Parcelable.Creator<android.app.prediction.AppPredictionSessionId> CREATOR;
+  }
+
+  public final class AppPredictor {
+    method public void destroy();
+    method public android.app.prediction.AppPredictionSessionId getSessionId();
+    method public void notifyAppTargetEvent(@NonNull android.app.prediction.AppTargetEvent);
+    method public void notifyLocationShown(@NonNull String, @NonNull java.util.List<android.app.prediction.AppTargetId>);
+    method public void registerPredictionUpdates(@NonNull java.util.concurrent.Executor, @NonNull android.app.prediction.AppPredictor.Callback);
+    method public void requestPredictionUpdate();
+    method @Nullable public void sortTargets(@NonNull java.util.List<android.app.prediction.AppTarget>, @NonNull java.util.concurrent.Executor, @NonNull java.util.function.Consumer<java.util.List<android.app.prediction.AppTarget>>);
+    method public void unregisterPredictionUpdates(@NonNull android.app.prediction.AppPredictor.Callback);
+  }
+
+  public static interface AppPredictor.Callback {
+    method public void onTargetsAvailable(@NonNull java.util.List<android.app.prediction.AppTarget>);
+  }
+
+  public final class AppTarget implements android.os.Parcelable {
+    ctor public AppTarget(@NonNull android.app.prediction.AppTargetId, @NonNull String, @Nullable String, @NonNull android.os.UserHandle);
+    method public int describeContents();
+    method @Nullable public String getClassName();
+    method @NonNull public android.app.prediction.AppTargetId getId();
+    method @NonNull public String getPackageName();
+    method public int getRank();
+    method @Nullable public android.content.pm.ShortcutInfo getShortcutInfo();
+    method @NonNull public android.os.UserHandle getUser();
+    method public void writeToParcel(android.os.Parcel, int);
+    field public static final android.os.Parcelable.Creator<android.app.prediction.AppTarget> CREATOR;
+  }
+
+  public final class AppTargetEvent implements android.os.Parcelable {
+    method public int describeContents();
+    method @NonNull public int getAction();
+    method @NonNull public String getLaunchLocation();
+    method @Nullable public android.app.prediction.AppTarget getTarget();
+    method public void writeToParcel(android.os.Parcel, int);
+    field public static final int ACTION_DISMISS = 2; // 0x2
+    field public static final int ACTION_LAUNCH = 1; // 0x1
+    field public static final int ACTION_PIN = 3; // 0x3
+    field public static final android.os.Parcelable.Creator<android.app.prediction.AppTargetEvent> CREATOR;
+  }
+
+  public static final class AppTargetEvent.Builder {
+    ctor public AppTargetEvent.Builder(@Nullable android.app.prediction.AppTarget, int);
+    method public android.app.prediction.AppTargetEvent build();
+    method public android.app.prediction.AppTargetEvent.Builder setLaunchLocation(String);
+  }
+
+  public final class AppTargetId implements android.os.Parcelable {
+    ctor public AppTargetId(@NonNull String);
+    method public int describeContents();
+    method public void writeToParcel(android.os.Parcel, int);
+    field public static final android.os.Parcelable.Creator<android.app.prediction.AppTargetId> CREATOR;
+  }
+
+}
+
 package android.app.role {
 
   public final class RoleManager {
@@ -518,23 +604,10 @@
 
 package android.graphics {
 
-  public final class Bitmap implements android.os.Parcelable {
-    method public void eraseColor(@ColorLong long);
-    method public android.graphics.Color getColor(int, int);
-    method public void setColorSpace(@NonNull android.graphics.ColorSpace);
-  }
-
   public final class ImageDecoder implements java.lang.AutoCloseable {
     method @AnyThread @NonNull public static android.graphics.ImageDecoder.Source createSource(android.content.res.Resources, java.io.InputStream, int);
   }
 
-  public class Paint {
-    method @ColorLong public long getColorLong();
-    method @ColorLong public long getShadowLayerColorLong();
-    method public void setColor(@ColorLong long);
-    method public void setShadowLayer(float, float, float, @ColorLong long);
-  }
-
 }
 
 package android.graphics.drawable {
@@ -754,6 +827,17 @@
     method public android.media.BufferingParams.Builder setResumePlaybackMarkMs(int);
   }
 
+  public class MediaPlayer2 implements android.media.AudioRouting java.lang.AutoCloseable {
+    method public android.media.MediaPlayer2.DrmInfo getDrmInfo(@NonNull android.media.DataSourceDesc);
+    method public android.media.MediaDrm.KeyRequest getDrmKeyRequest(@NonNull android.media.DataSourceDesc, @Nullable byte[], @Nullable byte[], @Nullable String, int, @Nullable java.util.Map<java.lang.String,java.lang.String>) throws android.media.MediaPlayer2.NoDrmSchemeException;
+    method public String getDrmPropertyString(@NonNull android.media.DataSourceDesc, @NonNull String) throws android.media.MediaPlayer2.NoDrmSchemeException;
+    method public Object prepareDrm(@NonNull android.media.DataSourceDesc, @NonNull java.util.UUID);
+    method public byte[] provideDrmKeyResponse(@NonNull android.media.DataSourceDesc, @Nullable byte[], @NonNull byte[]) throws android.media.DeniedByServerException, android.media.MediaPlayer2.NoDrmSchemeException;
+    method public void releaseDrm(@NonNull android.media.DataSourceDesc) throws android.media.MediaPlayer2.NoDrmSchemeException;
+    method public void restoreDrmKeys(@NonNull android.media.DataSourceDesc, @NonNull byte[]) throws android.media.MediaPlayer2.NoDrmSchemeException;
+    method public void setDrmPropertyString(@NonNull android.media.DataSourceDesc, @NonNull String, @NonNull String) throws android.media.MediaPlayer2.NoDrmSchemeException;
+  }
+
   public final class PlaybackParams implements android.os.Parcelable {
     method public int getAudioStretchMode();
     method public android.media.PlaybackParams setAudioStretchMode(int);
@@ -873,6 +957,10 @@
     method public boolean satisfiedByNetworkCapabilities(android.net.NetworkCapabilities);
   }
 
+  public class NetworkStack {
+    field public static final String PERMISSION_MAINLINE_NETWORK_STACK = "android.permission.MAINLINE_NETWORK_STACK";
+  }
+
   public final class RouteInfo implements android.os.Parcelable {
     ctor public RouteInfo(android.net.IpPrefix, java.net.InetAddress, String, int);
     method public int getType();
@@ -1660,7 +1748,7 @@
 
   public static interface DeviceConfig.Privacy {
     field public static final String NAMESPACE = "privacy";
-    field public static final String PROPERTY_LOCATION_ACCESS_CHECK_ENABLED = "enable_location_access_check";
+    field public static final String PROPERTY_LOCATION_ACCESS_CHECK_ENABLED = "location_access_check_enabled";
   }
 
   public final class MediaStore {
@@ -1774,6 +1862,24 @@
 
 }
 
+package android.service.appprediction {
+
+  public abstract class AppPredictionService extends android.app.Service {
+    ctor public AppPredictionService();
+    method @MainThread public abstract void onAppTargetEvent(@NonNull android.app.prediction.AppPredictionSessionId, @NonNull android.app.prediction.AppTargetEvent);
+    method public final android.os.IBinder onBind(android.content.Intent);
+    method public void onCreatePredictionSession(@NonNull android.app.prediction.AppPredictionContext, @NonNull android.app.prediction.AppPredictionSessionId);
+    method @MainThread public void onDestroyPredictionSession(@NonNull android.app.prediction.AppPredictionSessionId);
+    method @MainThread public abstract void onLocationShown(@NonNull android.app.prediction.AppPredictionSessionId, @NonNull String, @NonNull java.util.List<android.app.prediction.AppTargetId>);
+    method @MainThread public abstract void onRequestPredictionUpdate(@NonNull android.app.prediction.AppPredictionSessionId);
+    method @MainThread public abstract void onSortAppTargets(@NonNull android.app.prediction.AppPredictionSessionId, @NonNull java.util.List<android.app.prediction.AppTarget>, @NonNull android.os.CancellationSignal, @NonNull java.util.function.Consumer<java.util.List<android.app.prediction.AppTarget>>);
+    method @MainThread public void onStartPredictionUpdates();
+    method @MainThread public void onStopPredictionUpdates();
+    method public final void updatePredictions(@NonNull android.app.prediction.AppPredictionSessionId, @NonNull java.util.List<android.app.prediction.AppTarget>);
+  }
+
+}
+
 package android.service.autofill {
 
   public abstract class AutofillFieldClassificationService extends android.app.Service {
diff --git a/cmds/statsd/src/atoms.proto b/cmds/statsd/src/atoms.proto
index 30df850..937588b 100644
--- a/cmds/statsd/src/atoms.proto
+++ b/cmds/statsd/src/atoms.proto
@@ -234,6 +234,7 @@
         BluetoothBondStateChanged bluetooth_bond_state_changed = 165;
         BluetoothClassicPairingEventReported bluetooth_classic_pairing_event_reported = 166;
         BluetoothSmpPairingEventReported bluetooth_smp_pairing_event_reported = 167;
+        ScreenTimeoutExtensionReported screen_timeout_extension_reported = 168;
     }
 
     // Pulled events will start at field 10000.
@@ -288,6 +289,7 @@
         DebugElapsedClock debug_elapsed_clock = 10046;
         DebugFailingElapsedClock debug_failing_elapsed_clock = 10047;
         NumBiometricsEnrolled num_faces_enrolled = 10048;
+        RoleHolder role_holder = 10049;
     }
 
     // DO NOT USE field numbers above 100,000 in AOSP.
@@ -3973,6 +3975,20 @@
     optional int32 num_enrolled = 2;
 }
 
+/**
+ * A mapping of role holder -> role
+ */
+message RoleHolder {
+    // uid of the role holder
+    optional int32 uid = 1 [(is_uid) = true];
+
+    // package name of the role holder
+    optional string package_name = 2;
+
+    // the role held
+    optional string role = 3;
+}
+
 message AggStats {
     optional int64 min = 1;
 
@@ -4968,17 +4984,17 @@
  *     packages/apps/Nfc/src/com/android/nfc/cardemulation/AidRoutingManager.java
  */
 message NfcErrorOccurred {
-  enum Type {
-    UNKNOWN = 0;
-    CMD_TIMEOUT = 1;
-    ERROR_NOTIFICATION = 2;
-    AID_OVERFLOW = 3;
-  }
-  optional Type type = 1;
-  // If it's nci cmd timeout, log the timeout command.
-  optional uint32 nci_cmd = 2;
+    enum Type {
+        UNKNOWN = 0;
+        CMD_TIMEOUT = 1;
+        ERROR_NOTIFICATION = 2;
+        AID_OVERFLOW = 3;
+    }
+    optional Type type = 1;
+    // If it's nci cmd timeout, log the timeout command.
+    optional uint32 nci_cmd = 2;
 
-  optional uint32 error_ntf_status_code = 3;
+    optional uint32 error_ntf_status_code = 3;
 }
 
 /**
@@ -4987,14 +5003,14 @@
  *     packages/apps/Nfc/src/com/android/nfc/NfcService.java
  */
 message NfcStateChanged {
-  enum State {
-    UNKNOWN = 0;
-    OFF = 1;
-    ON = 2;
-    ON_LOCKED = 3; // Secure Nfc enabled.
-    CRASH_RESTART = 4; // NfcService watchdog timeout restart.
-  }
-  optional State state = 1;
+    enum State {
+        UNKNOWN = 0;
+        OFF = 1;
+        ON = 2;
+        ON_LOCKED = 3; // Secure Nfc enabled.
+        CRASH_RESTART = 4; // NfcService watchdog timeout restart.
+    }
+    optional State state = 1;
 }
 
 /**
@@ -5003,12 +5019,12 @@
  *     packages/apps/Nfc/src/com/android/nfc/P2pLinkManager.java
  */
 message NfcBeamOccurred {
-  enum Operation {
-      UNKNOWN = 0;
-      SEND = 1;
-      RECEIVE = 2;
-  }
-  optional Operation operation = 1;
+    enum Operation {
+        UNKNOWN = 0;
+        SEND = 1;
+        RECEIVE = 2;
+    }
+    optional Operation operation = 1;
 }
 
 /**
@@ -5018,16 +5034,16 @@
  *     packages/apps/Nfc/src/com/android/nfc/cardemulation/HostNfcFEmulationManager.java
  */
 message NfcCardemulationOccurred {
-  enum Category {
-      UNKNOWN = 0;
-      HCE_PAYMENT = 1;
-      HCE_OTHER = 2;
-      OFFHOST = 3;
-  }
-  // Transaction belongs to HCE payment or HCE other category, or offhost.
-  optional Category category = 1;
-  // SeName from transaction: SIMx, eSEx, HCE, HCEF.
-  optional string se_name = 2;
+    enum Category {
+        UNKNOWN = 0;
+        HCE_PAYMENT = 1;
+        HCE_OTHER = 2;
+        OFFHOST = 3;
+    }
+    // Transaction belongs to HCE payment or HCE other category, or offhost.
+    optional Category category = 1;
+    // SeName from transaction: SIMx, eSEx, HCE, HCEF.
+    optional string se_name = 2;
 }
 
 /**
@@ -5036,16 +5052,16 @@
  *     packages/apps/Nfc/src/com/android/nfc/NfcDispatcher.java
  */
 message NfcTagOccurred {
-  enum Type {
-      UNKNOWN = 0;
-      URL = 1;
-      BT_PAIRING = 2;
-      PROVISION = 3;
-      WIFI_CONNECT = 4;
-      APP_LAUNCH = 5;
-      OTHERS = 6;
-  }
-  optional Type type = 1;
+    enum Type {
+        UNKNOWN = 0;
+        URL = 1;
+        BT_PAIRING = 2;
+        PROVISION = 3;
+        WIFI_CONNECT = 4;
+        APP_LAUNCH = 5;
+        OTHERS = 6;
+    }
+    optional Type type = 1;
 }
 
 /**
@@ -5065,18 +5081,18 @@
  *     packages/apps/SecureElement/src/com/android/se/Terminal.java
  */
 message SeStateChanged {
-  enum State {
-    UNKNOWN = 0;
-    INITIALIZED = 1;
-    DISCONNECTED = 2;
-    CONNECTED = 3;
-    HALCRASH = 4;
-  }
-  optional State state = 1;
+    enum State {
+        UNKNOWN = 0;
+        INITIALIZED = 1;
+        DISCONNECTED = 2;
+        CONNECTED = 3;
+        HALCRASH = 4;
+    }
+    optional State state = 1;
 
-  optional string state_change_reason = 2;
-  // SIMx or eSEx.
-  optional string terminal = 3;
+    optional string state_change_reason = 2;
+    // SIMx or eSEx.
+    optional string terminal = 3;
 }
 
 /**
@@ -5085,15 +5101,15 @@
  *     packages/apps/SecureElement/src/com/android/se/Terminal.java
  */
 message SeOmapiReported {
-  enum Operation {
-    UNKNOWN = 0;
-    OPEN_CHANNEL = 1;
-  }
-  optional Operation operation = 1;
-  // SIMx or eSEx.
-  optional string terminal = 2;
+    enum Operation {
+        UNKNOWN = 0;
+        OPEN_CHANNEL = 1;
+    }
+    optional Operation operation = 1;
+    // SIMx or eSEx.
+    optional string terminal = 2;
 
-  optional string package_name = 3;
+    optional string package_name = 3;
 }
 
 /**
@@ -5247,6 +5263,7 @@
         PERMISSION_IGNORED = 12;
         SWIPE_LEFT = 13;
         SWIPE_RIGHT = 14;
+        STACK_EXPANDED = 15;
     }
     optional Action action = 6;
 
@@ -5275,3 +5292,15 @@
     }
     optional State state = 4;
 }
+
+/**
+ * Logs PowerManagerService screen timeout resets (extensions) that happen when an attention check
+ * returns true.
+ *
+ * Logged from:
+ *   frameworks/base/services/core/java/com/android/server/power/PowerManagerService.java
+ */
+message ScreenTimeoutExtensionReported {
+    // Describes how many times in a row did the power manager reset the screen off timeout.
+    optional uint32 consecutive_timeout_extended_count = 1;
+}
diff --git a/cmds/statsd/src/external/StatsPullerManager.cpp b/cmds/statsd/src/external/StatsPullerManager.cpp
index a6ba2ca..6f3eeaa 100644
--- a/cmds/statsd/src/external/StatsPullerManager.cpp
+++ b/cmds/statsd/src/external/StatsPullerManager.cpp
@@ -220,6 +220,9 @@
         // BuildInformation.
         {android::util::BUILD_INFORMATION,
          {.puller = new StatsCompanionServicePuller(android::util::BUILD_INFORMATION)}},
+        // RoleHolder.
+        {android::util::ROLE_HOLDER,
+         {.puller = new StatsCompanionServicePuller(android::util::ROLE_HOLDER)}},
 };
 
 StatsPullerManager::StatsPullerManager() : mNextPullTimeNs(NO_ALARM_UPDATE) {
diff --git a/config/boot-image-profile.txt b/config/boot-image-profile.txt
index c2e441b..7f4d1d0 100644
--- a/config/boot-image-profile.txt
+++ b/config/boot-image-profile.txt
@@ -19875,7 +19875,6 @@
 HSPLandroid/graphics/Bitmap;->createBitmap(Landroid/util/DisplayMetrics;IILandroid/graphics/Bitmap$Config;ZLandroid/graphics/ColorSpace;)Landroid/graphics/Bitmap;
 HSPLandroid/graphics/Bitmap;->createBitmap(Landroid/util/DisplayMetrics;[IIIIILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;
 HSPLandroid/graphics/Bitmap;->createBitmap([IIILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;
-HSPLandroid/graphics/Bitmap;->createHardwareBitmap(Landroid/graphics/GraphicBuffer;)Landroid/graphics/Bitmap;
 HSPLandroid/graphics/Bitmap;->createScaledBitmap(Landroid/graphics/Bitmap;IIZ)Landroid/graphics/Bitmap;
 HSPLandroid/graphics/Bitmap;->eraseColor(I)V
 HSPLandroid/graphics/Bitmap;->extractAlpha(Landroid/graphics/Paint;[I)Landroid/graphics/Bitmap;
diff --git a/core/java/android/app/ActivityManager.java b/core/java/android/app/ActivityManager.java
index 75ea7bb..db3b720 100644
--- a/core/java/android/app/ActivityManager.java
+++ b/core/java/android/app/ActivityManager.java
@@ -42,6 +42,7 @@
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
 import android.graphics.Color;
+import android.graphics.ColorSpace;
 import android.graphics.GraphicBuffer;
 import android.graphics.Matrix;
 import android.graphics.Point;
@@ -1777,6 +1778,10 @@
         private final int mSystemUiVisibility;
         private final boolean mIsTranslucent;
 
+        // TODO(b/116112787) TaskSnapshot must also book keep the color space from hardware bitmap
+        // when created.
+        private final ColorSpace mColorSpace = ColorSpace.get(ColorSpace.Named.SRGB);
+
         public TaskSnapshot(@NonNull ComponentName topActivityComponent, GraphicBuffer snapshot,
                 int orientation, Rect contentInsets, boolean reducedResolution, float scale,
                 boolean isRealSnapshot, int windowingMode, int systemUiVisibility,
@@ -1822,6 +1827,13 @@
         }
 
         /**
+         * @return The color space of graphic buffer representing the screenshot.
+         */
+        public ColorSpace getColorSpace() {
+            return mColorSpace;
+        }
+
+        /**
          * @return The screen orientation the screenshot was taken in.
          */
         @UnsupportedAppUsage
diff --git a/core/java/android/app/ActivityOptions.java b/core/java/android/app/ActivityOptions.java
index 94b42ff..e2d868f 100644
--- a/core/java/android/app/ActivityOptions.java
+++ b/core/java/android/app/ActivityOptions.java
@@ -33,6 +33,7 @@
 import android.graphics.Bitmap.Config;
 import android.graphics.GraphicBuffer;
 import android.graphics.Rect;
+import android.hardware.HardwareBuffer;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.IRemoteCallback;
@@ -916,7 +917,8 @@
                 // Unpackage the GraphicBuffer from the parceled thumbnail
                 final GraphicBuffer buffer = opts.getParcelable(KEY_ANIM_THUMBNAIL);
                 if (buffer != null) {
-                    mThumbnail = Bitmap.createHardwareBitmap(buffer);
+                    mThumbnail = Bitmap.wrapHardwareBuffer(
+                            HardwareBuffer.createFromGraphicBuffer(buffer), null);
                 }
                 mStartX = opts.getInt(KEY_ANIM_START_X, 0);
                 mStartY = opts.getInt(KEY_ANIM_START_Y, 0);
diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java
index 03a09ee..22dcce5 100644
--- a/core/java/android/app/ActivityThread.java
+++ b/core/java/android/app/ActivityThread.java
@@ -177,7 +177,6 @@
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.lang.ref.WeakReference;
-import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.net.InetAddress;
 import java.text.DateFormat;
@@ -5936,18 +5935,6 @@
         StrictMode.initThreadDefaults(data.appInfo);
         StrictMode.initVmDefaults(data.appInfo);
 
-        // We deprecated Build.SERIAL and only apps that target pre NMR1
-        // SDK can see it. Since access to the serial is now behind a
-        // permission we push down the value and here we fix it up
-        // before any app code has been loaded.
-        try {
-            Field field = Build.class.getDeclaredField("SERIAL");
-            field.setAccessible(true);
-            field.set(Build.class, data.buildSerial);
-        } catch (NoSuchFieldException | IllegalAccessException e) {
-            /* ignore */
-        }
-
         if (data.debugMode != ApplicationThreadConstants.DEBUG_OFF) {
             // XXX should have option to change the port.
             Debug.changeDebugPort(8100);
@@ -6083,7 +6070,12 @@
             instrApp.initForUser(UserHandle.myUserId());
             final LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
                     appContext.getClassLoader(), false, true, false);
-            final ContextImpl instrContext = ContextImpl.createAppContext(this, pi);
+
+            // The test context's op package name == the target app's op package name, because
+            // the app ops manager checks the op package name against the real calling UID,
+            // which is what the target package name is associated with.
+            final ContextImpl instrContext = ContextImpl.createAppContext(this, pi,
+                    appContext.getOpPackageName());
 
             try {
                 final ClassLoader cl = instrContext.getClassLoader();
diff --git a/core/java/android/app/ActivityView.java b/core/java/android/app/ActivityView.java
index eae7d6b..5814e69 100644
--- a/core/java/android/app/ActivityView.java
+++ b/core/java/android/app/ActivityView.java
@@ -363,7 +363,7 @@
         final IWindowManager wm = WindowManagerGlobal.getWindowManagerService();
 
         mRootSurfaceControl = new SurfaceControl.Builder(surfaceSession)
-                .setContainerLayer()
+                .setContainerLayer(true)
                 .setParent(mSurfaceView.getSurfaceControl())
                 .setName(DISPLAY_NAME)
                 .build();
diff --git a/core/java/android/app/ContextImpl.java b/core/java/android/app/ContextImpl.java
index 92cdb20..1a728c1 100644
--- a/core/java/android/app/ContextImpl.java
+++ b/core/java/android/app/ContextImpl.java
@@ -2128,7 +2128,7 @@
                 flags | CONTEXT_REGISTER_PACKAGE);
         if (pi != null) {
             ContextImpl c = new ContextImpl(this, mMainThread, pi, null, mActivityToken,
-                    new UserHandle(UserHandle.getUserId(application.uid)), flags, null);
+                    new UserHandle(UserHandle.getUserId(application.uid)), flags, null, null);
 
             final int displayId = getDisplayId();
 
@@ -2156,14 +2156,14 @@
             // The system resources are loaded in every application, so we can safely copy
             // the context without reloading Resources.
             return new ContextImpl(this, mMainThread, mPackageInfo, null, mActivityToken, user,
-                    flags, null);
+                    flags, null, null);
         }
 
         LoadedApk pi = mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(),
                 flags | CONTEXT_REGISTER_PACKAGE, user.getIdentifier());
         if (pi != null) {
             ContextImpl c = new ContextImpl(this, mMainThread, pi, null, mActivityToken, user,
-                    flags, null);
+                    flags, null, null);
 
             final int displayId = getDisplayId();
 
@@ -2190,7 +2190,7 @@
         final String[] paths = mPackageInfo.getSplitPaths(splitName);
 
         final ContextImpl context = new ContextImpl(this, mMainThread, mPackageInfo, splitName,
-                mActivityToken, mUser, mFlags, classLoader);
+                mActivityToken, mUser, mFlags, classLoader, null);
 
         final int displayId = getDisplayId();
 
@@ -2214,7 +2214,7 @@
         }
 
         ContextImpl context = new ContextImpl(this, mMainThread, mPackageInfo, mSplitName,
-                mActivityToken, mUser, mFlags, mClassLoader);
+                mActivityToken, mUser, mFlags, mClassLoader, null);
 
         final int displayId = getDisplayId();
         context.setResources(createResources(mActivityToken, mPackageInfo, mSplitName, displayId,
@@ -2229,7 +2229,7 @@
         }
 
         ContextImpl context = new ContextImpl(this, mMainThread, mPackageInfo, mSplitName,
-                mActivityToken, mUser, mFlags, mClassLoader);
+                mActivityToken, mUser, mFlags, mClassLoader, null);
 
         final int displayId = display.getDisplayId();
         context.setResources(createResources(mActivityToken, mPackageInfo, mSplitName, displayId,
@@ -2243,7 +2243,7 @@
         final int flags = (mFlags & ~Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE)
                 | Context.CONTEXT_DEVICE_PROTECTED_STORAGE;
         return new ContextImpl(this, mMainThread, mPackageInfo, mSplitName, mActivityToken, mUser,
-                flags, mClassLoader);
+                flags, mClassLoader, null);
     }
 
     @Override
@@ -2251,7 +2251,7 @@
         final int flags = (mFlags & ~Context.CONTEXT_DEVICE_PROTECTED_STORAGE)
                 | Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE;
         return new ContextImpl(this, mMainThread, mPackageInfo, mSplitName, mActivityToken, mUser,
-                flags, mClassLoader);
+                flags, mClassLoader, null);
     }
 
     @Override
@@ -2397,7 +2397,7 @@
     static ContextImpl createSystemContext(ActivityThread mainThread) {
         LoadedApk packageInfo = new LoadedApk(mainThread);
         ContextImpl context = new ContextImpl(null, mainThread, packageInfo, null, null, null, 0,
-                null);
+                null, null);
         context.setResources(packageInfo.getResources());
         context.mResources.updateConfiguration(context.mResourcesManager.getConfiguration(),
                 context.mResourcesManager.getDisplayMetrics());
@@ -2414,7 +2414,7 @@
     static ContextImpl createSystemUiContext(ContextImpl systemContext, int displayId) {
         final LoadedApk packageInfo = systemContext.mPackageInfo;
         ContextImpl context = new ContextImpl(null, systemContext.mMainThread, packageInfo, null,
-                null, null, 0, null);
+                null, null, 0, null, null);
         context.setResources(createResources(null, packageInfo, null, displayId, null,
                 packageInfo.getCompatibilityInfo()));
         context.updateDisplay(displayId);
@@ -2431,9 +2431,14 @@
 
     @UnsupportedAppUsage
     static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo) {
+        return createAppContext(mainThread, packageInfo, null);
+    }
+
+    static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo,
+            String opPackageName) {
         if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
         ContextImpl context = new ContextImpl(null, mainThread, packageInfo, null, null, null, 0,
-                null);
+                null, opPackageName);
         context.setResources(packageInfo.getResources());
         return context;
     }
@@ -2461,7 +2466,7 @@
         }
 
         ContextImpl context = new ContextImpl(null, mainThread, packageInfo, activityInfo.splitName,
-                activityToken, null, 0, classLoader);
+                activityToken, null, 0, classLoader, null);
 
         // Clamp display ID to DEFAULT_DISPLAY if it is INVALID_DISPLAY.
         displayId = (displayId != Display.INVALID_DISPLAY) ? displayId : Display.DEFAULT_DISPLAY;
@@ -2491,7 +2496,7 @@
     private ContextImpl(@Nullable ContextImpl container, @NonNull ActivityThread mainThread,
             @NonNull LoadedApk packageInfo, @Nullable String splitName,
             @Nullable IBinder activityToken, @Nullable UserHandle user, int flags,
-            @Nullable ClassLoader classLoader) {
+            @Nullable ClassLoader classLoader, @Nullable String overrideOpPackageName) {
         mOuterContext = this;
 
         // If creator didn't specify which storage to use, use the default
@@ -2520,9 +2525,11 @@
         mClassLoader = classLoader;
         mResourcesManager = ResourcesManager.getInstance();
 
+        String opPackageName;
+
         if (container != null) {
             mBasePackageName = container.mBasePackageName;
-            mOpPackageName = container.mOpPackageName;
+            opPackageName = container.mOpPackageName;
             setResources(container.mResources);
             mDisplay = container.mDisplay;
         } else {
@@ -2533,12 +2540,14 @@
                 // processes.  For purposes of app ops, we must then consider the context as
                 // belonging to the package of this process, not the system itself, otherwise
                 // the package+uid verifications in app ops will fail.
-                mOpPackageName = ActivityThread.currentPackageName();
+                opPackageName = ActivityThread.currentPackageName();
             } else {
-                mOpPackageName = mBasePackageName;
+                opPackageName = mBasePackageName;
             }
         }
 
+        mOpPackageName = overrideOpPackageName != null ? overrideOpPackageName : opPackageName;
+
         mContentResolver = new ApplicationContentResolver(this, mainThread);
     }
 
diff --git a/core/java/android/app/IActivityManager.aidl b/core/java/android/app/IActivityManager.aidl
index 412d7f4..eea0543 100644
--- a/core/java/android/app/IActivityManager.aidl
+++ b/core/java/android/app/IActivityManager.aidl
@@ -203,7 +203,7 @@
     void unbindFinished(in IBinder token, in Intent service, boolean doRebind);
     void setProcessImportant(in IBinder token, int pid, boolean isForeground, String reason);
     void setServiceForeground(in ComponentName className, in IBinder token,
-            int id, in Notification notification, int flags);
+            int id, in Notification notification, int flags, int foregroundServiceType);
     boolean moveActivityTaskToBack(in IBinder token, boolean nonRoot);
     void getMemoryInfo(out ActivityManager.MemoryInfo outInfo);
     List<ActivityManager.ProcessErrorStateInfo> getProcessesInErrorState();
diff --git a/core/java/android/app/Notification.java b/core/java/android/app/Notification.java
index 7c550d4..028e3ef 100644
--- a/core/java/android/app/Notification.java
+++ b/core/java/android/app/Notification.java
@@ -8122,6 +8122,10 @@
                     big.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
                 }
             }
+            bindMediaActionButton(big, R.id.media_seamless, new Action(R.drawable.ic_media_seamless,
+                    mBuilder.mContext.getString(
+                            com.android.internal.R.string.ext_media_seamless_action), null), p);
+            big.setViewVisibility(R.id.media_seamless, View.GONE);
             handleImage(big);
             return big;
         }
diff --git a/core/java/android/app/NotificationManager.java b/core/java/android/app/NotificationManager.java
index 6a1ff29..8207e0a 100644
--- a/core/java/android/app/NotificationManager.java
+++ b/core/java/android/app/NotificationManager.java
@@ -828,6 +828,7 @@
     /**
      * @hide
      */
+    @TestApi
     public boolean matchesCallFilter(Bundle extras) {
         INotificationManager service = getService();
         try {
diff --git a/core/java/android/app/Service.java b/core/java/android/app/Service.java
index 5fa8526..87bf5ed 100644
--- a/core/java/android/app/Service.java
+++ b/core/java/android/app/Service.java
@@ -16,7 +16,10 @@
 
 package android.app;
 
+import static android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST;
+
 import android.annotation.IntDef;
+import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.annotation.UnsupportedAppUsage;
 import android.content.ComponentCallbacks2;
@@ -685,12 +688,10 @@
      * the permission {@link android.Manifest.permission#FOREGROUND_SERVICE} in order to use
      * this API.</p>
      *
-     * <p>To use this API, apps targeting API {@link android.os.Build.VERSION_CODES#Q} or later must
-     * specify the foreground service type using attribute
-     * {@link android.R.attr#foregroundServiceType} in service element of manifest file, otherwise
-     * a SecurityException is thrown when this API is called. Apps targeting API older than
-     * {@link android.os.Build.VERSION_CODES#Q} do not need to specify the foreground service type
-     * </p>
+     * <p>Apps built with SDK version {@link android.os.Build.VERSION_CODES#Q} or later can specify
+     * the foreground service types using attribute {@link android.R.attr#foregroundServiceType} in
+     * service element of manifest file. The value of attribute
+     * {@link android.R.attr#foregroundServiceType} can be multiple flags ORed together.</p>
      *
      * @param id The identifier for this notification as per
      * {@link NotificationManager#notify(int, Notification)
@@ -703,7 +704,42 @@
         try {
             mActivityManager.setServiceForeground(
                     new ComponentName(this, mClassName), mToken, id,
-                    notification, 0);
+                    notification, 0, FOREGROUND_SERVICE_TYPE_MANIFEST);
+        } catch (RemoteException ex) {
+        }
+    }
+
+  /**
+   * An overloaded version of {@link #startForeground(int, Notification)} with additional
+   * foregroundServiceType parameter.
+   *
+   * <p>Apps built with SDK version {@link android.os.Build.VERSION_CODES#Q} or later can specify
+   * the foreground service types using attribute {@link android.R.attr#foregroundServiceType} in
+   * service element of manifest file. The value of attribute
+   * {@link android.R.attr#foregroundServiceType} can be multiple flags ORed together.</p>
+   *
+   * <p>The foregroundServiceType parameter must be a subset flags of what is specified in manifest
+   * attribute {@link android.R.attr#foregroundServiceType}, if not, an IllegalArgumentException is
+   * thrown. Specify foregroundServiceType parameter as
+   * {@link android.content.pm.ServiceInfo#FOREGROUND_SERVICE_TYPE_MANIFEST} to use all flags that
+   * is specified in manifest attribute foregroundServiceType.</p>
+   *
+   * @param id The identifier for this notification as per
+   * {@link NotificationManager#notify(int, Notification)
+   * NotificationManager.notify(int, Notification)}; must not be 0.
+   * @param notification The Notification to be displayed.
+   * @param foregroundServiceType must be a subset flags of manifest attribute
+   * {@link android.R.attr#foregroundServiceType} flags.
+   * @throws IllegalArgumentException if param foregroundServiceType is not subset of manifest
+   *     attribute {@link android.R.attr#foregroundServiceType}.
+   * @see {@link android.content.pm.ServiceInfo} for the set of FOREGROUND_SERVICE_TYPE flags.
+   */
+    public final void startForeground(int id, @NonNull Notification notification,
+            int foregroundServiceType) {
+        try {
+            mActivityManager.setServiceForeground(
+                    new ComponentName(this, mClassName), mToken, id,
+                    notification, 0, foregroundServiceType);
         } catch (RemoteException ex) {
         }
     }
@@ -731,7 +767,8 @@
     public final void stopForeground(@StopForegroundFlags int flags) {
         try {
             mActivityManager.setServiceForeground(
-                    new ComponentName(this, mClassName), mToken, 0, null, flags);
+                    new ComponentName(this, mClassName), mToken, 0, null,
+                    flags, 0);
         } catch (RemoteException ex) {
         }
     }
diff --git a/core/java/android/app/SharedElementCallback.java b/core/java/android/app/SharedElementCallback.java
index 80fb805..9fabfde 100644
--- a/core/java/android/app/SharedElementCallback.java
+++ b/core/java/android/app/SharedElementCallback.java
@@ -23,6 +23,7 @@
 import android.graphics.RectF;
 import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
+import android.hardware.HardwareBuffer;
 import android.os.Bundle;
 import android.os.Parcelable;
 import android.transition.TransitionUtils;
@@ -234,7 +235,8 @@
                 return null;
             }
             if (bitmap == null) {
-                bitmap = Bitmap.createHardwareBitmap(buffer);
+                bitmap = Bitmap.wrapHardwareBuffer(HardwareBuffer.createFromGraphicBuffer(buffer),
+                                                   null);
             }
             ImageView imageView = new ImageView(context);
             view = imageView;
diff --git a/core/java/android/app/SystemServiceRegistry.java b/core/java/android/app/SystemServiceRegistry.java
index ee13164..31a3def 100644
--- a/core/java/android/app/SystemServiceRegistry.java
+++ b/core/java/android/app/SystemServiceRegistry.java
@@ -132,11 +132,13 @@
 import android.os.Build;
 import android.os.DeviceIdleManager;
 import android.os.DropBoxManager;
+import android.os.DynamicAndroidManager;
 import android.os.HardwarePropertiesManager;
 import android.os.IBatteryPropertiesRegistrar;
 import android.os.IBinder;
 import android.os.IDeviceIdleController;
 import android.os.IDumpstate;
+import android.os.IDynamicAndroidService;
 import android.os.IHardwarePropertiesManager;
 import android.os.IPowerManager;
 import android.os.IRecoverySystem;
@@ -1260,6 +1262,17 @@
                         return new RollbackManager(ctx.getOuterContext(),
                                 IRollbackManager.Stub.asInterface(b));
                     }});
+
+        registerService(Context.DYNAMIC_ANDROID_SERVICE, DynamicAndroidManager.class,
+                new CachedServiceFetcher<DynamicAndroidManager>() {
+                    @Override
+                    public DynamicAndroidManager createService(ContextImpl ctx)
+                            throws ServiceNotFoundException {
+                        IBinder b = ServiceManager.getServiceOrThrow(
+                                Context.DYNAMIC_ANDROID_SERVICE);
+                        return new DynamicAndroidManager(
+                                IDynamicAndroidService.Stub.asInterface(b));
+                    }});
         //CHECKSTYLE:ON IndentationCheck
     }
 
diff --git a/core/java/android/app/ZygotePreload.java b/core/java/android/app/ZygotePreload.java
new file mode 100644
index 0000000..a295af3
--- /dev/null
+++ b/core/java/android/app/ZygotePreload.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package android.app;
+
+import android.content.pm.ApplicationInfo;
+
+/**
+ * This is the interface to be implemented for the class that is specified by the
+ * {@link android.R.styleable#AndroidManifestApplication_zygotePreloadName
+ * android:zygotePreloadName} of the &lt;application&gt; tag.
+ *
+ * It is responsible for preloading application code and data, that will be shared by all
+ * isolated services that have the
+ * {@link android.R.styleable#AndroidManifestService_useAppZygote android:useAppZygote} attribute
+ * of the &lt;service&gt; tag set to <code>true</code>.
+ *
+ * Note that this implementations of this class must provide a default constructor with no
+ * arguments.
+ */
+public interface ZygotePreload {
+    /**
+     * This method is called once every time the Application Zygote is started. It is normally
+     * started the first time an isolated service that uses it is started. The Application Zygote
+     * will be stopped when all isolated services that use it are stopped.
+     *
+     * @param appInfo The ApplicationInfo object belonging to the application
+     */
+    void doPreload(ApplicationInfo appInfo);
+}
diff --git a/core/java/android/app/admin/DevicePolicyManager.java b/core/java/android/app/admin/DevicePolicyManager.java
index f0d0aad..428c9b0 100644
--- a/core/java/android/app/admin/DevicePolicyManager.java
+++ b/core/java/android/app/admin/DevicePolicyManager.java
@@ -3149,6 +3149,7 @@
      * @throws SecurityException if {@code admin} is not an active administrator or {@code admin}
      *             does not use {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD}
      */
+    @RequiresFeature(PackageManager.FEATURE_SECURE_LOCK_SCREEN)
     public void setPasswordHistoryLength(@NonNull ComponentName admin, int length) {
         if (mService != null) {
             try {
@@ -10590,14 +10591,19 @@
     }
 
     /**
-     * Returns if a package is whitelisted to access cross-profile calendar APIs.
+     * Returns if a package is allowed to access cross-profile calendar APIs.
+     *
+     * <p>A package is allowed to access cross-profile calendar APIs if it's allowed by
+     * admins via {@link #setCrossProfileCalendarPackages(ComponentName, Set)} and
+     * {@link android.provider.Settings.Secure#CROSS_PROFILE_CALENDAR_ENABLED}
+     * is turned on in the managed profile.
      *
      * <p>To query for a specific user, use
      * {@link Context#createPackageContextAsUser(String, int, UserHandle)} to create a context for
      * that user, and get a {@link DevicePolicyManager} from this context.
      *
      * @param packageName the name of the package
-     * @return {@code true} if the package is whitelisted to access cross-profile calendar APIs.
+     * @return {@code true} if the package is allowed to access cross-profile calendar APIs.
      * {@code false} otherwise.
      *
      * @see #setCrossProfileCalendarPackages(ComponentName, Set)
diff --git a/core/java/android/app/prediction/AppPredictionContext.java b/core/java/android/app/prediction/AppPredictionContext.java
index 87ccb66..2fbe6e36 100644
--- a/core/java/android/app/prediction/AppPredictionContext.java
+++ b/core/java/android/app/prediction/AppPredictionContext.java
@@ -18,6 +18,7 @@
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.annotation.SystemApi;
+import android.annotation.TestApi;
 import android.content.Context;
 import android.os.Bundle;
 import android.os.Parcel;
@@ -29,6 +30,7 @@
  * @hide
  */
 @SystemApi
+@TestApi
 public final class AppPredictionContext implements Parcelable {
 
     private final int mPredictedTargetCount;
@@ -73,6 +75,17 @@
     }
 
     @Override
+    public boolean equals(Object o) {
+        if (o == this) return true;
+        if (!getClass().equals(o != null ? o.getClass() : null)) return false;
+
+        AppPredictionContext other = (AppPredictionContext) o;
+        return mPredictedTargetCount == other.mPredictedTargetCount
+                && mUiSurface.equals(other.mUiSurface)
+                && mPackageName.equals(other.mPackageName);
+    }
+
+    @Override
     public int describeContents() {
         return 0;
     }
@@ -104,6 +117,7 @@
      * @hide
      */
     @SystemApi
+    @TestApi
     public static final class Builder {
 
         @NonNull
@@ -116,8 +130,12 @@
         private Bundle mExtras;
 
         /**
+         * TODO(b/123591863): Add java docs
+         *
          * @hide
          */
+        @SystemApi
+        @TestApi
         public Builder(@NonNull Context context) {
             mPackageName = context.getPackageName();
         }
diff --git a/core/java/android/app/prediction/AppPredictionManager.java b/core/java/android/app/prediction/AppPredictionManager.java
index f8578d4..99f78f1 100644
--- a/core/java/android/app/prediction/AppPredictionManager.java
+++ b/core/java/android/app/prediction/AppPredictionManager.java
@@ -17,6 +17,7 @@
 
 import android.annotation.NonNull;
 import android.annotation.SystemApi;
+import android.annotation.TestApi;
 import android.content.Context;
 
 import com.android.internal.util.Preconditions;
@@ -26,6 +27,7 @@
  * @hide
  */
 @SystemApi
+@TestApi
 public final class AppPredictionManager {
 
     private final Context mContext;
diff --git a/core/java/android/app/prediction/AppPredictionSessionId.java b/core/java/android/app/prediction/AppPredictionSessionId.java
index 1d7308e..1e76c24 100644
--- a/core/java/android/app/prediction/AppPredictionSessionId.java
+++ b/core/java/android/app/prediction/AppPredictionSessionId.java
@@ -17,6 +17,7 @@
 
 import android.annotation.NonNull;
 import android.annotation.SystemApi;
+import android.annotation.TestApi;
 import android.os.Parcel;
 import android.os.Parcelable;
 
@@ -26,6 +27,7 @@
  * @hide
  */
 @SystemApi
+@TestApi
 public final class AppPredictionSessionId implements Parcelable {
 
     private final String mId;
diff --git a/core/java/android/app/prediction/AppPredictor.java b/core/java/android/app/prediction/AppPredictor.java
index 2ddbd08..12d6ce3 100644
--- a/core/java/android/app/prediction/AppPredictor.java
+++ b/core/java/android/app/prediction/AppPredictor.java
@@ -19,6 +19,7 @@
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.annotation.SystemApi;
+import android.annotation.TestApi;
 import android.app.prediction.IPredictionCallback.Stub;
 import android.content.Context;
 import android.content.pm.ParceledListSlice;
@@ -63,6 +64,7 @@
  * @hide
  */
 @SystemApi
+@TestApi
 public final class AppPredictor {
 
     private static final String TAG = AppPredictor.class.getSimpleName();
@@ -102,6 +104,10 @@
      * Notifies the prediction service of an app target event.
      */
     public void notifyAppTargetEvent(@NonNull AppTargetEvent event) {
+        if (mIsClosed.get()) {
+            throw new IllegalStateException("This client has already been destroyed.");
+        }
+
         try {
             mPredictionManager.notifyAppTargetEvent(mSessionId, event);
         } catch (RemoteException e) {
@@ -114,6 +120,10 @@
      */
     public void notifyLocationShown(@NonNull String launchLocation,
             @NonNull List<AppTargetId> targetIds) {
+        if (mIsClosed.get()) {
+            throw new IllegalStateException("This client has already been destroyed.");
+        }
+
         try {
             mPredictionManager.notifyLocationShown(mSessionId, launchLocation,
                     new ParceledListSlice<>(targetIds));
@@ -130,6 +140,10 @@
      */
     public void registerPredictionUpdates(@NonNull @CallbackExecutor Executor callbackExecutor,
             @NonNull AppPredictor.Callback callback) {
+        if (mIsClosed.get()) {
+            throw new IllegalStateException("This client has already been destroyed.");
+        }
+
         if (mRegisteredCallbacks.containsKey(callback)) {
             // Skip if this callback is already registered
             return;
@@ -149,6 +163,10 @@
      * callback until the callback is re-registered.
      */
     public void unregisterPredictionUpdates(@NonNull AppPredictor.Callback callback) {
+        if (mIsClosed.get()) {
+            throw new IllegalStateException("This client has already been destroyed.");
+        }
+
         if (!mRegisteredCallbacks.containsKey(callback)) {
             // Skip if this callback was never registered
             return;
@@ -168,6 +186,10 @@
      * @see Callback#onTargetsAvailable(List)
      */
     public void requestPredictionUpdate() {
+        if (mIsClosed.get()) {
+            throw new IllegalStateException("This client has already been destroyed.");
+        }
+
         try {
             mPredictionManager.requestPredictionUpdate(mSessionId);
         } catch (RemoteException e) {
@@ -182,6 +204,10 @@
     @Nullable
     public void sortTargets(@NonNull List<AppTarget> targets,
             @NonNull Executor callbackExecutor, @NonNull Consumer<List<AppTarget>> callback) {
+        if (mIsClosed.get()) {
+            throw new IllegalStateException("This client has already been destroyed.");
+        }
+
         try {
             mPredictionManager.sortAppTargets(mSessionId, new ParceledListSlice(targets),
                     new CallbackWrapper(callbackExecutor, callback));
@@ -206,6 +232,8 @@
             } catch (RemoteException e) {
                 Log.e(TAG, "Failed to notify app target event", e);
             }
+        } else {
+            throw new IllegalStateException("This client has already been destroyed.");
         }
     }
 
@@ -222,6 +250,16 @@
     }
 
     /**
+     * TODO(b/123591863): Add java docs
+     *
+     * @hide
+     */
+    @TestApi
+    public AppPredictionSessionId getSessionId() {
+        return mSessionId;
+    }
+
+    /**
      * Callback for receiving prediction updates.
      */
     public interface Callback {
diff --git a/core/java/android/app/prediction/AppTarget.java b/core/java/android/app/prediction/AppTarget.java
index 99c1c44..b924cec 100644
--- a/core/java/android/app/prediction/AppTarget.java
+++ b/core/java/android/app/prediction/AppTarget.java
@@ -18,6 +18,7 @@
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.annotation.SystemApi;
+import android.annotation.TestApi;
 import android.content.pm.ShortcutInfo;
 import android.os.Parcel;
 import android.os.Parcelable;
@@ -30,6 +31,7 @@
  * @hide
  */
 @SystemApi
+@TestApi
 public final class AppTarget implements Parcelable {
 
     private final AppTargetId mId;
@@ -42,8 +44,12 @@
     private int mRank;
 
     /**
+     * TODO(b/123591863): Add java docs
+     *
      * @hide
      */
+    @SystemApi
+    @TestApi
     public AppTarget(@NonNull AppTargetId id, @NonNull String packageName,
             @Nullable String className, @NonNull UserHandle user) {
         mId = id;
@@ -55,15 +61,19 @@
     }
 
     /**
+     * TODO(b/123591863): Add java docs
+     *
      * @hide
      */
-    public AppTarget(@NonNull AppTargetId id, @NonNull ShortcutInfo shortcutInfo) {
+    @SystemApi
+    public AppTarget(@NonNull AppTargetId id, @NonNull ShortcutInfo shortcutInfo,
+            @Nullable String className) {
         mId = id;
         mShortcutInfo = Preconditions.checkNotNull(shortcutInfo);
 
         mPackageName = mShortcutInfo.getPackage();
         mUser = mShortcutInfo.getUserHandle();
-        mClassName = null;
+        mClassName = className;
     }
 
     private AppTarget(Parcel parcel) {
@@ -71,13 +81,12 @@
         mShortcutInfo = parcel.readTypedObject(ShortcutInfo.CREATOR);
         if (mShortcutInfo == null) {
             mPackageName = parcel.readString();
-            mClassName = parcel.readString();
             mUser = UserHandle.of(parcel.readInt());
         } else {
             mPackageName = mShortcutInfo.getPackage();
             mUser = mShortcutInfo.getUserHandle();
-            mClassName = null;
         }
+        mClassName = parcel.readString();
         mRank = parcel.readInt();
     }
 
@@ -129,11 +138,32 @@
         mRank = rank;
     }
 
+    /**
+     * Returns the rank for the target.
+     */
     public int getRank() {
         return mRank;
     }
 
     @Override
+    public boolean equals(Object o) {
+        if (!getClass().equals(o != null ? o.getClass() : null)) return false;
+
+        AppTarget other = (AppTarget) o;
+        boolean sameClassName = (mClassName == null && other.mClassName == null)
+                || (mClassName != null && mClassName.equals(other.mClassName));
+        boolean sameShortcutInfo = (mShortcutInfo == null && other.mShortcutInfo == null)
+                || (mShortcutInfo != null && other.mShortcutInfo != null
+                        && mShortcutInfo.getId() == other.mShortcutInfo.getId());
+        return mId.equals(other.mId)
+                && mPackageName.equals(other.mPackageName)
+                && sameClassName
+                && mUser.equals(other.mUser)
+                && sameShortcutInfo
+                && mRank == other.mRank;
+    }
+
+    @Override
     public int describeContents() {
         return 0;
     }
@@ -144,9 +174,9 @@
         dest.writeTypedObject(mShortcutInfo, flags);
         if (mShortcutInfo == null) {
             dest.writeString(mPackageName);
-            dest.writeString(mClassName);
             dest.writeInt(mUser.getIdentifier());
         }
+        dest.writeString(mClassName);
         dest.writeInt(mRank);
     }
 
diff --git a/core/java/android/app/prediction/AppTargetEvent.java b/core/java/android/app/prediction/AppTargetEvent.java
index 18317e1..37e41de 100644
--- a/core/java/android/app/prediction/AppTargetEvent.java
+++ b/core/java/android/app/prediction/AppTargetEvent.java
@@ -19,6 +19,7 @@
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.annotation.SystemApi;
+import android.annotation.TestApi;
 import android.os.Parcel;
 import android.os.Parcelable;
 
@@ -30,6 +31,7 @@
  * @hide
  */
 @SystemApi
+@TestApi
 public final class AppTargetEvent implements Parcelable {
 
     /**
@@ -96,6 +98,16 @@
     }
 
     @Override
+    public boolean equals(Object o) {
+        if (!getClass().equals(o != null ? o.getClass() : null)) return false;
+
+        AppTargetEvent other = (AppTargetEvent) o;
+        return mTarget.equals(other.mTarget)
+                && mLocation.equals(other.mLocation)
+                && mAction == other.mAction;
+    }
+
+    @Override
     public int describeContents() {
         return 0;
     }
@@ -126,6 +138,7 @@
      * @hide
      */
     @SystemApi
+    @TestApi
     public static final class Builder {
         private AppTarget mTarget;
         private String mLocation;
diff --git a/core/java/android/app/prediction/AppTargetId.java b/core/java/android/app/prediction/AppTargetId.java
index 0b8fb47..639ba78 100644
--- a/core/java/android/app/prediction/AppTargetId.java
+++ b/core/java/android/app/prediction/AppTargetId.java
@@ -17,6 +17,7 @@
 
 import android.annotation.NonNull;
 import android.annotation.SystemApi;
+import android.annotation.TestApi;
 import android.os.Parcel;
 import android.os.Parcelable;
 
@@ -25,14 +26,19 @@
  * @hide
  */
 @SystemApi
+@TestApi
 public final class AppTargetId implements Parcelable {
 
     @NonNull
     private final String mId;
 
     /**
+     * TODO(b/123591863): Add java docs
+     *
      * @hide
      */
+    @SystemApi
+    @TestApi
     public AppTargetId(@NonNull String id) {
         mId = id;
     }
diff --git a/core/java/android/content/Context.java b/core/java/android/content/Context.java
index adc1bf2..97323ca 100644
--- a/core/java/android/content/Context.java
+++ b/core/java/android/content/Context.java
@@ -4585,6 +4585,14 @@
      */
     public static final String TELEPHONY_RCS_SERVICE = "ircs";
 
+     /**
+     * Use with {@link #getSystemService(String)} to retrieve an
+     * {@link android.os.DynamicAndroidManager}.
+     * @hide
+     */
+    @SystemApi
+    public static final String DYNAMIC_ANDROID_SERVICE = "dynamic_android";
+
     /**
      * Determine whether the given permission is allowed for a particular
      * process and user ID running in the system.
diff --git a/core/java/android/content/pm/ApplicationInfo.java b/core/java/android/content/pm/ApplicationInfo.java
index 5d6d144..b27c5dc 100644
--- a/core/java/android/content/pm/ApplicationInfo.java
+++ b/core/java/android/content/pm/ApplicationInfo.java
@@ -640,19 +640,15 @@
     public static final int PRIVATE_FLAG_HAS_FRAGILE_USER_DATA = 1 << 24;
 
     /**
-     * Indicate whether this application prefers code integrity, that is, run only code that is
-     * signed. This requires android:extractNativeLibs to be "false", as well as .dex and .so (if
-     * any) stored uncompressed inside the APK, which is signed. At run time, the implications
-     * include:
-     *
-     * <ul>
-     * <li>ART will JIT the dex code directly from the APK. There may be performance characteristic
-     * changes depend on the actual workload.
-     * </ul>
+     * Indicates whether this application wants to use the embedded dex in the APK, rather than
+     * extracted or locally compiled variants. This keeps the dex code protected by the APK
+     * signature. Such apps will always run in JIT mode (same when they are first installed), and
+     * the system will never generate ahead-of-time compiled code for them. Depending on the app's
+     * workload, there may be some run time performance change, noteably the cold start time.
      *
      * @hide
      */
-    public static final int PRIVATE_FLAG_PREFER_CODE_INTEGRITY = 1 << 25;
+    public static final int PRIVATE_FLAG_USE_EMBEDDED_DEX = 1 << 25;
 
     /** @hide */
     @IntDef(flag = true, prefix = { "PRIVATE_FLAG_" }, value = {
@@ -669,7 +665,7 @@
             PRIVATE_FLAG_ISOLATED_SPLIT_LOADING,
             PRIVATE_FLAG_OEM,
             PRIVATE_FLAG_PARTIALLY_DIRECT_BOOT_AWARE,
-            PRIVATE_FLAG_PREFER_CODE_INTEGRITY,
+            PRIVATE_FLAG_USE_EMBEDDED_DEX,
             PRIVATE_FLAG_PRIVILEGED,
             PRIVATE_FLAG_PRODUCT,
             PRIVATE_FLAG_PRODUCT_SERVICES,
@@ -1192,6 +1188,9 @@
     /** @hide */
     public boolean hiddenUntilInstalled;
 
+    /** @hide */
+    public String zygotePreloadName;
+
     /**
      * Represents the default policy. The actual policy used will depend on other properties of
      * the application, e.g. the target SDK version.
@@ -1533,6 +1532,7 @@
         compileSdkVersionCodename = orig.compileSdkVersionCodename;
         mHiddenApiPolicy = orig.mHiddenApiPolicy;
         hiddenUntilInstalled = orig.hiddenUntilInstalled;
+        zygotePreloadName = orig.zygotePreloadName;
     }
 
     public String toString() {
@@ -1609,6 +1609,7 @@
         dest.writeString(appComponentFactory);
         dest.writeInt(mHiddenApiPolicy);
         dest.writeInt(hiddenUntilInstalled ? 1 : 0);
+        dest.writeString(zygotePreloadName);
     }
 
     public static final Parcelable.Creator<ApplicationInfo> CREATOR
@@ -1682,6 +1683,7 @@
         appComponentFactory = source.readString();
         mHiddenApiPolicy = source.readInt();
         hiddenUntilInstalled = source.readInt() != 0;
+        zygotePreloadName = source.readString();
     }
 
     /**
@@ -1956,8 +1958,8 @@
     }
 
     /** @hide */
-    public boolean isCodeIntegrityPreferred() {
-        return (privateFlags & PRIVATE_FLAG_PREFER_CODE_INTEGRITY) != 0;
+    public boolean isEmbeddedDexUsed() {
+        return (privateFlags & PRIVATE_FLAG_USE_EMBEDDED_DEX) != 0;
     }
 
     /**
diff --git a/core/java/android/content/pm/ILauncherApps.aidl b/core/java/android/content/pm/ILauncherApps.aidl
index d1bc377..50bb3c7 100644
--- a/core/java/android/content/pm/ILauncherApps.aidl
+++ b/core/java/android/content/pm/ILauncherApps.aidl
@@ -24,6 +24,8 @@
 import android.content.pm.ApplicationInfo;
 import android.content.pm.IOnAppsChangedListener;
 import android.content.pm.LauncherApps;
+import android.content.pm.IPackageInstallerCallback;
+import android.content.pm.PackageInstaller;
 import android.content.pm.ParceledListSlice;
 import android.content.pm.ResolveInfo;
 import android.content.pm.ShortcutInfo;
@@ -44,6 +46,9 @@
             String callingPackage, String packageName, in UserHandle user);
     ActivityInfo resolveActivity(
             String callingPackage, in ComponentName component, in UserHandle user);
+    void startSessionDetailsActivityAsUser(in IApplicationThread caller, String callingPackage,
+                in PackageInstaller.SessionInfo sessionInfo, in Rect sourceBounds, in Bundle opts,
+                in UserHandle user);
     void startActivityAsUser(in IApplicationThread caller, String callingPackage,
             in ComponentName component, in Rect sourceBounds,
             in Bundle opts, in UserHandle user);
@@ -79,4 +84,9 @@
             String callingPackage, String packageName, in UserHandle user);
     IntentSender getShortcutConfigActivityIntent(String callingPackage, in ComponentName component,
             in UserHandle user);
+
+    // Unregister is performed using package installer
+    void registerPackageInstallerCallback(String callingPackage,
+            in IPackageInstallerCallback callback);
+    ParceledListSlice getAllSessions(String callingPackage);
 }
diff --git a/core/java/android/content/pm/LauncherApps.java b/core/java/android/content/pm/LauncherApps.java
index 98dd9b3..b0d16cd 100644
--- a/core/java/android/content/pm/LauncherApps.java
+++ b/core/java/android/content/pm/LauncherApps.java
@@ -16,6 +16,7 @@
 
 package android.content.pm;
 
+import android.annotation.CallbackExecutor;
 import android.annotation.IntDef;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
@@ -32,6 +33,9 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentSender;
+import android.content.pm.PackageInstaller.SessionCallback;
+import android.content.pm.PackageInstaller.SessionCallbackDelegate;
+import android.content.pm.PackageInstaller.SessionInfo;
 import android.content.pm.PackageManager.ApplicationInfoFlags;
 import android.content.pm.PackageManager.NameNotFoundException;
 import android.content.res.Resources;
@@ -65,7 +69,9 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.Iterator;
 import java.util.List;
+import java.util.concurrent.Executor;
 
 /**
  * Class for retrieving a list of launchable activities for the current user and any associated
@@ -154,8 +160,8 @@
     private final PackageManager mPm;
     private final UserManager mUserManager;
 
-    private List<CallbackMessageHandler> mCallbacks
-            = new ArrayList<CallbackMessageHandler>();
+    private final List<CallbackMessageHandler> mCallbacks = new ArrayList<>();
+    private final List<SessionCallbackDelegate> mDelegates = new ArrayList<>();
 
     /**
      * Callbacks for package changes to this and related managed profiles.
@@ -572,6 +578,24 @@
     }
 
     /**
+     * Starts an activity to show the details of the specified session.
+     *
+     * @param sessionInfo The SessionInfo of the session
+     * @param sourceBounds The Rect containing the source bounds of the clicked icon
+     * @param opts Options to pass to startActivity
+     */
+    public void startPackageInstallerSessionDetailsActivity(SessionInfo sessionInfo,
+            Rect sourceBounds, Bundle opts) {
+        try {
+            mService.startSessionDetailsActivityAsUser(mContext.getIApplicationThread(),
+                    mContext.getPackageName(), sessionInfo, sourceBounds, opts,
+                    sessionInfo.getUser());
+        } catch (RemoteException re) {
+            throw re.rethrowFromSystemServer();
+        }
+    }
+
+    /**
      * Starts the settings activity to show the application details for a
      * package in the specified profile.
      *
@@ -1131,7 +1155,7 @@
     }
 
     /**
-     * Registers a callback for changes to packages in current and managed profiles.
+     * Registers a callback for changes to packages in this user and managed profiles.
      *
      * @param callback The callback to register.
      */
@@ -1140,7 +1164,7 @@
     }
 
     /**
-     * Registers a callback for changes to packages in current and managed profiles.
+     * Registers a callback for changes to packages in this user and managed profiles.
      *
      * @param callback The callback to register.
      * @param handler that should be used to post callbacks on, may be null.
@@ -1446,6 +1470,64 @@
     }
 
     /**
+     * Register a callback to watch for session lifecycle events in this user and managed profiles.
+     * @param callback The callback to register.
+     * @param executor {@link Executor} to handle the callbacks, cannot be null.
+     *
+     * @see PackageInstaller#registerSessionCallback(SessionCallback)
+     */
+    public void registerPackageInstallerSessionCallback(
+            @NonNull @CallbackExecutor Executor executor, @NonNull SessionCallback callback) {
+        if (executor == null) {
+            throw new NullPointerException("Executor must not be null");
+        }
+
+        synchronized (mDelegates) {
+            final SessionCallbackDelegate delegate = new SessionCallbackDelegate(callback,
+                    executor);
+            try {
+                mService.registerPackageInstallerCallback(mContext.getPackageName(),
+                        delegate);
+            } catch (RemoteException e) {
+                throw e.rethrowFromSystemServer();
+            }
+            mDelegates.add(delegate);
+        }
+    }
+
+    /**
+     * Unregisters a callback that was previously registered.
+     *
+     * @param callback The callback to unregister.
+     * @see #registerPackageInstallerSessionCallback(Executor, SessionCallback)
+     */
+    public void unregisterPackageInstallerSessionCallback(SessionCallback callback) {
+        synchronized (mDelegates) {
+            for (Iterator<SessionCallbackDelegate> i = mDelegates.iterator(); i.hasNext();) {
+                final SessionCallbackDelegate delegate = i.next();
+                if (delegate.mCallback == callback) {
+                    mPm.getPackageInstaller().unregisterSessionCallback(delegate.mCallback);
+                    i.remove();
+                }
+            }
+        }
+    }
+
+    /**
+     * Return list of all known install sessions in this user and managed profiles, regardless
+     * of the installer.
+     *
+     * @see PackageInstaller#getAllSessions()
+     */
+    public @NonNull List<SessionInfo> getAllPackageInstallerSessions() {
+        try {
+            return mService.getAllSessions(mContext.getPackageName()).getList();
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+
+    /**
      * A helper method to extract a {@link PinItemRequest} set to
      * the {@link #EXTRA_PIN_ITEM_REQUEST} extra.
      */
diff --git a/core/java/android/content/pm/PackageInstaller.java b/core/java/android/content/pm/PackageInstaller.java
index 2dc014c..8b8f3e5 100644
--- a/core/java/android/content/pm/PackageInstaller.java
+++ b/core/java/android/content/pm/PackageInstaller.java
@@ -36,20 +36,21 @@
 import android.os.Build;
 import android.os.FileBridge;
 import android.os.Handler;
-import android.os.Looper;
-import android.os.Message;
+import android.os.HandlerExecutor;
 import android.os.Parcel;
 import android.os.ParcelFileDescriptor;
 import android.os.Parcelable;
 import android.os.ParcelableException;
 import android.os.RemoteException;
 import android.os.SystemProperties;
+import android.os.UserHandle;
 import android.system.ErrnoException;
 import android.system.Os;
 import android.util.ExceptionUtils;
 
 import com.android.internal.util.IndentingPrintWriter;
 import com.android.internal.util.Preconditions;
+import com.android.internal.util.function.pooled.PooledLambda;
 
 import java.io.Closeable;
 import java.io.IOException;
@@ -61,6 +62,7 @@
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.concurrent.Executor;
 
 /**
  * Offers the ability to install, upgrade, and remove applications on the
@@ -659,8 +661,7 @@
     }
 
     /** {@hide} */
-    private static class SessionCallbackDelegate extends IPackageInstallerCallback.Stub implements
-            Handler.Callback {
+    static class SessionCallbackDelegate extends IPackageInstallerCallback.Stub {
         private static final int MSG_SESSION_CREATED = 1;
         private static final int MSG_SESSION_BADGING_CHANGED = 2;
         private static final int MSG_SESSION_ACTIVE_CHANGED = 3;
@@ -668,63 +669,41 @@
         private static final int MSG_SESSION_FINISHED = 5;
 
         final SessionCallback mCallback;
-        final Handler mHandler;
+        final Executor mExecutor;
 
-        public SessionCallbackDelegate(SessionCallback callback, Looper looper) {
+        SessionCallbackDelegate(SessionCallback callback, Executor executor) {
             mCallback = callback;
-            mHandler = new Handler(looper, this);
-        }
-
-        @Override
-        public boolean handleMessage(Message msg) {
-            final int sessionId = msg.arg1;
-            switch (msg.what) {
-                case MSG_SESSION_CREATED:
-                    mCallback.onCreated(sessionId);
-                    return true;
-                case MSG_SESSION_BADGING_CHANGED:
-                    mCallback.onBadgingChanged(sessionId);
-                    return true;
-                case MSG_SESSION_ACTIVE_CHANGED:
-                    final boolean active = msg.arg2 != 0;
-                    mCallback.onActiveChanged(sessionId, active);
-                    return true;
-                case MSG_SESSION_PROGRESS_CHANGED:
-                    mCallback.onProgressChanged(sessionId, (float) msg.obj);
-                    return true;
-                case MSG_SESSION_FINISHED:
-                    mCallback.onFinished(sessionId, msg.arg2 != 0);
-                    return true;
-            }
-            return false;
+            mExecutor = executor;
         }
 
         @Override
         public void onSessionCreated(int sessionId) {
-            mHandler.obtainMessage(MSG_SESSION_CREATED, sessionId, 0).sendToTarget();
+            mExecutor.execute(PooledLambda.obtainRunnable(SessionCallback::onCreated, mCallback,
+                    sessionId).recycleOnUse());
         }
 
         @Override
         public void onSessionBadgingChanged(int sessionId) {
-            mHandler.obtainMessage(MSG_SESSION_BADGING_CHANGED, sessionId, 0).sendToTarget();
+            mExecutor.execute(PooledLambda.obtainRunnable(SessionCallback::onBadgingChanged,
+                    mCallback, sessionId).recycleOnUse());
         }
 
         @Override
         public void onSessionActiveChanged(int sessionId, boolean active) {
-            mHandler.obtainMessage(MSG_SESSION_ACTIVE_CHANGED, sessionId, active ? 1 : 0)
-                    .sendToTarget();
+            mExecutor.execute(PooledLambda.obtainRunnable(SessionCallback::onActiveChanged,
+                    mCallback, sessionId, active).recycleOnUse());
         }
 
         @Override
         public void onSessionProgressChanged(int sessionId, float progress) {
-            mHandler.obtainMessage(MSG_SESSION_PROGRESS_CHANGED, sessionId, 0, progress)
-                    .sendToTarget();
+            mExecutor.execute(PooledLambda.obtainRunnable(SessionCallback::onProgressChanged,
+                    mCallback, sessionId, progress).recycleOnUse());
         }
 
         @Override
         public void onSessionFinished(int sessionId, boolean success) {
-            mHandler.obtainMessage(MSG_SESSION_FINISHED, sessionId, success ? 1 : 0)
-                    .sendToTarget();
+            mExecutor.execute(PooledLambda.obtainRunnable(SessionCallback::onFinished,
+                    mCallback, sessionId, success).recycleOnUse());
         }
     }
 
@@ -758,7 +737,7 @@
     public void registerSessionCallback(@NonNull SessionCallback callback, @NonNull Handler handler) {
         synchronized (mDelegates) {
             final SessionCallbackDelegate delegate = new SessionCallbackDelegate(callback,
-                    handler.getLooper());
+                    new HandlerExecutor(handler));
             try {
                 mInstaller.registerCallback(delegate, mUserId);
             } catch (RemoteException e) {
@@ -1311,6 +1290,28 @@
             isStaged = source.readBoolean();
         }
 
+        /** {@hide} */
+        public SessionParams copy() {
+            SessionParams ret = new SessionParams(mode);
+            ret.installFlags = installFlags;
+            ret.installLocation = installLocation;
+            ret.installReason = installReason;
+            ret.sizeBytes = sizeBytes;
+            ret.appPackageName = appPackageName;
+            ret.appIcon = appIcon;  // not a copy.
+            ret.appLabel = appLabel;
+            ret.originatingUri = originatingUri;  // not a copy, but immutable.
+            ret.originatingUid = originatingUid;
+            ret.referrerUri = referrerUri;  // not a copy, but immutable.
+            ret.abiOverride = abiOverride;
+            ret.volumeUuid = volumeUuid;
+            ret.grantedRuntimePermissions = grantedRuntimePermissions;
+            ret.installerPackageName = installerPackageName;
+            ret.isMultiPackage = isMultiPackage;
+            ret.isStaged = isStaged;
+            return ret;
+        }
+
         /**
          * Check if there are hidden options set.
          *
@@ -1649,6 +1650,8 @@
         @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
         public int sessionId;
         /** {@hide} */
+        public int userId;
+        /** {@hide} */
         @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
         public String installerPackageName;
         /** {@hide} */
@@ -1720,6 +1723,7 @@
         /** {@hide} */
         public SessionInfo(Parcel source) {
             sessionId = source.readInt();
+            userId = source.readInt();
             installerPackageName = source.readString();
             resolvedBaseCodePath = source.readString();
             progress = source.readFloat();
@@ -1761,6 +1765,13 @@
         }
 
         /**
+         * Return the user associated with this session.
+         */
+        public UserHandle getUser() {
+            return new UserHandle(userId);
+        }
+
+        /**
          * Return the package name of the app that owns this session.
          */
         public @Nullable String getInstallerPackageName() {
@@ -2091,6 +2102,7 @@
         @Override
         public void writeToParcel(Parcel dest, int flags) {
             dest.writeInt(sessionId);
+            dest.writeInt(userId);
             dest.writeString(installerPackageName);
             dest.writeString(resolvedBaseCodePath);
             dest.writeFloat(progress);
diff --git a/core/java/android/content/pm/PackageManager.java b/core/java/android/content/pm/PackageManager.java
index d88f36c..84f87ff 100644
--- a/core/java/android/content/pm/PackageManager.java
+++ b/core/java/android/content/pm/PackageManager.java
@@ -552,8 +552,6 @@
      * currently active, i.e. mounted and available to other processes of the OS.
      * In particular, this flag alone will not match APEX files that are staged
      * for activation at next reboot.
-     * TODO(b/119767311): include uninstalled/inactive APEX if
-     *                    MATCH_UNINSTALLED_PACKAGES is set.
      */
     public static final int MATCH_APEX = 0x40000000;
 
diff --git a/core/java/android/content/pm/PackageParser.java b/core/java/android/content/pm/PackageParser.java
index eb59cfc..f10f0c6 100644
--- a/core/java/android/content/pm/PackageParser.java
+++ b/core/java/android/content/pm/PackageParser.java
@@ -475,7 +475,7 @@
         public final boolean extractNativeLibs;
         public final boolean isolatedSplits;
         public final boolean isSplitRequired;
-        public final boolean preferCodeIntegrity;
+        public final boolean useEmbeddedDex;
 
         public ApkLite(String codePath, String packageName, String splitName,
                 boolean isFeatureSplit,
@@ -484,7 +484,7 @@
                 int revisionCode, int installLocation, List<VerifierInfo> verifiers,
                 SigningDetails signingDetails, boolean coreApp,
                 boolean debuggable, boolean multiArch, boolean use32bitAbi,
-                boolean preferCodeIntegrity, boolean extractNativeLibs, boolean isolatedSplits) {
+                boolean useEmbeddedDex, boolean extractNativeLibs, boolean isolatedSplits) {
             this.codePath = codePath;
             this.packageName = packageName;
             this.splitName = splitName;
@@ -501,7 +501,7 @@
             this.debuggable = debuggable;
             this.multiArch = multiArch;
             this.use32bitAbi = use32bitAbi;
-            this.preferCodeIntegrity = preferCodeIntegrity;
+            this.useEmbeddedDex = useEmbeddedDex;
             this.extractNativeLibs = extractNativeLibs;
             this.isolatedSplits = isolatedSplits;
             this.isSplitRequired = isSplitRequired;
@@ -1726,7 +1726,7 @@
         boolean isolatedSplits = false;
         boolean isFeatureSplit = false;
         boolean isSplitRequired = false;
-        boolean preferCodeIntegrity = false;
+        boolean useEmbeddedDex = false;
         String configForSplit = null;
         String usesSplitName = null;
 
@@ -1790,8 +1790,8 @@
                         extractNativeLibsProvided = Boolean.valueOf(
                                 attrs.getAttributeBooleanValue(i, true));
                     }
-                    if ("preferCodeIntegrity".equals(attr)) {
-                        preferCodeIntegrity = attrs.getAttributeBooleanValue(i, false);
+                    if ("useEmbeddedDex".equals(attr)) {
+                        useEmbeddedDex = attrs.getAttributeBooleanValue(i, false);
                     }
                 }
             } else if (TAG_USES_SPLIT.equals(parser.getName())) {
@@ -1851,16 +1851,10 @@
         final boolean extractNativeLibs = (extractNativeLibsProvided != null)
                 ? extractNativeLibsProvided : extractNativeLibsDefault;
 
-        if (preferCodeIntegrity && extractNativeLibs) {
-            throw new PackageParserException(
-                    PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED,
-                    "Can't request both preferCodeIntegrity and extractNativeLibs");
-        }
-
         return new ApkLite(codePath, packageSplit.first, packageSplit.second, isFeatureSplit,
                 configForSplit, usesSplitName, isSplitRequired, versionCode, versionCodeMajor,
                 revisionCode, installLocation, verifiers, signingDetails, coreApp, debuggable,
-                multiArch, use32bitAbi, preferCodeIntegrity, extractNativeLibs, isolatedSplits);
+                multiArch, use32bitAbi, useEmbeddedDex, extractNativeLibs, isolatedSplits);
     }
 
     /**
@@ -3789,9 +3783,9 @@
         }
 
         if (sa.getBoolean(
-                R.styleable.AndroidManifestApplication_preferCodeIntegrity,
+                R.styleable.AndroidManifestApplication_useEmbeddedDex,
                 false)) {
-            ai.privateFlags |= ApplicationInfo.PRIVATE_FLAG_PREFER_CODE_INTEGRITY;
+            ai.privateFlags |= ApplicationInfo.PRIVATE_FLAG_USE_EMBEDDED_DEX;
         }
 
         if (sa.getBoolean(
@@ -3907,6 +3901,9 @@
             outError[0] = "Invalid class loader name: " + ai.classLoaderName;
         }
 
+        ai.zygotePreloadName = sa.getString(
+                com.android.internal.R.styleable.AndroidManifestApplication_zygotePreloadName);
+
         sa.recycle();
 
         if (outError[0] != null) {
@@ -5573,7 +5570,7 @@
 
         s.info.mForegroundServiceType = sa.getInt(
                 com.android.internal.R.styleable.AndroidManifestService_foregroundServiceType,
-                ServiceInfo.FOREGROUND_SERVICE_TYPE_UNSPECIFIED);
+                ServiceInfo.FOREGROUND_SERVICE_TYPE_NONE);
 
         s.info.flags = 0;
         if (sa.getBoolean(
diff --git a/core/java/android/content/pm/ServiceInfo.java b/core/java/android/content/pm/ServiceInfo.java
index 8300c0c..60475de 100644
--- a/core/java/android/content/pm/ServiceInfo.java
+++ b/core/java/android/content/pm/ServiceInfo.java
@@ -101,77 +101,73 @@
     /**
      * The default foreground service type if not been set in manifest file.
      */
-    public static final int FOREGROUND_SERVICE_TYPE_UNSPECIFIED = 0;
+    public static final int FOREGROUND_SERVICE_TYPE_NONE = 0;
 
     /**
-     * Constant corresponding to <code>sync</code> in
+     * Constant corresponding to <code>dataSync</code> in
      * the {@link android.R.attr#foregroundServiceType} attribute.
      * Data(photo, file, account) upload/download, backup/restore, import/export, fetch,
      * transfer over network between device and cloud.
      */
-    public static final int FOREGROUND_SERVICE_TYPE_SYNC = 1;
+    public static final int FOREGROUND_SERVICE_TYPE_DATA_SYNC = 1 << 0;
 
     /**
-     * Constant corresponding to <code>mediaPlay</code> in
+     * Constant corresponding to <code>mediaPlayback</code> in
      * the {@link android.R.attr#foregroundServiceType} attribute.
-     * Music, video, news or other media play.
+     * Music, video, news or other media playback.
      */
-    public static final int FOREGROUND_SERVICE_TYPE_MEDIA_PLAY = 2;
+    public static final int FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK = 1 << 1;
 
     /**
      * Constant corresponding to <code>phoneCall</code> in
      * the {@link android.R.attr#foregroundServiceType} attribute.
      * Ongoing phone call or video conference.
      */
-    public static final int FOREGROUND_SERVICE_TYPE_PHONE_CALL = 3;
+    public static final int FOREGROUND_SERVICE_TYPE_PHONE_CALL = 1 << 2;
 
     /**
      * Constant corresponding to <code>location</code> in
      * the {@link android.R.attr#foregroundServiceType} attribute.
      * GPS, map, navigation location update.
      */
-    public static final int FOREGROUND_SERVICE_TYPE_LOCATION = 4;
+    public static final int FOREGROUND_SERVICE_TYPE_LOCATION = 1 << 3;
 
     /**
-     * Constant corresponding to <code>deviceCompanion</code> in
+     * Constant corresponding to <code>connectedDevice</code> in
      * the {@link android.R.attr#foregroundServiceType} attribute.
      * Auto, bluetooth, TV or other devices connection, monitoring and interaction.
      */
-    public static final int FOREGROUND_SERVICE_TYPE_DEVICE_COMPANION = 5;
+    public static final int FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE = 1 << 4;
 
     /**
-     * Constant corresponding to <code>ongoingProcess</code> in
-     * the {@link android.R.attr#foregroundServiceType} attribute.
-     * Process that should not be interrupted, including installation, setup, photo
-     * compression etc.
+     * A special value indicates to use all types set in manifest file.
      */
-    public static final int FOREGROUND_SERVICE_TYPE_ONGOING_PROCESS = 6;
+    public static final int FOREGROUND_SERVICE_TYPE_MANIFEST = -1;
 
     /**
-     * The enumeration of values for foreground service type.
+     * The set of flags for foreground service type.
      * The foreground service type is set in {@link android.R.attr#foregroundServiceType}
      * attribute.
      * @hide
      */
-    @IntDef(flag = false, prefix = { "FOREGROUND_SERVICE_TYPE_" }, value = {
-            FOREGROUND_SERVICE_TYPE_UNSPECIFIED,
-            FOREGROUND_SERVICE_TYPE_SYNC,
-            FOREGROUND_SERVICE_TYPE_MEDIA_PLAY,
+    @IntDef(flag = true, prefix = { "FOREGROUND_SERVICE_TYPE_" }, value = {
+            FOREGROUND_SERVICE_TYPE_NONE,
+            FOREGROUND_SERVICE_TYPE_DATA_SYNC,
+            FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK,
             FOREGROUND_SERVICE_TYPE_PHONE_CALL,
             FOREGROUND_SERVICE_TYPE_LOCATION,
-            FOREGROUND_SERVICE_TYPE_DEVICE_COMPANION,
-            FOREGROUND_SERVICE_TYPE_ONGOING_PROCESS
+            FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE,
     })
     @Retention(RetentionPolicy.SOURCE)
     public @interface ForegroundServiceType {}
 
     /**
      * The type of foreground service, set in
-     * {@link android.R.attr#foregroundServiceType} attribute, one value in
+     * {@link android.R.attr#foregroundServiceType} attribute by ORing flags in
      * {@link ForegroundServiceType}
      * @hide
      */
-    public @ForegroundServiceType int mForegroundServiceType = FOREGROUND_SERVICE_TYPE_UNSPECIFIED;
+    public @ForegroundServiceType int mForegroundServiceType = FOREGROUND_SERVICE_TYPE_NONE;
 
     public ServiceInfo() {
     }
@@ -217,6 +213,7 @@
         super.writeToParcel(dest, parcelableFlags);
         dest.writeString(permission);
         dest.writeInt(flags);
+        dest.writeInt(mForegroundServiceType);
     }
 
     public static final Creator<ServiceInfo> CREATOR =
@@ -233,5 +230,6 @@
         super(source);
         permission = source.readString();
         flags = source.readInt();
+        mForegroundServiceType = source.readInt();
     }
 }
diff --git a/core/java/android/content/res/Resources.java b/core/java/android/content/res/Resources.java
index baf64ad..59db49e 100644
--- a/core/java/android/content/res/Resources.java
+++ b/core/java/android/content/res/Resources.java
@@ -1394,6 +1394,17 @@
         mResourcesImpl.getValue(name, outValue, resolveRefs);
     }
 
+
+    /**
+     * @param set AttributeSet for which we want to find the source.
+     * @return The resource id for the source that is backing the given AttributeSet
+     * @hide
+     */
+    @AnyRes
+    public static int getAttributeSetSourceResId(@Nullable AttributeSet set) {
+        return ResourcesImpl.getAttributeSetSourceResId(set);
+    }
+
     /**
      * This class holds the current attribute values for a particular theme.
      * In other words, a Theme is a set of values for resource attributes;
diff --git a/core/java/android/content/res/ResourcesImpl.java b/core/java/android/content/res/ResourcesImpl.java
index d8564d5..9898079 100644
--- a/core/java/android/content/res/ResourcesImpl.java
+++ b/core/java/android/content/res/ResourcesImpl.java
@@ -15,6 +15,8 @@
  */
 package android.content.res;
 
+import static android.content.res.Resources.ID_NULL;
+
 import android.animation.Animator;
 import android.animation.StateListAnimator;
 import android.annotation.AnyRes;
@@ -1222,7 +1224,7 @@
                     for (int i = 0; i < num; i++) {
                         if (cachedXmlBlockCookies[i] == assetCookie && cachedXmlBlockFiles[i] != null
                                 && cachedXmlBlockFiles[i].equals(file)) {
-                            return cachedXmlBlocks[i].newParser();
+                            return cachedXmlBlocks[i].newParser(id);
                         }
                     }
 
@@ -1239,7 +1241,7 @@
                         cachedXmlBlockCookies[pos] = assetCookie;
                         cachedXmlBlockFiles[pos] = file;
                         cachedXmlBlocks[pos] = block;
-                        return block.newParser();
+                        return block.newParser(id);
                     }
                 }
             } catch (Exception e) {
@@ -1299,6 +1301,14 @@
         }
     }
 
+    @AnyRes
+    static int getAttributeSetSourceResId(@Nullable AttributeSet set) {
+        if (set == null) {
+            return ID_NULL;
+        }
+        return ((XmlBlock.Parser) set).getSourceResId();
+    }
+
     LongSparseArray<Drawable.ConstantState> getPreloadedDrawables() {
         return sPreloadedDrawables[0];
     }
diff --git a/core/java/android/content/res/XmlBlock.java b/core/java/android/content/res/XmlBlock.java
index 4e1159a..d8c3dde 100644
--- a/core/java/android/content/res/XmlBlock.java
+++ b/core/java/android/content/res/XmlBlock.java
@@ -16,6 +16,9 @@
 
 package android.content.res;
 
+import static android.content.res.Resources.ID_NULL;
+
+import android.annotation.AnyRes;
 import android.annotation.Nullable;
 import android.annotation.UnsupportedAppUsage;
 import android.util.TypedValue;
@@ -73,19 +76,29 @@
 
     @UnsupportedAppUsage
     public XmlResourceParser newParser() {
+        return newParser(ID_NULL);
+    }
+
+    public XmlResourceParser newParser(@AnyRes int resId) {
         synchronized (this) {
             if (mNative != 0) {
-                return new Parser(nativeCreateParseState(mNative), this);
+                return new Parser(nativeCreateParseState(mNative), this, resId);
             }
             return null;
         }
     }
 
     /*package*/ final class Parser implements XmlResourceParser {
-        Parser(long parseState, XmlBlock block) {
+        Parser(long parseState, XmlBlock block, @AnyRes int sourceResId) {
             mParseState = parseState;
             mBlock = block;
             block.mOpenCount++;
+            mSourceResId = sourceResId;
+        }
+
+        @AnyRes
+        public int getSourceResId() {
+            return mSourceResId;
         }
 
         public void setFeature(String name, boolean state) throws XmlPullParserException {
@@ -473,6 +486,7 @@
         private boolean mDecNextDepth = false;
         private int mDepth = 0;
         private int mEventType = START_DOCUMENT;
+        private @AnyRes int mSourceResId;
     }
 
     protected void finalize() throws Throwable {
diff --git a/core/java/android/content/rollback/IRollbackManager.aidl b/core/java/android/content/rollback/IRollbackManager.aidl
index 32e2198..104661f 100644
--- a/core/java/android/content/rollback/IRollbackManager.aidl
+++ b/core/java/android/content/rollback/IRollbackManager.aidl
@@ -32,7 +32,7 @@
     // Exposed for use from the system server only. Callback from the package
     // manager during the install flow when user data can be restored for a given
     // package.
-    void restoreUserData(String packageName, int userId, int appId, long ceDataInode,
+    void restoreUserData(String packageName, in int[] userIds, int appId, long ceDataInode,
             String seInfo, int token);
 
     // Exposed for test purposes only.
diff --git a/core/java/android/hardware/HardwareBuffer.java b/core/java/android/hardware/HardwareBuffer.java
index 5d4928c..475be49 100644
--- a/core/java/android/hardware/HardwareBuffer.java
+++ b/core/java/android/hardware/HardwareBuffer.java
@@ -20,6 +20,7 @@
 import android.annotation.LongDef;
 import android.annotation.NonNull;
 import android.annotation.UnsupportedAppUsage;
+import android.graphics.GraphicBuffer;
 import android.os.Build;
 import android.os.Parcel;
 import android.os.Parcelable;
@@ -214,6 +215,19 @@
     }
 
     /**
+     * @hide
+     * Returns a <code>HardwareBuffer</code> instance from <code>GraphicBuffer</code>
+     *
+     * @param graphicBuffer A GraphicBuffer to be wrapped as HardwareBuffer
+     * @return A <code>HardwareBuffer</code> instance.
+     */
+    @NonNull
+    public static HardwareBuffer createFromGraphicBuffer(@NonNull GraphicBuffer graphicBuffer) {
+        long nativeObject = nCreateFromGraphicBuffer(graphicBuffer);
+        return new HardwareBuffer(nativeObject);
+    }
+
+    /**
      * Private use only. See {@link #create(int, int, int, int, long)}. May also be
      * called from JNI using an already allocated native <code>HardwareBuffer</code>.
      */
@@ -405,6 +419,7 @@
 
     private static native long nCreateHardwareBuffer(int width, int height, int format, int layers,
             long usage);
+    private static native long nCreateFromGraphicBuffer(GraphicBuffer graphicBuffer);
     private static native long nGetNativeFinalizer();
     private static native void nWriteHardwareBufferToParcel(long nativeObject, Parcel dest);
     private static native long nReadHardwareBufferFromParcel(Parcel in);
diff --git a/core/java/android/hardware/biometrics/BiometricManager.java b/core/java/android/hardware/biometrics/BiometricManager.java
index 5afe1a9..d257c03 100644
--- a/core/java/android/hardware/biometrics/BiometricManager.java
+++ b/core/java/android/hardware/biometrics/BiometricManager.java
@@ -21,6 +21,7 @@
 
 import android.annotation.IntDef;
 import android.annotation.RequiresPermission;
+import android.annotation.SystemService;
 import android.content.Context;
 import android.content.pm.PackageManager;
 import android.os.RemoteException;
@@ -29,6 +30,7 @@
 /**
  * A class that contains biometric utilities. For authentication, see {@link BiometricPrompt}.
  */
+@SystemService(Context.BIOMETRIC_SERVICE)
 public class BiometricManager {
 
     private static final String TAG = "BiometricManager";
diff --git a/core/java/android/net/ConnectivityManager.java b/core/java/android/net/ConnectivityManager.java
index ef8ca9e..1656c6f 100644
--- a/core/java/android/net/ConnectivityManager.java
+++ b/core/java/android/net/ConnectivityManager.java
@@ -1033,34 +1033,6 @@
         }
     }
 
-    /**
-     * Configures an always-on VPN connection through a specific application.
-     * This connection is automatically granted and persisted after a reboot.
-     *
-     * <p>The designated package should declare a {@link VpnService} in its
-     *    manifest guarded by {@link android.Manifest.permission.BIND_VPN_SERVICE},
-     *    otherwise the call will fail.
-     *
-     * @param userId The identifier of the user to set an always-on VPN for.
-     * @param vpnPackage The package name for an installed VPN app on the device, or {@code null}
-     *                   to remove an existing always-on VPN configuration.
-     * @param lockdownEnabled {@code true} to disallow networking when the VPN is not connected or
-     *        {@code false} otherwise.
-     * @return {@code true} if the package is set as always-on VPN controller;
-     *         {@code false} otherwise.
-     * @hide
-     */
-    @RequiresPermission(android.Manifest.permission.CONTROL_ALWAYS_ON_VPN)
-    public boolean setAlwaysOnVpnPackageForUser(int userId, @Nullable String vpnPackage,
-            boolean lockdownEnabled) {
-        try {
-            return mService.setAlwaysOnVpnPackage(
-                    userId, vpnPackage, lockdownEnabled, /* whitelist */ null);
-        } catch (RemoteException e) {
-            throw e.rethrowFromSystemServer();
-        }
-    }
-
    /**
      * Returns the package name of the currently set always-on VPN application.
      * If there is no always-on VPN set, or the VPN is provided by the system instead
@@ -2609,6 +2581,7 @@
     }
 
     /** {@hide} */
+    @SystemApi
     public static final int TETHER_ERROR_NO_ERROR           = 0;
     /** {@hide} */
     public static final int TETHER_ERROR_UNKNOWN_IFACE      = 1;
@@ -2631,9 +2604,13 @@
     /** {@hide} */
     public static final int TETHER_ERROR_IFACE_CFG_ERROR      = 10;
     /** {@hide} */
+    @SystemApi
     public static final int TETHER_ERROR_PROVISION_FAILED     = 11;
     /** {@hide} */
     public static final int TETHER_ERROR_DHCPSERVER_ERROR     = 12;
+    /** {@hide} */
+    @SystemApi
+    public static final int TETHER_ERROR_ENTITLEMENT_UNKONWN  = 13;
 
     /**
      * Get a more detailed error code after a Tethering or Untethering
@@ -2656,6 +2633,65 @@
     }
 
     /**
+     * Callback for use with {@link #getLatestTetheringEntitlementValue} to find out whether
+     * entitlement succeeded.
+     * @hide
+     */
+    @SystemApi
+    public abstract static class TetheringEntitlementValueListener  {
+        /**
+         * Called to notify entitlement result.
+         *
+         * @param resultCode a int value of entitlement result. It may be one of
+         *         {@link #TETHER_ERROR_NO_ERROR},
+         *         {@link #TETHER_ERROR_PROVISION_FAILED}, or
+         *         {@link #TETHER_ERROR_ENTITLEMENT_UNKONWN}.
+         */
+        public void onEntitlementResult(int resultCode) {}
+    }
+
+    /**
+     * Get the last value of the entitlement check on this downstream. If the cached value is
+     * {@link #TETHER_ERROR_NO_ERROR} or showEntitlementUi argument is false, it just return the
+     * cached value. Otherwise, a UI-based entitlement check would be performed. It is not
+     * guaranteed that the UI-based entitlement check will complete in any specific time period
+     * and may in fact never complete. Any successful entitlement check the platform performs for
+     * any reason will update the cached value.
+     *
+     * @param type the downstream type of tethering. Must be one of
+     *         {@link #TETHERING_WIFI},
+     *         {@link #TETHERING_USB}, or
+     *         {@link #TETHERING_BLUETOOTH}.
+     * @param showEntitlementUi a boolean indicating whether to run UI-based entitlement check.
+     * @param listener an {@link TetheringEntitlementValueListener} which will be called to notify
+     *         the caller of the result of entitlement check. The listener may be called zero or
+     *         one time.
+     * @param handler {@link Handler} to specify the thread upon which the listener will be invoked.
+     * {@hide}
+     */
+    @SystemApi
+    @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
+    public void getLatestTetheringEntitlementValue(int type, boolean showEntitlementUi,
+            @NonNull final TetheringEntitlementValueListener listener, @Nullable Handler handler) {
+        Preconditions.checkNotNull(listener, "TetheringEntitlementValueListener cannot be null.");
+        ResultReceiver wrappedListener = new ResultReceiver(handler) {
+            @Override
+            protected void onReceiveResult(int resultCode, Bundle resultData) {
+                listener.onEntitlementResult(resultCode);
+            }
+        };
+
+        try {
+            String pkgName = mContext.getOpPackageName();
+            Log.i(TAG, "getLatestTetheringEntitlementValue:" + pkgName);
+            mService.getLatestTetheringEntitlementValue(type, wrappedListener,
+                    showEntitlementUi, pkgName);
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+
+    /**
      * Report network connectivity status.  This is currently used only
      * to alter status bar UI.
      * <p>This method requires the caller to hold the permission
@@ -3986,10 +4022,17 @@
     @Deprecated
     public static boolean setProcessDefaultNetwork(@Nullable Network network) {
         int netId = (network == null) ? NETID_UNSET : network.netId;
-        if (netId == NetworkUtils.getBoundNetworkForProcess()) {
-            return true;
+        boolean isSameNetId = (netId == NetworkUtils.getBoundNetworkForProcess());
+
+        if (netId != NETID_UNSET) {
+            netId = network.getNetIdForResolv();
         }
-        if (NetworkUtils.bindProcessToNetwork(netId)) {
+
+        if (!NetworkUtils.bindProcessToNetwork(netId)) {
+            return false;
+        }
+
+        if (!isSameNetId) {
             // Set HTTP proxy system properties to match network.
             // TODO: Deprecate this static method and replace it with a non-static version.
             try {
@@ -4003,10 +4046,9 @@
             // Must flush socket pool as idle sockets will be bound to previous network and may
             // cause subsequent fetches to be performed on old network.
             NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
-            return true;
-        } else {
-            return false;
         }
+
+        return true;
     }
 
     /**
diff --git a/core/java/android/net/IConnectivityManager.aidl b/core/java/android/net/IConnectivityManager.aidl
index f88adc2..1148ac1 100644
--- a/core/java/android/net/IConnectivityManager.aidl
+++ b/core/java/android/net/IConnectivityManager.aidl
@@ -197,4 +197,7 @@
     int getConnectionOwnerUid(in ConnectionInfo connectionInfo);
     boolean isCallerCurrentAlwaysOnVpnApp();
     boolean isCallerCurrentAlwaysOnVpnLockdownApp();
+
+    void getLatestTetheringEntitlementValue(int type, in ResultReceiver receiver,
+            boolean showEntitlementUi, String callerPkg);
 }
diff --git a/core/java/android/net/NetworkStack.java b/core/java/android/net/NetworkStack.java
index ac6bff0..b6cd635 100644
--- a/core/java/android/net/NetworkStack.java
+++ b/core/java/android/net/NetworkStack.java
@@ -20,7 +20,9 @@
 
 import android.annotation.NonNull;
 import android.annotation.Nullable;
+import android.annotation.SystemApi;
 import android.annotation.SystemService;
+import android.annotation.TestApi;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
@@ -46,9 +48,22 @@
  * @hide
  */
 @SystemService(Context.NETWORK_STACK_SERVICE)
+@SystemApi
+@TestApi
 public class NetworkStack {
     private static final String TAG = NetworkStack.class.getSimpleName();
 
+    /**
+     * Permission granted only to the NetworkStack APK, defined in NetworkStackStub with signature
+     * protection level.
+     * @hide
+     */
+    @SystemApi
+    @TestApi
+    public static final String PERMISSION_MAINLINE_NETWORK_STACK =
+            "android.permission.MAINLINE_NETWORK_STACK";
+
+    /** @hide */
     public static final String NETWORKSTACK_PACKAGE_NAME = "com.android.mainline.networkstack";
 
     private static final int NETWORKSTACK_TIMEOUT_MS = 10_000;
@@ -66,12 +81,14 @@
         void onNetworkStackConnected(INetworkStackConnector connector);
     }
 
+    /** @hide */
     public NetworkStack() { }
 
     /**
      * Create a DHCP server according to the specified parameters.
      *
      * <p>The server will be returned asynchronously through the provided callbacks.
+     * @hide
      */
     public void makeDhcpServer(final String ifName, final DhcpServingParamsParcel params,
             final IDhcpServerCallbacks cb) {
@@ -88,6 +105,7 @@
      * Create an IpClient on the specified interface.
      *
      * <p>The IpClient will be returned asynchronously through the provided callbacks.
+     * @hide
      */
     public void makeIpClient(String ifName, IIpClientCallbacks cb) {
         requestConnector(connector -> {
@@ -103,6 +121,7 @@
      * Create a NetworkMonitor.
      *
      * <p>The INetworkMonitor will be returned asynchronously through the provided callbacks.
+     * @hide
      */
     public void makeNetworkMonitor(
             NetworkParcelable network, String name, INetworkMonitorCallbacks cb) {
@@ -153,6 +172,7 @@
      * the system server on devices that do not support the network stack module. The network stack
      * connector will then be delivered asynchronously to clients that requested it before it was
      * started.
+     * @hide
      */
     public void start(Context context) {
         mNetworkStackStartRequested = true;
@@ -223,7 +243,7 @@
     private void requestConnector(@NonNull NetworkStackCallback request) {
         // TODO: PID check.
         final int caller = Binder.getCallingUid();
-        if (caller != Process.SYSTEM_UID && caller != Process.BLUETOOTH_UID) {
+        if (caller != Process.SYSTEM_UID && !UserHandle.isSameApp(caller, Process.BLUETOOTH_UID)) {
             // Don't even attempt to obtain the connector and give a nice error message
             throw new SecurityException(
                     "Only the system server should try to bind to the network stack.");
diff --git a/core/java/android/net/VpnService.java b/core/java/android/net/VpnService.java
index dc099a4..784f233 100644
--- a/core/java/android/net/VpnService.java
+++ b/core/java/android/net/VpnService.java
@@ -791,6 +791,27 @@
         }
 
         /**
+         * Marks the VPN network as metered. A VPN network is classified as metered when the user is
+         * sensitive to heavy data usage due to monetary costs and/or data limitations. In such
+         * cases, you should set this to {@code true} so that apps on the system can avoid doing
+         * large data transfers. Otherwise, set this to {@code false}. Doing so would cause VPN
+         * network to inherit its meteredness from its underlying networks.
+         *
+         * <p>VPN apps targeting {@link android.os.Build.VERSION_CODES#Q} or above will be
+         * considered metered by default.
+         *
+         * @param isMetered {@code true} if VPN network should be treated as metered regardless of
+         *     underlying network meteredness
+         * @return this {@link Builder} object to facilitate chaining method calls
+         * @see #setUnderlyingNetworks(Networks[])
+         * @see ConnectivityManager#isActiveNetworkMetered()
+         */
+        public Builder setMetered(boolean isMetered) {
+            mConfig.isMetered = isMetered;
+            return this;
+        }
+
+        /**
          * Create a VPN interface using the parameters supplied to this
          * builder. The interface works on IP packets, and a file descriptor
          * is returned for the application to access them. Each read
diff --git a/packages/overlays/FontRubikRubikOverlay/res/values/strings.xml b/core/java/android/os/BatterySaverPolicyConfig.aidl
similarity index 60%
rename from packages/overlays/FontRubikRubikOverlay/res/values/strings.xml
rename to core/java/android/os/BatterySaverPolicyConfig.aidl
index 4bac7da..37c66d0 100644
--- a/packages/overlays/FontRubikRubikOverlay/res/values/strings.xml
+++ b/core/java/android/os/BatterySaverPolicyConfig.aidl
@@ -1,12 +1,11 @@
-<!--
-/**
- * Copyright (c) 2018, The Android Open Source Project
+/*
+ * Copyright (C) 2019 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
+ *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
@@ -14,8 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
--->
-<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
-    <!-- Headline / Body font Rubik overlay -->
-    <string name="font_rubik_rubik_overlay" translatable="false">Rubik / Rubik</string>
-</resources>
+
+package android.os;
+
+parcelable BatterySaverPolicyConfig;
diff --git a/core/java/android/os/BatterySaverPolicyConfig.java b/core/java/android/os/BatterySaverPolicyConfig.java
new file mode 100644
index 0000000..b6e2b69
--- /dev/null
+++ b/core/java/android/os/BatterySaverPolicyConfig.java
@@ -0,0 +1,468 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.os;
+
+import android.annotation.NonNull;
+import android.annotation.SystemApi;
+import android.text.TextUtils;
+import android.util.ArrayMap;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Config to set Battery Saver policy flags.
+ *
+ * @hide
+ */
+@SystemApi
+public final class BatterySaverPolicyConfig implements Parcelable {
+    private final float mAdjustBrightnessFactor;
+    private final boolean mAdvertiseIsEnabled;
+    private final boolean mDeferFullBackup;
+    private final boolean mDeferKeyValueBackup;
+    @NonNull
+    private final Map<String, String> mDeviceSpecificSettings;
+    private final boolean mDisableAnimation;
+    private final boolean mDisableAod;
+    private final boolean mDisableLaunchBoost;
+    private final boolean mDisableOptionalSensors;
+    private final boolean mDisableSoundTrigger;
+    private final boolean mDisableVibration;
+    private final boolean mEnableAdjustBrightness;
+    private final boolean mEnableDataSaver;
+    private final boolean mEnableFirewall;
+    private final boolean mEnableQuickDoze;
+    private final boolean mForceAllAppsStandby;
+    private final boolean mForceBackgroundCheck;
+    private final int mGpsMode;
+
+    private BatterySaverPolicyConfig(Builder in) {
+        mAdjustBrightnessFactor = Math.max(0, Math.min(in.mAdjustBrightnessFactor, 1f));
+        mAdvertiseIsEnabled = in.mAdvertiseIsEnabled;
+        mDeferFullBackup = in.mDeferFullBackup;
+        mDeferKeyValueBackup = in.mDeferKeyValueBackup;
+        mDeviceSpecificSettings = Collections.unmodifiableMap(in.mDeviceSpecificSettings);
+        mDisableAnimation = in.mDisableAnimation;
+        mDisableAod = in.mDisableAod;
+        mDisableLaunchBoost = in.mDisableLaunchBoost;
+        mDisableOptionalSensors = in.mDisableOptionalSensors;
+        mDisableSoundTrigger = in.mDisableSoundTrigger;
+        mDisableVibration = in.mDisableVibration;
+        mEnableAdjustBrightness = in.mEnableAdjustBrightness;
+        mEnableDataSaver = in.mEnableDataSaver;
+        mEnableFirewall = in.mEnableFirewall;
+        mEnableQuickDoze = in.mEnableQuickDoze;
+        mForceAllAppsStandby = in.mForceAllAppsStandby;
+        mForceBackgroundCheck = in.mForceBackgroundCheck;
+        mGpsMode = Math.max(PowerManager.MIN_LOCATION_MODE,
+                Math.min(in.mGpsMode, PowerManager.MAX_LOCATION_MODE));
+    }
+
+    private BatterySaverPolicyConfig(Parcel in) {
+        mAdjustBrightnessFactor = Math.max(0, Math.min(in.readFloat(), 1f));
+        mAdvertiseIsEnabled = in.readBoolean();
+        mDeferFullBackup = in.readBoolean();
+        mDeferKeyValueBackup = in.readBoolean();
+
+        final int size = in.readInt();
+        Map<String, String> deviceSpecificSettings = new ArrayMap<>(size);
+        for (int i = 0; i < size; ++i) {
+            String key = TextUtils.emptyIfNull(in.readString());
+            String val = TextUtils.emptyIfNull(in.readString());
+            if (key.trim().isEmpty()) {
+                continue;
+            }
+            deviceSpecificSettings.put(key, val);
+        }
+        mDeviceSpecificSettings = Collections.unmodifiableMap(deviceSpecificSettings);
+
+        mDisableAnimation = in.readBoolean();
+        mDisableAod = in.readBoolean();
+        mDisableLaunchBoost = in.readBoolean();
+        mDisableOptionalSensors = in.readBoolean();
+        mDisableSoundTrigger = in.readBoolean();
+        mDisableVibration = in.readBoolean();
+        mEnableAdjustBrightness = in.readBoolean();
+        mEnableDataSaver = in.readBoolean();
+        mEnableFirewall = in.readBoolean();
+        mEnableQuickDoze = in.readBoolean();
+        mForceAllAppsStandby = in.readBoolean();
+        mForceBackgroundCheck = in.readBoolean();
+        mGpsMode = Math.max(PowerManager.MIN_LOCATION_MODE,
+                Math.min(in.readInt(), PowerManager.MAX_LOCATION_MODE));
+    }
+
+    public static final Creator<BatterySaverPolicyConfig> CREATOR =
+            new Creator<BatterySaverPolicyConfig>() {
+                @Override
+                public BatterySaverPolicyConfig createFromParcel(Parcel in) {
+                    return new BatterySaverPolicyConfig(in);
+                }
+
+                @Override
+                public BatterySaverPolicyConfig[] newArray(int size) {
+                    return new BatterySaverPolicyConfig[size];
+                }
+            };
+
+    @Override
+    public int describeContents() {
+        return 0;
+    }
+
+    @Override
+    public void writeToParcel(Parcel dest, int flags) {
+        dest.writeFloat(mAdjustBrightnessFactor);
+        dest.writeBoolean(mAdvertiseIsEnabled);
+        dest.writeBoolean(mDeferFullBackup);
+        dest.writeBoolean(mDeferKeyValueBackup);
+
+        final Set<Map.Entry<String, String>> entries = mDeviceSpecificSettings.entrySet();
+        final int size = entries.size();
+        dest.writeInt(size);
+        for (Map.Entry<String, String> entry : entries) {
+            dest.writeString(entry.getKey());
+            dest.writeString(entry.getValue());
+        }
+
+        dest.writeBoolean(mDisableAnimation);
+        dest.writeBoolean(mDisableAod);
+        dest.writeBoolean(mDisableLaunchBoost);
+        dest.writeBoolean(mDisableOptionalSensors);
+        dest.writeBoolean(mDisableSoundTrigger);
+        dest.writeBoolean(mDisableVibration);
+        dest.writeBoolean(mEnableAdjustBrightness);
+        dest.writeBoolean(mEnableDataSaver);
+        dest.writeBoolean(mEnableFirewall);
+        dest.writeBoolean(mEnableQuickDoze);
+        dest.writeBoolean(mForceAllAppsStandby);
+        dest.writeBoolean(mForceBackgroundCheck);
+        dest.writeInt(mGpsMode);
+    }
+
+    @Override
+    public String toString() {
+        StringBuilder sb = new StringBuilder();
+        for (Map.Entry<String, String> entry : mDeviceSpecificSettings.entrySet()) {
+            sb.append(entry.getKey()).append("=").append(entry.getValue()).append(",");
+        }
+        return "adjust_brightness_disabled=" + !mEnableAdjustBrightness + ","
+                + "adjust_brightness_factor=" + mAdjustBrightnessFactor + ","
+                + "advertise_is_enabled=" + mAdvertiseIsEnabled + ","
+                + "animation_disabled=" + mDisableAnimation + ","
+                + "aod_disabled=" + mDisableAod + ","
+                + "datasaver_disabled=" + !mEnableDataSaver + ","
+                + "firewall_disabled=" + !mEnableFirewall + ","
+                + "force_all_apps_standby=" + mForceAllAppsStandby + ","
+                + "force_background_check=" + mForceBackgroundCheck + ","
+                + "fullbackup_deferred=" + mDeferFullBackup + ","
+                + "gps_mode=" + mGpsMode + ","
+                + "keyvaluebackup_deferred=" + mDeferKeyValueBackup + ","
+                + "launch_boost_disabled=" + mDisableLaunchBoost + ","
+                + "optional_sensors_disabled=" + mDisableOptionalSensors + ","
+                + "quick_doze_enabled=" + mEnableQuickDoze + ","
+                + "soundtrigger_disabled=" + mDisableSoundTrigger + ","
+                + "vibration_disabled=" + mDisableVibration + ","
+                + sb.toString();
+    }
+
+    /**
+     * How much to adjust the screen brightness while in Battery Saver. This will have no effect
+     * if {@link #getEnableAdjustBrightness()} is {@code false}.
+     */
+    public float getAdjustBrightnessFactor() {
+        return mAdjustBrightnessFactor;
+    }
+
+    /**
+     * Whether or not to tell the system (and other apps) that Battery Saver is currently enabled.
+     */
+    public boolean getAdvertiseIsEnabled() {
+        return mAdvertiseIsEnabled;
+    }
+
+    /** Whether or not to defer full backup while in Battery Saver. */
+    public boolean getDeferFullBackup() {
+        return mDeferFullBackup;
+    }
+
+    /** Whether or not to defer key-value backup while in Battery Saver. */
+    public boolean getDeferKeyValueBackup() {
+        return mDeferKeyValueBackup;
+    }
+
+    /**
+     * Returns the device-specific battery saver constants.
+     */
+    @NonNull
+    public Map<String, String> getDeviceSpecificSettings() {
+        return mDeviceSpecificSettings;
+    }
+
+    /** Whether or not to disable animation while in Battery Saver. */
+    public boolean getDisableAnimation() {
+        return mDisableAnimation;
+    }
+
+    /** Whether or not to disable Always On Display while in Battery Saver. */
+    public boolean getDisableAod() {
+        return mDisableAod;
+    }
+
+    /** Whether or not to disable launch boost while in Battery Saver. */
+    public boolean getDisableLaunchBoost() {
+        return mDisableLaunchBoost;
+    }
+
+    /** Whether or not to disable optional sensors while in Battery Saver. */
+    public boolean getDisableOptionalSensors() {
+        return mDisableOptionalSensors;
+    }
+
+    /** Whether or not to disable sound trigger while in Battery Saver. */
+    public boolean getDisableSoundTrigger() {
+        return mDisableSoundTrigger;
+    }
+
+    /** Whether or not to disable vibration while in Battery Saver. */
+    public boolean getDisableVibration() {
+        return mDisableVibration;
+    }
+
+    /** Whether or not to enable brightness adjustment while in Battery Saver. */
+    public boolean getEnableAdjustBrightness() {
+        return mEnableAdjustBrightness;
+    }
+
+    /** Whether or not to enable Data Saver while in Battery Saver. */
+    public boolean getEnableDataSaver() {
+        return mEnableDataSaver;
+    }
+
+    /** Whether or not to enable the network firewall while in Battery Saver. */
+    public boolean getEnableFirewall() {
+        return mEnableFirewall;
+    }
+
+    /** Whether or not to enable Quick Doze while in Battery Saver. */
+    public boolean getEnableQuickDoze() {
+        return mEnableQuickDoze;
+    }
+
+    /** Whether or not to force all apps to standby mode while in Battery Saver. */
+    public boolean getForceAllAppsStandby() {
+        return mForceAllAppsStandby;
+    }
+
+    /** Whether or not to force background check while in Battery Saver. */
+    public boolean getForceBackgroundCheck() {
+        return mForceBackgroundCheck;
+    }
+
+    /** The GPS mode while in Battery Saver. */
+    public int getGpsMode() {
+        return mGpsMode;
+    }
+
+    /** Builder class for constructing {@link BatterySaverPolicyConfig} objects. */
+    public static final class Builder {
+        private float mAdjustBrightnessFactor = 1f;
+        private boolean mAdvertiseIsEnabled = false;
+        private boolean mDeferFullBackup = false;
+        private boolean mDeferKeyValueBackup = false;
+        @NonNull
+        private final ArrayMap<String, String> mDeviceSpecificSettings = new ArrayMap<>();
+        private boolean mDisableAnimation = false;
+        private boolean mDisableAod = false;
+        private boolean mDisableLaunchBoost = false;
+        private boolean mDisableOptionalSensors = false;
+        private boolean mDisableSoundTrigger = false;
+        private boolean mDisableVibration = false;
+        private boolean mEnableAdjustBrightness = false;
+        private boolean mEnableDataSaver = false;
+        private boolean mEnableFirewall = false;
+        private boolean mEnableQuickDoze = false;
+        private boolean mForceAllAppsStandby = false;
+        private boolean mForceBackgroundCheck = false;
+        private int mGpsMode = PowerManager.LOCATION_MODE_NO_CHANGE;
+
+        public Builder() {
+        }
+
+        /**
+         * Set how much to adjust the screen brightness while in Battery Saver. The value should
+         * be in the [0, 1] range, where 1 will not change the brightness. This will have no
+         * effect if {@link #setEnableAdjustBrightness(boolean)} is not called with {@code true}.
+         */
+        @NonNull
+        public Builder setAdjustBrightnessFactor(float adjustBrightnessFactor) {
+            mAdjustBrightnessFactor = adjustBrightnessFactor;
+            return this;
+        }
+
+        /**
+         * Set whether or not to tell the system (and other apps) that Battery Saver is
+         * currently enabled.
+         */
+        @NonNull
+        public Builder setAdvertiseIsEnabled(boolean advertiseIsEnabled) {
+            mAdvertiseIsEnabled = advertiseIsEnabled;
+            return this;
+        }
+
+        /** Set whether or not to defer full backup while in Battery Saver. */
+        @NonNull
+        public Builder setDeferFullBackup(boolean deferFullBackup) {
+            mDeferFullBackup = deferFullBackup;
+            return this;
+        }
+
+        /** Set whether or not to defer key-value backup while in Battery Saver. */
+        @NonNull
+        public Builder setDeferKeyValueBackup(boolean deferKeyValueBackup) {
+            mDeferKeyValueBackup = deferKeyValueBackup;
+            return this;
+        }
+
+        /**
+         * Adds a key-value pair for device-specific battery saver constants. The supported keys
+         * and values are the same as those in
+         * {@link android.provider.Settings.Global#BATTERY_SAVER_DEVICE_SPECIFIC_CONSTANTS}.
+         *
+         * @throws IllegalArgumentException if the provided key is invalid (empty, null, or all
+         * whitespace)
+         */
+        @NonNull
+        public Builder addDeviceSpecificSetting(@NonNull String key, @NonNull String value) {
+            if (key == null) {
+                throw new IllegalArgumentException("Key cannot be null");
+            }
+            key = key.trim();
+            if (TextUtils.isEmpty(key)) {
+                throw new IllegalArgumentException("Key cannot be empty");
+            }
+            mDeviceSpecificSettings.put(key, TextUtils.emptyIfNull(value));
+            return this;
+        }
+
+        /** Set whether or not to disable animation while in Battery Saver. */
+        @NonNull
+        public Builder setDisableAnimation(boolean disableAnimation) {
+            mDisableAnimation = disableAnimation;
+            return this;
+        }
+
+        /** Set whether or not to disable Always On Display while in Battery Saver. */
+        @NonNull
+        public Builder setDisableAod(boolean disableAod) {
+            mDisableAod = disableAod;
+            return this;
+        }
+
+        /** Set whether or not to disable launch boost while in Battery Saver. */
+        @NonNull
+        public Builder setDisableLaunchBoost(boolean disableLaunchBoost) {
+            mDisableLaunchBoost = disableLaunchBoost;
+            return this;
+        }
+
+        /** Set whether or not to disable optional sensors while in Battery Saver. */
+        @NonNull
+        public Builder setDisableOptionalSensors(boolean disableOptionalSensors) {
+            mDisableOptionalSensors = disableOptionalSensors;
+            return this;
+        }
+
+        /** Set whether or not to disable sound trigger while in Battery Saver. */
+        @NonNull
+        public Builder setDisableSoundTrigger(boolean disableSoundTrigger) {
+            mDisableSoundTrigger = disableSoundTrigger;
+            return this;
+        }
+
+        /** Set whether or not to disable vibration while in Battery Saver. */
+        @NonNull
+        public Builder setDisableVibration(boolean disableVibration) {
+            mDisableVibration = disableVibration;
+            return this;
+        }
+
+        /** Set whether or not to enable brightness adjustment while in Battery Saver. */
+        @NonNull
+        public Builder setEnableAdjustBrightness(boolean enableAdjustBrightness) {
+            mEnableAdjustBrightness = enableAdjustBrightness;
+            return this;
+        }
+
+        /** Set whether or not to enable Data Saver while in Battery Saver. */
+        @NonNull
+        public Builder setEnableDataSaver(boolean enableDataSaver) {
+            mEnableDataSaver = enableDataSaver;
+            return this;
+        }
+
+        /** Set whether or not to enable the network firewall while in Battery Saver. */
+        @NonNull
+        public Builder setEnableFirewall(boolean enableFirewall) {
+            mEnableFirewall = enableFirewall;
+            return this;
+        }
+
+        /** Set whether or not to enable Quick Doze while in Battery Saver. */
+        @NonNull
+        public Builder setEnableQuickDoze(boolean enableQuickDoze) {
+            mEnableQuickDoze = enableQuickDoze;
+            return this;
+        }
+
+        /** Set whether or not to force all apps to standby mode while in Battery Saver. */
+        @NonNull
+        public Builder setForceAllAppsStandby(boolean forceAllAppsStandby) {
+            mForceAllAppsStandby = forceAllAppsStandby;
+            return this;
+        }
+
+        /** Set whether or not to force background check while in Battery Saver. */
+        @NonNull
+        public Builder setForceBackgroundCheck(boolean forceBackgroundCheck) {
+            mForceBackgroundCheck = forceBackgroundCheck;
+            return this;
+        }
+
+        /** Set the GPS mode while in Battery Saver. */
+        @NonNull
+        public Builder setGpsMode(@PowerManager.LocationPowerSaveMode int gpsMode) {
+            mGpsMode = gpsMode;
+            return this;
+        }
+
+        /**
+         * Build a {@link BatterySaverPolicyConfig} object using the set parameters. This object
+         * is immutable.
+         */
+        @NonNull
+        public BatterySaverPolicyConfig build() {
+            if (!mEnableAdjustBrightness && Float.compare(1f, mAdjustBrightnessFactor) != 0) {
+                throw new IllegalArgumentException("Brightness adjustment factor changed without "
+                        + "enabling brightness adjustment");
+            }
+            return new BatterySaverPolicyConfig(this);
+        }
+    }
+}
diff --git a/core/java/android/os/DynamicAndroidManager.java b/core/java/android/os/DynamicAndroidManager.java
new file mode 100644
index 0000000..5238896
--- /dev/null
+++ b/core/java/android/os/DynamicAndroidManager.java
@@ -0,0 +1,188 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.os;
+
+import android.annotation.RequiresPermission;
+import android.annotation.SystemService;
+import android.content.Context;
+import android.gsi.GsiProgress;
+
+/**
+ * The DynamicAndroidManager offers a mechanism to use a new Android image temporarily. After the
+ * installation, the device can reboot into this image with a new created /data. This image will
+ * last until the next reboot and then the device will go back to the original image. However the
+ * installed image and the new created /data are not deleted but disabled. Thus the application can
+ * either re-enable the installed image by calling {@link #toggle} or use the {@link #remove} to
+ * delete it completely. In other words, there are three device states: no installation, installed
+ * and running. The procedure to install a DynamicAndroid starts with a {@link #startInstallation},
+ * followed by a series of {@link #write} and ends with a {@link commit}. Once the installation is
+ * complete, the device state changes from no installation to the installed state and a followed
+ * reboot will change its state to running. Note one instance of dynamic android can exist on a
+ * given device thus the {@link #startInstallation} will fail if the device is currently running a
+ * DynamicAndroid.
+ *
+ * @hide
+ */
+@SystemService(Context.DYNAMIC_ANDROID_SERVICE)
+public class DynamicAndroidManager {
+    private static final String TAG = "DynamicAndroidManager";
+
+    private final IDynamicAndroidService mService;
+
+    /** {@hide} */
+    public DynamicAndroidManager(IDynamicAndroidService service) {
+        mService = service;
+    }
+
+    /** The DynamicAndroidManager.Session represents a started session for the installation. */
+    public class Session {
+        private Session() {}
+        /**
+         * Write a chunk of the DynamicAndroid system image
+         *
+         * @return {@code true} if the call succeeds. {@code false} if there is any native runtime
+         *     error.
+         */
+        @RequiresPermission(android.Manifest.permission.MANAGE_DYNAMIC_ANDROID)
+        public boolean write(byte[] buf) {
+            try {
+                return mService.write(buf);
+            } catch (RemoteException e) {
+                throw new RuntimeException(e.toString());
+            }
+        }
+
+        /**
+         * Finish write and make device to boot into the it after reboot.
+         *
+         * @return {@code true} if the call succeeds. {@code false} if there is any native runtime
+         *     error.
+         */
+        @RequiresPermission(android.Manifest.permission.MANAGE_DYNAMIC_ANDROID)
+        public boolean commit() {
+            try {
+                return mService.commit();
+            } catch (RemoteException e) {
+                throw new RuntimeException(e.toString());
+            }
+        }
+    }
+    /**
+     * Start DynamicAndroid installation. This call may take an unbounded amount of time. The caller
+     * may use another thread to call the getStartProgress() to get the progress.
+     *
+     * @param systemSize system size in bytes
+     * @param userdataSize userdata size in bytes
+     * @return {@code true} if the call succeeds. {@code false} either the device does not contain
+     *     enough space or a DynamicAndroid is currently in use where the {@link #isInUse} would be
+     *     true.
+     */
+    @RequiresPermission(android.Manifest.permission.MANAGE_DYNAMIC_ANDROID)
+    public Session startInstallation(long systemSize, long userdataSize) {
+        try {
+            if (mService.startInstallation(systemSize, userdataSize)) {
+                return new Session();
+            } else {
+                return null;
+            }
+        } catch (RemoteException e) {
+            throw new RuntimeException(e.toString());
+        }
+    }
+
+    /**
+     * Query the progress of the current installation operation. This can be called while the
+     * installation is in progress.
+     *
+     * @return GsiProgress GsiProgress { int status; long bytes_processed; long total_bytes; } The
+     *     status field can be IGsiService.STATUS_NO_OPERATION, IGsiService.STATUS_WORKING or
+     *     IGsiService.STATUS_COMPLETE.
+     */
+    @RequiresPermission(android.Manifest.permission.MANAGE_DYNAMIC_ANDROID)
+    public GsiProgress getInstallationProgress() {
+        try {
+            return mService.getInstallationProgress();
+        } catch (RemoteException e) {
+            throw new RuntimeException(e.toString());
+        }
+    }
+
+    /**
+     * Abort the installation process. Note this method must be called in a thread other than the
+     * one calling the startInstallation method as the startInstallation method will not return
+     * until it is finished.
+     *
+     * @return {@code true} if the call succeeds. {@code false} if there is no installation
+     *     currently.
+     */
+    @RequiresPermission(android.Manifest.permission.MANAGE_DYNAMIC_ANDROID)
+    public boolean abort() {
+        try {
+            return mService.abort();
+        } catch (RemoteException e) {
+            throw new RuntimeException(e.toString());
+        }
+    }
+
+    /** @return {@code true} if the device is running a dynamic android */
+    @RequiresPermission(android.Manifest.permission.MANAGE_DYNAMIC_ANDROID)
+    public boolean isInUse() {
+        try {
+            return mService.isInUse();
+        } catch (RemoteException e) {
+            throw new RuntimeException(e.toString());
+        }
+    }
+
+    /** @return {@code true} if the device has a dynamic android installed */
+    @RequiresPermission(android.Manifest.permission.MANAGE_DYNAMIC_ANDROID)
+    public boolean isInstalled() {
+        try {
+            return mService.isInstalled();
+        } catch (RemoteException e) {
+            throw new RuntimeException(e.toString());
+        }
+    }
+
+    /**
+     * Remove DynamicAndroid installation if present
+     *
+     * @return {@code true} if the call succeeds. {@code false} if there is no installed image.
+     */
+    @RequiresPermission(android.Manifest.permission.MANAGE_DYNAMIC_ANDROID)
+    public boolean remove() {
+        try {
+            return mService.remove();
+        } catch (RemoteException e) {
+            throw new RuntimeException(e.toString());
+        }
+    }
+
+    /**
+     * Enable DynamicAndroid when it's not enabled, otherwise, disable it.
+     *
+     * @return {@code true} if the call succeeds. {@code false} if there is no installed image.
+     */
+    @RequiresPermission(android.Manifest.permission.MANAGE_DYNAMIC_ANDROID)
+    public boolean toggle() {
+        try {
+            return mService.toggle();
+        } catch (RemoteException e) {
+            throw new RuntimeException(e.toString());
+        }
+    }
+}
diff --git a/core/java/android/os/IDynamicAndroidService.aidl b/core/java/android/os/IDynamicAndroidService.aidl
new file mode 100644
index 0000000..0b28799
--- /dev/null
+++ b/core/java/android/os/IDynamicAndroidService.aidl
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package android.os;
+
+import android.gsi.GsiProgress;
+
+/** {@hide} */
+interface IDynamicAndroidService
+{
+    /**
+     * Start DynamicAndroid installation. This call may take 60~90 seconds. The caller
+     * may use another thread to call the getStartProgress() to get the progress.
+     *
+     * @param systemSize system size in bytes
+     * @param userdataSize userdata size in bytes
+     * @return true if the call succeeds
+     */
+    boolean startInstallation(long systemSize, long userdataSize);
+
+    /**
+     * Query the progress of the current installation operation. This can be called while
+     * the installation is in progress.
+     *
+     * @return GsiProgress
+     */
+    GsiProgress getInstallationProgress();
+
+    /**
+     * Abort the installation process. Note this method must be called in a thread other
+     * than the one calling the startInstallation method as the startInstallation
+     * method will not return until it is finished.
+     *
+     * @return true if the call succeeds
+     */
+    boolean abort();
+
+    /**
+     * @return true if the device is running an DynamicAnroid image
+     */
+    boolean isInUse();
+
+    /**
+     * @return true if the device has an DynamicAndroid image installed
+     */
+    boolean isInstalled();
+
+    /**
+     * Remove DynamicAndroid installation if present
+     *
+     * @return true if the call succeeds
+     */
+    boolean remove();
+
+    /**
+     * Enable DynamicAndroid when it's not enabled, otherwise, disable it.
+     *
+     * @return true if the call succeeds
+     */
+    boolean toggle();
+
+    /**
+     * Write a chunk of the DynamicAndroid system image
+     *
+     * @return true if the call succeeds
+     */
+    boolean write(in byte[] buf);
+
+    /**
+     * Finish write and make device to boot into the it after reboot.
+     *
+     * @return true if the call succeeds
+     */
+    boolean commit();
+}
diff --git a/core/java/android/os/IPowerManager.aidl b/core/java/android/os/IPowerManager.aidl
index ca5b233..093897a 100644
--- a/core/java/android/os/IPowerManager.aidl
+++ b/core/java/android/os/IPowerManager.aidl
@@ -17,8 +17,9 @@
 
 package android.os;
 
-import android.os.WorkSource;
+import android.os.BatterySaverPolicyConfig;
 import android.os.PowerSaveState;
+import android.os.WorkSource;
 
 /** @hide */
 
@@ -49,6 +50,8 @@
     PowerSaveState getPowerSaveState(int serviceType);
     boolean setPowerSaveMode(boolean mode);
     boolean setDynamicPowerSavings(boolean dynamicPowerSavingsEnabled, int disableThreshold);
+    boolean setAdaptivePowerSavePolicy(in BatterySaverPolicyConfig config);
+    boolean setAdaptivePowerSaveEnabled(boolean enabled);
     int getPowerSaveMode();
     boolean isDeviceIdleMode();
     boolean isLightDeviceIdleMode();
diff --git a/core/java/android/os/OWNERS b/core/java/android/os/OWNERS
new file mode 100644
index 0000000..b568f15
--- /dev/null
+++ b/core/java/android/os/OWNERS
@@ -0,0 +1,2 @@
+# Zygote
+per-file ZygoteProcess.java = chriswailes@google.com, ngeoffray@google.com, sehr@google.com, narayan@google.com, maco@google.com
diff --git a/core/java/android/os/PowerManager.java b/core/java/android/os/PowerManager.java
index 7f4254e..0441ba2 100644
--- a/core/java/android/os/PowerManager.java
+++ b/core/java/android/os/PowerManager.java
@@ -640,6 +640,9 @@
      */
     public static final int LOCATION_MODE_FOREGROUND_ONLY = 3;
 
+    static final int MIN_LOCATION_MODE = LOCATION_MODE_NO_CHANGE;
+    static final int MAX_LOCATION_MODE = LOCATION_MODE_FOREGROUND_ONLY;
+
     /**
      * @hide
      */
@@ -1272,6 +1275,48 @@
     }
 
     /**
+     * Sets the policy for adaptive power save.
+     *
+     * @return true if there was an effectual change. If full battery saver is enabled or the
+     * adaptive policy is not enabled, then this will return false.
+     *
+     * @hide
+     */
+    @SystemApi
+    @RequiresPermission(anyOf = {
+            android.Manifest.permission.DEVICE_POWER,
+            android.Manifest.permission.POWER_SAVER
+    })
+    public boolean setAdaptivePowerSavePolicy(@NonNull BatterySaverPolicyConfig config) {
+        try {
+            return mService.setAdaptivePowerSavePolicy(config);
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+
+    /**
+     * Enables or disables adaptive power save.
+     *
+     * @return true if there was an effectual change. If full battery saver is enabled, then this
+     * will return false.
+     *
+     * @hide
+     */
+    @SystemApi
+    @RequiresPermission(anyOf = {
+            android.Manifest.permission.DEVICE_POWER,
+            android.Manifest.permission.POWER_SAVER
+    })
+    public boolean setAdaptivePowerSaveEnabled(boolean enabled) {
+        try {
+            return mService.setAdaptivePowerSaveEnabled(enabled);
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+
+    /**
      * Indicates automatic battery saver toggling by the system will be based on percentage.
      *
      * @see PowerManager#getPowerSaveMode()
diff --git a/core/java/android/os/UserHandle.java b/core/java/android/os/UserHandle.java
index ad8a4d5..40c48a0 100644
--- a/core/java/android/os/UserHandle.java
+++ b/core/java/android/os/UserHandle.java
@@ -226,6 +226,7 @@
      * @hide
      */
     @TestApi
+    @SystemApi
     public static @AppIdInt int getAppId(int uid) {
         return uid % PER_USER_RANGE;
     }
diff --git a/core/java/android/provider/DeviceConfig.java b/core/java/android/provider/DeviceConfig.java
index 30c0731..92650e1 100644
--- a/core/java/android/provider/DeviceConfig.java
+++ b/core/java/android/provider/DeviceConfig.java
@@ -181,10 +181,10 @@
         String NAMESPACE = "intelligence_attention";
 
         /** If {@code true}, enables the attention features. */
-        String PROPERTY_ATTENTION_ENABLED = "attention_enabled";
+        String ATTENTION_ENABLED = "attention_enabled";
 
         /** Settings for the attention features. */
-        String PROPERTY_ATTENTION_SETTINGS = "attention_settings";
+        String ATTENTION_SETTINGS = "attention_settings";
     }
 
     /**
@@ -203,12 +203,12 @@
          * @hide
          */
         @SystemApi
-        String PROPERTY_PERMISSIONS_HUB_ENABLED = "enable_permissions_hub";
+        String PROPERTY_PERMISSIONS_HUB_ENABLED = "permissions_hub_enabled";
 
         /**
          * Whether to show location access check notifications.
          */
-        String PROPERTY_LOCATION_ACCESS_CHECK_ENABLED = "enable_location_access_check";
+        String PROPERTY_LOCATION_ACCESS_CHECK_ENABLED = "location_access_check_enabled";
     }
 
     /**
@@ -220,13 +220,17 @@
     public interface Telephony {
         String NAMESPACE = "telephony";
         /**
-         * Whether to apply ramping ringer on incoming phone calls.
-         */
-        String PROPERTY_ENABLE_RAMPING_RINGER = "enable_ramping_ringer";
-        /**
          * Ringer ramping time in milliseconds.
          */
-        String PROPERTY_RAMPING_RINGER_DURATION = "ramping_duration";
+        String RAMPING_RINGER_DURATION = "ramping_ringer_duration";
+        /**
+         * Whether to apply ramping ringer on incoming phone calls.
+         */
+        String RAMPING_RINGER_ENABLED = "ramping_ringer_enabled";
+        /**
+         * Vibration time in milliseconds before ramping ringer starts.
+         */
+        String RAMPING_RINGER_VIBRATION_DURATION = "ramping_ringer_vibration_duration";
     }
 
     /**
@@ -261,6 +265,7 @@
         String KEY_COMPACT_THROTTLE_2 = "compact_throttle_2";
         String KEY_COMPACT_THROTTLE_3 = "compact_throttle_3";
         String KEY_COMPACT_THROTTLE_4 = "compact_throttle_4";
+        String KEY_COMPACT_STATSD_SAMPLE_RATE = "compact_statsd_sample_rate";
 
         /**
          * Maximum number of cached processes. See
@@ -279,10 +284,10 @@
         String NAMESPACE = "attention_manager_service";
 
         /** If {@code true}, enables {@link AttentionManagerService} features. */
-        String PROPERTY_SERVICE_ENABLED = "service_enabled";
+        String SERVICE_ENABLED = "service_enabled";
 
         /** Allows a CTS to inject a fake implementation. */
-        String PROPERTY_COMPONENT_NAME = "component_name";
+        String COMPONENT_NAME = "component_name";
     }
 
     /**
diff --git a/core/java/android/provider/MediaStore.java b/core/java/android/provider/MediaStore.java
index 124c50a..0743c23 100644
--- a/core/java/android/provider/MediaStore.java
+++ b/core/java/android/provider/MediaStore.java
@@ -641,6 +641,8 @@
          * location. For example, when this value is left undefined, pending
          * {@link MediaStore.Audio.Media} items are stored under
          * {@link Environment#DIRECTORY_MUSIC}.
+         *
+         * @see MediaColumns#PRIMARY_DIRECTORY
          */
         public void setPrimaryDirectory(@Nullable String primaryDirectory) {
             this.primaryDirectory = primaryDirectory;
@@ -652,6 +654,8 @@
          * <p>
          * You may leave this value undefined to store the media as a direct
          * descendant of the {@link #setPrimaryDirectory(String)} location.
+         *
+         * @see MediaColumns#SECONDARY_DIRECTORY
          */
         public void setSecondaryDirectory(@Nullable String secondaryDirectory) {
             this.secondaryDirectory = secondaryDirectory;
@@ -980,6 +984,26 @@
          * Type: TEXT
          */
         public static final String OWNER_PACKAGE_NAME = "owner_package_name";
+
+        /**
+         * The primary directory name this media exists under. The value may be
+         * {@code NULL} if the media doesn't have a primary directory name.
+         * <p>
+         * Type: TEXT
+         *
+         * @see PendingParams#setPrimaryDirectory(String)
+         */
+        public static final String PRIMARY_DIRECTORY = "primary_directory";
+
+        /**
+         * The secondary directory name this media exists under. The value may
+         * be {@code NULL} if the media doesn't have a secondary directory name.
+         * <p>
+         * Type: TEXT
+         *
+         * @see PendingParams#setSecondaryDirectory(String)
+         */
+        public static final String SECONDARY_DIRECTORY = "secondary_directory";
     }
 
     /**
@@ -1428,13 +1452,20 @@
             public static final String BUCKET_DISPLAY_NAME = "bucket_display_name";
 
             /**
-             * The secondary bucket ID of this media item. This can be useful to
-             * present the user a second-level clustering of related media
-             * items. This is a read-only column that is automatically computed.
+             * The group ID of this media item. This can be useful to present
+             * the user a grouping of related media items, such a burst of
+             * images, or a {@code JPG} and {@code DNG} version of the same
+             * image.
+             * <p>
+             * This is a read-only column that is automatically computed based
+             * on the first portion of the filename. For example,
+             * {@code IMG1024.BURST001.JPG} and {@code IMG1024.BURST002.JPG}
+             * will have the same {@link #GROUP_ID} because the first portion of
+             * their filenames is identical.
              * <p>
              * Type: INTEGER
              */
-            public static final String SECONDARY_BUCKET_ID = "secondary_bucket_id";
+            public static final String GROUP_ID = "group_id";
         }
 
         public static final class Media implements ImageColumns {
@@ -2697,13 +2728,20 @@
             public static final String BUCKET_DISPLAY_NAME = "bucket_display_name";
 
             /**
-             * The secondary bucket ID of this media item. This can be useful to
-             * present the user a second-level clustering of related media
-             * items. This is a read-only column that is automatically computed.
+             * The group ID of this media item. This can be useful to present
+             * the user a grouping of related media items, such a burst of
+             * images, or a {@code JPG} and {@code DNG} version of the same
+             * image.
+             * <p>
+             * This is a read-only column that is automatically computed based
+             * on the first portion of the filename. For example,
+             * {@code IMG1024.BURST001.JPG} and {@code IMG1024.BURST002.JPG}
+             * will have the same {@link #GROUP_ID} because the first portion of
+             * their filenames is identical.
              * <p>
              * Type: INTEGER
              */
-            public static final String SECONDARY_BUCKET_ID = "secondary_bucket_id";
+            public static final String GROUP_ID = "group_id";
 
             /**
              * The bookmark for the video. Time in ms. Represents the location in the video that the
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java
index 5151e29..ffa47a9 100644
--- a/core/java/android/provider/Settings.java
+++ b/core/java/android/provider/Settings.java
@@ -5874,30 +5874,24 @@
                 "unknown_sources_default_reversed";
 
         /**
-         * Comma-separated list of location providers that activities may access. Do not rely on
-         * this value being present in settings.db or on ContentObserver notifications on the
+         * Comma-separated list of location providers that are accessible. Do not rely on
+         * this value being present or correct, or on ContentObserver notifications on the
          * corresponding Uri.
          *
-         * @deprecated Providers should not be controlled individually. See {@link #LOCATION_MODE}
-          * documentation for information on reading/writing location information.
+         * @deprecated The preferred methods for checking provider status and listening for changes
+         * are via {@link LocationManager#isProviderEnabled(String)} and
+         * {@link LocationManager#PROVIDERS_CHANGED_ACTION}.
          */
         @Deprecated
         public static final String LOCATION_PROVIDERS_ALLOWED = "location_providers_allowed";
 
         /**
-         * The degree of location access enabled by the user.
-         * <p>
-         * When used with {@link #putInt(ContentResolver, String, int)}, must be one of {@link
-         * #LOCATION_MODE_HIGH_ACCURACY}, {@link #LOCATION_MODE_SENSORS_ONLY}, {@link
-         * #LOCATION_MODE_BATTERY_SAVING}, or {@link #LOCATION_MODE_OFF}. When used with {@link
-         * #getInt(ContentResolver, String)}, the caller must gracefully handle additional location
-         * modes that might be added in the future.
-         * <p>
-         * Note: do not rely on this value being present in settings.db or on ContentObserver
-         * notifications for the corresponding Uri. Use {@link LocationManager#MODE_CHANGED_ACTION}
-         * to receive changes in this value.
+         * The current location mode of the device. Do not rely on this value being present or on
+         * ContentObserver notifications on the corresponding Uri.
          *
-         * @deprecated To check location mode, use {@link LocationManager#isLocationEnabled()}.
+         * @deprecated The preferred methods for checking location mode and listening for changes
+         * are via {@link LocationManager#isLocationEnabled()} and
+         * {@link LocationManager#MODE_CHANGED_ACTION}.
          */
         @Deprecated
         public static final String LOCATION_MODE = "location_mode";
@@ -5924,7 +5918,7 @@
         public static final int LOCATION_CHANGER_QUICK_SETTINGS = 2;
 
         /**
-         * Location access disabled.
+         * Location mode is off.
          *
          * @deprecated See {@link #LOCATION_MODE}.
          */
@@ -5932,32 +5926,39 @@
         public static final int LOCATION_MODE_OFF = 0;
 
         /**
-         * Network Location Provider disabled, but GPS and other sensors enabled.
+         * This mode no longer has any distinct meaning, but is interpreted as the location mode is
+         * on.
          *
-         * @deprecated To check location status, use {@link LocationManager#isLocationEnabled()}. To
-         *             get the status of a location provider, use
-         *             {@link LocationManager#isProviderEnabled(String)}.
+         * @deprecated See {@link #LOCATION_MODE_ON}.
          */
         @Deprecated
         public static final int LOCATION_MODE_SENSORS_ONLY = 1;
 
         /**
-         * Reduced power usage, such as limiting the number of GPS updates per hour. Requests
-         * with {@link android.location.Criteria#POWER_HIGH} may be downgraded to
-         * {@link android.location.Criteria#POWER_MEDIUM}.
+         * This mode no longer has any distinct meaning, but is interpreted as the location mode is
+         * on.
          *
-         * @deprecated See {@link #LOCATION_MODE}.
+         * @deprecated See {@link #LOCATION_MODE_ON}.
          */
         @Deprecated
         public static final int LOCATION_MODE_BATTERY_SAVING = 2;
 
         /**
-         * Best-effort location computation allowed.
+         * This mode no longer has any distinct meaning, but is interpreted as the location mode is
+         * on.
+         *
+         * @deprecated See {@link #LOCATION_MODE_ON}.
+         */
+        @Deprecated
+        public static final int LOCATION_MODE_HIGH_ACCURACY = 3;
+
+        /**
+         * Location mode is on.
          *
          * @deprecated See {@link #LOCATION_MODE}.
          */
         @Deprecated
-        public static final int LOCATION_MODE_HIGH_ACCURACY = 3;
+        public static final int LOCATION_MODE_ON = LOCATION_MODE_HIGH_ACCURACY;
 
         /**
          * A flag containing settings used for biometric weak
@@ -7466,14 +7467,6 @@
         private static final Validator DOZE_TAP_SCREEN_GESTURE_VALIDATOR = BOOLEAN_VALIDATOR;
 
         /**
-         * Gesture that wakes up the lock screen.
-         * @hide
-         */
-        public static final String DOZE_WAKE_LOCK_SCREEN_GESTURE = "doze_wake_lock_screen_gesture";
-
-        private static final Validator DOZE_WAKE_LOCK_SCREEN_GESTURE_VALIDATOR = BOOLEAN_VALIDATOR;
-
-        /**
          * Gesture that wakes up the display, showing the ambient version of the status bar.
          * @hide
          */
@@ -8588,7 +8581,6 @@
             DOZE_PICK_UP_GESTURE,
             DOZE_DOUBLE_TAP_GESTURE,
             DOZE_TAP_SCREEN_GESTURE,
-            DOZE_WAKE_LOCK_SCREEN_GESTURE,
             DOZE_WAKE_SCREEN_GESTURE,
             NFC_PAYMENT_DEFAULT_COMPONENT,
             AUTOMATIC_STORAGE_MANAGER_DAYS_TO_RETAIN,
@@ -8748,7 +8740,6 @@
             VALIDATORS.put(DOZE_PICK_UP_GESTURE, DOZE_PICK_UP_GESTURE_VALIDATOR);
             VALIDATORS.put(DOZE_DOUBLE_TAP_GESTURE, DOZE_DOUBLE_TAP_GESTURE_VALIDATOR);
             VALIDATORS.put(DOZE_TAP_SCREEN_GESTURE, DOZE_TAP_SCREEN_GESTURE_VALIDATOR);
-            VALIDATORS.put(DOZE_WAKE_LOCK_SCREEN_GESTURE, DOZE_WAKE_LOCK_SCREEN_GESTURE_VALIDATOR);
             VALIDATORS.put(DOZE_WAKE_SCREEN_GESTURE, DOZE_WAKE_SCREEN_GESTURE_VALIDATOR);
             VALIDATORS.put(NFC_PAYMENT_DEFAULT_COMPONENT, NFC_PAYMENT_DEFAULT_COMPONENT_VALIDATOR);
             VALIDATORS.put(AUTOMATIC_STORAGE_MANAGER_DAYS_TO_RETAIN,
@@ -10600,6 +10591,18 @@
         private static final Validator WIFI_PNO_RECENCY_SORTING_ENABLED_VALIDATOR =
                 BOOLEAN_VALIDATOR;
 
+        /**
+         * Setting to enable the Wi-Fi link probing.
+         * Disabled by default, and setting it to 1 will enable it.
+         * The value is boolean (0 or 1).
+         * @hide
+         */
+        public static final String WIFI_LINK_PROBING_ENABLED =
+                "wifi_link_probing_enabled";
+
+        private static final Validator WIFI_LINK_PROBING_ENABLED_VALIDATOR =
+                BOOLEAN_VALIDATOR;
+
        /**
         * The maximum number of times we will retry a connection to an access
         * point for which we have failed in acquiring an IP address from DHCP.
@@ -11425,6 +11428,9 @@
          * The following keys are supported:
          *
          * <pre>
+         * advertise_is_enabled              (boolean)
+         * datasaver_disabled                (boolean)
+         * launch_boost_disabled             (boolean)
          * vibration_disabled                (boolean)
          * animation_disabled                (boolean)
          * soundtrigger_disabled             (boolean)
@@ -11448,6 +11454,14 @@
         /**
          * Battery Saver device specific settings
          * This is encoded as a key=value list, separated by commas.
+         *
+         * The following keys are supported:
+         *
+         * <pre>
+         *     cpufreq-i (list of "core-number:frequency" pairs concatenated with /)
+         *     cpufreq-n (list of "core-number:frequency" pairs concatenated with /)
+         * </pre>
+         *
          * See {@link com.android.server.power.batterysaver.BatterySaverPolicy} for the details.
          *
          * @hide
@@ -11456,6 +11470,24 @@
                 "battery_saver_device_specific_constants";
 
         /**
+         * Settings for adaptive Battery Saver mode. Uses the same flags as
+         * {@link #BATTERY_SAVER_CONSTANTS}.
+         *
+         * @hide
+         */
+        public static final String BATTERY_SAVER_ADAPTIVE_CONSTANTS =
+                "battery_saver_adaptive_constants";
+
+        /**
+         * Device specific settings for adaptive Battery Saver mode. Uses the same flags as
+         * {@link #BATTERY_SAVER_DEVICE_SPECIFIC_CONSTANTS}.
+         *
+         * @hide
+         */
+        public static final String BATTERY_SAVER_ADAPTIVE_DEVICE_SPECIFIC_CONSTANTS =
+                "battery_saver_adaptive_device_specific_constants";
+
+        /**
          * Battery tip specific settings
          * This is encoded as a key=value list, separated by commas. Ex:
          *
@@ -13393,6 +13425,7 @@
                     WIFI_PNO_FREQUENCY_CULLING_ENABLED_VALIDATOR);
             VALIDATORS.put(WIFI_PNO_RECENCY_SORTING_ENABLED,
                     WIFI_PNO_RECENCY_SORTING_ENABLED_VALIDATOR);
+            VALIDATORS.put(WIFI_LINK_PROBING_ENABLED, WIFI_LINK_PROBING_ENABLED_VALIDATOR);
         }
 
         /**
diff --git a/core/java/android/service/appprediction/AppPredictionService.java b/core/java/android/service/appprediction/AppPredictionService.java
index b77405a..d012851 100644
--- a/core/java/android/service/appprediction/AppPredictionService.java
+++ b/core/java/android/service/appprediction/AppPredictionService.java
@@ -21,6 +21,7 @@
 import android.annotation.MainThread;
 import android.annotation.NonNull;
 import android.annotation.SystemApi;
+import android.annotation.TestApi;
 import android.app.Service;
 import android.app.prediction.AppPredictionContext;
 import android.app.prediction.AppPredictionSessionId;
@@ -49,6 +50,7 @@
  * @hide
  */
 @SystemApi
+@TestApi
 public abstract class AppPredictionService extends Service {
 
     private static final String TAG = "AppPredictionService";
@@ -140,6 +142,7 @@
 
     @Override
     public final IBinder onBind(Intent intent) {
+        // TODO(b/111701043): Verify that the action is valid
         return mInterface.asBinder();
     }
 
@@ -228,6 +231,7 @@
     public void onStopPredictionUpdates() {}
 
     private void doRequestPredictionUpdate(@NonNull AppPredictionSessionId sessionId) {
+        // Just an optimization, if there are no callbacks, then don't bother notifying the service
         final ArrayList<CallbackWrapper> callbacks = mSessionCallbacks.get(sessionId);
         if (callbacks != null && !callbacks.isEmpty()) {
             onRequestPredictionUpdate(sessionId);
diff --git a/core/java/android/service/attention/AttentionService.java b/core/java/android/service/attention/AttentionService.java
index f6e448dc..24d74ff 100644
--- a/core/java/android/service/attention/AttentionService.java
+++ b/core/java/android/service/attention/AttentionService.java
@@ -64,10 +64,10 @@
     /** Attention is present. */
     public static final int ATTENTION_SUCCESS_PRESENT = 1;
 
-    /** Preempted by other camera user. */
+    /** Preempted by other client. */
     public static final int ATTENTION_FAILURE_PREEMPTED = 2;
 
-    /** Preempted by other camera user. */
+    /** Request timed out. */
     public static final int ATTENTION_FAILURE_TIMED_OUT = 3;
 
     /** Unknown reasons for failing to determine the attention. */
diff --git a/core/java/android/service/contentsuggestions/ContentSuggestionsService.java b/core/java/android/service/contentsuggestions/ContentSuggestionsService.java
index 0da8039..333f4be 100644
--- a/core/java/android/service/contentsuggestions/ContentSuggestionsService.java
+++ b/core/java/android/service/contentsuggestions/ContentSuggestionsService.java
@@ -31,6 +31,7 @@
 import android.content.Intent;
 import android.graphics.Bitmap;
 import android.graphics.GraphicBuffer;
+import android.hardware.HardwareBuffer;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.IBinder;
@@ -62,7 +63,8 @@
             mHandler.sendMessage(
                     obtainMessage(ContentSuggestionsService::processContextImage,
                             ContentSuggestionsService.this, taskId,
-                            Bitmap.createHardwareBitmap(contextImage),
+                            Bitmap.wrapHardwareBuffer(
+                                    HardwareBuffer.createFromGraphicBuffer(contextImage), null),
                             imageContextRequestExtras));
         }
 
diff --git a/core/java/android/service/dreams/OWNERS b/core/java/android/service/dreams/OWNERS
index 3c9bbf8..426f002 100644
--- a/core/java/android/service/dreams/OWNERS
+++ b/core/java/android/service/dreams/OWNERS
@@ -1,3 +1,3 @@
-dsandler@google.com
+dsandler@android.com
 michaelwr@google.com
 roosa@google.com
diff --git a/core/java/android/service/voice/VoiceInteractionService.java b/core/java/android/service/voice/VoiceInteractionService.java
index 2789651..e76e096 100644
--- a/core/java/android/service/voice/VoiceInteractionService.java
+++ b/core/java/android/service/voice/VoiceInteractionService.java
@@ -16,7 +16,6 @@
 
 package android.service.voice;
 
-import android.annotation.IntDef;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.annotation.SdkConstant;
@@ -41,8 +40,6 @@
 
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Locale;
@@ -80,33 +77,6 @@
      */
     public static final String SERVICE_META_DATA = "android.voice_interaction";
 
-    /** @hide */
-    @Retention(RetentionPolicy.SOURCE)
-    @IntDef(prefix = {"VOICE_STATE_"}, value = {
-            VOICE_STATE_NONE,
-            VOICE_STATE_CONDITIONAL_LISTENING,
-            VOICE_STATE_LISTENING,
-            VOICE_STATE_FULFILLING})
-    public @interface VoiceState {
-    }
-
-    /**
-     * Voice assistant inactive.
-     */
-    public static final int VOICE_STATE_NONE = 0;
-    /**
-     * Voice assistant listening, but will only trigger if it hears a request it can fulfill.
-     */
-    public static final int VOICE_STATE_CONDITIONAL_LISTENING = 1;
-    /**
-     * Voice assistant is listening to user speech.
-     */
-    public static final int VOICE_STATE_LISTENING = 2;
-    /**
-     * Voice assistant is fulfilling an action requested by the user.
-     */
-    public static final int VOICE_STATE_FULFILLING = 3;
-
     IVoiceInteractionService mInterface = new IVoiceInteractionService.Stub() {
         @Override
         public void ready() {
@@ -376,7 +346,7 @@
      *
      * @param state value indicating whether the assistant is listening, fulfilling, etc.
      */
-    public final void setVoiceState(@VoiceState int state) {
+    public final void setVoiceState(int state) {
         try {
             mSystemService.setVoiceState(state);
         } catch (RemoteException e) {
diff --git a/core/java/android/util/FeatureFlagUtils.java b/core/java/android/util/FeatureFlagUtils.java
index db9351b..1ca6398 100644
--- a/core/java/android/util/FeatureFlagUtils.java
+++ b/core/java/android/util/FeatureFlagUtils.java
@@ -36,6 +36,7 @@
     public static final String FFLAG_PREFIX = "sys.fflag.";
     public static final String FFLAG_OVERRIDE_PREFIX = FFLAG_PREFIX + "override.";
     public static final String PERSIST_PREFIX = "persist." + FFLAG_OVERRIDE_PREFIX;
+    public static final String SEAMLESS_TRANSFER = "settings_seamless_transfer";
     public static final String HEARING_AID_SETTINGS = "settings_bluetooth_hearing_aid";
     public static final String SAFETY_HUB = "settings_safety_hub";
     public static final String SCREENRECORD_LONG_PRESS = "settings_screenrecord_long_press";
@@ -51,12 +52,13 @@
         DEFAULT_FLAGS.put("settings_dynamic_homepage", "true");
         DEFAULT_FLAGS.put("settings_mobile_network_v2", "true");
         DEFAULT_FLAGS.put("settings_network_and_internet_v2", "false");
-        DEFAULT_FLAGS.put("settings_seamless_transfer", "false");
         DEFAULT_FLAGS.put("settings_slice_injection", "true");
         DEFAULT_FLAGS.put("settings_systemui_theme", "true");
         DEFAULT_FLAGS.put("settings_wifi_dpp", "true");
         DEFAULT_FLAGS.put("settings_wifi_mac_randomization", "true");
         DEFAULT_FLAGS.put("settings_wifi_sharing", "true");
+        DEFAULT_FLAGS.put("settings_mainline_module", "false");
+        DEFAULT_FLAGS.put(SEAMLESS_TRANSFER, "false");
         DEFAULT_FLAGS.put(HEARING_AID_SETTINGS, "false");
         DEFAULT_FLAGS.put(SAFETY_HUB, "false");
         DEFAULT_FLAGS.put(SCREENRECORD_LONG_PRESS, "false");
diff --git a/core/java/android/view/LayoutInflater.java b/core/java/android/view/LayoutInflater.java
index 6061cb2..6a290b7 100644
--- a/core/java/android/view/LayoutInflater.java
+++ b/core/java/android/view/LayoutInflater.java
@@ -414,8 +414,7 @@
 
         // Make sure the application allows code generation
         ApplicationInfo appInfo = mContext.getApplicationInfo();
-        if ((appInfo.privateFlags & ApplicationInfo.PRIVATE_FLAG_PREFER_CODE_INTEGRITY) != 0
-            || appInfo.isPrivilegedApp()) {
+        if (appInfo.isEmbeddedDexUsed() || appInfo.isPrivilegedApp()) {
             mUseCompiledView = false;
             return;
         }
diff --git a/core/java/android/view/MotionEvent.java b/core/java/android/view/MotionEvent.java
index 9d3552f..b6a4a09 100644
--- a/core/java/android/view/MotionEvent.java
+++ b/core/java/android/view/MotionEvent.java
@@ -431,8 +431,9 @@
 
     /**
      * This flag indicates that the window that received this motion event is partly
-     * or wholly obscured by another visible window above it.  This flag is set to true
-     * even if the event did not directly pass through the obscured area.
+     * or wholly obscured by another visible window above it. This flag is set to true
+     * if the event directly passed through the obscured area.
+     *
      * A security sensitive application can check this flag to identify situations in which
      * a malicious application may have covered up part of its content for the purpose
      * of misleading the user or hijacking touches.  An appropriate response might be
@@ -443,16 +444,17 @@
 
     /**
      * This flag indicates that the window that received this motion event is partly
-     * or wholly obscured by another visible window above it.  This flag is set to true
+     * or wholly obscured by another visible window above it. This flag is set to true
      * even if the event did not directly pass through the obscured area.
+     *
      * A security sensitive application can check this flag to identify situations in which
      * a malicious application may have covered up part of its content for the purpose
      * of misleading the user or hijacking touches.  An appropriate response might be
      * to drop the suspect touches or to take additional precautions to confirm the user's
      * actual intent.
      *
-     * Unlike FLAG_WINDOW_IS_OBSCURED, this is actually true.
-     * @hide
+     * Unlike FLAG_WINDOW_IS_OBSCURED, this is true even if the window that received this event is
+     * obstructed in areas other than the touched location.
      */
     public static final int FLAG_WINDOW_IS_PARTIALLY_OBSCURED = 0x2;
 
diff --git a/core/java/android/view/SurfaceControl.java b/core/java/android/view/SurfaceControl.java
index 141a7f0..8061cc3 100644
--- a/core/java/android/view/SurfaceControl.java
+++ b/core/java/android/view/SurfaceControl.java
@@ -41,6 +41,7 @@
 import android.graphics.Point;
 import android.graphics.Rect;
 import android.graphics.Region;
+import android.hardware.HardwareBuffer;
 import android.hardware.display.DisplayedContentSample;
 import android.hardware.display.DisplayedContentSamplingAttributes;
 import android.os.IBinder;
@@ -503,16 +504,7 @@
             }
             mWidth = width;
             mHeight = height;
-            // set this as a buffer layer since we are specifying a buffer size.
-            return setFlags(FX_SURFACE_NORMAL, FX_SURFACE_MASK);
-        }
-
-        /**
-         * Set the initial size of the controlled surface's buffers in pixels.
-         */
-        private void unsetBufferSize() {
-            mWidth = 0;
-            mHeight = 0;
+            return this;
         }
 
         /**
@@ -630,11 +622,16 @@
          * Color layers will not have an associated BufferQueue and will instead always render a
          * solid color (that is, solid before plane alpha). Currently that color is black.
          *
+         * @param isColorLayer Whether to create a color layer.
          * @hide
          */
-        public Builder setColorLayer() {
-            unsetBufferSize();
-            return setFlags(FX_SURFACE_DIM, FX_SURFACE_MASK);
+        public Builder setColorLayer(boolean isColorLayer) {
+            if (isColorLayer) {
+                mFlags |= FX_SURFACE_DIM;
+            } else {
+                mFlags &= ~FX_SURFACE_DIM;
+            }
+            return this;
         }
 
         private boolean isColorLayerSet() {
@@ -647,11 +644,16 @@
          * Container layers will not be rendered in any fashion and instead are used
          * as a parent of renderable layers.
          *
+         * @param isContainerLayer Whether to create a container layer.
          * @hide
          */
-        public Builder setContainerLayer() {
-            unsetBufferSize();
-            return setFlags(FX_SURFACE_CONTAINER, FX_SURFACE_MASK);
+        public Builder setContainerLayer(boolean isContainerLayer) {
+            if (isContainerLayer) {
+                mFlags |= FX_SURFACE_CONTAINER;
+            } else {
+                mFlags &= ~FX_SURFACE_CONTAINER;
+            }
+            return this;
         }
 
         private boolean isContainerLayerSet() {
@@ -659,7 +661,7 @@
         }
 
         /**
-         * Set 'Surface creation flags' such as {@link #HIDDEN}, {@link #SECURE}.
+         * Set 'Surface creation flags' such as {@link HIDDEN}, {@link SECURE}.
          *
          * TODO: Finish conversion to individual builder methods?
          * @param flags The combined flags
@@ -669,11 +671,6 @@
             mFlags = flags;
             return this;
         }
-
-        private Builder setFlags(int flags, int mask) {
-            mFlags = (mFlags & ~mask) | flags;
-            return this;
-        }
     }
 
     /**
@@ -1807,7 +1804,10 @@
             Log.w(TAG, "Failed to take screenshot");
             return null;
         }
-        return Bitmap.createHardwareBitmap(buffer);
+        // TODO(b/116112787) Now that hardware bitmap creation can take color space, we
+        // should continue to fix screenshot.
+        return Bitmap.wrapHardwareBuffer(HardwareBuffer.createFromGraphicBuffer(buffer),
+                ColorSpace.get(ColorSpace.Named.SRGB));
     }
 
     /**
diff --git a/core/java/android/view/SurfaceView.java b/core/java/android/view/SurfaceView.java
index ecb2ac4..ecbec65 100644
--- a/core/java/android/view/SurfaceView.java
+++ b/core/java/android/view/SurfaceView.java
@@ -33,6 +33,7 @@
 import android.graphics.RenderNode;
 import android.os.Build;
 import android.os.Handler;
+import android.os.IBinder;
 import android.os.Looper;
 import android.os.SystemClock;
 import android.util.AttributeSet;
@@ -587,7 +588,7 @@
                     mBackgroundControl = new SurfaceControl.Builder(mSurfaceSession)
                         .setName("Background for -" + name)
                         .setOpaque(true)
-                        .setColorLayer()
+                        .setColorLayer(true)
                         .setParent(mSurfaceControl)
                         .build();
 
diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java
index 519181d..ec8ed5b 100644
--- a/core/java/android/view/View.java
+++ b/core/java/android/view/View.java
@@ -16,6 +16,7 @@
 
 package android.view;
 
+import static android.content.res.Resources.ID_NULL;
 import static android.view.ViewRootImpl.NEW_INSETS_MODE_FULL;
 import static android.view.accessibility.AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED;
 
@@ -5052,6 +5053,9 @@
     @Nullable
     private WeakReference<ContentCaptureSession> mContentCaptureSession;
 
+    @LayoutRes
+    private int mSourceLayoutId = ID_NULL;
+
     /**
      * Simple constructor to use when creating a view from code.
      *
@@ -5217,6 +5221,8 @@
     public View(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
         this(context);
 
+        mSourceLayoutId = Resources.getAttributeSetSourceResId(attrs);
+
         final TypedArray a = context.obtainStyledAttributes(
                 attrs, com.android.internal.R.styleable.View, defStyleAttr, defStyleRes);
 
@@ -23253,6 +23259,18 @@
     }
 
     /**
+     * A {@link View} can be inflated from an XML layout. For such Views this method returns the
+     * resource ID of the source layout.
+     *
+     * @return The layout resource id if this view was inflated from XML, otherwise
+     * {@link Resources#ID_NULL}.
+     */
+    @LayoutRes
+    public int getSourceLayoutResId() {
+        return mSourceLayoutId;
+    }
+
+    /**
      * Returns the top padding of this view.
      *
      * @return the top padding in pixels
diff --git a/core/java/android/view/contentcapture/ContentCaptureManager.java b/core/java/android/view/contentcapture/ContentCaptureManager.java
index fde0ced..f31856c 100644
--- a/core/java/android/view/contentcapture/ContentCaptureManager.java
+++ b/core/java/android/view/contentcapture/ContentCaptureManager.java
@@ -67,9 +67,6 @@
 
     private final Object mLock = new Object();
 
-    @GuardedBy("mLock")
-    private boolean mDisabled;
-
     @NonNull
     private final Context mContext;
 
@@ -115,8 +112,7 @@
     public MainContentCaptureSession getMainContentCaptureSession() {
         synchronized (mLock) {
             if (mMainSession == null) {
-                mMainSession = new MainContentCaptureSession(mContext, mHandler, mService,
-                        mDisabled);
+                mMainSession = new MainContentCaptureSession(mContext, this, mHandler, mService);
                 if (VERBOSE) Log.v(TAG, "getMainContentCaptureSession(): created " + mMainSession);
             }
             return mMainSession;
@@ -180,9 +176,17 @@
      * </ul>
      */
     public boolean isContentCaptureEnabled() {
+        if (mService == null) return false;
+
+        final MainContentCaptureSession mainSession;
         synchronized (mLock) {
-            return mService != null && !mDisabled;
+            mainSession = mMainSession;
         }
+        // The main session is only set when the activity starts, so we need to return true until
+        // then.
+        if (mainSession != null && mainSession.isDisabled()) return false;
+
+        return true;
     }
 
     /**
@@ -287,7 +291,8 @@
     public void dump(String prefix, PrintWriter pw) {
         synchronized (mLock) {
             pw.print(prefix); pw.println("ContentCaptureManager");
-            pw.print(prefix); pw.print("Disabled: "); pw.println(mDisabled);
+            pw.print(prefix); pw.print("isContentCaptureEnabled(): ");
+            pw.println(isContentCaptureEnabled());
             pw.print(prefix); pw.print("Context: "); pw.println(mContext);
             pw.print(prefix); pw.print("User: "); pw.println(mContext.getUserId());
             if (mService != null) {
diff --git a/core/java/android/view/contentcapture/MainContentCaptureSession.java b/core/java/android/view/contentcapture/MainContentCaptureSession.java
index 2eca51f..034c8fa 100644
--- a/core/java/android/view/contentcapture/MainContentCaptureSession.java
+++ b/core/java/android/view/contentcapture/MainContentCaptureSession.java
@@ -89,21 +89,23 @@
      */
     public static final String EXTRA_BINDER = "binder";
 
-    // TODO(b/111276913): make sure disabled state is in sync with manager's disabled
     @NonNull
-    private final AtomicBoolean mDisabled;
+    private final AtomicBoolean mDisabled = new AtomicBoolean(false);
 
     @NonNull
     private final Context mContext;
 
     @NonNull
+    private final ContentCaptureManager mManager;
+
+    @NonNull
     private final Handler mHandler;
 
     /**
      * Interface to the system_server binder object - it's only used to start the session (and
      * notify when the session is finished).
      */
-    @Nullable
+    @Nullable // TODO(b/122959591): shoul never be null, we should make main session null instead
     private final IContentCaptureManager mSystemServerInterface;
 
     /**
@@ -136,13 +138,13 @@
     private final LocalLog mFlushHistory = new LocalLog(10);
 
     /** @hide */
-    protected MainContentCaptureSession(@NonNull Context context, @NonNull Handler handler,
-            @Nullable IContentCaptureManager systemServerInterface,
-            @NonNull boolean disabled) {
+    protected MainContentCaptureSession(@NonNull Context context,
+            @NonNull ContentCaptureManager manager, @NonNull Handler handler,
+            @Nullable IContentCaptureManager systemServerInterface) {
         mContext = context;
+        mManager = manager;
         mHandler = handler;
         mSystemServerInterface = systemServerInterface;
-        mDisabled = new AtomicBoolean(disabled);
     }
 
     @Override
@@ -235,8 +237,8 @@
 
     /**
      * Callback from {@code system_server} after call to
-     * {@link IContentCaptureManager#startSession(int, IBinder, ComponentName, String,
-     * int, IResultReceiver)}.
+     * {@link IContentCaptureManager#startSession(IBinder, ComponentName, String, int,
+     * IResultReceiver)}
      *
      * @param resultCode session state
      * @param binder handle to {@code IContentCaptureDirectManager}
@@ -517,8 +519,12 @@
 
     @Override
     boolean isContentCaptureEnabled() {
-        return super.isContentCaptureEnabled() && mSystemServerInterface != null
-                && !mDisabled.get();
+        return super.isContentCaptureEnabled() && mManager.isContentCaptureEnabled();
+    }
+
+    // Called by ContentCaptureManager.isContentCaptureEnabled
+    boolean isDisabled() {
+        return mDisabled.get();
     }
 
     // TODO(b/122454205): refactor "notifyXXXX" methods below to a common "Buffer" object that is
diff --git a/core/java/android/view/textclassifier/ExtrasUtils.java b/core/java/android/view/textclassifier/ExtrasUtils.java
new file mode 100644
index 0000000..602455c
--- /dev/null
+++ b/core/java/android/view/textclassifier/ExtrasUtils.java
@@ -0,0 +1,127 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.view.textclassifier;
+
+import android.annotation.Nullable;
+import android.app.RemoteAction;
+import android.content.Intent;
+import android.os.Bundle;
+
+import java.util.ArrayList;
+
+/**
+ * Utility class for inserting and retrieving data in TextClassifier request/response extras.
+ * @hide
+ */
+public final class ExtrasUtils {
+
+    private static final String ACTIONS_INTENTS = "actions-intents";
+    private static final String FOREIGN_LANGUAGE = "foreign-language";
+    private static final String ENTITY_TYPE = "entity-type";
+    private static final String SCORE = "score";
+    private static final String MODEL_VERSION = "model-version";
+    private static final String MODEL_NAME = "model-name";
+
+    private ExtrasUtils() {}
+
+    /**
+     * Bundles and returns foreign language detection information for TextClassifier responses.
+     */
+    static Bundle createForeignLanguageExtra(
+            String language, float score, int modelVersion) {
+        final Bundle bundle = new Bundle();
+        bundle.putString(ENTITY_TYPE, language);
+        bundle.putFloat(SCORE, score);
+        bundle.putInt(MODEL_VERSION, modelVersion);
+        bundle.putString(MODEL_NAME, "langId_v" + modelVersion);
+        return bundle;
+    }
+
+    /**
+     * Stores {@code extra} as foreign language information in TextClassifier response object's
+     * extras {@code container}.
+     */
+    static void putForeignLanguageExtra(Bundle container, Bundle extra) {
+        container.putParcelable(FOREIGN_LANGUAGE, extra);
+    }
+
+    /**
+     * Returns foreign language detection information contained in the TextClassification object.
+     * responses.
+     */
+    @Nullable
+    public static Bundle getForeignLanguageExtra(TextClassification classification) {
+        return classification.getExtras().getBundle(FOREIGN_LANGUAGE);
+    }
+
+    /**
+     * Stores {@code actionIntents} information in TextClassifier response object's extras
+     * {@code container}.
+     */
+    static void putActionsIntents(Bundle container, ArrayList<Intent> actionsIntents) {
+        container.putParcelableArrayList(ACTIONS_INTENTS, actionsIntents);
+    }
+
+    /**
+     * Returns {@code actionIntents} information contained in the TextClassification object.
+     */
+    @Nullable
+    public static ArrayList<Intent> getActionsIntents(TextClassification classification) {
+        return classification.getExtras().getParcelableArrayList(ACTIONS_INTENTS);
+    }
+
+    /**
+     * Returns the first "translate" action found in the {@code classification} object.
+     */
+    @Nullable
+    public static RemoteAction findTranslateAction(TextClassification classification) {
+        final ArrayList<Intent> actionIntents = getActionsIntents(classification);
+        if (actionIntents != null) {
+            final int size = actionIntents.size();
+            for (int i = 0; i < size; i++) {
+                if (Intent.ACTION_TRANSLATE.equals(actionIntents.get(i).getAction())) {
+                    return classification.getActions().get(i);
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Returns the entity type contained in the {@code extra}.
+     */
+    @Nullable
+    public static String getEntityType(Bundle extra) {
+        return extra.getString(ENTITY_TYPE);
+    }
+
+    /**
+     * Returns the score contained in the {@code extra}.
+     */
+    @Nullable
+    public static float getScore(Bundle extra) {
+        return extra.getFloat(SCORE, -1);
+    }
+
+    /**
+     * Returns the model name contained in the {@code extra}.
+     */
+    @Nullable
+    public static String getModelName(Bundle extra) {
+        return extra.getString(MODEL_NAME);
+    }
+}
diff --git a/core/java/android/view/textclassifier/TextClassification.java b/core/java/android/view/textclassifier/TextClassification.java
index d9f7965..a059209 100644
--- a/core/java/android/view/textclassifier/TextClassification.java
+++ b/core/java/android/view/textclassifier/TextClassification.java
@@ -378,6 +378,8 @@
         @Nullable private OnClickListener mLegacyOnClickListener;
         @Nullable private String mId;
         @Nullable private Bundle mExtras;
+        @NonNull private final ArrayList<Intent> mActionIntents = new ArrayList<>();
+        @Nullable private Bundle mForeignLanguageExtra;
 
         /**
          * Sets the classified text.
@@ -412,8 +414,19 @@
          */
         @NonNull
         public Builder addAction(@NonNull RemoteAction action) {
+            return addAction(action, null);
+        }
+
+        /**
+         * @param intent the intent in the remote action.
+         * @see #addAction(RemoteAction)
+         * @hide
+         */
+        @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
+        public Builder addAction(RemoteAction action, @Nullable Intent intent) {
             Preconditions.checkArgument(action != null);
             mActions.add(action);
+            mActionIntents.add(intent);
             return this;
         }
 
@@ -499,13 +512,33 @@
         }
 
         /**
+         * @see #setExtras(Bundle)
+         * @hide
+         */
+        @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
+        public Builder setForeignLanguageExtra(@Nullable Bundle extra) {
+            mForeignLanguageExtra = extra;
+            return this;
+        }
+
+        /**
          * Builds and returns a {@link TextClassification} object.
          */
         @NonNull
         public TextClassification build() {
             return new TextClassification(mText, mLegacyIcon, mLegacyLabel, mLegacyIntent,
-                    mLegacyOnClickListener, mActions, mEntityConfidence, mId,
-                    mExtras == null ? Bundle.EMPTY : mExtras.deepCopy());
+                    mLegacyOnClickListener, mActions, mEntityConfidence, mId, buildExtras());
+        }
+
+        private Bundle buildExtras() {
+            final Bundle extras = mExtras == null ? new Bundle() : mExtras.deepCopy();
+            if (!mActionIntents.isEmpty()) {
+                ExtrasUtils.putActionsIntents(extras, mActionIntents);
+            }
+            if (mForeignLanguageExtra != null) {
+                ExtrasUtils.putForeignLanguageExtra(extras, mForeignLanguageExtra);
+            }
+            return extras.isEmpty() ? Bundle.EMPTY : extras;
         }
     }
 
diff --git a/core/java/android/view/textclassifier/TextClassificationSession.java b/core/java/android/view/textclassifier/TextClassificationSession.java
index 45668c0..ba1287f 100644
--- a/core/java/android/view/textclassifier/TextClassificationSession.java
+++ b/core/java/android/view/textclassifier/TextClassificationSession.java
@@ -71,10 +71,23 @@
 
     @Override
     public void onSelectionEvent(SelectionEvent event) {
-        checkDestroyed();
-        Preconditions.checkNotNull(event);
-        if (mEventHelper.sanitizeEvent(event)) {
-            mDelegate.onSelectionEvent(event);
+        try {
+            if (mEventHelper.sanitizeEvent(event)) {
+                mDelegate.onSelectionEvent(event);
+            }
+        } catch (Exception e) {
+            // Avoid crashing for event reporting.
+            Log.e(LOG_TAG, "Error reporting text classifier selection event", e);
+        }
+    }
+
+    @Override
+    public void onTextClassifierEvent(TextClassifierEvent event) {
+        try {
+            mDelegate.onTextClassifierEvent(event);
+        } catch (Exception e) {
+            // Avoid crashing for event reporting.
+            Log.e(LOG_TAG, "Error reporting text classifier event", e);
         }
     }
 
diff --git a/core/java/android/view/textclassifier/TextClassifier.java b/core/java/android/view/textclassifier/TextClassifier.java
index 8a688d8..e010155 100644
--- a/core/java/android/view/textclassifier/TextClassifier.java
+++ b/core/java/android/view/textclassifier/TextClassifier.java
@@ -161,7 +161,12 @@
      * No-op TextClassifier.
      * This may be used to turn off TextClassifier features.
      */
-    TextClassifier NO_OP = new TextClassifier() {};
+    TextClassifier NO_OP = new TextClassifier() {
+        @Override
+        public String toString() {
+            return "TextClassifier.NO_OP";
+        }
+    };
 
     /**
      * Extra that is included on activity intents coming from a TextClassifier when
diff --git a/core/java/android/view/textclassifier/TextClassifierEvent.java b/core/java/android/view/textclassifier/TextClassifierEvent.java
index cd13cc0..0d4338b 100644
--- a/core/java/android/view/textclassifier/TextClassifierEvent.java
+++ b/core/java/android/view/textclassifier/TextClassifierEvent.java
@@ -141,6 +141,8 @@
     @Nullable private final String mLanguage;
     private final float mScore;
 
+    @Nullable private final String mModelName;
+
     private TextClassifierEvent(
             int eventCategory,
             int eventType,
@@ -156,7 +158,8 @@
             int relativeSuggestedWordEndIndex,
             int[] actionIndex,
             String language,
-            float score) {
+            float score,
+            String modelVersion) {
         mEventCategory = eventCategory;
         mEventType = eventType;
         mEntityTypes = entityTypes;
@@ -172,6 +175,7 @@
         mActionIndices = actionIndex;
         mLanguage = language;
         mScore = score;
+        mModelName = modelVersion;
     }
 
     @Override
@@ -196,6 +200,7 @@
         dest.writeIntArray(mActionIndices);
         dest.writeString(mLanguage);
         dest.writeFloat(mScore);
+        dest.writeString(mModelName);
     }
 
     private static TextClassifierEvent readFromParcel(Parcel in) {
@@ -214,7 +219,8 @@
                 /* relativeSuggestedWordEndIndex= */ in.readInt(),
                 /* actionIndices= */ in.createIntArray(),
                 /* language= */ in.readString(),
-                /* score= */ in.readFloat());
+                /* score= */ in.readFloat(),
+                /* modelVersion= */ in.readString());
     }
 
     /**
@@ -264,6 +270,7 @@
         return mEventIndex;
     }
 
+    // TODO: Remove this API.
     /**
      * Returns the time this event occurred. This is the number of milliseconds since
      * January 1, 1970, 00:00:00 GMT. 0 indicates not set.
@@ -339,6 +346,15 @@
     }
 
     /**
+     * Returns the model name.
+     * @hide
+     */
+    @Nullable
+    public String getModelName() {
+        return mModelName;
+    }
+
+    /**
      * Builder to build a text classifier event.
      */
     public static final class Builder {
@@ -359,6 +375,8 @@
         @Nullable private String mLanguage;
         private float mScore;
 
+        private String mModelName;
+
         /**
          * Creates a builder for building {@link TextClassifierEvent}s.
          *
@@ -407,6 +425,7 @@
             return this;
         }
 
+        // TODO: Remove this API.
         /**
          * Sets the time this event occurred. This is the number of milliseconds since
          * January 1, 1970, 00:00:00 GMT. 0 indicates not set.
@@ -501,6 +520,15 @@
         }
 
         /**
+         * Sets the model name string.
+         * @hide
+         */
+        public Builder setModelName(@Nullable String modelVersion) {
+            mModelName = modelVersion;
+            return this;
+        }
+
+        /**
          * Builds and returns a text classifier event.
          */
         @NonNull
@@ -521,7 +549,8 @@
                     mRelativeSuggestedWordEndIndex,
                     mActionIndices,
                     mLanguage,
-                    mScore);
+                    mScore,
+                    mModelName);
         }
         // TODO: Add build(boolean validate).
     }
@@ -544,6 +573,7 @@
         out.append(", mActionIndices=").append(Arrays.toString(mActionIndices));
         out.append(", mLanguage=").append(mLanguage);
         out.append(", mScore=").append(mScore);
+        out.append(", mModelName=").append(mModelName);
         out.append("}");
         return out.toString();
     }
diff --git a/core/java/android/view/textclassifier/TextClassifierEventTronLogger.java b/core/java/android/view/textclassifier/TextClassifierEventTronLogger.java
index 5563dfc..6a12250 100644
--- a/core/java/android/view/textclassifier/TextClassifierEventTronLogger.java
+++ b/core/java/android/view/textclassifier/TextClassifierEventTronLogger.java
@@ -16,7 +16,6 @@
 package android.view.textclassifier;
 
 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_TEXTCLASSIFIER_MODEL;
-import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_TEXT_CLASSIFIER_EVENT_TIME;
 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_TEXT_CLASSIFIER_FIRST_ENTITY_TYPE;
 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_TEXT_CLASSIFIER_SCORE;
 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_TEXT_CLASSIFIER_SECOND_ENTITY_TYPE;
@@ -46,7 +45,7 @@
     private final MetricsLogger mMetricsLogger;
 
     public TextClassifierEventTronLogger() {
-        mMetricsLogger = new MetricsLogger();
+        this(new MetricsLogger());
     }
 
     @VisibleForTesting
@@ -57,6 +56,7 @@
     /** Emits a text classifier event to the logs. */
     public void writeEvent(TextClassifierEvent event) {
         Preconditions.checkNotNull(event);
+
         int category = getCategory(event);
         if (category == -1) {
             Log.w(TAG, "Unknown category: " + event.getEventCategory());
@@ -65,14 +65,12 @@
         final LogMaker log = new LogMaker(category)
                 .setSubtype(getLogType(event))
                 .addTaggedData(FIELD_TEXT_CLASSIFIER_SESSION_ID, event.getResultId())
-                .addTaggedData(FIELD_TEXT_CLASSIFIER_EVENT_TIME, event.getEventTime())
-                .addTaggedData(FIELD_TEXTCLASSIFIER_MODEL,
-                        SelectionSessionLogger.SignatureParser.getModelName(event.getResultId()))
+                .addTaggedData(FIELD_TEXTCLASSIFIER_MODEL, getModelName(event))
                 .addTaggedData(FIELD_TEXT_CLASSIFIER_SCORE, event.getScore());
 
         String[] entityTypes = event.getEntityTypes();
-        // TRON does not support a field of list type, and thus workaround by store them
-        // in three separate fields. This is no longer an issue once we have moved to Westworld.
+        // The old logger does not support a field of list type, and thus workaround by store them
+        // in three separate fields. This is not an issue with the new logger.
         if (entityTypes.length >= 1) {
             log.addTaggedData(FIELD_TEXT_CLASSIFIER_FIRST_ENTITY_TYPE, entityTypes[0]);
         }
@@ -93,6 +91,13 @@
         debugLog(log);
     }
 
+    private static String getModelName(TextClassifierEvent event) {
+        if (event.getModelName() != null) {
+            return event.getModelName();
+        }
+        return SelectionSessionLogger.SignatureParser.getModelName(event.getResultId());
+    }
+
     private static int getCategory(TextClassifierEvent event) {
         switch (event.getEventCategory()) {
             case TextClassifierEvent.CATEGORY_CONVERSATION_ACTIONS:
diff --git a/core/java/android/view/textclassifier/TextClassifierImpl.java b/core/java/android/view/textclassifier/TextClassifierImpl.java
index c297928..14afa33 100644
--- a/core/java/android/view/textclassifier/TextClassifierImpl.java
+++ b/core/java/android/view/textclassifier/TextClassifierImpl.java
@@ -65,9 +65,6 @@
  */
 public final class TextClassifierImpl implements TextClassifier {
 
-    /** @hide */
-    public static final String ACTIONS_INTENTS = "actions-intents";
-
     private static final String LOG_TAG = DEFAULT_LOG_TAG;
 
     private static final boolean DEBUG = false;
@@ -343,7 +340,11 @@
         if (DEBUG) {
             Log.d(DEFAULT_LOG_TAG, "onTextClassifierEvent() called with: event = [" + event + "]");
         }
-        mTextClassifierEventTronLogger.writeEvent(event);
+        try {
+            mTextClassifierEventTronLogger.writeEvent(event);
+        } catch (Exception e) {
+            Log.e(LOG_TAG, "Error writing event", e);
+        }
     }
 
     /** @inheritDoc */
@@ -566,12 +567,16 @@
         final float foreignTextThreshold = mSettings.getLangIdThresholdOverride() >= 0
                 ? mSettings.getLangIdThresholdOverride()
                 : 0.5f /* TODO: Load this from the langId model. */;
+        final Bundle foreignLanguageBundle =
+                detectForeignLanguage(classifiedText, foreignTextThreshold);
+        builder.setForeignLanguageExtra(foreignLanguageBundle);
+
         boolean isPrimaryAction = true;
         final ArrayList<Intent> sourceIntents = new ArrayList<>();
         List<LabeledIntent> labeledIntents = mIntentFactory.create(
                 mContext,
                 classifiedText,
-                isForeignText(classifiedText, foreignTextThreshold),
+                foreignLanguageBundle != null,
                 referenceTime,
                 highestScoringResult);
         for (LabeledIntent labeledIntent : labeledIntents) {
@@ -590,28 +595,28 @@
                                 labeledIntent.getIntent(), labeledIntent.getRequestCode())));
                 isPrimaryAction = false;
             }
-            builder.addAction(action);
-            sourceIntents.add(labeledIntent.getIntent());
+            builder.addAction(action, labeledIntent.getIntent());
         }
 
-        final Bundle extras = new Bundle();
-        extras.putParcelableArrayList(ACTIONS_INTENTS, sourceIntents);
-
-        return builder.setId(createId(text, start, end))
-                .setExtras(extras)
-                .build();
+        return builder.setId(createId(text, start, end)).build();
     }
 
-    private boolean isForeignText(String text, float threshold) {
+    /**
+     * Returns a bundle with the language and confidence score if it finds the text to be
+     * in a foreign language. Otherwise returns null.
+     */
+    @Nullable
+    private Bundle detectForeignLanguage(String text, float threshold) {
         if (threshold > 1) {
-            return false;
+            return null;
         }
 
         // TODO: Revisit this algorithm.
         try {
-            final LangIdModel.LanguageResult[] langResults = getLangIdImpl().detectLanguages(text);
+            final LangIdModel langId = getLangIdImpl();
+            final LangIdModel.LanguageResult[] langResults = langId.detectLanguages(text);
             if (langResults.length <= 0) {
-                return false;
+                return null;
             }
 
             LangIdModel.LanguageResult highestScoringResult = langResults[0];
@@ -621,7 +626,7 @@
                 }
             }
             if (highestScoringResult.getScore() < threshold) {
-                return false;
+                return null;
             }
             // TODO: Remove
             Log.d(LOG_TAG, String.format("Language detected: <%s:%s>",
@@ -632,14 +637,15 @@
             final int size = deviceLocales.size();
             for (int i = 0; i < size; i++) {
                 if (deviceLocales.get(i).getLanguage().equals(detected.getLanguage())) {
-                    return false;
+                    return null;
                 }
             }
-            return true;
+            return ExtrasUtils.createForeignLanguageExtra(
+                    detected.getLanguage(), highestScoringResult.getScore(), langId.getVersion());
         } catch (Throwable t) {
             Log.e(LOG_TAG, "Error detecting foreign text. Ignored.", t);
         }
-        return false;
+        return null;
     }
 
     @Override
@@ -773,3 +779,4 @@
         }
     }
 }
+
diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java
index 4a60b6a..e9d72a2 100644
--- a/core/java/android/widget/Editor.java
+++ b/core/java/android/widget/Editor.java
@@ -4112,9 +4112,9 @@
             }
         }
 
-        private MenuItem addAssistMenuItem(Menu menu, RemoteAction action, int intemId, int order,
+        private MenuItem addAssistMenuItem(Menu menu, RemoteAction action, int itemId, int order,
                 int showAsAction) {
-            final MenuItem item = menu.add(TextView.ID_ASSIST, intemId, order, action.getTitle())
+            final MenuItem item = menu.add(TextView.ID_ASSIST, itemId, order, action.getTitle())
                     .setContentDescription(action.getContentDescription());
             if (action.shouldShowIcon()) {
                 item.setIcon(action.getIcon().loadDrawable(mTextView.getContext()));
@@ -4188,7 +4188,8 @@
 
         @Override
         public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
-            getSelectionActionModeHelper().onSelectionAction(item.getItemId());
+            getSelectionActionModeHelper()
+                    .onSelectionAction(item.getItemId(), item.getTitle().toString());
 
             if (mProcessTextIntentActionsHandler.performMenuItemAction(item)) {
                 return true;
@@ -4488,6 +4489,13 @@
         // when selecting text when the handles jump to the end / start of words which may be on
         // a different line.
         protected int mPreviousLineTouched = UNSET_LINE;
+        // The raw x coordinate of the motion down event which started the current dragging session.
+        // Only used and stored when magnifier is used.
+        private float mCurrentDragInitialTouchRawX = UNSET_X_VALUE;
+        // The scale transform applied by containers to the TextView. Only used and computed
+        // when magnifier is used.
+        private float mTextViewScaleX;
+        private float mTextViewScaleY;
 
         private HandleView(Drawable drawableLtr, Drawable drawableRtl, final int id) {
             super(mTextView.getContext());
@@ -4807,25 +4815,44 @@
                             / mMagnifierAnimator.mMagnifier.getZoom());
             final Paint.FontMetrics fontMetrics = mTextView.getPaint().getFontMetrics();
             final float glyphHeight = fontMetrics.descent - fontMetrics.ascent;
-            return glyphHeight > magnifierContentHeight;
+            return glyphHeight * mTextViewScaleY > magnifierContentHeight;
         }
 
-        private boolean viewIsMatrixTransformed() {
+        /**
+         * Traverses the hierarchy above the text view, and computes the total scale applied
+         * to it. If a rotation is encountered, the method returns {@code false}, indicating
+         * that the magnifier should not be shown anyways. It would be nice to keep these two
+         * pieces of logic separate (the rotation check and the total scale calculation),
+         * but for efficiency we can do them in a single go.
+         * @return whether the text view is rotated
+         */
+        private boolean checkForTransforms() {
             if (mMagnifierAnimator.mMagnifierIsShowing) {
                 // Do not check again when the magnifier is currently showing.
-                return false;
-            }
-            if (!mTextView.hasIdentityMatrix()) {
                 return true;
             }
+
+            if (mTextView.getRotation() != 0f || mTextView.getRotationX() != 0f
+                    || mTextView.getRotationY() != 0f) {
+                return false;
+            }
+            mTextViewScaleX = mTextView.getScaleX();
+            mTextViewScaleY = mTextView.getScaleY();
+
             ViewParent viewParent = mTextView.getParent();
             while (viewParent != null) {
-                if (viewParent instanceof View && !((View) viewParent).hasIdentityMatrix()) {
-                    return true;
+                if (viewParent instanceof View) {
+                    final View view = (View) viewParent;
+                    if (view.getRotation() != 0f || view.getRotationX() != 0f
+                            || view.getRotationY() != 0f) {
+                        return false;
+                    }
+                    mTextViewScaleX *= view.getScaleX();
+                    mTextViewScaleY *= view.getScaleY();
                 }
                 viewParent = viewParent.getParent();
             }
-            return false;
+            return true;
         }
 
         /**
@@ -4863,6 +4890,12 @@
                 return false;
             }
 
+            if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
+                mCurrentDragInitialTouchRawX = event.getRawX();
+            } else if (event.getActionMasked() == MotionEvent.ACTION_UP) {
+                mCurrentDragInitialTouchRawX = UNSET_X_VALUE;
+            }
+
             final Layout layout = mTextView.getLayout();
             final int lineNumber = layout.getLineForOffset(offset);
             // Compute whether the selection handles are currently on the same line, and,
@@ -4890,6 +4923,8 @@
             } else {
                 rightBound += mTextView.getLayout().getLineRight(lineNumber);
             }
+            leftBound *= mTextViewScaleX;
+            rightBound *= mTextViewScaleX;
             final float contentWidth = Math.round(mMagnifierAnimator.mMagnifier.getWidth()
                     / mMagnifierAnimator.mMagnifier.getZoom());
             if (touchXInView < leftBound - contentWidth / 2
@@ -4897,13 +4932,27 @@
                 // The touch is too far from the current line / selection, so hide the magnifier.
                 return false;
             }
-            showPosInView.x = Math.max(leftBound, Math.min(rightBound, touchXInView));
+
+            final float scaledTouchXInView;
+            if (mTextViewScaleX == 1f) {
+                // In the common case, do not use mCurrentDragInitialTouchRawX to compute this
+                // coordinate, although the formula on the else branch should be equivalent.
+                // Since the formula relies on mCurrentDragInitialTouchRawX being set on
+                // MotionEvent.ACTION_DOWN, this makes us more defensive against cases when
+                // the sequence of events might not look as expected: for example, a sequence of
+                // ACTION_MOVE not preceded by ACTION_DOWN.
+                scaledTouchXInView = touchXInView;
+            } else {
+                scaledTouchXInView = (event.getRawX() - mCurrentDragInitialTouchRawX)
+                        * mTextViewScaleX + mCurrentDragInitialTouchRawX
+                        - textViewLocationOnScreen[0];
+            }
+            showPosInView.x = Math.max(leftBound, Math.min(rightBound, scaledTouchXInView));
 
             // Vertically snap to middle of current line.
-            showPosInView.y = (mTextView.getLayout().getLineTop(lineNumber)
+            showPosInView.y = ((mTextView.getLayout().getLineTop(lineNumber)
                     + mTextView.getLayout().getLineBottom(lineNumber)) / 2.0f
-                    + mTextView.getTotalPaddingTop() - mTextView.getScrollY();
-
+                    + mTextView.getTotalPaddingTop() - mTextView.getScrollY()) * mTextViewScaleY;
             return true;
         }
 
@@ -4955,8 +5004,8 @@
             }
 
             final PointF showPosInView = new PointF();
-            final boolean shouldShow = !tooLargeTextForMagnifier()
-                    && !viewIsMatrixTransformed()
+            final boolean shouldShow = checkForTransforms() /*check not rotated and compute scale*/
+                    && !tooLargeTextForMagnifier()
                     && obtainMagnifierShowCoordinates(event, showPosInView);
             if (shouldShow) {
                 // Make the cursor visible and stop blinking.
diff --git a/core/java/android/widget/Magnifier.java b/core/java/android/widget/Magnifier.java
index 5147306..d5b1a3d 100644
--- a/core/java/android/widget/Magnifier.java
+++ b/core/java/android/widget/Magnifier.java
@@ -579,24 +579,11 @@
             zoomCenterY = Math.round(yPosInView + mViewCoordinatesInSurface[1]);
         }
 
-        final Rect[] bounds = new Rect[3]; // [MAX_IN_SURFACE, MAX_IN_VIEW, MAX_VISIBLE]
+        final Rect[] bounds = new Rect[2]; // [MAX_IN_SURFACE, MAX_VISIBLE]
         // Obtain the surface bounds rectangle.
         final Rect surfaceBounds = new Rect(0, 0,
                 mContentCopySurface.mWidth, mContentCopySurface.mHeight);
         bounds[0] = surfaceBounds;
-        // Obtain the view bounds rectangle.
-        final Rect viewBounds;
-        if (mView instanceof SurfaceView) {
-            viewBounds = new Rect(0, 0, mContentCopySurface.mWidth, mContentCopySurface.mHeight);
-        } else {
-            viewBounds = new Rect(
-                    mViewCoordinatesInSurface[0],
-                    mViewCoordinatesInSurface[1],
-                    mViewCoordinatesInSurface[0] + mView.getWidth(),
-                    mViewCoordinatesInSurface[1] + mView.getHeight()
-            );
-        }
-        bounds[1] = viewBounds;
         // Obtain the visible view region rectangle.
         final Rect viewVisibleRegion = new Rect();
         mView.getGlobalVisibleRect(viewVisibleRegion);
@@ -609,7 +596,7 @@
             // If we copy content from a SurfaceView, clamp coordinates relative to it.
             viewVisibleRegion.offset(-mViewCoordinatesInSurface[0], -mViewCoordinatesInSurface[1]);
         }
-        bounds[2] = viewVisibleRegion;
+        bounds[1] = viewVisibleRegion;
 
         // Aggregate the above to obtain the bounds where the content copy will be restricted.
         int resolvedLeft = Integer.MIN_VALUE;
@@ -1301,11 +1288,6 @@
          *   {@link android.view.View#getGlobalVisibleRect(Rect)}. For example, this will take into
          *   account the case when the view is contained in a scrollable container, and the
          *   magnifier will refuse to copy content outside of the visible view region</li>
-         *   <li>{@link #SOURCE_BOUND_MAX_IN_VIEW}, which extends the bound as much as possible
-         *   while remaining in the bounds of the view. Note that, although this option is
-         *   used, the magnifier will always only display content visible on the screen: if the
-         *   view lays outside the screen or is covered by a different view either partially or
-         *   totally, the magnifier will not show any view region not visible on the screen.</li>
          *   <li>{@link #SOURCE_BOUND_MAX_IN_SURFACE}, which extends the bound as much
          *   as possible while remaining inside the surface the content is copied from.</li>
          * </ul>
@@ -1349,21 +1331,14 @@
      * A source bound that will extend as much as possible, while remaining within the surface
      * the content is copied from.
      */
-
     public static final int SOURCE_BOUND_MAX_IN_SURFACE = 0;
-    /**
-     * A source bound that will extend as much as possible, while remaining within the
-     * magnified view.
-     */
-
-    public static final int SOURCE_BOUND_MAX_IN_VIEW = 1;
 
     /**
      * A source bound that will extend as much as possible, while remaining within the
      * visible region of the magnified view, as determined by
      * {@link View#getGlobalVisibleRect(Rect)}.
      */
-    public static final int SOURCE_BOUND_MAX_VISIBLE = 2;
+    public static final int SOURCE_BOUND_MAX_VISIBLE = 1;
 
 
     /**
@@ -1373,7 +1348,7 @@
      *
      * @hide
      */
-    @IntDef({SOURCE_BOUND_MAX_IN_SURFACE, SOURCE_BOUND_MAX_IN_VIEW, SOURCE_BOUND_MAX_VISIBLE})
+    @IntDef({SOURCE_BOUND_MAX_IN_SURFACE, SOURCE_BOUND_MAX_VISIBLE})
     @Retention(RetentionPolicy.SOURCE)
     public @interface SourceBound {}
 
diff --git a/core/java/android/widget/SelectionActionModeHelper.java b/core/java/android/widget/SelectionActionModeHelper.java
index 4caf288..564cfdd 100644
--- a/core/java/android/widget/SelectionActionModeHelper.java
+++ b/core/java/android/widget/SelectionActionModeHelper.java
@@ -20,12 +20,14 @@
 import android.annotation.Nullable;
 import android.annotation.UiThread;
 import android.annotation.WorkerThread;
+import android.app.RemoteAction;
 import android.content.Context;
 import android.graphics.Canvas;
 import android.graphics.PointF;
 import android.graphics.RectF;
 import android.os.AsyncTask;
 import android.os.Build;
+import android.os.Bundle;
 import android.os.LocaleList;
 import android.text.Layout;
 import android.text.Selection;
@@ -34,13 +36,16 @@
 import android.text.util.Linkify;
 import android.util.Log;
 import android.view.ActionMode;
+import android.view.textclassifier.ExtrasUtils;
 import android.view.textclassifier.SelectionEvent;
 import android.view.textclassifier.SelectionEvent.InvocationMethod;
 import android.view.textclassifier.SelectionSessionLogger;
 import android.view.textclassifier.TextClassification;
 import android.view.textclassifier.TextClassificationConstants;
+import android.view.textclassifier.TextClassificationContext;
 import android.view.textclassifier.TextClassificationManager;
 import android.view.textclassifier.TextClassifier;
+import android.view.textclassifier.TextClassifierEvent;
 import android.view.textclassifier.TextSelection;
 import android.widget.Editor.SelectionModifierCursorController;
 
@@ -166,16 +171,17 @@
         }
     }
 
-    public void onSelectionAction(int menuItemId) {
+    /** Reports a selection action event. */
+    public void onSelectionAction(int menuItemId, @Nullable String actionLabel) {
         mSelectionTracker.onSelectionAction(
                 mTextView.getSelectionStart(), mTextView.getSelectionEnd(),
-                getActionType(menuItemId), mTextClassification);
+                getActionType(menuItemId), actionLabel, mTextClassification);
     }
 
     public void onSelectionDrag() {
         mSelectionTracker.onSelectionAction(
                 mTextView.getSelectionStart(), mTextView.getSelectionEnd(),
-                SelectionEvent.ACTION_DRAG, mTextClassification);
+                SelectionEvent.ACTION_DRAG, /* actionLabel= */ null, mTextClassification);
     }
 
     public void onTextChanged(int start, int end) {
@@ -511,8 +517,11 @@
             mOriginalEnd = mSelectionEnd = selectionEnd;
             mAllowReset = false;
             maybeInvalidateLogger();
-            mLogger.logSelectionStarted(mTextView.getTextClassificationSession(),
-                    text, selectionStart,
+            mLogger.logSelectionStarted(
+                    mTextView.getTextClassificationSession(),
+                    mTextView.getTextClassificationContext(),
+                    text,
+                    selectionStart,
                     isLink ? SelectionEvent.INVOCATION_LINK : SelectionEvent.INVOCATION_MANUAL);
         }
 
@@ -570,10 +579,12 @@
         public void onSelectionAction(
                 int selectionStart, int selectionEnd,
                 @SelectionEvent.ActionType int action,
+                @Nullable String actionLabel,
                 @Nullable TextClassification classification) {
             if (isSelectionStarted()) {
                 mAllowReset = false;
-                mLogger.logSelectionAction(selectionStart, selectionEnd, action, classification);
+                mLogger.logSelectionAction(
+                        selectionStart, selectionEnd, action, actionLabel, classification);
             }
         }
 
@@ -596,7 +607,8 @@
                     mSelectionEnd = editor.getTextView().getSelectionEnd();
                     mLogger.logSelectionAction(
                             textView.getSelectionStart(), textView.getSelectionEnd(),
-                            SelectionEvent.ACTION_RESET, null /* classification */);
+                            SelectionEvent.ACTION_RESET,
+                            /* actionLabel= */ null, /* classification= */ null);
                 }
                 return selected;
             }
@@ -605,7 +617,9 @@
 
         public void onTextChanged(int start, int end, TextClassification classification) {
             if (isSelectionStarted() && start == mSelectionStart && end == mSelectionEnd) {
-                onSelectionAction(start, end, SelectionEvent.ACTION_OVERTYPE, classification);
+                onSelectionAction(
+                        start, end, SelectionEvent.ACTION_OVERTYPE,
+                        /* actionLabel= */ null, classification);
             }
         }
 
@@ -644,7 +658,8 @@
                 if (mIsPending) {
                     mLogger.logSelectionAction(
                             mSelectionStart, mSelectionEnd,
-                            SelectionEvent.ACTION_ABANDON, null /* classification */);
+                            SelectionEvent.ACTION_ABANDON,
+                            /* actionLabel= */ null, /* classification= */ null);
                     mSelectionStart = mSelectionEnd = -1;
                     mLogger.endTextClassificationSession();
                     mIsPending = false;
@@ -679,6 +694,11 @@
         private final BreakIterator mTokenIterator;
 
         @Nullable private TextClassifier mClassificationSession;
+        @Nullable private TextClassificationContext mClassificationContext;
+
+        @Nullable private TextClassifierEvent mTranslateViewEvent;
+        @Nullable private TextClassifierEvent mTranslateClickEvent;
+
         private int mStartIndex;
         private String mText;
 
@@ -690,6 +710,7 @@
 
         public void logSelectionStarted(
                 TextClassifier classificationSession,
+                TextClassificationContext classificationContext,
                 CharSequence text, int index,
                 @InvocationMethod int invocationMethod) {
             try {
@@ -701,6 +722,7 @@
                 mTokenIterator.setText(mText);
                 mStartIndex = index;
                 mClassificationSession = classificationSession;
+                mClassificationContext = classificationContext;
                 if (hasActiveClassificationSession()) {
                     mClassificationSession.onSelectionEvent(
                             SelectionEvent.createSelectionStartedEvent(invocationMethod, 0));
@@ -731,6 +753,7 @@
                                 SelectionEvent.createSelectionModifiedEvent(
                                         wordIndices[0], wordIndices[1]));
                     }
+                    maybeGenerateTranslateViewEvent(classification);
                 }
             } catch (Exception e) {
                 // Avoid crashes due to logging.
@@ -741,6 +764,7 @@
         public void logSelectionAction(
                 int start, int end,
                 @SelectionEvent.ActionType int action,
+                @Nullable String actionLabel,
                 @Nullable TextClassification classification) {
             try {
                 if (hasActiveClassificationSession()) {
@@ -757,6 +781,9 @@
                                 SelectionEvent.createSelectionActionEvent(
                                         wordIndices[0], wordIndices[1], action));
                     }
+
+                    maybeGenerateTranslateClickEvent(classification, actionLabel);
+
                     if (SelectionEvent.isTerminal(action)) {
                         endTextClassificationSession();
                     }
@@ -773,6 +800,7 @@
 
         public void endTextClassificationSession() {
             if (hasActiveClassificationSession()) {
+                maybeReportTranslateEvents();
                 mClassificationSession.destroy();
             }
         }
@@ -843,6 +871,78 @@
         private boolean isWhitespace(int start, int end) {
             return PATTERN_WHITESPACE.matcher(mText.substring(start, end)).matches();
         }
+
+        private void maybeGenerateTranslateViewEvent(@Nullable TextClassification classification) {
+            if (classification != null) {
+                final TextClassifierEvent event = generateTranslateEvent(
+                        TextClassifierEvent.TYPE_ACTIONS_SHOWN,
+                        classification, mClassificationContext, /* actionLabel= */null);
+                mTranslateViewEvent = (event != null) ? event : mTranslateViewEvent;
+            }
+        }
+
+        private void maybeGenerateTranslateClickEvent(
+                @Nullable TextClassification classification, String actionLabel) {
+            if (classification != null) {
+                mTranslateClickEvent = generateTranslateEvent(
+                        TextClassifierEvent.TYPE_SMART_ACTION,
+                        classification, mClassificationContext, actionLabel);
+            }
+        }
+
+        private void maybeReportTranslateEvents() {
+            // Translate view and click events should only be logged once per selection session.
+            if (mTranslateViewEvent != null) {
+                mClassificationSession.onTextClassifierEvent(mTranslateViewEvent);
+                mTranslateViewEvent = null;
+            }
+            if (mTranslateClickEvent != null) {
+                mClassificationSession.onTextClassifierEvent(mTranslateClickEvent);
+                mTranslateClickEvent = null;
+            }
+        }
+
+        @Nullable
+        private static TextClassifierEvent generateTranslateEvent(
+                int eventType, TextClassification classification,
+                TextClassificationContext classificationContext, @Nullable String actionLabel) {
+
+            // The platform attempts to log "views" and "clicks" of the "Translate" action.
+            // Views are logged if a user is presented with the translate action during a selection
+            // session.
+            // Clicks are logged if the user clicks on the translate action.
+            // The index of the translate action is also logged to indicate whether it might have
+            // been in the main panel or overflow panel of the selection toolbar.
+            // NOTE that the "views" metric may be flawed if a TextView removes the translate menu
+            // item via a custom action mode callback or does not show a selection menu item.
+
+            final RemoteAction translateAction = ExtrasUtils.findTranslateAction(classification);
+            if (translateAction == null) {
+                // No translate action present. Nothing to log. Exit.
+                return null;
+            }
+
+            if (eventType == TextClassifierEvent.TYPE_SMART_ACTION
+                    && !translateAction.getTitle().toString().equals(actionLabel)) {
+                // Clicked action is not a translate action. Nothing to log. Exit.
+                // Note that we don't expect an actionLabel for "view" events.
+                return null;
+            }
+
+            final Bundle foreignLanguageExtra = ExtrasUtils.getForeignLanguageExtra(classification);
+            final String language = ExtrasUtils.getEntityType(foreignLanguageExtra);
+            final float score = ExtrasUtils.getScore(foreignLanguageExtra);
+            final String model = ExtrasUtils.getModelName(foreignLanguageExtra);
+            return new TextClassifierEvent.Builder(
+                    TextClassifierEvent.CATEGORY_LANGUAGE_DETECTION, eventType)
+                    .setEventContext(classificationContext)
+                    .setResultId(classification.getId())
+                    .setEntityTypes(language)
+                    .setScore(score)
+                    .setActionIndices(classification.getActions().indexOf(translateAction))
+                    .setModelName(model)
+                    .build();
+        }
     }
 
     /**
diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java
index 2b45c06..8029cf0 100644
--- a/core/java/android/widget/TextView.java
+++ b/core/java/android/widget/TextView.java
@@ -456,6 +456,7 @@
 
     private TextClassifier mTextClassifier;
     private TextClassifier mTextClassificationSession;
+    private TextClassificationContext mTextClassificationContext;
 
     // A flag to prevent repeated movements from escaping the enclosing text view. The idea here is
     // that if a user is holding down a movement key to traverse text, we shouldn't also traverse
@@ -12068,16 +12069,15 @@
                 } else {
                     widgetType = TextClassifier.WIDGET_TYPE_UNSELECTABLE_TEXTVIEW;
                 }
-                final TextClassificationContext textClassificationContext =
-                        new TextClassificationContext.Builder(
-                                mContext.getPackageName(), widgetType)
-                                .build();
+                mTextClassificationContext = new TextClassificationContext.Builder(
+                        mContext.getPackageName(), widgetType)
+                        .build();
                 if (mTextClassifier != null) {
                     mTextClassificationSession = tcm.createTextClassificationSession(
-                            textClassificationContext, mTextClassifier);
+                            mTextClassificationContext, mTextClassifier);
                 } else {
                     mTextClassificationSession = tcm.createTextClassificationSession(
-                            textClassificationContext);
+                            mTextClassificationContext);
                 }
             } else {
                 mTextClassificationSession = TextClassifier.NO_OP;
@@ -12087,6 +12087,15 @@
     }
 
     /**
+     * Returns the {@link TextClassificationContext} for the current TextClassifier session.
+     * @see #getTextClassificationSession()
+     */
+    @Nullable
+    TextClassificationContext getTextClassificationContext() {
+        return mTextClassificationContext;
+    }
+
+    /**
      * Returns true if this TextView uses a no-op TextClassifier.
      */
     boolean usesNoOpTextClassifier() {
diff --git a/core/java/com/android/internal/app/ChooserActivity.java b/core/java/com/android/internal/app/ChooserActivity.java
index 46f42f7..cfe2939 100644
--- a/core/java/com/android/internal/app/ChooserActivity.java
+++ b/core/java/com/android/internal/app/ChooserActivity.java
@@ -25,6 +25,7 @@
 import android.app.prediction.AppTargetEvent;
 import android.app.prediction.AppTargetId;
 import android.content.ClipData;
+import android.content.ClipboardManager;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
@@ -80,11 +81,13 @@
 import android.view.ViewGroup.LayoutParams;
 import android.widget.AbsListView;
 import android.widget.BaseAdapter;
+import android.widget.Button;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
 import android.widget.ListView;
 import android.widget.Space;
 import android.widget.TextView;
+import android.widget.Toast;
 
 import com.android.internal.R;
 import com.android.internal.annotations.VisibleForTesting;
@@ -350,6 +353,50 @@
         super.onCreate(savedInstanceState, target, title, defaultTitleRes, initialIntents,
                 null, false);
 
+        Button copyButton = findViewById(R.id.copy_button);
+        copyButton.setOnClickListener(view -> {
+            Intent targetIntent = getTargetIntent();
+            if (targetIntent == null) {
+                finish();
+            } else {
+                final String action = targetIntent.getAction();
+
+                ClipData clipData = null;
+                if (Intent.ACTION_SEND.equals(action)) {
+                    String extraText = targetIntent.getStringExtra(Intent.EXTRA_TEXT);
+                    Uri extraStream = targetIntent.getParcelableExtra(Intent.EXTRA_STREAM);
+
+                    if (extraText != null) {
+                        clipData = ClipData.newPlainText(null, extraText);
+                    } else if (extraStream != null) {
+                        clipData = ClipData.newUri(getContentResolver(), null, extraStream);
+                    } else {
+                        Log.w(TAG, "No data available to copy to clipboard");
+                        return;
+                    }
+                } else if (Intent.ACTION_SEND_MULTIPLE.equals(action)) {
+                    final ArrayList<Uri> streams = targetIntent.getParcelableArrayListExtra(
+                            Intent.EXTRA_STREAM);
+                    clipData = ClipData.newUri(getContentResolver(), null, streams.get(0));
+                    for (int i = 1; i < streams.size(); i++) {
+                        clipData.addItem(getContentResolver(), new ClipData.Item(streams.get(i)));
+                    }
+                } else {
+                    // expected to only be visible with ACTION_SEND or ACTION_SEND_MULTIPLE
+                    // so warn about unexpected action
+                    Log.w(TAG, "Action (" + action + ") not supported for copying to clipboard");
+                    return;
+                }
+
+                ClipboardManager clipboardManager = (ClipboardManager) getSystemService(
+                        Context.CLIPBOARD_SERVICE);
+                clipboardManager.setPrimaryClip(clipData);
+                Toast.makeText(getApplicationContext(), R.string.copied, Toast.LENGTH_SHORT).show();
+
+                finish();
+            }
+        });
+
         MetricsLogger.action(this, MetricsEvent.ACTION_ACTIVITY_CHOOSER_SHOWN);
 
         mChooserShownTime = System.currentTimeMillis();
@@ -414,39 +461,39 @@
     private void showDefaultContentPreview(final ViewGroup parentLayout,
             final Intent targetIntent) {
         CharSequence sharingText = targetIntent.getCharSequenceExtra(Intent.EXTRA_TEXT);
-        TextView previewTextView = findViewById(R.id.content_preview_text);
         if (sharingText == null) {
-            previewTextView.setVisibility(View.GONE);
+            findViewById(R.id.content_preview_text_layout).setVisibility(View.GONE);
         } else {
-            previewTextView.setText(sharingText);
+            TextView textView = findViewById(R.id.content_preview_text);
+            textView.setText(sharingText);
         }
 
         String previewTitle = targetIntent.getStringExtra(Intent.EXTRA_TITLE);
-        TextView previewTitleView = findViewById(R.id.content_preview_title);
-        if (previewTitle == null) {
-            previewTitleView.setVisibility(View.GONE);
+        if (previewTitle == null || previewTitle.trim().isEmpty()) {
+            findViewById(R.id.content_preview_title_layout).setVisibility(View.GONE);
         } else {
+            TextView previewTitleView = findViewById(R.id.content_preview_title);
             previewTitleView.setText(previewTitle);
-        }
 
-        ClipData previewData = targetIntent.getClipData();
-        Uri previewThumbnail = null;
-        if (previewData != null) {
-            if (previewData.getItemCount() > 0) {
-                ClipData.Item previewDataItem = previewData.getItemAt(0);
-                previewThumbnail = previewDataItem.getUri();
+            ClipData previewData = targetIntent.getClipData();
+            Uri previewThumbnail = null;
+            if (previewData != null) {
+                if (previewData.getItemCount() > 0) {
+                    ClipData.Item previewDataItem = previewData.getItemAt(0);
+                    previewThumbnail = previewDataItem.getUri();
+                }
             }
-        }
 
-        ImageView previewThumbnailView = findViewById(R.id.content_preview_thumbnail);
-        if (previewThumbnail == null) {
-            previewThumbnailView.setVisibility(View.GONE);
-        } else {
-            Bitmap bmp = loadThumbnail(previewThumbnail, new Size(200, 200));
-            if (bmp == null) {
+            ImageView previewThumbnailView = findViewById(R.id.content_preview_thumbnail);
+            if (previewThumbnail == null) {
                 previewThumbnailView.setVisibility(View.GONE);
             } else {
-                previewThumbnailView.setImageBitmap(bmp);
+                Bitmap bmp = loadThumbnail(previewThumbnail, new Size(100, 100));
+                if (bmp == null) {
+                    previewThumbnailView.setVisibility(View.GONE);
+                } else {
+                    previewThumbnailView.setImageBitmap(bmp);
+                }
             }
         }
     }
@@ -2020,8 +2067,8 @@
         private void updatePath(int width, int height) {
             mPath.reset();
 
-            int imageWidth = width - getPaddingLeft() - getPaddingRight();
-            int imageHeight = height - getPaddingTop() - getPaddingBottom();
+            int imageWidth = width - getPaddingRight();
+            int imageHeight = height - getPaddingBottom();
             mPath.addRoundRect(getPaddingLeft(), getPaddingTop(), imageWidth, imageHeight, mRadius,
                     mRadius, Path.Direction.CW);
         }
diff --git a/core/java/com/android/internal/hardware/AmbientDisplayConfiguration.java b/core/java/com/android/internal/hardware/AmbientDisplayConfiguration.java
index 7a00a51..e19a32e 100644
--- a/core/java/com/android/internal/hardware/AmbientDisplayConfiguration.java
+++ b/core/java/com/android/internal/hardware/AmbientDisplayConfiguration.java
@@ -38,7 +38,7 @@
         return pulseOnNotificationEnabled(user)
                 || pulseOnLongPressEnabled(user)
                 || alwaysOnEnabled(user)
-                || wakeLockScreenGestureEnabled(user);
+                || wakeScreenGestureEnabled(user);
     }
 
     public boolean pulseOnNotificationEnabled(int user) {
@@ -76,11 +76,6 @@
         return !TextUtils.isEmpty(doubleTapSensorType());
     }
 
-    public boolean wakeLockScreenGestureEnabled(int user) {
-        return boolSettingDefaultOn(Settings.Secure.DOZE_WAKE_LOCK_SCREEN_GESTURE, user)
-                && wakeScreenGestureAvailable();
-    }
-
     public boolean wakeScreenGestureAvailable() {
         return mContext.getResources()
                 .getBoolean(R.bool.config_dozeWakeLockScreenSensorAvailable);
diff --git a/core/java/com/android/internal/net/VpnConfig.java b/core/java/com/android/internal/net/VpnConfig.java
index da8605e..65b974b 100644
--- a/core/java/com/android/internal/net/VpnConfig.java
+++ b/core/java/com/android/internal/net/VpnConfig.java
@@ -104,6 +104,7 @@
     public boolean allowBypass;
     public boolean allowIPv4;
     public boolean allowIPv6;
+    public boolean isMetered = true;
     public Network[] underlyingNetworks;
     public ProxyInfo proxyInfo;
 
@@ -165,6 +166,7 @@
         out.writeInt(allowBypass ? 1 : 0);
         out.writeInt(allowIPv4 ? 1 : 0);
         out.writeInt(allowIPv6 ? 1 : 0);
+        out.writeInt(isMetered ? 1 : 0);
         out.writeTypedArray(underlyingNetworks, flags);
         out.writeParcelable(proxyInfo, flags);
     }
@@ -191,6 +193,7 @@
             config.allowBypass = in.readInt() != 0;
             config.allowIPv4 = in.readInt() != 0;
             config.allowIPv6 = in.readInt() != 0;
+            config.isMetered = in.readInt() != 0;
             config.underlyingNetworks = in.createTypedArray(Network.CREATOR);
             config.proxyInfo = in.readParcelable(null);
             return config;
diff --git a/core/java/com/android/internal/os/AppZygoteInit.java b/core/java/com/android/internal/os/AppZygoteInit.java
index 6ba584d..0e83e41 100644
--- a/core/java/com/android/internal/os/AppZygoteInit.java
+++ b/core/java/com/android/internal/os/AppZygoteInit.java
@@ -17,13 +17,15 @@
 package com.android.internal.os;
 
 import android.app.LoadedApk;
+import android.app.ZygotePreload;
+import android.content.ComponentName;
 import android.content.pm.ApplicationInfo;
 import android.net.LocalSocket;
 import android.util.Log;
 
 import java.io.DataOutputStream;
 import java.io.IOException;
-import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
 
 /**
@@ -76,20 +78,29 @@
 
             Zygote.allowAppFilesAcrossFork(appInfo);
 
-            Class<?> cl;
-            Method m;
-            try {
-                cl = Class.forName(appInfo.packageName + ".ZygotePreload", true, loader);
-                m = cl.getMethod("doPreload");
-                m.setAccessible(true);
-                m.invoke(null);
-            } catch (ClassNotFoundException e) {
-                // Don't treat this as an error since an app may not want to do any preloads
-                Log.w(TAG, "No ZygotePreload class found for " + appInfo.packageName);
-            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
-                Log.e(TAG, "AppZygote application preload failed for "
-                        + appInfo.packageName, e);
+            if (appInfo.zygotePreloadName != null) {
+                Class<?> cl;
+                Method m;
+                try {
+                    ComponentName preloadName = ComponentName.createRelative(appInfo.packageName,
+                            appInfo.zygotePreloadName);
+                    cl = Class.forName(preloadName.getClassName(), true, loader);
+                    if (!ZygotePreload.class.isAssignableFrom(cl)) {
+                        Log.e(TAG, preloadName.getClassName() + " does not implement "
+                                + ZygotePreload.class.getName());
+                    } else {
+                        Constructor<?> ctor = cl.getConstructor();
+                        ZygotePreload preloadObject = (ZygotePreload) ctor.newInstance();
+                        preloadObject.doPreload(appInfo);
+                    }
+                } catch (ReflectiveOperationException e) {
+                    Log.e(TAG, "AppZygote application preload failed for "
+                            + appInfo.zygotePreloadName, e);
+                }
+            } else {
+                Log.i(TAG, "No zygotePreloadName attribute specified.");
             }
+
             try {
                 DataOutputStream socketOut = getSocketOutputStream();
                 socketOut.writeInt(loader != null ? 1 : 0);
diff --git a/core/java/com/android/internal/os/OWNERS b/core/java/com/android/internal/os/OWNERS
new file mode 100644
index 0000000..9283105
--- /dev/null
+++ b/core/java/com/android/internal/os/OWNERS
@@ -0,0 +1 @@
+per-file ZygoteArguments.java,ZygoteConnection.java,ZygoteInit.java,Zygote.java,ZygoteServer.java = chriswailes@google.com, ngeoffray@google.com, sehr@google.com, narayan@google.com, maco@google.com
diff --git a/core/jni/OWNERS b/core/jni/OWNERS
index a365a56..86342c4 100644
--- a/core/jni/OWNERS
+++ b/core/jni/OWNERS
@@ -4,3 +4,6 @@
 
 # Connectivity
 per-file android_net_* = ek@google.com, lorenzo@google.com, satk@google.com
+
+# Zygote
+per-file com_android_inernal_os_Zygote.*,fd_utils.* = chriswailes@google.com, ngeoffray@google.com, sehr@google.com, narayan@google.com, maco@google.com
diff --git a/core/jni/android/graphics/Bitmap.cpp b/core/jni/android/graphics/Bitmap.cpp
index d215f96..e817217 100755
--- a/core/jni/android/graphics/Bitmap.cpp
+++ b/core/jni/android/graphics/Bitmap.cpp
@@ -1149,19 +1149,6 @@
     return createBitmap(env, bitmap.release(), getPremulBitmapCreateFlags(false));
 }
 
-static jobject Bitmap_createHardwareBitmap(JNIEnv* env, jobject, jobject graphicBuffer) {
-    sp<GraphicBuffer> buffer(graphicBufferForJavaObject(env, graphicBuffer));
-    // To support any color space, we need to pass an additional ColorSpace argument to
-    // java Bitmap.createHardwareBitmap.
-    SkColorType ct = uirenderer::PixelFormatToColorType(buffer->getPixelFormat());
-    sk_sp<Bitmap> bitmap = Bitmap::createFrom(buffer, ct, SkColorSpace::MakeSRGB());
-    if (!bitmap.get()) {
-        ALOGW("failed to create hardware bitmap from graphic buffer");
-        return NULL;
-    }
-    return bitmap::createBitmap(env, bitmap.release(), getPremulBitmapCreateFlags(false));
-}
-
 static jobject Bitmap_wrapHardwareBufferBitmap(JNIEnv* env, jobject, jobject hardwareBuffer,
                                                jlong colorSpacePtr) {
     AHardwareBuffer* hwBuf = android_hardware_HardwareBuffer_getNativeHardwareBuffer(env,
@@ -1257,8 +1244,6 @@
     {   "nativeGetAllocationByteCount", "(J)I", (void*)Bitmap_getAllocationByteCount },
     {   "nativeCopyPreserveInternalConfig", "(J)Landroid/graphics/Bitmap;",
         (void*)Bitmap_copyPreserveInternalConfig },
-    {   "nativeCreateHardwareBitmap", "(Landroid/graphics/GraphicBuffer;)Landroid/graphics/Bitmap;",
-        (void*) Bitmap_createHardwareBitmap },
     {   "nativeWrapHardwareBufferBitmap", "(Landroid/hardware/HardwareBuffer;J)Landroid/graphics/Bitmap;",
         (void*) Bitmap_wrapHardwareBufferBitmap },
     {   "nativeCreateGraphicBufferHandle", "(J)Landroid/graphics/GraphicBuffer;",
diff --git a/core/jni/android/graphics/BitmapFactory.cpp b/core/jni/android/graphics/BitmapFactory.cpp
index 3f9ec45..79bfa17 100644
--- a/core/jni/android/graphics/BitmapFactory.cpp
+++ b/core/jni/android/graphics/BitmapFactory.cpp
@@ -15,6 +15,7 @@
 #include "Utils.h"
 #include "core_jni_helpers.h"
 
+#include <HardwareBitmapUploader.h>
 #include <nativehelper/JNIHelp.h>
 #include <androidfw/Asset.h>
 #include <androidfw/ResourceTypes.h>
@@ -278,6 +279,11 @@
 
     // Set the decode colorType
     SkColorType decodeColorType = codec->computeOutputColorType(prefColorType);
+    if (decodeColorType == kRGBA_F16_SkColorType && isHardware &&
+            !uirenderer::HardwareBitmapUploader::hasFP16Support()) {
+        decodeColorType = kN32_SkColorType;
+    }
+
     sk_sp<SkColorSpace> decodeColorSpace = codec->computeOutputColorSpace(
             decodeColorType, prefColorSpace);
 
@@ -354,13 +360,7 @@
     const SkImageInfo decodeInfo = SkImageInfo::Make(size.width(), size.height(),
             decodeColorType, alphaType, decodeColorSpace);
 
-    // For wide gamut images, we will leave the color space on the SkBitmap.  Otherwise,
-    // use the default.
     SkImageInfo bitmapInfo = decodeInfo;
-    if (decodeInfo.colorSpace() && decodeInfo.colorSpace()->isSRGB()) {
-        bitmapInfo = bitmapInfo.makeColorSpace(decodeInfo.refColorSpace());
-    }
-
     if (decodeColorType == kGray_8_SkColorType) {
         // The legacy implementation of BitmapFactory used kAlpha8 for
         // grayscale images (before kGray8 existed).  While the codec
diff --git a/core/jni/android/graphics/BitmapRegionDecoder.cpp b/core/jni/android/graphics/BitmapRegionDecoder.cpp
index b4ba749..d65f324 100644
--- a/core/jni/android/graphics/BitmapRegionDecoder.cpp
+++ b/core/jni/android/graphics/BitmapRegionDecoder.cpp
@@ -33,6 +33,7 @@
 #include "android_util_Binder.h"
 #include "core_jni_helpers.h"
 
+#include <HardwareBitmapUploader.h>
 #include <nativehelper/JNIHelp.h>
 #include <androidfw/Asset.h>
 #include <binder/Parcel.h>
@@ -166,6 +167,10 @@
 
     SkBitmapRegionDecoder* brd = reinterpret_cast<SkBitmapRegionDecoder*>(brdHandle);
     SkColorType decodeColorType = brd->computeOutputColorType(colorType);
+    if (decodeColorType == kRGBA_F16_SkColorType && isHardware &&
+            !uirenderer::HardwareBitmapUploader::hasFP16Support()) {
+        decodeColorType = kN32_SkColorType;
+    }
 
     // Set up the pixel allocator
     SkBRDAllocator* allocator = nullptr;
diff --git a/core/jni/android/graphics/ImageDecoder.cpp b/core/jni/android/graphics/ImageDecoder.cpp
index 72e14e7..2d83ac3 100644
--- a/core/jni/android/graphics/ImageDecoder.cpp
+++ b/core/jni/android/graphics/ImageDecoder.cpp
@@ -24,6 +24,7 @@
 #include "core_jni_helpers.h"
 
 #include <hwui/Bitmap.h>
+#include <HardwareBitmapUploader.h>
 
 #include <SkAndroidCodec.h>
 #include <SkEncodedImageFormat.h>
@@ -256,6 +257,17 @@
         // This is currently the only way to know that we should decode to F16.
         colorType = codec->computeOutputColorType(colorType);
     }
+
+    const bool isHardware = !requireMutable
+        && (allocator == ImageDecoder::kDefault_Allocator ||
+            allocator == ImageDecoder::kHardware_Allocator)
+        && colorType != kGray_8_SkColorType;
+
+    if (colorType == kRGBA_F16_SkColorType && isHardware &&
+            !uirenderer::HardwareBitmapUploader::hasFP16Support()) {
+        colorType = kN32_SkColorType;
+    }
+
     sk_sp<SkColorSpace> colorSpace = GraphicsJNI::getNativeColorSpace(colorSpaceHandle);
     colorSpace = codec->computeOutputColorSpace(colorType, colorSpace);
     decodeInfo = decodeInfo.makeColorType(colorType).makeColorSpace(colorSpace);
@@ -449,10 +461,7 @@
     if (requireMutable) {
         bitmapCreateFlags |= bitmap::kBitmapCreateFlag_Mutable;
     } else {
-        if ((allocator == ImageDecoder::kDefault_Allocator ||
-             allocator == ImageDecoder::kHardware_Allocator)
-            && bm.colorType() != kAlpha_8_SkColorType)
-        {
+        if (isHardware) {
             sk_sp<Bitmap> hwBitmap = Bitmap::allocateHardwareBitmap(bm);
             if (hwBitmap) {
                 hwBitmap->setImmutable();
diff --git a/core/jni/android/graphics/Shader.cpp b/core/jni/android/graphics/Shader.cpp
index ed6a84b..298f7f8 100644
--- a/core/jni/android/graphics/Shader.cpp
+++ b/core/jni/android/graphics/Shader.cpp
@@ -8,6 +8,8 @@
 
 #include <jni.h>
 
+#include <vector>
+
 using namespace android::uirenderer;
 
 /**
@@ -18,11 +20,11 @@
  */
 static const uint32_t sGradientShaderFlags = SkGradientShader::kInterpolateColorsInPremul_Flag;
 
-static void ThrowIAE_IfNull(JNIEnv* env, void* ptr) {
-    if (NULL == ptr) {
-        doThrowIAE(env);
+#define ThrowIAE_IfNull(env, ptr)   \
+    if (nullptr == ptr) {           \
+        doThrowIAE(env);            \
+        return 0;                   \
     }
-}
 
 static void Color_RGBToHSV(JNIEnv* env, jobject, jint red, jint green, jint blue, jfloatArray hsvArray)
 {
@@ -76,186 +78,115 @@
     }
     sk_sp<SkShader> shader = image->makeShader(
             (SkShader::TileMode)tileModeX, (SkShader::TileMode)tileModeY);
+    ThrowIAE_IfNull(env, shader.get());
 
     if (matrix) {
         shader = shader->makeWithLocalMatrix(*matrix);
     }
 
-    ThrowIAE_IfNull(env, shader.get());
     return reinterpret_cast<jlong>(shader.release());
 }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////
 
-static jlong LinearGradient_create1(JNIEnv* env, jobject o, jlong matrixPtr,
-        jfloat x0, jfloat y0, jfloat x1, jfloat y1,
-        jintArray colorArray, jfloatArray posArray, jint tileMode) {
-    const SkMatrix* matrix = reinterpret_cast<const SkMatrix*>(matrixPtr);
-    SkPoint pts[2];
-    pts[0].set(x0, y0);
-    pts[1].set(x1, y1);
+static std::vector<SkColor4f> convertColorLongs(JNIEnv* env, jlongArray colorArray) {
+    const size_t count = env->GetArrayLength(colorArray);
+    const jlong* colorValues = env->GetLongArrayElements(colorArray, nullptr);
 
-    size_t count = env->GetArrayLength(colorArray);
-    const jint* colorValues = env->GetIntArrayElements(colorArray, NULL);
-
-    AutoJavaFloatArray autoPos(env, posArray, count);
-#ifdef SK_SCALAR_IS_FLOAT
-    SkScalar* pos = autoPos.ptr();
-#else
-    #error Need to convert float array to SkScalar array before calling the following function.
-#endif
-
-    sk_sp<SkShader> baseShader(SkGradientShader::MakeLinear(pts,
-            reinterpret_cast<const SkColor*>(colorValues), pos, count,
-            static_cast<SkShader::TileMode>(tileMode), sGradientShaderFlags, NULL));
-
-    SkShader* shader;
-    if (matrix) {
-        shader = baseShader->makeWithLocalMatrix(*matrix).release();
-    } else {
-        shader = baseShader.release();
+    std::vector<SkColor4f> colors(count);
+    for (size_t i = 0; i < count; ++i) {
+        colors[i] = GraphicsJNI::convertColorLong(colorValues[i]);
     }
 
-    env->ReleaseIntArrayElements(colorArray, const_cast<jint*>(colorValues), JNI_ABORT);
-    ThrowIAE_IfNull(env, shader);
-    return reinterpret_cast<jlong>(shader);
-}
-
-static jlong LinearGradient_create2(JNIEnv* env, jobject o, jlong matrixPtr,
-        jfloat x0, jfloat y0, jfloat x1, jfloat y1, jint color0, jint color1, jint tileMode) {
-    const SkMatrix* matrix = reinterpret_cast<const SkMatrix*>(matrixPtr);
-
-    SkPoint pts[2];
-    pts[0].set(x0, y0);
-    pts[1].set(x1, y1);
-
-    SkColor colors[2];
-    colors[0] = color0;
-    colors[1] = color1;
-
-    sk_sp<SkShader> baseShader(SkGradientShader::MakeLinear(pts, colors, NULL, 2,
-            static_cast<SkShader::TileMode>(tileMode), sGradientShaderFlags, NULL));
-
-    SkShader* s;
-    if (matrix) {
-        s = baseShader->makeWithLocalMatrix(*matrix).release();
-    } else {
-        s = baseShader.release();
-    }
-
-    ThrowIAE_IfNull(env, s);
-    return reinterpret_cast<jlong>(s);
+    env->ReleaseLongArrayElements(colorArray, const_cast<jlong*>(colorValues), JNI_ABORT);
+    return colors;
 }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////
 
-static jlong RadialGradient_create1(JNIEnv* env, jobject, jlong matrixPtr, jfloat x, jfloat y,
-        jfloat radius, jintArray colorArray, jfloatArray posArray, jint tileMode) {
-    const SkMatrix* matrix = reinterpret_cast<const SkMatrix*>(matrixPtr);
-    SkPoint center;
-    center.set(x, y);
+static jlong LinearGradient_create(JNIEnv* env, jobject, jlong matrixPtr,
+        jfloat x0, jfloat y0, jfloat x1, jfloat y1, jlongArray colorArray,
+        jfloatArray posArray, jint tileMode, jlong colorSpaceHandle) {
+    SkPoint pts[2];
+    pts[0].set(x0, y0);
+    pts[1].set(x1, y1);
 
-    size_t      count = env->GetArrayLength(colorArray);
-    const jint* colorValues = env->GetIntArrayElements(colorArray, NULL);
+    std::vector<SkColor4f> colors = convertColorLongs(env, colorArray);
 
-    AutoJavaFloatArray autoPos(env, posArray, count);
+    AutoJavaFloatArray autoPos(env, posArray, colors.size());
 #ifdef SK_SCALAR_IS_FLOAT
     SkScalar* pos = autoPos.ptr();
 #else
     #error Need to convert float array to SkScalar array before calling the following function.
 #endif
 
-    sk_sp<SkShader> baseShader = SkGradientShader::MakeRadial(center, radius,
-            reinterpret_cast<const SkColor*>(colorValues), pos, count,
-            static_cast<SkShader::TileMode>(tileMode), sGradientShaderFlags, NULL);
+    sk_sp<SkShader> shader(SkGradientShader::MakeLinear(pts, &colors[0],
+                GraphicsJNI::getNativeColorSpace(colorSpaceHandle), pos, colors.size(),
+                static_cast<SkShader::TileMode>(tileMode), sGradientShaderFlags, nullptr));
+    ThrowIAE_IfNull(env, shader);
 
-    SkShader* shader;
+    const SkMatrix* matrix = reinterpret_cast<const SkMatrix*>(matrixPtr);
     if (matrix) {
-        shader = baseShader->makeWithLocalMatrix(*matrix).release();
-    } else {
-        shader = baseShader.release();
+        shader = shader->makeWithLocalMatrix(*matrix);
     }
 
-    env->ReleaseIntArrayElements(colorArray, const_cast<jint*>(colorValues),
-                                 JNI_ABORT);
-
-    ThrowIAE_IfNull(env, shader);
-    return reinterpret_cast<jlong>(shader);
+    return reinterpret_cast<jlong>(shader.release());
 }
 
-static jlong RadialGradient_create2(JNIEnv* env, jobject, jlong matrixPtr, jfloat x, jfloat y, jfloat radius,
-        jint color0, jint color1, jint tileMode) {
-    const SkMatrix* matrix = reinterpret_cast<const SkMatrix*>(matrixPtr);
+///////////////////////////////////////////////////////////////////////////////////////////////
+
+static jlong RadialGradient_create(JNIEnv* env, jobject, jlong matrixPtr, jfloat x, jfloat y,
+        jfloat radius, jlongArray colorArray, jfloatArray posArray, jint tileMode,
+        jlong colorSpaceHandle) {
     SkPoint center;
     center.set(x, y);
 
-    SkColor colors[2];
-    colors[0] = color0;
-    colors[1] = color1;
+    std::vector<SkColor4f> colors = convertColorLongs(env, colorArray);
 
-    sk_sp<SkShader> baseShader = SkGradientShader::MakeRadial(center, radius, colors, NULL, 2,
-            static_cast<SkShader::TileMode>(tileMode), sGradientShaderFlags, NULL);
+    AutoJavaFloatArray autoPos(env, posArray, colors.size());
+#ifdef SK_SCALAR_IS_FLOAT
+    SkScalar* pos = autoPos.ptr();
+#else
+    #error Need to convert float array to SkScalar array before calling the following function.
+#endif
 
-    SkShader* shader;
-    if (matrix) {
-        shader = baseShader->makeWithLocalMatrix(*matrix).release();
-    } else {
-        shader = baseShader.release();
-    }
+    sk_sp<SkShader> shader = SkGradientShader::MakeRadial(center, radius, &colors[0],
+            GraphicsJNI::getNativeColorSpace(colorSpaceHandle), pos, colors.size(),
+            static_cast<SkShader::TileMode>(tileMode), sGradientShaderFlags, nullptr);
     ThrowIAE_IfNull(env, shader);
-    return reinterpret_cast<jlong>(shader);
+
+    const SkMatrix* matrix = reinterpret_cast<const SkMatrix*>(matrixPtr);
+    if (matrix) {
+        shader = shader->makeWithLocalMatrix(*matrix);
+    }
+
+    return reinterpret_cast<jlong>(shader.release());
 }
 
 ///////////////////////////////////////////////////////////////////////////////
 
-static jlong SweepGradient_create1(JNIEnv* env, jobject, jlong matrixPtr, jfloat x, jfloat y,
-        jintArray jcolors, jfloatArray jpositions) {
-    const SkMatrix* matrix = reinterpret_cast<const SkMatrix*>(matrixPtr);
-    size_t      count = env->GetArrayLength(jcolors);
-    const jint* colors = env->GetIntArrayElements(jcolors, NULL);
+static jlong SweepGradient_create(JNIEnv* env, jobject, jlong matrixPtr, jfloat x, jfloat y,
+        jlongArray colorArray, jfloatArray jpositions, jlong colorSpaceHandle) {
+    std::vector<SkColor4f> colors = convertColorLongs(env, colorArray);
 
-    AutoJavaFloatArray autoPos(env, jpositions, count);
+    AutoJavaFloatArray autoPos(env, jpositions, colors.size());
 #ifdef SK_SCALAR_IS_FLOAT
     SkScalar* pos = autoPos.ptr();
 #else
     #error Need to convert float array to SkScalar array before calling the following function.
 #endif
 
-    sk_sp<SkShader> baseShader = SkGradientShader::MakeSweep(x, y,
-            reinterpret_cast<const SkColor*>(colors), pos, count,
-            sGradientShaderFlags, NULL);
-
-    SkShader* shader;
-    if (matrix) {
-        shader = baseShader->makeWithLocalMatrix(*matrix).release();
-    } else {
-        shader = baseShader.release();
-    }
-
-    env->ReleaseIntArrayElements(jcolors, const_cast<jint*>(colors),
-                                 JNI_ABORT);
+    sk_sp<SkShader> shader = SkGradientShader::MakeSweep(x, y, &colors[0],
+            GraphicsJNI::getNativeColorSpace(colorSpaceHandle), pos, colors.size(),
+            sGradientShaderFlags, nullptr);
     ThrowIAE_IfNull(env, shader);
-    return reinterpret_cast<jlong>(shader);
-}
 
-static jlong SweepGradient_create2(JNIEnv* env, jobject, jlong matrixPtr, jfloat x, jfloat y,
-        int color0, int color1) {
     const SkMatrix* matrix = reinterpret_cast<const SkMatrix*>(matrixPtr);
-    SkColor colors[2];
-    colors[0] = color0;
-    colors[1] = color1;
-
-    sk_sp<SkShader> baseShader = SkGradientShader::MakeSweep(x, y, colors,
-            NULL, 2, sGradientShaderFlags, NULL);
-
-    SkShader* shader;
     if (matrix) {
-        shader = baseShader->makeWithLocalMatrix(*matrix).release();
-    } else {
-        shader = baseShader.release();
+        shader = shader->makeWithLocalMatrix(*matrix);
     }
-    ThrowIAE_IfNull(env, shader);
-    return reinterpret_cast<jlong>(shader);
+
+    return reinterpret_cast<jlong>(shader.release());
 }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////
@@ -295,18 +226,15 @@
 };
 
 static const JNINativeMethod gLinearGradientMethods[] = {
-    { "nativeCreate1",     "(JFFFF[I[FI)J",  (void*)LinearGradient_create1     },
-    { "nativeCreate2",     "(JFFFFIII)J",    (void*)LinearGradient_create2     },
+    { "nativeCreate",     "(JFFFF[J[FIJ)J",  (void*)LinearGradient_create     },
 };
 
 static const JNINativeMethod gRadialGradientMethods[] = {
-    { "nativeCreate1",     "(JFFF[I[FI)J",  (void*)RadialGradient_create1     },
-    { "nativeCreate2",     "(JFFFIII)J",    (void*)RadialGradient_create2     },
+    { "nativeCreate",     "(JFFF[J[FIJ)J",  (void*)RadialGradient_create     },
 };
 
 static const JNINativeMethod gSweepGradientMethods[] = {
-    { "nativeCreate1",     "(JFF[I[F)J",  (void*)SweepGradient_create1     },
-    { "nativeCreate2",     "(JFFII)J",    (void*)SweepGradient_create2     },
+    { "nativeCreate",     "(JFF[J[FJ)J",  (void*)SweepGradient_create     },
 };
 
 static const JNINativeMethod gComposeShaderMethods[] = {
diff --git a/core/jni/android_hardware_HardwareBuffer.cpp b/core/jni/android_hardware_HardwareBuffer.cpp
index 2d1fec8..00e5ba3 100644
--- a/core/jni/android_hardware_HardwareBuffer.cpp
+++ b/core/jni/android_hardware_HardwareBuffer.cpp
@@ -21,6 +21,7 @@
 
 #include "android_os_Parcel.h"
 #include "android/graphics/GraphicsJNI.h"
+#include "android/graphics/GraphicBuffer.h"
 
 #include <android/hardware_buffer.h>
 #include <android_runtime/android_hardware_HardwareBuffer.h>
@@ -96,6 +97,12 @@
     return reinterpret_cast<jlong>(wrapper);
 }
 
+static jlong android_hardware_HardwareBuffer_createFromGraphicBuffer(JNIEnv* env, jobject clazz, jobject graphicBuffer) {
+    sp<GraphicBuffer> buffer(graphicBufferForJavaObject(env, graphicBuffer));
+    GraphicBufferWrapper* wrapper = new GraphicBufferWrapper(buffer);
+    return reinterpret_cast<jlong>(wrapper);
+}
+
 static void destroyWrapper(GraphicBufferWrapper* wrapper) {
     delete wrapper;
 }
@@ -243,6 +250,8 @@
 static const JNINativeMethod gMethods[] = {
     { "nCreateHardwareBuffer",  "(IIIIJ)J",
             (void*) android_hardware_HardwareBuffer_create },
+    { "nCreateFromGraphicBuffer", "(Landroid/graphics/GraphicBuffer;)J",
+            (void*) android_hardware_HardwareBuffer_createFromGraphicBuffer },
     { "nGetNativeFinalizer", "()J",
             (void*) android_hardware_HardwareBuffer_getNativeFinalizer },
     { "nWriteHardwareBufferToParcel",  "(JLandroid/os/Parcel;)V",
diff --git a/core/jni/android_hardware_input_InputApplicationHandle.cpp b/core/jni/android_hardware_input_InputApplicationHandle.cpp
index 1ab5843..b9301d4 100644
--- a/core/jni/android_hardware_input_InputApplicationHandle.cpp
+++ b/core/jni/android_hardware_input_InputApplicationHandle.cpp
@@ -17,6 +17,7 @@
 #define LOG_TAG "InputApplicationHandle"
 
 #include <nativehelper/JNIHelp.h>
+#include "core_jni_helpers.h"
 #include "jni.h"
 #include <android_runtime/AndroidRuntime.h>
 #include <utils/threads.h>
@@ -63,16 +64,7 @@
         mInfo = new InputApplicationInfo();
     }
 
-    jstring nameObj = jstring(env->GetObjectField(obj,
-            gInputApplicationHandleClassInfo.name));
-    if (nameObj) {
-        const char* nameStr = env->GetStringUTFChars(nameObj, NULL);
-        mInfo->name = nameStr;
-        env->ReleaseStringUTFChars(nameObj, nameStr);
-        env->DeleteLocalRef(nameObj);
-    } else {
-        mInfo->name = "<null>";
-    }
+    mInfo->name = getStringField(env, obj, gInputApplicationHandleClassInfo.name, "<null>");
 
     mInfo->dispatchingTimeout = env->GetLongField(obj,
             gInputApplicationHandleClassInfo.dispatchingTimeoutNanos);
diff --git a/core/jni/android_hardware_input_InputWindowHandle.cpp b/core/jni/android_hardware_input_InputWindowHandle.cpp
index 67a7441..eb71052 100644
--- a/core/jni/android_hardware_input_InputWindowHandle.cpp
+++ b/core/jni/android_hardware_input_InputWindowHandle.cpp
@@ -17,6 +17,7 @@
 #define LOG_TAG "InputWindowHandle"
 
 #include <nativehelper/JNIHelp.h>
+#include "core_jni_helpers.h"
 #include "jni.h"
 #include <android_runtime/AndroidRuntime.h>
 #include <utils/threads.h>
@@ -86,24 +87,14 @@
 
     mInfo.touchableRegion.clear();
 
-    jobject tokenObj = env->GetObjectField(obj,
-            gInputWindowHandleClassInfo.token);
+    jobject tokenObj = env->GetObjectField(obj, gInputWindowHandleClassInfo.token);
     if (tokenObj) {
         mInfo.token = ibinderForJavaObject(env, tokenObj);
     } else {
         mInfo.token.clear();
     }
 
-    jstring nameObj = jstring(env->GetObjectField(obj,
-            gInputWindowHandleClassInfo.name));
-    if (nameObj) {
-        const char* nameStr = env->GetStringUTFChars(nameObj, NULL);
-        mInfo.name = nameStr;
-        env->ReleaseStringUTFChars(nameObj, nameStr);
-        env->DeleteLocalRef(nameObj);
-    } else {
-        mInfo.name = "<null>";
-    }
+    mInfo.name = getStringField(env, obj, gInputWindowHandleClassInfo.name, "<null>");
 
     mInfo.layoutParamsFlags = env->GetIntField(obj,
             gInputWindowHandleClassInfo.layoutParamsFlags);
@@ -241,8 +232,7 @@
     GET_FIELD_ID(gInputWindowHandleClassInfo.ptr, clazz,
             "ptr", "J");
 
-    GET_FIELD_ID(gInputWindowHandleClassInfo.inputApplicationHandle,
-            clazz,
+    GET_FIELD_ID(gInputWindowHandleClassInfo.inputApplicationHandle, clazz,
             "inputApplicationHandle", "Landroid/view/InputApplicationHandle;");
 
     GET_FIELD_ID(gInputWindowHandleClassInfo.token, clazz,
diff --git a/core/jni/android_media_AudioAttributes.cpp b/core/jni/android_media_AudioAttributes.cpp
index 4be4def..b87a34d 100644
--- a/core/jni/android_media_AudioAttributes.cpp
+++ b/core/jni/android_media_AudioAttributes.cpp
@@ -135,7 +135,7 @@
 {
     audio_attributes_t *aa = new (calloc(1, sizeof(audio_attributes_t)))
                 audio_attributes_t{AUDIO_ATTRIBUTES_INITIALIZER};
-    return UniqueAaPtr{aa, free};
+    return UniqueAaPtr{aa};
 }
 
 jint JNIAudioAttributeHelper::nativeFromJava(JNIEnv* env, jobject jAudioAttributes,
diff --git a/core/jni/android_media_AudioAttributes.h b/core/jni/android_media_AudioAttributes.h
index c558352..628f7e3 100644
--- a/core/jni/android_media_AudioAttributes.h
+++ b/core/jni/android_media_AudioAttributes.h
@@ -27,7 +27,11 @@
 class JNIAudioAttributeHelper
 {
 public:
-    using UniqueAaPtr = std::unique_ptr<audio_attributes_t, decltype(free)*>;
+    struct FreeDeleter {
+        void operator()(void *p) const { ::free(p); }
+    };
+
+    using UniqueAaPtr = std::unique_ptr<audio_attributes_t, FreeDeleter>;
 
     /**
      * @brief makeUnique helper to prevent leak
diff --git a/core/jni/android_os_VintfObject.cpp b/core/jni/android_os_VintfObject.cpp
index e64d2af..ee11b61 100644
--- a/core/jni/android_os_VintfObject.cpp
+++ b/core/jni/android_os_VintfObject.cpp
@@ -96,7 +96,7 @@
     return toJavaStringArray(env, cStrings);
 }
 
-static jint verify(JNIEnv* env, jobjectArray packageInfo, android::vintf::CheckFlags::Type checks) {
+static jint android_os_VintfObject_verify(JNIEnv* env, jclass, jobjectArray packageInfo) {
     std::vector<std::string> cPackageInfo;
     if (packageInfo) {
         size_t count = env->GetArrayLength(packageInfo);
@@ -109,18 +109,19 @@
         }
     }
     std::string error;
-    int32_t status = VintfObject::CheckCompatibility(cPackageInfo, &error, checks);
+    int32_t status = VintfObject::CheckCompatibility(cPackageInfo, &error);
     if (status)
         LOG(WARNING) << "VintfObject.verify() returns " << status << ": " << error;
     return status;
 }
 
-static jint android_os_VintfObject_verify(JNIEnv* env, jclass, jobjectArray packageInfo) {
-    return verify(env, packageInfo, ::android::vintf::CheckFlags::ENABLE_ALL_CHECKS);
-}
-
 static jint android_os_VintfObject_verifyWithoutAvb(JNIEnv* env, jclass) {
-    return verify(env, nullptr, ::android::vintf::CheckFlags::DISABLE_AVB_CHECK);
+    std::string error;
+    int32_t status = VintfObject::CheckCompatibility({}, &error,
+            ::android::vintf::CheckFlags::DISABLE_AVB_CHECK);
+    if (status)
+        LOG(WARNING) << "VintfObject.verifyWithoutAvb() returns " << status << ": " << error;
+    return status;
 }
 
 static jobjectArray android_os_VintfObject_getHalNamesAndVersions(JNIEnv* env, jclass) {
diff --git a/core/jni/android_view_InputChannel.cpp b/core/jni/android_view_InputChannel.cpp
index fb6be6b..9819d9a 100644
--- a/core/jni/android_view_InputChannel.cpp
+++ b/core/jni/android_view_InputChannel.cpp
@@ -17,7 +17,7 @@
 #define LOG_TAG "InputChannel-JNI"
 
 #include <nativehelper/JNIHelp.h>
-
+#include "nativehelper/scoped_utf_chars.h"
 #include <android_runtime/AndroidRuntime.h>
 #include <binder/Parcel.h>
 #include <utils/Log.h>
@@ -123,9 +123,8 @@
 
 static jobjectArray android_view_InputChannel_nativeOpenInputChannelPair(JNIEnv* env,
         jclass clazz, jstring nameObj) {
-    const char* nameChars = env->GetStringUTFChars(nameObj, NULL);
-    std::string name = nameChars;
-    env->ReleaseStringUTFChars(nameObj, nameChars);
+    ScopedUtfChars nameChars(env, nameObj);
+    std::string name = nameChars.c_str();
 
     sp<InputChannel> serverChannel;
     sp<InputChannel> clientChannel;
diff --git a/core/jni/android_view_RenderNode.cpp b/core/jni/android_view_RenderNode.cpp
index 0d75de9..ce5512b 100644
--- a/core/jni/android_view_RenderNode.cpp
+++ b/core/jni/android_view_RenderNode.cpp
@@ -507,15 +507,15 @@
 jmethodID gPositionListener_PositionLostMethod;
 
 static void android_view_RenderNode_requestPositionUpdates(JNIEnv* env, jobject,
-        jlong renderNodePtr, jobject surfaceview) {
-    class SurfaceViewPositionUpdater : public RenderNode::PositionListener {
+        jlong renderNodePtr, jobject listener) {
+    class PositionListenerTrampoline : public RenderNode::PositionListener {
     public:
-        SurfaceViewPositionUpdater(JNIEnv* env, jobject surfaceview) {
+        PositionListenerTrampoline(JNIEnv* env, jobject listener) {
             env->GetJavaVM(&mVm);
-            mWeakRef = env->NewWeakGlobalRef(surfaceview);
+            mWeakRef = env->NewWeakGlobalRef(listener);
         }
 
-        virtual ~SurfaceViewPositionUpdater() {
+        virtual ~PositionListenerTrampoline() {
             jnienv()->DeleteWeakGlobalRef(mWeakRef);
             mWeakRef = nullptr;
         }
@@ -539,9 +539,14 @@
                 bounds.roundOut();
             }
 
+            if (mPreviousPosition == bounds) {
+                return;
+            }
+            mPreviousPosition = bounds;
+
             incStrong(0);
             auto functor = std::bind(
-                std::mem_fn(&SurfaceViewPositionUpdater::doUpdatePositionAsync), this,
+                std::mem_fn(&PositionListenerTrampoline::doUpdatePositionAsync), this,
                 (jlong) info.canvasContext.getFrameNumber(),
                 (jint) bounds.left, (jint) bounds.top,
                 (jint) bounds.right, (jint) bounds.bottom);
@@ -552,6 +557,11 @@
         virtual void onPositionLost(RenderNode& node, const TreeInfo* info) override {
             if (CC_UNLIKELY(!mWeakRef || (info && !info->updateWindowPositions))) return;
 
+            if (mPreviousPosition.isEmpty()) {
+                return;
+            }
+            mPreviousPosition.setEmpty();
+
             ATRACE_NAME("SurfaceView position lost");
             JNIEnv* env = jnienv();
             jobject localref = env->NewLocalRef(mWeakRef);
@@ -561,6 +571,7 @@
                 return;
             }
 
+            // TODO: Remember why this is synchronous and then make a comment
             env->CallVoidMethod(localref, gPositionListener_PositionLostMethod,
                     info ? info->canvasContext.getFrameNumber() : 0);
             env->DeleteLocalRef(localref);
@@ -596,10 +607,11 @@
 
         JavaVM* mVm;
         jobject mWeakRef;
+        uirenderer::Rect mPreviousPosition;
     };
 
     RenderNode* renderNode = reinterpret_cast<RenderNode*>(renderNodePtr);
-    renderNode->setPositionListener(new SurfaceViewPositionUpdater(env, surfaceview));
+    renderNode->setPositionListener(new PositionListenerTrampoline(env, listener));
 }
 
 // ----------------------------------------------------------------------------
diff --git a/core/jni/core_jni_helpers.h b/core/jni/core_jni_helpers.h
index 1325b0c..16ef753 100644
--- a/core/jni/core_jni_helpers.h
+++ b/core/jni/core_jni_helpers.h
@@ -18,6 +18,8 @@
 #define CORE_JNI_HELPERS
 
 #include <nativehelper/JNIHelp.h>
+#include <nativehelper/scoped_local_ref.h>
+#include <nativehelper/scoped_utf_chars.h>
 #include <android_runtime/AndroidRuntime.h>
 
 namespace android {
@@ -72,6 +74,20 @@
     return res;
 }
 
+/**
+ * Read the specified field from jobject, and convert to std::string.
+ * If the field cannot be obtained, return defaultValue.
+ */
+static inline std::string getStringField(JNIEnv* env, jobject obj, jfieldID fieldId,
+        const char* defaultValue) {
+    ScopedLocalRef<jstring> strObj(env, jstring(env->GetObjectField(obj, fieldId)));
+    if (strObj != nullptr) {
+        ScopedUtfChars chars(env, strObj.get());
+        return std::string(chars.c_str());
+    }
+    return std::string(defaultValue);
+}
+
 }  // namespace android
 
 #endif  // CORE_JNI_HELPERS
diff --git a/core/proto/android/app/settings_enums.proto b/core/proto/android/app/settings_enums.proto
index 94a6734..631fa1d 100644
--- a/core/proto/android/app/settings_enums.proto
+++ b/core/proto/android/app/settings_enums.proto
@@ -595,6 +595,11 @@
 
     // ACTION: Tap & Pay -> Default Application Setting -> Use Default
     ACTION_NFC_PAYMENT_ALWAYS_SETTING = 1623;
+
+    // ACTION: Settings > Search Bar > Avatar
+    // CATEGORY: SETTINGS
+    // OS: Q
+    CLICK_ACCOUNT_AVATAR = 1643;
 }
 
 /**
@@ -2189,4 +2194,9 @@
 
     // OPEN: Settings > System > Aware > Disable > Dialog
     DIALOG_AWARE_DISABLE = 1633;
+
+    // OPEN: Settings > Settings > Network & internet > Click Mobile network to land on page with
+    // details for a SIM/eSIM mobile network > Click edit icon to bring up a rename dialog.
+    // OS: Q
+    MOBILE_NETWORK_RENAME_DIALOG = 1642;
 }
diff --git a/core/proto/android/server/powermanagerservice.proto b/core/proto/android/server/powermanagerservice.proto
index af0a942..8fce94e 100644
--- a/core/proto/android/server/powermanagerservice.proto
+++ b/core/proto/android/server/powermanagerservice.proto
@@ -318,6 +318,16 @@
     // Whether battery saver is enabled.
     optional bool enabled = 1;
 
+    // Whether full battery saver is enabled.
+    optional bool is_full_enabled = 14;
+
+    // Whether adaptive battery saver is enabled.
+    optional bool is_adaptive_enabled = 15;
+
+    // Whether the battery saver policy indicates that is_enabled should be
+    // advertised.
+    optional bool should_advertise_is_enabled = 16;
+
     // Whether system has booted.
     optional bool boot_completed = 2;
 
@@ -358,4 +368,10 @@
     // The value of Global.LOW_POWER_MODE_STICKY_AUTO_DISABLE_LEVEL. This is a cached value, so it
     // could be slightly different from what's in GlobalSettingsProto.LowPowerMode.
     optional int32 setting_battery_saver_sticky_auto_disable_threshold = 13;
+
+    // The last time adaptive battery saver was changed by an external service,
+    // using elapsed realtime as the timebase.
+    optional int64 last_adaptive_battery_saver_changed_externally_elapsed = 17;
+
+    // Next tag: 18
 }
diff --git a/core/res/AndroidManifest.xml b/core/res/AndroidManifest.xml
index 7a261a9..bc9dc57 100644
--- a/core/res/AndroidManifest.xml
+++ b/core/res/AndroidManifest.xml
@@ -1730,7 +1730,7 @@
     <permission android:name="android.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS"
         android:protectionLevel="signature|privileged" />
 
-    <!-- Allows a system application to access hardware packet offload capabilities.
+    <!-- @SystemApi Allows a system application to access hardware packet offload capabilities.
          @hide -->
     <permission android:name="android.permission.PACKET_KEEPALIVE_OFFLOAD"
         android:protectionLevel="signature|privileged" />
@@ -1868,6 +1868,10 @@
     <permission android:name="android.permission.HARDWARE_TEST"
         android:protectionLevel="signature" />
 
+    <!-- @hide Allows an application to manage DynamicAndroid image -->
+    <permission android:name="android.permission.MANAGE_DYNAMIC_ANDROID"
+        android:protectionLevel="signature" />
+
     <!-- @SystemApi Allows access to Broadcast Radio
          @hide This is not a third-party API (intended for system apps).-->
     <permission android:name="android.permission.ACCESS_BROADCAST_RADIO"
diff --git a/core/res/res/drawable/chooser_content_preview_rounded.xml b/core/res/res/drawable/chooser_content_preview_rounded.xml
new file mode 100644
index 0000000..7d85738
--- /dev/null
+++ b/core/res/res/drawable/chooser_content_preview_rounded.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2019 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<shape xmlns:android="http://schemas.android.com/apk/res/android"
+    android:shape="rectangle">
+
+    <stroke
+        android:width="1dp"
+        android:color="#ccc" />
+ 
+    <corners android:radius="@dimen/chooser_corner_radius" />
+
+    <padding
+        android:left="16dp"
+        android:top="12dp"
+        android:right="16dp"
+        android:bottom="12dp"/>
+</shape>
diff --git a/core/res/res/drawable/ic_content_copy_gm2.xml b/core/res/res/drawable/ic_content_copy_gm2.xml
new file mode 100644
index 0000000..940da94
--- /dev/null
+++ b/core/res/res/drawable/ic_content_copy_gm2.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2019 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+    android:width="24dp"
+    android:height="24dp"
+    android:viewportWidth="24"
+    android:viewportHeight="24">
+  <path
+      android:fillColor="#F999"
+      android:pathData="M18,21L4,21L4,7L2,7v14c0,1.1 0.9,2 2,2h14v-2zM21,17L21,3c0,-1.1 -0.9,-2 -2,-2L8,1c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h11c1.1,0 2,-0.9 2,-2zM19,17L8,17L8,3h11v14z"/>
+</vector>
diff --git a/core/res/res/drawable/ic_media_seamless.xml b/core/res/res/drawable/ic_media_seamless.xml
new file mode 100644
index 0000000..122e9c5
--- /dev/null
+++ b/core/res/res/drawable/ic_media_seamless.xml
@@ -0,0 +1,25 @@
+<!--
+    Copyright (C) 2017 The Android Open Source Project
+
+    Licensed under the Apache License, Version 2.0 (the "License");
+    you may not use this file except in compliance with the License.
+    You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="24dp"
+        android:height="24dp"
+        android:viewportWidth="24.0"
+        android:viewportHeight="24.0"
+        android:tint="?android:attr/colorControlNormal">
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M12,3l0.01,10.55c-0.59,-0.34 -1.27,-0.55 -2,-0.55C7.79,13 6,14.79 6,17c0,2.21 1.79,4 4.01,4S14,19.21 14,17V7h4V3H12zM10.01,19c-1.1,0 -2,-0.9 -2,-2c0,-1.1 0.9,-2 2,-2s2,0.9 2,2C12.01,18.1 11.11,19 10.01,19z"/>
+</vector>
diff --git a/core/res/res/layout/chooser_grid.xml b/core/res/res/layout/chooser_grid.xml
index c42f43a..a24f920 100644
--- a/core/res/res/layout/chooser_grid.xml
+++ b/core/res/res/layout/chooser_grid.xml
@@ -17,19 +17,19 @@
 */
 -->
 <com.android.internal.widget.ResolverDrawerLayout
-        xmlns:android="http://schemas.android.com/apk/res/android"
-        android:layout_width="match_parent"
-        android:layout_height="match_parent"
-        android:maxWidth="@dimen/resolver_max_width"
-        android:maxCollapsedHeight="288dp"
-        android:maxCollapsedHeightSmall="56dp"
-        android:id="@id/contentPanel">
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:maxWidth="@dimen/resolver_max_width"
+    android:maxCollapsedHeight="288dp"
+    android:maxCollapsedHeightSmall="56dp"
+    android:id="@id/contentPanel">
 
     <RelativeLayout
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:layout_alwaysShow="true"
-            android:background="?attr/colorBackgroundFloating" >
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_alwaysShow="true"
+        android:background="?attr/colorBackgroundFloating">
         <TextView android:id="@+id/profile_button"
                   android:layout_width="wrap_content"
                   android:layout_height="48dp"
@@ -51,73 +51,98 @@
                   android:textAppearance="?attr/textAppearanceMedium"
                   android:textSize="20sp"
                   android:gravity="center"
-                  android:paddingEnd="?attr/dialogPreferredPadding"
-                  android:paddingTop="12dp"
-                  android:paddingBottom="6dp"
+                  android:paddingTop="18dp"
+                  android:paddingBottom="18dp"
+                  android:paddingLeft="24dp"
+                  android:paddingRight="24dp"
                   android:layout_below="@id/profile_button"
                   android:layout_centerHorizontal="true"/>
     </RelativeLayout>
 
-    <RelativeLayout 
+    <LinearLayout
         android:id="@+id/content_preview"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
+        android:orientation="vertical"
+        android:paddingBottom="@dimen/chooser_view_spacing"
         android:background="?attr/colorBackgroundFloating">
-      
-      <view class="com.android.internal.app.ChooserActivity$RoundedRectImageView"
-            android:id="@+id/content_preview_thumbnail"
-            android:layout_alignParentTop="true"
-            android:layout_width="100dp"
+
+        <LinearLayout
+            android:layout_width="match_parent"
             android:layout_height="wrap_content"
-            android:layout_alignParentStart="true"
-            android:gravity="center"
-            android:adjustViewBounds="true"
-            android:maxWidth="90dp"
-            android:maxHeight="90dp"
-            android:scaleType="fitCenter"
-            android:padding="5dp"/>
+            android:orientation="horizontal"
+            android:paddingLeft="@dimen/chooser_edge_margin_normal"
+            android:paddingRight="@dimen/chooser_edge_margin_normal"
+            android:layout_marginBottom="@dimen/chooser_view_spacing"
+            android:id="@+id/content_preview_text_layout">
+            <TextView
+                android:id="@+id/content_preview_text"
+                android:layout_width="0dp"
+                android:layout_weight="1"
+                android:layout_height="wrap_content"
+                android:layout_gravity="center_vertical"
+                android:ellipsize="end"
+                android:gravity="start|top"
+                android:paddingRight="24dp"
+                android:maxLines="2"/>
+            <Button
+                android:id="@+id/copy_button"
+                android:layout_width="24dp"
+                android:layout_height="24dp"
+                android:gravity="center"
+                android:layout_gravity="center_vertical"
+                android:background="@drawable/ic_content_copy_gm2"/>
+        </LinearLayout>
 
-      <TextView
-          android:id="@+id/content_preview_title"
-          android:layout_alignParentTop="true"
-          android:layout_toEndOf="@id/content_preview_thumbnail"
-          android:layout_width="match_parent"
-          android:layout_height="wrap_content"
-          android:gravity="start|top"
-          android:textAppearance="?attr/textAppearanceMedium"
-          android:maxLines="2"
-          android:ellipsize="end"
-          android:paddingStart="15dp"
-          android:paddingEnd="15dp"
-          android:paddingTop="10dp" />
+        <!-- Required sub-layout so we can get the nice rounded corners-->
+        <!-- around this section -->
+        <LinearLayout
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:orientation="horizontal"
+            android:layout_marginLeft="@dimen/chooser_edge_margin_thin"
+            android:layout_marginRight="@dimen/chooser_edge_margin_thin"
+            android:minHeight="80dp"
+            android:background="@drawable/chooser_content_preview_rounded"
+            android:id="@+id/content_preview_title_layout">
 
-      <TextView
-          android:id="@+id/content_preview_text"
-          android:layout_width="match_parent"
-          android:layout_height="wrap_content"
-          android:layout_below="@id/content_preview_title"
-          android:layout_toEndOf="@id/content_preview_thumbnail"
-          android:gravity="start|top"
-          android:maxLines="2"
-          android:ellipsize="end"
-          android:paddingStart="15dp"
-          android:paddingEnd="15dp"
-          android:paddingTop="10dp"
-          android:paddingBottom="5dp"/>
-    </RelativeLayout>
+            <view class="com.android.internal.app.ChooserActivity$RoundedRectImageView"
+                  android:id="@+id/content_preview_thumbnail"
+                  android:layout_width="80dp"
+                  android:layout_height="80dp"
+                  android:layout_marginRight="12dp"
+                  android:adjustViewBounds="true"
+                  android:layout_gravity="center_vertical"
+                  android:gravity="center"
+                  android:maxWidth="70dp"
+                  android:maxHeight="70dp"
+                  android:padding="5dp"
+                  android:scaleType="centerCrop"/>
+
+            <TextView
+                android:id="@+id/content_preview_title"
+                android:layout_width="0dp"
+                android:layout_weight="1"
+                android:layout_height="wrap_content"
+                android:layout_gravity="center_vertical"
+                android:ellipsize="end"
+                android:maxLines="2"
+                android:textAppearance="?attr/textAppearanceMedium"/>
+        </LinearLayout>
+    </LinearLayout>
 
     <ListView
-            android:layout_width="match_parent"
-            android:layout_height="match_parent"
-            android:id="@+id/resolver_list"
-            android:clipToPadding="false"
-            android:scrollbarStyle="outsideOverlay"
-            android:background="?attr/colorBackgroundFloating"
-            android:elevation="8dp"
-            android:listSelector="@color/transparent"
-            android:divider="@null"
-            android:scrollIndicators="top"
-            android:nestedScrollingEnabled="true" />
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:id="@+id/resolver_list"
+        android:clipToPadding="false"
+        android:scrollbarStyle="outsideOverlay"
+        android:background="?attr/colorBackgroundFloating"
+        android:elevation="8dp"
+        android:listSelector="@color/transparent"
+        android:divider="@null"
+        android:scrollIndicators="top"
+        android:nestedScrollingEnabled="true"/>
 
     <TextView android:id="@+id/empty"
               android:layout_width="match_parent"
@@ -127,6 +152,6 @@
               android:text="@string/noApplications"
               android:padding="32dp"
               android:gravity="center"
-              android:visibility="gone" />
+              android:visibility="gone"/>
 
 </com.android.internal.widget.ResolverDrawerLayout>
diff --git a/core/res/res/layout/notification_template_material_big_media.xml b/core/res/res/layout/notification_template_material_big_media.xml
index 5cb93eb..56f7a59 100644
--- a/core/res/res/layout/notification_template_material_big_media.xml
+++ b/core/res/res/layout/notification_template_material_big_media.xml
@@ -79,6 +79,10 @@
                 layout="@layout/notification_material_media_action"
                 android:id="@+id/action4"
             />
+            <include
+                layout="@layout/notification_material_media_action"
+                android:id="@+id/media_seamless"
+            />
         </LinearLayout>
     </LinearLayout>
 </com.android.internal.widget.MediaNotificationView>
diff --git a/core/res/res/values/attrs_manifest.xml b/core/res/res/values/attrs_manifest.xml
index 0abe456..7219d5c 100644
--- a/core/res/res/values/attrs_manifest.xml
+++ b/core/res/res/values/attrs_manifest.xml
@@ -1116,6 +1116,15 @@
          -->
     <attr name="classLoader" format="string" />
 
+    <!-- Name of the class that gets invoked for preloading application code, when starting an
+         {@link android.R.attr#isolatedProcess} service that has
+         {@link android.R.attr#useAppZygote} set to <code>true</code>. This is a fully
+         qualified class name (for example, com.mycompany.myapp.MyZygotePreload); as a
+         short-hand if the first character of the class is a period then it is appended
+         to your package name. The class must implement the {@link android.app.ZygotePreload}
+         interface. -->
+    <attr name="zygotePreloadName" format="string"/>
+
     <!-- If set to <code>true</code>, indicates to the platform that this APK is
          a 'feature' split and that it implicitly depends on the base APK. This distinguishes
          this split APK from a 'configuration' split, which provides resource overrides
@@ -1129,10 +1138,12 @@
          resource] to be present in order to function. Default value is false. -->
     <attr name="isSplitRequired" format="boolean" />
 
-    <!-- Flag to specify if this app prioritizes code integrity. The system may choose
-         to run with better integrity guarantee in various components if possible based on the app's
-         <code>targetSdkVersion</code>. -->
-    <attr name="preferCodeIntegrity" format="boolean" />
+    <!-- Flag to specify if this app wants to run the dex within its APK but not extracted or
+         locally compiled variants. This keeps the dex code protected by the APK signature. Such
+         apps will always run in JIT mode (same when they are first installed), and the system will
+         never generate ahead-of-time compiled code for them. Depending on the app's workload,
+         there may be some run time performance change, noteably the cold start time. -->
+    <attr name="useEmbeddedDex" format="boolean" />
 
     <!-- Extra options for an activity's UI. Applies to either the {@code <activity>} or
          {@code <application>} tag. If specified on the {@code <application>}
@@ -1436,26 +1447,20 @@
 
     <attr name="usesNonSdkApi" format="boolean" />
 
-    <!-- Specify the type of foreground service. Apps targeting API
-     {@link android.os.Build.VERSION_CODES#Q} or later must specify foreground service type,
-     otherwise a SecurityException is thrown when
-     {@link android.app.Service#startForeground(int, Notification)} on this service is called.
-    -->
+    <!-- Specify the type of foreground service. Multiple types can be specified by ORing the flags
+         together. -->
     <attr name="foregroundServiceType">
         <!-- Data (photo, file, account) upload/download, backup/restore, import/export, fetch,
         transfer over network between device and cloud.  -->
-        <enum name="sync" value="1" />
+        <flag name="dataSync" value="0x01" />
         <!-- Music, video, news or other media play. -->
-        <enum name="mediaPlay" value="2" />
+        <flag name="mediaPlayback" value="0x02" />
         <!-- Ongoing phone call or video conference. -->
-        <enum name="phoneCall" value="3" />
+        <flag name="phoneCall" value="0x04" />
         <!-- GPS, map, navigation location update. -->
-        <enum name="location" value="4" />
+        <flag name="location" value="0x08" />
         <!-- Auto, bluetooth, TV or other devices connection, monitoring and interaction. -->
-        <enum name="deviceCompanion" value="5" />
-        <!-- Process that should not be interrupted, including installation, setup, photo
-        compression etc. -->
-        <enum name="ongoingProcess" value="6" />
+        <flag name="connectedDevice" value="0x10" />
     </attr>
 
 
@@ -1602,7 +1607,7 @@
              to honor this flag as well. -->
         <attr name="usesCleartextTraffic" />
         <attr name="multiArch" />
-        <attr name="preferCodeIntegrity" />
+        <attr name="useEmbeddedDex" />
         <attr name="extractNativeLibs" />
         <attr name="defaultToDeviceProtectedStorage" format="boolean" />
         <attr name="directBootAware" />
@@ -1644,6 +1649,7 @@
         <!-- If {@code true} the user is prompted to keep the app's data on uninstall -->
         <attr name="hasFragileUserData" />
 
+	<attr name="zygotePreloadName" />
     </declare-styleable>
     <!-- The <code>permission</code> tag declares a security permission that can be
          used to control access from other packages to specific components or
@@ -2300,17 +2306,18 @@
         <!-- If true, and this is an {@link android.R.attr#isolatedProcess} service, the service
              will be spawned from an Application Zygote, instead of the regular Zygote.
              <p>
-             The Application Zygote will pre-initialize the application's class loader,
-             and call a static callback into the application to allow it to perform
+             The Application Zygote will first pre-initialize the application's class loader. Then,
+             if the application has defined the {@link android.R.attr#zygotePreloadName} attribute,
+             the Application Zygote will call into that class to allow it to perform
              application-specific preloads (such as loading a shared library). Therefore,
              spawning from the Application Zygote will typically reduce the service
              launch time and reduce its memory usage. The downside of using this flag
              is that you will have an additional process (the app zygote itself) that
              is taking up memory. Whether actual memory usage is improved therefore strongly
              depends on the number of isolated services that an application starts,
-             and how much memory those services save by preloading. Therefore, it is
-             recommended to measure memory usage under typical workloads to determine
-             whether it makes sense to use this flag. -->
+             and how much memory those services save by preloading and sharing memory with
+             the app zygote. Therefore, it is recommended to measure memory usage under
+             typical workloads to determine whether it makes sense to use this flag. -->
         <attr name="useAppZygote" format="boolean" />
         <!-- If this is a foreground service, specify its category. -->
         <attr name="foregroundServiceType" />
diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml
index 08d5a3d..df20f85 100644
--- a/core/res/res/values/config.xml
+++ b/core/res/res/values/config.xml
@@ -712,6 +712,9 @@
     <!-- Indicates that connected MAC randomization is supported on this device -->
     <bool translatable="false" name="config_wifi_connected_mac_randomization_supported">false</bool>
 
+    <!-- Indicates that wifi link probing is supported on this device -->
+    <bool translatable="false" name="config_wifi_link_probing_supported">false</bool>
+
     <!-- Flag indicating whether we should enable the automatic brightness.
          Software implementation will be used if config_hardware_auto_brightness_available is not set -->
     <bool name="config_automatic_brightness_available">false</bool>
@@ -3779,4 +3782,105 @@
 
     <!-- Whether or not aware is enabled by default -->
     <bool name="config_awareSettingAvailable">false</bool>
+
+    <!-- Display White-Balance -->
+
+    <!-- See AmbientSensor.AmbientBrightnessSensor.
+         The ambient brightness sensor rate (in milliseconds). Must be positive. -->
+    <integer name="config_displayWhiteBalanceBrightnessSensorRate">250</integer>
+
+    <!-- See AmbientFilter.
+         How long ambient brightness changes are kept and taken into consideration
+         (in milliseconds). Must be positive. -->
+    <integer name="config_displayWhiteBalanceBrightnessFilterHorizon">10000</integer>
+
+    <!-- See AmbientFilter.WeightedMovingAverageAmbientFilter.
+         Recent changes are prioritised by integrating their duration over y = x + intercept
+         (the higher it is, the less prioritised recent changes are). Must be a non-negative
+         number, or NaN to avoid this implementation. -->
+    <item name="config_displayWhiteBalanceBrightnessFilterIntercept" format="float" type="dimen">10.0</item>
+
+    <!-- See AmbientSensor.AmbientColorTemperatureSensor.
+         The ambient color temperature sensor name. -->
+    <string name="config_displayWhiteBalanceColorTemperatureSensorName">com.google.sensor.color</string>
+
+    <!-- See AmbientSensor.AmbientColorTemperatureSensor.
+         The ambient color temperature sensor rate (in milliseconds). Must be positive. -->
+    <integer name="config_displayWhiteBalanceColorTemperatureSensorRate">250</integer>
+
+    <!-- See AmbientFilter.
+         How long ambient color temperature changes are kept and taken into consideration
+         (in milliseconds). Must be positive. -->
+    <integer name="config_displayWhiteBalanceColorTemperatureFilterHorizon">10000</integer>
+
+    <!-- See AmbientFilter.WeightedMovingAverageAmbientFilter.
+         Recent changes are prioritised by integrating their duration over y = x + intercept
+         (the higher it is, the less prioritised recent changes are). Must be a non-negative
+         number, or NaN to avoid this implementation. -->
+    <item name="config_displayWhiteBalanceColorTemperatureFilterIntercept" format="float"
+            type="dimen">10.0</item>
+
+    <!-- See DisplayWhiteBalanceThrottler.
+         The debounce time (in milliseconds) for increasing the screen color temperature, throttled
+         if time > lastTime + debounce. Must be non-negative. -->
+    <integer name="config_displayWhiteBalanceIncreaseDebounce">5000</integer>
+
+    <!-- See DisplayWhiteBalanceThrottler.
+         The debounce time (in milliseconds) for decreasing the screen color tempearture, throttled
+         if time < lastTime - debounce. Must be non-negative. -->
+    <integer name="config_displayWhiteBalanceDecreaseDebounce">5000</integer>
+
+    <!-- See DisplayWhiteBalanceThrottler.
+         The ambient color temperature values used to determine the threshold as the corresponding
+         value in config_displayWhiteBalance{Increase,Decrease}Threholds. Must be non-empty, the
+         same length as config_displayWhiteBalance{Increase,Decrease}Thresholds, and contain
+         non-negative, strictly increasing numbers.
+
+         For example, if:
+
+         - baseThresolds = [0, 100, 1000];
+         - increaseThresholds = [0.1, 0.15, 0.2];
+         - decreaseThresholds = [0.1, 0.05, 0.0];
+
+         Then, given the ambient color temperature INCREASED from X to Y (so X < Y):
+         - If 0 <= Y < 100, we require Y > (1 + 0.1) * X = 1.1X;
+         - If 100 <= Y < 1000, we require Y > (1 + 0.15) * X = 1.15X;
+         - If 1000 <= Y, we require Y > (1 + 0.2) * X = 1.2X.
+
+         Or, if the ambient color temperature DECREASED from X to Y (so X > Y):
+         - If 0 <= Y < 100, we require Y < (1 - 0.1) * X = 0.9X;
+         - If 100 <= Y < 1000, we require Y < (1 - 0.05) * X = 0.95X;
+         - If 1000 <= Y, we require Y < (1 - 0) * X = X.
+
+         NOTE: the numbers in this example are made up, and don't represent how actual base,
+               increase or decrease thresholds would look like. -->
+    <array name="config_displayWhiteBalanceBaseThresholds">
+        <item>0.0</item>
+    </array>
+
+    <!-- See DisplayWhiteBalanceThrottler.
+         The increase threshold values, throttled if value < value * (1 + threshold). Must be
+         non-empty, the same length as config_displayWhiteBalanceBaseThresholds, and contain
+         non-negative numbers. -->
+    <array name="config_displayWhiteBalanceIncreaseThresholds">
+        <item>0.1</item>
+    </array>
+
+    <!-- See DisplayWhiteBalanceThrottler.
+         The decrease threshold values, throttled if value > value * (1 - threshold). Must be
+         non-empty, the same length as config_displayWhiteBalanceBaseThresholds, and contain
+         non-negative numbers. -->
+    <array name="config_displayWhiteBalanceDecreaseThresholds">
+        <item>0.1</item>
+    </array>
+
+    <!-- See DisplayWhiteBalanceController.
+         The ambient brightness threshold (in lux) beneath which we fall back to a fixed ambient
+         color temperature. -->
+    <item name="config_displayWhiteBalanceLowLightAmbientBrightnessThreshold" format="float" type="dimen">10.0</item>
+
+    <!-- See DisplayWhiteBalanceController.
+         The ambient color temperature (in cct) to which we fall back when the ambient brightness
+         drops beneath a certain threshold. -->
+    <item name="config_displayWhiteBalanceLowLightAmbientColorTemperature" format="float" type="dimen">6500.0</item>
 </resources>
diff --git a/core/res/res/values/dimens.xml b/core/res/res/values/dimens.xml
index ef3834c..38367fb 100644
--- a/core/res/res/values/dimens.xml
+++ b/core/res/res/values/dimens.xml
@@ -240,7 +240,7 @@
     <dimen name="notification_header_icon_size">18dp</dimen>
 
     <!-- size (width and height) of the icon in the notification header -->
-    <dimen name="notification_header_icon_size_ambient">20dp</dimen>
+    <dimen name="notification_header_icon_size_ambient">18dp</dimen>
 
     <!-- The margin before the start of the app name in the header. -->
     <dimen name="notification_header_app_name_margin_start">3dp</dimen>
@@ -715,6 +715,9 @@
     <!-- Line spacing modifier for the message field of the harmful app dialog -->
     <item name="harmful_app_message_line_spacing_modifier" type="dimen">1.22</item>
 
-    <!-- chooser corner radius -->
-    <dimen name="chooser_corner_radius">4dp</dimen>
+    <!-- chooser (sharesheet) spacing -->
+    <dimen name="chooser_corner_radius">8dp</dimen>
+    <dimen name="chooser_view_spacing">18dp</dimen>
+    <dimen name="chooser_edge_margin_thin">16dp</dimen>
+    <dimen name="chooser_edge_margin_normal">24dp</dimen>
 </resources>
diff --git a/core/res/res/values/public.xml b/core/res/res/values/public.xml
index ec1bac1..b5266e2 100644
--- a/core/res/res/values/public.xml
+++ b/core/res/res/values/public.xml
@@ -2936,6 +2936,7 @@
         <public name="minAspectRatio" />
         <!-- @hide @SystemApi -->
         <public name="inheritShowWhenLocked" />
+        <public name="zygotePreloadName" />
     </public-group>
 
     <public-group type="drawable" first-id="0x010800b4">
diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml
index 6adb4a7..fadb28f 100644
--- a/core/res/res/values/strings.xml
+++ b/core/res/res/values/strings.xml
@@ -2645,6 +2645,9 @@
     <!-- Displayed to the user to confirm that they have copied text from a web page to the clipboard. -->
     <string name="text_copied">Text copied to clipboard.</string>
 
+    <!-- Displayed to the user to confirm that they have copied text/images to the clipboard [CHAR LIMIT=NONE] -->
+    <string name="copied">Copied</string>
+
     <!-- Menu item displayed at the end of a menu to allow users to see another page worth of menu items. This is shown on any app's menu as long as the app has too many items in the menu.-->
     <string name="more_item_label">More</string>
     <!-- Prepended to the shortcut for a menu item to indicate that the user should hold the MENU button together with the shortcut to invoke the item. For example, if the shortcut to open a new tab in browser is MENU and B together, then this would be prepended to the letter "B" -->
@@ -3692,6 +3695,9 @@
     <!-- Notification action to browse external media [CHAR LIMIT=20] -->
     <string name="ext_media_browse_action">Explore</string>
 
+    <!-- Notification action to transfer media [CHAR LIMIT=40] -->
+    <string name="ext_media_seamless_action">Seamless transfer</string>
+
     <!-- Notification title when external media is missing [CHAR LIMIT=30] -->
     <string name="ext_media_missing_title"><xliff:g id="name" example="SD card">%s</xliff:g> missing</string>
     <!-- Notification body when external media is missing [CHAR LIMIT=30] -->
diff --git a/core/res/res/values/styles_device_defaults.xml b/core/res/res/values/styles_device_defaults.xml
index 79afe69..0dc54e0 100644
--- a/core/res/res/values/styles_device_defaults.xml
+++ b/core/res/res/values/styles_device_defaults.xml
@@ -374,7 +374,9 @@
     <style name="TextAppearance.DeviceDefault.Caption" parent="TextAppearance.Material.Caption">
         <item name="fontFamily">@string/config_bodyFontFamily</item>
     </style>
-    <style name="TextAppearance.DeviceDefault.ListItem" parent="TextAppearance.DeviceDefault.Subhead"/>
+    <style name="TextAppearance.DeviceDefault.ListItem" parent="TextAppearance.DeviceDefault.Subhead">
+        <item name="fontFamily">@string/config_headlineFontFamily</item>
+    </style>
     <style name="TextAppearance.DeviceDefault.ListItemSecondary" parent="TextAppearance.DeviceDefault.Body1"/>
 
     <!-- Preference Styles -->
diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml
index 54a3243..ca8fa82 100644
--- a/core/res/res/values/symbols.xml
+++ b/core/res/res/values/symbols.xml
@@ -53,7 +53,10 @@
   <java-symbol type="id" name="content_preview" />
   <java-symbol type="id" name="content_preview_thumbnail" />
   <java-symbol type="id" name="content_preview_text" />
+  <java-symbol type="id" name="content_preview_text_layout" />
   <java-symbol type="id" name="content_preview_title" />
+  <java-symbol type="id" name="content_preview_title_layout" />
+  <java-symbol type="id" name="copy_button" />
   <java-symbol type="id" name="current_scene" />
   <java-symbol type="id" name="scene_layoutid_cache" />
   <java-symbol type="id" name="customPanel" />
@@ -191,6 +194,7 @@
   <java-symbol type="id" name="action2" />
   <java-symbol type="id" name="action3" />
   <java-symbol type="id" name="action4" />
+  <java-symbol type="id" name="media_seamless" />
   <java-symbol type="id" name="big_picture" />
   <java-symbol type="id" name="big_text" />
   <java-symbol type="id" name="chronometer" />
@@ -544,6 +548,7 @@
   <java-symbol type="string" name="activity_resolver_work_profiles_support" />
   <java-symbol type="string" name="app_running_notification_title" />
   <java-symbol type="string" name="app_running_notification_text" />
+  <java-symbol type="string" name="copied" />
   <java-symbol type="string" name="delete" />
   <java-symbol type="string" name="deleteText" />
   <java-symbol type="string" name="grant_permissions_header_text" />
@@ -1910,6 +1915,7 @@
   <java-symbol type="bool" name="config_wifi_convert_apband_5ghz_to_any" />
   <java-symbol type="bool" name="config_wifi_local_only_hotspot_5ghz" />
   <java-symbol type="bool" name="config_wifi_connected_mac_randomization_supported" />
+  <java-symbol type="bool" name="config_wifi_link_probing_supported" />
   <java-symbol type="bool" name="config_wifi_fast_bss_transition_enabled" />
   <java-symbol type="bool" name="config_wimaxEnabled" />
   <java-symbol type="bool" name="show_ongoing_ime_switcher" />
@@ -2236,6 +2242,7 @@
   <java-symbol type="string" name="ext_media_init_action" />
   <java-symbol type="string" name="ext_media_unmount_action" />
   <java-symbol type="string" name="ext_media_browse_action" />
+  <java-symbol type="string" name="ext_media_seamless_action" />
   <java-symbol type="string" name="ext_media_missing_title" />
   <java-symbol type="string" name="ext_media_missing_message" />
   <java-symbol type="string" name="ext_media_move_specific_title" />
@@ -2708,6 +2715,9 @@
   <java-symbol type="id" name="month_view" />
   <java-symbol type="integer" name="config_zen_repeat_callers_threshold" />
   <java-symbol type="dimen" name="chooser_corner_radius" />
+  <java-symbol type="dimen" name="chooser_view_spacing" />
+  <java-symbol type="dimen" name="chooser_edge_margin_thin" />
+  <java-symbol type="dimen" name="chooser_edge_margin_normal" />
   <java-symbol type="layout" name="chooser_grid" />
   <java-symbol type="layout" name="resolve_grid_item" />
   <java-symbol type="id" name="day_picker_view_pager" />
@@ -3097,6 +3107,7 @@
   <java-symbol type="drawable" name="ic_restart" />
   <java-symbol type="drawable" name="ic_screenshot" />
   <java-symbol type="drawable" name="ic_faster_emergency" />
+  <java-symbol type="drawable" name="ic_media_seamless" />
   <java-symbol type="drawable" name="emergency_icon" />
 
   <java-symbol type="array" name="config_convert_to_emergency_number_map" />
@@ -3587,4 +3598,20 @@
   <java-symbol type="integer" name="config_attentionApiTimeout" />
 
   <java-symbol type="string" name="config_incidentReportApproverPackage" />
+
+  <!-- Display White-Balance -->
+  <java-symbol type="integer" name="config_displayWhiteBalanceBrightnessSensorRate" />
+  <java-symbol type="integer" name="config_displayWhiteBalanceBrightnessFilterHorizon" />
+  <java-symbol type="dimen" name="config_displayWhiteBalanceBrightnessFilterIntercept" />
+  <java-symbol type="string" name="config_displayWhiteBalanceColorTemperatureSensorName" />
+  <java-symbol type="integer" name="config_displayWhiteBalanceColorTemperatureSensorRate" />
+  <java-symbol type="integer" name="config_displayWhiteBalanceColorTemperatureFilterHorizon" />
+  <java-symbol type="dimen" name="config_displayWhiteBalanceColorTemperatureFilterIntercept" />
+  <java-symbol type="integer" name="config_displayWhiteBalanceIncreaseDebounce" />
+  <java-symbol type="integer" name="config_displayWhiteBalanceDecreaseDebounce" />
+  <java-symbol type="array" name="config_displayWhiteBalanceBaseThresholds" />
+  <java-symbol type="array" name="config_displayWhiteBalanceIncreaseThresholds" />
+  <java-symbol type="array" name="config_displayWhiteBalanceDecreaseThresholds" />
+  <java-symbol type="dimen" name="config_displayWhiteBalanceLowLightAmbientBrightnessThreshold" />
+  <java-symbol type="dimen" name="config_displayWhiteBalanceLowLightAmbientColorTemperature" />
 </resources>
diff --git a/core/tests/coretests/AndroidManifest.xml b/core/tests/coretests/AndroidManifest.xml
index 268bb81..1764249 100644
--- a/core/tests/coretests/AndroidManifest.xml
+++ b/core/tests/coretests/AndroidManifest.xml
@@ -59,6 +59,7 @@
     <uses-permission android:name="android.permission.READ_DEVICE_CONFIG" />
     <uses-permission android:name="android.permission.READ_DREAM_STATE" />
     <uses-permission android:name="android.permission.WRITE_DREAM_STATE" />
+    <uses-permission android:name="android.permission.READ_CLIPBOARD_IN_BACKGROUND" />
     <uses-permission android:name="android.permission.READ_LOGS"/>
     <uses-permission android:name="android.permission.READ_PHONE_STATE" />
     <uses-permission android:name="android.permission.READ_SMS"/>
diff --git a/core/tests/coretests/src/android/graphics/BitmapTest.java b/core/tests/coretests/src/android/graphics/BitmapTest.java
index e79d2ae..d2a1dd9 100644
--- a/core/tests/coretests/src/android/graphics/BitmapTest.java
+++ b/core/tests/coretests/src/android/graphics/BitmapTest.java
@@ -16,6 +16,8 @@
 
 package android.graphics;
 
+import android.hardware.HardwareBuffer;
+
 import androidx.test.filters.SmallTest;
 
 import junit.framework.TestCase;
@@ -233,14 +235,31 @@
     }
 
     @SmallTest
-    public void testCreateHardwareBitmapFromGraphicBuffer() {
+    public void testWrapHardwareBufferWithSrgbColorSpace() {
         GraphicBuffer buffer = GraphicBuffer.create(10, 10, PixelFormat.RGBA_8888,
                 GraphicBuffer.USAGE_HW_TEXTURE | GraphicBuffer.USAGE_SOFTWARE_MASK);
         Canvas canvas = buffer.lockCanvas();
         canvas.drawColor(Color.YELLOW);
         buffer.unlockCanvasAndPost(canvas);
-        Bitmap hardwareBitmap = Bitmap.createHardwareBitmap(buffer);
+        Bitmap hardwareBitmap =
+                Bitmap.wrapHardwareBuffer(HardwareBuffer.createFromGraphicBuffer(buffer), null);
         assertTrue(hardwareBitmap.isPremultiplied());
         assertFalse(hardwareBitmap.isMutable());
+        assertEquals(ColorSpace.get(ColorSpace.Named.SRGB), hardwareBitmap.getColorSpace());
+    }
+
+    @SmallTest
+    public void testWrapHardwareBufferWithDisplayP3ColorSpace() {
+        GraphicBuffer buffer = GraphicBuffer.create(10, 10, PixelFormat.RGBA_8888,
+                GraphicBuffer.USAGE_HW_TEXTURE | GraphicBuffer.USAGE_SOFTWARE_MASK);
+        Canvas canvas = buffer.lockCanvas();
+        canvas.drawColor(Color.YELLOW);
+        buffer.unlockCanvasAndPost(canvas);
+        Bitmap hardwareBitmap = Bitmap.wrapHardwareBuffer(
+                HardwareBuffer.createFromGraphicBuffer(buffer),
+                ColorSpace.get(ColorSpace.Named.DISPLAY_P3));
+        assertTrue(hardwareBitmap.isPremultiplied());
+        assertFalse(hardwareBitmap.isMutable());
+        assertEquals(ColorSpace.get(ColorSpace.Named.DISPLAY_P3), hardwareBitmap.getColorSpace());
     }
 }
diff --git a/core/tests/coretests/src/android/provider/SettingsBackupTest.java b/core/tests/coretests/src/android/provider/SettingsBackupTest.java
index e32b412..f420033 100644
--- a/core/tests/coretests/src/android/provider/SettingsBackupTest.java
+++ b/core/tests/coretests/src/android/provider/SettingsBackupTest.java
@@ -137,6 +137,7 @@
                     Settings.Global.BROADCAST_OFFLOAD_CONSTANTS,
                     Settings.Global.BATTERY_DISCHARGE_DURATION_THRESHOLD,
                     Settings.Global.BATTERY_DISCHARGE_THRESHOLD,
+                    Settings.Global.BATTERY_SAVER_ADAPTIVE_DEVICE_SPECIFIC_CONSTANTS,
                     Settings.Global.BATTERY_SAVER_DEVICE_SPECIFIC_CONSTANTS,
                     Settings.Global.BATTERY_STATS_CONSTANTS,
                     Settings.Global.BINDER_CALLS_STATS,
@@ -222,6 +223,7 @@
                     Settings.Global.DEVELOPMENT_SETTINGS_ENABLED,
                     Settings.Global.DEVICE_DEMO_MODE,
                     Settings.Global.DEVICE_IDLE_CONSTANTS,
+                    Settings.Global.BATTERY_SAVER_ADAPTIVE_CONSTANTS,
                     Settings.Global.BATTERY_SAVER_CONSTANTS,
                     Settings.Global.BATTERY_TIP_CONSTANTS,
                     Settings.Global.DEFAULT_SM_DP_PLUS,
@@ -525,6 +527,7 @@
                     Settings.Global.WIFI_LINK_SPEED_METRICS_ENABLED,
                     Settings.Global.WIFI_PNO_FREQUENCY_CULLING_ENABLED,
                     Settings.Global.WIFI_PNO_RECENCY_SORTING_ENABLED,
+                    Settings.Global.WIFI_LINK_PROBING_ENABLED,
                     Settings.Global.WIFI_MAX_DHCP_RETRY_COUNT,
                     Settings.Global.WIFI_MOBILE_DATA_TRANSITION_WAKELOCK_TIMEOUT_MS,
                     Settings.Global.WIFI_NETWORK_SHOW_RSSI,
diff --git a/core/tests/coretests/src/android/view/textclassifier/TextClassifierTest.java b/core/tests/coretests/src/android/view/textclassifier/TextClassifierTest.java
index 5e58f82..582be9d 100644
--- a/core/tests/coretests/src/android/view/textclassifier/TextClassifierTest.java
+++ b/core/tests/coretests/src/android/view/textclassifier/TextClassifierTest.java
@@ -21,8 +21,10 @@
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 
+import android.app.RemoteAction;
 import android.content.Context;
 import android.content.Intent;
+import android.os.Bundle;
 import android.os.LocaleList;
 import android.text.Spannable;
 import android.text.SpannableString;
@@ -246,25 +248,31 @@
     public void testClassifyText_foreignText() {
         LocaleList originalLocales = LocaleList.getDefault();
         LocaleList.setDefault(LocaleList.forLanguageTags("en"));
-        String foreignText = "これは日本語のテキストです";
+        String japaneseText = "これは日本語のテキストです";
 
         Context context = new FakeContextBuilder()
                 .setIntentComponent(Intent.ACTION_TRANSLATE, FakeContextBuilder.DEFAULT_COMPONENT)
                 .build();
         TextClassifier classifier = new TextClassifierImpl(context, TC_CONSTANTS);
         TextClassification.Request request = new TextClassification.Request.Builder(
-                foreignText, 0, foreignText.length())
+                japaneseText, 0, japaneseText.length())
                 .setDefaultLocales(LOCALES)
                 .build();
 
         TextClassification classification = classifier.classifyText(request);
+        RemoteAction translateAction = classification.getActions().get(0);
         assertEquals(1, classification.getActions().size());
         assertEquals(
                 context.getString(com.android.internal.R.string.translate),
-                classification.getActions().get(0).getTitle());
-        Intent intent = (Intent) classification.getExtras()
-                .getParcelableArrayList(TextClassifierImpl.ACTIONS_INTENTS).get(0);
+                translateAction.getTitle());
+
+        assertEquals(translateAction, ExtrasUtils.findTranslateAction(classification));
+        Intent intent = ExtrasUtils.getActionsIntents(classification).get(0);
         assertEquals(Intent.ACTION_TRANSLATE, intent.getAction());
+        Bundle foreignLanguageInfo = ExtrasUtils.getForeignLanguageExtra(classification);
+        assertEquals("ja", ExtrasUtils.getEntityType(foreignLanguageInfo));
+        assertTrue(ExtrasUtils.getScore(foreignLanguageInfo) >= 0);
+        assertTrue(ExtrasUtils.getScore(foreignLanguageInfo) <= 1);
 
         LocaleList.setDefault(originalLocales);
     }
diff --git a/core/tests/coretests/src/com/android/internal/app/ChooserActivityTest.java b/core/tests/coretests/src/com/android/internal/app/ChooserActivityTest.java
index 21fcae7..6b9e69c 100644
--- a/core/tests/coretests/src/com/android/internal/app/ChooserActivityTest.java
+++ b/core/tests/coretests/src/com/android/internal/app/ChooserActivityTest.java
@@ -34,6 +34,9 @@
 
 import android.app.usage.UsageStatsManager;
 import android.content.ClipData;
+import android.content.ClipDescription;
+import android.content.ClipboardManager;
+import android.content.Context;
 import android.content.Intent;
 import android.content.pm.ResolveInfo;
 import android.graphics.Bitmap;
@@ -392,6 +395,32 @@
         assertThat(chosen[0], is(toChoose));
     }
 
+    @Test
+    public void copyTextToClipboard() throws Exception {
+        Intent sendIntent = createSendTextIntent();
+        List<ResolvedComponentInfo> resolvedComponentInfos =
+                createResolvedComponentsForTestWithOtherProfile(1);
+
+        when(ChooserWrapperActivity.sOverrides.resolverListController.getResolversForIntent(
+                Mockito.anyBoolean(),
+                Mockito.anyBoolean(),
+                Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
+
+        final ChooserWrapperActivity activity = mActivityRule
+                .launchActivity(Intent.createChooser(sendIntent, null));
+        waitForIdle();
+
+        onView(withId(R.id.copy_button)).perform(click());
+
+        ClipboardManager clipboard = (ClipboardManager) activity.getSystemService(
+                Context.CLIPBOARD_SERVICE);
+        ClipData clipData = clipboard.getPrimaryClip();
+        assertThat("testing intent sending", is(clipData.getItemAt(0).getText()));
+
+        ClipDescription clipDescription = clipData.getDescription();
+        assertThat("text/plain", is(clipDescription.getMimeType(0)));
+    }
+
     private Intent createSendTextIntent() {
         Intent sendIntent = new Intent();
         sendIntent.setAction(Intent.ACTION_SEND);
diff --git a/data/etc/privapp-permissions-platform.xml b/data/etc/privapp-permissions-platform.xml
index a47ab87..bcac5fd 100644
--- a/data/etc/privapp-permissions-platform.xml
+++ b/data/etc/privapp-permissions-platform.xml
@@ -329,6 +329,7 @@
         <permission name="android.permission.USE_RESERVED_DISK"/>
         <permission name="android.permission.WRITE_MEDIA_STORAGE"/>
         <permission name="android.permission.WRITE_SECURE_SETTINGS"/>
+        <permission name="android.permission.STATUS_BAR_SERVICE"/>
         <permission name="android.permission.REQUEST_INCIDENT_REPORT_APPROVAL"/>
     </privapp-permissions>
 
diff --git a/graphics/java/android/graphics/Bitmap.java b/graphics/java/android/graphics/Bitmap.java
index 97cb2ec..080c5d5 100644
--- a/graphics/java/android/graphics/Bitmap.java
+++ b/graphics/java/android/graphics/Bitmap.java
@@ -21,7 +21,6 @@
 import android.annotation.ColorLong;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
-import android.annotation.TestApi;
 import android.annotation.UnsupportedAppUsage;
 import android.annotation.WorkerThread;
 import android.content.res.ResourcesImpl;
@@ -713,18 +712,6 @@
     }
 
     /**
-     * Create hardware bitmap backed GraphicBuffer.
-     *
-     * @return Bitmap or null if this GraphicBuffer has unsupported PixelFormat.
-     *         currently PIXEL_FORMAT_RGBA_8888 is the only supported format
-     * @hide
-     */
-    @UnsupportedAppUsage
-    public static Bitmap createHardwareBitmap(@NonNull GraphicBuffer graphicBuffer) {
-        return nativeCreateHardwareBitmap(graphicBuffer);
-    }
-
-    /**
      * Create a hardware bitmap backed by a {@link HardwareBuffer}.
      *
      * <p>The passed HardwareBuffer's usage flags must contain
@@ -1762,9 +1749,7 @@
      *         previously assigned color space.
      *
      * @param colorSpace to assign to the bitmap
-     * @hide
      */
-    @TestApi
     public void setColorSpace(@NonNull ColorSpace colorSpace) {
         checkRecycled("setColorSpace called on a recycled bitmap");
         if (colorSpace == null) {
@@ -1815,9 +1800,7 @@
      * @throws IllegalArgumentException if the color space encoded in the long
      *                                  is invalid or unknown.
      *
-     * @hide pending API approval
      */
-    @TestApi
     public void eraseColor(@ColorLong long c) {
         checkRecycled("Can't erase a recycled bitmap");
         if (!isMutable()) {
@@ -1864,9 +1847,7 @@
      * @throws IllegalArgumentException if x, y exceed the bitmap's bounds
      * @throws IllegalStateException if the bitmap's config is {@link Config#HARDWARE}
      *
-     * @hide pending API approval
      */
-    @TestApi
     public Color getColor(int x, int y) {
         checkRecycled("Can't call getColor() on a recycled bitmap");
         checkHardware("unable to getColor(), "
@@ -2256,7 +2237,6 @@
     private static native void nativePrepareToDraw(long nativeBitmap);
     private static native int nativeGetAllocationByteCount(long nativeBitmap);
     private static native Bitmap nativeCopyPreserveInternalConfig(long nativeBitmap);
-    private static native Bitmap nativeCreateHardwareBitmap(GraphicBuffer buffer);
     private static native Bitmap nativeWrapHardwareBufferBitmap(HardwareBuffer buffer,
                                                                 long nativeColorSpace);
     private static native GraphicBuffer nativeCreateGraphicBufferHandle(long nativeBitmap);
diff --git a/graphics/java/android/graphics/BitmapShader.java b/graphics/java/android/graphics/BitmapShader.java
index bcf7229..eb0f2e1 100644
--- a/graphics/java/android/graphics/BitmapShader.java
+++ b/graphics/java/android/graphics/BitmapShader.java
@@ -65,16 +65,6 @@
         return nativeCreate(nativeMatrix, mBitmap, mTileX, mTileY);
     }
 
-    /**
-     * @hide
-     */
-    @Override
-    protected Shader copy() {
-        final BitmapShader copy = new BitmapShader(mBitmap, mTileX, mTileY);
-        copyLocalMatrix(copy);
-        return copy;
-    }
-
     private static native long nativeCreate(long nativeMatrix, Bitmap bitmap,
             int shaderTileModeX, int shaderTileModeY);
 }
diff --git a/graphics/java/android/graphics/ComposeShader.java b/graphics/java/android/graphics/ComposeShader.java
index 70a5f53..189e174 100644
--- a/graphics/java/android/graphics/ComposeShader.java
+++ b/graphics/java/android/graphics/ComposeShader.java
@@ -87,17 +87,6 @@
         }
     }
 
-    /**
-     * @hide
-     */
-    @Override
-    protected Shader copy() {
-        final ComposeShader copy = new ComposeShader(
-                mShaderA.copy(), mShaderB.copy(), mPorterDuffMode);
-        copyLocalMatrix(copy);
-        return copy;
-    }
-
     private static native long nativeCreate(long nativeMatrix,
             long nativeShaderA, long nativeShaderB, int porterDuffMode);
 }
diff --git a/graphics/java/android/graphics/LinearGradient.java b/graphics/java/android/graphics/LinearGradient.java
index 7e6fc35..12e63c0 100644
--- a/graphics/java/android/graphics/LinearGradient.java
+++ b/graphics/java/android/graphics/LinearGradient.java
@@ -17,21 +17,13 @@
 package android.graphics;
 
 import android.annotation.ColorInt;
+import android.annotation.ColorLong;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.annotation.UnsupportedAppUsage;
 
+
 public class LinearGradient extends Shader {
-
-    private static final int TYPE_COLORS_AND_POSITIONS = 1;
-    private static final int TYPE_COLOR_START_AND_COLOR_END = 2;
-
-    /**
-     * Type of the LinearGradient: can be either TYPE_COLORS_AND_POSITIONS or
-     * TYPE_COLOR_START_AND_COLOR_END.
-     */
-    private int mType;
-
     @UnsupportedAppUsage
     private float mX0;
     @UnsupportedAppUsage
@@ -41,16 +33,43 @@
     @UnsupportedAppUsage
     private float mY1;
     @UnsupportedAppUsage
-    private int[] mColors;
-    @UnsupportedAppUsage
     private float[] mPositions;
     @UnsupportedAppUsage
+    private TileMode mTileMode;
+
+    // @ColorInts are replaced by @ColorLongs, but these remain due to @UnsupportedAppUsage.
+    @UnsupportedAppUsage
+    @ColorInt
+    private int[] mColors;
+    @UnsupportedAppUsage
+    @ColorInt
     private int mColor0;
     @UnsupportedAppUsage
+    @ColorInt
     private int mColor1;
 
-    @UnsupportedAppUsage
-    private TileMode mTileMode;
+    @ColorLong
+    private final long[] mColorLongs;
+
+
+    /**
+     * Create a shader that draws a linear gradient along a line.
+     *
+     * @param x0           The x-coordinate for the start of the gradient line
+     * @param y0           The y-coordinate for the start of the gradient line
+     * @param x1           The x-coordinate for the end of the gradient line
+     * @param y1           The y-coordinate for the end of the gradient line
+     * @param colors       The sRGB colors to be distributed along the gradient line
+     * @param positions    May be null. The relative positions [0..1] of
+     *                     each corresponding color in the colors array. If this is null,
+     *                     the the colors are distributed evenly along the gradient line.
+     * @param tile         The Shader tiling mode
+     */
+    public LinearGradient(float x0, float y0, float x1, float y1, @NonNull @ColorInt int[] colors,
+            @Nullable float[] positions, @NonNull TileMode tile) {
+        this(x0, y0, x1, y1, convertColors(colors), positions, tile,
+                ColorSpace.get(ColorSpace.Named.SRGB));
+    }
 
     /**
      * Create a shader that draws a linear gradient along a line.
@@ -64,21 +83,33 @@
      *                     each corresponding color in the colors array. If this is null,
      *                     the the colors are distributed evenly along the gradient line.
      * @param tile         The Shader tiling mode
-    */
-    public LinearGradient(float x0, float y0, float x1, float y1, @NonNull @ColorInt int colors[],
-            @Nullable float positions[], @NonNull TileMode tile) {
-        if (colors.length < 2) {
-            throw new IllegalArgumentException("needs >= 2 number of colors");
-        }
+     *
+     * @throws IllegalArgumentException if there are less than two colors, the colors do
+     *      not share the same {@link ColorSpace} or do not use a valid one, or {@code positions}
+     *      is not {@code null} and has a different length from {@code colors}.
+     */
+    public LinearGradient(float x0, float y0, float x1, float y1, @NonNull @ColorLong long[] colors,
+            @Nullable float[] positions, @NonNull TileMode tile) {
+        this(x0, y0, x1, y1, colors.clone(), positions, tile, detectColorSpace(colors));
+    }
+
+    /**
+     * Base constructor. Assumes @param colors is a copy that this object can hold onto,
+     * and all colors share @param colorSpace.
+     */
+    private LinearGradient(float x0, float y0, float x1, float y1,
+            @NonNull @ColorLong long[] colors, @Nullable float[] positions, @NonNull TileMode tile,
+            @NonNull ColorSpace colorSpace) {
+        super(colorSpace);
+
         if (positions != null && colors.length != positions.length) {
             throw new IllegalArgumentException("color and position arrays must be of equal length");
         }
-        mType = TYPE_COLORS_AND_POSITIONS;
         mX0 = x0;
         mY0 = y0;
         mX1 = x1;
         mY1 = y1;
-        mColors = colors.clone();
+        mColorLongs = colors;
         mPositions = positions != null ? positions.clone() : null;
         mTileMode = tile;
     }
@@ -90,54 +121,43 @@
      * @param y0       The y-coordinate for the start of the gradient line
      * @param x1       The x-coordinate for the end of the gradient line
      * @param y1       The y-coordinate for the end of the gradient line
+     * @param color0   The sRGB color at the start of the gradient line.
+     * @param color1   The sRGB color at the end of the gradient line.
+     * @param tile     The Shader tiling mode
+     */
+    public LinearGradient(float x0, float y0, float x1, float y1,
+            @ColorInt int color0, @ColorInt int color1,
+            @NonNull TileMode tile) {
+        this(x0, y0, x1, y1, Color.pack(color0), Color.pack(color1), tile);
+    }
+
+    /**
+     * Create a shader that draws a linear gradient along a line.
+     *
+     * @param x0       The x-coordinate for the start of the gradient line
+     * @param y0       The y-coordinate for the start of the gradient line
+     * @param x1       The x-coordinate for the end of the gradient line
+     * @param y1       The y-coordinate for the end of the gradient line
      * @param color0   The color at the start of the gradient line.
      * @param color1   The color at the end of the gradient line.
      * @param tile     The Shader tiling mode
-    */
+     *
+     * @throws IllegalArgumentException if the colors do
+     *      not share the same {@link ColorSpace} or do not use a valid one.
+     */
     public LinearGradient(float x0, float y0, float x1, float y1,
-            @ColorInt int color0, @ColorInt int color1,
+            @ColorLong long color0, @ColorLong long color1,
             @NonNull TileMode tile) {
-        mType = TYPE_COLOR_START_AND_COLOR_END;
-        mX0 = x0;
-        mY0 = y0;
-        mX1 = x1;
-        mY1 = y1;
-        mColor0 = color0;
-        mColor1 = color1;
-        mColors = null;
-        mPositions = null;
-        mTileMode = tile;
+        this(x0, y0, x1, y1, new long[] {color0, color1}, null, tile);
     }
 
     @Override
     long createNativeInstance(long nativeMatrix) {
-        if (mType == TYPE_COLORS_AND_POSITIONS) {
-            return nativeCreate1(nativeMatrix, mX0, mY0, mX1, mY1,
-                    mColors, mPositions, mTileMode.nativeInt);
-        } else { // TYPE_COLOR_START_AND_COLOR_END
-            return nativeCreate2(nativeMatrix, mX0, mY0, mX1, mY1,
-                    mColor0, mColor1, mTileMode.nativeInt);
-        }
+        return nativeCreate(nativeMatrix, mX0, mY0, mX1, mY1,
+                mColorLongs, mPositions, mTileMode.nativeInt,
+                colorSpace().getNativeInstance());
     }
 
-    /**
-     * @hide
-     */
-    @Override
-    protected Shader copy() {
-        final LinearGradient copy;
-        if (mType == TYPE_COLORS_AND_POSITIONS) {
-            copy = new LinearGradient(mX0, mY0, mX1, mY1, mColors.clone(),
-                    mPositions != null ? mPositions.clone() : null, mTileMode);
-        } else { // TYPE_COLOR_START_AND_COLOR_END
-            copy = new LinearGradient(mX0, mY0, mX1, mY1, mColor0, mColor1, mTileMode);
-        }
-        copyLocalMatrix(copy);
-        return copy;
-    }
-
-    private native long nativeCreate1(long matrix, float x0, float y0, float x1, float y1,
-            int colors[], float positions[], int tileMode);
-    private native long nativeCreate2(long matrix, float x0, float y0, float x1, float y1,
-            int color0, int color1, int tileMode);
+    private native long nativeCreate(long matrix, float x0, float y0, float x1, float y1,
+            long[] colors, float[] positions, int tileMode, long colorSpaceHandle);
 }
diff --git a/graphics/java/android/graphics/Paint.java b/graphics/java/android/graphics/Paint.java
index 7eee6f4..73442db 100644
--- a/graphics/java/android/graphics/Paint.java
+++ b/graphics/java/android/graphics/Paint.java
@@ -24,7 +24,6 @@
 import android.annotation.Nullable;
 import android.annotation.Px;
 import android.annotation.Size;
-import android.annotation.TestApi;
 import android.annotation.UnsupportedAppUsage;
 import android.graphics.fonts.FontVariationAxis;
 import android.os.Build;
@@ -974,10 +973,7 @@
      * @see Color for APIs that help manipulate a color long.
      *
      * @return the paint's color (and alpha).
-     *
-     * @hide pending API approval
      */
-    @TestApi
     @ColorLong
     public long getColorLong() {
         return mColor;
@@ -1006,10 +1002,7 @@
      *      to set in the paint.
      * @throws IllegalArgumentException if the color space encoded in the long
      *      is invalid or unknown.
-     *
-     * @hide pending API approval
      */
-    @TestApi
     public void setColor(@ColorLong long color) {
         ColorSpace cs = Color.colorSpace(color);
         float r = Color.red(color);
@@ -1445,10 +1438,7 @@
      *
      * @throws IllegalArgumentException if the color space encoded in the long
      *      is invalid or unknown.
-     *
-     * @hide pending API approval
      */
-    @TestApi
     public void setShadowLayer(float radius, float dx, float dy, @ColorLong long shadowColor) {
         ColorSpace cs = Color.colorSpace(shadowColor);
         float r = Color.red(shadowColor);
@@ -1517,9 +1507,7 @@
      * Returns the color of the shadow layer.
      * @see #setShadowLayer(float,float,float,int)
      * @see #setShadowLayer(float,float,float,long)
-     * @hide pending API approval
      */
-    @TestApi
     public @ColorLong long getShadowLayerColorLong() {
         return mShadowLayerColor;
     }
diff --git a/graphics/java/android/graphics/RadialGradient.java b/graphics/java/android/graphics/RadialGradient.java
index 41d2628..acbe3da 100644
--- a/graphics/java/android/graphics/RadialGradient.java
+++ b/graphics/java/android/graphics/RadialGradient.java
@@ -16,22 +16,13 @@
 
 package android.graphics;
 
+import android.annotation.ColorInt;
+import android.annotation.ColorLong;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
-import android.annotation.ColorInt;
 import android.annotation.UnsupportedAppUsage;
 
 public class RadialGradient extends Shader {
-
-    private static final int TYPE_COLORS_AND_POSITIONS = 1;
-    private static final int TYPE_COLOR_CENTER_AND_COLOR_EDGE = 2;
-
-    /**
-     * Type of the RadialGradient: can be either TYPE_COLORS_AND_POSITIONS or
-     * TYPE_COLOR_CENTER_AND_COLOR_EDGE.
-     */
-    private int mType;
-
     @UnsupportedAppUsage
     private float mX;
     @UnsupportedAppUsage
@@ -39,16 +30,43 @@
     @UnsupportedAppUsage
     private float mRadius;
     @UnsupportedAppUsage
-    private int[] mColors;
-    @UnsupportedAppUsage
     private float[] mPositions;
     @UnsupportedAppUsage
+    private TileMode mTileMode;
+
+    // @ColorInts are replaced by @ColorLongs, but these remain due to @UnsupportedAppUsage.
+    @UnsupportedAppUsage
+    @ColorInt
+    private int[] mColors;
+    @UnsupportedAppUsage
+    @ColorInt
     private int mCenterColor;
     @UnsupportedAppUsage
+    @ColorInt
     private int mEdgeColor;
 
-    @UnsupportedAppUsage
-    private TileMode mTileMode;
+    @ColorLong
+    private final long[] mColorLongs;
+
+    /**
+     * Create a shader that draws a radial gradient given the center and radius.
+     *
+     * @param centerX  The x-coordinate of the center of the radius
+     * @param centerY  The y-coordinate of the center of the radius
+     * @param radius   Must be positive. The radius of the circle for this gradient.
+     * @param colors   The sRGB colors to be distributed between the center and edge of the circle
+     * @param stops    May be <code>null</code>. Valid values are between <code>0.0f</code> and
+     *                 <code>1.0f</code>. The relative position of each corresponding color in
+     *                 the colors array. If <code>null</code>, colors are distributed evenly
+     *                 between the center and edge of the circle.
+     * @param tileMode The Shader tiling mode
+     */
+    public RadialGradient(float centerX, float centerY, float radius,
+            @NonNull @ColorInt int[] colors, @Nullable float[] stops,
+            @NonNull TileMode tileMode) {
+        this(centerX, centerY, radius, convertColors(colors), stops, tileMode,
+                ColorSpace.get(ColorSpace.Named.SRGB));
+    }
 
     /**
      * Create a shader that draws a radial gradient given the center and radius.
@@ -62,24 +80,36 @@
      *                 the colors array. If <code>null</code>, colors are distributed evenly
      *                 between the center and edge of the circle.
      * @param tileMode The Shader tiling mode
+     *
+     * @throws IllegalArgumentException if there are less than two colors, the colors do
+     *      not share the same {@link ColorSpace} or do not use a valid one, or {@code stops}
+     *      is not {@code null} and has a different length from {@code colors}.
      */
     public RadialGradient(float centerX, float centerY, float radius,
-            @NonNull @ColorInt int colors[], @Nullable float stops[],
+            @NonNull @ColorLong long[] colors, @Nullable float[] stops,
             @NonNull TileMode tileMode) {
+        this(centerX, centerY, radius, colors.clone(), stops, tileMode, detectColorSpace(colors));
+    }
+
+    /**
+     * Base constructor. Assumes @param colors is a copy that this object can hold onto,
+     * and all colors share @param colorSpace.
+     */
+    private RadialGradient(float centerX, float centerY, float radius,
+            @NonNull @ColorLong long[] colors, @Nullable float[] stops,
+            @NonNull TileMode tileMode, ColorSpace colorSpace) {
+        super(colorSpace);
+
         if (radius <= 0) {
             throw new IllegalArgumentException("radius must be > 0");
         }
-        if (colors.length < 2) {
-            throw new IllegalArgumentException("needs >= 2 number of colors");
-        }
         if (stops != null && colors.length != stops.length) {
             throw new IllegalArgumentException("color and position arrays must be of equal length");
         }
-        mType = TYPE_COLORS_AND_POSITIONS;
         mX = centerX;
         mY = centerY;
         mRadius = radius;
-        mColors = colors.clone();
+        mColorLongs = colors;
         mPositions = stops != null ? stops.clone() : null;
         mTileMode = tileMode;
     }
@@ -90,54 +120,41 @@
      * @param centerX     The x-coordinate of the center of the radius
      * @param centerY     The y-coordinate of the center of the radius
      * @param radius      Must be positive. The radius of the circle for this gradient
+     * @param centerColor The sRGB color at the center of the circle.
+     * @param edgeColor   The sRGB color at the edge of the circle.
+     * @param tileMode    The Shader tiling mode
+     */
+    public RadialGradient(float centerX, float centerY, float radius,
+            @ColorInt int centerColor, @ColorInt int edgeColor, @NonNull TileMode tileMode) {
+        this(centerX, centerY, radius, Color.pack(centerColor), Color.pack(edgeColor), tileMode);
+    }
+
+    /**
+     * Create a shader that draws a radial gradient given the center and radius.
+     *
+     * @param centerX     The x-coordinate of the center of the radius
+     * @param centerY     The y-coordinate of the center of the radius
+     * @param radius      Must be positive. The radius of the circle for this gradient
      * @param centerColor The color at the center of the circle.
      * @param edgeColor   The color at the edge of the circle.
      * @param tileMode    The Shader tiling mode
+     *
+     * @throws IllegalArgumentException if the colors do
+     *      not share the same {@link ColorSpace} or do not use a valid one.
      */
     public RadialGradient(float centerX, float centerY, float radius,
-            @ColorInt int centerColor, @ColorInt int edgeColor, @NonNull TileMode tileMode) {
-        if (radius <= 0) {
-            throw new IllegalArgumentException("radius must be > 0");
-        }
-        mType = TYPE_COLOR_CENTER_AND_COLOR_EDGE;
-        mX = centerX;
-        mY = centerY;
-        mRadius = radius;
-        mCenterColor = centerColor;
-        mEdgeColor = edgeColor;
-        mTileMode = tileMode;
+            @ColorLong long centerColor, @ColorLong long edgeColor, @NonNull TileMode tileMode) {
+        this(centerX, centerY, radius, new long[] {centerColor, edgeColor}, null, tileMode);
     }
 
     @Override
     long createNativeInstance(long nativeMatrix) {
-        if (mType == TYPE_COLORS_AND_POSITIONS) {
-            return nativeCreate1(nativeMatrix, mX, mY, mRadius,
-                    mColors, mPositions, mTileMode.nativeInt);
-        } else { // TYPE_COLOR_CENTER_AND_COLOR_EDGE
-            return nativeCreate2(nativeMatrix, mX, mY, mRadius,
-                    mCenterColor, mEdgeColor, mTileMode.nativeInt);
-        }
+        return nativeCreate(nativeMatrix, mX, mY, mRadius,
+                mColorLongs, mPositions, mTileMode.nativeInt,
+                colorSpace().getNativeInstance());
     }
 
-    /**
-     * @hide
-     */
-    @Override
-    protected Shader copy() {
-        final RadialGradient copy;
-        if (mType == TYPE_COLORS_AND_POSITIONS) {
-            copy = new RadialGradient(mX, mY, mRadius, mColors.clone(),
-                    mPositions != null ? mPositions.clone() : null, mTileMode);
-        } else { // TYPE_COLOR_CENTER_AND_COLOR_EDGE
-            copy = new RadialGradient(mX, mY, mRadius, mCenterColor, mEdgeColor, mTileMode);
-        }
-        copyLocalMatrix(copy);
-        return copy;
-    }
-
-    private static native long nativeCreate1(long matrix, float x, float y, float radius,
-            int colors[], float positions[], int tileMode);
-    private static native long nativeCreate2(long matrix, float x, float y, float radius,
-            int color0, int color1, int tileMode);
+    private static native long nativeCreate(long matrix, float x, float y, float radius,
+            @ColorLong long[] colors, float[] positions, int tileMode, long colorSpaceHandle);
 }
 
diff --git a/graphics/java/android/graphics/Shader.java b/graphics/java/android/graphics/Shader.java
index 40bcc9e..d555128 100644
--- a/graphics/java/android/graphics/Shader.java
+++ b/graphics/java/android/graphics/Shader.java
@@ -16,6 +16,8 @@
 
 package android.graphics;
 
+import android.annotation.ColorInt;
+import android.annotation.ColorLong;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.annotation.UnsupportedAppUsage;
@@ -39,7 +41,32 @@
      * @deprecated Use subclass constructors directly instead.
      */
     @Deprecated
-    public Shader() {}
+    public Shader() {
+        mColorSpace = null;
+    }
+
+    /**
+     * @hide
+     */
+    public Shader(ColorSpace colorSpace) {
+        mColorSpace = colorSpace;
+        if (colorSpace == null) {
+            throw new IllegalArgumentException(
+                    "Use Shader() to create a Shader with no ColorSpace");
+        }
+
+        // This just ensures that if the ColorSpace is invalid, the Exception will be thrown now.
+        mColorSpace.getNativeInstance();
+    }
+
+    private final ColorSpace mColorSpace;
+
+    /**
+     * @hide
+     */
+    protected ColorSpace colorSpace() {
+        return mColorSpace;
+    }
 
     /**
      * Current native shader instance. Created and updated lazily when {@link #getNativeInstance()}
@@ -135,21 +162,6 @@
     protected void verifyNativeInstance() {
     }
 
-    /**
-     * @hide
-     */
-    protected Shader copy() {
-        final Shader copy = new Shader();
-        copyLocalMatrix(copy);
-        return copy;
-    }
-
-    /**
-     * @hide
-     */
-    protected void copyLocalMatrix(Shader dest) {
-        dest.mLocalMatrix.set(mLocalMatrix);
-    }
 
     /**
      * @hide
@@ -169,6 +181,43 @@
         return mNativeInstance;
     }
 
+    /**
+     * @hide
+     */
+    public static @ColorLong long[] convertColors(@NonNull @ColorInt int[] colors) {
+        if (colors.length < 2) {
+            throw new IllegalArgumentException("needs >= 2 number of colors");
+        }
+
+        long[] colorLongs = new long[colors.length];
+        for (int i = 0; i < colors.length; ++i) {
+            colorLongs[i] = Color.pack(colors[i]);
+        }
+
+        return colorLongs;
+    }
+
+    /**
+     * Detect the ColorSpace that the {@code colors} share.
+     *
+     * @throws IllegalArgumentException if the colors do not all share the same,
+     *      valid ColorSpace, or if there are less than 2 colors.
+     *
+     * @hide
+     */
+    public static ColorSpace detectColorSpace(@NonNull @ColorLong long[] colors) {
+        if (colors.length < 2) {
+            throw new IllegalArgumentException("needs >= 2 number of colors");
+        }
+        final ColorSpace colorSpace = Color.colorSpace(colors[0]);
+        for (int i = 1; i < colors.length; ++i) {
+            if (Color.colorSpace(colors[i]) != colorSpace) {
+                throw new IllegalArgumentException("All colors must be in the same ColorSpace!");
+            }
+        }
+        return colorSpace;
+    }
+
     private static native long nativeGetFinalizer();
 
 }
diff --git a/graphics/java/android/graphics/SweepGradient.java b/graphics/java/android/graphics/SweepGradient.java
index f944d85..667f45a 100644
--- a/graphics/java/android/graphics/SweepGradient.java
+++ b/graphics/java/android/graphics/SweepGradient.java
@@ -17,34 +17,52 @@
 package android.graphics;
 
 import android.annotation.ColorInt;
+import android.annotation.ColorLong;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.annotation.UnsupportedAppUsage;
 
 public class SweepGradient extends Shader {
-
-    private static final int TYPE_COLORS_AND_POSITIONS = 1;
-    private static final int TYPE_COLOR_START_AND_COLOR_END = 2;
-
-    /**
-     * Type of the LinearGradient: can be either TYPE_COLORS_AND_POSITIONS or
-     * TYPE_COLOR_START_AND_COLOR_END.
-     */
-    private int mType;
-
     @UnsupportedAppUsage
     private float mCx;
     @UnsupportedAppUsage
     private float mCy;
     @UnsupportedAppUsage
+    private float[] mPositions;
+
+    // @ColorInts are replaced by @ColorLongs, but these remain due to @UnsupportedAppUsage.
+    @UnsupportedAppUsage
+    @ColorInt
     private int[] mColors;
     @UnsupportedAppUsage
-    private float[] mPositions;
-    @UnsupportedAppUsage
+    @ColorInt
     private int mColor0;
     @UnsupportedAppUsage
+    @ColorInt
     private int mColor1;
 
+    @ColorLong
+    private final long[] mColorLongs;
+
+    /**
+     * A Shader that draws a sweep gradient around a center point.
+     *
+     * @param cx       The x-coordinate of the center
+     * @param cy       The y-coordinate of the center
+     * @param colors   The sRGB colors to be distributed between around the center.
+     *                 There must be at least 2 colors in the array.
+     * @param positions May be NULL. The relative position of
+     *                 each corresponding color in the colors array, beginning
+     *                 with 0 and ending with 1.0. If the values are not
+     *                 monotonic, the drawing may produce unexpected results.
+     *                 If positions is NULL, then the colors are automatically
+     *                 spaced evenly.
+     */
+    public SweepGradient(float cx, float cy, @NonNull @ColorInt int[] colors,
+            @Nullable float[] positions) {
+        this(cx, cy, convertColors(colors), positions, ColorSpace.get(ColorSpace.Named.SRGB));
+    }
+
     /**
      * A Shader that draws a sweep gradient around a center point.
      *
@@ -58,20 +76,30 @@
      *                 monotonic, the drawing may produce unexpected results.
      *                 If positions is NULL, then the colors are automatically
      *                 spaced evenly.
+     * @throws IllegalArgumentException if there are less than two colors, the colors do
+     *      not share the same {@link ColorSpace} or do not use a valid one, or {@code positions}
+     *      is not {@code null} and has a different length from {@code colors}.
      */
-    public SweepGradient(float cx, float cy,
-            @NonNull @ColorInt int colors[], @Nullable float positions[]) {
-        if (colors.length < 2) {
-            throw new IllegalArgumentException("needs >= 2 number of colors");
-        }
+    public SweepGradient(float cx, float cy, @NonNull @ColorLong long[] colors,
+            @Nullable float[] positions) {
+        this(cx, cy, colors.clone(), positions, detectColorSpace(colors));
+    }
+
+    /**
+     * Base constructor. Assumes @param colors is a copy that this object can hold onto,
+     * and all colors share @param colorSpace.
+     */
+    private SweepGradient(float cx, float cy, @NonNull @ColorLong long[] colors,
+            @Nullable float[] positions, ColorSpace colorSpace) {
+        super(colorSpace);
+
         if (positions != null && colors.length != positions.length) {
             throw new IllegalArgumentException(
                     "color and position arrays must be of equal length");
         }
-        mType = TYPE_COLORS_AND_POSITIONS;
         mCx = cx;
         mCy = cy;
-        mColors = colors.clone();
+        mColorLongs = colors;
         mPositions = positions != null ? positions.clone() : null;
     }
 
@@ -80,47 +108,35 @@
      *
      * @param cx       The x-coordinate of the center
      * @param cy       The y-coordinate of the center
+     * @param color0   The sRGB color to use at the start of the sweep
+     * @param color1   The sRGB color to use at the end of the sweep
+     */
+    public SweepGradient(float cx, float cy, @ColorInt int color0, @ColorInt int color1) {
+        this(cx, cy, Color.pack(color0), Color.pack(color1));
+    }
+
+    /**
+     * A Shader that draws a sweep gradient around a center point.
+     *
+     * @param cx       The x-coordinate of the center
+     * @param cy       The y-coordinate of the center
      * @param color0   The color to use at the start of the sweep
      * @param color1   The color to use at the end of the sweep
+     *
+     * @throws IllegalArgumentException if the colors do
+     *      not share the same {@link ColorSpace} or do not use a valid one.
      */
-    public SweepGradient(float cx, float cy, @ColorInt int color0, @ColorInt int color1) {
-        mType = TYPE_COLOR_START_AND_COLOR_END;
-        mCx = cx;
-        mCy = cy;
-        mColor0 = color0;
-        mColor1 = color1;
-        mColors = null;
-        mPositions = null;
+    public SweepGradient(float cx, float cy, @ColorLong long color0, @ColorLong long color1) {
+        this(cx, cy, new long[] {color0, color1}, null);
     }
 
     @Override
     long createNativeInstance(long nativeMatrix) {
-        if (mType == TYPE_COLORS_AND_POSITIONS) {
-            return nativeCreate1(nativeMatrix, mCx, mCy, mColors, mPositions);
-        } else { // TYPE_COLOR_START_AND_COLOR_END
-            return nativeCreate2(nativeMatrix, mCx, mCy, mColor0, mColor1);
-        }
+        return nativeCreate(nativeMatrix, mCx, mCy, mColorLongs, mPositions,
+                colorSpace().getNativeInstance());
     }
 
-    /**
-     * @hide
-     */
-    @Override
-    protected Shader copy() {
-        final SweepGradient copy;
-        if (mType == TYPE_COLORS_AND_POSITIONS) {
-            copy = new SweepGradient(mCx, mCy, mColors.clone(),
-                    mPositions != null ? mPositions.clone() : null);
-        } else { // TYPE_COLOR_START_AND_COLOR_END
-            copy = new SweepGradient(mCx, mCy, mColor0, mColor1);
-        }
-        copyLocalMatrix(copy);
-        return copy;
-    }
-
-    private static native long nativeCreate1(long matrix, float x, float y,
-            int colors[], float positions[]);
-    private static native long nativeCreate2(long matrix, float x, float y,
-            int color0, int color1);
+    private static native long nativeCreate(long matrix, float x, float y,
+            long[] colors, float[] positions, long colorSpaceHandle);
 }
 
diff --git a/libs/hwui/HardwareBitmapUploader.cpp b/libs/hwui/HardwareBitmapUploader.cpp
index 39bfcdd..6b7ec97 100644
--- a/libs/hwui/HardwareBitmapUploader.cpp
+++ b/libs/hwui/HardwareBitmapUploader.cpp
@@ -86,7 +86,7 @@
     return sEglManager.eglDisplay();
 }
 
-static bool hasFP16Support() {
+bool HardwareBitmapUploader::hasFP16Support() {
     static std::once_flag sOnce;
     static bool hasFP16Support = false;
 
@@ -127,7 +127,7 @@
             formatInfo.type = GL_UNSIGNED_BYTE;
             break;
         case kRGBA_F16_SkColorType:
-            formatInfo.isSupported = hasFP16Support();
+            formatInfo.isSupported = HardwareBitmapUploader::hasFP16Support();
             if (formatInfo.isSupported) {
                 formatInfo.type = GL_HALF_FLOAT;
                 formatInfo.pixelFormat = PIXEL_FORMAT_RGBA_FP16;
diff --git a/libs/hwui/HardwareBitmapUploader.h b/libs/hwui/HardwareBitmapUploader.h
index 40f2b0c..6f41e6d 100644
--- a/libs/hwui/HardwareBitmapUploader.h
+++ b/libs/hwui/HardwareBitmapUploader.h
@@ -20,10 +20,12 @@
 
 namespace android::uirenderer {
 
-class HardwareBitmapUploader {
+class ANDROID_API HardwareBitmapUploader {
 public:
     static sk_sp<Bitmap> allocateHardwareBitmap(const SkBitmap& sourceBitmap);
     static void terminate();
+
+    static bool hasFP16Support();
 };
 
 }  // namespace android::uirenderer
diff --git a/libs/hwui/RecordingCanvas.cpp b/libs/hwui/RecordingCanvas.cpp
index 6dc9d34..8594766 100644
--- a/libs/hwui/RecordingCanvas.cpp
+++ b/libs/hwui/RecordingCanvas.cpp
@@ -759,6 +759,8 @@
 void RecordingCanvas::reset(DisplayListData* dl, const SkIRect& bounds) {
     this->resetCanvas(bounds.right(), bounds.bottom());
     fDL = dl;
+    mClipMayBeComplex = false;
+    mSaveCount = mComplexSaveCount = 0;
 }
 
 sk_sp<SkSurface> RecordingCanvas::onNewSurface(const SkImageInfo&, const SkSurfaceProps&) {
@@ -770,6 +772,7 @@
 }
 
 void RecordingCanvas::willSave() {
+    mSaveCount++;
     fDL->save();
 }
 SkCanvas::SaveLayerStrategy RecordingCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) {
@@ -778,6 +781,11 @@
     return SkCanvas::kNoLayer_SaveLayerStrategy;
 }
 void RecordingCanvas::willRestore() {
+    mSaveCount--;
+    if (mSaveCount < mComplexSaveCount) {
+        mClipMayBeComplex = false;
+        mComplexSaveCount = 0;
+    }
     fDL->restore();
 }
 
@@ -798,17 +806,27 @@
 
 void RecordingCanvas::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle style) {
     fDL->clipRect(rect, op, style == kSoft_ClipEdgeStyle);
+    if (!getTotalMatrix().isScaleTranslate()) {
+        setClipMayBeComplex();
+    }
     this->INHERITED::onClipRect(rect, op, style);
 }
 void RecordingCanvas::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle style) {
+    if (rrect.getType() > SkRRect::kRect_Type || !getTotalMatrix().isScaleTranslate()) {
+        setClipMayBeComplex();
+    }
     fDL->clipRRect(rrect, op, style == kSoft_ClipEdgeStyle);
     this->INHERITED::onClipRRect(rrect, op, style);
 }
 void RecordingCanvas::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle style) {
+    setClipMayBeComplex();
     fDL->clipPath(path, op, style == kSoft_ClipEdgeStyle);
     this->INHERITED::onClipPath(path, op, style);
 }
 void RecordingCanvas::onClipRegion(const SkRegion& region, SkClipOp op) {
+    if (region.isComplex() || !getTotalMatrix().isScaleTranslate()) {
+        setClipMayBeComplex();
+    }
     fDL->clipRegion(region, op);
     this->INHERITED::onClipRegion(region, op);
 }
diff --git a/libs/hwui/RecordingCanvas.h b/libs/hwui/RecordingCanvas.h
index caaef67..3a76ca1 100644
--- a/libs/hwui/RecordingCanvas.h
+++ b/libs/hwui/RecordingCanvas.h
@@ -203,10 +203,41 @@
 
     void drawVectorDrawable(VectorDrawableRoot* tree);
 
+    /**
+     * If "isClipMayBeComplex" returns false, it is guaranteed the current clip is a rectangle.
+     * If the return value is true, then clip may or may not be complex (there is no guarantee).
+     */
+    inline bool isClipMayBeComplex() { return mClipMayBeComplex; }
+
 private:
     typedef SkCanvasVirtualEnforcer<SkNoDrawCanvas> INHERITED;
 
+    inline void setClipMayBeComplex() {
+        if (!mClipMayBeComplex) {
+            mComplexSaveCount = mSaveCount;
+            mClipMayBeComplex = true;
+        }
+    }
+
     DisplayListData* fDL;
+
+    /**
+     * mClipMayBeComplex tracks if the current clip is a rectangle. This flag is used to promote
+     * FunctorDrawable to a layer, if it is clipped by a non-rect.
+     */
+    bool mClipMayBeComplex = false;
+
+    /**
+     * mSaveCount is the current level of our save tree.
+     */
+    int mSaveCount = 0;
+
+    /**
+     * mComplexSaveCount is the first save level, which has a complex clip. Every level below
+     * mComplexSaveCount is assumed to have a complex clip and every level above mComplexSaveCount
+     * is guaranteed to not be complex.
+     */
+    int mComplexSaveCount = 0;
 };
 
 }  // namespace uirenderer
diff --git a/libs/hwui/RenderProperties.h b/libs/hwui/RenderProperties.h
index ddb7e4e..e6710cc 100644
--- a/libs/hwui/RenderProperties.h
+++ b/libs/hwui/RenderProperties.h
@@ -151,6 +151,7 @@
         // parent may have already dictated that a descendant layer is needed
         bool functorsNeedLayer =
                 ancestorDictatesFunctorsNeedLayer
+                || CC_UNLIKELY(isClipMayBeComplex())
 
                 // Round rect clipping forces layer for functors
                 || CC_UNLIKELY(getOutline().willRoundRectClip()) ||
@@ -193,6 +194,12 @@
 
     bool isProjectionReceiver() const { return mPrimitiveFields.mProjectionReceiver; }
 
+    bool setClipMayBeComplex(bool isClipMayBeComplex) {
+        return RP_SET(mPrimitiveFields.mClipMayBeComplex, isClipMayBeComplex);
+    }
+
+    bool isClipMayBeComplex() const { return mPrimitiveFields.mClipMayBeComplex; }
+
     bool setStaticMatrix(const SkMatrix* matrix) {
         delete mStaticMatrix;
         if (matrix) {
@@ -563,6 +570,7 @@
         bool mProjectBackwards = false;
         bool mProjectionReceiver = false;
         bool mAllowForceDark = true;
+        bool mClipMayBeComplex = false;
         Rect mClipBounds;
         Outline mOutline;
         RevealClip mRevealClip;
diff --git a/libs/hwui/pipeline/skia/SkiaDisplayList.cpp b/libs/hwui/pipeline/skia/SkiaDisplayList.cpp
index 230065c..29d5ef2 100644
--- a/libs/hwui/pipeline/skia/SkiaDisplayList.cpp
+++ b/libs/hwui/pipeline/skia/SkiaDisplayList.cpp
@@ -73,7 +73,6 @@
         RenderNode* childNode = child.getRenderNode();
         Matrix4 mat4(child.getRecordedMatrix());
         info.damageAccumulator->pushTransform(&mat4);
-        // TODO: a layer is needed if the canvas is rotated or has a non-rect clip
         info.hasBackwardProjectedNodes = false;
         childFn(childNode, observer, info, functorsNeedLayer);
         hasBackwardProjectedNodesSubtree |= info.hasBackwardProjectedNodes;
diff --git a/libs/hwui/pipeline/skia/SkiaRecordingCanvas.cpp b/libs/hwui/pipeline/skia/SkiaRecordingCanvas.cpp
index d54275f..d945635 100644
--- a/libs/hwui/pipeline/skia/SkiaRecordingCanvas.cpp
+++ b/libs/hwui/pipeline/skia/SkiaRecordingCanvas.cpp
@@ -113,6 +113,10 @@
     // Record the child node. Drawable dtor will be invoked when mChildNodes deque is cleared.
     mDisplayList->mChildNodes.emplace_back(renderNode, asSkCanvas(), true, mCurrentBarrier);
     auto& renderNodeDrawable = mDisplayList->mChildNodes.back();
+    if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) {
+        // Put Vulkan WebViews with non-rectangular clips in a HW layer
+        renderNode->mutateStagingProperties().setClipMayBeComplex(mRecorder.isClipMayBeComplex());
+    }
     drawDrawable(&renderNodeDrawable);
 
     // use staging property, since recording on UI thread
@@ -138,10 +142,8 @@
 void SkiaRecordingCanvas::drawWebViewFunctor(int functor) {
     FunctorDrawable* functorDrawable;
     if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) {
-        // TODO(cblume) use VkFunctorDrawable instead of VkInteropFunctorDrawable here when the
-        // interop is disabled.
         functorDrawable =
-                mDisplayList->allocateDrawable<VkInteropFunctorDrawable>(functor, asSkCanvas());
+                mDisplayList->allocateDrawable<VkFunctorDrawable>(functor, asSkCanvas());
     } else {
         functorDrawable = mDisplayList->allocateDrawable<GLFunctorDrawable>(functor, asSkCanvas());
     }
diff --git a/location/java/android/location/LocationManager.java b/location/java/android/location/LocationManager.java
index 59c6a0a..63b57d1 100644
--- a/location/java/android/location/LocationManager.java
+++ b/location/java/android/location/LocationManager.java
@@ -181,22 +181,14 @@
         "android.location.GPS_ENABLED_CHANGE";
 
     /**
-     * Broadcast intent action when the configured location providers
-     * change. For use with {@link #isProviderEnabled(String)}. If you're interacting with the
-     * {@link android.provider.Settings.Secure#LOCATION_MODE} API, use {@link #MODE_CHANGED_ACTION}
-     * instead.
+     * Broadcast intent action when the set of enabled location providers changes. To check the
+     * status of a provider, use {@link #isProviderEnabled(String)}.
      */
-    public static final String PROVIDERS_CHANGED_ACTION =
-        "android.location.PROVIDERS_CHANGED";
+    public static final String PROVIDERS_CHANGED_ACTION = "android.location.PROVIDERS_CHANGED";
 
     /**
-     * Broadcast intent action when {@link android.provider.Settings.Secure#LOCATION_MODE} changes.
-     * For use with the {@link android.provider.Settings.Secure#LOCATION_MODE} API.
-     * If you're interacting with {@link #isProviderEnabled(String)}, use
-     * {@link #PROVIDERS_CHANGED_ACTION} instead.
-     *
-     * In the future, there may be mode changes that do not result in
-     * {@link #PROVIDERS_CHANGED_ACTION} broadcasts.
+     * Broadcast intent action when the device location mode changes. To check the location mode,
+     * use {@link #isLocationEnabled()}.
      */
     public static final String MODE_CHANGED_ACTION = "android.location.MODE_CHANGED";
 
@@ -207,8 +199,10 @@
      * If you're interacting with {@link #isProviderEnabled(String)}, use
      * {@link #PROVIDERS_CHANGED_ACTION} instead.
      *
+     * @deprecated Do not use.
      * @hide
      */
+    @Deprecated
     public static final String MODE_CHANGING_ACTION = "com.android.settings.location.MODE_CHANGING";
 
     /**
@@ -299,7 +293,7 @@
             "com.android.settings.location.FOOTER_STRING";
 
     // Map from LocationListeners to their associated ListenerTransport objects
-    private HashMap<LocationListener,ListenerTransport> mListeners =
+    private final HashMap<LocationListener, ListenerTransport> mListeners =
         new HashMap<LocationListener,ListenerTransport>();
 
     private class ListenerTransport extends ILocationListener.Stub {
@@ -1264,39 +1258,20 @@
     }
 
     /**
-     * Returns the current enabled/disabled status of location
+     * Returns the current enabled/disabled state of location. To listen for changes, see
+     * {@link #MODE_CHANGED_ACTION}.
      *
-     * @return true if location is enabled. false if location is disabled.
+     * @return true if location is enabled and false if location is disabled.
      */
     public boolean isLocationEnabled() {
         return isLocationEnabledForUser(Process.myUserHandle());
     }
 
     /**
-     * Method for enabling or disabling location.
-     *
-     * @param enabled true to enable location. false to disable location
-     * @param userHandle the user to set
-     *
-     * @hide
-     */
-    @SystemApi
-    @RequiresPermission(WRITE_SECURE_SETTINGS)
-    public void setLocationEnabledForUser(boolean enabled, UserHandle userHandle) {
-        Settings.Secure.putIntForUser(
-                mContext.getContentResolver(),
-                Settings.Secure.LOCATION_MODE,
-                enabled
-                        ? Settings.Secure.LOCATION_MODE_HIGH_ACCURACY
-                        : Settings.Secure.LOCATION_MODE_OFF,
-                userHandle.getIdentifier());
-    }
-
-    /**
-     * Returns the current enabled/disabled status of location
+     * Returns the current enabled/disabled state of location.
      *
      * @param userHandle the user to query
-     * @return true location is enabled. false if location is disabled.
+     * @return true if location is enabled and false if location is disabled.
      *
      * @hide
      */
@@ -1310,19 +1285,32 @@
     }
 
     /**
-     * Returns the current enabled/disabled status of the given provider.
+     * Enables or disables the location setting.
      *
-     * <p>If the user has enabled this provider in the Settings menu, true
-     * is returned otherwise false is returned
+     * @param enabled true to enable location and false to disable location.
+     * @param userHandle the user to set
      *
-     * <p>Callers should instead use {@link #isLocationEnabled()}
-     * unless they depend on provider-specific APIs such as
-     * {@link #requestLocationUpdates(String, long, float, LocationListener)}.
+     * @hide
+     */
+    @SystemApi
+    @RequiresPermission(WRITE_SECURE_SETTINGS)
+    public void setLocationEnabledForUser(boolean enabled, UserHandle userHandle) {
+        Settings.Secure.putIntForUser(
+                mContext.getContentResolver(),
+                Settings.Secure.LOCATION_MODE,
+                enabled
+                        ? Settings.Secure.LOCATION_MODE_ON
+                        : Settings.Secure.LOCATION_MODE_OFF,
+                userHandle.getIdentifier());
+    }
+
+    /**
+     * Returns the current enabled/disabled status of the given provider. To listen for changes, see
+     * {@link #PROVIDERS_CHANGED_ACTION}.
      *
-     * <p>
-     * Before API version {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this
-     * method would throw {@link SecurityException} if the location permissions
-     * were not sufficient to use the specified provider.
+     * Before API version {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method would throw
+     * {@link SecurityException} if the location permissions were not sufficient to use the
+     * specified provider.
      *
      * @param provider the name of the provider
      * @return true if the provider exists and is enabled
@@ -1334,19 +1322,13 @@
     }
 
     /**
-     * Returns the current enabled/disabled status of the given provider and user.
+     * Returns the current enabled/disabled status of the given provider and user. Callers should
+     * prefer {@link #isLocationEnabledForUser(UserHandle)} unless they depend on provider-specific
+     * APIs.
      *
-     * <p>If the user has enabled this provider in the Settings menu, true
-     * is returned otherwise false is returned
-     *
-     * <p>Callers should instead use {@link #isLocationEnabled()}
-     * unless they depend on provider-specific APIs such as
-     * {@link #requestLocationUpdates(String, long, float, LocationListener)}.
-     *
-     * <p>
-     * Before API version {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this
-     * method would throw {@link SecurityException} if the location permissions
-     * were not sufficient to use the specified provider.
+     * Before API version {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method would throw
+     * {@link SecurityException} if the location permissions were not sufficient to use the
+     * specified provider.
      *
      * @param provider the name of the provider
      * @param userHandle the user to query
@@ -1367,12 +1349,14 @@
     }
 
     /**
-     * Method for enabling or disabling a single location provider.
+     * Method for enabling or disabling a single location provider. This method is deprecated and
+     * functions as a best effort. It should not be relied on in any meaningful sense as providers
+     * may no longer be enabled or disabled by clients.
      *
      * @param provider the name of the provider
      * @param enabled true to enable the provider. false to disable the provider
      * @param userHandle the user to set
-     * @return true if the value was set, false on database errors
+     * @return true if the value was set, false otherwise
      *
      * @throws IllegalArgumentException if provider is null
      * @deprecated Do not manipulate providers individually, use
diff --git a/location/java/android/location/LocationRequest.java b/location/java/android/location/LocationRequest.java
index 3d0afb09..0480eab 100644
--- a/location/java/android/location/LocationRequest.java
+++ b/location/java/android/location/LocationRequest.java
@@ -683,6 +683,7 @@
                     request.setSmallestDisplacement(in.readFloat());
                     request.setHideFromAppOps(in.readInt() != 0);
                     request.setLowPowerMode(in.readInt() != 0);
+                    request.setLocationSettingsIgnored(in.readInt() != 0);
                     String provider = in.readString();
                     if (provider != null) request.setProvider(provider);
                     WorkSource workSource = in.readParcelable(null);
@@ -711,6 +712,7 @@
         parcel.writeFloat(mSmallestDisplacement);
         parcel.writeInt(mHideFromAppOps ? 1 : 0);
         parcel.writeInt(mLowPowerMode ? 1 : 0);
+        parcel.writeInt(mLocationSettingsIgnored ? 1 : 0);
         parcel.writeString(mProvider);
         parcel.writeParcelable(mWorkSource, 0);
     }
@@ -755,6 +757,9 @@
             s.append(" num=").append(mNumUpdates);
         }
         s.append(" lowPowerMode=").append(mLowPowerMode);
+        if (mLocationSettingsIgnored) {
+            s.append(" ignoreSettings");
+        }
         s.append(']');
         return s.toString();
     }
diff --git a/media/Android.bp b/media/Android.bp
index e7d5faf..c7d5ace 100644
--- a/media/Android.bp
+++ b/media/Android.bp
@@ -1,30 +1,4 @@
 java_library {
-    name: "updatable-media1",
-
-    srcs: [
-        ":media1-srcs",
-        ":framework-media-annotation-srcs",
-    ],
-
-    aidl: {
-        export_include_dirs: [
-            "apex/java",
-        ],
-
-        // TODO: find out a way to include only the necessary aidl files instead of dirs.
-        include_dirs: [
-            "frameworks/base/core/java",
-            "frameworks/base/media/java",
-        ],
-    },
-
-    installable: true,
-
-    // Make sure that the implementaion only relies on SDK or system APIs.
-    sdk_version: "system_current",
-}
-
-java_library {
     name: "updatable-media",
 
     srcs: [
@@ -57,55 +31,12 @@
 filegroup {
     name: "media-srcs-without-aidls",
     srcs : [
-        ":media1-srcs-without-aidls",
         ":mediasession2-srcs-without-aidls",
         ":mediaplayer2-srcs",
     ],
 }
 
 filegroup {
-    name: "media1-srcs",
-    srcs: [
-        "apex/java/android/media/MediaDescription.java",
-        "apex/java/android/media/MediaParceledListSlice.java",
-        "apex/java/android/media/Rating.java",
-        "apex/java/android/media/VolumeProvider.java",
-        "apex/java/android/media/browse/MediaBrowser.java",
-        "apex/java/android/media/browse/MediaBrowserUtils.java",
-        "apex/java/android/media/session/ControllerCallbackLink.java",
-        "apex/java/android/media/session/ControllerLink.java",
-        "apex/java/android/media/session/ISession.aidl",
-        "apex/java/android/media/session/ISessionCallback.aidl",
-        "apex/java/android/media/session/ISessionController.aidl",
-        "apex/java/android/media/session/ISessionControllerCallback.aidl",
-        "apex/java/android/media/session/MediaController.java",
-        "apex/java/android/media/session/MediaSessionEngine.java",
-        "apex/java/android/media/session/MediaSessionProviderService.java",
-        "apex/java/android/media/session/PlaybackState.java",
-        "apex/java/android/media/session/SessionCallbackLink.java",
-        "apex/java/android/media/session/SessionLink.java",
-        "apex/java/android/service/media/IMediaBrowserService.aidl",
-        "apex/java/android/service/media/IMediaBrowserServiceCallbacks.aidl",
-        "apex/java/android/service/media/MediaBrowserService.java",
-    ],
-}
-
-filegroup {
-    name: "media1-srcs-without-aidls",
-    srcs: [
-        ":media1-srcs",
-    ],
-    exclude_srcs: [
-        "apex/java/android/media/session/ISession.aidl",
-        "apex/java/android/media/session/ISessionCallback.aidl",
-        "apex/java/android/media/session/ISessionController.aidl",
-        "apex/java/android/media/session/ISessionControllerCallback.aidl",
-        "apex/java/android/service/media/IMediaBrowserService.aidl",
-        "apex/java/android/service/media/IMediaBrowserServiceCallbacks.aidl",
-    ],
-}
-
-filegroup {
     name: "mediasession2-srcs",
     srcs: [
         "apex/java/android/media/Controller2Link.java",
@@ -144,7 +75,6 @@
         "apex/java/android/media/UriDataSourceDesc.java",
         "apex/java/android/media/FileDataSourceDesc.java",
         "apex/java/android/media/CallbackDataSourceDesc.java",
-        "apex/java/android/media/VideoSize.java",
         "apex/java/android/media/Media2Utils.java",
         "apex/java/android/media/MediaPlayer2Utils.java",
         "apex/java/android/media/MediaPlayer2.java",
diff --git a/media/apex/java/android/media/MediaPlayer2.java b/media/apex/java/android/media/MediaPlayer2.java
index 1a1f6fb..925ca0d 100644
--- a/media/apex/java/android/media/MediaPlayer2.java
+++ b/media/apex/java/android/media/MediaPlayer2.java
@@ -21,6 +21,7 @@
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.annotation.StringDef;
+import android.annotation.TestApi;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.res.AssetFileDescriptor;
@@ -40,6 +41,7 @@
 import android.os.PowerManager;
 import android.util.Log;
 import android.util.Pair;
+import android.util.Size;
 import android.view.Surface;
 import android.view.SurfaceHolder;
 
@@ -61,7 +63,6 @@
 import java.nio.ByteOrder;
 import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedList;
@@ -299,7 +300,7 @@
     private final AtomicLong mSrcIdGenerator = new AtomicLong(0);
 
     private volatile float mVolume = 1.0f;
-    private VideoSize mVideoSize = new VideoSize(0, 0);
+    private Size mVideoSize = new Size(0, 0);
 
     private static ExecutorService sDrmThreadPool = Executors.newCachedThreadPool();
 
@@ -1526,7 +1527,7 @@
      * notification {@code EventCallback.onVideoSizeChanged} when the size
      * is available.
      */
-    public VideoSize getVideoSize() {
+    public Size getVideoSize() {
         return mVideoSize;
     }
 
@@ -2526,7 +2527,7 @@
                     final int width = msg.arg1;
                     final int height = msg.arg2;
 
-                    mVideoSize = new VideoSize(width, height);
+                    mVideoSize = new Size(width, height);
                     sendEvent(new EventNotifier() {
                         @Override
                         public void notify(EventCallback callback) {
@@ -2765,7 +2766,7 @@
          * @param size the size of the video
          */
         public void onVideoSizeChanged(
-                @NonNull MediaPlayer2 mp, @NonNull DataSourceDesc dsd, @NonNull VideoSize size) { }
+                @NonNull MediaPlayer2 mp, @NonNull DataSourceDesc dsd, @NonNull Size size) { }
 
         /**
          * Called to indicate an avaliable timed text
@@ -3667,53 +3668,66 @@
     }
 
     /**
-     * The status codes for {@link DrmEventCallback#onDrmPrepared} listener.
+     * A status code for {@link DrmEventCallback#onDrmPrepared} listener.
      * <p>
      *
      * DRM preparation has succeeded.
-     * @hide
      */
     public static final int PREPARE_DRM_STATUS_SUCCESS = 0;
 
     /**
+     * A status code for {@link DrmEventCallback#onDrmPrepared} listener.
+     * <p>
+     *
      * The device required DRM provisioning but couldn't reach the provisioning server.
-     * @hide
      */
     public static final int PREPARE_DRM_STATUS_PROVISIONING_NETWORK_ERROR = 1;
 
     /**
+     * A status code for {@link DrmEventCallback#onDrmPrepared} listener.
+     * <p>
+     *
      * The device required DRM provisioning but the provisioning server denied the request.
-     * @hide
      */
     public static final int PREPARE_DRM_STATUS_PROVISIONING_SERVER_ERROR = 2;
 
     /**
+     * A status code for {@link DrmEventCallback#onDrmPrepared} listener.
+     * <p>
+     *
      * The DRM preparation has failed .
-     * @hide
      */
     public static final int PREPARE_DRM_STATUS_PREPARATION_ERROR = 3;
 
     /**
+     * A status code for {@link DrmEventCallback#onDrmPrepared} listener.
+     * <p>
+     *
      * The crypto scheme UUID is not supported by the device.
-     * @hide
      */
     public static final int PREPARE_DRM_STATUS_UNSUPPORTED_SCHEME = 4;
 
     /**
+     * A status code for {@link DrmEventCallback#onDrmPrepared} listener.
+     * <p>
+     *
      * The hardware resources are not available, due to being in use.
-     * @hide
      */
     public static final int PREPARE_DRM_STATUS_RESOURCE_BUSY = 5;
 
     /**
+     * A status code for {@link DrmEventCallback#onDrmPrepared} listener.
+     * <p>
+     *
      * Restoring persisted offline keys failed.
-     * @hide
      */
     public static final int PREPARE_DRM_STATUS_RESTORE_ERROR = 6;
 
     /**
+     * A status code for {@link DrmEventCallback#onDrmPrepared} listener.
+     * <p>
+     *
      * Error during key request/response exchange with license server.
-     * @hide
      */
     public static final int PREPARE_DRM_STATUS_KEY_EXCHANGE_ERROR = 7;
 
@@ -3758,6 +3772,7 @@
      * @throws IllegalStateException if called before being prepared
      * @hide
      */
+    @TestApi
     public DrmInfo getDrmInfo(@NonNull DataSourceDesc dsd) {
         final SourceInfo sourceInfo = getSourceInfo(dsd);
         if (sourceInfo != null) {
@@ -3814,6 +3829,7 @@
      * @hide
      */
     // This is an asynchronous call.
+    @TestApi
     public Object prepareDrm(@NonNull DataSourceDesc dsd, @NonNull UUID uuid) {
         return addTask(newPrepareDrmTask(dsd, uuid));
     }
@@ -3905,6 +3921,7 @@
      * @hide
      */
     // This is a synchronous call.
+    @TestApi
     public void releaseDrm(@NonNull DataSourceDesc dsd)
             throws NoDrmSchemeException {
         final SourceInfo sourceInfo = getSourceInfo(dsd);
@@ -3955,6 +3972,7 @@
      * @throws NoDrmSchemeException if there is no active DRM session
      * @hide
      */
+    @TestApi
     public MediaDrm.KeyRequest getDrmKeyRequest(
             @NonNull DataSourceDesc dsd,
             @Nullable byte[] keySetId, @Nullable byte[] initData,
@@ -3997,6 +4015,7 @@
      * @hide
      */
     // This is a synchronous call.
+    @TestApi
     public byte[] provideDrmKeyResponse(
             @NonNull DataSourceDesc dsd,
             @Nullable byte[] keySetId, @NonNull byte[] response)
@@ -4023,6 +4042,7 @@
      * @hide
      */
     // This is a synchronous call.
+    @TestApi
     public void restoreDrmKeys(
             @NonNull DataSourceDesc dsd,
             @NonNull byte[] keySetId)
@@ -4050,6 +4070,7 @@
      * @throws NoDrmSchemeException if there is no active DRM session
      * @hide
      */
+    @TestApi
     public String getDrmPropertyString(
             @NonNull DataSourceDesc dsd,
             @NonNull @MediaDrmStringProperty String propertyName)
@@ -4078,6 +4099,7 @@
      * @hide
      */
     // This is a synchronous call.
+    @TestApi
     public void setDrmPropertyString(
             @NonNull DataSourceDesc dsd,
             @NonNull @MediaDrmStringProperty String propertyName, @NonNull String value)
diff --git a/media/apex/java/android/media/VideoSize.java b/media/apex/java/android/media/VideoSize.java
deleted file mode 100644
index 19631e0..0000000
--- a/media/apex/java/android/media/VideoSize.java
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * Copyright 2018 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package android.media;
-
-/**
- * Immutable class for describing width and height dimensions.
- */
-public final class VideoSize {
-    /**
-     * Create a new immutable VideoSize instance.
-     *
-     * @param width The width of the video size
-     * @param height The height of the video size
-     */
-    VideoSize(int width, int height) {
-        mWidth = width;
-        mHeight = height;
-    }
-
-    /**
-     * Get the width of the video size
-     * @return width
-     */
-    public int getWidth() {
-        return mWidth;
-    }
-
-    /**
-     * Get the height of the video size
-     * @return height
-     */
-    public int getHeight() {
-        return mHeight;
-    }
-
-    /**
-     * Check if this video size is equal to another video size.
-     * <p>
-     * Two video sizes are equal if and only if both their widths and heights are
-     * equal.
-     * </p>
-     * <p>
-     * A video size object is never equal to any other type of object.
-     * </p>
-     *
-     * @return {@code true} if the objects were equal, {@code false} otherwise
-     */
-    @Override
-    public boolean equals(final Object obj) {
-        if (obj == null) {
-            return false;
-        }
-        if (this == obj) {
-            return true;
-        }
-        if (obj instanceof VideoSize) {
-            VideoSize other = (VideoSize) obj;
-            return mWidth == other.mWidth && mHeight == other.mHeight;
-        }
-        return false;
-    }
-
-    /**
-     * Return the video size represented as a string with the format {@code "WxH"}
-     *
-     * @return string representation of the video size
-     */
-    @Override
-    public String toString() {
-        return mWidth + "x" + mHeight;
-    }
-
-    private final int mWidth;
-    private final int mHeight;
-}
diff --git a/media/java/android/media/MediaCodec.java b/media/java/android/media/MediaCodec.java
index f756658..0c3d625 100644
--- a/media/java/android/media/MediaCodec.java
+++ b/media/java/android/media/MediaCodec.java
@@ -3314,7 +3314,7 @@
      */
     public void setAudioPresentation(@NonNull AudioPresentation presentation) {
         if (presentation == null) {
-            throw new IllegalArgumentException("audio presentation is null");
+            throw new NullPointerException("audio presentation is null");
         }
         native_setAudioPresentation(presentation.getPresentationId(), presentation.getProgramId());
     }
diff --git a/media/apex/java/android/media/MediaDescription.aidl b/media/java/android/media/MediaDescription.aidl
similarity index 100%
rename from media/apex/java/android/media/MediaDescription.aidl
rename to media/java/android/media/MediaDescription.aidl
diff --git a/media/apex/java/android/media/MediaDescription.java b/media/java/android/media/MediaDescription.java
similarity index 100%
rename from media/apex/java/android/media/MediaDescription.java
rename to media/java/android/media/MediaDescription.java
diff --git a/media/java/android/media/MediaDrm.java b/media/java/android/media/MediaDrm.java
index 2d2c4a8..9258b85 100644
--- a/media/java/android/media/MediaDrm.java
+++ b/media/java/android/media/MediaDrm.java
@@ -1186,22 +1186,22 @@
     public static final int OFFLINE_LICENSE_STATE_UNKNOWN = 0;
 
     /**
-     * Offline license state is usable, the keys may be used for decryption.
+     * Offline license is usable, the keys may be used for decryption.
      */
-    public static final int OFFLINE_LICENSE_USABLE = 1;
+    public static final int OFFLINE_LICENSE_STATE_USABLE = 1;
 
     /**
-     * Offline license state is inactive, the keys have been marked for
-     * release using {@link #getKeyRequest} with KEY_TYPE_RELEASE but the
-     * key response has not been received.
+     * Offline license is released, the keys have been marked for
+     * release using {@link #getKeyRequest} with KEY_TYPE_RELEASE but
+     * the key response has not been received.
      */
-    public static final int OFFLINE_LICENSE_INACTIVE = 2;
+    public static final int OFFLINE_LICENSE_STATE_RELEASED = 2;
 
     /** @hide */
     @IntDef({
         OFFLINE_LICENSE_STATE_UNKNOWN,
-        OFFLINE_LICENSE_USABLE,
-        OFFLINE_LICENSE_INACTIVE,
+        OFFLINE_LICENSE_STATE_USABLE,
+        OFFLINE_LICENSE_STATE_RELEASED,
     })
     @Retention(RetentionPolicy.SOURCE)
     public @interface OfflineLicenseState {}
diff --git a/media/apex/java/android/media/MediaParceledListSlice.aidl b/media/java/android/media/MediaParceledListSlice.aidl
similarity index 100%
rename from media/apex/java/android/media/MediaParceledListSlice.aidl
rename to media/java/android/media/MediaParceledListSlice.aidl
diff --git a/media/apex/java/android/media/MediaParceledListSlice.java b/media/java/android/media/MediaParceledListSlice.java
similarity index 100%
rename from media/apex/java/android/media/MediaParceledListSlice.java
rename to media/java/android/media/MediaParceledListSlice.java
diff --git a/media/apex/java/android/media/Rating.aidl b/media/java/android/media/Rating.aidl
similarity index 100%
rename from media/apex/java/android/media/Rating.aidl
rename to media/java/android/media/Rating.aidl
diff --git a/media/apex/java/android/media/Rating.java b/media/java/android/media/Rating.java
similarity index 100%
rename from media/apex/java/android/media/Rating.java
rename to media/java/android/media/Rating.java
diff --git a/media/apex/java/android/media/VolumeProvider.java b/media/java/android/media/VolumeProvider.java
similarity index 98%
rename from media/apex/java/android/media/VolumeProvider.java
rename to media/java/android/media/VolumeProvider.java
index 49202ee..8f68cbd 100644
--- a/media/apex/java/android/media/VolumeProvider.java
+++ b/media/java/android/media/VolumeProvider.java
@@ -16,7 +16,6 @@
 package android.media;
 
 import android.annotation.IntDef;
-import android.annotation.SystemApi;
 import android.media.session.MediaSession;
 
 import java.lang.annotation.Retention;
@@ -148,7 +147,6 @@
      * Sets a callback to receive volume changes.
      * @hide
      */
-    @SystemApi
     public void setCallback(Callback callback) {
         mCallback = callback;
     }
@@ -157,7 +155,6 @@
      * Listens for changes to the volume.
      * @hide
      */
-    @SystemApi
     public abstract static class Callback {
         /**
          * Called when volume changed.
diff --git a/media/apex/java/android/media/browse/MediaBrowser.aidl b/media/java/android/media/browse/MediaBrowser.aidl
similarity index 100%
rename from media/apex/java/android/media/browse/MediaBrowser.aidl
rename to media/java/android/media/browse/MediaBrowser.aidl
diff --git a/media/apex/java/android/media/browse/MediaBrowser.java b/media/java/android/media/browse/MediaBrowser.java
similarity index 100%
rename from media/apex/java/android/media/browse/MediaBrowser.java
rename to media/java/android/media/browse/MediaBrowser.java
diff --git a/media/apex/java/android/media/browse/MediaBrowserUtils.java b/media/java/android/media/browse/MediaBrowserUtils.java
similarity index 100%
rename from media/apex/java/android/media/browse/MediaBrowserUtils.java
rename to media/java/android/media/browse/MediaBrowserUtils.java
diff --git a/media/apex/java/android/media/session/ControllerCallbackLink.aidl b/media/java/android/media/session/ControllerCallbackLink.aidl
similarity index 100%
rename from media/apex/java/android/media/session/ControllerCallbackLink.aidl
rename to media/java/android/media/session/ControllerCallbackLink.aidl
diff --git a/media/apex/java/android/media/session/ControllerCallbackLink.java b/media/java/android/media/session/ControllerCallbackLink.java
similarity index 99%
rename from media/apex/java/android/media/session/ControllerCallbackLink.java
rename to media/java/android/media/session/ControllerCallbackLink.java
index adc14a5..428be0d 100644
--- a/media/apex/java/android/media/session/ControllerCallbackLink.java
+++ b/media/java/android/media/session/ControllerCallbackLink.java
@@ -20,7 +20,6 @@
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.annotation.RequiresPermission;
-import android.annotation.SystemApi;
 import android.content.Context;
 import android.content.pm.PackageManager;
 import android.media.MediaMetadata;
@@ -41,7 +40,6 @@
  * Handles incoming commands to {@link MediaController.Callback}.
  * @hide
  */
-@SystemApi
 public final class ControllerCallbackLink implements Parcelable {
     final Context mContext;
     final CallbackStub mCallbackStub;
diff --git a/media/apex/java/android/media/session/ControllerLink.aidl b/media/java/android/media/session/ControllerLink.aidl
similarity index 100%
rename from media/apex/java/android/media/session/ControllerLink.aidl
rename to media/java/android/media/session/ControllerLink.aidl
diff --git a/media/apex/java/android/media/session/ControllerLink.java b/media/java/android/media/session/ControllerLink.java
similarity index 99%
rename from media/apex/java/android/media/session/ControllerLink.java
rename to media/java/android/media/session/ControllerLink.java
index f60ec00..64d283f 100644
--- a/media/apex/java/android/media/session/ControllerLink.java
+++ b/media/java/android/media/session/ControllerLink.java
@@ -18,7 +18,6 @@
 
 import android.annotation.NonNull;
 import android.annotation.Nullable;
-import android.annotation.SystemApi;
 import android.app.PendingIntent;
 import android.media.MediaMetadata;
 import android.media.MediaParceledListSlice;
@@ -40,7 +39,6 @@
  * Handles incoming commands from {@link MediaController}.
  * @hide
  */
-@SystemApi
 public final class ControllerLink implements Parcelable {
     public static final Parcelable.Creator<ControllerLink> CREATOR =
             new Parcelable.Creator<ControllerLink>() {
diff --git a/media/apex/java/android/media/session/ISession.aidl b/media/java/android/media/session/ISession.aidl
similarity index 100%
rename from media/apex/java/android/media/session/ISession.aidl
rename to media/java/android/media/session/ISession.aidl
diff --git a/media/apex/java/android/media/session/ISessionCallback.aidl b/media/java/android/media/session/ISessionCallback.aidl
similarity index 100%
rename from media/apex/java/android/media/session/ISessionCallback.aidl
rename to media/java/android/media/session/ISessionCallback.aidl
diff --git a/media/apex/java/android/media/session/ISessionController.aidl b/media/java/android/media/session/ISessionController.aidl
similarity index 100%
rename from media/apex/java/android/media/session/ISessionController.aidl
rename to media/java/android/media/session/ISessionController.aidl
diff --git a/media/apex/java/android/media/session/ISessionControllerCallback.aidl b/media/java/android/media/session/ISessionControllerCallback.aidl
similarity index 100%
rename from media/apex/java/android/media/session/ISessionControllerCallback.aidl
rename to media/java/android/media/session/ISessionControllerCallback.aidl
diff --git a/media/apex/java/android/media/session/MediaController.aidl b/media/java/android/media/session/MediaController.aidl
similarity index 100%
rename from media/apex/java/android/media/session/MediaController.aidl
rename to media/java/android/media/session/MediaController.aidl
diff --git a/media/apex/java/android/media/session/MediaController.java b/media/java/android/media/session/MediaController.java
similarity index 99%
rename from media/apex/java/android/media/session/MediaController.java
rename to media/java/android/media/session/MediaController.java
index 1333ab0..6e2c8c5 100644
--- a/media/apex/java/android/media/session/MediaController.java
+++ b/media/java/android/media/session/MediaController.java
@@ -413,6 +413,7 @@
      * Get the session's tag for debugging purposes.
      *
      * @return The session's tag.
+     * @hide
      */
     public String getTag() {
         if (mTag == null) {
diff --git a/media/java/android/media/session/MediaSession.java b/media/java/android/media/session/MediaSession.java
index ca3346c..1b9ebda 100644
--- a/media/java/android/media/session/MediaSession.java
+++ b/media/java/android/media/session/MediaSession.java
@@ -19,7 +19,6 @@
 import android.annotation.IntDef;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
-import android.annotation.SystemApi;
 import android.annotation.UnsupportedAppUsage;
 import android.app.Activity;
 import android.app.PendingIntent;
@@ -142,10 +141,9 @@
         MediaSessionManager manager = (MediaSessionManager) context
                 .getSystemService(Context.MEDIA_SESSION_SERVICE);
         try {
-            MediaSessionEngine.CallbackStub cbStub = new MediaSessionEngine.CallbackStub();
-            SessionCallbackLink cbLink = new SessionCallbackLink(context, cbStub);
+            SessionCallbackLink cbLink = new SessionCallbackLink(context);
             SessionLink sessionLink = manager.createSession(cbLink, tag);
-            mImpl = new MediaSessionEngine(context, sessionLink, cbLink, cbStub);
+            mImpl = new MediaSessionEngine(context, sessionLink, cbLink);
             mMaxBitmapSize = context.getResources().getDimensionPixelSize(
                     android.R.dimen.config_mediaMetadataBitmapMaxSize);
         } catch (RuntimeException e) {
@@ -488,7 +486,6 @@
          * Gets the controller link in this token.
          * @hide
          */
-        @SystemApi
         public ControllerLink getControllerLink() {
             return mControllerLink;
         }
@@ -697,7 +694,6 @@
         /**
          * @hide
          */
-        @SystemApi
         public void onSetMediaButtonEventDelegate(
                 @NonNull MediaSessionEngine.MediaButtonEventDelegate delegate) {
             mMediaButtonEventDelegate = delegate;
diff --git a/media/apex/java/android/media/session/MediaSessionEngine.java b/media/java/android/media/session/MediaSessionEngine.java
similarity index 78%
rename from media/apex/java/android/media/session/MediaSessionEngine.java
rename to media/java/android/media/session/MediaSessionEngine.java
index 31714e1..e19bdbc 100644
--- a/media/apex/java/android/media/session/MediaSessionEngine.java
+++ b/media/java/android/media/session/MediaSessionEngine.java
@@ -18,8 +18,6 @@
 
 import android.annotation.NonNull;
 import android.annotation.Nullable;
-import android.annotation.SystemApi;
-import android.annotation.UnsupportedAppUsage;
 import android.app.Activity;
 import android.app.PendingIntent;
 import android.content.Context;
@@ -44,14 +42,12 @@
 import android.view.KeyEvent;
 import android.view.ViewConfiguration;
 
-import java.lang.ref.WeakReference;
 import java.util.List;
 import java.util.Objects;
 
 /**
  * @hide
  */
-@SystemApi
 public final class MediaSessionEngine implements AutoCloseable {
     private static final String TAG = "MediaSession";
 
@@ -60,10 +56,7 @@
     private final MediaSession.Token mSessionToken;
     private final MediaController mController;
     private final SessionLink mSessionLink;
-    private final SessionCallbackLink mCbLink;
 
-    // Do not change the name of mCallbackWrapper. Support lib accesses this by using reflection.
-    @UnsupportedAppUsage
     private CallbackMessageHandler mCallbackHandler;
     private VolumeProvider mVolumeProvider;
     private PlaybackState mPlaybackState;
@@ -78,14 +71,12 @@
      *
      * @param context The context to use to create the session.
      * @param sessionLink A session link for the binder of MediaSessionRecord
-     * @param cbStub A callback link that handles incoming command to {@link MediaSession.Callback}.
      */
     public MediaSessionEngine(@NonNull Context context, @NonNull SessionLink sessionLink,
-            @NonNull SessionCallbackLink cbLink, @NonNull CallbackStub cbStub) {
+            @NonNull SessionCallbackLink cbLink) {
         mSessionLink = sessionLink;
-        mCbLink = cbLink;
 
-        cbStub.setSessionImpl(this);
+        cbLink.setSessionEngine(this);
         mSessionToken = new MediaSession.Token(mSessionLink.getController());
         mController = new MediaController(context, mSessionToken);
     }
@@ -479,97 +470,97 @@
         }
     }
 
-    private void dispatchPrepare(RemoteUserInfo caller) {
+    void dispatchPrepare(RemoteUserInfo caller) {
         postToCallback(caller, CallbackMessageHandler.MSG_PREPARE, null, null);
     }
 
-    private void dispatchPrepareFromMediaId(RemoteUserInfo caller, String mediaId, Bundle extras) {
+    void dispatchPrepareFromMediaId(RemoteUserInfo caller, String mediaId, Bundle extras) {
         postToCallback(caller, CallbackMessageHandler.MSG_PREPARE_MEDIA_ID, mediaId, extras);
     }
 
-    private void dispatchPrepareFromSearch(RemoteUserInfo caller, String query, Bundle extras) {
+    void dispatchPrepareFromSearch(RemoteUserInfo caller, String query, Bundle extras) {
         postToCallback(caller, CallbackMessageHandler.MSG_PREPARE_SEARCH, query, extras);
     }
 
-    private void dispatchPrepareFromUri(RemoteUserInfo caller, Uri uri, Bundle extras) {
+    void dispatchPrepareFromUri(RemoteUserInfo caller, Uri uri, Bundle extras) {
         postToCallback(caller, CallbackMessageHandler.MSG_PREPARE_URI, uri, extras);
     }
 
-    private void dispatchPlay(RemoteUserInfo caller) {
+    void dispatchPlay(RemoteUserInfo caller) {
         postToCallback(caller, CallbackMessageHandler.MSG_PLAY, null, null);
     }
 
-    private void dispatchPlayFromMediaId(RemoteUserInfo caller, String mediaId, Bundle extras) {
+    void dispatchPlayFromMediaId(RemoteUserInfo caller, String mediaId, Bundle extras) {
         postToCallback(caller, CallbackMessageHandler.MSG_PLAY_MEDIA_ID, mediaId, extras);
     }
 
-    private void dispatchPlayFromSearch(RemoteUserInfo caller, String query, Bundle extras) {
+    void dispatchPlayFromSearch(RemoteUserInfo caller, String query, Bundle extras) {
         postToCallback(caller, CallbackMessageHandler.MSG_PLAY_SEARCH, query, extras);
     }
 
-    private void dispatchPlayFromUri(RemoteUserInfo caller, Uri uri, Bundle extras) {
+    void dispatchPlayFromUri(RemoteUserInfo caller, Uri uri, Bundle extras) {
         postToCallback(caller, CallbackMessageHandler.MSG_PLAY_URI, uri, extras);
     }
 
-    private void dispatchSkipToItem(RemoteUserInfo caller, long id) {
+    void dispatchSkipToItem(RemoteUserInfo caller, long id) {
         postToCallback(caller, CallbackMessageHandler.MSG_SKIP_TO_ITEM, id, null);
     }
 
-    private void dispatchPause(RemoteUserInfo caller) {
+    void dispatchPause(RemoteUserInfo caller) {
         postToCallback(caller, CallbackMessageHandler.MSG_PAUSE, null, null);
     }
 
-    private void dispatchStop(RemoteUserInfo caller) {
+    void dispatchStop(RemoteUserInfo caller) {
         postToCallback(caller, CallbackMessageHandler.MSG_STOP, null, null);
     }
 
-    private void dispatchNext(RemoteUserInfo caller) {
+    void dispatchNext(RemoteUserInfo caller) {
         postToCallback(caller, CallbackMessageHandler.MSG_NEXT, null, null);
     }
 
-    private void dispatchPrevious(RemoteUserInfo caller) {
+    void dispatchPrevious(RemoteUserInfo caller) {
         postToCallback(caller, CallbackMessageHandler.MSG_PREVIOUS, null, null);
     }
 
-    private void dispatchFastForward(RemoteUserInfo caller) {
+    void dispatchFastForward(RemoteUserInfo caller) {
         postToCallback(caller, CallbackMessageHandler.MSG_FAST_FORWARD, null, null);
     }
 
-    private void dispatchRewind(RemoteUserInfo caller) {
+    void dispatchRewind(RemoteUserInfo caller) {
         postToCallback(caller, CallbackMessageHandler.MSG_REWIND, null, null);
     }
 
-    private void dispatchSeekTo(RemoteUserInfo caller, long pos) {
+    void dispatchSeekTo(RemoteUserInfo caller, long pos) {
         postToCallback(caller, CallbackMessageHandler.MSG_SEEK_TO, pos, null);
     }
 
-    private void dispatchRate(RemoteUserInfo caller, Rating rating) {
+    void dispatchRate(RemoteUserInfo caller, Rating rating) {
         postToCallback(caller, CallbackMessageHandler.MSG_RATE, rating, null);
     }
 
-    private void dispatchCustomAction(RemoteUserInfo caller, String action, Bundle args) {
+    void dispatchCustomAction(RemoteUserInfo caller, String action, Bundle args) {
         postToCallback(caller, CallbackMessageHandler.MSG_CUSTOM_ACTION, action, args);
     }
 
-    private void dispatchMediaButton(RemoteUserInfo caller, Intent mediaButtonIntent) {
+    void dispatchMediaButton(RemoteUserInfo caller, Intent mediaButtonIntent) {
         postToCallback(caller, CallbackMessageHandler.MSG_MEDIA_BUTTON, mediaButtonIntent, null);
     }
 
-    private void dispatchMediaButtonDelayed(RemoteUserInfo info, Intent mediaButtonIntent,
+    void dispatchMediaButtonDelayed(RemoteUserInfo info, Intent mediaButtonIntent,
             long delay) {
         postToCallbackDelayed(info, CallbackMessageHandler.MSG_PLAY_PAUSE_KEY_DOUBLE_TAP_TIMEOUT,
                 mediaButtonIntent, null, delay);
     }
 
-    private void dispatchAdjustVolume(RemoteUserInfo caller, int direction) {
+    void dispatchAdjustVolume(RemoteUserInfo caller, int direction) {
         postToCallback(caller, CallbackMessageHandler.MSG_ADJUST_VOLUME, direction, null);
     }
 
-    private void dispatchSetVolumeTo(RemoteUserInfo caller, int volume) {
+    void dispatchSetVolumeTo(RemoteUserInfo caller, int volume) {
         postToCallback(caller, CallbackMessageHandler.MSG_SET_VOLUME, volume, null);
     }
 
-    private void dispatchCommand(RemoteUserInfo caller, String command, Bundle args,
+    void dispatchCommand(RemoteUserInfo caller, String command, Bundle args,
             ResultReceiver resultCb) {
         Command cmd = new Command(command, args, resultCb);
         postToCallback(caller, CallbackMessageHandler.MSG_COMMAND, cmd, null);
@@ -979,259 +970,7 @@
         }
     }
 
-    /**
-     * @hide
-     */
-    @SystemApi
-    public static final class CallbackStub extends SessionCallbackLink.CallbackStub {
-        private WeakReference<MediaSessionEngine> mSessionImpl;
-
-        private static RemoteUserInfo createRemoteUserInfo(String packageName, int pid, int uid) {
-            return new RemoteUserInfo(packageName, pid, uid);
-        }
-
-        public CallbackStub() {
-        }
-
-        @Override
-        public void onCommand(String packageName, int pid, int uid,
-                ControllerCallbackLink caller, String command, Bundle args, ResultReceiver cb) {
-            MediaSessionEngine sessionImpl = mSessionImpl.get();
-            if (sessionImpl != null) {
-                sessionImpl.dispatchCommand(createRemoteUserInfo(packageName, pid, uid),
-                        command, args, cb);
-            }
-        }
-
-        @Override
-        public void onMediaButton(String packageName, int pid, int uid, Intent mediaButtonIntent,
-                int sequenceNumber, ResultReceiver cb) {
-            MediaSessionEngine sessionImpl = mSessionImpl.get();
-            try {
-                if (sessionImpl != null) {
-                    sessionImpl.dispatchMediaButton(
-                            createRemoteUserInfo(packageName, pid, uid), mediaButtonIntent);
-                }
-            } finally {
-                if (cb != null) {
-                    cb.send(sequenceNumber, null);
-                }
-            }
-        }
-
-        @Override
-        public void onMediaButtonFromController(String packageName, int pid, int uid,
-                ControllerCallbackLink caller, Intent mediaButtonIntent) {
-            MediaSessionEngine sessionImpl = mSessionImpl.get();
-            if (sessionImpl != null) {
-                sessionImpl.dispatchMediaButton(createRemoteUserInfo(packageName, pid, uid),
-                        mediaButtonIntent);
-            }
-        }
-
-        @Override
-        public void onPrepare(String packageName, int pid, int uid,
-                ControllerCallbackLink caller) {
-            MediaSessionEngine sessionImpl = mSessionImpl.get();
-            if (sessionImpl != null) {
-                sessionImpl.dispatchPrepare(createRemoteUserInfo(packageName, pid, uid));
-            }
-        }
-
-        @Override
-        public void onPrepareFromMediaId(String packageName, int pid, int uid,
-                ControllerCallbackLink caller, String mediaId,
-                Bundle extras) {
-            MediaSessionEngine sessionImpl = mSessionImpl.get();
-            if (sessionImpl != null) {
-                sessionImpl.dispatchPrepareFromMediaId(
-                        createRemoteUserInfo(packageName, pid, uid), mediaId, extras);
-            }
-        }
-
-        @Override
-        public void onPrepareFromSearch(String packageName, int pid, int uid,
-                ControllerCallbackLink caller, String query,
-                Bundle extras) {
-            MediaSessionEngine sessionImpl = mSessionImpl.get();
-            if (sessionImpl != null) {
-                sessionImpl.dispatchPrepareFromSearch(
-                        createRemoteUserInfo(packageName, pid, uid), query, extras);
-            }
-        }
-
-        @Override
-        public void onPrepareFromUri(String packageName, int pid, int uid,
-                ControllerCallbackLink caller, Uri uri, Bundle extras) {
-            MediaSessionEngine sessionImpl = mSessionImpl.get();
-            if (sessionImpl != null) {
-                sessionImpl.dispatchPrepareFromUri(
-                        createRemoteUserInfo(packageName, pid, uid), uri, extras);
-            }
-        }
-
-        @Override
-        public void onPlay(String packageName, int pid, int uid,
-                ControllerCallbackLink caller) {
-            MediaSessionEngine sessionImpl = mSessionImpl.get();
-            if (sessionImpl != null) {
-                sessionImpl.dispatchPlay(createRemoteUserInfo(packageName, pid, uid));
-            }
-        }
-
-        @Override
-        public void onPlayFromMediaId(String packageName, int pid, int uid,
-                ControllerCallbackLink caller, String mediaId,
-                Bundle extras) {
-            MediaSessionEngine sessionImpl = mSessionImpl.get();
-            if (sessionImpl != null) {
-                sessionImpl.dispatchPlayFromMediaId(
-                        createRemoteUserInfo(packageName, pid, uid), mediaId, extras);
-            }
-        }
-
-        @Override
-        public void onPlayFromSearch(String packageName, int pid, int uid,
-                ControllerCallbackLink caller, String query,
-                Bundle extras) {
-            MediaSessionEngine sessionImpl = mSessionImpl.get();
-            if (sessionImpl != null) {
-                sessionImpl.dispatchPlayFromSearch(
-                        createRemoteUserInfo(packageName, pid, uid), query, extras);
-            }
-        }
-
-        @Override
-        public void onPlayFromUri(String packageName, int pid, int uid,
-                ControllerCallbackLink caller, Uri uri, Bundle extras) {
-            MediaSessionEngine sessionImpl = mSessionImpl.get();
-            if (sessionImpl != null) {
-                sessionImpl.dispatchPlayFromUri(
-                        createRemoteUserInfo(packageName, pid, uid), uri, extras);
-            }
-        }
-
-        @Override
-        public void onSkipToTrack(String packageName, int pid, int uid,
-                ControllerCallbackLink caller, long id) {
-            MediaSessionEngine sessionImpl = mSessionImpl.get();
-            if (sessionImpl != null) {
-                sessionImpl.dispatchSkipToItem(
-                        createRemoteUserInfo(packageName, pid, uid), id);
-            }
-        }
-
-        @Override
-        public void onPause(String packageName, int pid, int uid,
-                ControllerCallbackLink caller) {
-            MediaSessionEngine sessionImpl = mSessionImpl.get();
-            if (sessionImpl != null) {
-                sessionImpl.dispatchPause(createRemoteUserInfo(packageName, pid, uid));
-            }
-        }
-
-        @Override
-        public void onStop(String packageName, int pid, int uid,
-                ControllerCallbackLink caller) {
-            MediaSessionEngine sessionImpl = mSessionImpl.get();
-            if (sessionImpl != null) {
-                sessionImpl.dispatchStop(createRemoteUserInfo(packageName, pid, uid));
-            }
-        }
-
-        @Override
-        public void onNext(String packageName, int pid, int uid,
-                ControllerCallbackLink caller) {
-            MediaSessionEngine sessionImpl = mSessionImpl.get();
-            if (sessionImpl != null) {
-                sessionImpl.dispatchNext(createRemoteUserInfo(packageName, pid, uid));
-            }
-        }
-
-        @Override
-        public void onPrevious(String packageName, int pid, int uid,
-                ControllerCallbackLink caller) {
-            MediaSessionEngine sessionImpl = mSessionImpl.get();
-            if (sessionImpl != null) {
-                sessionImpl.dispatchPrevious(createRemoteUserInfo(packageName, pid, uid));
-            }
-        }
-
-        @Override
-        public void onFastForward(String packageName, int pid, int uid,
-                ControllerCallbackLink caller) {
-            MediaSessionEngine sessionImpl = mSessionImpl.get();
-            if (sessionImpl != null) {
-                sessionImpl.dispatchFastForward(
-                        createRemoteUserInfo(packageName, pid, uid));
-            }
-        }
-
-        @Override
-        public void onRewind(String packageName, int pid, int uid,
-                ControllerCallbackLink caller) {
-            MediaSessionEngine sessionImpl = mSessionImpl.get();
-            if (sessionImpl != null) {
-                sessionImpl.dispatchRewind(createRemoteUserInfo(packageName, pid, uid));
-            }
-        }
-
-        @Override
-        public void onSeekTo(String packageName, int pid, int uid,
-                ControllerCallbackLink caller, long pos) {
-            MediaSessionEngine sessionImpl = mSessionImpl.get();
-            if (sessionImpl != null) {
-                sessionImpl.dispatchSeekTo(
-                        createRemoteUserInfo(packageName, pid, uid), pos);
-            }
-        }
-
-        @Override
-        public void onRate(String packageName, int pid, int uid, ControllerCallbackLink caller,
-                Rating rating) {
-            MediaSessionEngine sessionImpl = mSessionImpl.get();
-            if (sessionImpl != null) {
-                sessionImpl.dispatchRate(
-                        createRemoteUserInfo(packageName, pid, uid), rating);
-            }
-        }
-
-        @Override
-        public void onCustomAction(String packageName, int pid, int uid,
-                ControllerCallbackLink caller, String action, Bundle args) {
-            MediaSessionEngine sessionImpl = mSessionImpl.get();
-            if (sessionImpl != null) {
-                sessionImpl.dispatchCustomAction(
-                        createRemoteUserInfo(packageName, pid, uid), action, args);
-            }
-        }
-
-        @Override
-        public void onAdjustVolume(String packageName, int pid, int uid,
-                ControllerCallbackLink caller, int direction) {
-            MediaSessionEngine sessionImpl = mSessionImpl.get();
-            if (sessionImpl != null) {
-                sessionImpl.dispatchAdjustVolume(
-                        createRemoteUserInfo(packageName, pid, uid), direction);
-            }
-        }
-
-        @Override
-        public void onSetVolumeTo(String packageName, int pid, int uid,
-                ControllerCallbackLink caller, int value) {
-            MediaSessionEngine sessionImpl = mSessionImpl.get();
-            if (sessionImpl != null) {
-                sessionImpl.dispatchSetVolumeTo(
-                        createRemoteUserInfo(packageName, pid, uid), value);
-            }
-        }
-
-        void setSessionImpl(MediaSessionEngine sessionImpl) {
-            mSessionImpl = new WeakReference<>(sessionImpl);
-        }
-    }
-
-    /**
+     /**
      * A single item that is part of the play queue. It contains a description
      * of the item and its id in the queue.
      */
diff --git a/media/apex/java/android/media/session/MediaSessionProviderService.java b/media/java/android/media/session/MediaSessionProviderService.java
similarity index 100%
rename from media/apex/java/android/media/session/MediaSessionProviderService.java
rename to media/java/android/media/session/MediaSessionProviderService.java
diff --git a/media/apex/java/android/media/session/PlaybackState.aidl b/media/java/android/media/session/PlaybackState.aidl
similarity index 100%
rename from media/apex/java/android/media/session/PlaybackState.aidl
rename to media/java/android/media/session/PlaybackState.aidl
diff --git a/media/apex/java/android/media/session/PlaybackState.java b/media/java/android/media/session/PlaybackState.java
similarity index 100%
rename from media/apex/java/android/media/session/PlaybackState.java
rename to media/java/android/media/session/PlaybackState.java
diff --git a/media/apex/java/android/media/session/SessionCallbackLink.aidl b/media/java/android/media/session/SessionCallbackLink.aidl
similarity index 100%
rename from media/apex/java/android/media/session/SessionCallbackLink.aidl
rename to media/java/android/media/session/SessionCallbackLink.aidl
diff --git a/media/apex/java/android/media/session/SessionCallbackLink.java b/media/java/android/media/session/SessionCallbackLink.java
similarity index 79%
rename from media/apex/java/android/media/session/SessionCallbackLink.java
rename to media/java/android/media/session/SessionCallbackLink.java
index 3bcb65c..f59a69d 100644
--- a/media/apex/java/android/media/session/SessionCallbackLink.java
+++ b/media/java/android/media/session/SessionCallbackLink.java
@@ -20,11 +20,11 @@
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.annotation.RequiresPermission;
-import android.annotation.SystemApi;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.media.Rating;
+import android.media.session.MediaSessionManager.RemoteUserInfo;
 import android.net.Uri;
 import android.os.Binder;
 import android.os.Bundle;
@@ -35,23 +35,23 @@
 import android.os.RemoteException;
 import android.os.ResultReceiver;
 
+import java.lang.ref.WeakReference;
+
 /**
  * Handles incoming commands to {@link MediaSession.Callback}.
  * @hide
  */
-@SystemApi
 public final class SessionCallbackLink implements Parcelable {
     final Context mContext;
-    final CallbackStub mCallbackStub;
     final ISessionCallback mISessionCallback;
 
     /**
      * Constructor for stub (Callee)
+     * @hide
      */
-    SessionCallbackLink(@NonNull Context context, @NonNull CallbackStub callbackStub) {
+    public SessionCallbackLink(@NonNull Context context) {
         mContext = context;
-        mCallbackStub = callbackStub;
-        mISessionCallback = new CallbackStubProxy();
+        mISessionCallback = new CallbackStub();
     }
 
     /**
@@ -59,11 +59,19 @@
      */
     public SessionCallbackLink(IBinder binder) {
         mContext = null;
-        mCallbackStub = null;
         mISessionCallback = ISessionCallback.Stub.asInterface(binder);
     }
 
     /**
+     * Set {@link MediaSessionEngine} which will be used by {@link CallbackStub}.
+     */
+    void setSessionEngine(@Nullable MediaSessionEngine sessionImpl) {
+        if (mISessionCallback instanceof CallbackStub) {
+            ((CallbackStub) mISessionCallback).mSessionImpl = new WeakReference<>(sessionImpl);
+        }
+    }
+
+    /**
      * Notify session that a controller sends a command.
      *
      * @param packageName the package name of the controller
@@ -540,139 +548,24 @@
                 }
             };
 
-    /**
-     * Class for Stub implementation
-     */
-    abstract static class CallbackStub {
-        /** Stub method for ISessionCallback.notifyCommand */
-        public void onCommand(@NonNull String packageName, int pid, int uid,
-                @NonNull ControllerCallbackLink caller, @NonNull String command,
-                @Nullable Bundle args, @Nullable ResultReceiver cb) {
+    private class CallbackStub extends ISessionCallback.Stub {
+        private WeakReference<MediaSessionEngine> mSessionImpl;
+
+        private RemoteUserInfo createRemoteUserInfo(String packageName, int pid, int uid) {
+            return new RemoteUserInfo(packageName, pid, uid);
         }
 
-        /** Stub method for ISessionCallback.notifyMediaButton */
-        public void onMediaButton(@NonNull String packageName, int pid, int uid,
-                @NonNull Intent mediaButtonIntent, int sequenceNumber,
-                @Nullable ResultReceiver cb) {
-        }
-
-        /** Stub method for ISessionCallback.notifyMediaButtonFromController */
-        public void onMediaButtonFromController(@NonNull String packageName, int pid, int uid,
-                @NonNull ControllerCallbackLink caller, @NonNull Intent mediaButtonIntent) {
-        }
-
-        /** Stub method for ISessionCallback.notifyPrepare */
-        public void onPrepare(@NonNull String packageName, int pid, int uid,
-                @NonNull ControllerCallbackLink caller) {
-        }
-
-        /** Stub method for ISessionCallback.notifyPrepareFromMediaId */
-        public void onPrepareFromMediaId(@NonNull String packageName, int pid, int uid,
-                @NonNull ControllerCallbackLink caller, @NonNull String mediaId,
-                @Nullable Bundle extras) {
-        }
-
-        /** Stub method for ISessionCallback.notifyPrepareFromSearch */
-        public void onPrepareFromSearch(@NonNull String packageName, int pid, int uid,
-                @NonNull ControllerCallbackLink caller, String query, @Nullable Bundle extras) {
-        }
-
-        /** Stub method for ISessionCallback.notifyPrepareFromUri */
-        public void onPrepareFromUri(@NonNull String packageName, int pid, int uid,
-                @NonNull ControllerCallbackLink caller, @NonNull Uri uri, @Nullable Bundle extras) {
-        }
-
-        /** Stub method for ISessionCallback.notifyPlay */
-        public void onPlay(@NonNull String packageName, int pid, int uid,
-                @NonNull ControllerCallbackLink caller) {
-        }
-
-        /** Stub method for ISessionCallback.notifyPlayFromMediaId */
-        public void onPlayFromMediaId(@NonNull String packageName, int pid, int uid,
-                @NonNull ControllerCallbackLink caller, @NonNull String mediaId,
-                @Nullable Bundle extras) {
-        }
-
-        /** Stub method for ISessionCallback.notifyPlayFromSearch */
-        public void onPlayFromSearch(@NonNull String packageName, int pid, int uid,
-                @NonNull ControllerCallbackLink caller, String query, @Nullable Bundle extras) {
-        }
-
-        /** Stub method for ISessionCallback.notifyPlayFromUri */
-        public void onPlayFromUri(@NonNull String packageName, int pid, int uid,
-                @NonNull ControllerCallbackLink caller, @NonNull Uri uri, @Nullable Bundle extras) {
-        }
-
-        /** Stub method for ISessionCallback.notifySkipToTrack */
-        public void onSkipToTrack(@NonNull String packageName, int pid, int uid,
-                @NonNull ControllerCallbackLink caller, long id) {
-        }
-
-        /** Stub method for ISessionCallback.notifyPause */
-        public void onPause(@NonNull String packageName, int pid, int uid,
-                @NonNull ControllerCallbackLink caller) {
-        }
-
-        /** Stub method for ISessionCallback.notifyStop */
-        public void onStop(@NonNull String packageName, int pid, int uid,
-                @NonNull ControllerCallbackLink caller) {
-        }
-
-        /** Stub method for ISessionCallback.notifyNext */
-        public void onNext(@NonNull String packageName, int pid, int uid,
-                @NonNull ControllerCallbackLink caller) {
-        }
-
-        /** Stub method for ISessionCallback.notifyPrevious */
-        public void onPrevious(@NonNull String packageName, int pid, int uid,
-                @NonNull ControllerCallbackLink caller) {
-        }
-
-        /** Stub method for ISessionCallback.notifyFastForward */
-        public void onFastForward(@NonNull String packageName, int pid, int uid,
-                @NonNull ControllerCallbackLink caller) {
-        }
-
-        /** Stub method for ISessionCallback.notifyRewind */
-        public void onRewind(@NonNull String packageName, int pid, int uid,
-                @NonNull ControllerCallbackLink caller) {
-        }
-
-        /** Stub method for ISessionCallback.notifySeekTo */
-        public void onSeekTo(@NonNull String packageName, int pid, int uid,
-                @NonNull ControllerCallbackLink caller, long pos) {
-        }
-
-        /** Stub method for ISessionCallback.notifyRate */
-        public void onRate(@NonNull String packageName, int pid, int uid,
-                @NonNull ControllerCallbackLink caller, @NonNull Rating rating) {
-        }
-
-        /** Stub method for ISessionCallback.notifyCustomAction */
-        public void onCustomAction(@NonNull String packageName, int pid, int uid,
-                @NonNull ControllerCallbackLink caller, @NonNull String action,
-                @Nullable Bundle args) {
-        }
-
-        /** Stub method for ISessionCallback.notifyAdjustVolume */
-        public void onAdjustVolume(@NonNull String packageName, int pid, int uid,
-                @NonNull ControllerCallbackLink caller, int direction) {
-        }
-
-        /** Stub method for ISessionCallback.notifySetVolumeTo */
-        public void onSetVolumeTo(@NonNull String packageName, int pid, int uid,
-                @NonNull ControllerCallbackLink caller, int value) {
-        }
-    }
-
-    private class CallbackStubProxy extends ISessionCallback.Stub {
         @Override
         public void notifyCommand(String packageName, int pid, int uid,
                 ControllerCallbackLink caller, String command, Bundle args, ResultReceiver cb) {
             ensureMediaControlPermission();
             final long token = Binder.clearCallingIdentity();
             try {
-                mCallbackStub.onCommand(packageName, pid, uid, caller, command, args, cb);
+                MediaSessionEngine sessionImpl = mSessionImpl.get();
+                if (sessionImpl != null) {
+                    sessionImpl.dispatchCommand(createRemoteUserInfo(packageName, pid, uid),
+                            command, args, cb);
+                }
             } finally {
                 Binder.restoreCallingIdentity(token);
             }
@@ -684,9 +577,15 @@
             ensureMediaControlPermission();
             final long token = Binder.clearCallingIdentity();
             try {
-                mCallbackStub.onMediaButton(packageName, pid, uid, mediaButtonIntent,
-                        sequenceNumber, cb);
+                MediaSessionEngine sessionImpl = mSessionImpl.get();
+                if (sessionImpl != null) {
+                    sessionImpl.dispatchMediaButton(
+                            createRemoteUserInfo(packageName, pid, uid), mediaButtonIntent);
+                }
             } finally {
+                if (cb != null) {
+                    cb.send(sequenceNumber, null);
+                }
                 Binder.restoreCallingIdentity(token);
             }
         }
@@ -697,8 +596,11 @@
             ensureMediaControlPermission();
             final long token = Binder.clearCallingIdentity();
             try {
-                mCallbackStub.onMediaButtonFromController(packageName, pid, uid, caller,
-                        mediaButtonIntent);
+                MediaSessionEngine sessionImpl = mSessionImpl.get();
+                if (sessionImpl != null) {
+                    sessionImpl.dispatchMediaButton(createRemoteUserInfo(packageName, pid, uid),
+                            mediaButtonIntent);
+                }
             } finally {
                 Binder.restoreCallingIdentity(token);
             }
@@ -710,7 +612,10 @@
             ensureMediaControlPermission();
             final long token = Binder.clearCallingIdentity();
             try {
-                mCallbackStub.onPrepare(packageName, pid, uid, caller);
+                MediaSessionEngine sessionImpl = mSessionImpl.get();
+                if (sessionImpl != null) {
+                    sessionImpl.dispatchPrepare(createRemoteUserInfo(packageName, pid, uid));
+                }
             } finally {
                 Binder.restoreCallingIdentity(token);
             }
@@ -722,7 +627,11 @@
             ensureMediaControlPermission();
             final long token = Binder.clearCallingIdentity();
             try {
-                mCallbackStub.onPrepareFromMediaId(packageName, pid, uid, caller, mediaId, extras);
+                MediaSessionEngine sessionImpl = mSessionImpl.get();
+                if (sessionImpl != null) {
+                    sessionImpl.dispatchPrepareFromMediaId(
+                            createRemoteUserInfo(packageName, pid, uid), mediaId, extras);
+                }
             } finally {
                 Binder.restoreCallingIdentity(token);
             }
@@ -734,7 +643,11 @@
             ensureMediaControlPermission();
             final long token = Binder.clearCallingIdentity();
             try {
-                mCallbackStub.onPrepareFromSearch(packageName, pid, uid, caller, query, extras);
+                MediaSessionEngine sessionImpl = mSessionImpl.get();
+                if (sessionImpl != null) {
+                    sessionImpl.dispatchPrepareFromSearch(
+                            createRemoteUserInfo(packageName, pid, uid), query, extras);
+                }
             } finally {
                 Binder.restoreCallingIdentity(token);
             }
@@ -746,7 +659,11 @@
             ensureMediaControlPermission();
             final long token = Binder.clearCallingIdentity();
             try {
-                mCallbackStub.onPrepareFromUri(packageName, pid, uid, caller, uri, extras);
+                MediaSessionEngine sessionImpl = mSessionImpl.get();
+                if (sessionImpl != null) {
+                    sessionImpl.dispatchPrepareFromUri(
+                            createRemoteUserInfo(packageName, pid, uid), uri, extras);
+                }
             } finally {
                 Binder.restoreCallingIdentity(token);
             }
@@ -758,7 +675,10 @@
             ensureMediaControlPermission();
             final long token = Binder.clearCallingIdentity();
             try {
-                mCallbackStub.onPlay(packageName, pid, uid, caller);
+                MediaSessionEngine sessionImpl = mSessionImpl.get();
+                if (sessionImpl != null) {
+                    sessionImpl.dispatchPlay(createRemoteUserInfo(packageName, pid, uid));
+                }
             } finally {
                 Binder.restoreCallingIdentity(token);
             }
@@ -770,7 +690,11 @@
             ensureMediaControlPermission();
             final long token = Binder.clearCallingIdentity();
             try {
-                mCallbackStub.onPlayFromMediaId(packageName, pid, uid, caller, mediaId, extras);
+                MediaSessionEngine sessionImpl = mSessionImpl.get();
+                if (sessionImpl != null) {
+                    sessionImpl.dispatchPlayFromMediaId(
+                            createRemoteUserInfo(packageName, pid, uid), mediaId, extras);
+                }
             } finally {
                 Binder.restoreCallingIdentity(token);
             }
@@ -782,7 +706,11 @@
             ensureMediaControlPermission();
             final long token = Binder.clearCallingIdentity();
             try {
-                mCallbackStub.onPlayFromSearch(packageName, pid, uid, caller, query, extras);
+                MediaSessionEngine sessionImpl = mSessionImpl.get();
+                if (sessionImpl != null) {
+                    sessionImpl.dispatchPlayFromSearch(
+                            createRemoteUserInfo(packageName, pid, uid), query, extras);
+                }
             } finally {
                 Binder.restoreCallingIdentity(token);
             }
@@ -794,7 +722,11 @@
             ensureMediaControlPermission();
             final long token = Binder.clearCallingIdentity();
             try {
-                mCallbackStub.onPlayFromUri(packageName, pid, uid, caller, uri, extras);
+                MediaSessionEngine sessionImpl = mSessionImpl.get();
+                if (sessionImpl != null) {
+                    sessionImpl.dispatchPlayFromUri(
+                            createRemoteUserInfo(packageName, pid, uid), uri, extras);
+                }
             } finally {
                 Binder.restoreCallingIdentity(token);
             }
@@ -806,7 +738,11 @@
             ensureMediaControlPermission();
             final long token = Binder.clearCallingIdentity();
             try {
-                mCallbackStub.onSkipToTrack(packageName, pid, uid, caller, id);
+                MediaSessionEngine sessionImpl = mSessionImpl.get();
+                if (sessionImpl != null) {
+                    sessionImpl.dispatchSkipToItem(
+                            createRemoteUserInfo(packageName, pid, uid), id);
+                }
             } finally {
                 Binder.restoreCallingIdentity(token);
             }
@@ -818,7 +754,10 @@
             ensureMediaControlPermission();
             final long token = Binder.clearCallingIdentity();
             try {
-                mCallbackStub.onPause(packageName, pid, uid, caller);
+                MediaSessionEngine sessionImpl = mSessionImpl.get();
+                if (sessionImpl != null) {
+                    sessionImpl.dispatchPause(createRemoteUserInfo(packageName, pid, uid));
+                }
             } finally {
                 Binder.restoreCallingIdentity(token);
             }
@@ -830,7 +769,10 @@
             ensureMediaControlPermission();
             final long token = Binder.clearCallingIdentity();
             try {
-                mCallbackStub.onStop(packageName, pid, uid, caller);
+                MediaSessionEngine sessionImpl = mSessionImpl.get();
+                if (sessionImpl != null) {
+                    sessionImpl.dispatchStop(createRemoteUserInfo(packageName, pid, uid));
+                }
             } finally {
                 Binder.restoreCallingIdentity(token);
             }
@@ -842,7 +784,10 @@
             ensureMediaControlPermission();
             final long token = Binder.clearCallingIdentity();
             try {
-                mCallbackStub.onNext(packageName, pid, uid, caller);
+                MediaSessionEngine sessionImpl = mSessionImpl.get();
+                if (sessionImpl != null) {
+                    sessionImpl.dispatchNext(createRemoteUserInfo(packageName, pid, uid));
+                }
             } finally {
                 Binder.restoreCallingIdentity(token);
             }
@@ -854,7 +799,10 @@
             ensureMediaControlPermission();
             final long token = Binder.clearCallingIdentity();
             try {
-                mCallbackStub.onPrevious(packageName, pid, uid, caller);
+                MediaSessionEngine sessionImpl = mSessionImpl.get();
+                if (sessionImpl != null) {
+                    sessionImpl.dispatchPrevious(createRemoteUserInfo(packageName, pid, uid));
+                }
             } finally {
                 Binder.restoreCallingIdentity(token);
             }
@@ -866,7 +814,11 @@
             ensureMediaControlPermission();
             final long token = Binder.clearCallingIdentity();
             try {
-                mCallbackStub.onFastForward(packageName, pid, uid, caller);
+                MediaSessionEngine sessionImpl = mSessionImpl.get();
+                if (sessionImpl != null) {
+                    sessionImpl.dispatchFastForward(
+                            createRemoteUserInfo(packageName, pid, uid));
+                }
             } finally {
                 Binder.restoreCallingIdentity(token);
             }
@@ -878,7 +830,10 @@
             ensureMediaControlPermission();
             final long token = Binder.clearCallingIdentity();
             try {
-                mCallbackStub.onRewind(packageName, pid, uid, caller);
+                MediaSessionEngine sessionImpl = mSessionImpl.get();
+                if (sessionImpl != null) {
+                    sessionImpl.dispatchRewind(createRemoteUserInfo(packageName, pid, uid));
+                }
             } finally {
                 Binder.restoreCallingIdentity(token);
             }
@@ -890,7 +845,11 @@
             ensureMediaControlPermission();
             final long token = Binder.clearCallingIdentity();
             try {
-                mCallbackStub.onSeekTo(packageName, pid, uid, caller, pos);
+                MediaSessionEngine sessionImpl = mSessionImpl.get();
+                if (sessionImpl != null) {
+                    sessionImpl.dispatchSeekTo(
+                            createRemoteUserInfo(packageName, pid, uid), pos);
+                }
             } finally {
                 Binder.restoreCallingIdentity(token);
             }
@@ -902,7 +861,11 @@
             ensureMediaControlPermission();
             final long token = Binder.clearCallingIdentity();
             try {
-                mCallbackStub.onRate(packageName, pid, uid, caller, rating);
+                MediaSessionEngine sessionImpl = mSessionImpl.get();
+                if (sessionImpl != null) {
+                    sessionImpl.dispatchRate(
+                            createRemoteUserInfo(packageName, pid, uid), rating);
+                }
             } finally {
                 Binder.restoreCallingIdentity(token);
             }
@@ -913,7 +876,11 @@
             ensureMediaControlPermission();
             final long token = Binder.clearCallingIdentity();
             try {
-                mCallbackStub.onCustomAction(packageName, pid, uid, caller, action, args);
+                MediaSessionEngine sessionImpl = mSessionImpl.get();
+                if (sessionImpl != null) {
+                    sessionImpl.dispatchCustomAction(
+                            createRemoteUserInfo(packageName, pid, uid), action, args);
+                }
             } finally {
                 Binder.restoreCallingIdentity(token);
             }
@@ -925,7 +892,11 @@
             ensureMediaControlPermission();
             final long token = Binder.clearCallingIdentity();
             try {
-                mCallbackStub.onAdjustVolume(packageName, pid, uid, caller, direction);
+                MediaSessionEngine sessionImpl = mSessionImpl.get();
+                if (sessionImpl != null) {
+                    sessionImpl.dispatchAdjustVolume(
+                            createRemoteUserInfo(packageName, pid, uid), direction);
+                }
             } finally {
                 Binder.restoreCallingIdentity(token);
             }
@@ -937,7 +908,11 @@
             ensureMediaControlPermission();
             final long token = Binder.clearCallingIdentity();
             try {
-                mCallbackStub.onSetVolumeTo(packageName, pid, uid, caller, value);
+                MediaSessionEngine sessionImpl = mSessionImpl.get();
+                if (sessionImpl != null) {
+                    sessionImpl.dispatchSetVolumeTo(
+                            createRemoteUserInfo(packageName, pid, uid), value);
+                }
             } finally {
                 Binder.restoreCallingIdentity(token);
             }
diff --git a/media/apex/java/android/media/session/SessionLink.aidl b/media/java/android/media/session/SessionLink.aidl
similarity index 100%
rename from media/apex/java/android/media/session/SessionLink.aidl
rename to media/java/android/media/session/SessionLink.aidl
diff --git a/media/apex/java/android/media/session/SessionLink.java b/media/java/android/media/session/SessionLink.java
similarity index 99%
rename from media/apex/java/android/media/session/SessionLink.java
rename to media/java/android/media/session/SessionLink.java
index 4ea7623..2331a4a 100644
--- a/media/apex/java/android/media/session/SessionLink.java
+++ b/media/java/android/media/session/SessionLink.java
@@ -18,7 +18,6 @@
 
 import android.annotation.NonNull;
 import android.annotation.Nullable;
-import android.annotation.SystemApi;
 import android.app.PendingIntent;
 import android.media.AudioAttributes;
 import android.media.MediaMetadata;
@@ -36,7 +35,6 @@
  * Handles incoming commands from {@link MediaSession}.
  * @hide
  */
-@SystemApi
 public final class SessionLink implements Parcelable {
     public static final Parcelable.Creator<SessionLink> CREATOR =
             new Parcelable.Creator<SessionLink>() {
diff --git a/media/apex/java/android/service/media/IMediaBrowserService.aidl b/media/java/android/service/media/IMediaBrowserService.aidl
similarity index 100%
rename from media/apex/java/android/service/media/IMediaBrowserService.aidl
rename to media/java/android/service/media/IMediaBrowserService.aidl
diff --git a/media/apex/java/android/service/media/IMediaBrowserServiceCallbacks.aidl b/media/java/android/service/media/IMediaBrowserServiceCallbacks.aidl
similarity index 100%
rename from media/apex/java/android/service/media/IMediaBrowserServiceCallbacks.aidl
rename to media/java/android/service/media/IMediaBrowserServiceCallbacks.aidl
diff --git a/media/apex/java/android/service/media/MediaBrowserService.java b/media/java/android/service/media/MediaBrowserService.java
similarity index 100%
rename from media/apex/java/android/service/media/MediaBrowserService.java
rename to media/java/android/service/media/MediaBrowserService.java
diff --git a/media/jni/android_media_MediaDrm.cpp b/media/jni/android_media_MediaDrm.cpp
index 866325c..d7ab854 100644
--- a/media/jni/android_media_MediaDrm.cpp
+++ b/media/jni/android_media_MediaDrm.cpp
@@ -175,7 +175,7 @@
 
 struct OfflineLicenseState {
     jint kOfflineLicenseStateUsable;
-    jint kOfflineLicenseStateInactive;
+    jint kOfflineLicenseStateReleased;
     jint kOfflineLicenseStateUnknown;
 } gOfflineLicenseStates;
 
@@ -797,10 +797,10 @@
     GET_STATIC_FIELD_ID(field, clazz, "SECURITY_LEVEL_HW_SECURE_ALL", "I");
     gSecurityLevels.kSecurityLevelHwSecureAll = env->GetStaticIntField(clazz, field);
 
-    GET_STATIC_FIELD_ID(field, clazz, "OFFLINE_LICENSE_USABLE", "I");
+    GET_STATIC_FIELD_ID(field, clazz, "OFFLINE_LICENSE_STATE_USABLE", "I");
     gOfflineLicenseStates.kOfflineLicenseStateUsable = env->GetStaticIntField(clazz, field);
-    GET_STATIC_FIELD_ID(field, clazz, "OFFLINE_LICENSE_INACTIVE", "I");
-    gOfflineLicenseStates.kOfflineLicenseStateInactive = env->GetStaticIntField(clazz, field);
+    GET_STATIC_FIELD_ID(field, clazz, "OFFLINE_LICENSE_STATE_RELEASED", "I");
+    gOfflineLicenseStates.kOfflineLicenseStateReleased = env->GetStaticIntField(clazz, field);
     GET_STATIC_FIELD_ID(field, clazz, "OFFLINE_LICENSE_STATE_UNKNOWN", "I");
     gOfflineLicenseStates.kOfflineLicenseStateUnknown = env->GetStaticIntField(clazz, field);
 
@@ -1581,8 +1581,8 @@
     switch(state) {
     case DrmPlugin::kOfflineLicenseStateUsable:
         return gOfflineLicenseStates.kOfflineLicenseStateUsable;
-    case DrmPlugin::kOfflineLicenseStateInactive:
-        return gOfflineLicenseStates.kOfflineLicenseStateInactive;
+    case DrmPlugin::kOfflineLicenseStateReleased:
+        return gOfflineLicenseStates.kOfflineLicenseStateReleased;
     default:
         return gOfflineLicenseStates.kOfflineLicenseStateUnknown;
     }
diff --git a/media/jni/android_media_MediaExtractor.cpp b/media/jni/android_media_MediaExtractor.cpp
index f3442f4..c6b171b 100644
--- a/media/jni/android_media_MediaExtractor.cpp
+++ b/media/jni/android_media_MediaExtractor.cpp
@@ -913,13 +913,9 @@
     }
 
     // build and return the Bundle
-    MediaAnalyticsItem *item = new MediaAnalyticsItem;
+    std::unique_ptr<MediaAnalyticsItem> item(MediaAnalyticsItem::create());
     item->readFromParcel(reply);
-    jobject mybundle = MediaMetricsJNI::writeMetricsToBundle(env, item, NULL);
-
-    // housekeeping
-    delete item;
-    item = NULL;
+    jobject mybundle = MediaMetricsJNI::writeMetricsToBundle(env, item.get(), NULL);
 
     return mybundle;
 }
diff --git a/media/jni/android_media_MediaPlayer.cpp b/media/jni/android_media_MediaPlayer.cpp
index 76bbce7..35b1081 100644
--- a/media/jni/android_media_MediaPlayer.cpp
+++ b/media/jni/android_media_MediaPlayer.cpp
@@ -682,13 +682,9 @@
         return (jobject) NULL;
     }
 
-    MediaAnalyticsItem *item = new MediaAnalyticsItem;
+    std::unique_ptr<MediaAnalyticsItem> item(MediaAnalyticsItem::create());
     item->readFromParcel(p);
-    jobject mybundle = MediaMetricsJNI::writeMetricsToBundle(env, item, NULL);
-
-    // housekeeping
-    delete item;
-    item = NULL;
+    jobject mybundle = MediaMetricsJNI::writeMetricsToBundle(env, item.get(), NULL);
 
     return mybundle;
 }
diff --git a/media/jni/android_media_MediaRecorder.cpp b/media/jni/android_media_MediaRecorder.cpp
index ca30f32..a7c0159 100644
--- a/media/jni/android_media_MediaRecorder.cpp
+++ b/media/jni/android_media_MediaRecorder.cpp
@@ -664,15 +664,11 @@
     }
 
     // build and return the Bundle
-    MediaAnalyticsItem *item = new MediaAnalyticsItem;
+    std::unique_ptr<MediaAnalyticsItem> item(MediaAnalyticsItem::create());
     item->readFromParcel(reply);
-    jobject mybundle = MediaMetricsJNI::writeMetricsToBundle(env, item, NULL);
+    jobject mybundle = MediaMetricsJNI::writeMetricsToBundle(env, item.get(), NULL);
 
-    // housekeeping
-    delete item;
-    item = NULL;
     return mybundle;
-
 }
 
 static jboolean
diff --git a/native/webview/plat_support/draw_functor.cpp b/native/webview/plat_support/draw_functor.cpp
index 6deb47f..e43a60c 100644
--- a/native/webview/plat_support/draw_functor.cpp
+++ b/native/webview/plat_support/draw_functor.cpp
@@ -177,9 +177,6 @@
         webview_functor_callbacks.vk.initialize = &initializeVk;
         webview_functor_callbacks.vk.draw = &drawVk;
         webview_functor_callbacks.vk.postDraw = &postDrawVk;
-        // TODO(boliu): Remove this once SkiaRecordingCanvas::drawWebViewFunctor
-        // no longer uses GL interop.
-        webview_functor_callbacks.gles.draw = &draw_gl;
         break;
     }
     callbacks_initialized = true;
diff --git a/packages/CaptivePortalLogin/Android.bp b/packages/CaptivePortalLogin/Android.bp
new file mode 100644
index 0000000..7acdfa1
--- /dev/null
+++ b/packages/CaptivePortalLogin/Android.bp
@@ -0,0 +1,27 @@
+//
+// Copyright (C) 2019 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+android_app {
+    name: "CaptivePortalLogin",
+    srcs: ["src/**/*.java"],
+    sdk_version: "system_current",
+    certificate: "platform",
+    static_libs: [
+        "androidx.legacy_legacy-support-v4",
+        "metrics-constants-protos",
+    ],
+    manifest: "AndroidManifest.xml",
+}
diff --git a/packages/CaptivePortalLogin/Android.mk b/packages/CaptivePortalLogin/Android.mk
deleted file mode 100644
index 8c63f45..0000000
--- a/packages/CaptivePortalLogin/Android.mk
+++ /dev/null
@@ -1,14 +0,0 @@
-LOCAL_PATH:= $(call my-dir)
-include $(CLEAR_VARS)
-
-LOCAL_MODULE_TAGS := optional
-LOCAL_USE_AAPT2 := true
-LOCAL_STATIC_ANDROID_LIBRARIES := androidx.legacy_legacy-support-v4
-
-LOCAL_SRC_FILES := $(call all-java-files-under, src)
-
-LOCAL_PACKAGE_NAME := CaptivePortalLogin
-LOCAL_PRIVATE_PLATFORM_APIS := true
-LOCAL_CERTIFICATE := platform
-
-include $(BUILD_PACKAGE)
diff --git a/packages/CaptivePortalLogin/AndroidManifest.xml b/packages/CaptivePortalLogin/AndroidManifest.xml
index 5ab6632..e15dca0 100644
--- a/packages/CaptivePortalLogin/AndroidManifest.xml
+++ b/packages/CaptivePortalLogin/AndroidManifest.xml
@@ -21,9 +21,10 @@
 
     <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
-    <uses-permission android:name="android.permission.CONNECTIVITY_INTERNAL" />
-    <uses-permission android:name="android.permission.NETWORK_BYPASS_PRIVATE_DNS" />
+    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
+    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
     <uses-permission android:name="android.permission.NETWORK_SETTINGS" />
+    <uses-permission android:name="android.permission.NETWORK_BYPASS_PRIVATE_DNS" />
 
     <application android:label="@string/app_name"
                  android:usesCleartextTraffic="true"
diff --git a/packages/CaptivePortalLogin/res/layout/ssl_error_msg.xml b/packages/CaptivePortalLogin/res/layout/ssl_error_msg.xml
deleted file mode 100644
index d460041..0000000
--- a/packages/CaptivePortalLogin/res/layout/ssl_error_msg.xml
+++ /dev/null
@@ -1,28 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2018 The Android Open Source Project
-
-     Licensed under the Apache License, Version 2.0 (the "License");
-     you may not use this file except in compliance with the License.
-     You may obtain a copy of the License at
-
-          http://www.apache.org/licenses/LICENSE-2.0
-
-     Unless required by applicable law or agreed to in writing, software
-     distributed under the License is distributed on an "AS IS" BASIS,
-     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-     See the License for the specific language governing permissions and
-     limitations under the License.
--->
-
-<TextView
-    xmlns:android="http://schemas.android.com/apk/res/android"
-    android:id="@+id/ssl_error_msg"
-    android:layout_width="wrap_content"
-    android:layout_height="wrap_content"
-    android:textAppearance="?android:attr/textAppearanceSmall"
-    android:layout_marginStart="20dip"
-    android:layout_marginEnd="20dip"
-    android:gravity="center_vertical"
-    android:layout_marginBottom="4dip"
-    android:layout_marginTop="4dip" />
-
diff --git a/packages/CaptivePortalLogin/res/layout/ssl_warning.xml b/packages/CaptivePortalLogin/res/layout/ssl_warning.xml
index ffd57a4..ce05e78 100644
--- a/packages/CaptivePortalLogin/res/layout/ssl_warning.xml
+++ b/packages/CaptivePortalLogin/res/layout/ssl_warning.xml
@@ -78,7 +78,18 @@
             android:id="@+id/certificate_layout"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
+            android:orientation="vertical"
             android:layout_marginBottom="16dip" >
+            <TextView
+                android:id="@+id/ssl_error_msg"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:textAppearance="?android:attr/textAppearanceSmall"
+                android:layout_marginStart="20dip"
+                android:layout_marginEnd="20dip"
+                android:gravity="center_vertical"
+                android:layout_marginBottom="4dip"
+                android:layout_marginTop="16dip" />
         </LinearLayout>
 
     </ScrollView>
diff --git a/packages/CaptivePortalLogin/src/com/android/captiveportallogin/CaptivePortalLoginActivity.java b/packages/CaptivePortalLogin/src/com/android/captiveportallogin/CaptivePortalLoginActivity.java
index 0a571c5..ce627ce 100644
--- a/packages/CaptivePortalLogin/src/com/android/captiveportallogin/CaptivePortalLoginActivity.java
+++ b/packages/CaptivePortalLogin/src/com/android/captiveportallogin/CaptivePortalLoginActivity.java
@@ -20,7 +20,7 @@
 
 import android.app.Activity;
 import android.app.AlertDialog;
-import android.app.LoadedApk;
+import android.app.Application;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
@@ -37,8 +37,9 @@
 import android.net.http.SslCertificate;
 import android.net.http.SslError;
 import android.net.wifi.WifiInfo;
-import android.os.Build;
+import android.net.wifi.WifiManager;
 import android.os.Bundle;
+import android.os.SystemProperties;
 import android.text.TextUtils;
 import android.util.ArrayMap;
 import android.util.Log;
@@ -96,6 +97,7 @@
     private CaptivePortal mCaptivePortal;
     private NetworkCallback mNetworkCallback;
     private ConnectivityManager mCm;
+    private WifiManager mWifiManager;
     private boolean mLaunchBrowser = false;
     private MyWebViewClient mWebViewClient;
     private SwipeRefreshLayout mSwipeRefreshLayout;
@@ -109,7 +111,8 @@
         mCaptivePortal = getIntent().getParcelableExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL);
         logMetricsEvent(MetricsEvent.ACTION_CAPTIVE_PORTAL_LOGIN_ACTIVITY);
 
-        mCm = ConnectivityManager.from(this);
+        mCm = getSystemService(ConnectivityManager.class);
+        mWifiManager = getSystemService(WifiManager.class);
         mNetwork = getIntent().getParcelableExtra(ConnectivityManager.EXTRA_NETWORK);
         mUserAgent =
                 getIntent().getStringExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL_USER_AGENT);
@@ -151,7 +154,6 @@
         // Also initializes proxy system properties.
         mNetwork = mNetwork.getPrivateDnsBypassingCopy();
         mCm.bindProcessToNetwork(mNetwork);
-        mCm.setProcessDefaultNetworkForHostResolution(mNetwork);
 
         // Proxy system properties must be initialized before setContentView is called because
         // setContentView initializes the WebView logic which in turn reads the system properties.
@@ -190,9 +192,12 @@
 
     // Find WebView's proxy BroadcastReceiver and prompt it to read proxy system properties.
     private void setWebViewProxy() {
-        LoadedApk loadedApk = getApplication().mLoadedApk;
+        // TODO: migrate to androidx WebView proxy setting API as soon as it is finalized
         try {
-            Field receiversField = LoadedApk.class.getDeclaredField("mReceivers");
+            final Field loadedApkField = Application.class.getDeclaredField("mLoadedApk");
+            final Class<?> loadedApkClass = loadedApkField.getType();
+            final Object loadedApk = loadedApkField.get(getApplication());
+            Field receiversField = loadedApkClass.getDeclaredField("mReceivers");
             receiversField.setAccessible(true);
             ArrayMap receivers = (ArrayMap) receiversField.get(loadedApk);
             for (Object receiverMap : receivers.values()) {
@@ -333,7 +338,11 @@
 
     private static String sanitizeURL(URL url) {
         // In non-Debug build, only show host to avoid leaking private info.
-        return Build.IS_DEBUGGABLE ? Objects.toString(url) : host(url);
+        return isDebuggable() ? Objects.toString(url) : host(url);
+    }
+
+    private static boolean isDebuggable() {
+        return SystemProperties.getInt("ro.debuggable", 0) == 1;
     }
 
     private void testForCaptivePortal() {
@@ -586,19 +595,18 @@
         }
 
         private void setViewSecurityCertificate(LinearLayout certificateLayout, SslError error) {
+            ((TextView) certificateLayout.findViewById(R.id.ssl_error_msg))
+                    .setText(sslErrorMessage(error));
             SslCertificate cert = error.getCertificate();
-
-            View certificateView = cert.inflateCertificateView(CaptivePortalLoginActivity.this);
-            final LinearLayout placeholder = (LinearLayout) certificateView
-                    .findViewById(com.android.internal.R.id.placeholder);
-            LayoutInflater factory = LayoutInflater.from(CaptivePortalLoginActivity.this);
-
-            TextView textView = (TextView) factory.inflate(
-                    R.layout.ssl_error_msg, placeholder, false);
-            textView.setText(sslErrorMessage(error));
-            placeholder.addView(textView);
-
-            certificateLayout.addView(certificateView);
+            // TODO: call the method directly once inflateCertificateView is @SystemApi
+            try {
+                final View certificateView = (View) SslCertificate.class.getMethod(
+                        "inflateCertificateView", Context.class)
+                        .invoke(cert, CaptivePortalLoginActivity.this);
+                certificateLayout.addView(certificateView);
+            } catch (ReflectiveOperationException | SecurityException e) {
+                Log.e(TAG, "Could not create certificate view", e);
+            }
         }
     }
 
@@ -619,11 +627,30 @@
 
     private String getHeaderTitle() {
         NetworkCapabilities nc = mCm.getNetworkCapabilities(mNetwork);
-        if (nc == null || TextUtils.isEmpty(nc.getSSID())
-            || !nc.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
+        final String ssid = getSsid();
+        if (TextUtils.isEmpty(ssid)
+                || nc == null || !nc.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
             return getString(R.string.action_bar_label);
         }
-        return getString(R.string.action_bar_title, WifiInfo.removeDoubleQuotes(nc.getSSID()));
+        return getString(R.string.action_bar_title, ssid);
+    }
+
+    // TODO: remove once SSID is obtained from NetworkCapabilities
+    private String getSsid() {
+        if (mWifiManager == null) {
+            return null;
+        }
+        final WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
+        return removeDoubleQuotes(wifiInfo.getSSID());
+    }
+
+    private static String removeDoubleQuotes(String string) {
+        if (string == null) return null;
+        final int length = string.length();
+        if ((length > 1) && (string.charAt(0) == '"') && (string.charAt(length - 1) == '"')) {
+            return string.substring(1, length - 1);
+        }
+        return string;
     }
 
     private String getHeaderSubtitle(URL url) {
diff --git a/packages/CarSystemUI/res/values/integers_car.xml b/packages/CarSystemUI/res/values/integers_car.xml
index 777283d..be2cb0d 100644
--- a/packages/CarSystemUI/res/values/integers_car.xml
+++ b/packages/CarSystemUI/res/values/integers_car.xml
@@ -23,7 +23,7 @@
     <!-- Number of milliseconds user can spend driving with the keyguard up. After that, we switch to Guest. -->
     <!-- If the number is negative, the feature is disabled.
          If it's zero, we switch to guest immediately as we start driving. -->
-    <integer name="driving_on_keyguard_timeout_ms">30000</integer>
+    <integer name="driving_on_keyguard_timeout_ms">-1</integer>
 
     <!--Percentage of the screen height, from the bottom, that a notification panel being
     partially closed at will result in it remaining open if released-->
diff --git a/packages/ExtServices/Android.bp b/packages/ExtServices/Android.bp
new file mode 100644
index 0000000..77972fe
--- /dev/null
+++ b/packages/ExtServices/Android.bp
@@ -0,0 +1,11 @@
+android_library {
+    name: "ExtServices-core",
+     srcs: [
+         "src/**/*.java",
+     ],
+     resource_dirs: [
+         "res",
+     ],
+
+     manifest: "AndroidManifest.xml",
+}
\ No newline at end of file
diff --git a/packages/ExtServices/tests/Android.bp b/packages/ExtServices/tests/Android.bp
new file mode 100644
index 0000000..5de4548
--- /dev/null
+++ b/packages/ExtServices/tests/Android.bp
@@ -0,0 +1,26 @@
+android_test {
+    name: "ExtServicesUnitTests",
+
+    // Include all test java files.
+    srcs: ["src/**/*.java"],
+
+    // We only want this apk build for tests.
+    certificate: "platform",
+
+    libs: [
+        "android.test.runner",
+        "android.test.base",
+    ],
+
+    static_libs: [
+        "ExtServices-core",
+        "android-support-test",
+        "mockito-target-minus-junit4",
+        "espresso-core",
+        "truth-prebuilt",
+        "testables",
+        "testng",
+    ],
+
+    platform_apis: true,
+}
\ No newline at end of file
diff --git a/packages/ExtServices/tests/Android.mk b/packages/ExtServices/tests/Android.mk
deleted file mode 100644
index a57fa94..0000000
--- a/packages/ExtServices/tests/Android.mk
+++ /dev/null
@@ -1,26 +0,0 @@
-LOCAL_PATH:= $(call my-dir)
-include $(CLEAR_VARS)
-
-# We only want this apk build for tests.
-LOCAL_MODULE_TAGS := tests
-LOCAL_CERTIFICATE := platform
-
-LOCAL_JAVA_LIBRARIES := android.test.runner android.test.base
-
-LOCAL_STATIC_JAVA_LIBRARIES := \
-    android-support-test \
-    mockito-target-minus-junit4 \
-    espresso-core \
-    truth-prebuilt \
-    testables \
-    testng
-
-# Include all test java files.
-LOCAL_SRC_FILES := $(call all-java-files-under, src)
-
-LOCAL_PACKAGE_NAME := ExtServicesUnitTests
-LOCAL_PRIVATE_PLATFORM_APIS := true
-
-LOCAL_INSTRUMENTATION_FOR := ExtServices
-
-include $(BUILD_PACKAGE)
diff --git a/packages/ExtServices/tests/AndroidManifest.xml b/packages/ExtServices/tests/AndroidManifest.xml
index ddf725b..3cf1527 100644
--- a/packages/ExtServices/tests/AndroidManifest.xml
+++ b/packages/ExtServices/tests/AndroidManifest.xml
@@ -24,7 +24,7 @@
     </application>
 
     <instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
-                     android:targetPackage="android.ext.services"
+                     android:targetPackage="android.ext.services.tests.unit"
                      android:label="ExtServices Test Cases">
     </instrumentation>
 
diff --git a/packages/ExtServices/tests/src/android/ext/services/notification/NotificationCategorizerTest.java b/packages/ExtServices/tests/src/android/ext/services/notification/NotificationCategorizerTest.java
index 12908e6..c59885e 100644
--- a/packages/ExtServices/tests/src/android/ext/services/notification/NotificationCategorizerTest.java
+++ b/packages/ExtServices/tests/src/android/ext/services/notification/NotificationCategorizerTest.java
@@ -180,7 +180,7 @@
         assertFalse(nc.shouldSilence(NotificationCategorizer.CATEGORY_SYSTEM));
 
         when(mSbn.getUid()).thenReturn(FIRST_APPLICATION_UID);
-        assertEquals(NotificationCategorizer.CATEGORY_EVERYTHING_ELSE, nc.getCategory(mEntry));
+        assertEquals(NotificationCategorizer.CATEGORY_HIGH, nc.getCategory(mEntry));
     }
 
     @Test
diff --git a/packages/NetworkStack/Android.bp b/packages/NetworkStack/Android.bp
index b0522f2..d656593 100644
--- a/packages/NetworkStack/Android.bp
+++ b/packages/NetworkStack/Android.bp
@@ -41,4 +41,5 @@
         "NetworkStackLib"
     ],
     manifest: "AndroidManifest.xml",
+    required: ["NetworkStackPermissionStub"],
 }
\ No newline at end of file
diff --git a/packages/NetworkStack/AndroidManifest.xml b/packages/NetworkStack/AndroidManifest.xml
index 5ab833b..ac55bfa 100644
--- a/packages/NetworkStack/AndroidManifest.xml
+++ b/packages/NetworkStack/AndroidManifest.xml
@@ -25,6 +25,8 @@
     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
     <uses-permission android:name="android.permission.CONNECTIVITY_INTERNAL" />
     <uses-permission android:name="android.permission.NETWORK_SETTINGS" />
+    <!-- Signature permission defined in NetworkStackStub -->
+    <uses-permission android:name="android.permission.MAINLINE_NETWORK_STACK" />
     <!-- Launch captive portal app as specific user -->
     <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" />
     <uses-permission android:name="android.permission.NETWORK_STACK" />
diff --git a/packages/NetworkStack/src/com/android/server/connectivity/NetworkMonitor.java b/packages/NetworkStack/src/com/android/server/connectivity/NetworkMonitor.java
index b34efc4..dbffa6d 100644
--- a/packages/NetworkStack/src/com/android/server/connectivity/NetworkMonitor.java
+++ b/packages/NetworkStack/src/com/android/server/connectivity/NetworkMonitor.java
@@ -109,6 +109,8 @@
     private static final boolean DBG  = true;
     private static final boolean VDBG = false;
     private static final boolean VDBG_STALL = Log.isLoggable(TAG, Log.DEBUG);
+    // TODO: use another permission for CaptivePortalLoginActivity once it has its own certificate
+    private static final String PERMISSION_NETWORK_SETTINGS = "android.permission.NETWORK_SETTINGS";
     // Default configuration values for captive portal detection probes.
     // TODO: append a random length parameter to the default HTTPS url.
     // TODO: randomize browser version ids in the default User-Agent String.
@@ -682,7 +684,7 @@
                                 public void appResponse(int response) {
                                     if (response == APP_RETURN_WANTED_AS_IS) {
                                         mContext.enforceCallingPermission(
-                                                android.Manifest.permission.CONNECTIVITY_INTERNAL,
+                                                PERMISSION_NETWORK_SETTINGS,
                                                 "CaptivePortal");
                                     }
                                     sendMessage(CMD_CAPTIVE_PORTAL_APP_FINISHED, response);
@@ -692,7 +694,7 @@
                                 public void logEvent(int eventId, String packageName)
                                         throws RemoteException {
                                     mContext.enforceCallingPermission(
-                                            android.Manifest.permission.CONNECTIVITY_INTERNAL,
+                                            PERMISSION_NETWORK_SETTINGS,
                                             "CaptivePortal");
                                     mCallback.logCaptivePortalLoginEvent(eventId, packageName);
                                 }
diff --git a/packages/NetworkStack/src/com/android/server/util/PermissionUtil.java b/packages/NetworkStack/src/com/android/server/util/PermissionUtil.java
index 82bf038..f6eb900 100644
--- a/packages/NetworkStack/src/com/android/server/util/PermissionUtil.java
+++ b/packages/NetworkStack/src/com/android/server/util/PermissionUtil.java
@@ -19,6 +19,7 @@
 import static android.os.Binder.getCallingUid;
 
 import android.os.Process;
+import android.os.UserHandle;
 
 /**
  * Utility class to check calling permissions on the network stack.
@@ -32,7 +33,7 @@
     public static void checkNetworkStackCallingPermission() {
         // TODO: check that the calling PID is the system server.
         final int caller = getCallingUid();
-        if (caller != Process.SYSTEM_UID && caller != Process.BLUETOOTH_UID) {
+        if (caller != Process.SYSTEM_UID && UserHandle.getAppId(caller) != Process.BLUETOOTH_UID) {
             throw new SecurityException("Invalid caller: " + caller);
         }
     }
diff --git a/packages/NetworkStackPermissionStub/Android.bp b/packages/NetworkStackPermissionStub/Android.bp
new file mode 100644
index 0000000..94870c9
--- /dev/null
+++ b/packages/NetworkStackPermissionStub/Android.bp
@@ -0,0 +1,27 @@
+//
+// Copyright (C) 2019 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+// Stub APK to define permissions for NetworkStack
+android_app {
+    name: "NetworkStackPermissionStub",
+    // TODO: mark app as hasCode=false in manifest once soong stops complaining about apps without
+    // a classes.dex.
+    srcs: ["src/**/*.java"],
+    platform_apis: true,
+    certificate: "platform",
+    privileged: true,
+    manifest: "AndroidManifest.xml",
+}
diff --git a/packages/NetworkStackPermissionStub/AndroidManifest.xml b/packages/NetworkStackPermissionStub/AndroidManifest.xml
new file mode 100644
index 0000000..2ccf5ff
--- /dev/null
+++ b/packages/NetworkStackPermissionStub/AndroidManifest.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+-->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="com.android.mainline.networkstack.permissionstub">
+    <!--
+    This package only exists to define the below permissions, and enforce that they are only
+    granted to apps sharing the same signature.
+    Permissions defined here are intended to be used only by the NetworkStack: both
+    NetworkStack and this stub APK are to be signed with a dedicated certificate to ensure
+    that, with the below permissions being signature permissions.
+
+    This APK *must* be installed, even if the NetworkStack app is not installed, because otherwise,
+    any application will be able to define this permission and the system will give that application
+    full access to the network stack.
+     -->
+    <permission android:name="android.permission.MAINLINE_NETWORK_STACK"
+                android:protectionLevel="signature"/>
+
+    <application android:name="com.android.server.NetworkStackPermissionStub"/>
+</manifest>
\ No newline at end of file
diff --git a/packages/NetworkStackPermissionStub/src/com/android/server/NetworkStackPermissionStub.java b/packages/NetworkStackPermissionStub/src/com/android/server/NetworkStackPermissionStub.java
new file mode 100644
index 0000000..01e59d2
--- /dev/null
+++ b/packages/NetworkStackPermissionStub/src/com/android/server/NetworkStackPermissionStub.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server;
+
+import android.app.Application;
+
+/**
+ * Empty application for NetworkStackStub that only exists because soong builds complain if APKs
+ * have no source file.
+ */
+public class NetworkStackPermissionStub extends Application {
+}
diff --git a/packages/SettingsLib/OWNERS b/packages/SettingsLib/OWNERS
index d188c65..d879087 100644
--- a/packages/SettingsLib/OWNERS
+++ b/packages/SettingsLib/OWNERS
@@ -4,7 +4,7 @@
 dehboxturtle@google.com
 dhnishi@google.com
 dling@google.com
-dsandler@google.com
+dsandler@android.com
 evanlaird@google.com
 jackqdyulei@google.com
 jmonk@google.com
diff --git a/packages/SettingsLib/res/values/strings.xml b/packages/SettingsLib/res/values/strings.xml
index ec70f8c..191bd8f 100644
--- a/packages/SettingsLib/res/values/strings.xml
+++ b/packages/SettingsLib/res/values/strings.xml
@@ -106,7 +106,7 @@
     <!-- Status message of Wi-Fi when network has matching passpoint credentials. [CHAR LIMIT=NONE] -->
     <string name="available_via_passpoint">Available via %1$s</string>
     <!-- Status message of OSU Provider network when not connected. [CHAR LIMIT=NONE] -->
-    <string name="tap_to_set_up">Tap to set up</string>
+    <string name="tap_to_sign_up">Tap to sign up</string>
     <!-- Package name for Settings app-->
     <string name="settings_package" translatable="false">com.android.settings</string>
     <!-- Package name for Certinstaller app-->
@@ -129,78 +129,16 @@
     <!-- Status message of Wi-Fi when an available network is a carrier network. [CHAR LIMIT=NONE] -->
     <string name="available_via_carrier">Available via %1$s</string>
 
-    <!-- Status message of OSU Provider on receiving OSU_FAILURE_AP_CONNECTION. [CHAR LIMIT=NONE] -->
-    <string name="osu_failure_ap_connection">Connection failed</string>
-    <!-- Status message of OSU Provider on receiving OSU_FAILURE_SERVER_URL_INVALID. [CHAR LIMIT=NONE] -->
-    <string name="osu_failure_server_url_invalid">Invalid OSU server URL</string>
-    <!-- Status message of OSU Provider on receiving OSU_FAILURE_SERVER_CONNECTION. [CHAR LIMIT=NONE] -->
-    <string name="osu_failure_server_connection">OSU server connection failed</string>
-    <!-- Status message of OSU Provider on receiving OSU_FAILURE_SERVER_VALIDATION. [CHAR LIMIT=NONE] -->
-    <string name="osu_failure_server_validation">OSU server validation failed</string>
-    <!-- Status message of OSU Provider on receiving OSU_FAILURE_SERVICE_PROVIDER_VERIFICATION. [CHAR LIMIT=NONE] -->
-    <string name="osu_failure_service_provider_verification">Invalid OSU server certificate</string>
-    <!-- Status message of OSU Provider on receiving OSU_FAILURE_PROVISIONING_ABORTED. [CHAR LIMIT=NONE] -->
-    <string name="osu_failure_provisioning_aborted">Provisioning aborted</string>
-    <!-- Status message of OSU Provider on receiving OSU_FAILURE_PROVISIONING_NOT_AVAILABLE. [CHAR LIMIT=NONE] -->
-    <string name="osu_failure_provisioning_not_available">Provisioning not available</string>
-    <!-- Status message of OSU Provider on receiving OSU_FAILURE_INVALID_SERVER_URL. [CHAR LIMIT=NONE] -->
-    <string name="osu_failure_invalid_server_url">Invalid OSU server URL</string>
-    <!-- Status message of OSU Provider on receiving OSU_FAILURE_UNEXPECTED_COMMAND_TYPE. [CHAR LIMIT=NONE] -->
-    <string name="osu_failure_unexpected_command_type">Unexpected command type</string>
-    <!-- Status message of OSU Provider on receiving OSU_FAILURE_UNEXPECTED_SOAP_MESSAGE_TYPE. [CHAR LIMIT=NONE] -->
-    <string name="osu_failure_unexpected_soap_message_type">Unexpected SOAP message type</string>
-    <!-- Status message of OSU Provider on receiving OSU_FAILURE_SOAP_MESSAGE_EXCHANGE. [CHAR LIMIT=NONE] -->
-    <string name="osu_failure_soap_message_exchange">SOAP message exchange failed</string>
-    <!-- Status message of OSU Provider on receiving OSU_FAILURE_START_REDIRECT_LISTENER. [CHAR LIMIT=NONE] -->
-    <string name="osu_failure_start_redirect_listener">Redirect listener failed to start</string>
-    <!-- Status message of OSU Provider on receiving OSU_FAILURE_TIMED_OUT_REDIRECT_LISTENER. [CHAR LIMIT=NONE] -->
-    <string name="osu_failure_timed_out_redirect_listener">Timed out waiting for redirect</string>
-    <!-- Status message of OSU Provider on receiving OSU_FAILURE_NO_OSU_ACTIVITY_FOUND. [CHAR LIMIT=NONE] -->
-    <string name="osu_failure_no_osu_activity_found">No OSU activity found</string>
-    <!-- Status message of OSU Provider on receiving OSU_FAILURE_UNEXPECTED_SOAP_MESSAGE_STATUS. [CHAR LIMIT=NONE] -->
-    <string name="osu_failure_unexpected_soap_message_status">Unexpected SOAP message status</string>
-    <!-- Status message of OSU Provider on receiving OSU_FAILURE_NO_PPS_MO. [CHAR LIMIT=NONE] -->
-    <string name="osu_failure_no_pps_mo">Failed to find PPS-MO</string>
-    <!-- Status message of OSU Provider on receiving OSU_FAILURE_NO_AAA_SERVER_TRUST_ROOT_NODE. [CHAR LIMIT=NONE] -->
-    <string name="osu_failure_no_aaa_server_trust_root_node">Failed to find trust root node for AAA server</string>
-    <!-- Status message of OSU Provider on receiving OSU_FAILURE_NO_REMEDIATION_SERVER_TRUST_ROOT_NODE. [CHAR LIMIT=NONE] -->
-    <string name="osu_failure_no_remediation_server_trust_root_node">Failed to find trust root node for remediation server</string>
-    <!-- Status message of OSU Provider on receiving OSU_FAILURE_NO_POLICY_SERVER_TRUST_ROOT_NODE. [CHAR LIMIT=NONE] -->
-    <string name="osu_failure_no_policy_server_trust_root_node">Failed to find trust root node for policy server</string>
-    <!-- Status message of OSU Provider on receiving OSU_FAILURE_RETRIEVE_TRUST_ROOT_CERTIFICATES. [CHAR LIMIT=NONE] -->
-    <string name="osu_failure_retrieve_trust_root_certificates">Failed to retrieve trust root certificates</string>
-    <!-- Status message of OSU Provider on receiving OSU_FAILURE_NO_AAA_TRUST_ROOT_CERTIFICATE. [CHAR LIMIT=NONE] -->
-    <string name="osu_failure_no_aaa_trust_root_certificate">Failed to find trust root certificate for AAA server</string>
-    <!-- Status message of OSU Provider on receiving OSU_FAILURE_ADD_PASSPOINT_CONFIGURATION. [CHAR LIMIT=NONE] -->
-    <string name="osu_failure_add_passpoint_configuration">Failed to add PassPoint configuration</string>
-    <!-- Status message of OSU Provider on receiving OSU_FAILURE_OSU_PROVIDER_NOT_FOUND. [CHAR LIMIT=NONE] -->
-    <string name="osu_failure_osu_provider_not_found">Failed to find an OSU provider</string>
-
-    <!-- Status message of OSU Provider on receiving OSU_STATUS_AP_CONNECTING. [CHAR LIMIT=NONE] -->
-    <string name="osu_status_ap_connecting">Connecting</string>
-    <!-- Status message of OSU Provider on receiving OSU_STATUS_AP_CONNECTED. [CHAR LIMIT=NONE] -->
-    <string name="osu_status_ap_connected">Connected</string>
-    <!-- Status message of OSU Provider on receiving OSU_STATUS_SERVER_CONNECTING. [CHAR LIMIT=NONE] -->
-    <string name="osu_status_server_connecting">Connecting to OSU server</string>
-    <!-- Status message of OSU Provider on receiving OSU_STATUS_SERVER_VALIDATED. [CHAR LIMIT=NONE] -->
-    <string name="osu_status_server_validated">OSU server validated</string>
-    <!-- Status message of OSU Provider on receiving OSU_STATUS_SERVER_CONNECTED. [CHAR LIMIT=NONE] -->
-    <string name="osu_status_server_connected">Connected to OSU server</string>
-    <!-- Status message of OSU Provider on receiving OSU_STATUS_INIT_SOAP_EXCHANGE. [CHAR LIMIT=NONE] -->
-    <string name="osu_status_init_soap_exchange">Initial SOAP exchange</string>
-    <!-- Status message of OSU Provider on receiving OSU_STATUS_WAITING_FOR_REDIRECT_RESPONSE. [CHAR LIMIT=NONE] -->
-    <string name="osu_status_waiting_for_redirect_response">Waiting for redirect response</string>
-    <!-- Status message of OSU Provider on receiving OSU_STATUS_REDIRECT_RESPONSE_RECEIVED. [CHAR LIMIT=NONE] -->
-    <string name="osu_status_redirect_response_received">Received redirect response</string>
-    <!-- Status message of OSU Provider on receiving OSU_STATUS_SECOND_SOAP_EXCHANGE. [CHAR LIMIT=NONE] -->
-    <string name="osu_status_second_soap_exchange">Second SOAP exchange</string>
-    <!-- Status message of OSU Provider on receiving OSU_STATUS_THIRD_SOAP_EXCHANGE. [CHAR LIMIT=NONE] -->
-    <string name="osu_status_third_soap_exchange">Third SOAP exchange</string>
-    <!-- Status message of OSU Provider on receiving OSU_STATUS_RETRIEVING_TRUST_ROOT_CERTS. [CHAR LIMIT=NONE] -->
-    <string name="osu_status_retrieving_trust_root_certs">Retrieving trust root certificates</string>
-
+    <!-- Status message of OSU Provider upon initiating provisioning flow [CHAR LIMIT=NONE] -->
+    <string name="osu_opening_provider">Opening <xliff:g id="passpointProvider" example="Passpoint Provider">%1$s</xliff:g></string>
+    <!-- Status message of OSU Provider when connection fails [CHAR LIMIT=NONE] -->
+    <string name="osu_connect_failed">Couldn\u2019t connect</string>
+    <!-- Status message of OSU Provider after user completes provisioning flow [CHAR LIMIT=NONE] -->
+    <string name="osu_completing_sign_up">Completing sign-up\u2026</string>
+    <!-- Status message of OSU Provider when sign up could not be completed [CHAR LIMIT=NONE] -->
+    <string name="osu_sign_up_failed">Couldn\u2019t complete sign-up. Tap to try again.</string>
     <!-- Status message of OSU Provider on completing provisioning. [CHAR LIMIT=NONE] -->
-    <string name="osu_provisioning_complete">Provisioning complete</string>
+    <string name="osu_sign_up_complete">Sign-up complete. Connecting\u2026</string>
 
     <!-- Speed label for very slow network speed -->
     <string name="speed_label_very_slow">Very Slow</string>
diff --git a/packages/SettingsLib/src/com/android/settingslib/wifi/AccessPoint.java b/packages/SettingsLib/src/com/android/settingslib/wifi/AccessPoint.java
index c74d9ff..ed8b487 100644
--- a/packages/SettingsLib/src/com/android/settingslib/wifi/AccessPoint.java
+++ b/packages/SettingsLib/src/com/android/settingslib/wifi/AccessPoint.java
@@ -888,13 +888,13 @@
 
         if (isOsuProvider()) {
             if (mOsuProvisioningComplete) {
-                summary.append(mContext.getString(R.string.osu_provisioning_complete));
+                summary.append(mContext.getString(R.string.osu_sign_up_complete));
             } else if (mOsuFailure != null) {
                 summary.append(mOsuFailure);
             } else if (mOsuStatus != null) {
                 summary.append(mOsuStatus);
             } else {
-                summary.append(mContext.getString(R.string.tap_to_set_up));
+                summary.append(mContext.getString(R.string.tap_to_sign_up));
             }
         } else if (isActive()) {
             if (isPasspoint()) {
@@ -1554,91 +1554,12 @@
      * All methods are invoked on the Main Thread
      */
     private class AccessPointProvisioningCallback extends ProvisioningCallback {
-        // TODO: Remove logs and implement summary changing logic for these provisioning callbacks.
         @Override
         @MainThread public void onProvisioningFailure(int status) {
-            switch (status) {
-                case OSU_FAILURE_AP_CONNECTION:
-                    mOsuFailure = mContext.getString(R.string.osu_failure_ap_connection);
-                    break;
-                case OSU_FAILURE_SERVER_URL_INVALID:
-                    mOsuFailure = mContext.getString(R.string.osu_failure_server_url_invalid);
-                    break;
-                case OSU_FAILURE_SERVER_CONNECTION:
-                    mOsuFailure = mContext.getString(R.string.osu_failure_server_connection);
-                    break;
-                case OSU_FAILURE_SERVER_VALIDATION:
-                    mOsuFailure = mContext.getString(R.string.osu_failure_server_validation);
-                    break;
-                case OSU_FAILURE_SERVICE_PROVIDER_VERIFICATION:
-                    mOsuFailure = mContext.getString(
-                            R.string.osu_failure_service_provider_verification);
-                    break;
-                case OSU_FAILURE_PROVISIONING_ABORTED:
-                    mOsuFailure = mContext.getString(R.string.osu_failure_provisioning_aborted);
-                    break;
-                case OSU_FAILURE_PROVISIONING_NOT_AVAILABLE:
-                    mOsuFailure = mContext.getString(
-                            R.string.osu_failure_provisioning_not_available);
-                    break;
-                case OSU_FAILURE_INVALID_URL_FORMAT_FOR_OSU:
-                    mOsuFailure = mContext.getString(R.string.osu_failure_invalid_server_url);
-                    break;
-                case OSU_FAILURE_UNEXPECTED_COMMAND_TYPE:
-                    mOsuFailure = mContext.getString(R.string.osu_failure_unexpected_command_type);
-                    break;
-                case OSU_FAILURE_UNEXPECTED_SOAP_MESSAGE_TYPE:
-                    mOsuFailure = mContext.getString(
-                            R.string.osu_failure_unexpected_soap_message_type);
-                    break;
-                case OSU_FAILURE_SOAP_MESSAGE_EXCHANGE:
-                    mOsuFailure = mContext.getString(R.string.osu_failure_soap_message_exchange);
-                    break;
-                case OSU_FAILURE_START_REDIRECT_LISTENER:
-                    mOsuFailure = mContext.getString(R.string.osu_failure_start_redirect_listener);
-                    break;
-                case OSU_FAILURE_TIMED_OUT_REDIRECT_LISTENER:
-                    mOsuFailure = mContext.getString(
-                            R.string.osu_failure_timed_out_redirect_listener);
-                    break;
-                case OSU_FAILURE_NO_OSU_ACTIVITY_FOUND:
-                    mOsuFailure = mContext.getString(R.string.osu_failure_no_osu_activity_found);
-                    break;
-                case OSU_FAILURE_UNEXPECTED_SOAP_MESSAGE_STATUS:
-                    mOsuFailure = mContext.getString(
-                            R.string.osu_failure_unexpected_soap_message_status);
-                    break;
-                case OSU_FAILURE_NO_PPS_MO:
-                    mOsuFailure = mContext.getString(
-                            R.string.osu_failure_no_pps_mo);
-                    break;
-                case OSU_FAILURE_NO_AAA_SERVER_TRUST_ROOT_NODE:
-                    mOsuFailure = mContext.getString(
-                            R.string.osu_failure_no_aaa_server_trust_root_node);
-                    break;
-                case OSU_FAILURE_NO_REMEDIATION_SERVER_TRUST_ROOT_NODE:
-                    mOsuFailure = mContext.getString(
-                            R.string.osu_failure_no_remediation_server_trust_root_node);
-                    break;
-                case OSU_FAILURE_NO_POLICY_SERVER_TRUST_ROOT_NODE:
-                    mOsuFailure = mContext.getString(
-                            R.string.osu_failure_no_policy_server_trust_root_node);
-                    break;
-                case OSU_FAILURE_RETRIEVE_TRUST_ROOT_CERTIFICATES:
-                    mOsuFailure = mContext.getString(
-                            R.string.osu_failure_retrieve_trust_root_certificates);
-                    break;
-                case OSU_FAILURE_NO_AAA_TRUST_ROOT_CERTIFICATE:
-                    mOsuFailure = mContext.getString(
-                            R.string.osu_failure_no_aaa_trust_root_certificate);
-                    break;
-                case OSU_FAILURE_ADD_PASSPOINT_CONFIGURATION:
-                    mOsuFailure = mContext.getString(
-                            R.string.osu_failure_add_passpoint_configuration);
-                    break;
-                case OSU_FAILURE_OSU_PROVIDER_NOT_FOUND:
-                    mOsuFailure = mContext.getString(R.string.osu_failure_osu_provider_not_found);
-                    break;
+            if (TextUtils.equals(mOsuStatus, mContext.getString(R.string.osu_completing_sign_up))) {
+                mOsuFailure = mContext.getString(R.string.osu_sign_up_failed);
+            } else {
+                mOsuFailure = mContext.getString(R.string.osu_connect_failed);
             }
             mOsuStatus = null;
             mOsuProvisioningComplete = false;
@@ -1651,50 +1572,37 @@
 
         @Override
         @MainThread public void onProvisioningStatus(int status) {
+            String newStatus = null;
             switch (status) {
                 case OSU_STATUS_AP_CONNECTING:
-                    mOsuStatus = mContext.getString(R.string.osu_status_ap_connecting);
-                    break;
                 case OSU_STATUS_AP_CONNECTED:
-                    mOsuStatus = mContext.getString(R.string.osu_status_ap_connected);
-                    break;
                 case OSU_STATUS_SERVER_CONNECTING:
-                    mOsuStatus = mContext.getString(R.string.osu_status_server_connecting);
-                    break;
                 case OSU_STATUS_SERVER_VALIDATED:
-                    mOsuStatus = mContext.getString(R.string.osu_status_server_validated);
-                    break;
                 case OSU_STATUS_SERVER_CONNECTED:
-                    mOsuStatus = mContext.getString(R.string.osu_status_server_connected);
-                    break;
                 case OSU_STATUS_INIT_SOAP_EXCHANGE:
-                    mOsuStatus = mContext.getString(R.string.osu_status_init_soap_exchange);
-                    break;
                 case OSU_STATUS_WAITING_FOR_REDIRECT_RESPONSE:
-                    mOsuStatus = mContext.getString(
-                            R.string.osu_status_waiting_for_redirect_response);
+                    newStatus = String.format(mContext.getString(R.string.osu_opening_provider),
+                            mOsuProvider.getFriendlyName());
                     break;
                 case OSU_STATUS_REDIRECT_RESPONSE_RECEIVED:
-                    mOsuStatus = mContext.getString(R.string.osu_status_redirect_response_received);
-                    break;
                 case OSU_STATUS_SECOND_SOAP_EXCHANGE:
-                    mOsuStatus = mContext.getString(R.string.osu_status_second_soap_exchange);
-                    break;
                 case OSU_STATUS_THIRD_SOAP_EXCHANGE:
-                    mOsuStatus = mContext.getString(R.string.osu_status_third_soap_exchange);
-                    break;
                 case OSU_STATUS_RETRIEVING_TRUST_ROOT_CERTS:
-                    mOsuStatus = mContext.getString(
-                            R.string.osu_status_retrieving_trust_root_certs);
+                    newStatus = mContext.getString(
+                            R.string.osu_completing_sign_up);
                     break;
             }
+            boolean updated = !TextUtils.equals(mOsuStatus, newStatus);
+            mOsuStatus = newStatus;
             mOsuFailure = null;
             mOsuProvisioningComplete = false;
-            ThreadUtils.postOnMainThread(() -> {
-                if (mAccessPointListener != null) {
-                    mAccessPointListener.onAccessPointChanged(AccessPoint.this);
-                }
-            });
+            if (updated) {
+                ThreadUtils.postOnMainThread(() -> {
+                    if (mAccessPointListener != null) {
+                        mAccessPointListener.onAccessPointChanged(AccessPoint.this);
+                    }
+                });
+            }
         }
 
         @Override
diff --git a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java b/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
index 4453121..23e5f0e 100644
--- a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
+++ b/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
@@ -4231,7 +4231,7 @@
 
                         String defLocationMode = Integer.toString(
                                 !TextUtils.isEmpty(locationProvidersAllowed.getValue())
-                                        ? Secure.LOCATION_MODE_HIGH_ACCURACY
+                                        ? Secure.LOCATION_MODE_ON
                                         : Secure.LOCATION_MODE_OFF);
                         secureSettings.insertSettingLocked(
                                 Secure.LOCATION_MODE, defLocationMode,
diff --git a/packages/Shell/AndroidManifest.xml b/packages/Shell/AndroidManifest.xml
index c3c3f25..c032683 100644
--- a/packages/Shell/AndroidManifest.xml
+++ b/packages/Shell/AndroidManifest.xml
@@ -145,6 +145,7 @@
     <uses-permission android:name="android.permission.SET_TIME_ZONE" />
     <uses-permission android:name="android.permission.DISABLE_HIDDEN_API_CHECKS" />
     <uses-permission android:name="android.permission.MANAGE_ROLE_HOLDERS" />
+    <uses-permission android:name="android.permission.STATUS_BAR_SERVICE" />
     <!-- Permission needed to rename bugreport notifications (so they're not shown as Shell) -->
     <uses-permission android:name="android.permission.SUBSTITUTE_NOTIFICATION_APP_NAME" />
     <!-- Permission needed to hold a wakelock in dumpstate.cpp (drop_root_user()) -->
diff --git a/packages/SystemUI/AndroidManifest.xml b/packages/SystemUI/AndroidManifest.xml
index a18eb76..815ae9a 100644
--- a/packages/SystemUI/AndroidManifest.xml
+++ b/packages/SystemUI/AndroidManifest.xml
@@ -22,6 +22,11 @@
         android:sharedUserId="android.uid.systemui"
         coreApp="true">
 
+    <!-- Using OpenGL ES 2.0 -->
+    <uses-feature
+        android:glEsVersion="0x00020000"
+        android:required="true" />
+
     <!-- SysUI must be the one to define this permission; its name is
          referenced by the core OS. -->
     <permission android:name="android.permission.systemui.IDENTITY"
diff --git a/packages/SystemUI/OWNERS b/packages/SystemUI/OWNERS
index 2f6e32b..e7e2c1a 100644
--- a/packages/SystemUI/OWNERS
+++ b/packages/SystemUI/OWNERS
@@ -1,6 +1,6 @@
 set noparent
 
-dsandler@google.com
+dsandler@android.com
 
 adamcohen@google.com
 asc@google.com
diff --git a/packages/SystemUI/plugin/src/com/android/systemui/plugins/ActivityStarter.java b/packages/SystemUI/plugin/src/com/android/systemui/plugins/ActivityStarter.java
index 5317a6d..66d5d11 100644
--- a/packages/SystemUI/plugin/src/com/android/systemui/plugins/ActivityStarter.java
+++ b/packages/SystemUI/plugin/src/com/android/systemui/plugins/ActivityStarter.java
@@ -27,7 +27,7 @@
  */
 @ProvidesInterface(version = ActivityStarter.VERSION)
 public interface ActivityStarter {
-    int VERSION = 1;
+    int VERSION = 2;
 
     void startPendingIntentDismissingKeyguard(PendingIntent intent);
 
@@ -37,6 +37,11 @@
      */
     void startPendingIntentDismissingKeyguard(PendingIntent intent,
             Runnable intentSentUiThreadCallback);
+
+    /**
+     * The intent flag can be specified in startActivity().
+     */
+    void startActivity(Intent intent, boolean onlyProvisioned, boolean dismissShade, int flags);
     void startActivity(Intent intent, boolean dismissShade);
     void startActivity(Intent intent, boolean onlyProvisioned, boolean dismissShade);
     void startActivity(Intent intent, boolean dismissShade, Callback callback);
diff --git a/packages/SystemUI/res-keyguard/values/strings.xml b/packages/SystemUI/res-keyguard/values/strings.xml
index 7432f9c..41acf82 100644
--- a/packages/SystemUI/res-keyguard/values/strings.xml
+++ b/packages/SystemUI/res-keyguard/values/strings.xml
@@ -405,7 +405,7 @@
     <!-- Header for typographic clock face. [CHAR LIMIT=8] -->
     <string name="type_clock_header">It\u2019s</string>
 
-    <!-- Hour displayed in words on the typographic clock face. [CHAR LIMIT=8] -->
+    <!-- Hour displayed in words on the typographic clock face. [CHAR LIMIT=12] -->
     <string-array name="type_clock_hours">
         <item>Twelve</item>
         <item>One</item>
@@ -421,7 +421,7 @@
         <item>Eleven</item>
     </string-array>
 
-    <!-- Minutes displayed in words on the typographic clock face. [CHAR LIMIT=16] -->
+    <!-- Minutes displayed in words on the typographic clock face. [CHAR LIMIT=20] -->
     <string-array name="type_clock_minutes">
         <item>O\u2019Clock</item>
         <item>O\u2019One</item>
diff --git a/packages/SystemUI/res/drawable/ic_open_in_new.xml b/packages/SystemUI/res/drawable/ic_open_in_new.xml
new file mode 100644
index 0000000..3d53871
--- /dev/null
+++ b/packages/SystemUI/res/drawable/ic_open_in_new.xml
@@ -0,0 +1,23 @@
+<!-- Copyright (C) 2019 The Android Open Source Project
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at"+
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="24dp"
+        android:height="24dp"
+        android:viewportWidth="24"
+        android:viewportHeight="24">
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M19,19H5V5h7V3H5c-1.11,0 -2,0.9 -2,2v14c0,1.1 0.89,2 2,2h14c1.1,0 2,-0.9 2,-2v-7h-2v7zM14,3v2h3.59l-9.83,9.83 1.41,1.41L19,6.41V10h2V3h-7z"/>
+</vector>
diff --git a/packages/SystemUI/res/layout/bubble_expanded_view.xml b/packages/SystemUI/res/layout/bubble_expanded_view.xml
index 1aeb52c..f0d2b2e 100644
--- a/packages/SystemUI/res/layout/bubble_expanded_view.xml
+++ b/packages/SystemUI/res/layout/bubble_expanded_view.xml
@@ -18,6 +18,7 @@
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_height="wrap_content"
     android:layout_width="match_parent"
+    android:orientation="vertical"
     android:id="@+id/bubble_expanded_view">
 
     <View
@@ -26,17 +27,48 @@
         android:layout_height="@dimen/bubble_pointer_height"
     />
 
-    <TextView
-        android:id="@+id/bubble_content_header"
-        android:background="@drawable/bubble_expanded_header_bg"
-        android:textAppearance="@*android:style/TextAppearance.Material.Title"
-        android:textSize="18sp"
-        android:layout_width="match_parent"
+    <LinearLayout
+        android:id="@+id/header_layout"
         android:layout_height="@dimen/bubble_expanded_header_height"
-        android:gravity="start|center_vertical"
-        android:singleLine="true"
-        android:paddingLeft="@dimen/bubble_expanded_header_horizontal_padding"
-        android:paddingRight="@dimen/bubble_expanded_header_horizontal_padding"
-    />
+        android:layout_width="match_parent"
+        android:orientation="horizontal"
+        android:background="@drawable/bubble_expanded_header_bg">
+
+        <TextView
+            android:id="@+id/header_text"
+            android:textAppearance="@*android:style/TextAppearance.Material.Title"
+            android:textSize="18sp"
+            android:layout_weight="1"
+            android:layout_width="0dp"
+            android:layout_height="match_parent"
+            android:gravity="start|center_vertical"
+            android:singleLine="true"
+            android:paddingLeft="@dimen/bubble_expanded_header_horizontal_padding"
+            android:paddingRight="@dimen/bubble_expanded_header_horizontal_padding"
+        />
+
+        <ImageButton
+            android:id="@+id/deep_link_button"
+            android:layout_width="@dimen/bubble_header_icon_size"
+            android:layout_height="@dimen/bubble_header_icon_size"
+            android:gravity="end|center_vertical"
+            android:src="@drawable/ic_open_in_new"
+            android:scaleType="center"
+            android:tint="?android:attr/colorForeground"
+            android:background="?android:attr/selectableItemBackground"
+        />
+
+        <ImageButton
+            android:id="@id/settings_button"
+            android:layout_width="@dimen/bubble_header_icon_size"
+            android:layout_height="@dimen/bubble_header_icon_size"
+            android:src="@drawable/ic_settings"
+            android:gravity="end|center_vertical"
+            android:scaleType="center"
+            android:tint="?android:attr/colorForeground"
+            android:background="?android:attr/selectableItemBackground"
+        />
+
+    </LinearLayout>
 
 </com.android.systemui.bubbles.BubbleExpandedViewContainer>
diff --git a/packages/SystemUI/res/raw/image_wallpaper_fragment_shader.glsl b/packages/SystemUI/res/raw/image_wallpaper_fragment_shader.glsl
new file mode 100644
index 0000000..11d73a9
--- /dev/null
+++ b/packages/SystemUI/res/raw/image_wallpaper_fragment_shader.glsl
@@ -0,0 +1,33 @@
+precision mediump float;
+
+uniform sampler2D uTexture;
+uniform float uCenterReveal;
+uniform float uReveal;
+uniform float uAod2Opacity;
+uniform int uAodMode;
+varying vec2 vTextureCoordinates;
+
+vec3 luminosity(vec3 color) {
+    float lum = 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b;
+    return vec3(lum);
+}
+
+vec4 transform(vec3 diffuse) {
+    // TODO: Add well comments here, tracking on b/123615467.
+    vec3 lum = luminosity(diffuse);
+    diffuse = mix(diffuse, lum, smoothstep(0., uCenterReveal, uReveal));
+    float val = mix(uReveal, uCenterReveal, step(uCenterReveal, uReveal));
+    diffuse = smoothstep(val, 1.0, diffuse);
+    diffuse *= uAod2Opacity * (1. - smoothstep(uCenterReveal, 1., uReveal));
+    return vec4(diffuse.r, diffuse.g, diffuse.b, 1.);
+}
+
+void main() {
+    vec4 fragColor = texture2D(uTexture, vTextureCoordinates);
+    // TODO: Remove the branch logic here, tracking on b/123615467.
+    if (uAodMode != 0) {
+        gl_FragColor = transform(fragColor.rgb);
+    } else {
+        gl_FragColor = fragColor;
+    }
+}
\ No newline at end of file
diff --git a/packages/SystemUI/res/raw/image_wallpaper_vertex_shader.glsl b/packages/SystemUI/res/raw/image_wallpaper_vertex_shader.glsl
new file mode 100644
index 0000000..4393e2b
--- /dev/null
+++ b/packages/SystemUI/res/raw/image_wallpaper_vertex_shader.glsl
@@ -0,0 +1,8 @@
+attribute vec4 aPosition;
+attribute vec2 aTextureCoordinates;
+varying vec2 vTextureCoordinates;
+
+void main() {
+    vTextureCoordinates = aTextureCoordinates;
+    gl_Position = aPosition;
+}
\ No newline at end of file
diff --git a/packages/SystemUI/res/values/dimens.xml b/packages/SystemUI/res/values/dimens.xml
index d435c67..8f5effb 100644
--- a/packages/SystemUI/res/values/dimens.xml
+++ b/packages/SystemUI/res/values/dimens.xml
@@ -1030,4 +1030,6 @@
     <dimen name="bubble_stack_offscreen">5dp</dimen>
     <!-- How far down the screen the stack starts. -->
     <dimen name="bubble_stack_starting_offset_y">100dp</dimen>
+    <!-- Size of image buttons in the bubble header -->
+    <dimen name="bubble_header_icon_size">48dp</dimen>
 </resources>
diff --git a/packages/SystemUI/res/values/strings.xml b/packages/SystemUI/res/values/strings.xml
index ffa5de8..56d9bf4c 100644
--- a/packages/SystemUI/res/values/strings.xml
+++ b/packages/SystemUI/res/values/strings.xml
@@ -2309,7 +2309,7 @@
     <string name="ongoing_privacy_dialog_ok">Got it</string>
 
     <!-- Action on Ongoing Privacy Dialog to open privacy hub [CHAR LIMIT=20]-->
-    <string name="ongoing_privacy_dialog_open_settings">View details</string>
+    <string name="ongoing_privacy_dialog_open_settings">Privacy settings</string>
 
     <!-- Text for item in Ongoing Privacy Dialog title when only one app is using app ops [CHAR LIMIT=NONE] -->
     <string name="ongoing_privacy_dialog_single_app_title">App using your <xliff:g id="types_list" example="camera( and location)">%s</xliff:g></string>
@@ -2346,4 +2346,12 @@
 
     <!-- What to show on the ambient display player when song doesn't have a title. [CHAR LIMIT=20] -->
     <string name="music_controls_no_title">No title</string>
+
+    <!-- Text used for content description of deep link button in the header of expanded bubble
+         view. [CHAR_LIMIT=NONE] -->
+    <string name="bubbles_deep_link_button_description">Open <xliff:g id="app_name" example="YouTube">%1$s</xliff:g></string>
+    <!-- Text used for content description of settings button in the header of expanded bubble
+         view. [CHAR_LIMIT=NONE] -->
+    <string name="bubbles_settings_button_description">Open notification settings for <xliff:g id="app_name" example="YouTube">%1$s</xliff:g></string>
+
 </resources>
diff --git a/packages/SystemUI/shared/src/com/android/systemui/shared/recents/model/ThumbnailData.java b/packages/SystemUI/shared/src/com/android/systemui/shared/recents/model/ThumbnailData.java
index f84c72e..1413ac1 100644
--- a/packages/SystemUI/shared/src/com/android/systemui/shared/recents/model/ThumbnailData.java
+++ b/packages/SystemUI/shared/src/com/android/systemui/shared/recents/model/ThumbnailData.java
@@ -17,11 +17,13 @@
 package com.android.systemui.shared.recents.model;
 
 import static android.content.res.Configuration.ORIENTATION_UNDEFINED;
+
 import static com.android.systemui.shared.system.WindowManagerWrapper.WINDOWING_MODE_UNDEFINED;
 
 import android.app.ActivityManager.TaskSnapshot;
 import android.graphics.Bitmap;
 import android.graphics.Rect;
+import android.hardware.HardwareBuffer;
 
 /**
  * Data for a single thumbnail.
@@ -51,7 +53,9 @@
     }
 
     public ThumbnailData(TaskSnapshot snapshot) {
-        thumbnail = Bitmap.createHardwareBitmap(snapshot.getSnapshot());
+        thumbnail = Bitmap.wrapHardwareBuffer(
+                HardwareBuffer.createFromGraphicBuffer(snapshot.getSnapshot()),
+                snapshot.getColorSpace());
         insets = new Rect(snapshot.getContentInsets());
         orientation = snapshot.getOrientation();
         reducedResolution = snapshot.isReducedResolution();
diff --git a/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java b/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java
index bcd41a0..0970c21 100644
--- a/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java
+++ b/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java
@@ -99,6 +99,7 @@
 import java.util.List;
 import java.util.Map.Entry;
 import java.util.TimeZone;
+import java.util.function.Consumer;
 
 /**
  * Watches for updates that may be interesting to the keyguard, and provides
@@ -218,7 +219,8 @@
     // Battery status
     private BatteryStatus mBatteryStatus;
 
-    private final StrongAuthTracker mStrongAuthTracker;
+    @VisibleForTesting
+    protected StrongAuthTracker mStrongAuthTracker;
 
     private final ArrayList<WeakReference<KeyguardUpdateMonitorCallback>>
             mCallbacks = Lists.newArrayList();
@@ -344,8 +346,7 @@
                     handleUserUnlocked();
                     break;
                 case MSG_ASSISTANT_STACK_CHANGED:
-                    mAssistantVisible = (boolean)msg.obj;
-                    updateBiometricListeningState();
+                    setAssistantVisible((boolean) msg.obj);
                     break;
                 case MSG_BIOMETRIC_AUTHENTICATION_CONTINUE:
                     updateBiometricListeningState();
@@ -354,7 +355,7 @@
                     updateLogoutEnabled();
                     break;
                 case MSG_TELEPHONY_CAPABLE:
-                    updateTelephonyCapable((boolean)msg.obj);
+                    updateTelephonyCapable((boolean) msg.obj);
                     break;
                 default:
                     super.handleMessage(msg);
@@ -538,7 +539,8 @@
         }
     }
 
-    private void onFingerprintAuthenticated(int userId) {
+    @VisibleForTesting
+    protected void onFingerprintAuthenticated(int userId) {
         Trace.beginSection("KeyGuardUpdateMonitor#onFingerPrintAuthenticated");
         mUserFingerprintAuthenticated.put(userId, true);
         // Update/refresh trust state only if user can skip bouncer
@@ -693,7 +695,8 @@
         }
     }
 
-    private void onFaceAuthenticated(int userId) {
+    @VisibleForTesting
+    protected void onFaceAuthenticated(int userId) {
         Trace.beginSection("KeyGuardUpdateMonitor#onFaceAuthenticated");
         mUserFaceAuthenticated.put(userId, true);
         // Update/refresh trust state only if user can skip bouncer
@@ -898,8 +901,9 @@
 
 
     public boolean getUserCanSkipBouncer(int userId) {
-        return getUserHasTrust(userId) || (mUserFingerprintAuthenticated.get(userId)
-                && isUnlockingWithBiometricAllowed());
+        boolean fingerprintOrFace = mUserFingerprintAuthenticated.get(userId)
+                || mUserFaceAuthenticated.get(userId);
+        return getUserHasTrust(userId) || (fingerprintOrFace && isUnlockingWithBiometricAllowed());
     }
 
     public boolean getUserHasTrust(int userId) {
@@ -950,6 +954,12 @@
         }
     }
 
+    @VisibleForTesting
+    void setAssistantVisible(boolean assistantVisible) {
+        mAssistantVisible = assistantVisible;
+        updateBiometricListeningState();
+    }
+
     static class DisplayClientState {
         public int clientGeneration;
         public boolean clearing;
@@ -1047,7 +1057,8 @@
         }
     };
 
-    private final BroadcastReceiver mBroadcastAllReceiver = new BroadcastReceiver() {
+    @VisibleForTesting
+    protected final BroadcastReceiver mBroadcastAllReceiver = new BroadcastReceiver() {
 
         @Override
         public void onReceive(Context context, Intent intent) {
@@ -1121,7 +1132,8 @@
         }
     };
 
-    private FaceManager.AuthenticationCallback mFaceAuthenticationCallback
+    @VisibleForTesting
+    FaceManager.AuthenticationCallback mFaceAuthenticationCallback
             = new FaceManager.AuthenticationCallback() {
 
         @Override
@@ -1298,9 +1310,13 @@
         }
     }
 
-    public class StrongAuthTracker extends LockPatternUtils.StrongAuthTracker {
-        public StrongAuthTracker(Context context) {
+    public static class StrongAuthTracker extends LockPatternUtils.StrongAuthTracker {
+        private final Consumer<Integer> mStrongAuthRequiredChangedCallback;
+
+        public StrongAuthTracker(Context context,
+                Consumer<Integer> strongAuthRequiredChangedCallback) {
             super(context);
+            mStrongAuthRequiredChangedCallback = strongAuthRequiredChangedCallback;
         }
 
         public boolean isUnlockingWithBiometricAllowed() {
@@ -1316,7 +1332,7 @@
 
         @Override
         public void onStrongAuthRequiredChanged(int userId) {
-            notifyStrongAuthStateChanged(userId);
+            mStrongAuthRequiredChangedCallback.accept(userId);
         }
     }
 
@@ -1423,7 +1439,7 @@
         mContext = context;
         mSubscriptionManager = SubscriptionManager.from(context);
         mDeviceProvisioned = isDeviceProvisionedInSettingsDb();
-        mStrongAuthTracker = new StrongAuthTracker(context);
+        mStrongAuthTracker = new StrongAuthTracker(context, this::notifyStrongAuthStateChanged);
 
         // Since device can't be un-provisioned, we only need to register a content observer
         // to update mDeviceProvisioned when we are...
@@ -1548,6 +1564,14 @@
         }
     }
 
+    /**
+     * Request passive authentication, when sensors detect that a user might be present.
+     */
+    public void onAuthInterruptDetected() {
+        if (DEBUG) Log.d(TAG, "onAuthInterruptDetected()");
+        updateFaceListeningState();
+    }
+
     private void updateFaceListeningState() {
         // If this message exists, we should not authenticate again until this message is
         // consumed by the handler
@@ -1585,11 +1609,13 @@
     }
 
     private boolean shouldListenForFace() {
-        return (mKeyguardIsVisible || !mDeviceInteractive ||
-                (mBouncer && !mKeyguardGoingAway) || mGoingToSleep ||
-                shouldListenForFaceAssistant() || (mKeyguardOccluded && mIsDreaming))
-                && !mSwitchingUser && !isFaceDisabled(getCurrentUser())
-                && !mKeyguardGoingAway && !mFaceLockedOut && mFaceSettingEnabledForUser;
+        final boolean awakeKeyguard = mKeyguardIsVisible && mDeviceInteractive && !mGoingToSleep;
+        final int user = getCurrentUser();
+
+        return (mBouncer || awakeKeyguard || shouldListenForFaceAssistant())
+                && !mSwitchingUser && !getUserCanSkipBouncer(user) && !isFaceDisabled(user)
+                && !mKeyguardGoingAway && !mFaceLockedOut && mFaceSettingEnabledForUser
+                && mUserManager.isUserUnlocked(user);
     }
 
 
diff --git a/packages/SystemUI/src/com/android/systemui/ActivityStarterDelegate.java b/packages/SystemUI/src/com/android/systemui/ActivityStarterDelegate.java
index b461f69..04f887b 100644
--- a/packages/SystemUI/src/com/android/systemui/ActivityStarterDelegate.java
+++ b/packages/SystemUI/src/com/android/systemui/ActivityStarterDelegate.java
@@ -53,6 +53,15 @@
     }
 
     @Override
+    public void startActivity(Intent intent, boolean onlyProvisioned, boolean dismissShade,
+            int flags) {
+        if (mActualStarter == null) {
+            return;
+        }
+        mActualStarter.startActivity(intent, onlyProvisioned, dismissShade, flags);
+    }
+
+    @Override
     public void startActivity(Intent intent, boolean dismissShade) {
         if (mActualStarter == null) {
             return;
diff --git a/packages/SystemUI/src/com/android/systemui/ImageWallpaper.java b/packages/SystemUI/src/com/android/systemui/ImageWallpaper.java
index 2aecc24..5040942 100644
--- a/packages/SystemUI/src/com/android/systemui/ImageWallpaper.java
+++ b/packages/SystemUI/src/com/android/systemui/ImageWallpaper.java
@@ -19,8 +19,11 @@
 import static android.view.Display.DEFAULT_DISPLAY;
 
 import android.app.WallpaperManager;
+import android.content.BroadcastReceiver;
 import android.content.ComponentCallbacks2;
 import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
 import android.graphics.RecordingCanvas;
@@ -28,7 +31,9 @@
 import android.graphics.RectF;
 import android.graphics.Region.Op;
 import android.hardware.display.DisplayManager;
+import android.opengl.GLSurfaceView;
 import android.os.AsyncTask;
+import android.os.Build;
 import android.os.Handler;
 import android.os.Trace;
 import android.service.wallpaper.WallpaperService;
@@ -39,6 +44,7 @@
 import android.view.SurfaceHolder;
 
 import com.android.internal.annotations.VisibleForTesting;
+import com.android.systemui.glwallpaper.ImageWallpaperRenderer;
 
 import java.io.FileDescriptor;
 import java.io.IOException;
@@ -51,12 +57,17 @@
 public class ImageWallpaper extends WallpaperService {
     private static final String TAG = "ImageWallpaper";
     private static final String GL_LOG_TAG = "ImageWallpaperGL";
+    // TODO: Testing purpose, need to remove later, b/123616712.
+    private static final String SENSOR_EVENT_AWAKE = "systemui.test.event.awake";
+    // TODO: Testing purpose, need to remove later, b/123616712.
+    private static final String SENSOR_EVENT_SLEEP = "systemui.test.event.sleep";
     private static final boolean DEBUG = false;
     private static final String PROPERTY_KERNEL_QEMU = "ro.kernel.qemu";
     private static final long DELAY_FORGET_WALLPAPER = 5000;
 
     private WallpaperManager mWallpaperManager;
     private DrawableEngine mEngine;
+    private GLEngine mGlEngine;
 
     @Override
     public void onCreate() {
@@ -73,10 +84,112 @@
 
     @Override
     public Engine onCreateEngine() {
-        mEngine = new DrawableEngine();
-        return mEngine;
+        mGlEngine = new GLEngine(this);
+        return mGlEngine;
     }
 
+    class GLEngine extends Engine {
+        private GLWallpaperSurfaceView mWallpaperSurfaceView;
+
+        GLEngine(Context context) {
+            mWallpaperSurfaceView = new GLWallpaperSurfaceView(context);
+            mWallpaperSurfaceView.setRenderer(
+                    new ImageWallpaperRenderer(context, mWallpaperSurfaceView));
+            mWallpaperSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
+            setOffsetNotificationsEnabled(true);
+        }
+
+        @Override
+        public void onAmbientModeChanged(boolean inAmbientMode, long animationDuration) {
+            if (mWallpaperSurfaceView != null) {
+                mWallpaperSurfaceView.notifyAmbientModeChanged(inAmbientMode);
+            }
+        }
+
+        @Override
+        public void onOffsetsChanged(float xOffset, float yOffset, float xOffsetStep,
+                float yOffsetStep, int xPixelOffset, int yPixelOffset) {
+            if (mWallpaperSurfaceView != null) {
+                mWallpaperSurfaceView.notifyOffsetsChanged(xOffset, yOffset);
+            }
+        }
+
+        private class GLWallpaperSurfaceView extends GLSurfaceView implements ImageGLView {
+            private SensorEventListener mEventListener;
+            private WallpaperStatusListener mWallpaperChangedListener;
+
+            // TODO: Testing purpose, need to remove later, b/123616712.
+            /**
+             * For testing only: adb shell am broadcast -a <INTENT>
+             */
+            private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
+                @Override
+                public void onReceive(Context context, Intent intent) {
+                    if (intent == null) {
+                        return;
+                    }
+                    switch (intent.getAction()) {
+                        case SENSOR_EVENT_AWAKE:
+                            notifySensorEvents(true);
+                            break;
+                        case SENSOR_EVENT_SLEEP:
+                            notifySensorEvents(false);
+                            break;
+                    }
+                }
+            };
+
+            GLWallpaperSurfaceView(Context context) {
+                super(context);
+                setEGLContextClientVersion(2);
+                // TODO: Testing purpose, need to remove later, b/123616712.
+                if (Build.IS_DEBUGGABLE) {
+                    IntentFilter filter = new IntentFilter();
+                    filter.addAction(SENSOR_EVENT_AWAKE);
+                    filter.addAction(SENSOR_EVENT_SLEEP);
+                    registerReceiver(mReceiver, filter);
+                }
+            }
+
+            @Override
+            public SurfaceHolder getHolder() {
+                return getSurfaceHolder();
+            }
+
+            @Override
+            public void setRenderer(Renderer renderer) {
+                super.setRenderer(renderer);
+                mEventListener = (SensorEventListener) renderer;
+                mWallpaperChangedListener = (WallpaperStatusListener) renderer;
+            }
+
+            private void notifySensorEvents(boolean reach) {
+                if (mEventListener != null) {
+                    mEventListener.onSensorEvent(reach);
+                }
+            }
+
+            private void notifyAmbientModeChanged(boolean inAmbient) {
+                if (mWallpaperChangedListener != null) {
+                    mWallpaperChangedListener.onAmbientModeChanged(inAmbient);
+                }
+            }
+
+            private void notifyOffsetsChanged(float xOffset, float yOffset) {
+                if (mWallpaperChangedListener != null) {
+                    mWallpaperChangedListener.onOffsetsChanged(
+                            xOffset, yOffset, getHolder().getSurfaceFrame());
+                }
+            }
+
+            @Override
+            public void render() {
+                requestRender();
+            }
+        }
+    }
+
+    // TODO: Remove this engine, tracking on b/123617158.
     class DrawableEngine extends Engine {
         private final Runnable mUnloadWallpaperCallback = () -> {
             unloadWallpaper(false /* forgetSize */);
@@ -564,4 +677,46 @@
             }
         }
     }
+
+    /**
+     * A listener to trace sensor event.
+     */
+    public interface SensorEventListener {
+
+        /**
+         * Called back while sensor event comes.
+         * @param reach The status of sensor.
+         */
+        void onSensorEvent(boolean reach);
+    }
+
+    /**
+     * A listener to trace status of image wallpaper.
+     */
+    public interface WallpaperStatusListener {
+
+        /**
+         * Called back while ambient mode changes.
+         * @param inAmbientMode true if is in ambient mode, false otherwise.
+         */
+        void onAmbientModeChanged(boolean inAmbientMode);
+
+        /**
+         * Called back while wallpaper offsets.
+         * @param xOffset The offset portion along x.
+         * @param yOffset The offset portion along y.
+         */
+        void onOffsetsChanged(float xOffset, float yOffset, Rect frame);
+    }
+
+    /**
+     * An abstraction for view of GLRenderer.
+     */
+    public interface ImageGLView {
+
+        /**
+         * Ask the view to render.
+         */
+        void render();
+    }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/bubbles/BubbleExpandedViewContainer.java b/packages/SystemUI/src/com/android/systemui/bubbles/BubbleExpandedViewContainer.java
index 71ae1f8..67b18fd 100644
--- a/packages/SystemUI/src/com/android/systemui/bubbles/BubbleExpandedViewContainer.java
+++ b/packages/SystemUI/src/com/android/systemui/bubbles/BubbleExpandedViewContainer.java
@@ -17,31 +17,53 @@
 package com.android.systemui.bubbles;
 
 import android.annotation.Nullable;
+import android.app.Notification;
+import android.app.PendingIntent;
 import android.content.Context;
+import android.content.Intent;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
 import android.content.res.Resources;
+import android.content.res.TypedArray;
 import android.graphics.Color;
 import android.graphics.drawable.ShapeDrawable;
+import android.provider.Settings;
 import android.text.TextUtils;
 import android.util.AttributeSet;
+import android.util.Log;
 import android.view.View;
+import android.widget.ImageButton;
 import android.widget.LinearLayout;
 import android.widget.TextView;
 
 import com.android.systemui.R;
 import com.android.systemui.recents.TriangleShape;
+import com.android.systemui.statusbar.notification.collection.NotificationEntry;
 
 /**
  * Container for the expanded bubble view, handles rendering the caret and header of the view.
  */
-public class BubbleExpandedViewContainer extends LinearLayout {
+public class BubbleExpandedViewContainer extends LinearLayout implements View.OnClickListener {
+    private static final String TAG = "BubbleExpandedView";
 
     // The triangle pointing to the expanded view
     private View mPointerView;
     // The view displayed between the pointer and the expanded view
     private TextView mHeaderView;
+    // Tappable header icon deeplinking into the app
+    private ImageButton mDeepLinkIcon;
+    // Tappable header icon deeplinking into notification settings
+    private ImageButton mSettingsIcon;
     // The view that is being displayed for the expanded state
     private View mExpandedView;
 
+    private NotificationEntry mEntry;
+    private PackageManager mPm;
+    private String mAppName;
+
+    // Need reference to let it know to collapse when new task is launched
+    private BubbleStackView mStackView;
+
     public BubbleExpandedViewContainer(Context context) {
         this(context, null);
     }
@@ -57,7 +79,7 @@
     public BubbleExpandedViewContainer(Context context, AttributeSet attrs, int defStyleAttr,
             int defStyleRes) {
         super(context, attrs, defStyleAttr, defStyleRes);
-        setOrientation(VERTICAL);
+        mPm = context.getPackageManager();
     }
 
     @Override
@@ -68,11 +90,85 @@
         mPointerView = findViewById(R.id.pointer_view);
         int width = res.getDimensionPixelSize(R.dimen.bubble_pointer_width);
         int height = res.getDimensionPixelSize(R.dimen.bubble_pointer_height);
+
+        TypedArray ta = getContext().obtainStyledAttributes(
+                new int[] {android.R.attr.colorBackgroundFloating});
+        int bgColor = ta.getColor(0, Color.WHITE);
+        ta.recycle();
+
         ShapeDrawable triangleDrawable = new ShapeDrawable(
                 TriangleShape.create(width, height, true /* pointUp */));
-        triangleDrawable.setTint(Color.WHITE); // TODO: dark mode
+        triangleDrawable.setTint(bgColor);
         mPointerView.setBackground(triangleDrawable);
-        mHeaderView = findViewById(R.id.bubble_content_header);
+
+        mHeaderView = findViewById(R.id.header_text);
+        mDeepLinkIcon = findViewById(R.id.deep_link_button);
+        mSettingsIcon = findViewById(R.id.settings_button);
+        mDeepLinkIcon.setOnClickListener(this);
+        mSettingsIcon.setOnClickListener(this);
+    }
+
+    /**
+     * Sets the notification entry used to populate this view.
+     */
+    public void setEntry(NotificationEntry entry, BubbleStackView stackView) {
+        mStackView = stackView;
+        mEntry = entry;
+
+        ApplicationInfo info;
+        try {
+            info = mPm.getApplicationInfo(
+                    entry.notification.getPackageName(),
+                    PackageManager.MATCH_UNINSTALLED_PACKAGES
+                            | PackageManager.MATCH_DISABLED_COMPONENTS
+                            | PackageManager.MATCH_DIRECT_BOOT_UNAWARE
+                            | PackageManager.MATCH_DIRECT_BOOT_AWARE);
+            if (info != null) {
+                mAppName = String.valueOf(mPm.getApplicationLabel(info));
+            }
+        } catch (PackageManager.NameNotFoundException e) {
+            // Ahh... just use package name
+            mAppName = entry.notification.getPackageName();
+        }
+
+        updateHeaderView();
+    }
+
+    private void updateHeaderView() {
+        mSettingsIcon.setContentDescription(getResources().getString(
+                R.string.bubbles_settings_button_description, mAppName));
+        mDeepLinkIcon.setContentDescription(getResources().getString(
+                R.string.bubbles_deep_link_button_description, mAppName));
+        if (mEntry != null && mEntry.getBubbleMetadata() != null) {
+            setHeaderText(mEntry.getBubbleMetadata().getTitle());
+        } else {
+            // This should only happen if we're auto-bubbling notification content that isn't
+            // explicitly a bubble
+            setHeaderText(mAppName);
+        }
+    }
+
+    @Override
+    public void onClick(View view) {
+        if (mEntry == null) {
+            return;
+        }
+        Notification n = mEntry.notification.getNotification();
+        int id = view.getId();
+        if (id == R.id.deep_link_button) {
+            mStackView.collapseStack(() -> {
+                try {
+                    n.contentIntent.send();
+                } catch (PendingIntent.CanceledException e) {
+                    Log.w(TAG, "Failed to send intent for bubble with key: "
+                            + (mEntry != null ? mEntry.key : " null entry"));
+                }
+            });
+        } else if (id == R.id.settings_button) {
+            Intent intent = getSettingsIntent(mEntry.notification.getPackageName(),
+                    mEntry.notification.getUid());
+            mStackView.collapseStack(() -> mContext.startActivity(intent));
+        }
     }
 
     /**
@@ -87,7 +183,7 @@
     /**
      * Set the text displayed within the header.
      */
-    public void setHeaderText(CharSequence text) {
+    private void setHeaderText(CharSequence text) {
         mHeaderView.setText(text);
         mHeaderView.setVisibility(TextUtils.isEmpty(text) ? GONE : VISIBLE);
     }
@@ -115,4 +211,13 @@
     public View getExpandedView() {
         return mExpandedView;
     }
+
+    private Intent getSettingsIntent(String packageName, final int appUid) {
+        final Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
+        intent.putExtra(Settings.EXTRA_APP_PACKAGE, packageName);
+        intent.putExtra(Settings.EXTRA_APP_UID, appUid);
+        intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
+        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+        return intent;
+    }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/bubbles/BubbleStackView.java b/packages/SystemUI/src/com/android/systemui/bubbles/BubbleStackView.java
index 8731bd5..c67e3ff 100644
--- a/packages/SystemUI/src/com/android/systemui/bubbles/BubbleStackView.java
+++ b/packages/SystemUI/src/com/android/systemui/bubbles/BubbleStackView.java
@@ -20,7 +20,6 @@
 import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
 
 import android.app.ActivityView;
-import android.app.PendingIntent;
 import android.content.Context;
 import android.content.res.Resources;
 import android.graphics.Outline;
@@ -28,7 +27,9 @@
 import android.graphics.PointF;
 import android.graphics.Rect;
 import android.graphics.RectF;
+import android.service.notification.StatusBarNotification;
 import android.util.Log;
+import android.util.StatsLog;
 import android.view.LayoutInflater;
 import android.view.MotionEvent;
 import android.view.View;
@@ -55,6 +56,9 @@
 import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
 import com.android.systemui.statusbar.notification.stack.ExpandableViewState;
 
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+
 /**
  * Renders bubbles in a stack and handles animating expanded and collapsed states.
  */
@@ -237,16 +241,24 @@
      * Sets the bubble that should be expanded and expands if needed.
      */
     public void setExpandedBubble(BubbleView bubbleToExpand) {
+        if (mIsExpanded && !bubbleToExpand.equals(mExpandedBubble)) {
+            // Previously expanded, notify that this bubble is no longer expanded
+            notifyExpansionChanged(mExpandedBubble, false /* expanded */);
+        }
+        BubbleView prevBubble = mExpandedBubble;
         mExpandedBubble = bubbleToExpand;
         if (!mIsExpanded) {
             // If we weren't previously expanded we should animate open.
             animateExpansion(true /* expand */);
+            logBubbleEvent(mExpandedBubble, StatsLog.BUBBLE_UICHANGED__ACTION__EXPANDED);
         } else {
             // Otherwise just update the views
             // TODO: probably animate / page to expanded one
             updateExpandedBubble();
             updatePointerPosition();
             requestUpdate();
+            logBubbleEvent(prevBubble, StatsLog.BUBBLE_UICHANGED__ACTION__COLLAPSED);
+            logBubbleEvent(mExpandedBubble, StatsLog.BUBBLE_UICHANGED__ACTION__EXPANDED);
         }
         mExpandedBubble.getEntry().setShowInShadeWhenBubble(false);
         notifyExpansionChanged(mExpandedBubble, true /* expanded */);
@@ -275,6 +287,7 @@
                 new FrameLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
         ViewClippingUtil.setClippingDeactivated(bubbleView, true, mClippingParameters);
         requestUpdate();
+        logBubbleEvent(bubbleView, StatsLog.BUBBLE_UICHANGED__ACTION__POSTED);
     }
 
     /**
@@ -291,12 +304,13 @@
             int nextIndex = bubbleCount > removedIndex ? removedIndex : bubbleCount - 1;
             BubbleView expandedBubble = (BubbleView) mBubbleContainer.getChildAt(nextIndex);
             setExpandedBubble(expandedBubble);
+            requestUpdate();
         }
         mIsExpanded = wasExpanded && mBubbleContainer.getChildCount() > 0;
         if (wasExpanded != mIsExpanded) {
             notifyExpansionChanged(mExpandedBubble, mIsExpanded);
         }
-        requestUpdate();
+        logBubbleEvent(bubbleView, StatsLog.BUBBLE_UICHANGED__ACTION__DISMISSED);
     }
 
     /**
@@ -305,6 +319,8 @@
     public void stackDismissed() {
         collapseStack();
         mBubbleContainer.removeAllViews();
+        logBubbleEvent(null /* no bubble associated with bubble stack dismiss */,
+                StatsLog.BUBBLE_UICHANGED__ACTION__STACK_DISMISSED);
     }
 
     /**
@@ -320,8 +336,7 @@
         if (updatePosition && !mIsExpanded) {
             // If alerting it gets promoted to top of the stack.
             if (mBubbleContainer.indexOfChild(bubbleView) != 0) {
-                mBubbleContainer.removeViewAndThen(bubbleView,
-                        () -> mBubbleContainer.addView(bubbleView, 0));
+                mBubbleContainer.moveViewTo(bubbleView, 0);
             }
             requestUpdate();
         }
@@ -329,6 +344,7 @@
             entry.setShowInShadeWhenBubble(false);
             requestUpdate();
         }
+        logBubbleEvent(bubbleView, StatsLog.BUBBLE_UICHANGED__ACTION__UPDATED);
     }
 
     /**
@@ -364,18 +380,24 @@
             // TODO: Save opened bubble & move it to top of stack
             animateExpansion(false /* shouldExpand */);
             notifyExpansionChanged(mExpandedBubble, mIsExpanded);
+            logBubbleEvent(mExpandedBubble, StatsLog.BUBBLE_UICHANGED__ACTION__COLLAPSED);
         }
     }
 
+    void collapseStack(Runnable endRunnable) {
+        collapseStack();
+        // TODO - use the runnable at end of animation
+        endRunnable.run();
+    }
+
     /**
      * Expands the stack fo bubbles.
      */
     public void expandStack() {
         if (!mIsExpanded) {
             mExpandedBubble = getTopBubble();
-            mExpandedBubble.getEntry().setShowInShadeWhenBubble(false);
-            animateExpansion(true /* shouldExpand */);
-            notifyExpansionChanged(mExpandedBubble, true /* expanded */);
+            setExpandedBubble(mExpandedBubble);
+            logBubbleEvent(mExpandedBubble, StatsLog.BUBBLE_UICHANGED__ACTION__STACK_EXPANDED);
         }
     }
 
@@ -568,6 +590,9 @@
                         .setStiffness(SpringForce.STIFFNESS_LOW)
                         .setDampingRatio(SPRING_DAMPING_RATIO),
                 /* destination */ null);
+
+        logBubbleEvent(null /* no bubble associated with bubble stack move */,
+                StatsLog.BUBBLE_UICHANGED__ACTION__STACK_MOVED);
     }
 
     /**
@@ -627,6 +652,7 @@
             return;
         }
 
+        mExpandedViewContainer.setEntry(mExpandedBubble.getEntry(), this);
         if (mExpandedBubble.hasAppOverlayIntent()) {
             // Bubble with activity view expanded state
             ActivityView expandedView = mExpandedBubble.getActivityView();
@@ -634,8 +660,6 @@
             expandedView.setLayoutParams(new LinearLayout.LayoutParams(
                     ViewGroup.LayoutParams.MATCH_PARENT, mExpandedBubbleHeight));
 
-            final PendingIntent intent = mExpandedBubble.getAppOverlayIntent();
-            mExpandedViewContainer.setHeaderText(intent.getIntent().getComponent().toShortString());
             mExpandedViewContainer.setExpandedView(expandedView);
         } else {
             // Bubble with notification view expanded state
@@ -649,8 +673,6 @@
             } else {
                 mExpandedViewContainer.setExpandedView(null);
             }
-            // Bubble with notification as expanded state doesn't need a header / title
-            mExpandedViewContainer.setHeaderText(null);
         }
     }
 
@@ -730,4 +752,72 @@
         viewState.headsUpIsVisible = false;
         viewState.applyToView(view);
     }
+
+    /**
+     * @return the number of bubbles in the stack view.
+     */
+    private int getBubbleCount() {
+        return mBubbleContainer.getChildCount();
+    }
+
+    /**
+     * Finds the bubble index within the stack.
+     *
+     * @param bubbleView the view of the bubble.
+     * @return the index of the bubble view within the bubble stack. The range of the position
+     * is between 0 and the bubble count minus 1.
+     */
+    private int getBubbleIndex(BubbleView bubbleView) {
+        return mBubbleContainer.indexOfChild(bubbleView);
+    }
+
+    /**
+     * @return the normalized x-axis position of the bubble stack rounded to 4 decimal places.
+     */
+    private float getNormalizedXPosition() {
+        return new BigDecimal(getPosition().x / mDisplaySize.x)
+                .setScale(4, RoundingMode.CEILING.HALF_UP)
+                .floatValue();
+    }
+
+    /**
+     * @return the normalized y-axis position of the bubble stack rounded to 4 decimal places.
+     */
+    private float getNormalizedYPosition() {
+        return new BigDecimal(getPosition().y / mDisplaySize.y)
+                .setScale(4, RoundingMode.CEILING.HALF_UP)
+                .floatValue();
+    }
+
+    /**
+     * Logs the bubble UI event.
+     *
+     * @param bubble the bubble that is being interacted on. Null value indicates that
+     *               the user interaction is not specific to one bubble.
+     * @param action the user interaction enum.
+     */
+    private void logBubbleEvent(@Nullable BubbleView bubble, int action) {
+        if (bubble == null) {
+            StatsLog.write(StatsLog.BUBBLE_UI_CHANGED,
+                    null /* package name */,
+                    null /* notification channel */,
+                    0 /* notification ID */,
+                    0 /* bubble position */,
+                    getBubbleCount(),
+                    action,
+                    getNormalizedXPosition(),
+                    getNormalizedYPosition());
+        } else {
+            StatusBarNotification notification = bubble.getEntry().notification;
+            StatsLog.write(StatsLog.BUBBLE_UI_CHANGED,
+                    notification.getPackageName(),
+                    notification.getNotification().getChannelId(),
+                    notification.getId(),
+                    getBubbleIndex(bubble),
+                    getBubbleCount(),
+                    action,
+                    getNormalizedXPosition(),
+                    getNormalizedYPosition());
+        }
+    }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/bubbles/animation/PhysicsAnimationLayout.java b/packages/SystemUI/src/com/android/systemui/bubbles/animation/PhysicsAnimationLayout.java
index 1ced3a4..e4e6bc9 100644
--- a/packages/SystemUI/src/com/android/systemui/bubbles/animation/PhysicsAnimationLayout.java
+++ b/packages/SystemUI/src/com/android/systemui/bubbles/animation/PhysicsAnimationLayout.java
@@ -27,9 +27,8 @@
 
 import com.android.systemui.R;
 
-import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.List;
+import java.util.HashSet;
 import java.util.Set;
 
 /**
@@ -122,12 +121,8 @@
     protected final HashMap<DynamicAnimation.ViewProperty, DynamicAnimation.OnAnimationEndListener>
             mEndListenerForProperty = new HashMap<>();
 
-    /**
-     * List of views that were passed to removeView, but are currently being animated out. These
-     * views will be actually removed by the controller (via super.removeView) once they're done
-     * animating out.
-     */
-    private final List<View> mViewsToBeActuallyRemoved = new ArrayList<>();
+    /** Set of currently rendered transient views. */
+    private final Set<View> mTransientViews = new HashSet<>();
 
     /** The currently active animation controller. */
     private PhysicsAnimationController mController;
@@ -186,23 +181,6 @@
         mEndListenerForProperty.remove(property);
     }
 
-    /**
-     * Returns the index of the view that precedes the given index, ignoring views that were passed
-     * to removeView, but are currently being animated out before actually being removed.
-     *
-     * @return index of the preceding view, or -1 if there are none.
-     */
-    public int getPrecedingNonRemovedViewIndex(int index) {
-        for (int i = index + 1; i < getChildCount(); i++) {
-            View precedingView = getChildAt(i);
-            if (!mViewsToBeActuallyRemoved.contains(precedingView)) {
-                return i;
-            }
-        }
-
-        return -1;
-    }
-
     @Override
     public void addView(View child, int index, ViewGroup.LayoutParams params) {
         super.addView(child, index, params);
@@ -224,6 +202,24 @@
         removeViewAndThen(view, /* callback */ null);
     }
 
+    @Override
+    public void addTransientView(View view, int index) {
+        super.addTransientView(view, index);
+        mTransientViews.add(view);
+    }
+
+    @Override
+    public void removeTransientView(View view) {
+        super.removeTransientView(view);
+        mTransientViews.remove(view);
+    }
+
+    /** Immediately moves the view from wherever it currently is, to the given index. */
+    public void moveViewTo(View view, int index) {
+        super.removeView(view);
+        addView(view, index);
+    }
+
     /**
      * Let the controller know that this view should be removed, and then call the callback once the
      * controller has finished any removal animations and the view has actually been removed.
@@ -231,23 +227,31 @@
     public void removeViewAndThen(View view, Runnable callback) {
         if (mController != null) {
             final int index = indexOfChild(view);
-            // Remove the view only if it exists in this layout, and we're not already working on
-            // animating its removal.
-            if (index > -1 && !mViewsToBeActuallyRemoved.contains(view)) {
-                mViewsToBeActuallyRemoved.add(view);
-                setChildrenVisibility();
 
-                // Tell the controller to animate this view out, and call the callback when it wants
-                // to actually remove the view.
-                mController.onChildToBeRemoved(view, index, () -> {
-                    removeViewImmediateAndThen(view, callback);
-                    mViewsToBeActuallyRemoved.remove(view);
-                });
-            }
+            // Remove the view and add it back as a transient view so we can animate it out.
+            super.removeView(view);
+            addTransientView(view, index);
+
+            setChildrenVisibility();
+
+            // Tell the controller to animate this view out, and call the callback when it's
+            // finished.
+            mController.onChildToBeRemoved(view, index, () -> {
+                // Done animating, remove the transient view.
+                removeTransientView(view);
+
+                if (callback != null) {
+                    callback.run();
+                }
+            });
         } else {
             // Without a controller, nobody will animate this view out, so it gets an unceremonious
             // departure.
-            removeViewImmediateAndThen(view, callback);
+            super.removeView(view);
+
+            if (callback != null) {
+                callback.run();
+            }
         }
     }
 
@@ -278,17 +282,18 @@
     }
 
     /**
-     * Animates the property of the child at the given index to the given value, then runs the
-     * callback provided when the animation ends.
+     * Animates the property of the given child view, then runs the callback provided when the
+     * animation ends.
      */
-    protected void animateValueForChildAtIndex(
+    protected void animateValueForChild(
             DynamicAnimation.ViewProperty property,
-            int index,
+            View view,
             float value,
             float startVel,
             Runnable after) {
-        if (index < getChildCount()) {
-            final SpringAnimation animation = getAnimationAtIndex(property, index);
+        if (view != null) {
+            final SpringAnimation animation =
+                    (SpringAnimation) view.getTag(getTagIdForProperty(property));
             if (after != null) {
                 animation.addEndListener(new OneTimeEndListener() {
                     @Override
@@ -300,14 +305,38 @@
                 });
             }
 
-            if (startVel != Float.MAX_VALUE) {
-                animation.setStartVelocity(startVel);
-            }
-
             animation.animateToFinalPosition(value);
         }
     }
 
+    protected void animateValueForChild(
+            DynamicAnimation.ViewProperty property,
+            View view,
+            float value,
+            Runnable after) {
+        animateValueForChild(property, view, value, Float.MAX_VALUE, after);
+    }
+
+    protected void animateValueForChild(
+            DynamicAnimation.ViewProperty property,
+            View view,
+            float value) {
+        animateValueForChild(property, view, value, Float.MAX_VALUE, /* after */ null);
+    }
+
+    /**
+     * Animates the property of the child at the given index to the given value, then runs the
+     * callback provided when the animation ends.
+     */
+    protected void animateValueForChildAtIndex(
+            DynamicAnimation.ViewProperty property,
+            int index,
+            float value,
+            float startVel,
+            Runnable after) {
+        animateValueForChild(property, getChildAt(index), value, startVel, after);
+    }
+
     /** Shortcut to animate a value with a callback, but no start velocity. */
     protected void animateValueForChildAtIndex(
             DynamicAnimation.ViewProperty property,
@@ -366,18 +395,6 @@
         }
     }
 
-
-    /** Immediately removes the view, without notifying the controller, then runs the callback. */
-    private void removeViewImmediateAndThen(View view, Runnable callback) {
-        super.removeView(view);
-
-        if (callback != null) {
-            callback.run();
-        }
-
-        setChildrenVisibility();
-    }
-
     /**
      * Retrieves the animation of the given property from the view at the given index via the view
      * tag system.
@@ -401,7 +418,11 @@
         newAnim.addUpdateListener((animation, value, velocity) -> {
             final int nextAnimInChain =
                     mController.getNextAnimationInChain(property, indexOfChild(child));
-            if (nextAnimInChain == PhysicsAnimationController.NONE) {
+
+            // If the controller doesn't want us to chain, or if we're a transient view in the
+            // process of being removed, don't chain.
+            if (nextAnimInChain == PhysicsAnimationController.NONE
+                    || mTransientViews.contains(child)) {
                 return;
             }
 
@@ -412,9 +433,7 @@
             // If this property's animations should be chained, then check to see if there is a
             // subsequent animation within the rendering limit, and if so, tell it to animate to
             // this animation's new value (plus the offset).
-            if (nextAnimInChain < Math.min(
-                    getChildCount(),
-                    mMaxRenderedChildren + mViewsToBeActuallyRemoved.size())) {
+            if (nextAnimInChain < Math.min(getChildCount(), mMaxRenderedChildren)) {
                 getAnimationAtIndex(property, animIndex + 1)
                         .animateToFinalPosition(value + offset);
             } else if (nextAnimInChain < getChildCount()) {
@@ -442,9 +461,7 @@
                     // Ignore views that are animating out when calculating whether to hide the
                     // view. That is, if we're supposed to render 5 views, but 4 are animating out
                     // and will soon be removed, render up to 9 views temporarily.
-                    i < (mMaxRenderedChildren + mViewsToBeActuallyRemoved.size())
-                        ? View.VISIBLE
-                        : View.GONE);
+                    i < mMaxRenderedChildren ? View.VISIBLE : View.GONE);
         }
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/bubbles/animation/StackAnimationController.java b/packages/SystemUI/src/com/android/systemui/bubbles/animation/StackAnimationController.java
index 0f51376..23c6fd3 100644
--- a/packages/SystemUI/src/com/android/systemui/bubbles/animation/StackAnimationController.java
+++ b/packages/SystemUI/src/com/android/systemui/bubbles/animation/StackAnimationController.java
@@ -53,8 +53,8 @@
     /** Scale factor to use initially for new bubbles being animated in. */
     private static final float ANIMATE_IN_STARTING_SCALE = 1.15f;
 
-    /** Translation factor (multiplied by stack offset) to use for new bubbles being animated in. */
-    private static final int ANIMATE_IN_TRANSLATION_FACTOR = 4;
+    /** Translation factor (multiplied by stack offset) to use for bubbles being animated in/out. */
+    private static final int ANIMATE_TRANSLATION_FACTOR = 4;
 
     /**
      * Values to use for the default {@link SpringForce} provided to the physics animation layout.
@@ -309,7 +309,7 @@
             // animate in from this position. Since the animations are chained, when the new bubble
             // flies in from the side, it will push the other ones out of the way.
             float xOffset = getOffsetForChainedPropertyAnimation(DynamicAnimation.TRANSLATION_X);
-            child.setTranslationX(mStackPosition.x - (ANIMATE_IN_TRANSLATION_FACTOR * xOffset));
+            child.setTranslationX(mStackPosition.x - ANIMATE_TRANSLATION_FACTOR * xOffset);
             mLayout.animateValueForChildAtIndex(
                     DynamicAnimation.TRANSLATION_X, 0, mStackPosition.x);
         }
@@ -318,27 +318,19 @@
     @Override
     void onChildToBeRemoved(View child, int index, Runnable actuallyRemove) {
         // Animate the child out, actually removing it once its alpha is zero.
-        mLayout.animateValueForChildAtIndex(
-                DynamicAnimation.ALPHA, index, 0f, () -> {
-                    actuallyRemove.run();
-                });
-        mLayout.animateValueForChildAtIndex(
-                DynamicAnimation.SCALE_X, index, ANIMATE_IN_STARTING_SCALE);
-        mLayout.animateValueForChildAtIndex(
-                DynamicAnimation.SCALE_Y, index, ANIMATE_IN_STARTING_SCALE);
+        mLayout.animateValueForChild(
+                DynamicAnimation.ALPHA, child, 0f, actuallyRemove);
+        mLayout.animateValueForChild(DynamicAnimation.SCALE_X, child, ANIMATE_IN_STARTING_SCALE);
+        mLayout.animateValueForChild(DynamicAnimation.SCALE_Y, child, ANIMATE_IN_STARTING_SCALE);
 
-        final boolean hasPrecedingChild = index + 1 < mLayout.getChildCount();
-        if (hasPrecedingChild) {
-            final int precedingViewIndex = mLayout.getPrecedingNonRemovedViewIndex(index);
-            if (precedingViewIndex >= 0) {
-                final float offsetX =
-                        getOffsetForChainedPropertyAnimation(DynamicAnimation.TRANSLATION_X);
-                mLayout.animatePositionForChildAtIndex(
-                        precedingViewIndex,
-                        mStackPosition.x + (index * offsetX),
-                        mStackPosition.y);
-            }
-        }
+        // Animate the removing view in the opposite direction of the stack.
+        final float xOffset = getOffsetForChainedPropertyAnimation(DynamicAnimation.TRANSLATION_X);
+        mLayout.animateValueForChild(DynamicAnimation.TRANSLATION_X, child,
+                mStackPosition.x - (-xOffset * ANIMATE_TRANSLATION_FACTOR));
+
+        // Pull the top of the stack to the correct position, the chained animations will instruct
+        // any children that are out of place to animate to the correct position.
+        mLayout.animateValueForChildAtIndex(DynamicAnimation.TRANSLATION_X, 0, mStackPosition.x);
     }
 
     /** Moves the stack, without any animation, to the starting position. */
diff --git a/packages/SystemUI/src/com/android/systemui/doze/DozeLog.java b/packages/SystemUI/src/com/android/systemui/doze/DozeLog.java
index a784773..9bca3c7 100644
--- a/packages/SystemUI/src/com/android/systemui/doze/DozeLog.java
+++ b/packages/SystemUI/src/com/android/systemui/doze/DozeLog.java
@@ -35,19 +35,19 @@
     private static final int SIZE = Build.IS_DEBUGGABLE ? 400 : 50;
     static final SimpleDateFormat FORMAT = new SimpleDateFormat("MM-dd HH:mm:ss.SSS");
 
-    private static final int REASONS = 10;
+    public static final int REASONS = 10;
 
     public static final int PULSE_REASON_NONE = -1;
     public static final int PULSE_REASON_INTENT = 0;
     public static final int PULSE_REASON_NOTIFICATION = 1;
     public static final int PULSE_REASON_SENSOR_SIGMOTION = 2;
-    public static final int PULSE_REASON_SENSOR_PICKUP = 3;
-    public static final int PULSE_REASON_SENSOR_DOUBLE_TAP = 4;
+    public static final int REASON_SENSOR_PICKUP = 3;
+    public static final int REASON_SENSOR_DOUBLE_TAP = 4;
     public static final int PULSE_REASON_SENSOR_LONG_PRESS = 5;
     public static final int PULSE_REASON_DOCKING = 6;
     public static final int REASON_SENSOR_WAKE_UP = 7;
     public static final int PULSE_REASON_SENSOR_WAKE_LOCK_SCREEN = 8;
-    public static final int PULSE_REASON_SENSOR_TAP = 9;
+    public static final int REASON_SENSOR_TAP = 9;
 
     private static boolean sRegisterKeyguardCallback = true;
 
@@ -202,13 +202,13 @@
             case PULSE_REASON_INTENT: return "intent";
             case PULSE_REASON_NOTIFICATION: return "notification";
             case PULSE_REASON_SENSOR_SIGMOTION: return "sigmotion";
-            case PULSE_REASON_SENSOR_PICKUP: return "pickup";
-            case PULSE_REASON_SENSOR_DOUBLE_TAP: return "doubletap";
+            case REASON_SENSOR_PICKUP: return "pickup";
+            case REASON_SENSOR_DOUBLE_TAP: return "doubletap";
             case PULSE_REASON_SENSOR_LONG_PRESS: return "longpress";
             case PULSE_REASON_DOCKING: return "docking";
             case PULSE_REASON_SENSOR_WAKE_LOCK_SCREEN: return "wakelockscreen";
             case REASON_SENSOR_WAKE_UP: return "wakeup";
-            case PULSE_REASON_SENSOR_TAP: return "tap";
+            case REASON_SENSOR_TAP: return "tap";
             default: throw new IllegalArgumentException("bad reason: " + pulseReason);
         }
     }
diff --git a/packages/SystemUI/src/com/android/systemui/doze/DozeSensors.java b/packages/SystemUI/src/com/android/systemui/doze/DozeSensors.java
index 562edd6..5efdc2f 100644
--- a/packages/SystemUI/src/com/android/systemui/doze/DozeSensors.java
+++ b/packages/SystemUI/src/com/android/systemui/doze/DozeSensors.java
@@ -97,20 +97,20 @@
                         mSensorManager.getDefaultSensor(Sensor.TYPE_PICK_UP_GESTURE),
                         Settings.Secure.DOZE_PICK_UP_GESTURE,
                         config.dozePickupSensorAvailable(),
-                        DozeLog.PULSE_REASON_SENSOR_PICKUP, false /* touchCoords */,
+                        DozeLog.REASON_SENSOR_PICKUP, false /* touchCoords */,
                         false /* touchscreen */),
                 new TriggerSensor(
                         findSensorWithType(config.doubleTapSensorType()),
                         Settings.Secure.DOZE_DOUBLE_TAP_GESTURE,
                         true /* configured */,
-                        DozeLog.PULSE_REASON_SENSOR_DOUBLE_TAP,
+                        DozeLog.REASON_SENSOR_DOUBLE_TAP,
                         dozeParameters.doubleTapReportsTouchCoordinates(),
                         true /* touchscreen */),
                 new TriggerSensor(
                         findSensorWithType(config.tapSensorType()),
                         Settings.Secure.DOZE_TAP_SCREEN_GESTURE,
                         true /* configured */,
-                        DozeLog.PULSE_REASON_SENSOR_TAP,
+                        DozeLog.REASON_SENSOR_TAP,
                         false /* reports touch coordinates */,
                         true /* touchscreen */),
                 new TriggerSensor(
@@ -130,7 +130,7 @@
                         false /* touchscreen */),
                 new PluginSensor(
                         new SensorManagerPlugin.Sensor(TYPE_WAKE_LOCK_SCREEN),
-                        Settings.Secure.DOZE_WAKE_LOCK_SCREEN_GESTURE,
+                        Settings.Secure.DOZE_WAKE_SCREEN_GESTURE,
                         mConfig.wakeScreenGestureAvailable(),
                         DozeLog.PULSE_REASON_SENSOR_WAKE_LOCK_SCREEN,
                         false /* reports touch coordinates */,
@@ -530,7 +530,7 @@
 
         /**
          * Called when a sensor requests a pulse
-         * @param pulseReason Requesting sensor, e.g. {@link DozeLog#PULSE_REASON_SENSOR_PICKUP}
+         * @param pulseReason Requesting sensor, e.g. {@link DozeLog#REASON_SENSOR_PICKUP}
          * @param sensorPerformedProxCheck true if the sensor already checked for FAR proximity.
          * @param screenX the location on the screen where the sensor fired or -1
  *                if the sensor doesn't support reporting screen locations.
diff --git a/packages/SystemUI/src/com/android/systemui/doze/DozeTriggers.java b/packages/SystemUI/src/com/android/systemui/doze/DozeTriggers.java
index dc505b5..8a35e70 100644
--- a/packages/SystemUI/src/com/android/systemui/doze/DozeTriggers.java
+++ b/packages/SystemUI/src/com/android/systemui/doze/DozeTriggers.java
@@ -108,10 +108,6 @@
         DozeLog.traceNotificationPulse(mContext);
     }
 
-    private void onWhisper() {
-        requestPulse(DozeLog.PULSE_REASON_NOTIFICATION, false /* performedProxCheck */);
-    }
-
     private void proximityCheckThenCall(IntConsumer callback,
             boolean alreadyPerformedProxCheck,
             int reason) {
@@ -137,9 +133,9 @@
     @VisibleForTesting
     void onSensor(int pulseReason, boolean sensorPerformedProxCheck,
             float screenX, float screenY, float[] rawValues) {
-        boolean isDoubleTap = pulseReason == DozeLog.PULSE_REASON_SENSOR_DOUBLE_TAP;
-        boolean isTap = pulseReason == DozeLog.PULSE_REASON_SENSOR_TAP;
-        boolean isPickup = pulseReason == DozeLog.PULSE_REASON_SENSOR_PICKUP;
+        boolean isDoubleTap = pulseReason == DozeLog.REASON_SENSOR_DOUBLE_TAP;
+        boolean isTap = pulseReason == DozeLog.REASON_SENSOR_TAP;
+        boolean isPickup = pulseReason == DozeLog.REASON_SENSOR_PICKUP;
         boolean isLongPress = pulseReason == DozeLog.PULSE_REASON_SENSOR_LONG_PRESS;
         boolean isWakeDisplay = pulseReason == DozeLog.REASON_SENSOR_WAKE_UP;
         boolean isWakeLockScreen = pulseReason == DozeLog.PULSE_REASON_SENSOR_WAKE_LOCK_SCREEN;
diff --git a/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageGLProgram.java b/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageGLProgram.java
new file mode 100644
index 0000000..d03b00b
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageGLProgram.java
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.glwallpaper;
+
+import static android.opengl.GLES20.GL_FRAGMENT_SHADER;
+import static android.opengl.GLES20.GL_VERTEX_SHADER;
+import static android.opengl.GLES20.glAttachShader;
+import static android.opengl.GLES20.glCompileShader;
+import static android.opengl.GLES20.glCreateProgram;
+import static android.opengl.GLES20.glCreateShader;
+import static android.opengl.GLES20.glGetAttribLocation;
+import static android.opengl.GLES20.glGetUniformLocation;
+import static android.opengl.GLES20.glLinkProgram;
+import static android.opengl.GLES20.glShaderSource;
+import static android.opengl.GLES20.glUseProgram;
+
+import android.content.Context;
+import android.content.res.Resources;
+import android.util.Log;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+/**
+ * This class takes charge of linking shader codes and then return a handle for OpenGL ES program.
+ */
+class ImageGLProgram {
+    private static final String TAG = ImageGLProgram.class.getSimpleName();
+
+    private Context mContext;
+    private int mProgramHandle;
+
+    ImageGLProgram(Context context) {
+        mContext = context.getApplicationContext();
+    }
+
+    private int loadShaderProgram(int vertexId, int fragmentId) {
+        final String vertexSrc = getShaderResource(vertexId);
+        final String fragmentSrc = getShaderResource(fragmentId);
+        final int vertexHandle = getShaderHandle(GL_VERTEX_SHADER, vertexSrc);
+        final int fragmentHandle = getShaderHandle(GL_FRAGMENT_SHADER, fragmentSrc);
+        return getProgramHandle(vertexHandle, fragmentHandle);
+    }
+
+    private String getShaderResource(int shaderId) {
+        Resources res = mContext.getResources();
+        StringBuilder code = new StringBuilder();
+
+        try (BufferedReader reader = new BufferedReader(
+                new InputStreamReader(res.openRawResource(shaderId)))) {
+            String nextLine;
+            while ((nextLine = reader.readLine()) != null) {
+                code.append(nextLine).append("\n");
+            }
+        } catch (IOException | Resources.NotFoundException ex) {
+            Log.d(TAG, "Can not read the shader source", ex);
+            code = null;
+        }
+
+        return code == null ? "" : code.toString();
+    }
+
+    private int getShaderHandle(int type, String src) {
+        final int shader = glCreateShader(type);
+        if (shader == 0) {
+            Log.d(TAG, "Create shader failed, type=" + type);
+            return 0;
+        }
+        glShaderSource(shader, src);
+        glCompileShader(shader);
+        return shader;
+    }
+
+    private int getProgramHandle(int vertexHandle, int fragmentHandle) {
+        final int program = glCreateProgram();
+        if (program == 0) {
+            Log.d(TAG, "Can not create OpenGL ES program");
+            return 0;
+        }
+
+        glAttachShader(program, vertexHandle);
+        glAttachShader(program, fragmentHandle);
+        glLinkProgram(program);
+        return program;
+    }
+
+    boolean useGLProgram(int vertexResId, int fragmentResId) {
+        mProgramHandle = loadShaderProgram(vertexResId, fragmentResId);
+        glUseProgram(mProgramHandle);
+        return true;
+    }
+
+    int getAttributeHandle(String name) {
+        return glGetAttribLocation(mProgramHandle, name);
+    }
+
+    int getUniformHandle(String name) {
+        return glGetUniformLocation(mProgramHandle, name);
+    }
+}
diff --git a/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageGLWallpaper.java b/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageGLWallpaper.java
new file mode 100644
index 0000000..4e07872
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageGLWallpaper.java
@@ -0,0 +1,245 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.glwallpaper;
+
+import static android.opengl.GLES20.GL_FLOAT;
+import static android.opengl.GLES20.GL_LINEAR;
+import static android.opengl.GLES20.GL_TEXTURE0;
+import static android.opengl.GLES20.GL_TEXTURE_2D;
+import static android.opengl.GLES20.GL_TEXTURE_MAG_FILTER;
+import static android.opengl.GLES20.GL_TEXTURE_MIN_FILTER;
+import static android.opengl.GLES20.GL_TRIANGLES;
+import static android.opengl.GLES20.glActiveTexture;
+import static android.opengl.GLES20.glBindTexture;
+import static android.opengl.GLES20.glDrawArrays;
+import static android.opengl.GLES20.glEnableVertexAttribArray;
+import static android.opengl.GLES20.glGenTextures;
+import static android.opengl.GLES20.glTexParameteri;
+import static android.opengl.GLES20.glUniform1i;
+import static android.opengl.GLES20.glVertexAttribPointer;
+
+import android.graphics.Bitmap;
+import android.opengl.GLUtils;
+import android.util.Log;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.FloatBuffer;
+
+/**
+ * This class takes charge of the geometry data like vertices and texture coordinates.
+ * It delivers these data to opengl runtime and triggers draw calls if necessary.
+ */
+class ImageGLWallpaper {
+    private static final String TAG = ImageGLWallpaper.class.getSimpleName();
+
+    static final String A_POSITION = "aPosition";
+    static final String A_TEXTURE_COORDINATES = "aTextureCoordinates";
+    static final String U_CENTER_REVEAL = "uCenterReveal";
+    static final String U_REVEAL = "uReveal";
+    static final String U_AOD2OPACITY = "uAod2Opacity";
+    static final String U_TEXTURE = "uTexture";
+    static final String U_AOD_MODE = "uAodMode";
+
+    private static final int HANDLE_UNDEFINED = -1;
+    private static final int POSITION_COMPONENT_COUNT = 2;
+    private static final int TEXTURE_COMPONENT_COUNT = 2;
+    private static final int BYTES_PER_FLOAT = 4;
+
+    // Vertices to define the square with 2 triangles.
+    private static final float[] VERTICES = {
+            -1.0f,  -1.0f,
+            +1.0f,  -1.0f,
+            +1.0f,  +1.0f,
+            +1.0f,  +1.0f,
+            -1.0f,  +1.0f,
+            -1.0f,  -1.0f
+    };
+
+    // Texture coordinates that maps to vertices.
+    private static final float[] TEXTURES = {
+            0f, 1f,
+            1f, 1f,
+            1f, 0f,
+            1f, 0f,
+            0f, 0f,
+            0f, 1f
+    };
+
+    private final FloatBuffer mVertexBuffer;
+    private final FloatBuffer mTextureBuffer;
+    private final ImageGLProgram mProgram;
+
+    private int mAttrPosition;
+    private int mAttrTextureCoordinates;
+    private int mUniAod2Opacity;
+    private int mUniAodMode;
+    private int mUniCenterReveal;
+    private int mUniReveal;
+    private int mUniTexture;
+    private int mTextureId;
+
+    ImageGLWallpaper(ImageGLProgram program) {
+        mProgram = program;
+
+        // Create an float array in opengles runtime (native) and put vertex data.
+        mVertexBuffer = ByteBuffer.allocateDirect(VERTICES.length * BYTES_PER_FLOAT)
+            .order(ByteOrder.nativeOrder())
+            .asFloatBuffer();
+        mVertexBuffer.put(VERTICES);
+        mVertexBuffer.position(0);
+
+        // Create an float array in opengles runtime (native) and put texture data.
+        mTextureBuffer = ByteBuffer.allocateDirect(TEXTURES.length * BYTES_PER_FLOAT)
+            .order(ByteOrder.nativeOrder())
+            .asFloatBuffer();
+        mTextureBuffer.put(TEXTURES);
+        mTextureBuffer.position(0);
+    }
+
+    void setup() {
+        setupAttributes();
+        setupUniforms();
+    }
+
+    private void setupAttributes() {
+        mAttrPosition = mProgram.getAttributeHandle(A_POSITION);
+        mVertexBuffer.position(0);
+        glVertexAttribPointer(mAttrPosition, POSITION_COMPONENT_COUNT, GL_FLOAT,
+                false, 0, mVertexBuffer);
+        glEnableVertexAttribArray(mAttrPosition);
+
+        mAttrTextureCoordinates = mProgram.getAttributeHandle(A_TEXTURE_COORDINATES);
+        mTextureBuffer.position(0);
+        glVertexAttribPointer(mAttrTextureCoordinates, TEXTURE_COMPONENT_COUNT, GL_FLOAT,
+                false, 0, mTextureBuffer);
+        glEnableVertexAttribArray(mAttrTextureCoordinates);
+    }
+
+    private void setupUniforms() {
+        mUniAod2Opacity = mProgram.getUniformHandle(U_AOD2OPACITY);
+        mUniAodMode = mProgram.getUniformHandle(U_AOD_MODE);
+        mUniCenterReveal = mProgram.getUniformHandle(U_CENTER_REVEAL);
+        mUniReveal = mProgram.getUniformHandle(U_REVEAL);
+        mUniTexture = mProgram.getUniformHandle(U_TEXTURE);
+    }
+
+    int getHandle(String name) {
+        switch (name) {
+            case A_POSITION:
+                return mAttrPosition;
+            case A_TEXTURE_COORDINATES:
+                return mAttrTextureCoordinates;
+            case U_AOD2OPACITY:
+                return mUniAod2Opacity;
+            case U_AOD_MODE:
+                return mUniAodMode;
+            case U_CENTER_REVEAL:
+                return mUniCenterReveal;
+            case U_REVEAL:
+                return mUniReveal;
+            case U_TEXTURE:
+                return mUniTexture;
+            default:
+                return HANDLE_UNDEFINED;
+        }
+    }
+
+    void draw() {
+        glDrawArrays(GL_TRIANGLES, 0, VERTICES.length / 2);
+    }
+
+    void setupTexture(Bitmap bitmap) {
+        final int[] tids = new int[1];
+
+        if (bitmap == null) {
+            Log.w(TAG, "setupTexture: invalid bitmap");
+            return;
+        }
+
+        // Generate one texture object and store the id in tids[0].
+        glGenTextures(1, tids, 0);
+        if (tids[0] == 0) {
+            Log.w(TAG, "setupTexture: glGenTextures() failed");
+            return;
+        }
+
+        // Bind a named texture to a texturing target.
+        glBindTexture(GL_TEXTURE_2D, tids[0]);
+        // Load the bitmap data and copy it over into the texture object that is currently bound.
+        GLUtils.texImage2D(GL_TEXTURE_2D, 0, bitmap, 0);
+        // Use bilinear texture filtering when minification.
+        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+        // Use bilinear texture filtering when magnification.
+        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+
+        mTextureId = tids[0];
+    }
+
+    void useTexture() {
+        // Set the active texture unit to texture unit 0.
+        glActiveTexture(GL_TEXTURE0);
+        // Bind the texture to this unit.
+        glBindTexture(GL_TEXTURE_2D, mTextureId);
+        // Let the texture sampler in fragment shader to read form this texture unit.
+        glUniform1i(mUniTexture, 0);
+    }
+
+    void adjustTextureCoordinates(Bitmap bitmap, int surfaceWidth, int surfaceHeight,
+            float xOffset, float yOffset) {
+        if (bitmap == null) {
+            Log.d(TAG, "adjustTextureCoordinates: invalid bitmap");
+            return;
+        }
+
+        float ratioW = 1f;
+        float ratioH = 1f;
+        int bitmapWidth = bitmap.getWidth();
+        int bitmapHeight = bitmap.getHeight();
+
+        boolean adjustWidth = bitmapWidth > surfaceWidth;
+        if (adjustWidth) {
+            ratioW = (float) surfaceWidth / bitmapWidth;
+            float referenceX = xOffset + ratioW > 1f ? 1f - ratioW : xOffset;
+            for (int i = 0; i < TEXTURES.length; i += 2) {
+                if (i == 2 || i == 4 || i == 6) {
+                    TEXTURES[i] = Math.min(1f, referenceX + ratioW);
+                } else {
+                    TEXTURES[i] = referenceX;
+                }
+            }
+        }
+
+        boolean adjustHeight = bitmapHeight > surfaceHeight;
+        if (adjustHeight) {
+            ratioH = (float) surfaceHeight / bitmapHeight;
+            float referenceY = yOffset + ratioH > 1f ? 1f - ratioH : yOffset;
+            for (int i = 1; i < TEXTURES.length; i += 2) {
+                if (i == 1 || i == 3 || i == 11) {
+                    TEXTURES[i] = Math.min(1f, referenceY + ratioH);
+                } else {
+                    TEXTURES[i] = referenceY;
+                }
+            }
+        }
+
+        if (adjustWidth || adjustHeight) {
+            mTextureBuffer.put(TEXTURES);
+            mTextureBuffer.position(0);
+        }
+    }
+}
diff --git a/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageProcessHelper.java b/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageProcessHelper.java
new file mode 100644
index 0000000..477e7d7e
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageProcessHelper.java
@@ -0,0 +1,144 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.glwallpaper;
+
+import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.ColorMatrix;
+import android.graphics.ColorMatrixColorFilter;
+import android.graphics.Matrix;
+import android.graphics.Paint;
+import android.os.AsyncTask;
+import android.os.Handler;
+import android.os.Handler.Callback;
+import android.os.Message;
+import android.util.Log;
+
+/**
+ * A helper class that computes histogram and percentile 85 from a bitmap.
+ * Percentile 85 will be computed each time the user picks a new image wallpaper.
+ */
+class ImageProcessHelper {
+    private static final String TAG = ImageProcessHelper.class.getSimpleName();
+    private static final float DEFAULT_PER85 = 0.8f;
+    private static final int MSG_UPDATE_PER85 = 1;
+
+    /**
+     * This color matrix will be applied to each pixel to get luminance from rgb by below formula:
+     * Luminance = .2126f * r + .7152f * g + .0722f * b.
+     */
+    private static final float[] LUMINOSITY_MATRIX = new float[] {
+            .2126f,     .0000f,     .0000f,     .0000f,     .0000f,
+            .0000f,     .7152f,     .0000f,     .0000f,     .0000f,
+            .0000f,     .0000f,     .0722f,     .0000f,     .0000f,
+            .0000f,     .0000f,     .0000f,     1.000f,     .0000f
+    };
+
+    private final Handler mHandler = new Handler(new Callback() {
+        @Override
+        public boolean handleMessage(Message msg) {
+            switch (msg.what) {
+                case MSG_UPDATE_PER85:
+                    mPer85 = (float) msg.obj;
+                    return true;
+                default:
+                    return false;
+            }
+        }
+    });
+
+    private float mPer85 = DEFAULT_PER85;
+
+    void startComputingPercentile85(Bitmap bitmap) {
+        new Per85ComputeTask(mHandler).execute(bitmap);
+    }
+
+    float getPercentile85() {
+        return mPer85;
+    }
+
+    private static class Per85ComputeTask extends AsyncTask<Bitmap, Void, Float> {
+        private Handler mUpdateHandler;
+
+        Per85ComputeTask(Handler handler) {
+            super(handler);
+            mUpdateHandler = handler;
+        }
+
+        @Override
+        protected Float doInBackground(Bitmap... bitmaps) {
+            Bitmap bitmap = bitmaps[0];
+            if (bitmap != null) {
+                int[] histogram = processHistogram(bitmap);
+                return computePercentile85(bitmap, histogram);
+            }
+            Log.e(TAG, "Per85ComputeTask: Can't get bitmap");
+            return DEFAULT_PER85;
+        }
+
+        @Override
+        protected void onPostExecute(Float result) {
+            Message msg = mUpdateHandler.obtainMessage(MSG_UPDATE_PER85, result);
+            mUpdateHandler.sendMessage(msg);
+        }
+
+        private int[] processHistogram(Bitmap bitmap) {
+            int width = bitmap.getWidth();
+            int height = bitmap.getHeight();
+
+            Bitmap target = Bitmap.createBitmap(width, height, bitmap.getConfig());
+            Canvas canvas = new Canvas(target);
+            ColorMatrix cm = new ColorMatrix(LUMINOSITY_MATRIX);
+            Paint paint = new Paint();
+            paint.setColorFilter(new ColorMatrixColorFilter(cm));
+            canvas.drawBitmap(bitmap, new Matrix(), paint);
+
+            // TODO: Fine tune the performance here, tracking on b/123615079.
+            int[] histogram = new int[256];
+            for (int row = 0; row < height; row++) {
+                for (int col = 0; col < width; col++) {
+                    int pixel = target.getPixel(col, row);
+                    int y = Color.red(pixel) + Color.green(pixel) + Color.blue(pixel);
+                    histogram[y]++;
+                }
+            }
+
+            return histogram;
+        }
+
+        private float computePercentile85(Bitmap bitmap, int[] histogram) {
+            float per85 = DEFAULT_PER85;
+            int pixelCount = bitmap.getWidth() * bitmap.getHeight();
+            float[] acc = new float[256];
+            for (int i = 0; i < acc.length; i++) {
+                acc[i] = (float) histogram[i] / pixelCount;
+                float prev = i == 0 ? 0f : acc[i - 1];
+                float next = acc[i];
+                float idx = (float) (i + 1) / 255;
+                float sum = prev + next;
+                if (prev < 0.85f && sum >= 0.85f) {
+                    per85 = idx;
+                }
+                if (i > 0) {
+                    acc[i] += acc[i - 1];
+                }
+            }
+            return per85;
+        }
+    }
+}
diff --git a/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageRevealHelper.java b/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageRevealHelper.java
new file mode 100644
index 0000000..787972c
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageRevealHelper.java
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.glwallpaper;
+
+import android.animation.Animator;
+import android.animation.AnimatorListenerAdapter;
+import android.animation.ValueAnimator;
+
+import com.android.systemui.Interpolators;
+
+/**
+ * Use ValueAnimator and appropriate interpolator to control the progress of reveal transition.
+ * The transition will happen while getting awake and quit events.
+ */
+class ImageRevealHelper {
+    private static final String TAG = ImageRevealHelper.class.getSimpleName();
+    private static final float MAX_REVEAL = 0f;
+    private static final float MIN_REVEAL = 1f;
+    private static final int REVEAL_DURATION = 1000;
+
+    private final ValueAnimator mAnimator;
+    private final RevealStateListener mRevealListener;
+    private float mReveal = MIN_REVEAL;
+    private boolean mAwake = false;
+
+    ImageRevealHelper(RevealStateListener listener) {
+        mRevealListener = listener;
+        mAnimator = ValueAnimator.ofFloat();
+        mAnimator.setDuration(REVEAL_DURATION);
+        mAnimator.setInterpolator(Interpolators.FAST_OUT_SLOW_IN);
+        mAnimator.addUpdateListener(animator -> {
+            mReveal = (float) animator.getAnimatedValue();
+            if (mRevealListener != null) {
+                mRevealListener.onRevealStateChanged();
+            }
+        });
+        mAnimator.addListener(new AnimatorListenerAdapter() {
+            private boolean mIsCanceled;
+
+            @Override
+            public void onAnimationCancel(Animator animation) {
+                mIsCanceled = true;
+            }
+
+            @Override
+            public void onAnimationEnd(Animator animation) {
+                if (!mIsCanceled) {
+                    mAwake = !mAwake;
+                }
+                mIsCanceled = false;
+            }
+        });
+    }
+
+    private void animate() {
+        mAnimator.cancel();
+        mAnimator.setFloatValues(mReveal, !mAwake ? MIN_REVEAL : MAX_REVEAL);
+        mAnimator.start();
+    }
+
+    public float getReveal() {
+        return mReveal;
+    }
+
+    public boolean isAwake() {
+        return mAwake;
+    }
+
+    void updateAwake(boolean awake) {
+        mAwake = awake;
+        animate();
+    }
+
+    void sleep() {
+        mReveal = MIN_REVEAL;
+        mAwake = false;
+    }
+
+    /**
+     * A listener to trace value changes of reveal.
+     */
+    public interface RevealStateListener {
+
+        /**
+         * Called back while reveal status changes.
+         */
+        void onRevealStateChanged();
+    }
+}
diff --git a/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageWallpaperRenderer.java b/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageWallpaperRenderer.java
new file mode 100644
index 0000000..8916b28
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageWallpaperRenderer.java
@@ -0,0 +1,153 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.glwallpaper;
+
+import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT;
+import static android.opengl.GLES20.glClear;
+import static android.opengl.GLES20.glClearColor;
+import static android.opengl.GLES20.glUniform1f;
+import static android.opengl.GLES20.glUniform1i;
+import static android.opengl.GLES20.glViewport;
+
+import android.app.WallpaperManager;
+import android.content.Context;
+import android.graphics.Bitmap;
+import android.graphics.Rect;
+import android.opengl.GLSurfaceView;
+import android.util.Log;
+
+import com.android.systemui.ImageWallpaper;
+import com.android.systemui.ImageWallpaper.ImageGLView;
+import com.android.systemui.R;
+
+import javax.microedition.khronos.egl.EGLConfig;
+import javax.microedition.khronos.opengles.GL10;
+
+/**
+ * A GL renderer for image wallpaper.
+ */
+public class ImageWallpaperRenderer implements GLSurfaceView.Renderer,
+        ImageWallpaper.SensorEventListener, ImageWallpaper.WallpaperStatusListener,
+        ImageRevealHelper.RevealStateListener {
+    private static final String TAG = ImageWallpaperRenderer.class.getSimpleName();
+
+    private final WallpaperManager mWallpaperManager;
+    private final ImageGLProgram mProgram;
+    private final ImageGLWallpaper mWallpaper;
+    private final ImageProcessHelper mImageProcessHelper;
+    private final ImageRevealHelper mImageRevealHelper;
+    private final ImageGLView mGLView;
+    private boolean mIsInAmbientMode;
+    private float mXOffset = 0f;
+    private float mYOffset = 0f;
+
+    public ImageWallpaperRenderer(Context context, ImageGLView glView) {
+        mWallpaperManager = context.getSystemService(WallpaperManager.class);
+        if (mWallpaperManager == null) {
+            Log.w(TAG, "WallpaperManager not available");
+        }
+
+        mProgram = new ImageGLProgram(context);
+        mWallpaper = new ImageGLWallpaper(mProgram);
+        mImageProcessHelper = new ImageProcessHelper();
+        mImageRevealHelper = new ImageRevealHelper(this);
+        mGLView = glView;
+
+        if (mWallpaperManager != null) {
+            // Compute per85 as transition threshold, this is an async work.
+            mImageProcessHelper.startComputingPercentile85(mWallpaperManager.getBitmap());
+        }
+    }
+
+    @Override
+    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
+        glClearColor(0f, 0f, 0f, 1.0f);
+        mProgram.useGLProgram(
+                R.raw.image_wallpaper_vertex_shader, R.raw.image_wallpaper_fragment_shader);
+        mWallpaper.setup();
+        mWallpaper.setupTexture(mWallpaperManager.getBitmap());
+    }
+
+    @Override
+    public void onSurfaceChanged(GL10 gl, int width, int height) {
+        glViewport(0, 0, width, height);
+        mWallpaper.adjustTextureCoordinates(mWallpaperManager.getBitmap(),
+                width, height, mXOffset, mYOffset);
+    }
+
+    @Override
+    public void onDrawFrame(GL10 gl) {
+        float threshold = mImageProcessHelper.getPercentile85();
+        float reveal = mImageRevealHelper.getReveal();
+
+        glClear(GL_COLOR_BUFFER_BIT);
+
+        glUniform1f(mWallpaper.getHandle(ImageGLWallpaper.U_AOD2OPACITY), .25f);
+        glUniform1f(mWallpaper.getHandle(ImageGLWallpaper.U_CENTER_REVEAL), threshold);
+        glUniform1f(mWallpaper.getHandle(ImageGLWallpaper.U_REVEAL), reveal);
+        glUniform1i(mWallpaper.getHandle(ImageGLWallpaper.U_AOD_MODE), mIsInAmbientMode ? 1 : 0);
+
+        mWallpaper.useTexture();
+        mWallpaper.draw();
+    }
+
+    @Override
+    public void onSensorEvent(boolean awake) {
+        mImageRevealHelper.updateAwake(awake);
+    }
+
+    @Override
+    public void onAmbientModeChanged(boolean inAmbientMode) {
+        mIsInAmbientMode = inAmbientMode;
+        if (inAmbientMode) {
+            mImageRevealHelper.sleep();
+        }
+        requestRender();
+    }
+
+    @Override
+    public void onOffsetsChanged(float xOffset, float yOffset, Rect frame) {
+        if (frame == null || mWallpaperManager == null
+                || (xOffset == mXOffset && yOffset == mYOffset)) {
+            return;
+        }
+
+        Bitmap bitmap = mWallpaperManager.getBitmap();
+        if (bitmap == null) {
+            return;
+        }
+
+        int width = frame.width();
+        int height = frame.height();
+        mXOffset = xOffset;
+        mYOffset = yOffset;
+
+        mWallpaper.adjustTextureCoordinates(bitmap, width, height, mXOffset, mYOffset);
+        requestRender();
+    }
+
+    @Override
+    public void onRevealStateChanged() {
+        requestRender();
+    }
+
+    private void requestRender() {
+        if (mGLView != null) {
+            mGLView.render();
+        }
+    }
+}
diff --git a/packages/SystemUI/src/com/android/systemui/privacy/OngoingPrivacyDialog.kt b/packages/SystemUI/src/com/android/systemui/privacy/OngoingPrivacyDialog.kt
index e2688f1..1cd3509 100644
--- a/packages/SystemUI/src/com/android/systemui/privacy/OngoingPrivacyDialog.kt
+++ b/packages/SystemUI/src/com/android/systemui/privacy/OngoingPrivacyDialog.kt
@@ -22,6 +22,7 @@
 import android.content.pm.PackageManager
 import android.content.res.ColorStateList
 import android.os.UserHandle
+import android.provider.Settings
 import android.util.IconDrawableFactory
 import android.view.Gravity
 import android.view.LayoutInflater
@@ -64,7 +65,7 @@
             setPositiveButton(R.string.ongoing_privacy_dialog_ok, null)
             setNeutralButton(R.string.ongoing_privacy_dialog_open_settings,
                     object : DialogInterface.OnClickListener {
-                        val intent = Intent(Intent.ACTION_REVIEW_PERMISSION_USAGE).putExtra(
+                        val intent = Intent(Settings.ACTION_ENTERPRISE_PRIVACY_SETTINGS).putExtra(
                                 Intent.EXTRA_DURATION_MILLIS, TimeUnit.MINUTES.toMillis(1))
 
                         @Suppress("DEPRECATION")
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/MediaTransferManager.java b/packages/SystemUI/src/com/android/systemui/statusbar/MediaTransferManager.java
new file mode 100644
index 0000000..752b6a8
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/MediaTransferManager.java
@@ -0,0 +1,99 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.statusbar;
+
+import android.content.Context;
+import android.content.Intent;
+import android.service.notification.StatusBarNotification;
+import android.util.FeatureFlagUtils;
+import android.view.View;
+import android.view.ViewGroup;
+import android.view.ViewParent;
+
+import com.android.settingslib.media.MediaOutputSliceConstants;
+import com.android.systemui.Dependency;
+import com.android.systemui.plugins.ActivityStarter;
+import com.android.systemui.statusbar.notification.collection.NotificationEntry;
+import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
+
+/**
+ * Class for handling MediaTransfer state over a set of notifications.
+ */
+public class MediaTransferManager {
+    private final Context mContext;
+    private final ActivityStarter mActivityStarter;
+
+    private final View.OnClickListener mOnClickHandler = new View.OnClickListener() {
+        @Override
+        public void onClick(View view) {
+            if (handleMediaTransfer(view)) {
+                return;
+            }
+        }
+
+        private boolean handleMediaTransfer(View view) {
+            if (view.findViewById(com.android.internal.R.id.media_seamless) == null) {
+                return false;
+            }
+
+            ViewParent parent = view.getParent();
+            StatusBarNotification statusBarNotification = getNotificationForParent(parent);
+            final Intent intent = new Intent()
+                    .setAction(MediaOutputSliceConstants.ACTION_MEDIA_OUTPUT)
+                    .putExtra(MediaOutputSliceConstants.EXTRA_PACKAGE_NAME,
+                            statusBarNotification.getPackageName());
+            mActivityStarter.startActivity(intent, false, true /* dismissShade */,
+                    Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
+            return true;
+        }
+
+        private StatusBarNotification getNotificationForParent(ViewParent parent) {
+            while (parent != null) {
+                if (parent instanceof ExpandableNotificationRow) {
+                    return ((ExpandableNotificationRow) parent).getStatusBarNotification();
+                }
+                parent = parent.getParent();
+            }
+            return null;
+        }
+    };
+
+    public MediaTransferManager(Context context) {
+        mContext = context;
+        mActivityStarter = Dependency.get(ActivityStarter.class);
+    }
+
+    /**
+     * apply the action button for MediaTransfer
+     *
+     * @param root  The parent container of the view.
+     * @param entry The entry of MediaTransfer action button.
+     */
+    public void applyMediaTransferView(ViewGroup root, NotificationEntry entry) {
+        if (!FeatureFlagUtils.isEnabled(mContext, FeatureFlagUtils.SEAMLESS_TRANSFER)) {
+            return;
+        }
+
+        View view = root.findViewById(com.android.internal.R.id.media_seamless);
+        if (view == null) {
+            return;
+        }
+
+        view.setVisibility(View.VISIBLE);
+        view.setOnClickListener(mOnClickHandler);
+    }
+}
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/NotificationListener.java b/packages/SystemUI/src/com/android/systemui/statusbar/NotificationListener.java
index 0583843..be63bad 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/NotificationListener.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/NotificationListener.java
@@ -55,8 +55,6 @@
     private final ArrayList<NotificationSettingsListener> mSettingsListeners = new ArrayList<>();
     private final Context mContext;
 
-    protected NotificationPresenter mPresenter;
-
     public NotificationListener(Context context) {
         mContext = context;
     }
@@ -152,9 +150,7 @@
         }
     }
 
-    public void setUpWithPresenter(NotificationPresenter presenter) {
-        mPresenter = presenter;
-
+    public void registerAsSystemService() {
         try {
             registerAsSystemService(mContext,
                     new ComponentName(mContext.getPackageName(), getClass().getCanonicalName()),
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/NotificationUiAdjustment.java b/packages/SystemUI/src/com/android/systemui/statusbar/NotificationUiAdjustment.java
index f0d804d..3db02b9 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/NotificationUiAdjustment.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/NotificationUiAdjustment.java
@@ -54,7 +54,7 @@
     public static NotificationUiAdjustment extractFromNotificationEntry(
             NotificationEntry entry) {
         return new NotificationUiAdjustment(
-                entry.key, entry.systemGeneratedSmartActions, entry.smartReplies);
+                entry.key, entry.systemGeneratedSmartActions, entry.systemGeneratedSmartReplies);
     }
 
     public static boolean needReinflate(
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/NotificationEntry.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/NotificationEntry.java
index 2e93c382..db9fcc8 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/NotificationEntry.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/NotificationEntry.java
@@ -102,7 +102,9 @@
     /** Smart Actions provided by the NotificationAssistantService. */
     @NonNull
     public List<Notification.Action> systemGeneratedSmartActions = Collections.emptyList();
-    public CharSequence[] smartReplies = new CharSequence[0];
+    /** Smart replies provided by the NotificationAssistantService. */
+    @NonNull
+    public CharSequence[] systemGeneratedSmartReplies = new CharSequence[0];
     @VisibleForTesting
     public int suppressedVisualEffects;
     public boolean suspended;
@@ -182,7 +184,7 @@
         userSentiment = ranking.getUserSentiment();
         systemGeneratedSmartActions = ranking.getSmartActions() == null
                 ? Collections.emptyList() : ranking.getSmartActions();
-        smartReplies = ranking.getSmartReplies() == null
+        systemGeneratedSmartReplies = ranking.getSmartReplies() == null
                 ? new CharSequence[0]
                 : ranking.getSmartReplies().toArray(new CharSequence[0]);
         suppressedVisualEffects = ranking.getSuppressedVisualEffects();
@@ -239,6 +241,13 @@
     }
 
     /**
+     * Returns the data needed for a bubble for this notification, if it exists.
+     */
+    public Notification.BubbleMetadata getBubbleMetadata() {
+        return notification.getNotification().getBubbleMetadata();
+    }
+
+    /**
      * Resets the notification entry to be re-used.
      */
     public void reset() {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/row/NotificationContentView.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/row/NotificationContentView.java
index c161da3..878d533 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/row/NotificationContentView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/row/NotificationContentView.java
@@ -43,6 +43,7 @@
 import com.android.internal.util.ContrastColorUtil;
 import com.android.systemui.Dependency;
 import com.android.systemui.R;
+import com.android.systemui.statusbar.MediaTransferManager;
 import com.android.systemui.statusbar.RemoteInputController;
 import com.android.systemui.statusbar.SmartReplyController;
 import com.android.systemui.statusbar.TransformableView;
@@ -163,11 +164,13 @@
     private boolean mIsContentExpandable;
     private boolean mRemoteInputVisible;
     private int mUnrestrictedContentHeight;
+    private MediaTransferManager mMediaTransferManager;
 
 
     public NotificationContentView(Context context, AttributeSet attrs) {
         super(context, attrs);
         mHybridGroupManager = new HybridGroupManager(getContext(), this);
+        mMediaTransferManager = new MediaTransferManager(getContext());
         mSmartReplyConstants = Dependency.get(SmartReplyConstants.class);
         mSmartReplyController = Dependency.get(SmartReplyController.class);
         initView();
@@ -1250,6 +1253,7 @@
             mAmbientWrapper.onContentUpdated(row);
         }
         applyRemoteInputAndSmartReply(entry);
+        applyMediaTransfer(entry);
         updateLegacy();
         mForceSelectNextLayout = true;
         setDark(mDark, false /* animate */, 0 /* delay */);
@@ -1292,6 +1296,22 @@
         }
     }
 
+    private void applyMediaTransfer(final NotificationEntry entry) {
+        View bigContentView = mExpandedChild;
+        if (bigContentView == null || !entry.isMediaNotification()) {
+            return;
+        }
+
+        View mediaActionContainer = bigContentView.findViewById(
+                com.android.internal.R.id.media_actions);
+        if (!(mediaActionContainer instanceof LinearLayout)) {
+            return;
+        }
+
+        mMediaTransferManager.applyMediaTransferView((ViewGroup) mediaActionContainer,
+                entry);
+    }
+
     private void applyRemoteInputAndSmartReply(final NotificationEntry entry) {
         if (mRemoteInputController == null) {
             return;
@@ -1314,16 +1334,21 @@
     static SmartRepliesAndActions chooseSmartRepliesAndActions(
             SmartReplyConstants smartReplyConstants,
             final NotificationEntry entry) {
-        boolean enableAppGeneratedSmartReplies = (smartReplyConstants.isEnabled()
-                && (!smartReplyConstants.requiresTargetingP()
-                || entry.targetSdk >= Build.VERSION_CODES.P));
-
         Notification notification = entry.notification.getNotification();
         Pair<RemoteInput, Notification.Action> remoteInputActionPair =
                 notification.findRemoteInputActionPair(false /* freeform */);
         Pair<RemoteInput, Notification.Action> freeformRemoteInputActionPair =
                 notification.findRemoteInputActionPair(true /* freeform */);
 
+        if (!smartReplyConstants.isEnabled()) {
+            return new SmartRepliesAndActions(null, null, freeformRemoteInputActionPair != null);
+        }
+        // Only use smart replies from the app if they target P or above. We have this check because
+        // the smart reply API has been used for other things (Wearables) in the past. The API to
+        // add smart actions is new in Q so it doesn't require a target-sdk check.
+        boolean enableAppGeneratedSmartReplies = (!smartReplyConstants.requiresTargetingP()
+                || entry.targetSdk >= Build.VERSION_CODES.P);
+
         boolean appGeneratedSmartRepliesExist =
                 enableAppGeneratedSmartReplies
                         && remoteInputActionPair != null
@@ -1348,13 +1373,13 @@
         }
         // Apps didn't provide any smart replies / actions, use those from NAS (if any).
         if (!appGeneratedSmartRepliesExist && !appGeneratedSmartActionsExist) {
-            boolean useGeneratedReplies = !ArrayUtils.isEmpty(entry.smartReplies)
+            boolean useGeneratedReplies = !ArrayUtils.isEmpty(entry.systemGeneratedSmartReplies)
                     && freeformRemoteInputActionPair != null
                     && freeformRemoteInputActionPair.second.getAllowGeneratedReplies()
                     && freeformRemoteInputActionPair.second.actionIntent != null;
             if (useGeneratedReplies) {
                 smartReplies = new SmartReplyView.SmartReplies(
-                        entry.smartReplies,
+                        entry.systemGeneratedSmartReplies,
                         freeformRemoteInputActionPair.first,
                         freeformRemoteInputActionPair.second.actionIntent,
                         true /* fromAssistant */);
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/row/NotificationGutsManager.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/row/NotificationGutsManager.java
index cb1384c..aa221993 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/row/NotificationGutsManager.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/row/NotificationGutsManager.java
@@ -25,7 +25,6 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageManager;
-import android.metrics.LogMaker;
 import android.net.Uri;
 import android.os.ServiceManager;
 import android.os.UserHandle;
@@ -392,12 +391,6 @@
             return false;
         }
 
-        LogMaker logMaker = (row.getStatusBarNotification() == null)
-                ? new LogMaker(MetricsProto.MetricsEvent.ACTION_NOTE_CONTROLS)
-                : row.getStatusBarNotification().getLogMaker();
-        mMetricsLogger.write(logMaker.setCategory(MetricsProto.MetricsEvent.ACTION_NOTE_CONTROLS)
-                    .setType(MetricsProto.MetricsEvent.TYPE_ACTION));
-
         // ensure that it's laid but not visible until actually laid out
         guts.setVisibility(View.INVISIBLE);
         // Post to ensure the the guts are properly laid out.
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/row/NotificationInfo.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/row/NotificationInfo.java
index 359bc6e..b6948fc 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/row/NotificationInfo.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/row/NotificationInfo.java
@@ -126,18 +126,23 @@
     private OnClickListener mOnKeepShowing = v -> {
         mExitReason = NotificationCounters.BLOCKING_HELPER_KEEP_SHOWING;
         closeControls(v);
-        mMetricsLogger.write(getLogMaker().setCategory(MetricsEvent.NOTIFICATION_BLOCKING_HELPER)
-                .setType(MetricsEvent.TYPE_ACTION)
-                .setSubtype(MetricsEvent.BLOCKING_HELPER_CLICK_STAY_SILENT));
+        if (mIsForBlockingHelper) {
+            mMetricsLogger.write(getLogMaker().setCategory(
+                    MetricsEvent.NOTIFICATION_BLOCKING_HELPER)
+                    .setType(MetricsEvent.TYPE_ACTION)
+                    .setSubtype(MetricsEvent.BLOCKING_HELPER_CLICK_STAY_SILENT));
+        }
     };
 
     private OnClickListener mOnToggleSilent = v -> {
         Runnable saveImportance = () -> {
             swapContent(ACTION_TOGGLE_SILENT, true /* animate */);
-            mMetricsLogger.write(getLogMaker()
-                    .setCategory(MetricsEvent.NOTIFICATION_BLOCKING_HELPER)
-                    .setType(MetricsEvent.TYPE_ACTION)
-                    .setSubtype(MetricsEvent.BLOCKING_HELPER_CLICK_ALERT_ME));
+            if (mIsForBlockingHelper) {
+                mMetricsLogger.write(getLogMaker()
+                        .setCategory(MetricsEvent.NOTIFICATION_BLOCKING_HELPER)
+                        .setType(MetricsEvent.TYPE_ACTION)
+                        .setSubtype(MetricsEvent.BLOCKING_HELPER_CLICK_ALERT_ME));
+            }
         };
         if (mCheckSaveListener != null) {
             mCheckSaveListener.checkSave(saveImportance, mSbn);
@@ -149,10 +154,12 @@
     private OnClickListener mOnStopOrMinimizeNotifications = v -> {
         Runnable saveImportance = () -> {
             swapContent(ACTION_BLOCK, true /* animate */);
-            mMetricsLogger.write(getLogMaker()
-                    .setCategory(MetricsEvent.NOTIFICATION_BLOCKING_HELPER)
-                    .setType(MetricsEvent.TYPE_ACTION)
-                    .setSubtype(MetricsEvent.BLOCKING_HELPER_CLICK_BLOCKED));
+            if (mIsForBlockingHelper) {
+                mMetricsLogger.write(getLogMaker()
+                        .setCategory(MetricsEvent.NOTIFICATION_BLOCKING_HELPER)
+                        .setType(MetricsEvent.TYPE_ACTION)
+                        .setSubtype(MetricsEvent.BLOCKING_HELPER_CLICK_BLOCKED));
+            }
         };
         if (mCheckSaveListener != null) {
             mCheckSaveListener.checkSave(saveImportance, mSbn);
@@ -164,12 +171,16 @@
     private OnClickListener mOnUndo = v -> {
         // Reset exit counter that we'll log and record an undo event separately (not an exit event)
         mExitReason = NotificationCounters.BLOCKING_HELPER_DISMISSED;
-        logBlockingHelperCounter(NotificationCounters.BLOCKING_HELPER_UNDO);
-        mMetricsLogger.write(importanceChangeLogMaker().setType(MetricsEvent.TYPE_DISMISS));
+        if (mIsForBlockingHelper) {
+            logBlockingHelperCounter(NotificationCounters.BLOCKING_HELPER_UNDO);
+            mMetricsLogger.write(getLogMaker().setCategory(
+                    MetricsEvent.NOTIFICATION_BLOCKING_HELPER)
+                    .setType(MetricsEvent.TYPE_DISMISS)
+                    .setSubtype(MetricsEvent.BLOCKING_HELPER_CLICK_UNDO));
+        } else {
+            mMetricsLogger.write(importanceChangeLogMaker().setType(MetricsEvent.TYPE_DISMISS));
+        }
         swapContent(ACTION_UNDO, true /* animate */);
-        mMetricsLogger.write(getLogMaker().setCategory(MetricsEvent.NOTIFICATION_BLOCKING_HELPER)
-                .setType(MetricsEvent.TYPE_DISMISS)
-                .setSubtype(MetricsEvent.BLOCKING_HELPER_CLICK_UNDO));
     };
 
     public NotificationInfo(Context context, AttributeSet attrs) {
@@ -269,11 +280,11 @@
         bindPrompt();
         bindButtons();
 
-        mMetricsLogger.write(getLogMaker().setCategory(MetricsEvent.NOTIFICATION_BLOCKING_HELPER)
-                .setType(MetricsEvent.TYPE_OPEN)
-                .setSubtype(MetricsEvent.BLOCKING_HELPER_DISPLAY));
+        mMetricsLogger.write(notificationControlsLogMaker());
     }
 
+
+
     private void bindHeader() throws RemoteException {
         // Package name
         Drawable pkgicon = null;
@@ -404,19 +415,6 @@
         }
     }
 
-    /**
-     * Returns an initialized LogMaker for logging importance changes.
-     * The caller may override the type (to DISMISS) before passing it to mMetricsLogger.
-     * @return new LogMaker
-     */
-    private LogMaker importanceChangeLogMaker() {
-        Integer chosenImportance =
-                mChosenImportance != null ? mChosenImportance : mStartingChannelImportance;
-        return new LogMaker(MetricsEvent.ACTION_SAVE_IMPORTANCE)
-                .setType(MetricsEvent.TYPE_ACTION)
-                .setSubtype(chosenImportance - mStartingChannelImportance);
-    }
-
     private boolean hasImportanceChanged() {
         return mSingleNotificationChannel != null
                 && mChosenImportance != null
@@ -616,8 +614,8 @@
         confirmation.setAlpha(1f);
         header.setVisibility(VISIBLE);
         header.setAlpha(1f);
-        mMetricsLogger.write(getLogMaker().setCategory(MetricsEvent.NOTIFICATION_BLOCKING_HELPER)
-                .setType(MetricsEvent.TYPE_CLOSE));
+
+        mMetricsLogger.write(notificationControlsLogMaker().setType(MetricsEvent.TYPE_CLOSE));
     }
 
     @Override
@@ -764,7 +762,39 @@
         }
     }
 
+    /**
+     * Returns a LogMaker with all available notification information.
+     * Caller should set category, type, and maybe subtype, before passing it to mMetricsLogger.
+     * @return LogMaker
+     */
     private LogMaker getLogMaker() {
-        return mSbn.getLogMaker();
+        // The constructor requires a category, so also do it in the other branch for consistency.
+        return mSbn == null ? new LogMaker(MetricsEvent.NOTIFICATION_BLOCKING_HELPER)
+                : mSbn.getLogMaker().setCategory(MetricsEvent.NOTIFICATION_BLOCKING_HELPER);
+    }
+
+    /**
+     * Returns an initialized LogMaker for logging importance changes.
+     * The caller may override the type before passing it to mMetricsLogger.
+     * @return LogMaker
+     */
+    private LogMaker importanceChangeLogMaker() {
+        Integer chosenImportance =
+                mChosenImportance != null ? mChosenImportance : mStartingChannelImportance;
+        return getLogMaker().setCategory(MetricsEvent.ACTION_SAVE_IMPORTANCE)
+                .setType(MetricsEvent.TYPE_ACTION)
+                .setSubtype(chosenImportance - mStartingChannelImportance);
+    }
+
+    /**
+     * Returns an initialized LogMaker for logging open/close of the info display.
+     * The caller may override the type before passing it to mMetricsLogger.
+     * @return LogMaker
+     */
+    private LogMaker notificationControlsLogMaker() {
+        return getLogMaker().setCategory(MetricsEvent.ACTION_NOTE_CONTROLS)
+                .setType(MetricsEvent.TYPE_OPEN)
+                .setSubtype(mIsForBlockingHelper ? MetricsEvent.BLOCKING_HELPER_DISPLAY
+                        : MetricsEvent.BLOCKING_HELPER_UNKNOWN);
     }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/BiometricUnlockController.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/BiometricUnlockController.java
index 302d630..304d2ee 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/BiometricUnlockController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/BiometricUnlockController.java
@@ -47,7 +47,7 @@
     private static final String BIOMETRIC_WAKE_LOCK_NAME = "wake-and-unlock wakelock";
 
     /**
-     * Mode in which we don't need to wake up the device when we get a fingerprint.
+     * Mode in which we don't need to wake up the device when we authenticate.
      */
     public static final int MODE_NONE = 0;
 
@@ -70,8 +70,7 @@
     public static final int MODE_SHOW_BOUNCER = 3;
 
     /**
-     * Mode in which we only wake up the device, and keyguard was not showing when we acquired a
-     * fingerprint.
+     * Mode in which we only wake up the device, and keyguard was not showing when we authenticated.
      * */
     public static final int MODE_ONLY_WAKE = 4;
 
@@ -96,22 +95,21 @@
      */
     private static final float BIOMETRIC_COLLAPSE_SPEEDUP_FACTOR = 1.1f;
 
-    private final NotificationMediaManager mMediaManager =
-            Dependency.get(NotificationMediaManager.class);
-    private PowerManager mPowerManager;
-    private Handler mHandler = new Handler();
+    private final NotificationMediaManager mMediaManager;
+    private final PowerManager mPowerManager;
+    private final Handler mHandler;
     private PowerManager.WakeLock mWakeLock;
-    private KeyguardUpdateMonitor mUpdateMonitor;
+    private final KeyguardUpdateMonitor mUpdateMonitor;
+    private final UnlockMethodCache mUnlockMethodCache;
+    private final StatusBarWindowController mStatusBarWindowController;
+    private final Context mContext;
+    private final int mWakeUpDelay;
     private int mMode;
     private StatusBarKeyguardViewManager mStatusBarKeyguardViewManager;
-    private StatusBarWindowController mStatusBarWindowController;
     private DozeScrimController mDozeScrimController;
     private KeyguardViewMediator mKeyguardViewMediator;
     private ScrimController mScrimController;
     private StatusBar mStatusBar;
-    private final UnlockMethodCache mUnlockMethodCache;
-    private final Context mContext;
-    private final int mWakeUpDelay;
     private int mPendingAuthenticatedUserId = -1;
     private BiometricSourceType mPendingAuthenticatedBioSourceType = null;
     private boolean mPendingShowBouncer;
@@ -122,11 +120,14 @@
                                      KeyguardViewMediator keyguardViewMediator,
                                      ScrimController scrimController,
                                      StatusBar statusBar,
-                                     UnlockMethodCache unlockMethodCache) {
+                                     UnlockMethodCache unlockMethodCache, Handler handler,
+                                     KeyguardUpdateMonitor keyguardUpdateMonitor,
+                                     int wakeUpDelay) {
         mContext = context;
         mPowerManager = context.getSystemService(PowerManager.class);
-        mUpdateMonitor = KeyguardUpdateMonitor.getInstance(context);
+        mUpdateMonitor = keyguardUpdateMonitor;
         mUpdateMonitor.registerCallback(this);
+        mMediaManager = Dependency.get(NotificationMediaManager.class);
         Dependency.get(WakefulnessLifecycle.class).addObserver(mWakefulnessObserver);
         Dependency.get(ScreenLifecycle.class).addObserver(mScreenObserver);
         mStatusBarWindowController = Dependency.get(StatusBarWindowController.class);
@@ -135,8 +136,8 @@
         mScrimController = scrimController;
         mStatusBar = statusBar;
         mUnlockMethodCache = unlockMethodCache;
-        mWakeUpDelay = context.getResources().getInteger(
-                com.android.internal.R.integer.config_wakeUpDelayDoze);
+        mHandler = handler;
+        mWakeUpDelay = wakeUpDelay;
     }
 
     public void setStatusBarKeyguardViewManager(
@@ -206,7 +207,7 @@
             Trace.endSection();
             return;
         }
-        startWakeAndUnlock(calculateMode());
+        startWakeAndUnlock(calculateMode(biometricSourceType));
     }
 
     public void startWakeAndUnlock(int mode) {
@@ -295,7 +296,7 @@
     }
 
     private void showBouncer() {
-        if (calculateMode() == MODE_SHOW_BOUNCER) {
+        if (mMode == MODE_SHOW_BOUNCER) {
             mStatusBarKeyguardViewManager.showBouncer(false);
         }
         mStatusBarKeyguardViewManager.animateCollapsePanels(
@@ -339,29 +340,30 @@
         return mMode;
     }
 
-    private int calculateMode() {
+    private int calculateMode(BiometricSourceType biometricSourceType) {
         boolean unlockingAllowed = mUpdateMonitor.isUnlockingWithBiometricAllowed();
         boolean deviceDreaming = mUpdateMonitor.isDreaming();
+        boolean isFace = biometricSourceType == BiometricSourceType.FACE;
 
         if (!mUpdateMonitor.isDeviceInteractive()) {
             if (!mStatusBarKeyguardViewManager.isShowing()) {
                 return MODE_ONLY_WAKE;
             } else if (mDozeScrimController.isPulsing() && unlockingAllowed) {
-                return MODE_WAKE_AND_UNLOCK_PULSING;
+                return isFace ? MODE_NONE : MODE_WAKE_AND_UNLOCK_PULSING;
             } else if (unlockingAllowed || !mUnlockMethodCache.isMethodSecure()) {
                 return MODE_WAKE_AND_UNLOCK;
             } else {
                 return MODE_SHOW_BOUNCER;
             }
         }
-        if (unlockingAllowed && deviceDreaming) {
+        if (unlockingAllowed && deviceDreaming && !isFace) {
             return MODE_WAKE_AND_UNLOCK_FROM_DREAM;
         }
         if (mStatusBarKeyguardViewManager.isShowing()) {
             if (mStatusBarKeyguardViewManager.isBouncerShowing() && unlockingAllowed) {
                 return MODE_DISMISS_BOUNCER;
             } else if (unlockingAllowed) {
-                return MODE_UNLOCK;
+                return isFace ? MODE_ONLY_WAKE : MODE_UNLOCK;
             } else if (!mStatusBarKeyguardViewManager.isBouncerShowing()) {
                 return MODE_SHOW_BOUNCER;
             }
@@ -437,7 +439,7 @@
     }
 
     /**
-     * Successful authentication with fingerprint that wakes up the device.
+     * Successful authentication with fingerprint, face, or iris that wakes up the device.
      */
     public boolean isWakeAndUnlock() {
         return mMode == MODE_WAKE_AND_UNLOCK
@@ -446,7 +448,8 @@
     }
 
     /**
-     * Successful authentication with fingerprint when the screen was either on or off.
+     * Successful authentication with fingerprint, face, or iris when the screen was either
+     * on or off.
      */
     public boolean isBiometricUnlock() {
         return isWakeAndUnlock() || mMode == MODE_UNLOCK;
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java
index ceadd1e..c129143 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java
@@ -354,6 +354,8 @@
     protected StatusBarWindowController mStatusBarWindowController;
     protected UnlockMethodCache mUnlockMethodCache;
     @VisibleForTesting
+    KeyguardUpdateMonitor mKeyguardUpdateMonitor;
+    @VisibleForTesting
     DozeServiceHost mDozeServiceHost = new DozeServiceHost();
     private boolean mWakeUpComingFromTouch;
     private PointF mWakeUpTouchLocation;
@@ -480,8 +482,7 @@
             updateAodMaskVisibility(deviceSupportsAodWallpaper && aodImageWallpaperEnabled);
             // If WallpaperInfo is null, it must be ImageWallpaper.
             final boolean supportsAmbientMode = deviceSupportsAodWallpaper
-                    && (info == null && aodImageWallpaperEnabled
-                        || info != null && info.supportsAmbientMode());
+                    && (info == null || info.supportsAmbientMode());
 
             mStatusBarWindowController.setWallpaperSupportsAmbientMode(supportsAmbientMode);
             mScrimController.setWallpaperSupportsAmbientMode(supportsAmbientMode);
@@ -613,6 +614,7 @@
         mNotificationLogger = Dependency.get(NotificationLogger.class);
         mRemoteInputManager = Dependency.get(NotificationRemoteInputManager.class);
         mNotificationListener =  Dependency.get(NotificationListener.class);
+        mNotificationListener.registerAsSystemService();
         mNetworkController = Dependency.get(NetworkController.class);
         mUserSwitcherController = Dependency.get(UserSwitcherController.class);
         mScreenLifecycle = Dependency.get(ScreenLifecycle.class);
@@ -674,7 +676,7 @@
                 mContext.getSystemService(Context.ACCESSIBILITY_SERVICE);
 
         mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
-
+        mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(mContext);
         mBarService = IStatusBarService.Stub.asInterface(
                 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
 
@@ -761,7 +763,7 @@
         mUnlockMethodCache.addListener(this);
         startKeyguard();
 
-        KeyguardUpdateMonitor.getInstance(mContext).registerCallback(mUpdateCallback);
+        mKeyguardUpdateMonitor.registerCallback(mUpdateCallback);
         putComponent(DozeHost.class, mDozeServiceHost);
 
         mScreenPinningRequest = new ScreenPinningRequest(mContext);
@@ -1055,7 +1057,6 @@
                         mDeviceProvisionedController);
 
         mAppOpsController.addCallback(APP_OPS, this);
-        mNotificationListener.setUpWithPresenter(mPresenter);
         mNotificationShelf.setOnActivatedListener(mPresenter);
         mRemoteInputManager.getController().addCallback(mStatusBarWindowController);
 
@@ -1207,7 +1208,9 @@
         KeyguardViewMediator keyguardViewMediator = getComponent(KeyguardViewMediator.class);
         mBiometricUnlockController = new BiometricUnlockController(mContext,
                 mDozeScrimController, keyguardViewMediator,
-                mScrimController, this, UnlockMethodCache.getInstance(mContext));
+                mScrimController, this, UnlockMethodCache.getInstance(mContext),
+                new Handler(), mKeyguardUpdateMonitor, mContext.getResources().getInteger(
+                com.android.internal.R.integer.config_wakeUpDelayDoze));
         mStatusBarKeyguardViewManager = keyguardViewMediator.registerStatusBar(this,
                 getBouncerContainer(), mNotificationPanel, mBiometricUnlockController);
         mKeyguardIndicationController
@@ -1445,6 +1448,7 @@
         return new StatusBar.H();
     }
 
+    @Override
     public void startActivity(Intent intent, boolean onlyProvisioned, boolean dismissShade,
             int flags) {
         startActivityDismissingKeyguard(intent, onlyProvisioned, dismissShade, flags);
@@ -2357,8 +2361,8 @@
             mLightBarController.dump(fd, pw, args);
         }
 
-        if (KeyguardUpdateMonitor.getInstance(mContext) != null) {
-            KeyguardUpdateMonitor.getInstance(mContext).dump(fd, pw, args);
+        if (mKeyguardUpdateMonitor != null) {
+            mKeyguardUpdateMonitor.dump(fd, pw, args);
         }
 
         FalsingManager.getInstance(mContext).dump(pw);
@@ -3891,6 +3895,11 @@
                 return;
             }
 
+            if (mKeyguardUpdateMonitor != null
+                    && reason == DozeLog.PULSE_REASON_SENSOR_WAKE_LOCK_SCREEN) {
+                mKeyguardUpdateMonitor.onAuthInterruptDetected();
+            }
+
             // Set the state to pulsing, so ScrimController will know what to do once we ask it to
             // execute the transition. The pulse callback will then be invoked when the scrims
             // are black, indicating that StatusBar is ready to present the rest of the UI.
diff --git a/packages/SystemUI/src/com/android/systemui/wallpaper/AodMaskView.java b/packages/SystemUI/src/com/android/systemui/wallpaper/AodMaskView.java
index 52cabe2..dd1d0ca 100644
--- a/packages/SystemUI/src/com/android/systemui/wallpaper/AodMaskView.java
+++ b/packages/SystemUI/src/com/android/systemui/wallpaper/AodMaskView.java
@@ -156,6 +156,8 @@
 
     private boolean checkIfNeedMask() {
         // We need mask for ImageWallpaper / LockScreen Wallpaper (Music album art).
+        // Because of conflicting with another wallpaper feature,
+        // we only support LockScreen wallpaper currently.
         return mWallpaperManager.getWallpaperInfo() == null || ScrimState.AOD.hasBackdrop();
     }
 
diff --git a/packages/SystemUI/tests/src/com/android/keyguard/KeyguardUpdateMonitorTest.java b/packages/SystemUI/tests/src/com/android/keyguard/KeyguardUpdateMonitorTest.java
index 2055519..d5a275e 100644
--- a/packages/SystemUI/tests/src/com/android/keyguard/KeyguardUpdateMonitorTest.java
+++ b/packages/SystemUI/tests/src/com/android/keyguard/KeyguardUpdateMonitorTest.java
@@ -18,15 +18,33 @@
 
 import static com.google.common.truth.Truth.assertThat;
 
-import com.google.common.truth.Truth.*;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.reset;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
+import android.app.admin.DevicePolicyManager;
+import android.app.trust.TrustManager;
 import android.content.Context;
 import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.hardware.biometrics.BiometricManager;
+import android.hardware.biometrics.BiometricSourceType;
+import android.hardware.biometrics.IBiometricEnabledOnKeyguardCallback;
+import android.hardware.face.FaceManager;
+import android.hardware.fingerprint.FingerprintManager;
 import android.os.Bundle;
+import android.os.UserManager;
 import android.telephony.ServiceState;
 import android.telephony.SubscriptionManager;
 import android.test.suitebuilder.annotation.SmallTest;
 import android.testing.AndroidTestingRunner;
+import android.testing.TestableContext;
 import android.testing.TestableLooper;
 import android.testing.TestableLooper.RunWithLooper;
 
@@ -39,6 +57,8 @@
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
 
 import java.util.concurrent.atomic.AtomicBoolean;
 
@@ -52,37 +72,71 @@
 @RunWithLooper(setAsMainLooper = true)
 public class KeyguardUpdateMonitorTest extends SysuiTestCase {
 
+    @Mock
+    private KeyguardUpdateMonitor.StrongAuthTracker mStrongAuthTracker;
+    @Mock
+    private TrustManager mTrustManager;
+    @Mock
+    private FingerprintManager mFingerprintManager;
+    @Mock
+    private FaceManager mFaceManager;
+    @Mock
+    private BiometricManager mBiometricManager;
+    @Mock
+    private PackageManager mPackageManager;
+    @Mock
+    private UserManager mUserManager;
+    @Mock
+    private DevicePolicyManager mDevicePolicyManager;
     private TestableLooper mTestableLooper;
+    private TestableKeyguardUpdateMonitor mKeyguardUpdateMonitor;
 
     @Before
     public void setup() {
+        MockitoAnnotations.initMocks(this);
+        TestableContext context = spy(mContext);
+        when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true);
+        when(context.getPackageManager()).thenReturn(mPackageManager);
+        doAnswer(invocation -> {
+            IBiometricEnabledOnKeyguardCallback callback = invocation.getArgument(0);
+            callback.onChanged(BiometricSourceType.FACE, true /* enabled */);
+            return null;
+        }).when(mBiometricManager).registerEnabledOnKeyguardCallback(any());
+        when(mFaceManager.isHardwareDetected()).thenReturn(true);
+        when(mFaceManager.hasEnrolledTemplates()).thenReturn(true);
+        when(mFaceManager.hasEnrolledTemplates(anyInt())).thenReturn(true);
+        when(mUserManager.isUserUnlocked(anyInt())).thenReturn(true);
+        when(mStrongAuthTracker.isUnlockingWithBiometricAllowed()).thenReturn(true);
+        context.addMockSystemService(TrustManager.class, mTrustManager);
+        context.addMockSystemService(FingerprintManager.class, mFingerprintManager);
+        context.addMockSystemService(BiometricManager.class, mBiometricManager);
+        context.addMockSystemService(FaceManager.class, mFaceManager);
+        context.addMockSystemService(UserManager.class, mUserManager);
+        context.addMockSystemService(DevicePolicyManager.class, mDevicePolicyManager);
+
         mTestableLooper = TestableLooper.get(this);
+        mKeyguardUpdateMonitor = new TestableKeyguardUpdateMonitor(context);
     }
 
     @Test
     public void testIgnoresSimStateCallback_rebroadcast() {
         Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
 
-        TestableKeyguardUpdateMonitor keyguardUpdateMonitor =
-                new TestableKeyguardUpdateMonitor(getContext());
-
-        keyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent);
+        mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent);
         mTestableLooper.processAllMessages();
         Assert.assertTrue("onSimStateChanged not called",
-                keyguardUpdateMonitor.hasSimStateJustChanged());
+                mKeyguardUpdateMonitor.hasSimStateJustChanged());
 
         intent.putExtra(TelephonyIntents.EXTRA_REBROADCAST_ON_UNLOCK, true);
-        keyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent);
+        mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent);
         mTestableLooper.processAllMessages();
         Assert.assertFalse("onSimStateChanged should have been skipped",
-                keyguardUpdateMonitor.hasSimStateJustChanged());
+                mKeyguardUpdateMonitor.hasSimStateJustChanged());
     }
 
     @Test
     public void testTelephonyCapable_BootInitState() {
-        TestableKeyguardUpdateMonitor keyguardUpdateMonitor =
-                new TestableKeyguardUpdateMonitor(getContext());
-        assertThat(keyguardUpdateMonitor.mTelephonyCapable).isFalse();
+        assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
     }
 
     @Test
@@ -90,36 +144,30 @@
         Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
         intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE
                 , IccCardConstants.INTENT_VALUE_ICC_ABSENT);
-        TestableKeyguardUpdateMonitor keyguardUpdateMonitor =
-                new TestableKeyguardUpdateMonitor(getContext());
-        keyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
+        mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
                 , putPhoneInfo(intent,null, false));
         mTestableLooper.processAllMessages();
-        assertThat(keyguardUpdateMonitor.mTelephonyCapable).isTrue();
+        assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
     }
 
     @Test
     public void testTelephonyCapable_SimInvalid_ServiceState_InService() {
         // SERVICE_STATE - IN_SERVICE, but SIM_STATE is invalid TelephonyCapable should be False
-        TestableKeyguardUpdateMonitor keyguardUpdateMonitor =
-                new TestableKeyguardUpdateMonitor(getContext());
         Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
         Bundle data = new Bundle();
         ServiceState state = new ServiceState();
         state.setState(ServiceState.STATE_IN_SERVICE);
         state.fillInNotifierBundle(data);
-        keyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
+        mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
                 , putPhoneInfo(intent, data, false));
         mTestableLooper.processAllMessages();
-        assertThat(keyguardUpdateMonitor.mTelephonyCapable).isFalse();
+        assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
     }
 
     @Test
     public void testTelephonyCapable_SimValid_ServiceState_PowerOff() {
         // Simulate AirplaneMode case, SERVICE_STATE - POWER_OFF, check TelephonyCapable False
         // Only receive ServiceState callback IN_SERVICE -> OUT_OF_SERVICE -> POWER_OFF
-        TestableKeyguardUpdateMonitor keyguardUpdateMonitor =
-                new TestableKeyguardUpdateMonitor(getContext());
         Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
         intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE
                 , IccCardConstants.INTENT_VALUE_ICC_LOADED);
@@ -127,10 +175,10 @@
         ServiceState state = new ServiceState();
         state.setState(ServiceState.STATE_POWER_OFF);
         state.fillInNotifierBundle(data);
-        keyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
+        mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
                 , putPhoneInfo(intent, data, true));
         mTestableLooper.processAllMessages();
-        assertThat(keyguardUpdateMonitor.mTelephonyCapable).isTrue();
+        assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
     }
 
     /* Normal SIM inserted flow
@@ -142,24 +190,20 @@
      */
     @Test
     public void testTelephonyCapable_BootInitState_ServiceState_OutOfService() {
-        TestableKeyguardUpdateMonitor keyguardUpdateMonitor =
-                new TestableKeyguardUpdateMonitor(getContext());
         Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
         Bundle data = new Bundle();
         ServiceState state = new ServiceState();
         state.setState(ServiceState.STATE_OUT_OF_SERVICE);
         state.fillInNotifierBundle(data);
         intent.putExtras(data);
-        keyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
+        mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
                 , putPhoneInfo(intent, data, false));
         mTestableLooper.processAllMessages();
-        assertThat(keyguardUpdateMonitor.mTelephonyCapable).isFalse();
+        assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
     }
 
     @Test
     public void testTelephonyCapable_BootInitState_SimState_NotReady() {
-        TestableKeyguardUpdateMonitor keyguardUpdateMonitor =
-                new TestableKeyguardUpdateMonitor(getContext());
         Bundle data = new Bundle();
         ServiceState state = new ServiceState();
         state.setState(ServiceState.STATE_OUT_OF_SERVICE);
@@ -167,16 +211,14 @@
         Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
         intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE
                 , IccCardConstants.INTENT_VALUE_ICC_NOT_READY);
-        keyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
+        mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
                 , putPhoneInfo(intent, data, false));
         mTestableLooper.processAllMessages();
-        assertThat(keyguardUpdateMonitor.mTelephonyCapable).isFalse();
+        assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
     }
 
     @Test
     public void testTelephonyCapable_BootInitState_SimState_Ready() {
-        TestableKeyguardUpdateMonitor keyguardUpdateMonitor =
-                new TestableKeyguardUpdateMonitor(getContext());
         Bundle data = new Bundle();
         ServiceState state = new ServiceState();
         state.setState(ServiceState.STATE_OUT_OF_SERVICE);
@@ -184,46 +226,40 @@
         Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
         intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE
                 , IccCardConstants.INTENT_VALUE_ICC_READY);
-        keyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
+        mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
                 , putPhoneInfo(intent, data, false));
         mTestableLooper.processAllMessages();
-        assertThat(keyguardUpdateMonitor.mTelephonyCapable).isFalse();
+        assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
     }
 
     @Test
     public void testTelephonyCapable_BootInitState_ServiceState_PowerOff() {
-        TestableKeyguardUpdateMonitor keyguardUpdateMonitor =
-                new TestableKeyguardUpdateMonitor(getContext());
         Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
         Bundle data = new Bundle();
         ServiceState state = new ServiceState();
         state.setState(ServiceState.STATE_POWER_OFF);
         state.fillInNotifierBundle(data);
-        keyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
+        mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
                 , putPhoneInfo(intent, data, false));
         mTestableLooper.processAllMessages();
-        assertThat(keyguardUpdateMonitor.mTelephonyCapable).isFalse();
+        assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
     }
 
     @Test
     public void testTelephonyCapable_SimValid_ServiceState_InService() {
-        TestableKeyguardUpdateMonitor keyguardUpdateMonitor =
-                new TestableKeyguardUpdateMonitor(getContext());
         Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
         Bundle data = new Bundle();
         ServiceState state = new ServiceState();
         state.setState(ServiceState.STATE_IN_SERVICE);
         state.fillInNotifierBundle(data);
-        keyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
+        mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
                 , putPhoneInfo(intent, data, true));
         mTestableLooper.processAllMessages();
-        assertThat(keyguardUpdateMonitor.mTelephonyCapable).isTrue();
+        assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
     }
 
     @Test
     public void testTelephonyCapable_SimValid_SimState_Loaded() {
-        TestableKeyguardUpdateMonitor keyguardUpdateMonitor =
-                new TestableKeyguardUpdateMonitor(getContext());
         Bundle data = new Bundle();
         ServiceState state = new ServiceState();
         state.setState(ServiceState.STATE_IN_SERVICE);
@@ -231,19 +267,96 @@
         Intent intentSimState = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
         intentSimState.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE
                 , IccCardConstants.INTENT_VALUE_ICC_LOADED);
-        keyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
+        mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
                 , putPhoneInfo(intentSimState, data, true));
         mTestableLooper.processAllMessages();
         // Even SimState Loaded, still need ACTION_SERVICE_STATE_CHANGED turn on mTelephonyCapable
-        assertThat(keyguardUpdateMonitor.mTelephonyCapable).isFalse();
+        assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
 
         Intent intentServiceState =  new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
         intentSimState.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE
                 , IccCardConstants.INTENT_VALUE_ICC_LOADED);
-        keyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
+        mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
                 , putPhoneInfo(intentServiceState, data, true));
         mTestableLooper.processAllMessages();
-        assertThat(keyguardUpdateMonitor.mTelephonyCapable).isTrue();
+        assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
+    }
+
+    @Test
+    public void testTriesToAuthenticate_whenBouncer() {
+        mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true);
+        mTestableLooper.processAllMessages();
+
+        verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any());
+        verify(mFaceManager).isHardwareDetected();
+        verify(mFaceManager).hasEnrolledTemplates(anyInt());
+    }
+
+    @Test
+    public void testTriesToAuthenticate_whenKeyguard() {
+        mKeyguardUpdateMonitor.dispatchStartedWakingUp();
+        mTestableLooper.processAllMessages();
+        mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
+        verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any());
+    }
+
+    @Test
+    public void skipsAuthentication_whenEncryptedKeyguard() {
+        reset(mUserManager);
+        when(mUserManager.isUserUnlocked(anyInt())).thenReturn(false);
+
+        mKeyguardUpdateMonitor.dispatchStartedWakingUp();
+        mTestableLooper.processAllMessages();
+        mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
+        verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any());
+    }
+
+    @Test
+    public void testTriesToAuthenticate_whenAssistant() {
+        mKeyguardUpdateMonitor.setKeyguardOccluded(true);
+        mKeyguardUpdateMonitor.setAssistantVisible(true);
+
+        verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any());
+    }
+
+    @Test
+    public void testNeverAuthenticates_whenFaceLockout() {
+        mKeyguardUpdateMonitor.mFaceAuthenticationCallback
+                .onAuthenticationError(FaceManager.FACE_ERROR_LOCKOUT, "lockout");
+        mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true);
+        mTestableLooper.processAllMessages();
+
+        verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any());
+    }
+
+    @Test
+    public void testOnFaceAuthenticated_skipsFaceWhenAuthenticated() {
+        mKeyguardUpdateMonitor.onFaceAuthenticated(KeyguardUpdateMonitor.getCurrentUser());
+        mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true);
+        mTestableLooper.processAllMessages();
+
+        verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any());
+    }
+
+    @Test
+    public void testGetUserCanSkipBouncer_whenFace() {
+        int user = KeyguardUpdateMonitor.getCurrentUser();
+        mKeyguardUpdateMonitor.onFaceAuthenticated(user);
+        assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue();
+    }
+
+    @Test
+    public void testGetUserCanSkipBouncer_whenFingerprint() {
+        int user = KeyguardUpdateMonitor.getCurrentUser();
+        mKeyguardUpdateMonitor.onFingerprintAuthenticated(user);
+        assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue();
+    }
+
+    @Test
+    public void testGetUserCanSkipBouncer_whenTrust() {
+        int user = KeyguardUpdateMonitor.getCurrentUser();
+        mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */, user, 0 /* flags */);
+        assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue();
     }
 
     private Intent putPhoneInfo(Intent intent, Bundle data, Boolean simInited) {
@@ -261,8 +374,9 @@
 
         protected TestableKeyguardUpdateMonitor(Context context) {
             super(context);
-            // Avoid race condition when unexpected broadcast could be received.
             context.unregisterReceiver(mBroadcastReceiver);
+            context.unregisterReceiver(mBroadcastAllReceiver);
+            mStrongAuthTracker = KeyguardUpdateMonitorTest.this.mStrongAuthTracker;
         }
 
         public boolean hasSimStateJustChanged() {
diff --git a/packages/SystemUI/tests/src/com/android/systemui/bubbles/BubbleControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/bubbles/BubbleControllerTest.java
index 6a3bd73..5be8826 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/bubbles/BubbleControllerTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/bubbles/BubbleControllerTest.java
@@ -77,6 +77,10 @@
 
     @Mock
     private NotificationData mNotificationData;
+    @Mock
+    private BubbleController.BubbleStateChangeListener mBubbleStateChangeListener;
+    @Mock
+    private BubbleController.BubbleExpandListener mBubbleExpandListener;
 
     @Before
     public void setUp() throws Exception {
@@ -101,6 +105,8 @@
         when(mNotificationData.getChannel(mNoChannelRow.getEntry().key)).thenReturn(null);
 
         mBubbleController = new TestableBubbleController(mContext, mStatusBarWindowController);
+        mBubbleController.setBubbleStateChangeListener(mBubbleStateChangeListener);
+        mBubbleController.setExpandListener(mBubbleExpandListener);
 
         // Get a reference to the BubbleController's entry listener
         verify(mNotificationEntryManager, atLeastOnce())
@@ -112,6 +118,8 @@
     public void testAddBubble() {
         mBubbleController.updateBubble(mRow.getEntry(), true /* updatePosition */);
         assertTrue(mBubbleController.hasBubbles());
+
+        verify(mBubbleStateChangeListener).onHasBubblesChanged(true);
     }
 
     @Test
@@ -126,10 +134,14 @@
         mBubbleController.updateBubble(mRow.getEntry(), true /* updatePosition */);
         assertTrue(mBubbleController.hasBubbles());
 
+        verify(mBubbleStateChangeListener).onHasBubblesChanged(true);
+
         mBubbleController.removeBubble(mRow.getEntry().key);
         assertFalse(mStatusBarWindowController.getBubblesShowing());
         assertTrue(mRow.getEntry().isBubbleDismissed());
         verify(mNotificationEntryManager).updateNotifications();
+
+        verify(mBubbleStateChangeListener).onHasBubblesChanged(false);
     }
 
     @Test
@@ -141,41 +153,133 @@
         mBubbleController.dismissStack();
         assertFalse(mStatusBarWindowController.getBubblesShowing());
         verify(mNotificationEntryManager).updateNotifications();
+        assertTrue(mRow.getEntry().isBubbleDismissed());
+        assertTrue(mRow2.getEntry().isBubbleDismissed());
     }
 
     @Test
-    public void testIsStackExpanded() {
+    public void testExpandCollapseStack() {
         assertFalse(mBubbleController.isStackExpanded());
+
+        // Mark it as a bubble and add it explicitly
+        mEntryListener.onPendingEntryAdded(mRow.getEntry());
         mBubbleController.updateBubble(mRow.getEntry(), true /* updatePosition */);
 
+        // We should have bubbles & their notifs should show in the shade
+        assertTrue(mBubbleController.hasBubbles());
+        assertTrue(mRow.getEntry().showInShadeWhenBubble());
+
+        // Expand the stack
         BubbleStackView stackView = mBubbleController.getStackView();
         stackView.expandStack();
         assertTrue(mBubbleController.isStackExpanded());
+        verify(mBubbleExpandListener).onBubbleExpandChanged(true, mRow.getEntry().key);
 
+        // Make sure it's no longer in the shade
+        assertFalse(mRow.getEntry().showInShadeWhenBubble());
+
+        // Collapse
         stackView.collapseStack();
+        verify(mBubbleExpandListener).onBubbleExpandChanged(false, mRow.getEntry().key);
         assertFalse(mBubbleController.isStackExpanded());
     }
 
     @Test
-    public void testCollapseStack() {
+    public void testCollapseAfterChangingExpandedBubble() {
+        // Mark it as a bubble and add it explicitly
+        mEntryListener.onPendingEntryAdded(mRow.getEntry());
+        mEntryListener.onPendingEntryAdded(mRow2.getEntry());
         mBubbleController.updateBubble(mRow.getEntry(), true /* updatePosition */);
         mBubbleController.updateBubble(mRow2.getEntry(), true /* updatePosition */);
 
+        // We should have bubbles & their notifs should show in the shade
+        assertTrue(mBubbleController.hasBubbles());
+        assertTrue(mRow.getEntry().showInShadeWhenBubble());
+        assertTrue(mRow2.getEntry().showInShadeWhenBubble());
+
+        // Expand
         BubbleStackView stackView = mBubbleController.getStackView();
         stackView.expandStack();
         assertTrue(mBubbleController.isStackExpanded());
+        verify(mBubbleExpandListener).onBubbleExpandChanged(true, mRow2.getEntry().key);
 
+        // Last added is the one that is expanded
+        assertEquals(mRow2.getEntry(), stackView.getExpandedBubble().getEntry());
+        assertFalse(mRow2.getEntry().showInShadeWhenBubble());
+
+        // Switch which bubble is expanded
         stackView.setExpandedBubble(mRow.getEntry());
-        assertEquals(stackView.getExpandedBubble().getEntry(), mRow.getEntry());
+        assertEquals(mRow.getEntry(), stackView.getExpandedBubble().getEntry());
+        assertFalse(mRow.getEntry().showInShadeWhenBubble());
 
-        stackView.setExpandedBubble(mRow2.getEntry());
-        assertEquals(stackView.getExpandedBubble().getEntry(), mRow2.getEntry());
+        // collapse for previous bubble
+        verify(mBubbleExpandListener).onBubbleExpandChanged(false, mRow2.getEntry().key);
+        // expand for selected bubble
+        verify(mBubbleExpandListener).onBubbleExpandChanged(true, mRow.getEntry().key);
 
+        // Collapse
         mBubbleController.collapseStack();
         assertFalse(mBubbleController.isStackExpanded());
     }
 
     @Test
+    public void testExpansionRemovesShowInShade() {
+        // Mark it as a bubble and add it explicitly
+        mEntryListener.onPendingEntryAdded(mRow.getEntry());
+        mBubbleController.updateBubble(mRow.getEntry(), true /* updatePosition */);
+
+        // We should have bubbles & their notifs should show in the shade
+        assertTrue(mBubbleController.hasBubbles());
+        assertTrue(mRow.getEntry().showInShadeWhenBubble());
+
+        // Expand
+        BubbleStackView stackView = mBubbleController.getStackView();
+        stackView.expandStack();
+        assertTrue(mBubbleController.isStackExpanded());
+        verify(mBubbleExpandListener).onBubbleExpandChanged(true, mRow.getEntry().key);
+
+        // No longer show shade in notif after expansion
+        assertFalse(mRow.getEntry().showInShadeWhenBubble());
+    }
+
+    @Test
+    public void testRemoveLastExpandedCollapses() {
+        // Mark it as a bubble and add it explicitly
+        mEntryListener.onPendingEntryAdded(mRow.getEntry());
+        mEntryListener.onPendingEntryAdded(mRow2.getEntry());
+        mBubbleController.updateBubble(mRow.getEntry(), true /* updatePosition */);
+        mBubbleController.updateBubble(mRow2.getEntry(), true /* updatePosition */);
+        verify(mBubbleStateChangeListener).onHasBubblesChanged(true);
+
+        // Expand
+        BubbleStackView stackView = mBubbleController.getStackView();
+        stackView.expandStack();
+
+        assertTrue(mBubbleController.isStackExpanded());
+        verify(mBubbleExpandListener).onBubbleExpandChanged(true, mRow2.getEntry().key);
+
+        // Last added is the one that is expanded
+        assertEquals(mRow2.getEntry(), stackView.getExpandedBubble().getEntry());
+        assertFalse(mRow2.getEntry().showInShadeWhenBubble());
+
+        // Dismiss currently expanded
+        mBubbleController.removeBubble(stackView.getExpandedBubble().getKey());
+        verify(mBubbleExpandListener).onBubbleExpandChanged(false, mRow2.getEntry().key);
+
+        // Make sure next bubble is selected
+        assertEquals(mRow.getEntry(), stackView.getExpandedBubble().getEntry());
+        verify(mBubbleExpandListener).onBubbleExpandChanged(true, mRow.getEntry().key);
+
+        // Dismiss that one
+        mBubbleController.removeBubble(stackView.getExpandedBubble().getKey());
+
+        // Make sure state changes and collapse happens
+        verify(mBubbleExpandListener).onBubbleExpandChanged(false, mRow.getEntry().key);
+        verify(mBubbleStateChangeListener).onHasBubblesChanged(false);
+        assertFalse(mBubbleController.hasBubbles());
+    }
+
+    @Test
     public void testMarkNewNotificationAsBubble() {
         mEntryListener.onPendingEntryAdded(mRow.getEntry());
         assertTrue(mRow.getEntry().isBubble());
diff --git a/packages/SystemUI/tests/src/com/android/systemui/bubbles/animation/PhysicsAnimationLayoutTest.java b/packages/SystemUI/tests/src/com/android/systemui/bubbles/animation/PhysicsAnimationLayoutTest.java
index bfc02d9..5be991f 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/bubbles/animation/PhysicsAnimationLayoutTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/bubbles/animation/PhysicsAnimationLayoutTest.java
@@ -75,7 +75,7 @@
     }
 
     @Test
-    public void testRenderVisibility() {
+    public void testRenderVisibility() throws InterruptedException {
         mLayout.setController(mTestableController);
         addOneMoreThanRenderLimitBubbles();
 
@@ -87,7 +87,7 @@
     }
 
     @Test
-    public void testHierarchyChanges() {
+    public void testHierarchyChanges() throws InterruptedException {
         mLayout.setController(mTestableController);
         addOneMoreThanRenderLimitBubbles();
 
@@ -242,26 +242,6 @@
     }
 
     @Test
-    public void testPrecedingNonRemovedIndex() {
-        mLayout.setController(mTestableController);
-        addOneMoreThanRenderLimitBubbles();
-
-        // Call removeView at index 4, but don't actually remove it yet (as if we're animating it
-        // out). The preceding, non-removed view index to 3 should initially be 4, but then 5 since
-        // 4 is on its way out.
-        assertEquals(4, mLayout.getPrecedingNonRemovedViewIndex(3));
-        mLayout.removeView(mViews.get(4));
-        assertEquals(5, mLayout.getPrecedingNonRemovedViewIndex(3));
-
-        // Call removeView at index 1, and actually remove it immediately. With the old view at 1
-        // instantly gone, the preceding view to 0 should be 1 in both cases.
-        assertEquals(1, mLayout.getPrecedingNonRemovedViewIndex(0));
-        mTestableController.setRemoveImmediately(true);
-        mLayout.removeView(mViews.get(1));
-        assertEquals(1, mLayout.getPrecedingNonRemovedViewIndex(0));
-    }
-
-    @Test
     public void testSetController() throws InterruptedException {
         // Add the bubbles, then set the controller, to make sure that a controller added to an
         // already-initialized view works correctly.
@@ -360,8 +340,6 @@
 
         mLayout.cancelAllAnimations();
 
-        waitForLayoutMessageQueue();
-
         // Animations should be somewhere before their end point.
         assertTrue(mViews.get(0).getTranslationX() < 1000);
         assertTrue(mViews.get(0).getTranslationY() < 1000);
diff --git a/packages/SystemUI/tests/src/com/android/systemui/bubbles/animation/PhysicsAnimationLayoutTestCase.java b/packages/SystemUI/tests/src/com/android/systemui/bubbles/animation/PhysicsAnimationLayoutTestCase.java
index 186a762..31e44d7 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/bubbles/animation/PhysicsAnimationLayoutTestCase.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/bubbles/animation/PhysicsAnimationLayoutTestCase.java
@@ -23,6 +23,7 @@
 import android.os.Looper;
 import android.view.DisplayCutout;
 import android.view.View;
+import android.view.ViewGroup;
 import android.view.WindowInsets;
 import android.widget.FrameLayout;
 
@@ -93,7 +94,7 @@
     }
 
     /** Add one extra bubble over the limit, so we can make sure it's gone/chains appropriately. */
-    void addOneMoreThanRenderLimitBubbles() {
+    void addOneMoreThanRenderLimitBubbles() throws InterruptedException {
         for (int i = 0; i < mMaxRenderedBubbles + 1; i++) {
             final View newView = new FrameLayout(mContext);
             mLayout.addView(newView, 0);
@@ -129,7 +130,7 @@
     void waitForLayoutMessageQueue() throws InterruptedException {
         // Wait for layout, then the view should be actually removed.
         CountDownLatch layoutLatch = new CountDownLatch(1);
-        mLayout.post(layoutLatch::countDown);
+        mMainThreadHandler.post(layoutLatch::countDown);
         layoutLatch.await(1, TimeUnit.SECONDS);
     }
 
@@ -145,11 +146,7 @@
         @Override
         public void setController(PhysicsAnimationController controller) {
             mMainThreadHandler.post(() -> super.setController(controller));
-            try {
-                waitForLayoutMessageQueue();
-            } catch (InterruptedException e) {
-                e.printStackTrace();
-            }
+            waitForMessageQueueAndIgnoreIfInterrupted();
         }
 
         @Override
@@ -169,6 +166,32 @@
             return mWindowInsets;
         }
 
+        @Override
+        public void removeView(View view) {
+            mMainThreadHandler.post(() ->
+                    super.removeView(view));
+            waitForMessageQueueAndIgnoreIfInterrupted();
+        }
+
+        @Override
+        public void addView(View child, int index, ViewGroup.LayoutParams params) {
+            mMainThreadHandler.post(() ->
+                    super.addView(child, index, params));
+            waitForMessageQueueAndIgnoreIfInterrupted();
+        }
+
+        /**
+         * Wait for the queue but just catch and print the exception if interrupted, since we can't
+         * just add the exception to the overridden methods' signatures.
+         */
+        private void waitForMessageQueueAndIgnoreIfInterrupted() {
+            try {
+                waitForLayoutMessageQueue();
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+        }
+
         /**
          * Sets an end listener that will be called after the 'real' end listener that was already
          * set.
diff --git a/packages/SystemUI/tests/src/com/android/systemui/bubbles/animation/StackAnimationControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/bubbles/animation/StackAnimationControllerTest.java
index db819d5..97935ed 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/bubbles/animation/StackAnimationControllerTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/bubbles/animation/StackAnimationControllerTest.java
@@ -178,17 +178,20 @@
 
     @Test
     public void testChildRemoved() throws InterruptedException {
+        assertEquals(0, mLayout.getTransientViewCount());
+
         final View firstView = mLayout.getChildAt(0);
         mLayout.removeView(firstView);
 
-        // The view should still be there, since the controller is animating it out and hasn't yet
-        // actually removed it from the parent view.
-        assertEquals(0, mLayout.indexOfChild(firstView));
+        // The view should now be transient, and missing from the view's normal hierarchy.
+        assertEquals(1, mLayout.getTransientViewCount());
+        assertEquals(-1, mLayout.indexOfChild(firstView));
 
         waitForPropertyAnimations(DynamicAnimation.ALPHA);
         waitForLayoutMessageQueue();
 
-        assertEquals(-1, mLayout.indexOfChild(firstView));
+        // The view should now be gone entirely, no transient views left.
+        assertEquals(0, mLayout.getTransientViewCount());
 
         // The subsequent view should have been translated over to 0, not stacked off to the left.
         assertEquals(0, mLayout.getChildAt(0).getTranslationX(), .1f);
diff --git a/packages/SystemUI/tests/src/com/android/systemui/doze/DozeMachineTest.java b/packages/SystemUI/tests/src/com/android/systemui/doze/DozeMachineTest.java
index e1c481e..5a6200f 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/doze/DozeMachineTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/doze/DozeMachineTest.java
@@ -230,9 +230,9 @@
     public void testPulseReason_getMatchesRequest() {
         mMachine.requestState(INITIALIZED);
         mMachine.requestState(DOZE);
-        mMachine.requestPulse(DozeLog.PULSE_REASON_SENSOR_DOUBLE_TAP);
+        mMachine.requestPulse(DozeLog.REASON_SENSOR_DOUBLE_TAP);
 
-        assertEquals(DozeLog.PULSE_REASON_SENSOR_DOUBLE_TAP, mMachine.getPulseReason());
+        assertEquals(DozeLog.REASON_SENSOR_DOUBLE_TAP, mMachine.getPulseReason());
     }
 
     @Test
diff --git a/packages/SystemUI/tests/src/com/android/systemui/doze/DozeTriggersTest.java b/packages/SystemUI/tests/src/com/android/systemui/doze/DozeTriggersTest.java
index 7b358b9..cdac7c97 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/doze/DozeTriggersTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/doze/DozeTriggersTest.java
@@ -125,7 +125,7 @@
     public void testOnSensor_whenUndockedWithNearAndDoubleTapScreen_shouldNotWakeUp() {
         mSensors.getMockProximitySensor().sendProximityResult(false /* far */);
 
-        mTriggers.onSensor(DozeLog.PULSE_REASON_SENSOR_DOUBLE_TAP,
+        mTriggers.onSensor(DozeLog.REASON_SENSOR_DOUBLE_TAP,
                 false /* sensorPerformedProxCheck */, 50 /* screenX */, 50 /* screenY */,
                 null /* rawValues */);
 
@@ -137,7 +137,7 @@
         doReturn(true).when(mDockManagerFake).isDocked();
         mSensors.getMockProximitySensor().sendProximityResult(false /* far */);
 
-        mTriggers.onSensor(DozeLog.PULSE_REASON_SENSOR_DOUBLE_TAP,
+        mTriggers.onSensor(DozeLog.REASON_SENSOR_DOUBLE_TAP,
                 false /* sensorPerformedProxCheck */, 50 /* screenX */, 50 /* screenY */,
                 null /* rawValues */);
 
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/NonPhoneDependencyTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/NonPhoneDependencyTest.java
index 425ca58..8e926848 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/NonPhoneDependencyTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/NonPhoneDependencyTest.java
@@ -97,7 +97,6 @@
                 mDelegate);
         lockscreenUserManager.setUpWithPresenter(mPresenter);
         viewHierarchyManager.setUpWithPresenter(mPresenter, mListContainer);
-        notificationListener.setUpWithPresenter(mPresenter);
 
         TestableLooper.get(this).processAllMessages();
         assertFalse(mDependency.hasInstantiatedDependency(StatusBarWindowController.class));
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationListenerTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationListenerTest.java
index bcf5964..cffa9c7 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationListenerTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationListenerTest.java
@@ -77,8 +77,6 @@
         mListener = new NotificationListener(mContext);
         mSbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME, 0, null, TEST_UID, 0,
                 new Notification(), UserHandle.CURRENT, null, 0);
-
-        mListener.setUpWithPresenter(mPresenter);
     }
 
     @Test
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/row/NotificationContentViewTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/row/NotificationContentViewTest.java
index d94bf84..c80396d 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/row/NotificationContentViewTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/row/NotificationContentViewTest.java
@@ -193,14 +193,33 @@
     }
 
     @Test
-    public void chooseSmartRepliesAndActions_smartRepliesOff_noAppGeneratedSmartReplies() {
-        setupAppGeneratedReplies(new String[] {"Reply1", "Reply2"});
+    public void chooseSmartRepliesAndActions_smartRepliesOff_noAppGeneratedSmartSuggestions() {
+        CharSequence[] smartReplies = new String[] {"Reply1", "Reply2"};
+        List<Notification.Action> smartActions =
+                createActions(new String[] {"Test Action 1", "Test Action 2"});
+        setupAppGeneratedSuggestions(smartReplies, smartActions);
         when(mSmartReplyConstants.isEnabled()).thenReturn(false);
 
         NotificationContentView.SmartRepliesAndActions repliesAndActions =
                 NotificationContentView.chooseSmartRepliesAndActions(mSmartReplyConstants, mEntry);
 
         assertThat(repliesAndActions.smartReplies).isNull();
+        assertThat(repliesAndActions.smartActions).isNull();
+    }
+
+    @Test
+    public void chooseSmartRepliesAndActions_smartRepliesOff_noSystemGeneratedSmartSuggestions() {
+        mEntry.systemGeneratedSmartReplies =
+                new String[] {"Sys Smart Reply 1", "Sys Smart Reply 2"};
+        mEntry.systemGeneratedSmartActions =
+                createActions(new String[] {"Sys Smart Action 1", "Sys Smart Action 2"});
+        when(mSmartReplyConstants.isEnabled()).thenReturn(false);
+
+        NotificationContentView.SmartRepliesAndActions repliesAndActions =
+                NotificationContentView.chooseSmartRepliesAndActions(mSmartReplyConstants, mEntry);
+
+        assertThat(repliesAndActions.smartReplies).isNull();
+        assertThat(repliesAndActions.smartActions).isNull();
     }
 
     @Test
@@ -238,11 +257,13 @@
         // replies.
         setupAppGeneratedReplies(null /* smartReplies */);
 
-        mEntry.smartReplies = new String[] {"Sys Smart Reply 1", "Sys Smart Reply 2"};
+        mEntry.systemGeneratedSmartReplies =
+                new String[] {"Sys Smart Reply 1", "Sys Smart Reply 2"};
         NotificationContentView.SmartRepliesAndActions repliesAndActions =
                 NotificationContentView.chooseSmartRepliesAndActions(mSmartReplyConstants, mEntry);
 
-        assertThat(repliesAndActions.smartReplies.choices).isEqualTo(mEntry.smartReplies);
+        assertThat(repliesAndActions.smartReplies.choices).isEqualTo(
+                mEntry.systemGeneratedSmartReplies);
         assertThat(repliesAndActions.smartReplies.fromAssistant).isTrue();
         assertThat(repliesAndActions.smartActions).isNull();
     }
@@ -253,7 +274,7 @@
         // replies.
         setupAppGeneratedReplies(null /* smartReplies */, false /* allowSystemGeneratedReplies */);
 
-        mEntry.smartReplies =
+        mEntry.systemGeneratedSmartReplies =
                 new String[] {"Sys Smart Reply 1", "Sys Smart Reply 2"};
         NotificationContentView.SmartRepliesAndActions repliesAndActions =
                 NotificationContentView.chooseSmartRepliesAndActions(mSmartReplyConstants, mEntry);
@@ -288,7 +309,8 @@
                 createActions(new String[] {"Test Action 1", "Test Action 2"});
         setupAppGeneratedSuggestions(appGenSmartReplies, appGenSmartActions);
 
-        mEntry.smartReplies = new String[] {"Sys Smart Reply 1", "Sys Smart Reply 2"};
+        mEntry.systemGeneratedSmartReplies =
+                new String[] {"Sys Smart Reply 1", "Sys Smart Reply 2"};
         mEntry.systemGeneratedSmartActions =
                 createActions(new String[] {"Sys Smart Action 1", "Sys Smart Action 2"});
 
@@ -307,7 +329,8 @@
         // actions.
         setupAppGeneratedReplies(null /* smartReplies */, false /* allowSystemGeneratedReplies */);
         when(mNotification.getAllowSystemGeneratedContextualActions()).thenReturn(false);
-        mEntry.smartReplies = new String[] {"Sys Smart Reply 1", "Sys Smart Reply 2"};
+        mEntry.systemGeneratedSmartReplies =
+                new String[] {"Sys Smart Reply 1", "Sys Smart Reply 2"};
         mEntry.systemGeneratedSmartActions =
                 createActions(new String[] {"Sys Smart Action 1", "Sys Smart Action 2"});
 
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/row/NotificationGutsManagerTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/row/NotificationGutsManagerTest.java
index fdc9e0c..727e9af 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/row/NotificationGutsManagerTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/row/NotificationGutsManagerTest.java
@@ -45,7 +45,6 @@
 import android.app.NotificationChannel;
 import android.content.Intent;
 import android.content.pm.PackageManager;
-import android.metrics.LogMaker;
 import android.os.Binder;
 import android.os.Handler;
 import android.provider.Settings;
@@ -57,7 +56,6 @@
 import android.view.View;
 
 import com.android.internal.logging.MetricsLogger;
-import com.android.internal.logging.nano.MetricsProto;
 import com.android.systemui.SysuiTestCase;
 import com.android.systemui.plugins.statusbar.NotificationMenuRowPlugin;
 import com.android.systemui.statusbar.NotificationPresenter;
@@ -220,34 +218,6 @@
     }
 
     @Test
-    public void testOpenGutsLogging() {
-        NotificationGutsManager gutsManager = spy(mGutsManager);
-        doReturn(true).when(gutsManager).bindGuts(any(), any());
-
-        NotificationGuts guts = spy(new NotificationGuts(mContext));
-        doReturn(true).when(guts).post(any());
-
-        ExpandableNotificationRow realRow = createTestNotificationRow();
-        NotificationMenuRowPlugin.MenuItem menuItem = createTestMenuItem(realRow);
-
-        ExpandableNotificationRow row = spy(realRow);
-        when(row.getWindowToken()).thenReturn(new Binder());
-        when(row.getGuts()).thenReturn(guts);
-        StatusBarNotification notification = spy(realRow.getStatusBarNotification());
-        when(row.getStatusBarNotification()).thenReturn(notification);
-
-        assertTrue(gutsManager.openGuts(row, 0, 0, menuItem));
-
-        ArgumentCaptor<LogMaker> logMakerCaptor = ArgumentCaptor.forClass(LogMaker.class);
-        verify(notification).getLogMaker();
-        verify(mMetricsLogger).write(logMakerCaptor.capture());
-        assertEquals(MetricsProto.MetricsEvent.ACTION_NOTE_CONTROLS,
-                logMakerCaptor.getValue().getCategory());
-        assertEquals(MetricsProto.MetricsEvent.TYPE_ACTION,
-                logMakerCaptor.getValue().getType());
-    }
-
-    @Test
     public void testAppOpsSettingsIntent_camera() {
         ArraySet<Integer> ops = new ArraySet<>();
         ops.add(OP_CAMERA);
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/row/NotificationInfoTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/row/NotificationInfoTest.java
index 554baaf..2a64445 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/row/NotificationInfoTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/row/NotificationInfoTest.java
@@ -83,7 +83,6 @@
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.ArgumentCaptor;
-import org.mockito.ArgumentMatcher;
 import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnit;
 import org.mockito.junit.MockitoRule;
@@ -107,11 +106,16 @@
     private NotificationChannel mDefaultNotificationChannel;
     private StatusBarNotification mSbn;
 
-    @Rule public MockitoRule mockito = MockitoJUnit.rule();
-    @Mock private MetricsLogger mMetricsLogger;
-    @Mock private INotificationManager mMockINotificationManager;
-    @Mock private PackageManager mMockPackageManager;
-    @Mock private NotificationBlockingHelperManager mBlockingHelperManager;
+    @Rule
+    public MockitoRule mockito = MockitoJUnit.rule();
+    @Mock
+    private MetricsLogger mMetricsLogger;
+    @Mock
+    private INotificationManager mMockINotificationManager;
+    @Mock
+    private PackageManager mMockPackageManager;
+    @Mock
+    private NotificationBlockingHelperManager mBlockingHelperManager;
 
     @Before
     public void setUp() throws Exception {
@@ -172,44 +176,25 @@
         PollingCheck.waitFor(1000,
                 () -> VISIBLE == mNotificationInfo.findViewById(R.id.confirmation).getVisibility());
     }
+
     private void ensureNoUndoButton() {
         PollingCheck.waitFor(1000,
                 () -> GONE == mNotificationInfo.findViewById(R.id.confirmation).getVisibility()
                         && !mNotificationInfo.isAnimating());
     }
+
     private void waitForStopButton() {
         PollingCheck.waitFor(1000,
                 () -> VISIBLE == mNotificationInfo.findViewById(R.id.prompt).getVisibility());
     }
 
-    class ImportanceChangeLogMaker implements ArgumentMatcher<LogMaker> {
-        private static final int CATEGORY = MetricsProto.MetricsEvent.ACTION_SAVE_IMPORTANCE;
-        private int mType, mSubtype;
-
-        ImportanceChangeLogMaker(int type, int subtype) {
-            mType = type;
-            mSubtype = subtype;
-        }
-        public boolean matches(LogMaker l) {
-            return (l.getCategory() == CATEGORY)
-                    && (l.getType() == mType)
-                    && (l.getSubtype() == mSubtype);
-        }
-
-        public String toString() {
-            return String.format("LogMaker(%d, %d, %d)", CATEGORY, mType, mSubtype);
-        }
-    }
-
-    private LogMaker importanceChangeLog(int type, int subtype) {
-        return argThat(new ImportanceChangeLogMaker(type, subtype));
-    }
-
     @Test
     public void testBindNotification_SetsTextApplicationName() throws Exception {
         when(mMockPackageManager.getApplicationLabel(any())).thenReturn("App Name");
         mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
-                TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
+                TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn,
+                null, null, null,
+                true, false,
                 IMPORTANCE_DEFAULT, true);
         final TextView textView = mNotificationInfo.findViewById(R.id.pkgname);
         assertTrue(textView.getText().toString().contains("App Name"));
@@ -330,9 +315,9 @@
 
     @Test
     public void testBindNotification_BlockButton() throws Exception {
-       mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
-               TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
-               IMPORTANCE_DEFAULT, true);
+        mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
+                TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
+                IMPORTANCE_DEFAULT, true);
         final View block = mNotificationInfo.findViewById(R.id.int_block);
         final View minimize = mNotificationInfo.findViewById(R.id.block_or_minimize);
         assertEquals(VISIBLE, block.getVisibility());
@@ -340,7 +325,7 @@
     }
 
     @Test
-    public void testBindNotification_BlockButton_BlockHelper() throws Exception {
+    public void testBindNotification_BlockButton_BlockingHelper() throws Exception {
         mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
                 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
                 true /* isBlockingHelper */, false, IMPORTANCE_DEFAULT, true);
@@ -498,13 +483,29 @@
     }
 
     @Test
-    public void testLogBlockingHelperCounter_logGutsViewDisplayed() throws Exception {
+    public void testBindNotificationLogging_notBlockingHelper() throws Exception {
         mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
-                TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
+                TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn,
+                null, null, null,
+                true, false,
                 IMPORTANCE_DEFAULT, true);
-        mNotificationInfo.logBlockingHelperCounter("HowCanNotifsBeRealIfAppsArent");
         verify(mMetricsLogger).write(argThat(logMaker ->
-                logMaker.getCategory() == MetricsEvent.NOTIFICATION_BLOCKING_HELPER
+                logMaker.getCategory() == MetricsEvent.ACTION_NOTE_CONTROLS
+                        && logMaker.getType() == MetricsEvent.TYPE_OPEN
+                        && logMaker.getSubtype() == MetricsEvent.BLOCKING_HELPER_UNKNOWN
+        ));
+    }
+
+    @Test
+    public void testBindNotificationLogging_BlockingHelper() throws Exception {
+        mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
+                TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn,
+                null, null, null,
+                false, true,
+                true, true,
+                IMPORTANCE_DEFAULT, true);
+        verify(mMetricsLogger).write(argThat(logMaker ->
+                logMaker.getCategory() == MetricsEvent.ACTION_NOTE_CONTROLS
                         && logMaker.getType() == MetricsEvent.TYPE_OPEN
                         && logMaker.getSubtype() == MetricsEvent.BLOCKING_HELPER_DISPLAY
         ));
@@ -513,8 +514,11 @@
     @Test
     public void testLogBlockingHelperCounter_logsForBlockingHelper() throws Exception {
         mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
-                TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, false, true,
-                true, true, IMPORTANCE_DEFAULT, true);
+                TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn,
+                null, null, null,
+                false, true,
+                true, true,
+                IMPORTANCE_DEFAULT, true);
         mNotificationInfo.logBlockingHelperCounter("HowCanNotifsBeRealIfAppsArent");
         verify(mMetricsLogger).count(eq("HowCanNotifsBeRealIfAppsArent"), eq(1));
     }
@@ -680,7 +684,7 @@
         mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
                 TEST_PACKAGE_NAME, mNotificationChannel /* notificationChannel */,
                 10 /* numUniqueChannelsInRow */, mSbn, null /* checkSaveListener */,
-                null /* onSettingsClick */, null /* onAppSettingsClick */ ,
+                null /* onSettingsClick */, null /* onAppSettingsClick */,
                 true, false /* isNonblockable */, IMPORTANCE_DEFAULT, false
         );
 
@@ -722,7 +726,7 @@
         mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
                 TEST_PACKAGE_NAME, mNotificationChannel /* notificationChannel */,
                 10 /* numUniqueChannelsInRow */, mSbn, null /* checkSaveListener */,
-                null /* onSettingsClick */, null /* onAppSettingsClick */ ,
+                null /* onSettingsClick */, null /* onAppSettingsClick */,
                 true /* provisioned */,
                 false /* isNonblockable */, true /* isForBlockingHelper */,
                 true /* isUserSentimentNegative */, IMPORTANCE_DEFAULT, true);
@@ -744,14 +748,14 @@
     }
 
     @Test
-    public void testCloseControls_nonNullCheckSaveListenerDoesntDelayKeepShowing()
+    public void testCloseControls_nonNullCheckSaveListenerDoesntDelayKeepShowing_BlockingHelper()
             throws Exception {
         NotificationInfo.CheckSaveListener listener =
                 mock(NotificationInfo.CheckSaveListener.class);
         mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
                 TEST_PACKAGE_NAME, mNotificationChannel /* notificationChannel */,
                 10 /* numUniqueChannelsInRow */, mSbn, listener /* checkSaveListener */,
-                null /* onSettingsClick */, null /* onAppSettingsClick */ , true /* provisioned */,
+                null /* onSettingsClick */, null /* onAppSettingsClick */, true /* provisioned */,
                 false /* isNonblockable */, true /* isForBlockingHelper */,
                 true /* isUserSentimentNegative */, IMPORTANCE_DEFAULT, true);
 
@@ -772,14 +776,14 @@
     }
 
     @Test
-    public void testCloseControls_nonNullCheckSaveListenerDoesntDelayDismiss()
+    public void testCloseControls_nonNullCheckSaveListenerDoesntDelayDismiss_BlockingHelper()
             throws Exception {
         NotificationInfo.CheckSaveListener listener =
                 mock(NotificationInfo.CheckSaveListener.class);
         mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
                 TEST_PACKAGE_NAME, mNotificationChannel /* notificationChannel */,
                 10 /* numUniqueChannelsInRow */, mSbn, listener /* checkSaveListener */,
-                null /* onSettingsClick */, null /* onAppSettingsClick */ ,
+                null /* onSettingsClick */, null /* onAppSettingsClick */,
                 false /* isNonblockable */, true /* isForBlockingHelper */,
                 true, true /* isUserSentimentNegative */,  /* isNoisy */
                 IMPORTANCE_DEFAULT, true);
@@ -791,7 +795,7 @@
     }
 
     @Test
-    public void testCloseControls_checkSaveListenerDelaysStopNotifications()
+    public void testCloseControls_checkSaveListenerDelaysStopNotifications_BlockingHelper()
             throws Exception {
         NotificationInfo.CheckSaveListener listener =
                 mock(NotificationInfo.CheckSaveListener.class);
@@ -849,18 +853,25 @@
     }
 
     @Test
-    public void testBlockChangedCallsUpdateNotificationChannel() throws Exception {
+    public void testBlockChangedCallsUpdateNotificationChannel_notBlockingHelper()
+            throws Exception {
         mNotificationChannel.setImportance(IMPORTANCE_LOW);
         mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
-                TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
+                TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn,
+                null, null, null,
+                true, false,
                 IMPORTANCE_DEFAULT, false);
 
         mNotificationInfo.findViewById(R.id.int_block).performClick();
         waitForUndoButton();
         mNotificationInfo.handleCloseControls(true, false);
 
-        verify(mMetricsLogger).write(importanceChangeLog(
-                MetricsProto.MetricsEvent.TYPE_ACTION, IMPORTANCE_NONE - IMPORTANCE_LOW));
+        ArgumentCaptor<LogMaker> logMakerCaptor = ArgumentCaptor.forClass(LogMaker.class);
+        verify(mMetricsLogger, times(2)).write(logMakerCaptor.capture());
+        assertEquals(MetricsProto.MetricsEvent.TYPE_ACTION,
+                logMakerCaptor.getValue().getType());
+        assertEquals(IMPORTANCE_NONE - IMPORTANCE_LOW,
+                logMakerCaptor.getValue().getSubtype());
 
         mTestableLooper.processAllMessages();
         ArgumentCaptor<NotificationChannel> updated =
@@ -896,8 +907,12 @@
         waitForUndoButton();
         mNotificationInfo.handleCloseControls(true, false);
 
-        verify(mMetricsLogger).write(importanceChangeLog(
-                MetricsProto.MetricsEvent.TYPE_ACTION, IMPORTANCE_NONE - IMPORTANCE_LOW));
+        ArgumentCaptor<LogMaker> logMakerCaptor = ArgumentCaptor.forClass(LogMaker.class);
+        verify(mMetricsLogger, times(3)).write(logMakerCaptor.capture());
+        assertEquals(MetricsProto.MetricsEvent.TYPE_ACTION,
+                logMakerCaptor.getValue().getType());
+        assertEquals(IMPORTANCE_NONE - IMPORTANCE_LOW,
+                logMakerCaptor.getValue().getSubtype());
 
         mTestableLooper.processAllMessages();
         ArgumentCaptor<NotificationChannel> updated =
@@ -965,10 +980,12 @@
     }
 
     @Test
-    public void testBlockUndoDoesNotBlockNotificationChannel() throws Exception {
+    public void testBlockUndoDoesNotBlockNotificationChannel_notBlockingHelper() throws Exception {
         mNotificationChannel.setImportance(IMPORTANCE_LOW);
         mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
-                TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
+                TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn,
+                null, null, null,
+                true, false,
                 IMPORTANCE_DEFAULT, false);
 
         mNotificationInfo.findViewById(R.id.int_block).performClick();
@@ -977,8 +994,15 @@
         waitForStopButton();
         // mNotificationInfo.handleCloseControls doesn't get called by this interaction.
 
-        verify(mMetricsLogger).write(importanceChangeLog(
-                MetricsProto.MetricsEvent.TYPE_DISMISS, IMPORTANCE_NONE - IMPORTANCE_LOW));
+        ArgumentCaptor<LogMaker> logMakerCaptor = ArgumentCaptor.forClass(LogMaker.class);
+        verify(mMetricsLogger, times(2)).write(logMakerCaptor.capture());
+        assertEquals(MetricsEvent.ACTION_SAVE_IMPORTANCE,
+                logMakerCaptor.getValue().getCategory());
+        assertEquals(MetricsEvent.TYPE_DISMISS,
+                logMakerCaptor.getValue().getType());
+        assertEquals(IMPORTANCE_NONE - IMPORTANCE_LOW,
+                logMakerCaptor.getValue().getSubtype());
+
 
         mTestableLooper.processAllMessages();
         verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
@@ -986,11 +1010,12 @@
     }
 
     @Test
-    public void testMinUndoDoesNotMinNotificationChannel() throws Exception {
+    public void testMinUndoDoesNotMinNotificationChannel_notBlockingHelper() throws Exception {
         mNotificationChannel.setImportance(IMPORTANCE_LOW);
         mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
-                TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, true,
-                IMPORTANCE_DEFAULT, false);
+                TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn,
+                null, null, null, true,
+                true, IMPORTANCE_DEFAULT, false);
 
         mNotificationInfo.findViewById(R.id.minimize).performClick();
         waitForUndoButton();
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/BiometricsUnlockControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/BiometricsUnlockControllerTest.java
new file mode 100644
index 0000000..7be4756
--- /dev/null
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/BiometricsUnlockControllerTest.java
@@ -0,0 +1,159 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.statusbar.phone;
+
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.anyFloat;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.reset;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.hardware.biometrics.BiometricSourceType;
+import android.os.Handler;
+import android.os.PowerManager;
+import android.os.UserHandle;
+import android.test.suitebuilder.annotation.SmallTest;
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper.RunWithLooper;
+
+import com.android.keyguard.KeyguardUpdateMonitor;
+import com.android.systemui.SysuiTestCase;
+import com.android.systemui.keyguard.KeyguardViewMediator;
+import com.android.systemui.statusbar.NotificationMediaManager;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+@SmallTest
+@RunWith(AndroidTestingRunner.class)
+@RunWithLooper
+public class BiometricsUnlockControllerTest extends SysuiTestCase {
+
+    @Mock
+    private NotificationMediaManager mMediaManager;
+    @Mock
+    private PowerManager mPowerManager;
+    @Mock
+    private KeyguardUpdateMonitor mUpdateMonitor;
+    @Mock
+    private StatusBarKeyguardViewManager mStatusBarKeyguardViewManager;
+    @Mock
+    private StatusBarWindowController mStatusBarWindowController;
+    @Mock
+    private DozeScrimController mDozeScrimController;
+    @Mock
+    private KeyguardViewMediator mKeyguardViewMediator;
+    @Mock
+    private ScrimController mScrimController;
+    @Mock
+    private StatusBar mStatusBar;
+    @Mock
+    private UnlockMethodCache mUnlockMethodCache;
+    private BiometricUnlockController mBiometricUnlockController;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        when(mStatusBarKeyguardViewManager.isShowing()).thenReturn(true);
+        when(mUpdateMonitor.isDeviceInteractive()).thenReturn(true);
+        mContext.addMockSystemService(PowerManager.class, mPowerManager);
+        mDependency.injectTestDependency(NotificationMediaManager.class, mMediaManager);
+        mDependency.injectTestDependency(StatusBarWindowController.class,
+                mStatusBarWindowController);
+        mBiometricUnlockController = new BiometricUnlockController(mContext, mDozeScrimController,
+                mKeyguardViewMediator, mScrimController, mStatusBar, mUnlockMethodCache,
+                new Handler(), mUpdateMonitor, 0 /* wakeUpDelay */);
+        mBiometricUnlockController.setStatusBarKeyguardViewManager(mStatusBarKeyguardViewManager);
+    }
+
+    @Test
+    public void onBiometricAuthenticated_whenFingerprintAndBiometricsDisallowed_showBouncer() {
+        mBiometricUnlockController.onBiometricAuthenticated(UserHandle.USER_CURRENT,
+                BiometricSourceType.FINGERPRINT);
+        verify(mStatusBarKeyguardViewManager).showBouncer(eq(false));
+        verify(mStatusBarKeyguardViewManager).animateCollapsePanels(anyFloat());
+    }
+
+    @Test
+    public void onBiometricAuthenticated_whenFingerprintAndNotInteractive_wakeAndUnlock() {
+        reset(mUpdateMonitor);
+        reset(mStatusBarKeyguardViewManager);
+        when(mStatusBarKeyguardViewManager.isShowing()).thenReturn(true);
+        when(mUpdateMonitor.isUnlockingWithBiometricAllowed()).thenReturn(true);
+        when(mDozeScrimController.isPulsing()).thenReturn(true);
+        mBiometricUnlockController.onBiometricAuthenticated(UserHandle.USER_CURRENT,
+                BiometricSourceType.FINGERPRINT);
+
+        verify(mKeyguardViewMediator).onWakeAndUnlocking();
+    }
+
+    @Test
+    public void onBiometricAuthenticated_whenFingerprint_dismissKeyguard() {
+        when(mUpdateMonitor.isUnlockingWithBiometricAllowed()).thenReturn(true);
+        mBiometricUnlockController.onBiometricAuthenticated(UserHandle.USER_CURRENT,
+                BiometricSourceType.FINGERPRINT);
+
+        verify(mStatusBarKeyguardViewManager, never()).showBouncer(anyBoolean());
+        verify(mStatusBarKeyguardViewManager).animateCollapsePanels(anyFloat());
+    }
+
+    @Test
+    public void onBiometricAuthenticated_whenFingerprintOnBouncer_dismissBouncer() {
+        when(mUpdateMonitor.isUnlockingWithBiometricAllowed()).thenReturn(true);
+        when(mStatusBarKeyguardViewManager.isBouncerShowing()).thenReturn(true);
+        mBiometricUnlockController.onBiometricAuthenticated(UserHandle.USER_CURRENT,
+                BiometricSourceType.FINGERPRINT);
+
+        verify(mStatusBarKeyguardViewManager).notifyKeyguardAuthenticated(eq(false));
+    }
+
+    @Test
+    public void onBiometricAuthenticated_whenFace_dontDismissKeyguard() {
+        when(mUpdateMonitor.isUnlockingWithBiometricAllowed()).thenReturn(true);
+        mBiometricUnlockController.onBiometricAuthenticated(UserHandle.USER_CURRENT,
+                BiometricSourceType.FACE);
+
+        verify(mStatusBarKeyguardViewManager, never()).animateCollapsePanels(anyFloat());
+    }
+
+    @Test
+    public void onBiometricAuthenticated_whenFaceOnBouncer_dismissBouncer() {
+        when(mUpdateMonitor.isUnlockingWithBiometricAllowed()).thenReturn(true);
+        when(mStatusBarKeyguardViewManager.isBouncerShowing()).thenReturn(true);
+        mBiometricUnlockController.onBiometricAuthenticated(UserHandle.USER_CURRENT,
+                BiometricSourceType.FACE);
+
+        verify(mStatusBarKeyguardViewManager).notifyKeyguardAuthenticated(eq(false));
+    }
+
+    @Test
+    public void onBiometricAuthenticated_whenFaceAndPulsing_dontDismissKeyguard() {
+        reset(mUpdateMonitor);
+        reset(mStatusBarKeyguardViewManager);
+        when(mUpdateMonitor.isUnlockingWithBiometricAllowed()).thenReturn(true);
+        when(mDozeScrimController.isPulsing()).thenReturn(true);
+        mBiometricUnlockController.onBiometricAuthenticated(UserHandle.USER_CURRENT,
+                BiometricSourceType.FACE);
+
+        verify(mStatusBarKeyguardViewManager, never()).animateCollapsePanels(anyFloat());
+    }
+}
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/StatusBarTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/StatusBarTest.java
index 17611ff..49fcafd 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/StatusBarTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/StatusBarTest.java
@@ -64,6 +64,7 @@
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
 import com.android.internal.logging.testing.FakeMetricsLogger;
 import com.android.internal.statusbar.IStatusBarService;
+import com.android.keyguard.KeyguardUpdateMonitor;
 import com.android.systemui.Dependency;
 import com.android.systemui.ForegroundServiceController;
 import com.android.systemui.InitController;
@@ -120,6 +121,9 @@
 import java.io.ByteArrayOutputStream;
 import java.io.PrintWriter;
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
 
 @SmallTest
 @RunWith(AndroidTestingRunner.class)
@@ -161,6 +165,8 @@
     private NotificationAlertingManager mNotificationAlertingManager;
     @Mock
     private NotificationLogger.ExpansionStateLogger mExpansionStateLogger;
+    @Mock
+    private KeyguardUpdateMonitor mKeyguardUpdateMonitor;
 
     private TestableStatusBar mStatusBar;
     private FakeMetricsLogger mMetricsLogger;
@@ -252,7 +258,7 @@
                 mDozeScrimController, mock(NotificationShelf.class),
                 mLockscreenUserManager, mCommandQueue, mNotificationPresenter,
                 mock(BubbleController.class), mock(NavigationBarController.class),
-                mock(AutoHideController.class));
+                mock(AutoHideController.class), mKeyguardUpdateMonitor);
         mStatusBar.mContext = mContext;
         mStatusBar.mComponents = mContext.getComponents();
         SystemUIFactory.getInstance().getRootComponent()
@@ -631,6 +637,39 @@
     }
 
     @Test
+    public void testPulseWhileDozing_notifyAuthInterrupt() {
+        HashSet<Integer> reasonsWantingAuth = new HashSet<>(
+                Collections.singletonList(DozeLog.PULSE_REASON_SENSOR_WAKE_LOCK_SCREEN));
+        HashSet<Integer> reasonsSkippingAuth = new HashSet<>(
+                Arrays.asList(DozeLog.PULSE_REASON_INTENT,
+                        DozeLog.PULSE_REASON_NOTIFICATION,
+                        DozeLog.PULSE_REASON_SENSOR_SIGMOTION,
+                        DozeLog.REASON_SENSOR_PICKUP,
+                        DozeLog.REASON_SENSOR_DOUBLE_TAP,
+                        DozeLog.PULSE_REASON_SENSOR_LONG_PRESS,
+                        DozeLog.PULSE_REASON_DOCKING,
+                        DozeLog.REASON_SENSOR_WAKE_UP,
+                        DozeLog.REASON_SENSOR_TAP));
+        HashSet<Integer> reasonsThatDontPulse = new HashSet<>(
+                Arrays.asList(DozeLog.REASON_SENSOR_PICKUP,
+                        DozeLog.REASON_SENSOR_DOUBLE_TAP,
+                        DozeLog.REASON_SENSOR_TAP));
+
+        for (int i = 0; i < DozeLog.REASONS; i++) {
+            reset(mKeyguardUpdateMonitor);
+            mStatusBar.mDozeServiceHost.pulseWhileDozing(mock(DozeHost.PulseCallback.class), i);
+            if (reasonsWantingAuth.contains(i)) {
+                verify(mKeyguardUpdateMonitor).onAuthInterruptDetected();
+            } else if (reasonsSkippingAuth.contains(i) || reasonsThatDontPulse.contains(i)) {
+                verify(mKeyguardUpdateMonitor, never()).onAuthInterruptDetected();
+            } else {
+                throw new AssertionError("Reason " + i + " isn't specified as wanting or skipping"
+                        + " passive auth. Please consider how this pulse reason should behave.");
+            }
+        }
+    }
+
+    @Test
     public void testSetState_changesIsFullScreenUserSwitcherState() {
         mStatusBar.setBarStateForTest(StatusBarState.KEYGUARD);
         assertFalse(mStatusBar.isFullScreenUserSwitcherState());
@@ -705,7 +744,8 @@
                 NotificationPresenter notificationPresenter,
                 BubbleController bubbleController,
                 NavigationBarController navBarController,
-                AutoHideController autoHideController) {
+                AutoHideController autoHideController,
+                KeyguardUpdateMonitor keyguardUpdateMonitor) {
             mStatusBarKeyguardViewManager = man;
             mUnlockMethodCache = unlock;
             mKeyguardIndicationController = key;
@@ -738,6 +778,7 @@
             mBubbleController = bubbleController;
             mNavigationBarController = navBarController;
             mAutoHideController = autoHideController;
+            mKeyguardUpdateMonitor = keyguardUpdateMonitor;
         }
 
         private WakefulnessLifecycle createAwakeWakefulnessLifecycle() {
diff --git a/packages/VpnDialogs/AndroidManifest.xml b/packages/VpnDialogs/AndroidManifest.xml
index 8172e71..1d0b9b6 100644
--- a/packages/VpnDialogs/AndroidManifest.xml
+++ b/packages/VpnDialogs/AndroidManifest.xml
@@ -20,6 +20,7 @@
         package="com.android.vpndialogs">
 
     <uses-permission android:name="android.permission.CONTROL_VPN" />
+    <uses-permission android:name="android.permission.CONTROL_ALWAYS_ON_VPN" />
     <uses-permission android:name="android.permission.CONNECTIVITY_INTERNAL" />
 
     <application android:label="VpnDialogs"
diff --git a/packages/overlays/FontArbutusSourceOverlay/Android.mk b/packages/overlays/FontArbutusSourceOverlay/Android.mk
deleted file mode 100644
index 23aee55..0000000
--- a/packages/overlays/FontArbutusSourceOverlay/Android.mk
+++ /dev/null
@@ -1,31 +0,0 @@
-#
-#  Copyright 2018, The Android Open Source Project
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#     http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-LOCAL_PATH:= $(call my-dir)
-include $(CLEAR_VARS)
-
-LOCAL_RRO_THEME := FontArbutusSource
-LOCAL_CERTIFICATE := platform
-LOCAL_PRODUCT_MODULE := true
-
-LOCAL_SRC_FILES := $(call all-subdir-java-files)
-
-LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res
-
-LOCAL_PACKAGE_NAME := FontArbutusSourceOverlay
-LOCAL_SDK_VERSION := current
-
-include $(BUILD_RRO_PACKAGE)
diff --git a/packages/overlays/FontArbutusSourceOverlay/AndroidManifest.xml b/packages/overlays/FontArbutusSourceOverlay/AndroidManifest.xml
deleted file mode 100644
index 46c06b9..0000000
--- a/packages/overlays/FontArbutusSourceOverlay/AndroidManifest.xml
+++ /dev/null
@@ -1,31 +0,0 @@
-<!--
-/**
- * Copyright (c) 2018, The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
--->
-<manifest xmlns:android="http://schemas.android.com/apk/res/android"
-    package="com.android.theme.font.arbutussource"
-    android:versionCode="1"
-    android:versionName="1.0">
-    <overlay android:targetPackage="android"
-        android:category="android.theme.customization.font"
-        android:priority="1"/>
-
-    <application android:label="@string/font_arbutus_source_overlay" android:hasCode="false">
-        <meta-data
-            android:name="android.theme.customization.REQUIRED_SYSTEM_FONTS"
-            android:value="arbutus-slab,source-sans-pro,source-sans-pro-medium" />
-    </application>
-</manifest>
diff --git a/packages/overlays/FontArbutusSourceOverlay/res/values/config.xml b/packages/overlays/FontArbutusSourceOverlay/res/values/config.xml
deleted file mode 100644
index a6aa64c..0000000
--- a/packages/overlays/FontArbutusSourceOverlay/res/values/config.xml
+++ /dev/null
@@ -1,28 +0,0 @@
-<!--
-/**
- * Copyright (c) 2018, The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
--->
-<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
-     <!-- Name of a font family to use for body text. -->
-    <string name="config_bodyFontFamily" translatable="false">source-sans-pro</string>
-    <!-- Name of a font family to use for medium body text. -->
-    <string name="config_bodyFontFamilyMedium" translatable="false">source-sans-pro-semi-bold</string>
-    <!-- Name of a font family to use for headlines. If empty, falls back to platform default -->
-    <string name="config_headlineFontFamily" translatable="false">arbutus-slab</string>
-    <!-- Name of the font family used for system surfaces where the font should use medium weight -->
-    <string name="config_headlineFontFamilyMedium" translatable="false">arbutus-slab</string>
-</resources>
-
diff --git a/packages/overlays/FontArbutusSourceOverlay/res/values/strings.xml b/packages/overlays/FontArbutusSourceOverlay/res/values/strings.xml
deleted file mode 100644
index d80eb20..0000000
--- a/packages/overlays/FontArbutusSourceOverlay/res/values/strings.xml
+++ /dev/null
@@ -1,21 +0,0 @@
-<!--
-/**
- * Copyright (c) 2018, The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
--->
-<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
-    <!-- Headline / Body font Arbutus Slab / Source Sans Pro overlay -->
-    <string name="font_arbutus_source_overlay" translatable="false">Arbutus Slab / Source Sans Pro</string>
-</resources>
diff --git a/packages/overlays/FontArvoLatoOverlay/Android.mk b/packages/overlays/FontArvoLatoOverlay/Android.mk
deleted file mode 100644
index 3433ecf..0000000
--- a/packages/overlays/FontArvoLatoOverlay/Android.mk
+++ /dev/null
@@ -1,31 +0,0 @@
-#
-#  Copyright 2018, The Android Open Source Project
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#     http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-LOCAL_PATH:= $(call my-dir)
-include $(CLEAR_VARS)
-
-LOCAL_RRO_THEME := FontArvoLato
-LOCAL_CERTIFICATE := platform
-LOCAL_PRODUCT_MODULE := true
-
-LOCAL_SRC_FILES := $(call all-subdir-java-files)
-
-LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res
-
-LOCAL_PACKAGE_NAME := FontArvoLatoOverlay
-LOCAL_SDK_VERSION := current
-
-include $(BUILD_RRO_PACKAGE)
diff --git a/packages/overlays/FontArvoLatoOverlay/AndroidManifest.xml b/packages/overlays/FontArvoLatoOverlay/AndroidManifest.xml
deleted file mode 100644
index 3f2e5de..0000000
--- a/packages/overlays/FontArvoLatoOverlay/AndroidManifest.xml
+++ /dev/null
@@ -1,31 +0,0 @@
-<!--
-/**
- * Copyright (c) 2018, The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
--->
-<manifest xmlns:android="http://schemas.android.com/apk/res/android"
-    package="com.android.theme.font.arvolato"
-    android:versionCode="1"
-    android:versionName="1.0">
-    <overlay android:targetPackage="android"
-        android:category="android.theme.customization.font"
-        android:priority="1"/>
-
-    <application android:label="@string/font_arvo_lato_overlay" android:hasCode="false">
-        <meta-data
-            android:name="android.theme.customization.REQUIRED_SYSTEM_FONTS"
-            android:value="arvo,arvo-medium,lato,lato-medium" />
-    </application>
-</manifest>
diff --git a/packages/overlays/FontArvoLatoOverlay/res/values/config.xml b/packages/overlays/FontArvoLatoOverlay/res/values/config.xml
deleted file mode 100644
index 4e70d72..0000000
--- a/packages/overlays/FontArvoLatoOverlay/res/values/config.xml
+++ /dev/null
@@ -1,28 +0,0 @@
-<!--
-/**
- * Copyright (c) 2018, The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
--->
-<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
-     <!-- Name of a font family to use for body text. -->
-    <string name="config_bodyFontFamily" translatable="false">lato</string>
-    <!-- Name of a font family to use for medium body text. -->
-    <string name="config_bodyFontFamilyMedium" translatable="false">lato-bold</string>
-    <!-- Name of a font family to use for headlines. If empty, falls back to platform default -->
-    <string name="config_headlineFontFamily" translatable="false">arvo</string>
-    <!-- Name of the font family used for system surfaces where the font should use medium weight -->
-    <string name="config_headlineFontFamilyMedium" translatable="false">arvo-bold</string>
-</resources>
-
diff --git a/packages/overlays/FontArvoLatoOverlay/res/values/strings.xml b/packages/overlays/FontArvoLatoOverlay/res/values/strings.xml
deleted file mode 100644
index 9ea097f..0000000
--- a/packages/overlays/FontArvoLatoOverlay/res/values/strings.xml
+++ /dev/null
@@ -1,21 +0,0 @@
-<!--
-/**
- * Copyright (c) 2018, The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
--->
-<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
-    <!-- Headline / Body font Arvo / Lato overlay -->
-    <string name="font_arvo_lato_overlay" translatable="false">Arvo / Lato</string>
-</resources>
diff --git a/packages/overlays/FontRubikRubikOverlay/Android.mk b/packages/overlays/FontRubikRubikOverlay/Android.mk
deleted file mode 100644
index 21d617e..0000000
--- a/packages/overlays/FontRubikRubikOverlay/Android.mk
+++ /dev/null
@@ -1,31 +0,0 @@
-#
-#  Copyright 2018, The Android Open Source Project
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#     http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-LOCAL_PATH:= $(call my-dir)
-include $(CLEAR_VARS)
-
-LOCAL_RRO_THEME := FontRubikRubik
-LOCAL_CERTIFICATE := platform
-LOCAL_PRODUCT_MODULE := true
-
-LOCAL_SRC_FILES := $(call all-subdir-java-files)
-
-LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res
-
-LOCAL_PACKAGE_NAME := FontRubikRubikOverlay
-LOCAL_SDK_VERSION := current
-
-include $(BUILD_RRO_PACKAGE)
diff --git a/packages/overlays/FontRubikRubikOverlay/AndroidManifest.xml b/packages/overlays/FontRubikRubikOverlay/AndroidManifest.xml
deleted file mode 100644
index 1f28d46..0000000
--- a/packages/overlays/FontRubikRubikOverlay/AndroidManifest.xml
+++ /dev/null
@@ -1,31 +0,0 @@
-<!--
-/**
- * Copyright (c) 2018, The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
--->
-<manifest xmlns:android="http://schemas.android.com/apk/res/android"
-    package="com.android.theme.font.rubikrubik"
-    android:versionCode="1"
-    android:versionName="1.0">
-    <overlay android:targetPackage="android"
-        android:category="android.theme.customization.font"
-        android:priority="1"/>
-
-    <application android:label="@string/font_rubik_rubik_overlay" android:hasCode="false">
-        <meta-data
-            android:name="android.theme.customization.REQUIRED_SYSTEM_FONTS"
-            android:value="rubik,rubik-medium" />
-    </application>
-</manifest>
diff --git a/packages/overlays/FontRubikRubikOverlay/res/values/config.xml b/packages/overlays/FontRubikRubikOverlay/res/values/config.xml
deleted file mode 100644
index 4f90e29..0000000
--- a/packages/overlays/FontRubikRubikOverlay/res/values/config.xml
+++ /dev/null
@@ -1,28 +0,0 @@
-<!--
-/**
- * Copyright (c) 2018, The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
--->
-<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
-     <!-- Name of a font family to use for body text. -->
-    <string name="config_bodyFontFamily" translatable="false">rubik</string>
-    <!-- Name of a font family to use for medium body text. -->
-    <string name="config_bodyFontFamilyMedium" translatable="false">rubik-medium</string>
-    <!-- Name of a font family to use for headlines. If empty, falls back to platform default -->
-    <string name="config_headlineFontFamily" translatable="false">rubik</string>
-    <!-- Name of the font family used for system surfaces where the font should use medium weight -->
-    <string name="config_headlineFontFamilyMedium" translatable="false">rubik-medium</string>
-</resources>
-
diff --git a/proto/Android.bp b/proto/Android.bp
index f3811bd..817a54d 100644
--- a/proto/Android.bp
+++ b/proto/Android.bp
@@ -17,3 +17,24 @@
         },
     },
 }
+
+java_library_static {
+    name: "metrics-constants-protos",
+    host_supported: true,
+    proto: {
+        type: "nano",
+    },
+    srcs: ["src/metrics_constants/metrics_constants.proto"],
+    no_framework_libs: true,
+    sdk_version: "system_current",
+    // Pin java_version until jarjar is certified to support later versions. http://b/72703434
+    java_version: "1.8",
+    target: {
+        android: {
+            jarjar_rules: "jarjar-rules.txt",
+        },
+        host: {
+            static_libs: ["libprotobuf-java-nano"],
+        },
+    },
+}
diff --git a/proto/src/metrics_constants/metrics_constants.proto b/proto/src/metrics_constants/metrics_constants.proto
index 385931d..fceb656 100644
--- a/proto/src/metrics_constants/metrics_constants.proto
+++ b/proto/src/metrics_constants/metrics_constants.proto
@@ -6910,6 +6910,16 @@
     // CATEGORY: NOTIFICATION
     FIELD_NOTIFICATION_CATEGORY = 1641;
 
+    // OPEN: Settings > Settings > Network & internet > Click Mobile network to land on page with
+    // details for a SIM/eSIM mobile network > Click edit icon to bring up a rename dialog.
+    // OS: Q
+    MOBILE_NETWORK_RENAME_DIALOG = 1642;
+
+    // ACTION: Settings > Search Bar > Avatar
+    // CATEGORY: SETTINGS
+    // OS: Q
+    ACTION_CLICK_ACCOUNT_AVATAR = 1643;
+
     // ---- End Q Constants, all Q constants go above this line ----
     // Add new aosp constants above this line.
     // END OF AOSP CONSTANTS
diff --git a/proto/src/wifi.proto b/proto/src/wifi.proto
index b84736b..2b45b49 100644
--- a/proto/src/wifi.proto
+++ b/proto/src/wifi.proto
@@ -1888,11 +1888,31 @@
     LABEL_BAD = 2;
   }
 
+  enum UsabilityStatsTriggerType {
+    // Default/Invalid event
+    TYPE_UNKNOWN = 0;
+
+    // There is a data stall from tx failures
+    TYPE_DATA_STALL_BAD_TX = 1;
+
+    // There is a data stall from rx failures
+    TYPE_DATA_STALL_TX_WITHOUT_RX = 2;
+
+    // There is a data stall from both tx and rx failures
+    TYPE_DATA_STALL_BOTH = 3;
+
+    // Firmware generated an alert
+    TYPE_FIRMWARE_ALERT = 4;
+  }
+
   // The current wifi usability state
   optional Label label = 1;
 
   // The list of timestamped wifi usability stats
   repeated WifiUsabilityStatsEntry stats = 2;
+
+  // What event triggered WifiUsabilityStats.
+  optional UsabilityStatsTriggerType trigger_type = 3;
 }
 
 message DeviceMobilityStatePnoScanStats {
diff --git a/services/accessibility/java/com/android/server/accessibility/AbstractAccessibilityServiceConnection.java b/services/accessibility/java/com/android/server/accessibility/AbstractAccessibilityServiceConnection.java
index f4ac130..fc43882 100644
--- a/services/accessibility/java/com/android/server/accessibility/AbstractAccessibilityServiceConnection.java
+++ b/services/accessibility/java/com/android/server/accessibility/AbstractAccessibilityServiceConnection.java
@@ -348,8 +348,8 @@
     }
 
     int getRelevantEventTypes() {
-        return (mUsesAccessibilityCache ? AccessibilityCache.CACHE_CRITICAL_EVENTS_MASK : 0)
-                | mEventTypes;
+        return (mUsesAccessibilityCache ? AccessibilityCache.CACHE_CRITICAL_EVENTS_MASK
+                : AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) | mEventTypes;
     }
 
     @Override
diff --git a/services/backup/java/com/android/server/backup/Trampoline.java b/services/backup/java/com/android/server/backup/Trampoline.java
index 303734a..87872e8 100644
--- a/services/backup/java/com/android/server/backup/Trampoline.java
+++ b/services/backup/java/com/android/server/backup/Trampoline.java
@@ -332,7 +332,14 @@
                 // If the user is unlocked, we can start the backup service for it. Otherwise we
                 // will start the service when the user is unlocked as part of its unlock callback.
                 if (getUserManager().isUserUnlocked(userId)) {
-                    startServiceForUser(userId);
+                    // Clear calling identity as initialization enforces the system identity but we
+                    // can be coming from shell.
+                    long oldId = Binder.clearCallingIdentity();
+                    try {
+                        startServiceForUser(userId);
+                    } finally {
+                        Binder.restoreCallingIdentity(oldId);
+                    }
                 }
             } else {
                 try {
diff --git a/services/core/Android.bp b/services/core/Android.bp
index 4408db8..bf08f3e 100644
--- a/services/core/Android.bp
+++ b/services/core/Android.bp
@@ -8,6 +8,7 @@
             "frameworks/native/cmds/dumpstate/binder",
             "system/core/storaged/binder",
             "system/vold/binder",
+            "system/gsid/aidl",
         ],
     },
     srcs: [
@@ -17,6 +18,7 @@
         ":installd_aidl",
         ":storaged_aidl",
         ":vold_aidl",
+        ":gsiservice_aidl",
         ":mediaupdateservice_aidl",
         "java/com/android/server/EventLogTags.logtags",
         "java/com/android/server/am/EventLogTags.logtags",
diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java
index 58263fc..f807543 100644
--- a/services/core/java/com/android/server/ConnectivityService.java
+++ b/services/core/java/com/android/server/ConnectivityService.java
@@ -3653,6 +3653,20 @@
         mTethering.stopTethering(type);
     }
 
+    /**
+     * Get the latest value of the tethering entitlement check.
+     *
+     * Note: Allow privileged apps who have TETHER_PRIVILEGED permission to access. If it turns
+     * out some such apps are observed to abuse this API, change to per-UID limits on this API
+     * if it's really needed.
+     */
+    @Override
+    public void getLatestTetheringEntitlementValue(int type, ResultReceiver receiver,
+            boolean showEntitlementUi, String callerPkg) {
+        ConnectivityManager.enforceTetherChangePermission(mContext, callerPkg);
+        mTethering.getLatestTetheringEntitlementValue(type, receiver, showEntitlementUi);
+    }
+
     // Called when we lose the default network and have no replacement yet.
     // This will automatically be cleared after X seconds or a new default network
     // becomes CONNECTED, whichever happens first.  The timer is started by the
diff --git a/services/core/java/com/android/server/DynamicAndroidService.java b/services/core/java/com/android/server/DynamicAndroidService.java
new file mode 100644
index 0000000..12a3f02
--- /dev/null
+++ b/services/core/java/com/android/server/DynamicAndroidService.java
@@ -0,0 +1,134 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server;
+
+import android.content.Context;
+import android.content.pm.PackageManager;
+import android.gsi.GsiProgress;
+import android.gsi.IGsiService;
+import android.os.IBinder;
+import android.os.IBinder.DeathRecipient;
+import android.os.IDynamicAndroidService;
+import android.os.RemoteException;
+import android.os.ServiceManager;
+import android.util.Slog;
+
+/**
+ * DynamicAndroidService implements IDynamicAndroidService. It provides permission check before
+ * passing requests to gsid
+ */
+public class DynamicAndroidService extends IDynamicAndroidService.Stub implements DeathRecipient {
+    private static final String TAG = "DynamicAndroidService";
+    private static final String NO_SERVICE_ERROR = "no gsiservice";
+
+    private Context mContext;
+    private volatile IGsiService mGsiService;
+
+    DynamicAndroidService(Context context) {
+        mContext = context;
+    }
+
+    private static IGsiService connect(DeathRecipient recipient) throws RemoteException {
+        IBinder binder = ServiceManager.getService("gsiservice");
+        if (binder == null) {
+            throw new RemoteException(NO_SERVICE_ERROR);
+        }
+        /**
+         * The init will restart gsiservice if it crashed and the proxy object will need to be
+         * re-initialized in this case.
+         */
+        binder.linkToDeath(recipient, 0);
+        return IGsiService.Stub.asInterface(binder);
+    }
+
+    /** implements DeathRecipient */
+    @Override
+    public void binderDied() {
+        Slog.w(TAG, "gsiservice died; reconnecting");
+        synchronized (this) {
+            mGsiService = null;
+        }
+    }
+
+    private IGsiService getGsiService() throws RemoteException {
+        checkPermission();
+        synchronized (this) {
+            if (mGsiService == null) {
+                mGsiService = connect(this);
+            }
+            return mGsiService;
+        }
+    }
+
+    private void checkPermission() {
+        if (mContext.checkCallingOrSelfPermission(
+                        android.Manifest.permission.MANAGE_DYNAMIC_ANDROID)
+                != PackageManager.PERMISSION_GRANTED) {
+            throw new SecurityException("Requires MANAGE_DYNAMIC_ANDROID permission");
+        }
+    }
+
+    @Override
+    public boolean startInstallation(long systemSize, long userdataSize) throws RemoteException {
+        return getGsiService().startGsiInstall(systemSize, userdataSize, true) == 0;
+    }
+
+    @Override
+    public GsiProgress getInstallationProgress() throws RemoteException {
+        return getGsiService().getInstallProgress();
+    }
+
+    @Override
+    public boolean abort() throws RemoteException {
+        return getGsiService().cancelGsiInstall();
+    }
+
+    @Override
+    public boolean isInUse() throws RemoteException {
+        return getGsiService().isGsiRunning();
+    }
+
+    @Override
+    public boolean isInstalled() throws RemoteException {
+        return getGsiService().isGsiInstalled();
+    }
+
+    @Override
+    public boolean remove() throws RemoteException {
+        return getGsiService().removeGsiInstall();
+    }
+
+    @Override
+    public boolean toggle() throws RemoteException {
+        IGsiService gsiService = getGsiService();
+        if (gsiService.isGsiRunning()) {
+            return gsiService.disableGsiInstall();
+        } else {
+            return gsiService.setGsiBootable() == 0;
+        }
+    }
+
+    @Override
+    public boolean write(byte[] buf) throws RemoteException {
+        return getGsiService().commitGsiChunkFromMemory(buf);
+    }
+
+    @Override
+    public boolean commit() throws RemoteException {
+        return getGsiService().setGsiBootable() == 0;
+    }
+}
diff --git a/services/core/java/com/android/server/EventLogTags.logtags b/services/core/java/com/android/server/EventLogTags.logtags
index d869734..0ed5beb 100644
--- a/services/core/java/com/android/server/EventLogTags.logtags
+++ b/services/core/java/com/android/server/EventLogTags.logtags
@@ -33,7 +33,7 @@
 # It logs the time remaining before the device would've normally gone to sleep without the request.
 2731 power_soft_sleep_requested (savedwaketimems|2)
 # Power save state has changed. See BatterySaverController.java for the details.
-2739 battery_saver_mode (prevOffOrOn|1|5),(nowOffOrOn|1|5),(interactive|1|5),(features|3|5),(reason|1|5)
+2739 battery_saver_mode (fullPrevOffOrOn|1|5),(adaptivePrevOffOrOn|1|5),(fullNowOffOrOn|1|5),(adaptiveNowOffOrOn|1|5),(interactive|1|5),(features|3|5),(reason|1|5)
 27390 battery_saving_stats (batterySaver|1|5),(interactive|1|5),(doze|1|5),(delta_duration|2|3),(delta_battery_drain|1|1),(delta_battery_drain_percent|1|6),(total_duration|2|3),(total_battery_drain|1|1),(total_battery_drain_percent|1|6)
 # Note when the user activity timeout has been overriden by ActivityManagerService
 27391 user_activity_timeout_override (override|2|3)
diff --git a/services/core/java/com/android/server/LocationManagerService.java b/services/core/java/com/android/server/LocationManagerService.java
index d2c6354..33c6dd2 100644
--- a/services/core/java/com/android/server/LocationManagerService.java
+++ b/services/core/java/com/android/server/LocationManagerService.java
@@ -405,6 +405,7 @@
 
         // initialize in-memory settings values
         onBackgroundThrottleWhitelistChangedLocked();
+        onIgnoreSettingsWhitelistChangedLocked();
     }
 
     @GuardedBy("mLock")
@@ -547,17 +548,16 @@
 
     @GuardedBy("mLock")
     private void onBackgroundThrottleWhitelistChangedLocked() {
-        String setting = Settings.Global.getString(
-                mContext.getContentResolver(),
-                Settings.Global.LOCATION_BACKGROUND_THROTTLE_PACKAGE_WHITELIST);
-        if (setting == null) {
-            setting = "";
-        }
-
         mBackgroundThrottlePackageWhitelist.clear();
         mBackgroundThrottlePackageWhitelist.addAll(
                 SystemConfig.getInstance().getAllowUnthrottledLocation());
-        mBackgroundThrottlePackageWhitelist.addAll(Arrays.asList(setting.split(",")));
+
+        String setting = Settings.Global.getString(
+                mContext.getContentResolver(),
+                Settings.Global.LOCATION_BACKGROUND_THROTTLE_PACKAGE_WHITELIST);
+        if (!TextUtils.isEmpty(setting)) {
+            mBackgroundThrottlePackageWhitelist.addAll(Arrays.asList(setting.split(",")));
+        }
 
         for (LocationProvider p : mProviders) {
             applyRequirementsLocked(p);
@@ -566,17 +566,16 @@
 
     @GuardedBy("lock")
     private void onIgnoreSettingsWhitelistChangedLocked() {
-        String setting = Settings.Global.getString(
-                mContext.getContentResolver(),
-                Settings.Global.LOCATION_IGNORE_SETTINGS_PACKAGE_WHITELIST);
-        if (setting == null) {
-            setting = "";
-        }
-
         mIgnoreSettingsPackageWhitelist.clear();
         mIgnoreSettingsPackageWhitelist.addAll(
                 SystemConfig.getInstance().getAllowIgnoreLocationSettings());
-        mIgnoreSettingsPackageWhitelist.addAll(Arrays.asList(setting.split(",")));
+
+        String setting = Settings.Global.getString(
+                mContext.getContentResolver(),
+                Settings.Global.LOCATION_IGNORE_SETTINGS_PACKAGE_WHITELIST);
+        if (!TextUtils.isEmpty(setting)) {
+            mIgnoreSettingsPackageWhitelist.addAll(Arrays.asList(setting.split(",")));
+        }
 
         for (LocationProvider p : mProviders) {
             applyRequirementsLocked(p);
@@ -2200,7 +2199,7 @@
             return false;
         }
 
-        if (mBackgroundThrottlePackageWhitelist.contains(record.mReceiver.mIdentity.mPackageName)) {
+        if (mIgnoreSettingsPackageWhitelist.contains(record.mReceiver.mIdentity.mPackageName)) {
             return true;
         }
 
diff --git a/services/core/java/com/android/server/StorageManagerService.java b/services/core/java/com/android/server/StorageManagerService.java
index cecd55a3..f2329d3 100644
--- a/services/core/java/com/android/server/StorageManagerService.java
+++ b/services/core/java/com/android/server/StorageManagerService.java
@@ -1541,10 +1541,6 @@
         mCallbacks = new Callbacks(FgThread.get().getLooper());
         mLockPatternUtils = new LockPatternUtils(mContext);
 
-        mPmInternal = LocalServices.getService(PackageManagerInternal.class);
-        mUmInternal = LocalServices.getService(UserManagerInternal.class);
-        mAmInternal = LocalServices.getService(ActivityManagerInternal.class);
-
         HandlerThread hthread = new HandlerThread(TAG);
         hthread.start();
         mHandler = new StorageManagerServiceHandler(hthread.getLooper());
@@ -1662,6 +1658,19 @@
     }
 
     private void servicesReady() {
+        mPmInternal = LocalServices.getService(PackageManagerInternal.class);
+        mUmInternal = LocalServices.getService(UserManagerInternal.class);
+        mAmInternal = LocalServices.getService(ActivityManagerInternal.class);
+
+        mIPackageManager = IPackageManager.Stub.asInterface(
+                ServiceManager.getService("package"));
+        mIAppOpsService = IAppOpsService.Stub.asInterface(
+                ServiceManager.getService(Context.APP_OPS_SERVICE));
+        try {
+            mIAppOpsService.startWatchingMode(OP_REQUEST_INSTALL_PACKAGES, null, mAppOpsCallback);
+        } catch (RemoteException e) {
+        }
+
         synchronized (mLock) {
             final boolean thisIsolatedStorage = StorageManager.hasIsolatedStorage();
             if (mLastIsolatedStorage == thisIsolatedStorage) {
@@ -1734,14 +1743,6 @@
                 .registerScreenObserver(this);
 
         mSystemReady = true;
-        mIPackageManager = IPackageManager.Stub.asInterface(
-                ServiceManager.getService("package"));
-        mIAppOpsService = IAppOpsService.Stub.asInterface(
-                ServiceManager.getService(Context.APP_OPS_SERVICE));
-        try {
-            mIAppOpsService.startWatchingMode(OP_REQUEST_INSTALL_PACKAGES, null, mAppOpsCallback);
-        } catch (RemoteException e) {
-        }
         mHandler.obtainMessage(H_SYSTEM_READY).sendToTarget();
     }
 
diff --git a/services/core/java/com/android/server/ThreadPriorityBooster.java b/services/core/java/com/android/server/ThreadPriorityBooster.java
index 53e8ce4..f74a4385 100644
--- a/services/core/java/com/android/server/ThreadPriorityBooster.java
+++ b/services/core/java/com/android/server/ThreadPriorityBooster.java
@@ -43,9 +43,9 @@
 
     public void boost() {
         final int tid = myTid();
-        final int prevPriority = getThreadPriority(tid);
         final PriorityState state = mThreadState.get();
         if (state.regionCounter == 0) {
+            final int prevPriority = getThreadPriority(tid);
             state.prevPriority = prevPriority;
             if (prevPriority > mBoostToPriority) {
                 setThreadPriority(tid, mBoostToPriority);
@@ -60,9 +60,11 @@
     public void reset() {
         final PriorityState state = mThreadState.get();
         state.regionCounter--;
-        final int currentPriority = getThreadPriority(myTid());
-        if (state.regionCounter == 0 && state.prevPriority != currentPriority) {
-            setThreadPriority(myTid(), state.prevPriority);
+        if (state.regionCounter == 0) {
+            final int currentPriority = getThreadPriority(myTid());
+            if (state.prevPriority != currentPriority) {
+                setThreadPriority(myTid(), state.prevPriority);
+            }
         }
     }
 
@@ -77,9 +79,11 @@
         mBoostToPriority = priority;
         final PriorityState state = mThreadState.get();
         final int tid = myTid();
-        final int prevPriority = getThreadPriority(tid);
-        if (state.regionCounter != 0 && prevPriority != priority) {
-            setThreadPriority(tid, priority);
+        if (state.regionCounter != 0) {
+            final int prevPriority = getThreadPriority(tid);
+            if (prevPriority != priority) {
+                setThreadPriority(tid, priority);
+            }
         }
     }
 
diff --git a/services/core/java/com/android/server/VibratorService.java b/services/core/java/com/android/server/VibratorService.java
index f6e698f..9b9911a 100644
--- a/services/core/java/com/android/server/VibratorService.java
+++ b/services/core/java/com/android/server/VibratorService.java
@@ -593,7 +593,7 @@
                 Vibration vib = new Vibration(token, effect, usageHint, uid, opPkg, reason);
                 if (mProcStatesCache.get(uid, ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND)
                         > ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND
-                        && vib.isHapticFeedback()) {
+                        && !vib.isNotification() && !vib.isRingtone()) {
                     Slog.e(TAG, "Ignoring incoming vibration as process with uid = "
                             + uid + " is background");
                     return;
diff --git a/services/core/java/com/android/server/am/ActiveInstrumentation.java b/services/core/java/com/android/server/am/ActiveInstrumentation.java
index 15de3de..9510f59 100644
--- a/services/core/java/com/android/server/am/ActiveInstrumentation.java
+++ b/services/core/java/com/android/server/am/ActiveInstrumentation.java
@@ -49,6 +49,9 @@
     // Connection to use the UI introspection APIs.
     IUiAutomationConnection mUiAutomationConnection;
 
+    // Whether the caller holds START_ACTIVITIES_FROM_BACKGROUND permission
+    boolean mHasBackgroundActivityStartsPermission;
+
     // As given to us
     Bundle mArguments;
 
@@ -117,6 +120,8 @@
             pw.print(prefix); pw.print("mUiAutomationConnection=");
             pw.println(mUiAutomationConnection);
         }
+        pw.print("mHasBackgroundActivityStartsPermission=");
+        pw.println(mHasBackgroundActivityStartsPermission);
         pw.print(prefix); pw.print("mArguments=");
         pw.println(mArguments);
     }
diff --git a/services/core/java/com/android/server/am/ActiveServices.java b/services/core/java/com/android/server/am/ActiveServices.java
index d95604e..2f1f91e 100644
--- a/services/core/java/com/android/server/am/ActiveServices.java
+++ b/services/core/java/com/android/server/am/ActiveServices.java
@@ -17,6 +17,7 @@
 package com.android.server.am;
 
 import static android.content.pm.PackageManager.PERMISSION_GRANTED;
+import static android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST;
 
 import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_BACKGROUND_CHECK;
 import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOREGROUND_SERVICE;
@@ -218,7 +219,7 @@
                     if (DEBUG_FOREGROUND_SERVICE) {
                         Slog.i(TAG, "  Stopping fg for service " + r);
                     }
-                    setServiceForegroundInnerLocked(r, 0, null, 0);
+                    setServiceForegroundInnerLocked(r, 0, null, 0, 0);
                 }
             }
         }
@@ -914,13 +915,13 @@
     }
 
     public void setServiceForegroundLocked(ComponentName className, IBinder token,
-            int id, Notification notification, int flags) {
+            int id, Notification notification, int flags, int foregroundServiceType) {
         final int userId = UserHandle.getCallingUserId();
         final long origId = Binder.clearCallingIdentity();
         try {
             ServiceRecord r = findServiceLocked(className, token, userId);
             if (r != null) {
-                setServiceForegroundInnerLocked(r, id, notification, flags);
+                setServiceForegroundInnerLocked(r, id, notification, flags, foregroundServiceType);
             }
         } finally {
             Binder.restoreCallingIdentity(origId);
@@ -1211,7 +1212,7 @@
      * @param id Notification ID.  Zero === exit foreground state for the given service.
      */
     private void setServiceForegroundInnerLocked(final ServiceRecord r, int id,
-            Notification notification, int flags) {
+            Notification notification, int flags, int foregroundServiceType) {
         if (id != 0) {
             if (notification == null) {
                 throw new IllegalArgumentException("null notification");
@@ -1244,13 +1245,20 @@
                             android.Manifest.permission.FOREGROUND_SERVICE,
                             r.app.pid, r.appInfo.uid, "startForeground");
                 }
-                if (r.appInfo.targetSdkVersion >= Build.VERSION_CODES.Q) {
-                    if (r.serviceInfo.getForegroundServiceType()
-                            == ServiceInfo.FOREGROUND_SERVICE_TYPE_UNSPECIFIED) {
-                        // STOPSHIP(b/120611119): replace log message with SecurityException.
-                        Slog.w(TAG, "missing foregroundServiceType attribute in "
-                                + "service element of manifest file");
-                    }
+
+                int manifestType = r.serviceInfo.getForegroundServiceType();
+                // If passed in foreground service type is FOREGROUND_SERVICE_TYPE_MANIFEST,
+                // consider it is the same as manifest foreground service type.
+                if (foregroundServiceType == FOREGROUND_SERVICE_TYPE_MANIFEST) {
+                    foregroundServiceType = manifestType;
+                }
+                // Check the passed in foreground service type flags is a subset of manifest
+                // foreground service type flags.
+                if ((foregroundServiceType & manifestType) != foregroundServiceType) {
+                    // STOPSHIP(b/120611119): replace log message with IllegalArgumentException.
+                    Slog.w(TAG, "foregroundServiceType must be a subset of "
+                            + "foregroundServiceType attribute in "
+                            + "service element of manifest file");
                 }
             }
             boolean alreadyStartedOp = false;
@@ -1307,6 +1315,7 @@
                     }
                     notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
                     r.foregroundNoti = notification;
+                    r.foregroundServiceType = foregroundServiceType;
                     if (!r.isForeground) {
                         final ServiceMap smap = getServiceMapLocked(r.userId);
                         if (smap != null) {
@@ -1443,7 +1452,7 @@
             ServiceRecord sr = proc.services.valueAt(i);
             if (sr.isForeground || sr.fgRequired) {
                 anyForeground = true;
-                fgServiceTypes |= sr.serviceInfo.mForegroundServiceType;
+                fgServiceTypes |= sr.foregroundServiceType;
                 break;
             }
         }
diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java
index bec7386..29baacb 100644
--- a/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -22,6 +22,7 @@
 import static android.Manifest.permission.INTERACT_ACROSS_USERS;
 import static android.Manifest.permission.INTERACT_ACROSS_USERS_FULL;
 import static android.Manifest.permission.REMOVE_TASKS;
+import static android.Manifest.permission.START_ACTIVITIES_FROM_BACKGROUND;
 import static android.app.ActivityManager.INSTR_FLAG_DISABLE_HIDDEN_API_CHECKS;
 import static android.app.ActivityManager.INSTR_FLAG_MOUNT_EXTERNAL_STORAGE_FULL;
 import static android.app.ActivityManager.PROCESS_STATE_LAST_ACTIVITY;
@@ -548,6 +549,11 @@
     // Whether we should use SCHED_FIFO for UI and RenderThreads.
     boolean mUseFifoUiScheduling = false;
 
+    // Use an offload queue for long broadcasts, e.g. BOOT_COMPLETED.
+    // For simplicity, since we statically declare the size of the array of BroadcastQueues,
+    // we still create this new offload queue, but never ever put anything on it.
+    boolean mEnableOffloadQueue;
+
     BroadcastQueue mFgBroadcastQueue;
     BroadcastQueue mBgBroadcastQueue;
     BroadcastQueue mOffloadBroadcastQueue;
@@ -2217,7 +2223,7 @@
         mConstants = hasHandlerThread ? new ActivityManagerConstants(this, mHandler) : null;
         final ActiveUids activeUids = new ActiveUids(this, false /* postChangesToAtm */);
         mProcessList.init(this, activeUids);
-        mOomAdjuster = new OomAdjuster(this, mProcessList, activeUids);
+        mOomAdjuster = new OomAdjuster(this, mProcessList, activeUids, new Object());
 
         mIntentFirewall = hasHandlerThread
                 ? new IntentFirewall(new IntentFirewallInterface(), mHandler) : null;
@@ -2265,7 +2271,7 @@
         mConstants = new ActivityManagerConstants(this, mHandler);
         final ActiveUids activeUids = new ActiveUids(this, true /* postChangesToAtm */);
         mProcessList.init(this, activeUids);
-        mOomAdjuster = new OomAdjuster(this, mProcessList, activeUids);
+        mOomAdjuster = new OomAdjuster(this, mProcessList, activeUids, atm.getGlobalLock());
 
         // Broadcast policy parameters
         final BroadcastConstants foreConstants = new BroadcastConstants(
@@ -2282,6 +2288,9 @@
         // by default, no "slow" policy in this queue
         offloadConstants.SLOW_TIME = Integer.MAX_VALUE;
 
+        mEnableOffloadQueue = SystemProperties.getBoolean(
+                "persist.device_config.activity_manager_native_boot.offload_queue_enabled", false);
+
         mFgBroadcastQueue = new BroadcastQueue(this, mHandler,
                 "foreground", foreConstants, false);
         mBgBroadcastQueue = new BroadcastQueue(this, mHandler,
@@ -3091,7 +3100,7 @@
                 } else {
                     UidRecord validateUid = mValidateUids.get(item.uid);
                     if (validateUid == null) {
-                        validateUid = new UidRecord(item.uid, mAtmInternal);
+                        validateUid = new UidRecord(item.uid);
                         mValidateUids.put(item.uid, validateUid);
                     }
                     if ((item.change & UidRecord.CHANGE_IDLE) != 0) {
@@ -13529,9 +13538,10 @@
 
     @Override
     public void setServiceForeground(ComponentName className, IBinder token,
-            int id, Notification notification, int flags) {
+            int id, Notification notification, int flags, int foregroundServiceType) {
         synchronized(this) {
-            mServices.setServiceForegroundLocked(className, token, id, notification, flags);
+            mServices.setServiceForegroundLocked(className, token, id, notification, flags,
+                    foregroundServiceType);
         }
     }
 
@@ -15195,7 +15205,9 @@
             IInstrumentationWatcher watcher, IUiAutomationConnection uiAutomationConnection,
             int userId, String abiOverride) {
         enforceNotIsolatedCaller("startInstrumentation");
-        userId = mUserController.handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(),
+        final int callingUid = Binder.getCallingUid();
+        final int callingPid = Binder.getCallingPid();
+        userId = mUserController.handleIncomingUser(callingPid, callingUid,
                 userId, false, ALLOW_FULL_ONLY, "startInstrumentation", null);
         // Refuse possible leaked file descriptors
         if (arguments != null && arguments.hasFileDescriptors()) {
@@ -15260,6 +15272,9 @@
             activeInstr.mWatcher = watcher;
             activeInstr.mUiAutomationConnection = uiAutomationConnection;
             activeInstr.mResultClass = className;
+            activeInstr.mHasBackgroundActivityStartsPermission = checkPermission(
+                    START_ACTIVITIES_FROM_BACKGROUND, callingPid, callingUid)
+                            == PackageManager.PERMISSION_GRANTED;
 
             boolean disableHiddenApiChecks = ai.usesNonSdkApi()
                     || (flags & INSTR_FLAG_DISABLE_HIDDEN_API_CHECKS) != 0;
@@ -18383,6 +18398,6 @@
     }
 
     private boolean isOnOffloadQueue(int flags) {
-        return ((flags & Intent.FLAG_RECEIVER_OFFLOAD) != 0);
+        return (mEnableOffloadQueue && ((flags & Intent.FLAG_RECEIVER_OFFLOAD) != 0));
     }
 }
diff --git a/services/core/java/com/android/server/am/AppCompactor.java b/services/core/java/com/android/server/am/AppCompactor.java
index bb55ec31..1118014 100644
--- a/services/core/java/com/android/server/am/AppCompactor.java
+++ b/services/core/java/com/android/server/am/AppCompactor.java
@@ -19,6 +19,7 @@
 import static android.os.Process.THREAD_PRIORITY_FOREGROUND;
 import static android.provider.DeviceConfig.ActivityManager.KEY_COMPACT_ACTION_1;
 import static android.provider.DeviceConfig.ActivityManager.KEY_COMPACT_ACTION_2;
+import static android.provider.DeviceConfig.ActivityManager.KEY_COMPACT_STATSD_SAMPLE_RATE;
 import static android.provider.DeviceConfig.ActivityManager.KEY_COMPACT_THROTTLE_1;
 import static android.provider.DeviceConfig.ActivityManager.KEY_COMPACT_THROTTLE_2;
 import static android.provider.DeviceConfig.ActivityManager.KEY_COMPACT_THROTTLE_3;
@@ -45,6 +46,7 @@
 import java.io.FileOutputStream;
 import java.io.PrintWriter;
 import java.util.ArrayList;
+import java.util.Random;
 
 public final class AppCompactor {
 
@@ -65,6 +67,8 @@
     @VisibleForTesting static final long DEFAULT_COMPACT_THROTTLE_2 = 10_000;
     @VisibleForTesting static final long DEFAULT_COMPACT_THROTTLE_3 = 500;
     @VisibleForTesting static final long DEFAULT_COMPACT_THROTTLE_4 = 10_000;
+    // The sampling rate to push app compaction events into statsd for upload.
+    @VisibleForTesting static final float DEFAULT_STATSD_SAMPLE_RATE = 0.1f;
 
     @VisibleForTesting
     interface PropertyChangedCallbackForTest {
@@ -104,6 +108,8 @@
                                 || KEY_COMPACT_THROTTLE_3.equals(name)
                                 || KEY_COMPACT_THROTTLE_4.equals(name)) {
                             updateCompactionThrottles();
+                        } else if (KEY_COMPACT_STATSD_SAMPLE_RATE.equals(name)) {
+                            updateStatsdSampleRate();
                         }
                     }
                     if (mTestCallback != null) {
@@ -116,21 +122,25 @@
 
     // Configured by phenotype. Updates from the server take effect immediately.
     @GuardedBy("mPhenotypeFlagLock")
-    @VisibleForTesting String mCompactActionSome =
+    @VisibleForTesting volatile String mCompactActionSome =
             compactActionIntToString(DEFAULT_COMPACT_ACTION_1);
     @GuardedBy("mPhenotypeFlagLock")
-    @VisibleForTesting String mCompactActionFull =
+    @VisibleForTesting volatile String mCompactActionFull =
             compactActionIntToString(DEFAULT_COMPACT_ACTION_2);
     @GuardedBy("mPhenotypeFlagLock")
-    @VisibleForTesting long mCompactThrottleSomeSome = DEFAULT_COMPACT_THROTTLE_1;
+    @VisibleForTesting volatile long mCompactThrottleSomeSome = DEFAULT_COMPACT_THROTTLE_1;
     @GuardedBy("mPhenotypeFlagLock")
-    @VisibleForTesting long mCompactThrottleSomeFull = DEFAULT_COMPACT_THROTTLE_2;
+    @VisibleForTesting volatile long mCompactThrottleSomeFull = DEFAULT_COMPACT_THROTTLE_2;
     @GuardedBy("mPhenotypeFlagLock")
-    @VisibleForTesting long mCompactThrottleFullSome = DEFAULT_COMPACT_THROTTLE_3;
+    @VisibleForTesting volatile long mCompactThrottleFullSome = DEFAULT_COMPACT_THROTTLE_3;
     @GuardedBy("mPhenotypeFlagLock")
-    @VisibleForTesting long mCompactThrottleFullFull = DEFAULT_COMPACT_THROTTLE_4;
+    @VisibleForTesting volatile long mCompactThrottleFullFull = DEFAULT_COMPACT_THROTTLE_4;
     @GuardedBy("mPhenotypeFlagLock")
-    private boolean mUseCompaction = DEFAULT_USE_COMPACTION;
+    private volatile boolean mUseCompaction = DEFAULT_USE_COMPACTION;
+
+    private final Random mRandom = new Random();
+    @GuardedBy("mPhenotypeFlagLock")
+    @VisibleForTesting volatile float mStatsdSampleRate = DEFAULT_STATSD_SAMPLE_RATE;
 
     // Handler on which compaction runs.
     private Handler mCompactionHandler;
@@ -158,6 +168,7 @@
             updateUseCompaction();
             updateCompactionActions();
             updateCompactionThrottles();
+            updateStatsdSampleRate();
         }
     }
 
@@ -181,6 +192,7 @@
             pw.println("  " + KEY_COMPACT_THROTTLE_2 + "=" + mCompactThrottleSomeFull);
             pw.println("  " + KEY_COMPACT_THROTTLE_3 + "=" + mCompactThrottleFullSome);
             pw.println("  " + KEY_COMPACT_THROTTLE_4 + "=" + mCompactThrottleFullFull);
+            pw.println("  " + KEY_COMPACT_STATSD_SAMPLE_RATE + "=" + mStatsdSampleRate);
         }
     }
 
@@ -289,6 +301,19 @@
         }
     }
 
+    @GuardedBy("mPhenotypeFlagLock")
+    private void updateStatsdSampleRate() {
+        String sampleRateFlag = DeviceConfig.getProperty(DeviceConfig.ActivityManager.NAMESPACE,
+                KEY_COMPACT_STATSD_SAMPLE_RATE);
+        try {
+            mStatsdSampleRate = TextUtils.isEmpty(sampleRateFlag)
+                    ? DEFAULT_STATSD_SAMPLE_RATE : Float.parseFloat(sampleRateFlag);
+        } catch (NumberFormatException e) {
+            mStatsdSampleRate = DEFAULT_STATSD_SAMPLE_RATE;
+        }
+        mStatsdSampleRate = Math.min(1.0f, Math.max(0.0f, mStatsdSampleRate));
+    }
+
     @VisibleForTesting
     static String compactActionIntToString(int action) {
         switch(action) {
@@ -385,11 +410,16 @@
                                 rssBefore[0], rssBefore[1], rssBefore[2], rssBefore[3],
                                 rssAfter[0], rssAfter[1], rssAfter[2], rssAfter[3], time,
                                 lastCompactAction, lastCompactTime, msg.arg1, msg.arg2);
-                        StatsLog.write(StatsLog.APP_COMPACTED, pid, name, pendingAction,
-                                rssBefore[0], rssBefore[1], rssBefore[2], rssBefore[3],
-                                rssAfter[0], rssAfter[1], rssAfter[2], rssAfter[3], time,
-                                lastCompactAction, lastCompactTime, msg.arg1,
-                                ActivityManager.processStateAmToProto(msg.arg2));
+                        // Note that as above not taking mPhenoTypeFlagLock here to avoid locking
+                        // on every single compaction for a flag that will seldom change and the
+                        // impact of reading the wrong value here is low.
+                        if (mRandom.nextFloat() < mStatsdSampleRate) {
+                            StatsLog.write(StatsLog.APP_COMPACTED, pid, name, pendingAction,
+                                    rssBefore[0], rssBefore[1], rssBefore[2], rssBefore[3],
+                                    rssAfter[0], rssAfter[1], rssAfter[2], rssAfter[3], time,
+                                    lastCompactAction, lastCompactTime, msg.arg1,
+                                    ActivityManager.processStateAmToProto(msg.arg2));
+                        }
                         synchronized (mAm) {
                             proc.lastCompactTime = end;
                             proc.lastCompactAction = pendingAction;
diff --git a/services/core/java/com/android/server/am/BroadcastQueue.java b/services/core/java/com/android/server/am/BroadcastQueue.java
index 64a36ef..1fb11ba 100644
--- a/services/core/java/com/android/server/am/BroadcastQueue.java
+++ b/services/core/java/com/android/server/am/BroadcastQueue.java
@@ -450,7 +450,7 @@
         if (state == BroadcastRecord.IDLE) {
             Slog.w(TAG, "finishReceiver [" + mQueueName + "] called but state is IDLE");
         }
-        if (r.allowBackgroundActivityStarts) {
+        if (r.allowBackgroundActivityStarts && r.curApp != null) {
             r.curApp.removeAllowBackgroundActivityStartsToken(r);
          }
         // If we're abandoning this broadcast before any receivers were actually spun up,
diff --git a/services/core/java/com/android/server/am/OomAdjuster.java b/services/core/java/com/android/server/am/OomAdjuster.java
index 6e8646e..4985c52 100644
--- a/services/core/java/com/android/server/am/OomAdjuster.java
+++ b/services/core/java/com/android/server/am/OomAdjuster.java
@@ -127,8 +127,18 @@
     private final ActivityManagerService mService;
     private final ProcessList mProcessList;
 
-    OomAdjuster(ActivityManagerService service, ProcessList processList, ActiveUids activeUids) {
+    /**
+     * Used to lock {@link #updateOomAdjImpl} for state consistency. It also reduces frequency lock
+     * and unlock when getting and setting value to {@link ProcessRecord#mWindowProcessController}.
+     * Note it is declared as Object type so the locked-region-code-injection won't wrap the
+     * unnecessary priority booster.
+     */
+    private final Object mAtmGlobalLock;
+
+    OomAdjuster(ActivityManagerService service, ProcessList processList, ActiveUids activeUids,
+            Object atmGlobalLock) {
         mService = service;
+        mAtmGlobalLock = atmGlobalLock;
         mProcessList = processList;
         mActiveUids = activeUids;
 
@@ -186,6 +196,13 @@
 
     @GuardedBy("mService")
     final void updateOomAdjLocked() {
+        synchronized (mAtmGlobalLock) {
+            updateOomAdjImpl();
+        }
+    }
+
+    @GuardedBy({"mService", "mAtmGlobalLock"})
+    private void updateOomAdjImpl() {
         Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "updateOomAdj");
         mService.mOomAdjProfiler.oomAdjStarted();
         final ProcessRecord TOP_APP = mService.getTopAppLocked();
@@ -534,6 +551,7 @@
                 uidRec.setProcState = uidRec.getCurProcState();
                 uidRec.setWhitelist = uidRec.curWhitelist;
                 uidRec.setIdle = uidRec.idle;
+                mService.mAtmInternal.onUidProcStateChanged(uidRec.uid, uidRec.setProcState);
                 mService.enqueueUidChangeLocked(uidRec, -1, uidChange);
                 mService.noteUidProcessState(uidRec.uid, uidRec.getCurProcState());
                 if (uidRec.foregroundServices) {
diff --git a/services/core/java/com/android/server/am/ProcessList.java b/services/core/java/com/android/server/am/ProcessList.java
index 4aa71f9..9cbd065 100644
--- a/services/core/java/com/android/server/am/ProcessList.java
+++ b/services/core/java/com/android/server/am/ProcessList.java
@@ -1507,7 +1507,7 @@
                 mService.mNativeDebuggingApp = null;
             }
 
-            if (app.info.isCodeIntegrityPreferred()
+            if (app.info.isEmbeddedDexUsed()
                     || (app.info.isPrivilegedApp()
                         && DexManager.isPackageSelectedToRunOob(app.pkgList.mPkgList.keySet()))) {
                 runtimeFlags |= Zygote.ONLY_USE_SYSTEM_OAT_FILES;
@@ -2197,7 +2197,7 @@
         }
         UidRecord uidRec = mActiveUids.get(proc.uid);
         if (uidRec == null) {
-            uidRec = new UidRecord(proc.uid, mService.mAtmInternal);
+            uidRec = new UidRecord(proc.uid);
             // This is the first appearance of the uid, report it now!
             if (DEBUG_UID_OBSERVERS) Slog.i(TAG_UID_OBSERVERS,
                     "Creating new process uid: " + uidRec);
diff --git a/services/core/java/com/android/server/am/ProcessRecord.java b/services/core/java/com/android/server/am/ProcessRecord.java
index 7ae77d5..6161f7d 100644
--- a/services/core/java/com/android/server/am/ProcessRecord.java
+++ b/services/core/java/com/android/server/am/ProcessRecord.java
@@ -655,10 +655,6 @@
         return mWindowProcessController.hasActivities();
     }
 
-    void clearActivities() {
-        mWindowProcessController.clearActivities();
-    }
-
     boolean hasActivitiesOrRecentTasks() {
         return mWindowProcessController.hasActivitiesOrRecentTasks();
     }
@@ -667,10 +663,6 @@
         return mWindowProcessController.hasRecentTasks();
     }
 
-    void clearRecentTasks() {
-        mWindowProcessController.clearRecentTasks();
-    }
-
     /**
      * This method returns true if any of the activities within the process record are interesting
      * to the user. See HistoryRecord.isInterestingToUserLocked()
@@ -1181,6 +1173,8 @@
     void setActiveInstrumentation(ActiveInstrumentation instr) {
         mInstr = instr;
         mWindowProcessController.setInstrumenting(instr != null);
+        mWindowProcessController.setInstrumentingWithBackgroundActivityStartPrivileges(instr != null
+                && instr.mHasBackgroundActivityStartsPermission);
     }
 
     ActiveInstrumentation getActiveInstrumentation() {
diff --git a/services/core/java/com/android/server/am/ServiceRecord.java b/services/core/java/com/android/server/am/ServiceRecord.java
index da5ce1c..abc1066a 100644
--- a/services/core/java/com/android/server/am/ServiceRecord.java
+++ b/services/core/java/com/android/server/am/ServiceRecord.java
@@ -16,10 +16,8 @@
 
 package com.android.server.am;
 
-import com.android.internal.app.procstats.ServiceState;
-import com.android.internal.os.BatteryStatsImpl;
-import com.android.server.LocalServices;
-import com.android.server.notification.NotificationManagerInternal;
+import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
+import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
 
 import android.app.INotificationManager;
 import android.app.Notification;
@@ -44,6 +42,11 @@
 import android.util.TimeUtils;
 import android.util.proto.ProtoOutputStream;
 import android.util.proto.ProtoUtils;
+
+import com.android.internal.app.procstats.ServiceState;
+import com.android.internal.os.BatteryStatsImpl;
+import com.android.server.LocalServices;
+import com.android.server.notification.NotificationManagerInternal;
 import com.android.server.uri.NeededUriGrants;
 import com.android.server.uri.UriPermissionOwner;
 
@@ -52,9 +55,6 @@
 import java.util.List;
 import java.util.Objects;
 
-import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
-import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
-
 /**
  * A running application service.
  */
@@ -103,6 +103,7 @@
     boolean isForeground;   // is service currently in foreground mode?
     int foregroundId;       // Notification ID of last foreground req.
     Notification foregroundNoti; // Notification record of foreground state.
+    int foregroundServiceType; // foreground service types.
     long lastActivity;      // last time there was some activity on the service.
     long startingBgTimeout;  // time at which we scheduled this for a delayed start.
     boolean startRequested; // someone explicitly called start?
@@ -722,7 +723,7 @@
                         // If it gave us a garbage notification, it doesn't
                         // get to be foreground.
                         ams.setServiceForeground(instanceName, ServiceRecord.this,
-                                0, null, 0);
+                                0, null, 0, 0);
                         ams.crashApplication(appUid, appPid, localPackageName, -1,
                                 "Bad notification for startForeground: " + e);
                     }
diff --git a/services/core/java/com/android/server/am/UidRecord.java b/services/core/java/com/android/server/am/UidRecord.java
index 6cb1097..22a7de7 100644
--- a/services/core/java/com/android/server/am/UidRecord.java
+++ b/services/core/java/com/android/server/am/UidRecord.java
@@ -26,7 +26,6 @@
 import android.util.proto.ProtoUtils;
 
 import com.android.internal.annotations.GuardedBy;
-import com.android.server.wm.ActivityTaskManagerInternal;
 
 /**
  * Overall information about a uid that has actively running processes.
@@ -43,7 +42,6 @@
     boolean idle;
     boolean setIdle;
     int numProcs;
-    final ActivityTaskManagerInternal mAtmInternal;
 
     /**
      * Sequence number associated with the {@link #mCurProcState}. This is incremented using
@@ -117,10 +115,9 @@
     ChangeItem pendingChange;
     int lastReportedChange;
 
-    public UidRecord(int _uid, ActivityTaskManagerInternal atmInternal) {
+    public UidRecord(int _uid) {
         uid = _uid;
         idle = true;
-        mAtmInternal = atmInternal;
         reset();
     }
 
@@ -130,9 +127,6 @@
 
     public void setCurProcState(int curProcState) {
         mCurProcState = curProcState;
-        if (mAtmInternal != null) {
-            mAtmInternal.onUidProcStateChanged(uid, curProcState);
-        }
     }
 
     public void reset() {
diff --git a/services/core/java/com/android/server/attention/AttentionManagerService.java b/services/core/java/com/android/server/attention/AttentionManagerService.java
index 5b469fe..2061b26 100644
--- a/services/core/java/com/android/server/attention/AttentionManagerService.java
+++ b/services/core/java/com/android/server/attention/AttentionManagerService.java
@@ -16,9 +16,9 @@
 
 package com.android.server.attention;
 
+import static android.provider.DeviceConfig.AttentionManagerService.COMPONENT_NAME;
 import static android.provider.DeviceConfig.AttentionManagerService.NAMESPACE;
-import static android.provider.DeviceConfig.AttentionManagerService.PROPERTY_COMPONENT_NAME;
-import static android.provider.DeviceConfig.AttentionManagerService.PROPERTY_SERVICE_ENABLED;
+import static android.provider.DeviceConfig.AttentionManagerService.SERVICE_ENABLED;
 
 import android.Manifest;
 import android.annotation.Nullable;
@@ -129,7 +129,7 @@
     }
 
     private boolean isServiceEnabled() {
-        final String enabled = DeviceConfig.getProperty(NAMESPACE, PROPERTY_SERVICE_ENABLED);
+        final String enabled = DeviceConfig.getProperty(NAMESPACE, SERVICE_ENABLED);
         return enabled == null ? DEFAULT_SERVICE_ENABLED : "true".equals(enabled);
     }
 
@@ -279,7 +279,7 @@
      * system.
      */
     private static ComponentName resolveAttentionService(Context context) {
-        final String flag = DeviceConfig.getProperty(NAMESPACE, PROPERTY_COMPONENT_NAME);
+        final String flag = DeviceConfig.getProperty(NAMESPACE, COMPONENT_NAME);
 
         final String componentNameString = flag != null ? flag : context.getString(
                 R.string.config_defaultAttentionService);
diff --git a/services/core/java/com/android/server/audio/AudioDeviceBroker.java b/services/core/java/com/android/server/audio/AudioDeviceBroker.java
index d652f93..deaa931 100644
--- a/services/core/java/com/android/server/audio/AudioDeviceBroker.java
+++ b/services/core/java/com/android/server/audio/AudioDeviceBroker.java
@@ -600,7 +600,7 @@
                     break;
                 case MSG_IL_SET_A2DP_SINK_CONNECTION_STATE:
                     mDeviceInventory.onSetA2dpSinkConnectionState(
-                            (BtHelper.BluetoothA2dpDeviceInfo) msg.obj, msg.arg1, msg.arg2);
+                            (BtHelper.BluetoothA2dpDeviceInfo) msg.obj, msg.arg1);
                     break;
                 case MSG_IL_SET_A2DP_SOURCE_CONNECTION_STATE:
                     mDeviceInventory.onSetA2dpSourceConnectionState(
diff --git a/services/core/java/com/android/server/audio/AudioDeviceInventory.java b/services/core/java/com/android/server/audio/AudioDeviceInventory.java
index eb76e6e..97649a7 100644
--- a/services/core/java/com/android/server/audio/AudioDeviceInventory.java
+++ b/services/core/java/com/android/server/audio/AudioDeviceInventory.java
@@ -147,18 +147,20 @@
     }
 
     /*package*/ void onSetA2dpSinkConnectionState(@NonNull BtHelper.BluetoothA2dpDeviceInfo btInfo,
-            @AudioService.BtProfileConnectionState int state, int a2dpVolume) {
+            @AudioService.BtProfileConnectionState int state) {
         final BluetoothDevice btDevice = btInfo.getBtDevice();
+        int a2dpVolume = btInfo.getVolume();
         if (AudioService.DEBUG_DEVICES) {
             Log.d(TAG, "onSetA2dpSinkConnectionState btDevice=" + btDevice + " state="
-                    + state + " is dock=" + btDevice.isBluetoothDock());
+                    + state + " is dock=" + btDevice.isBluetoothDock() + " vol=" + a2dpVolume);
         }
         String address = btDevice.getAddress();
         if (!BluetoothAdapter.checkBluetoothAddress(address)) {
             address = "";
         }
         AudioService.sDeviceLogger.log(new AudioEventLogger.StringEvent(
-                "A2DP sink connected: device addr=" + address + " state=" + state));
+                "A2DP sink connected: device addr=" + address + " state=" + state
+                        + " vol=" + a2dpVolume));
 
         final int a2dpCodec;
         synchronized (mDeviceBroker.mA2dpAvrcpLock) {
diff --git a/services/core/java/com/android/server/audio/AudioService.java b/services/core/java/com/android/server/audio/AudioService.java
index df33bf2..1723163 100644
--- a/services/core/java/com/android/server/audio/AudioService.java
+++ b/services/core/java/com/android/server/audio/AudioService.java
@@ -392,12 +392,14 @@
     * e.g. user on homescreen, no app playing anything, presses hardware volume buttons, this
     *    stream type is controlled.
     */
-   protected static final int DEFAULT_VOL_STREAM_NO_PLAYBACK = AudioSystem.STREAM_MUSIC;
+    protected static final int DEFAULT_VOL_STREAM_NO_PLAYBACK = AudioSystem.STREAM_MUSIC;
 
     private final AudioSystem.ErrorCallback mAudioSystemCallback = new AudioSystem.ErrorCallback() {
         public void onError(int error) {
             switch (error) {
             case AudioSystem.AUDIO_STATUS_SERVER_DIED:
+                mRecordMonitor.clear();
+
                 sendMsg(mAudioHandler, MSG_AUDIO_SERVER_DIED,
                         SENDMSG_NOOP, 0, 0, null, 0);
                 sendMsg(mAudioHandler, MSG_DISPATCH_AUDIO_SERVER_STATE,
diff --git a/services/core/java/com/android/server/audio/RecordingActivityMonitor.java b/services/core/java/com/android/server/audio/RecordingActivityMonitor.java
index 9d6628c..b2c7ff3 100644
--- a/services/core/java/com/android/server/audio/RecordingActivityMonitor.java
+++ b/services/core/java/com/android/server/audio/RecordingActivityMonitor.java
@@ -78,24 +78,27 @@
                 updateSnapshot(event, uid, session, source, recordingInfo,
                 portId, silenced, activeSource, clientEffects, effects);
         if (configsSystem != null){
-            synchronized (mClients) {
-                // list of recording configurations for "public consumption". It is only computed if
-                // there are non-system recording activity listeners.
-                final List<AudioRecordingConfiguration> configsPublic = mHasPublicClients ?
-                        anonymizeForPublicConsumption(configsSystem) :
-                            new ArrayList<AudioRecordingConfiguration>();
-                final Iterator<RecMonitorClient> clientIterator = mClients.iterator();
-                while (clientIterator.hasNext()) {
-                    final RecMonitorClient rmc = clientIterator.next();
-                    try {
-                        if (rmc.mIsPrivileged) {
-                            rmc.mDispatcherCb.dispatchRecordingConfigChange(configsSystem);
-                        } else {
-                            rmc.mDispatcherCb.dispatchRecordingConfigChange(configsPublic);
-                        }
-                    } catch (RemoteException e) {
-                        Log.w(TAG, "Could not call dispatchRecordingConfigChange() on client", e);
+            dispatchCallbacks(configsSystem);
+        }
+    }
+    private void dispatchCallbacks(List<AudioRecordingConfiguration> configs) {
+        synchronized (mClients) {
+            // list of recording configurations for "public consumption". It is only computed if
+            // there are non-system recording activity listeners.
+            final List<AudioRecordingConfiguration> configsPublic = mHasPublicClients
+                    ? anonymizeForPublicConsumption(configs) :
+                      new ArrayList<AudioRecordingConfiguration>();
+            final Iterator<RecMonitorClient> clientIterator = mClients.iterator();
+            while (clientIterator.hasNext()) {
+                final RecMonitorClient rmc = clientIterator.next();
+                try {
+                    if (rmc.mIsPrivileged) {
+                        rmc.mDispatcherCb.dispatchRecordingConfigChange(configs);
+                    } else {
+                        rmc.mDispatcherCb.dispatchRecordingConfigChange(configsPublic);
                     }
+                } catch (RemoteException e) {
+                    Log.w(TAG, "Could not call dispatchRecordingConfigChange() on client", e);
                 }
             }
         }
@@ -130,6 +133,13 @@
         AudioSystem.setRecordingCallback(this);
     }
 
+    void clear() {
+        synchronized (mRecordConfigs) {
+            mRecordConfigs.clear();
+        }
+        dispatchCallbacks(new ArrayList<AudioRecordingConfiguration>());
+    }
+
     void registerRecordingCallback(IRecordingConfigDispatcher rcdb, boolean isPrivileged) {
         if (rcdb == null) {
             return;
diff --git a/services/core/java/com/android/server/biometrics/BiometricService.java b/services/core/java/com/android/server/biometrics/BiometricService.java
index 15d66e6..8cc560e 100644
--- a/services/core/java/com/android/server/biometrics/BiometricService.java
+++ b/services/core/java/com/android/server/biometrics/BiometricService.java
@@ -719,14 +719,17 @@
             // result back to the client.
             // TODO(b/123378871): Remove when moved.
             if (bundle.getBoolean(BiometricPrompt.KEY_ENABLE_FALLBACK)) {
-                mConfirmDeviceCredentialReceiver = receiver;
-                final KeyguardManager kgm = getContext().getSystemService(KeyguardManager.class);
-                // Use this so we don't need to duplicate logic..
-                final Intent intent = kgm.createConfirmDeviceCredentialIntent(null /* title */,
-                        null /* description */);
-                // Then give it the bundle to do magic behavior..
-                intent.putExtra(KeyguardManager.EXTRA_BIOMETRIC_PROMPT_BUNDLE, bundle);
-                getContext().startActivityAsUser(intent, UserHandle.CURRENT);
+                mHandler.post(() -> {
+                    mConfirmDeviceCredentialReceiver = receiver;
+                    final KeyguardManager kgm = getContext().getSystemService(
+                            KeyguardManager.class);
+                    // Use this so we don't need to duplicate logic..
+                    final Intent intent = kgm.createConfirmDeviceCredentialIntent(null /* title */,
+                            null /* description */);
+                    // Then give it the bundle to do magic behavior..
+                    intent.putExtra(KeyguardManager.EXTRA_BIOMETRIC_PROMPT_BUNDLE, bundle);
+                    getContext().startActivityAsUser(intent, UserHandle.CURRENT);
+                });
                 return;
             }
 
diff --git a/services/core/java/com/android/server/connectivity/Tethering.java b/services/core/java/com/android/server/connectivity/Tethering.java
index eb5be77..a14fd17 100644
--- a/services/core/java/com/android/server/connectivity/Tethering.java
+++ b/services/core/java/com/android/server/connectivity/Tethering.java
@@ -121,7 +121,6 @@
 import java.util.HashSet;
 import java.util.Set;
 
-
 /**
  * @hide
  *
@@ -223,7 +222,8 @@
 
         IntentFilter filter = new IntentFilter();
         filter.addAction(ACTION_CARRIER_CONFIG_CHANGED);
-        mEntitlementMgr = mDeps.getEntitlementManager(mContext, mLog, systemProperties);
+        mEntitlementMgr = mDeps.getEntitlementManager(mContext, mTetherMasterSM,
+                mLog, systemProperties);
         mCarrierConfigChange = new VersionedBroadcastListener(
                 "CarrierConfigChangeListener", mContext, smHandler, filter,
                 (Intent ignored) -> {
@@ -470,6 +470,7 @@
                 } else {
                     sendTetherResult(receiver, resultCode);
                 }
+                mEntitlementMgr.updateEntitlementCacheValue(type, resultCode);
             }
         };
 
@@ -1662,6 +1663,14 @@
         mUpstreamNetworkMonitor.startTrackDefaultNetwork(mDeps.getDefaultNetworkRequest());
     }
 
+    /** Get the latest value of the tethering entitlement check. */
+    public void getLatestTetheringEntitlementValue(int type, ResultReceiver receiver,
+            boolean showEntitlementUi) {
+        if (receiver != null) {
+            mEntitlementMgr.getLatestTetheringEntitlementValue(type, receiver, showEntitlementUi);
+        }
+    }
+
     @Override
     public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
         // Binder.java closes the resource for us.
diff --git a/services/core/java/com/android/server/connectivity/Vpn.java b/services/core/java/com/android/server/connectivity/Vpn.java
index 7e95f10..e1af81b 100644
--- a/services/core/java/com/android/server/connectivity/Vpn.java
+++ b/services/core/java/com/android/server/connectivity/Vpn.java
@@ -166,6 +166,7 @@
     private final NetworkInfo mNetworkInfo;
     private String mPackage;
     private int mOwnerUID;
+    private boolean mIsPackageTargetingAtLeastQ;
     private String mInterface;
     private Connection mConnection;
     private LegacyVpnRunner mLegacyVpnRunner;
@@ -227,6 +228,7 @@
 
         mPackage = VpnConfig.LEGACY_VPN;
         mOwnerUID = getAppUid(mPackage, mUserHandle);
+        mIsPackageTargetingAtLeastQ = doesPackageTargetAtLeastQ(mPackage);
 
         try {
             netService.registerObserver(mObserver);
@@ -268,8 +270,11 @@
 
     public void updateCapabilities() {
         final Network[] underlyingNetworks = (mConfig != null) ? mConfig.underlyingNetworks : null;
+        // Only apps targeting Q and above can explicitly declare themselves as metered.
+        final boolean isAlwaysMetered =
+                mIsPackageTargetingAtLeastQ && (mConfig == null || mConfig.isMetered);
         updateCapabilities(mContext.getSystemService(ConnectivityManager.class), underlyingNetworks,
-                mNetworkCapabilities);
+                mNetworkCapabilities, isAlwaysMetered);
 
         if (mNetworkAgent != null) {
             mNetworkAgent.sendNetworkCapabilities(mNetworkCapabilities);
@@ -278,11 +283,13 @@
 
     @VisibleForTesting
     public static void updateCapabilities(ConnectivityManager cm, Network[] underlyingNetworks,
-            NetworkCapabilities caps) {
+            NetworkCapabilities caps, boolean isAlwaysMetered) {
         int[] transportTypes = new int[] { NetworkCapabilities.TRANSPORT_VPN };
         int downKbps = NetworkCapabilities.LINK_BANDWIDTH_UNSPECIFIED;
         int upKbps = NetworkCapabilities.LINK_BANDWIDTH_UNSPECIFIED;
-        boolean metered = false;
+        // VPN's meteredness is OR'd with isAlwaysMetered and meteredness of its underlying
+        // networks.
+        boolean metered = isAlwaysMetered;
         boolean roaming = false;
         boolean congested = false;
 
@@ -725,6 +732,7 @@
             Log.i(TAG, "Switched from " + mPackage + " to " + newPackage);
             mPackage = newPackage;
             mOwnerUID = getAppUid(newPackage, mUserHandle);
+            mIsPackageTargetingAtLeastQ = doesPackageTargetAtLeastQ(newPackage);
             try {
                 mNetd.allowProtect(mOwnerUID);
             } catch (Exception e) {
@@ -790,6 +798,21 @@
         return result;
     }
 
+    private boolean doesPackageTargetAtLeastQ(String packageName) {
+        if (VpnConfig.LEGACY_VPN.equals(packageName)) {
+            return true;
+        }
+        PackageManager pm = mContext.getPackageManager();
+        try {
+            ApplicationInfo appInfo =
+                    pm.getApplicationInfoAsUser(packageName, 0 /*flags*/, mUserHandle);
+            return appInfo.targetSdkVersion >= VERSION_CODES.Q;
+        } catch (NameNotFoundException unused) {
+            Log.w(TAG, "Can't find \"" + packageName + "\"");
+            return false;
+        }
+    }
+
     public NetworkInfo getNetworkInfo() {
         return mNetworkInfo;
     }
@@ -1078,6 +1101,8 @@
                 // as rules are deleted. This prevents data leakage as the rules are moved over.
                 agentDisconnect(oldNetworkAgent);
             }
+            // Set up VPN's capabilities such as meteredness.
+            updateCapabilities();
 
             if (oldConnection != null) {
                 mContext.unbindService(oldConnection);
@@ -1778,6 +1803,7 @@
         config.user = profile.key;
         config.interfaze = iface;
         config.session = profile.name;
+        config.isMetered = false;
 
         config.addLegacyRoutes(profile.routes);
         if (!profile.dnsServers.isEmpty()) {
diff --git a/services/core/java/com/android/server/connectivity/tethering/EntitlementManager.java b/services/core/java/com/android/server/connectivity/tethering/EntitlementManager.java
index a4e3e1d..75aac10 100644
--- a/services/core/java/com/android/server/connectivity/tethering/EntitlementManager.java
+++ b/services/core/java/com/android/server/connectivity/tethering/EntitlementManager.java
@@ -21,6 +21,9 @@
 import static android.net.ConnectivityManager.EXTRA_REM_TETHER_TYPE;
 import static android.net.ConnectivityManager.EXTRA_RUN_PROVISION;
 import static android.net.ConnectivityManager.EXTRA_SET_ALARM;
+import static android.net.ConnectivityManager.TETHER_ERROR_ENTITLEMENT_UNKONWN;
+import static android.net.ConnectivityManager.TETHER_ERROR_NO_ERROR;
+import static android.net.ConnectivityManager.TETHER_ERROR_PROVISION_FAILED;
 
 import static com.android.internal.R.string.config_wifi_tether_enable;
 
@@ -31,15 +34,21 @@
 import android.content.res.Resources;
 import android.net.util.SharedLog;
 import android.os.Binder;
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.Parcel;
 import android.os.PersistableBundle;
 import android.os.ResultReceiver;
 import android.os.UserHandle;
 import android.provider.Settings;
 import android.telephony.CarrierConfigManager;
 import android.util.ArraySet;
+import android.util.Log;
+import android.util.SparseIntArray;
 
 import com.android.internal.annotations.GuardedBy;
 import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.util.StateMachine;
 import com.android.server.connectivity.MockableSystemProperties;
 
 /**
@@ -50,6 +59,7 @@
  */
 public class EntitlementManager {
     private static final String TAG = EntitlementManager.class.getSimpleName();
+    private static final boolean DBG = false;
 
     // {@link ComponentName} of the Service used to run tether provisioning.
     private static final ComponentName TETHER_SERVICE = ComponentName.unflattenFromString(
@@ -65,15 +75,19 @@
     private final Context mContext;
     private final MockableSystemProperties mSystemProperties;
     private final SharedLog mLog;
+    private final Handler mMasterHandler;
+    private final SparseIntArray mEntitlementCacheValue;
     @Nullable
     private TetheringConfiguration mConfig;
 
-    public EntitlementManager(Context ctx, SharedLog log,
+    public EntitlementManager(Context ctx, StateMachine tetherMasterSM, SharedLog log,
             MockableSystemProperties systemProperties) {
         mContext = ctx;
-        mLog = log;
+        mLog = log.forSubComponent(TAG);
         mCurrentTethers = new ArraySet<Integer>();
         mSystemProperties = systemProperties;
+        mEntitlementCacheValue = new SparseIntArray();
+        mMasterHandler = tetherMasterSM.getHandler();
     }
 
     /**
@@ -128,6 +142,10 @@
      * Reference ConnectivityManager.TETHERING_{@code *} for each tether type.
      */
     public void reevaluateSimCardProvisioning() {
+        synchronized (mEntitlementCacheValue) {
+            mEntitlementCacheValue.clear();
+        }
+
         if (!mConfig.hasMobileHotspotProvisionApp()) return;
         if (carrierConfigAffirmsEntitlementCheckNotRequired()) return;
 
@@ -175,6 +193,11 @@
     }
 
     public void runUiTetherProvisioningAndEnable(int type, ResultReceiver receiver) {
+        runUiTetherProvisioning(type, receiver);
+    }
+
+    @VisibleForTesting
+    protected void runUiTetherProvisioning(int type, ResultReceiver receiver) {
         Intent intent = new Intent(Settings.ACTION_TETHER_PROVISIONING);
         intent.putExtra(EXTRA_ADD_TETHER_TYPE, type);
         intent.putExtra(EXTRA_PROVISION_CALLBACK, receiver);
@@ -221,4 +244,70 @@
             Binder.restoreCallingIdentity(ident);
         }
     }
+
+    private ResultReceiver buildProxyReceiver(int type, final ResultReceiver receiver) {
+        ResultReceiver rr = new ResultReceiver(mMasterHandler) {
+            @Override
+            protected void onReceiveResult(int resultCode, Bundle resultData) {
+                int updatedCacheValue = updateEntitlementCacheValue(type, resultCode);
+                receiver.send(updatedCacheValue, null);
+            }
+        };
+
+        return writeToParcel(rr);
+    }
+
+    private ResultReceiver writeToParcel(final ResultReceiver receiver) {
+        // This is necessary to avoid unmarshalling issues when sending the receiver
+        // across processes.
+        Parcel parcel = Parcel.obtain();
+        receiver.writeToParcel(parcel, 0);
+        parcel.setDataPosition(0);
+        ResultReceiver receiverForSending = ResultReceiver.CREATOR.createFromParcel(parcel);
+        parcel.recycle();
+        return receiverForSending;
+    }
+
+    /**
+     * Update the last entitlement value to internal cache
+     *
+     * @param type tethering type from ConnectivityManager.TETHERING_{@code *}
+     * @param resultCode last entitlement value
+     * @return the last updated entitlement value
+     */
+    public int updateEntitlementCacheValue(int type, int resultCode) {
+        if (DBG) {
+            Log.d(TAG, "updateEntitlementCacheValue: " + type + ", result: " + resultCode);
+        }
+        synchronized (mEntitlementCacheValue) {
+            if (resultCode == TETHER_ERROR_NO_ERROR) {
+                mEntitlementCacheValue.put(type, resultCode);
+                return resultCode;
+            } else {
+                mEntitlementCacheValue.put(type, TETHER_ERROR_PROVISION_FAILED);
+                return TETHER_ERROR_PROVISION_FAILED;
+            }
+        }
+    }
+
+    /** Get the last value of the tethering entitlement check. */
+    public void getLatestTetheringEntitlementValue(int downstream, ResultReceiver receiver,
+            boolean showEntitlementUi) {
+        if (!isTetherProvisioningRequired()) {
+            receiver.send(TETHER_ERROR_NO_ERROR, null);
+            return;
+        }
+
+        final int cacheValue;
+        synchronized (mEntitlementCacheValue) {
+            cacheValue = mEntitlementCacheValue.get(
+                downstream, TETHER_ERROR_ENTITLEMENT_UNKONWN);
+        }
+        if (cacheValue == TETHER_ERROR_NO_ERROR || !showEntitlementUi) {
+            receiver.send(cacheValue, null);
+        } else {
+            ResultReceiver proxy = buildProxyReceiver(downstream, receiver);
+            runUiTetherProvisioning(downstream, proxy);
+        }
+    }
 }
diff --git a/services/core/java/com/android/server/connectivity/tethering/TetheringDependencies.java b/services/core/java/com/android/server/connectivity/tethering/TetheringDependencies.java
index a42efe9..6d6f81e 100644
--- a/services/core/java/com/android/server/connectivity/tethering/TetheringDependencies.java
+++ b/services/core/java/com/android/server/connectivity/tethering/TetheringDependencies.java
@@ -81,8 +81,8 @@
     /**
      * Get a reference to the EntitlementManager to be used by tethering.
      */
-    public EntitlementManager getEntitlementManager(Context ctx, SharedLog log,
-            MockableSystemProperties systemProperties) {
-        return new EntitlementManager(ctx, log, systemProperties);
+    public EntitlementManager getEntitlementManager(Context ctx, StateMachine target,
+            SharedLog log, MockableSystemProperties systemProperties) {
+        return new EntitlementManager(ctx, target, log, systemProperties);
     }
 }
diff --git a/services/core/java/com/android/server/display/ColorFade.java b/services/core/java/com/android/server/display/ColorFade.java
index 0400b56..f2c539c 100644
--- a/services/core/java/com/android/server/display/ColorFade.java
+++ b/services/core/java/com/android/server/display/ColorFade.java
@@ -575,7 +575,7 @@
                 final SurfaceControl.Builder builder =
                         new SurfaceControl.Builder(mSurfaceSession).setName("ColorFade");
                 if (mMode == MODE_FADE) {
-                    builder.setColorLayer();
+                    builder.setColorLayer(true);
                 } else {
                     builder.setBufferSize(mDisplayWidth, mDisplayHeight);
                 }
diff --git a/services/core/java/com/android/server/display/DisplayManagerService.java b/services/core/java/com/android/server/display/DisplayManagerService.java
index c9df86e..80ea1da 100644
--- a/services/core/java/com/android/server/display/DisplayManagerService.java
+++ b/services/core/java/com/android/server/display/DisplayManagerService.java
@@ -2196,6 +2196,22 @@
             }
         }
 
+        void setDisplayWhiteBalanceLoggingEnabled(boolean enabled) {
+            if (mDisplayPowerController != null) {
+                synchronized (mSyncRoot) {
+                    mDisplayPowerController.setDisplayWhiteBalanceLoggingEnabled(enabled);
+                }
+            }
+        }
+
+        void setAmbientColorTemperatureOverride(float cct) {
+            if (mDisplayPowerController != null) {
+                synchronized (mSyncRoot) {
+                    mDisplayPowerController.setAmbientColorTemperatureOverride(cct);
+                }
+            }
+        }
+
         private boolean validatePackageName(int uid, String packageName) {
             if (packageName != null) {
                 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
diff --git a/services/core/java/com/android/server/display/DisplayManagerShellCommand.java b/services/core/java/com/android/server/display/DisplayManagerShellCommand.java
index abbfc7b..04d28ea 100644
--- a/services/core/java/com/android/server/display/DisplayManagerShellCommand.java
+++ b/services/core/java/com/android/server/display/DisplayManagerShellCommand.java
@@ -45,6 +45,12 @@
                 return setAutoBrightnessLoggingEnabled(true);
             case "ab-logging-disable":
                 return setAutoBrightnessLoggingEnabled(false);
+            case "dwb-logging-enable":
+                return setDisplayWhiteBalanceLoggingEnabled(true);
+            case "dwb-logging-disable":
+                return setDisplayWhiteBalanceLoggingEnabled(false);
+            case "dwb-set-cct":
+                return setAmbientColorTemperatureOverride();
             default:
                 return handleDefaultCommands(cmd);
         }
@@ -65,6 +71,12 @@
         pw.println("    Enable auto-brightness logging.");
         pw.println("  ab-logging-disable");
         pw.println("    Disable auto-brightness logging.");
+        pw.println("  dwb-logging-enable");
+        pw.println("    Enable display white-balance logging.");
+        pw.println("  dwb-logging-disable");
+        pw.println("    Disable display white-balance logging.");
+        pw.println("  dwb-set-cct CCT");
+        pw.println("    Sets the ambient color temperature override to CCT (use -1 to disable).");
         pw.println();
         Intent.printIntentArgsHelp(pw , "");
     }
@@ -75,7 +87,7 @@
             getErrPrintWriter().println("Error: no brightness specified");
             return 1;
         }
-        float brightness = -1;
+        float brightness = -1.0f;
         try {
             brightness = Float.parseFloat(brightnessText);
         } catch (NumberFormatException e) {
@@ -84,7 +96,7 @@
             getErrPrintWriter().println("Error: brightness should be a number between 0 and 1");
             return 1;
         }
-        mService.setBrightness((int) brightness * 255);
+        mService.setBrightness((int) (brightness * 255));
         return 0;
     }
 
@@ -97,4 +109,26 @@
         mService.setAutoBrightnessLoggingEnabled(enabled);
         return 0;
     }
+
+    private int setDisplayWhiteBalanceLoggingEnabled(boolean enabled) {
+        mService.setDisplayWhiteBalanceLoggingEnabled(enabled);
+        return 0;
+    }
+
+    private int setAmbientColorTemperatureOverride() {
+        String cctText = getNextArg();
+        if (cctText == null) {
+            getErrPrintWriter().println("Error: no cct specified");
+            return 1;
+        }
+        float cct;
+        try {
+            cct = Float.parseFloat(cctText);
+        } catch (NumberFormatException e) {
+            getErrPrintWriter().println("Error: cct should be a number");
+            return 1;
+        }
+        mService.setAmbientColorTemperatureOverride(cct);
+        return 0;
+    }
 }
diff --git a/services/core/java/com/android/server/display/DisplayPowerController.java b/services/core/java/com/android/server/display/DisplayPowerController.java
index db3928e..dc5be6a 100644
--- a/services/core/java/com/android/server/display/DisplayPowerController.java
+++ b/services/core/java/com/android/server/display/DisplayPowerController.java
@@ -52,6 +52,9 @@
 import com.android.internal.app.IBatteryStats;
 import com.android.server.LocalServices;
 import com.android.server.am.BatteryStatsService;
+import com.android.server.display.whitebalance.DisplayWhiteBalanceController;
+import com.android.server.display.whitebalance.DisplayWhiteBalanceFactory;
+import com.android.server.display.whitebalance.DisplayWhiteBalanceSettings;
 import com.android.server.policy.WindowManagerPolicy;
 
 import java.io.PrintWriter;
@@ -78,7 +81,8 @@
  * For debugging, you can make the color fade and brightness animations run
  * slower by changing the "animator duration scale" option in Development Settings.
  */
-final class DisplayPowerController implements AutomaticBrightnessController.Callbacks {
+final class DisplayPowerController implements AutomaticBrightnessController.Callbacks,
+        DisplayWhiteBalanceController.Callbacks {
     private static final String TAG = "DisplayPowerController";
     private static final String SCREEN_ON_BLOCKED_TRACE_NAME = "Screen on blocked";
     private static final String SCREEN_OFF_BLOCKED_TRACE_NAME = "Screen off blocked";
@@ -307,6 +311,12 @@
     // Whether or not to skip the initial brightness ramps into STATE_ON.
     private final boolean mSkipScreenOnBrightnessRamp;
 
+    // Display white balance components.
+    @Nullable
+    private final DisplayWhiteBalanceSettings mDisplayWhiteBalanceSettings;
+    @Nullable
+    private final DisplayWhiteBalanceController mDisplayWhiteBalanceController;
+
     // A record of state for skipping brightness ramps.
     private int mSkipRampState = RAMP_STATE_SKIP_NONE;
 
@@ -504,6 +514,20 @@
         mPendingScreenBrightnessSetting = -1;
         mTemporaryAutoBrightnessAdjustment = Float.NaN;
         mPendingAutoBrightnessAdjustment = Float.NaN;
+
+        DisplayWhiteBalanceSettings displayWhiteBalanceSettings = null;
+        DisplayWhiteBalanceController displayWhiteBalanceController = null;
+        try {
+            displayWhiteBalanceSettings = new DisplayWhiteBalanceSettings(mContext, mHandler);
+            displayWhiteBalanceController = DisplayWhiteBalanceFactory.create(mHandler,
+                    mSensorManager, resources);
+            displayWhiteBalanceSettings.setCallbacks(this);
+            displayWhiteBalanceController.setCallbacks(this);
+        } catch (Exception e) {
+            Slog.e(TAG, "failed to set up display white-balance: " + e);
+        }
+        mDisplayWhiteBalanceSettings = displayWhiteBalanceSettings;
+        mDisplayWhiteBalanceController = displayWhiteBalanceController;
     }
 
     /**
@@ -526,6 +550,9 @@
     public void onSwitchUser(@UserIdInt int newUserId) {
         handleSettingsChange(true /* userSwitch */);
         mBrightnessTracker.onSwitchUser(newUserId);
+        if (mDisplayWhiteBalanceSettings != null) {
+            mDisplayWhiteBalanceSettings.onSwitchUser();
+        }
     }
 
     public ParceledListSlice<AmbientBrightnessDayStats> getAmbientBrightnessStats(
@@ -592,6 +619,9 @@
     }
 
     public BrightnessConfiguration getDefaultBrightnessConfiguration() {
+        if (mAutomaticBrightnessController == null) {
+            return null;
+        }
         return mAutomaticBrightnessController.getDefaultConfig();
     }
 
@@ -985,6 +1015,16 @@
 
         }
 
+        // Update display white-balance.
+        if (mDisplayWhiteBalanceController != null) {
+            if (state == Display.STATE_ON && mDisplayWhiteBalanceSettings.isEnabled()) {
+                mDisplayWhiteBalanceController.setEnabled(true);
+                mDisplayWhiteBalanceController.updateScreenColorTemperature();
+            } else {
+                mDisplayWhiteBalanceController.setEnabled(false);
+            }
+        }
+
         // Determine whether the display is ready for use in the newly requested state.
         // Note that we do not wait for the brightness ramp animation to complete before
         // reporting the display is ready because we only need to ensure the screen is in the
@@ -1699,6 +1739,12 @@
             pw.println();
             mBrightnessTracker.dump(pw);
         }
+
+        pw.println();
+        if (mDisplayWhiteBalanceController != null) {
+            mDisplayWhiteBalanceController.dump(pw);
+            mDisplayWhiteBalanceSettings.dump(pw);
+        }
     }
 
     private static String proximityToString(int state) {
@@ -1845,4 +1891,26 @@
             mAutomaticBrightnessController.setLoggingEnabled(enabled);
         }
     }
+
+    @Override // DisplayWhiteBalanceController.Callbacks
+    public void updateWhiteBalance() {
+        sendUpdatePowerState();
+    }
+
+    void setDisplayWhiteBalanceLoggingEnabled(boolean enabled) {
+        if (mDisplayWhiteBalanceController != null) {
+            mDisplayWhiteBalanceController.setLoggingEnabled(enabled);
+            mDisplayWhiteBalanceSettings.setLoggingEnabled(enabled);
+        }
+    }
+
+    void setAmbientColorTemperatureOverride(float cct) {
+        if (mDisplayWhiteBalanceController != null) {
+            mDisplayWhiteBalanceController.setAmbientColorTemperatureOverride(cct);
+            // The ambient color temperature override is only applied when the ambient color
+            // temperature changes or is updated, so it doesn't necessarily change the screen color
+            // temperature immediately. So, let's make it!
+            sendUpdatePowerState();
+        }
+    }
 }
diff --git a/services/core/java/com/android/server/display/utils/History.java b/services/core/java/com/android/server/display/utils/History.java
new file mode 100644
index 0000000..ed171b8
--- /dev/null
+++ b/services/core/java/com/android/server/display/utils/History.java
@@ -0,0 +1,101 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.display.utils;
+
+import java.time.Clock;
+
+/**
+ * A fixed-size buffer that keeps the most recent values and their times.
+ *
+ * This class is used for logging and debugging purposes only, so there's no way to retrieve the
+ * history other than toString(), and a non-monotonic clock is good enough.
+ */
+public class History {
+
+    private int mSize;
+    private int mCount;
+    private int mStart;
+    private int mEnd;
+
+    private long[] mTimes;
+    private float[] mValues;
+
+    private Clock mClock;
+
+    /**
+     * @param size
+     *      The maximum number of values kept.
+     */
+    public History(int size) {
+        this(size, Clock.systemUTC());
+    }
+
+    /**
+     * @param size
+     *    The maximum number of values kept.
+     * @param clock
+     *    The clock used.
+     */
+    public History(int size, Clock clock) {
+        mSize = size;
+        mCount = 0;
+        mStart = 0;
+        mEnd = 0;
+        mTimes = new long[size];
+        mValues = new float[size];
+        mClock = clock;
+    }
+
+    /**
+     * Add a value.
+     *
+     * @param value
+     *      The value.
+     */
+    public void add(float value) {
+        mTimes[mEnd] = mClock.millis();
+        mValues[mEnd] = value;
+        if (mCount < mSize) {
+            mCount++;
+        } else {
+            mStart = (mStart + 1) % mSize;
+        }
+        mEnd = (mEnd + 1) % mSize;
+    }
+
+    /**
+     * Convert the buffer to string.
+     *
+     * @return The buffer as string.
+     */
+    public String toString() {
+        StringBuffer sb = new StringBuffer();
+        sb.append("[");
+        for (int i = 0; i < mCount; i++) {
+            final int index = (mStart + i) % mSize;
+            final long time = mTimes[index];
+            final float value = mValues[index];
+            sb.append(value + " @ " + time);
+            if (i + 1 != mCount) {
+                sb.append(", ");
+            }
+        }
+        sb.append("]");
+        return sb.toString();
+    }
+
+}
diff --git a/services/core/java/com/android/server/display/utils/RollingBuffer.java b/services/core/java/com/android/server/display/utils/RollingBuffer.java
new file mode 100644
index 0000000..dd5b7ab
--- /dev/null
+++ b/services/core/java/com/android/server/display/utils/RollingBuffer.java
@@ -0,0 +1,184 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.display.utils;
+
+/**
+ * A buffer that supports adding new values and truncating old ones.
+ */
+public class RollingBuffer {
+
+    private static final int INITIAL_SIZE = 50;
+
+    private int mSize;
+    private int mCount;
+    private int mStart;
+    private int mEnd;
+
+    private long[] mTimes; // Milliseconds
+    private float[] mValues;
+
+    public RollingBuffer() {
+        mSize = INITIAL_SIZE;
+        mTimes = new long[INITIAL_SIZE];
+        mValues = new float[INITIAL_SIZE];
+        clear();
+    }
+
+    /**
+     * Add a value at a given time.
+     *
+     * @param time
+     *      The time (in milliseconds).
+     * @param value
+     *      The value.
+     */
+    public void add(long time, float value) {
+        if (mCount >= mSize) {
+            expandBuffer();
+        }
+        mTimes[mEnd] = time;
+        mValues[mEnd] = value;
+        mEnd = (mEnd + 1) % mSize;
+        mCount++;
+    }
+
+    /**
+     * Get the size of the buffer.
+     *
+     * @return The size of the buffer.
+     */
+    public int size() {
+        return mCount;
+    }
+
+    /**
+     * Return whether the buffer is empty or not.
+     *
+     * @return Whether the buffer is empty or not.
+     */
+    public boolean isEmpty() {
+        return size() == 0;
+    }
+
+    /**
+     * Get a time.
+     *
+     * @param index
+     *      The index of the time.
+     *
+     * @return The time.
+     */
+    public long getTime(int index) {
+        return mTimes[offsetOf(index)];
+    }
+
+    /**
+     * Get a value.
+     *
+     * @param index
+     *      The index of the value.
+     *
+     * @return The value.
+     */
+    public float getValue(int index) {
+        return mValues[offsetOf(index)];
+    }
+
+    /**
+     * Truncate old values.
+     *
+     * @param minTime
+     *      The minimum time (all values older than this time are truncated).
+     */
+    public void truncate(long minTime) {
+        if (isEmpty() || getTime(0) >= minTime) {
+            return;
+        }
+        final int index = getLatestIndexBefore(minTime);
+        mStart = offsetOf(index);
+        mCount -= index;
+        // Remove everything that happened before mTimes[index], but set the index-th value time to
+        // minTime rather than dropping it, as that would've been the value between the minTime and
+        // mTimes[index+1].
+        //
+        // -*---*---|---*---*- => xxxxxxxxx|*--*---*- rather than xxxxxxxxx|???*---*-
+        //      ^       ^                   ^  ^                               ^
+        //      i      i+1                  i i+1                             i+1
+        mTimes[mStart] = minTime;
+    }
+
+    /**
+     * Clears the buffer.
+     */
+    public void clear() {
+        mCount = 0;
+        mStart = 0;
+        mEnd = 0;
+    }
+
+    /**
+     * Convert the buffer to string.
+     *
+     * @return The buffer as string.
+     */
+    public String toString() {
+        StringBuffer sb = new StringBuffer();
+        sb.append("[");
+        for (int i = 0; i < mCount; i++) {
+            final int index = offsetOf(i);
+            sb.append(mValues[index] + " @ " + mTimes[index]);
+            if (i + 1 != mCount) {
+                sb.append(", ");
+            }
+        }
+        sb.append("]");
+        return sb.toString();
+    }
+
+    private int offsetOf(int index) {
+        if (index < 0 || index >= mCount) {
+            throw new ArrayIndexOutOfBoundsException("invalid index: " + index + ", mCount= "
+                    + mCount);
+        }
+        return (mStart + index) % mSize;
+    }
+
+    private void expandBuffer() {
+        final int size = mSize * 2;
+        long[] times = new long[size];
+        float[] values = new float[size];
+        System.arraycopy(mTimes, mStart, times, 0, mCount - mStart);
+        System.arraycopy(mTimes, 0, times, mCount - mStart, mStart);
+        System.arraycopy(mValues, mStart, values, 0, mCount - mStart);
+        System.arraycopy(mValues, 0, values, mCount - mStart, mStart);
+        mSize = size;
+        mStart = 0;
+        mEnd = mCount;
+        mTimes = times;
+        mValues = values;
+    }
+
+    private int getLatestIndexBefore(long time) {
+        for (int i = 1; i < mCount; i++) {
+            if (mTimes[offsetOf(i)] > time) {
+                return i - 1;
+            }
+        }
+        return mCount - 1;
+    }
+
+}
diff --git a/services/core/java/com/android/server/display/whitebalance/AmbientFilter.java b/services/core/java/com/android/server/display/whitebalance/AmbientFilter.java
new file mode 100644
index 0000000..532bbed
--- /dev/null
+++ b/services/core/java/com/android/server/display/whitebalance/AmbientFilter.java
@@ -0,0 +1,256 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.display.whitebalance;
+
+import android.util.Slog;
+
+import com.android.server.display.utils.RollingBuffer;
+
+import java.io.PrintWriter;
+import java.util.Arrays;
+
+/**
+ * The DisplayWhiteBalanceController uses the AmbientFilter to average ambient changes over time,
+ * filter out the noise, and arrive at an estimate of the actual value.
+ *
+ * When the DisplayWhiteBalanceController detects a change in ambient brightness or color
+ * temperature, it passes it to the AmbientFilter, and when it needs the actual ambient value, it
+ * asks it for an estimate.
+ *
+ * Implementations:
+ * - {@link WeightedMovingAverageAmbientFilter}
+ *   A weighted average prioritising recent changes.
+ */
+abstract class AmbientFilter {
+
+    protected static final boolean DEBUG = false; // Enable for verbose logs.
+
+    protected final String mTag;
+    protected boolean mLoggingEnabled;
+
+    // How long ambient value changes are kept and taken into consideration.
+    private final int mHorizon; // Milliseconds
+
+    private final RollingBuffer mBuffer;
+
+    /**
+     * @param tag
+     *      The tag used for dumping and logging.
+     * @param horizon
+     *      How long ambient value changes are kept and taken into consideration.
+     *
+     * @throws IllegalArgumentException
+     *      - horizon is not positive.
+     */
+    AmbientFilter(String tag, int horizon) {
+        validateArguments(horizon);
+        mTag = tag;
+        mLoggingEnabled = false;
+        mHorizon = horizon;
+        mBuffer = new RollingBuffer();
+    }
+
+    /**
+     * Add an ambient value change.
+     *
+     * @param time
+     *      The time.
+     * @param value
+     *      The ambient value.
+     *
+     * @return Whether the method succeeded or not.
+     */
+    public boolean addValue(long time, float value) {
+        if (value < 0.0f) {
+            return false;
+        }
+        truncateOldValues(time);
+        if (mLoggingEnabled) {
+            Slog.d(mTag, "add value: " + value + " @ " + time);
+        }
+        mBuffer.add(time, value);
+        return true;
+    }
+
+    /**
+     * Get an estimate of the actual ambient color temperature.
+     *
+     * @param time
+     *      The time.
+     *
+     * @return An estimate of the actual ambient color temperature.
+     */
+    public float getEstimate(long time) {
+        truncateOldValues(time);
+        final float value = filter(time, mBuffer);
+        if (mLoggingEnabled) {
+            Slog.d(mTag, "get estimate: " + value + " @ " + time);
+        }
+        return value;
+    }
+
+    /**
+     * Clears the filter state.
+     */
+    public void clear() {
+        mBuffer.clear();
+    }
+
+    /**
+     * Enable/disable logging.
+     *
+     * @param loggingEnabled
+     *      Whether logging is on/off.
+     *
+     * @return Whether the method succeeded or not.
+     */
+    public boolean setLoggingEnabled(boolean loggingEnabled) {
+        if (mLoggingEnabled == loggingEnabled) {
+            return false;
+        }
+        mLoggingEnabled = loggingEnabled;
+        return true;
+    }
+
+    /**
+     * Dump the state.
+     *
+     * @param writer
+     *      The PrintWriter used to dump the state.
+     */
+    public void dump(PrintWriter writer) {
+        writer.println("  " + mTag);
+        writer.println("    mLoggingEnabled=" + mLoggingEnabled);
+        writer.println("    mHorizon=" + mHorizon);
+        writer.println("    mBuffer=" + mBuffer);
+    }
+
+    private void validateArguments(int horizon) {
+        if (horizon <= 0) {
+            throw new IllegalArgumentException("horizon must be positive");
+        }
+    }
+
+    private void truncateOldValues(long time) {
+        final long minTime = time - mHorizon;
+        mBuffer.truncate(minTime);
+    }
+
+    protected abstract float filter(long time, RollingBuffer buffer);
+
+    /**
+     * A weighted average prioritising recent changes.
+     */
+    static class WeightedMovingAverageAmbientFilter extends AmbientFilter {
+
+        // How long the latest ambient value change is predicted to last.
+        private static final int PREDICTION_TIME = 100; // Milliseconds
+
+        // Recent changes are prioritised by integrating their duration over y = x + mIntercept
+        // (the higher it is, the less prioritised recent changes are).
+        private final float mIntercept;
+
+        /**
+         * @param tag
+         *      The tag used for dumping and logging.
+         * @param horizon
+         *      How long ambient value changes are kept and taken into consideration.
+         * @param intercept
+         *      Recent changes are prioritised by integrating their duration over y = x + intercept
+         *      (the higher it is, the less prioritised recent changes are).
+         *
+         * @throws IllegalArgumentException
+         *      - horizon is not positive.
+         *      - intercept is NaN or negative.
+         */
+        WeightedMovingAverageAmbientFilter(String tag, int horizon, float intercept) {
+            super(tag, horizon);
+            validateArguments(intercept);
+            mIntercept = intercept;
+        }
+
+        /**
+         * See {@link AmbientFilter#dump base class}.
+         */
+        @Override
+        public void dump(PrintWriter writer) {
+            super.dump(writer);
+            writer.println("    mIntercept=" + mIntercept);
+        }
+
+        // Normalise the times to [t1=0, t2, ..., tN, now + PREDICTION_TIME], so the first change
+        // starts at 0 and the last change is predicted to last a bit, and divide them by 1000 as
+        // milliseconds are high enough to overflow.
+        // The weight of the value from t[i] to t[i+1] is the area under (A.K.A. the integral of)
+        // y = x + mIntercept from t[i] to t[i+1].
+        @Override
+        protected float filter(long time, RollingBuffer buffer) {
+            if (buffer.isEmpty()) {
+                return -1.0f;
+            }
+            float total = 0.0f;
+            float totalWeight = 0.0f;
+            final float[] weights = getWeights(time, buffer);
+            if (DEBUG && mLoggingEnabled) {
+                Slog.v(mTag, "filter: " + buffer + " => " + Arrays.toString(weights));
+            }
+            for (int i = 0; i < weights.length; i++) {
+                final float value = buffer.getValue(i);
+                final float weight = weights[i];
+                total += weight * value;
+                totalWeight += weight;
+            }
+            if (totalWeight == 0.0f) {
+                return buffer.getValue(buffer.size() - 1);
+            }
+            return total / totalWeight;
+        }
+
+        private void validateArguments(float intercept) {
+            if (Float.isNaN(intercept) || intercept < 0.0f) {
+                throw new IllegalArgumentException("intercept must be a non-negative number");
+            }
+        }
+
+        private float[] getWeights(long time, RollingBuffer buffer) {
+            float[] weights = new float[buffer.size()];
+            final long startTime = buffer.getTime(0);
+            float previousTime = 0.0f;
+            for (int i = 1; i < weights.length; i++) {
+                final float currentTime = (buffer.getTime(i) - startTime) / 1000.0f;
+                final float weight = calculateIntegral(previousTime, currentTime);
+                weights[i - 1] = weight;
+                previousTime = currentTime;
+            }
+            final float lastTime = (time + PREDICTION_TIME - startTime) / 1000.0f;
+            final float lastWeight = calculateIntegral(previousTime, lastTime);
+            weights[weights.length - 1] = lastWeight;
+            return weights;
+        }
+
+        private float calculateIntegral(float from, float to) {
+            return antiderivative(to) - antiderivative(from);
+        }
+
+        private float antiderivative(float x) {
+            // f(x) = x + c => F(x) = 1/2 * x^2 + c * x
+            return 0.5f * x * x + mIntercept * x;
+        }
+
+    }
+
+}
diff --git a/services/core/java/com/android/server/display/whitebalance/AmbientSensor.java b/services/core/java/com/android/server/display/whitebalance/AmbientSensor.java
new file mode 100644
index 0000000..1707a62
--- /dev/null
+++ b/services/core/java/com/android/server/display/whitebalance/AmbientSensor.java
@@ -0,0 +1,381 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.display.whitebalance;
+
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.hardware.Sensor;
+import android.hardware.SensorEvent;
+import android.hardware.SensorEventListener;
+import android.hardware.SensorManager;
+import android.os.Handler;
+import android.util.Slog;
+
+import com.android.internal.util.Preconditions;
+import com.android.server.display.utils.History;
+
+import java.io.PrintWriter;
+
+/**
+ * The DisplayWhiteBalanceController uses the AmbientSensor to detect changes in the ambient
+ * brightness and color temperature.
+ *
+ * The AmbientSensor listens on an actual sensor, derives the ambient brightness or color
+ * temperature from its events, and calls back into the DisplayWhiteBalanceController to report it.
+ */
+abstract class AmbientSensor {
+
+    protected String mTag;
+    protected boolean mLoggingEnabled;
+
+    private final Handler mHandler;
+
+    protected final SensorManager mSensorManager;
+    protected Sensor mSensor;
+
+    private boolean mEnabled;
+
+    private int mRate; // Milliseconds
+
+    // The total events count and the most recent events are kept for debugging purposes.
+    private int mEventsCount;
+    private static final int HISTORY_SIZE = 50;
+    private History mEventsHistory;
+
+    /**
+     * @param tag
+     *      The tag used for dumping and logging.
+     * @param handler
+     *      The handler used to determine which thread to run on.
+     * @param sensorManager
+     *      The sensor manager used to acquire necessary sensors.
+     * @param rate
+     *      The sensor rate.
+     *
+     * @throws IllegalArgumentException
+     *      - rate is not positive.
+     * @throws NullPointerException
+     *      - handler is null;
+     *      - sensorManager is null.
+     * @throws IllegalStateException
+     *      - Cannot find the necessary sensor.
+     */
+    AmbientSensor(String tag, @NonNull Handler handler, @NonNull SensorManager sensorManager,
+            int rate) {
+        validateArguments(handler, sensorManager, rate);
+        mTag = tag;
+        mLoggingEnabled = false;
+        mHandler = handler;
+        mSensorManager = sensorManager;
+        mEnabled = false;
+        mRate = rate;
+        mEventsCount = 0;
+        mEventsHistory = new History(HISTORY_SIZE);
+    }
+
+    /**
+     * Enable/disable the sensor.
+     *
+     * @param enabled
+     *      Whether the sensor should be on/off.
+     *
+     * @return Whether the method succeeded or not.
+     */
+    public boolean setEnabled(boolean enabled) {
+        if (enabled) {
+            return enable();
+        } else {
+            return disable();
+        }
+    }
+
+    /**
+     * Enable/disable logging.
+     *
+     * @param loggingEnabled
+     *      Whether logging should be on/off.
+     *
+     * @return Whether the method succeeded or not.
+     */
+    public boolean setLoggingEnabled(boolean loggingEnabled) {
+        if (mLoggingEnabled == loggingEnabled) {
+            return false;
+        }
+        mLoggingEnabled = loggingEnabled;
+        return true;
+    }
+
+    /**
+     * Dump the state.
+     *
+     * @param writer
+     *      The PrintWriter used to dump the state.
+     */
+    public void dump(PrintWriter writer) {
+        writer.println("  " + mTag);
+        writer.println("    mLoggingEnabled=" + mLoggingEnabled);
+        writer.println("    mHandler=" + mHandler);
+        writer.println("    mSensorManager=" + mSensorManager);
+        writer.println("    mSensor=" + mSensor);
+        writer.println("    mEnabled=" + mEnabled);
+        writer.println("    mRate=" + mRate);
+        writer.println("    mEventsCount=" + mEventsCount);
+        writer.println("    mEventsHistory=" + mEventsHistory);
+    }
+
+
+    private static void validateArguments(Handler handler, SensorManager sensorManager, int rate) {
+        Preconditions.checkNotNull(handler, "handler cannot be null");
+        Preconditions.checkNotNull(sensorManager, "sensorManager cannot be null");
+        if (rate <= 0) {
+            throw new IllegalArgumentException("rate must be positive");
+        }
+    }
+
+    protected abstract void update(float value);
+
+    private boolean enable() {
+        if (mEnabled) {
+            return false;
+        }
+        if (mLoggingEnabled) {
+            Slog.d(mTag, "enabling");
+        }
+        mEnabled = true;
+        startListening();
+        return true;
+    }
+
+    private boolean disable() {
+        if (!mEnabled) {
+            return false;
+        }
+        if (mLoggingEnabled) {
+            Slog.d(mTag, "disabling");
+        }
+        mEnabled = false;
+        mEventsCount = 0;
+        stopListening();
+        return true;
+    }
+
+    private void startListening() {
+        if (mSensorManager == null) {
+            return;
+        }
+        mSensorManager.registerListener(mListener, mSensor, mRate * 1000, mHandler);
+    }
+
+    private void stopListening() {
+        if (mSensorManager == null) {
+            return;
+        }
+        mSensorManager.unregisterListener(mListener);
+    }
+
+    private void handleNewEvent(float value) {
+        // This shouldn't really happen, except for the race condition where the sensor is disabled
+        // with an event already in the handler queue, in which case we discard that event.
+        if (!mEnabled) {
+            return;
+        }
+        if (mLoggingEnabled) {
+            Slog.d(mTag, "handle new event: " + value);
+        }
+        mEventsCount++;
+        mEventsHistory.add(value);
+        update(value);
+    }
+
+    private SensorEventListener mListener = new SensorEventListener() {
+
+        @Override
+        public void onSensorChanged(SensorEvent event) {
+            final float value = event.values[0];
+            handleNewEvent(value);
+        }
+
+        @Override
+        public void onAccuracyChanged(Sensor sensor, int accuracy) {
+            // Not used.
+        }
+
+    };
+
+    /**
+     * A sensor that reports the ambient brightness.
+     */
+    static class AmbientBrightnessSensor extends AmbientSensor {
+
+        private static final String TAG = "AmbientBrightnessSensor";
+
+        // To decouple the DisplayWhiteBalanceController from the AmbientBrightnessSensor, the
+        // DWBC implements Callbacks and passes itself to the ABS so it can call back into it
+        // without knowing about it.
+        @Nullable
+        private Callbacks mCallbacks;
+
+        /**
+         * @param handler
+         *      The handler used to determine which thread to run on.
+         * @param sensorManager
+         *      The sensor manager used to acquire necessary sensors.
+         * @param rate
+         *      The sensor rate.
+         *
+         * @throws IllegalArgumentException
+         *      - rate is not positive.
+         * @throws NullPointerException
+         *      - handler is null;
+         *      - sensorManager is null.
+         * @throws IllegalStateException
+         *      - Cannot find the light sensor.
+         */
+        AmbientBrightnessSensor(@NonNull Handler handler, @NonNull SensorManager sensorManager,
+                int rate) {
+            super(TAG, handler, sensorManager, rate);
+            mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
+            if (mSensor == null) {
+                throw new IllegalStateException("cannot find light sensor");
+            }
+            mCallbacks = null;
+        }
+
+        /**
+         * Set an object to call back to when the ambient brightness changes.
+         *
+         * @param callbacks
+         *      The object to call back to.
+         *
+         * @return Whether the method succeeded or not.
+         */
+        public boolean setCallbacks(Callbacks callbacks) {
+            if (mCallbacks == callbacks) {
+                return false;
+            }
+            mCallbacks = callbacks;
+            return true;
+        }
+
+        /**
+         * See {@link AmbientSensor#dump base class}.
+         */
+        @Override
+        public void dump(PrintWriter writer) {
+            super.dump(writer);
+            writer.println("    mCallbacks=" + mCallbacks);
+        }
+
+        interface Callbacks {
+            void onAmbientBrightnessChanged(float value);
+        }
+
+        @Override
+        protected void update(float value) {
+            if (mCallbacks != null) {
+                mCallbacks.onAmbientBrightnessChanged(value);
+            }
+        }
+
+    }
+
+    /**
+     * A sensor that reports the ambient color temperature.
+     */
+    static class AmbientColorTemperatureSensor extends AmbientSensor {
+
+        private static final String TAG = "AmbientColorTemperatureSensor";
+
+        // To decouple the DisplayWhiteBalanceController from the
+        // AmbientColorTemperatureSensor, the DWBC implements Callbacks and passes itself to the
+        // ACTS so it can call back into it without knowing about it.
+        @Nullable
+        private Callbacks mCallbacks;
+
+        /**
+         * @param handler
+         *      The handler used to determine which thread to run on.
+         * @param sensorManager
+         *      The sensor manager used to acquire necessary sensors.
+         * @param name
+         *      The color sensor name.
+         * @param rate
+         *      The sensor rate.
+         *
+         * @throws IllegalArgumentException
+         *      - rate is not positive.
+         * @throws NullPointerException
+         *      - handler is null;
+         *      - sensorManager is null.
+         * @throws IllegalStateException
+         *      - Cannot find the color sensor.
+         */
+        AmbientColorTemperatureSensor(@NonNull Handler handler,
+                @NonNull SensorManager sensorManager, String name, int rate) {
+            super(TAG, handler, sensorManager, rate);
+            mSensor = null;
+            for (Sensor sensor : mSensorManager.getSensorList(Sensor.TYPE_ALL)) {
+                if (sensor.getStringType().equals(name)) {
+                    mSensor = sensor;
+                    break;
+                }
+            }
+            if (mSensor == null) {
+                throw new IllegalStateException("cannot find sensor " + name);
+            }
+            mCallbacks = null;
+        }
+
+        /**
+         * Set an object to call back to when the ambient color temperature changes.
+         *
+         * @param callbacks
+         *      The object to call back to.
+         *
+         * @return Whether the method succeeded or not.
+         */
+        public boolean setCallbacks(Callbacks callbacks) {
+            if (mCallbacks == callbacks) {
+                return false;
+            }
+            mCallbacks = callbacks;
+            return true;
+        }
+
+        /**
+         * See {@link AmbientSensor#dump base class}.
+         */
+        @Override
+        public void dump(PrintWriter writer) {
+            super.dump(writer);
+            writer.println("    mCallbacks=" + mCallbacks);
+        }
+
+        interface Callbacks {
+            void onAmbientColorTemperatureChanged(float value);
+        }
+
+        @Override
+        protected void update(float value) {
+            if (mCallbacks != null) {
+                mCallbacks.onAmbientColorTemperatureChanged(value);
+            }
+        }
+
+    }
+
+}
diff --git a/services/core/java/com/android/server/display/whitebalance/DisplayWhiteBalanceController.java b/services/core/java/com/android/server/display/whitebalance/DisplayWhiteBalanceController.java
new file mode 100644
index 0000000..7ae00af
--- /dev/null
+++ b/services/core/java/com/android/server/display/whitebalance/DisplayWhiteBalanceController.java
@@ -0,0 +1,385 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.display.whitebalance;
+
+import android.annotation.NonNull;
+import android.util.Slog;
+
+import com.android.internal.util.Preconditions;
+import com.android.server.LocalServices;
+import com.android.server.display.ColorDisplayService.ColorDisplayServiceInternal;
+import com.android.server.display.utils.History;
+
+import java.io.PrintWriter;
+
+/**
+ * The DisplayWhiteBalanceController drives display white-balance (automatically correcting the
+ * screen color temperature depending on the ambient color temperature).
+ *
+ * The DisplayWhiteBalanceController:
+ * - Uses the AmbientColorTemperatureSensor to detect changes in the ambient color temperature;
+ * - Uses the AmbientColorTemperatureFilter to average these changes over time, filter out the
+ *   noise, and arrive at an estimate of the actual ambient color temperature;
+ * - Uses the DisplayWhiteBalanceThrottler to decide whether the screen color tempearture should be
+ *   updated, suppressing changes that are too frequent or too minor.
+ */
+public class DisplayWhiteBalanceController implements
+        AmbientSensor.AmbientBrightnessSensor.Callbacks,
+        AmbientSensor.AmbientColorTemperatureSensor.Callbacks {
+
+    protected static final String TAG = "DisplayWhiteBalanceController";
+    protected boolean mLoggingEnabled;
+
+    private boolean mEnabled;
+
+    // To decouple the DisplayPowerController from the DisplayWhiteBalanceController, the DPC
+    // implements Callbacks and passes itself to the DWBC so it can call back into it without
+    // knowing about it.
+    private Callbacks mCallbacks;
+
+    private AmbientSensor.AmbientBrightnessSensor mBrightnessSensor;
+    private AmbientFilter mBrightnessFilter;
+    private AmbientSensor.AmbientColorTemperatureSensor mColorTemperatureSensor;
+    private AmbientFilter mColorTemperatureFilter;
+    private DisplayWhiteBalanceThrottler mThrottler;
+
+    // When the brightness drops below a certain threshold, it affects the color temperature
+    // accuracy, so we fall back to a fixed ambient color temperature.
+    private final float mLowLightAmbientBrightnessThreshold;
+    private final float mLowLightAmbientColorTemperature;
+
+    private float mAmbientColorTemperature;
+    private float mPendingAmbientColorTemperature;
+    private float mLastAmbientColorTemperature;
+
+    private ColorDisplayServiceInternal mColorDisplayServiceInternal;
+
+    // The most recent ambient color temperature values are kept for debugging purposes.
+    private static final int HISTORY_SIZE = 50;
+    private History mAmbientColorTemperatureHistory;
+
+    // Override the ambient color temperature for debugging purposes.
+    private float mAmbientColorTemperatureOverride;
+
+    /**
+     * @param brightnessSensor
+     *      The sensor used to detect changes in the ambient brightness.
+     * @param brightnessFilter
+     *      The filter used to avergae ambient brightness changes over time, filter out the noise
+     *      and arrive at an estimate of the actual ambient brightness.
+     * @param colorTemperatureSensor
+     *      The sensor used to detect changes in the ambient color temperature.
+     * @param colorTemperatureFilter
+     *      The filter used to average ambient color temperature changes over time, filter out the
+     *      noise and arrive at an estimate of the actual ambient color temperature.
+     * @param throttler
+     *      The throttler used to determine whether the new screen color temperature should be
+     *      updated or not.
+     * @param lowLightAmbientBrightnessThreshold
+     *      The ambient brightness threshold beneath which we fall back to a fixed ambient color
+     *      temperature.
+     * @param lowLightAmbientColorTemperature
+     *      The ambient color temperature to which we fall back when the ambient brightness drops
+     *      beneath a certain threshold.
+     *
+     * @throws NullPointerException
+     *      - brightnessSensor is null;
+     *      - brightnessFilter is null;
+     *      - colorTemperatureSensor is null;
+     *      - colorTemperatureFilter is null;
+     *      - throttler is null.
+     */
+    public DisplayWhiteBalanceController(
+            @NonNull AmbientSensor.AmbientBrightnessSensor brightnessSensor,
+            @NonNull AmbientFilter brightnessFilter,
+            @NonNull AmbientSensor.AmbientColorTemperatureSensor colorTemperatureSensor,
+            @NonNull AmbientFilter colorTemperatureFilter,
+            @NonNull DisplayWhiteBalanceThrottler throttler,
+            float lowLightAmbientBrightnessThreshold, float lowLightAmbientColorTemperature) {
+        validateArguments(brightnessSensor, brightnessFilter, colorTemperatureSensor,
+                colorTemperatureFilter, throttler);
+        mLoggingEnabled = false;
+        mEnabled = false;
+        mCallbacks = null;
+        mBrightnessSensor = brightnessSensor;
+        mBrightnessFilter = brightnessFilter;
+        mColorTemperatureSensor = colorTemperatureSensor;
+        mColorTemperatureFilter = colorTemperatureFilter;
+        mThrottler = throttler;
+        mLowLightAmbientBrightnessThreshold = lowLightAmbientBrightnessThreshold;
+        mLowLightAmbientColorTemperature = lowLightAmbientColorTemperature;
+        mAmbientColorTemperature = -1.0f;
+        mPendingAmbientColorTemperature = -1.0f;
+        mLastAmbientColorTemperature = -1.0f;
+        mAmbientColorTemperatureHistory = new History(HISTORY_SIZE);
+        mAmbientColorTemperatureOverride = -1.0f;
+        mColorDisplayServiceInternal = LocalServices.getService(ColorDisplayServiceInternal.class);
+    }
+
+    /**
+     * Enable/disable the controller.
+     *
+     * @param enabled
+     *      Whether the controller should be on/off.
+     *
+     * @return Whether the method succeeded or not.
+     */
+    public boolean setEnabled(boolean enabled) {
+        if (enabled) {
+            return enable();
+        } else {
+            return disable();
+        }
+    }
+
+    /**
+     * Set an object to call back to when the screen color temperature should be updated.
+     *
+     * @param callbacks
+     *      The object to call back to.
+     *
+     * @return Whether the method succeeded or not.
+     */
+    public boolean setCallbacks(Callbacks callbacks) {
+        if (mCallbacks == callbacks) {
+            return false;
+        }
+        mCallbacks = callbacks;
+        return true;
+    }
+
+    /**
+     * Enable/disable logging.
+     *
+     * @param loggingEnabled
+     *      Whether logging should be on/off.
+     *
+     * @return Whether the method succeeded or not.
+     */
+    public boolean setLoggingEnabled(boolean loggingEnabled) {
+        if (mLoggingEnabled == loggingEnabled) {
+            return false;
+        }
+        mLoggingEnabled = loggingEnabled;
+        mBrightnessSensor.setLoggingEnabled(loggingEnabled);
+        mBrightnessFilter.setLoggingEnabled(loggingEnabled);
+        mColorTemperatureSensor.setLoggingEnabled(loggingEnabled);
+        mColorTemperatureFilter.setLoggingEnabled(loggingEnabled);
+        mThrottler.setLoggingEnabled(loggingEnabled);
+        return true;
+    }
+
+    /**
+     * Set the ambient color temperature override.
+     *
+     * This is only applied when the ambient color temperature changes or is updated (in which case
+     * it overrides the ambient color temperature estimate); in other words, it doesn't necessarily
+     * change the screen color temperature immediately.
+     *
+     * @param ambientColorTemperatureOverride
+     *      The ambient color temperature override.
+     *
+     * @return Whether the method succeeded or not.
+     */
+    public boolean setAmbientColorTemperatureOverride(float ambientColorTemperatureOverride) {
+        if (mAmbientColorTemperatureOverride == ambientColorTemperatureOverride) {
+            return false;
+        }
+        mAmbientColorTemperatureOverride = ambientColorTemperatureOverride;
+        return true;
+    }
+
+    /**
+     * Dump the state.
+     *
+     * @param writer
+     *      The writer used to dump the state.
+     */
+    public void dump(PrintWriter writer) {
+        writer.println("DisplayWhiteBalanceController");
+        writer.println("  mLoggingEnabled=" + mLoggingEnabled);
+        writer.println("  mEnabled=" + mEnabled);
+        writer.println("  mCallbacks=" + mCallbacks);
+        mBrightnessSensor.dump(writer);
+        mBrightnessFilter.dump(writer);
+        mColorTemperatureSensor.dump(writer);
+        mColorTemperatureFilter.dump(writer);
+        mThrottler.dump(writer);
+        writer.println("  mLowLightAmbientBrightnessThreshold="
+                + mLowLightAmbientBrightnessThreshold);
+        writer.println("  mLowLightAmbientColorTemperature=" + mLowLightAmbientColorTemperature);
+        writer.println("  mAmbientColorTemperature=" + mAmbientColorTemperature);
+        writer.println("  mPendingAmbientColorTemperature=" + mPendingAmbientColorTemperature);
+        writer.println("  mLastAmbientColorTemperature=" + mLastAmbientColorTemperature);
+        writer.println("  mAmbientColorTemperatureHistory=" + mAmbientColorTemperatureHistory);
+        writer.println("  mAmbientColorTemperatureOverride=" + mAmbientColorTemperatureOverride);
+    }
+
+    @Override // AmbientSensor.AmbientBrightnessSensor.Callbacks
+    public void onAmbientBrightnessChanged(float value) {
+        final long time = System.currentTimeMillis();
+        mBrightnessFilter.addValue(time, value);
+        updateAmbientColorTemperature();
+    }
+
+    @Override // AmbientSensor.AmbientColorTemperatureSensor.Callbacks
+    public void onAmbientColorTemperatureChanged(float value) {
+        final long time = System.currentTimeMillis();
+        mColorTemperatureFilter.addValue(time, value);
+        updateAmbientColorTemperature();
+    }
+
+    /**
+     * Updates the ambient color temperature.
+     */
+    public void updateAmbientColorTemperature() {
+        final long time = System.currentTimeMillis();
+        float ambientColorTemperature = mColorTemperatureFilter.getEstimate(time);
+
+        final float ambientBrightness = mBrightnessFilter.getEstimate(time);
+        if (ambientBrightness < mLowLightAmbientBrightnessThreshold) {
+            if (mLoggingEnabled) {
+                Slog.d(TAG, "low light ambient brightness: " + ambientBrightness + " < "
+                        + mLowLightAmbientBrightnessThreshold
+                        + ", falling back to fixed ambient color temperature: "
+                        + ambientColorTemperature + " => " + mLowLightAmbientColorTemperature);
+            }
+            ambientColorTemperature = mLowLightAmbientColorTemperature;
+        }
+
+        if (mAmbientColorTemperatureOverride != -1.0f) {
+            if (mLoggingEnabled) {
+                Slog.d(TAG, "override ambient color temperature: " + ambientColorTemperature
+                        + " => " + mAmbientColorTemperatureOverride);
+            }
+            ambientColorTemperature = mAmbientColorTemperatureOverride;
+        }
+
+        // When the screen color temperature needs to be updated, we call DisplayPowerController to
+        // call our updateColorTemperature. The reason we don't call it directly is that we want
+        // all changes to the system to happen in a predictable order in DPC's main loop
+        // (updatePowerState).
+        if (ambientColorTemperature == -1.0f || mThrottler.throttle(ambientColorTemperature)) {
+            return;
+        }
+
+        if (mLoggingEnabled) {
+            Slog.d(TAG, "pending ambient color temperature: " + ambientColorTemperature);
+        }
+        mPendingAmbientColorTemperature = ambientColorTemperature;
+        if (mCallbacks != null) {
+            mCallbacks.updateWhiteBalance();
+        }
+    }
+
+    /**
+     * Updates the screen color temperature.
+     */
+    public void updateScreenColorTemperature() {
+        float ambientColorTemperature = -1.0f;
+
+        // If both the pending and the current ambient color temperatures are -1, it means the DWBC
+        // was just enabled, and we use the last ambient color temperature until new sensor events
+        // give us a better estimate.
+        if (mAmbientColorTemperature == -1.0f && mPendingAmbientColorTemperature == -1.0f) {
+            ambientColorTemperature = mLastAmbientColorTemperature;
+        }
+
+        // Otherwise, we use the pending ambient color temperature, but only if it's non-trivial
+        // and different than the current one.
+        if (mPendingAmbientColorTemperature != -1.0f
+                && mPendingAmbientColorTemperature != mAmbientColorTemperature) {
+            ambientColorTemperature = mPendingAmbientColorTemperature;
+        }
+
+        if (ambientColorTemperature == -1.0f) {
+            return;
+        }
+
+        mAmbientColorTemperature = ambientColorTemperature;
+        if (mLoggingEnabled) {
+            Slog.d(TAG, "ambient color temperature: " + mAmbientColorTemperature);
+        }
+        mPendingAmbientColorTemperature = -1.0f;
+        mAmbientColorTemperatureHistory.add(mAmbientColorTemperature);
+        mColorDisplayServiceInternal.setDisplayWhiteBalanceColorTemperature(
+                (int) mAmbientColorTemperature);
+        mLastAmbientColorTemperature = mAmbientColorTemperature;
+    }
+
+    /**
+     * The DisplayWhiteBalanceController decouples itself from its parent (DisplayPowerController)
+     * by providing this interface to implement (and a method to set its callbacks object), and
+     * calling these methods.
+     */
+    public interface Callbacks {
+
+        /**
+         * Called whenever the display white-balance state has changed.
+         *
+         * Usually, this means the estimated ambient color temperature has changed enough, and the
+         * screen color temperature should be updated; but it is also called by
+         */
+        void updateWhiteBalance();
+    }
+
+    private void validateArguments(AmbientSensor.AmbientBrightnessSensor brightnessSensor,
+            AmbientFilter brightnessFilter,
+            AmbientSensor.AmbientColorTemperatureSensor colorTemperatureSensor,
+            AmbientFilter colorTemperatureFilter,
+            DisplayWhiteBalanceThrottler throttler) {
+        Preconditions.checkNotNull(brightnessSensor, "brightnessSensor must not be null");
+        Preconditions.checkNotNull(brightnessFilter, "brightnessFilter must not be null");
+        Preconditions.checkNotNull(colorTemperatureSensor,
+                "colorTemperatureSensor must not be null");
+        Preconditions.checkNotNull(colorTemperatureFilter,
+                "colorTemperatureFilter must not be null");
+        Preconditions.checkNotNull(throttler, "throttler cannot be null");
+    }
+
+    private boolean enable() {
+        if (mEnabled) {
+            return false;
+        }
+        if (mLoggingEnabled) {
+            Slog.d(TAG, "enabling");
+        }
+        mEnabled = true;
+        mBrightnessSensor.setEnabled(true);
+        mColorTemperatureSensor.setEnabled(true);
+        return true;
+    }
+
+    private boolean disable() {
+        if (!mEnabled) {
+            return false;
+        }
+        if (mLoggingEnabled) {
+            Slog.d(TAG, "disabling");
+        }
+        mEnabled = false;
+        mBrightnessSensor.setEnabled(false);
+        mBrightnessFilter.clear();
+        mColorTemperatureSensor.setEnabled(false);
+        mColorTemperatureFilter.clear();
+        mThrottler.clear();
+        mAmbientColorTemperature = -1.0f;
+        mPendingAmbientColorTemperature = -1.0f;
+        return true;
+    }
+
+}
diff --git a/services/core/java/com/android/server/display/whitebalance/DisplayWhiteBalanceFactory.java b/services/core/java/com/android/server/display/whitebalance/DisplayWhiteBalanceFactory.java
new file mode 100644
index 0000000..fd78ddb
--- /dev/null
+++ b/services/core/java/com/android/server/display/whitebalance/DisplayWhiteBalanceFactory.java
@@ -0,0 +1,171 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.display.whitebalance;
+
+import android.content.res.Resources;
+import android.content.res.TypedArray;
+import android.hardware.SensorManager;
+import android.os.Handler;
+import android.util.TypedValue;
+
+/**
+ * The DisplayWhiteBalanceFactory creates and configures an DisplayWhiteBalanceController.
+ */
+public class DisplayWhiteBalanceFactory {
+
+    private static final String BRIGHTNESS_FILTER_TAG = "AmbientBrightnessFilter";
+    private static final String COLOR_TEMPERATURE_FILTER_TAG = "AmbientColorTemperatureFilter";
+
+    /**
+     * Create and configure an DisplayWhiteBalanceController.
+     *
+     * @param handler
+     *      The handler used to determine which thread to run on.
+     * @param sensorManager
+     *      The sensor manager used to acquire necessary sensors.
+     * @param resources
+     *      The resources used to configure the various components.
+     *
+     * @return An DisplayWhiteBalanceController.
+     *
+     * @throws NullPointerException
+     *      - handler is null;
+     *      - sensorManager is null.
+     * @throws Resources.NotFoundException
+     *      - Configurations are missing.
+     * @throws IllegalArgumentException
+     *      - Configurations are invalid.
+     * @throws IllegalStateException
+     *      - Cannot find the necessary sensors.
+     */
+    public static DisplayWhiteBalanceController create(Handler handler,
+            SensorManager sensorManager, Resources resources) {
+        final AmbientSensor.AmbientBrightnessSensor brightnessSensor =
+                createBrightnessSensor(handler, sensorManager, resources);
+        final AmbientFilter brightnessFilter = createBrightnessFilter(resources);
+        final AmbientSensor.AmbientColorTemperatureSensor colorTemperatureSensor =
+                createColorTemperatureSensor(handler, sensorManager, resources);
+        final AmbientFilter colorTemperatureFilter = createColorTemperatureFilter(resources);
+        final DisplayWhiteBalanceThrottler throttler = createThrottler(resources);
+        final float lowLightAmbientBrightnessThreshold = getFloat(resources,
+                com.android.internal.R.dimen
+                .config_displayWhiteBalanceLowLightAmbientBrightnessThreshold);
+        final float lowLightAmbientColorTemperature = getFloat(resources,
+                com.android.internal.R.dimen
+                .config_displayWhiteBalanceLowLightAmbientColorTemperature);
+        final DisplayWhiteBalanceController controller = new DisplayWhiteBalanceController(
+                brightnessSensor, brightnessFilter, colorTemperatureSensor, colorTemperatureFilter,
+                throttler, lowLightAmbientBrightnessThreshold, lowLightAmbientColorTemperature);
+        brightnessSensor.setCallbacks(controller);
+        colorTemperatureSensor.setCallbacks(controller);
+        return controller;
+    }
+
+    // Instantiation is disabled.
+    private DisplayWhiteBalanceFactory() { }
+
+    private static AmbientSensor.AmbientBrightnessSensor createBrightnessSensor(Handler handler,
+            SensorManager sensorManager, Resources resources) {
+        final int rate = resources.getInteger(
+                com.android.internal.R.integer.config_displayWhiteBalanceBrightnessSensorRate);
+        return new AmbientSensor.AmbientBrightnessSensor(handler, sensorManager, rate);
+    }
+
+    private static AmbientFilter createBrightnessFilter(Resources resources) {
+        final int horizon = resources.getInteger(
+                com.android.internal.R.integer.config_displayWhiteBalanceBrightnessFilterHorizon);
+        final float intercept = getFloat(resources,
+                com.android.internal.R.dimen.config_displayWhiteBalanceBrightnessFilterIntercept);
+        if (!Float.isNaN(intercept)) {
+            return new AmbientFilter.WeightedMovingAverageAmbientFilter(
+                    BRIGHTNESS_FILTER_TAG, horizon, intercept);
+        }
+        throw new IllegalArgumentException("missing configurations: "
+                + "expected config_displayWhiteBalanceBrightnessFilterIntercept");
+    }
+
+
+    private static AmbientSensor.AmbientColorTemperatureSensor createColorTemperatureSensor(
+            Handler handler, SensorManager sensorManager, Resources resources) {
+        final String name = resources.getString(
+                com.android.internal.R.string
+                .config_displayWhiteBalanceColorTemperatureSensorName);
+        final int rate = resources.getInteger(
+                com.android.internal.R.integer
+                .config_displayWhiteBalanceColorTemperatureSensorRate);
+        return new AmbientSensor.AmbientColorTemperatureSensor(handler, sensorManager, name, rate);
+    }
+
+    private static AmbientFilter createColorTemperatureFilter(Resources resources) {
+        final int horizon = resources.getInteger(
+                com.android.internal.R.integer
+                .config_displayWhiteBalanceColorTemperatureFilterHorizon);
+        final float intercept = getFloat(resources,
+                com.android.internal.R.dimen
+                .config_displayWhiteBalanceColorTemperatureFilterIntercept);
+        if (!Float.isNaN(intercept)) {
+            return new AmbientFilter.WeightedMovingAverageAmbientFilter(
+                    COLOR_TEMPERATURE_FILTER_TAG, horizon, intercept);
+        }
+        throw new IllegalArgumentException("missing configurations: "
+                + "expected config_displayWhiteBalanceColorTemperatureFilterIntercept");
+    }
+
+    private static DisplayWhiteBalanceThrottler createThrottler(Resources resources) {
+        final int increaseDebounce = resources.getInteger(
+                com.android.internal.R.integer.config_displayWhiteBalanceDecreaseDebounce);
+        final int decreaseDebounce = resources.getInteger(
+                com.android.internal.R.integer.config_displayWhiteBalanceIncreaseDebounce);
+        final float[] baseThresholds = getFloatArray(resources,
+                com.android.internal.R.array.config_displayWhiteBalanceBaseThresholds);
+        final float[] increaseThresholds = getFloatArray(resources,
+                com.android.internal.R.array.config_displayWhiteBalanceIncreaseThresholds);
+        final float[] decreaseThresholds = getFloatArray(resources,
+                com.android.internal.R.array.config_displayWhiteBalanceDecreaseThresholds);
+        return new DisplayWhiteBalanceThrottler(increaseDebounce, decreaseDebounce, baseThresholds,
+                increaseThresholds, decreaseThresholds);
+    }
+
+    private static float getFloat(Resources resources, int id) {
+        TypedValue value = new TypedValue();
+        resources.getValue(id, value, true /* resolveRefs */);
+        if (value.type != TypedValue.TYPE_FLOAT) {
+            return Float.NaN;
+        }
+        return value.getFloat();
+    }
+
+    private static float[] getFloatArray(Resources resources, int id) {
+        TypedArray array = resources.obtainTypedArray(id);
+        try {
+            if (array.length() == 0) {
+                return null;
+            }
+            float[] values = new float[array.length()];
+            for (int i = 0; i < values.length; i++) {
+                values[i] = array.getFloat(i, Float.NaN);
+                if (Float.isNaN(values[i])) {
+                    return null;
+                }
+            }
+            return values;
+        } finally {
+            array.recycle();
+        }
+    }
+
+}
diff --git a/services/core/java/com/android/server/display/whitebalance/DisplayWhiteBalanceSettings.java b/services/core/java/com/android/server/display/whitebalance/DisplayWhiteBalanceSettings.java
new file mode 100644
index 0000000..a53e91c
--- /dev/null
+++ b/services/core/java/com/android/server/display/whitebalance/DisplayWhiteBalanceSettings.java
@@ -0,0 +1,230 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.display.whitebalance;
+
+import android.annotation.NonNull;
+import android.content.Context;
+import android.database.ContentObserver;
+import android.net.Uri;
+import android.os.Handler;
+import android.os.Looper;
+import android.os.Message;
+import android.os.UserHandle;
+import android.provider.Settings.Secure;
+import android.util.Slog;
+
+import com.android.internal.util.Preconditions;
+import com.android.server.LocalServices;
+import com.android.server.display.ColorDisplayService;
+import com.android.server.display.ColorDisplayService.ColorDisplayServiceInternal;
+import com.android.server.display.whitebalance.DisplayWhiteBalanceController.Callbacks;
+
+import java.io.PrintWriter;
+
+/**
+ * The DisplayWhiteBalanceSettings holds the state of all the settings related to
+ * display white-balance, and can be used to decide whether to enable the
+ * DisplayWhiteBalanceController.
+ */
+public class DisplayWhiteBalanceSettings implements
+        ColorDisplayService.DisplayWhiteBalanceListener {
+
+    protected static final String TAG = "DisplayWhiteBalanceSettings";
+    protected boolean mLoggingEnabled;
+
+    private static final String SETTING_URI = Secure.DISPLAY_WHITE_BALANCE_ENABLED;
+    private static final int SETTING_DEFAULT = 0;
+    private static final int SETTING_ENABLED = 1;
+
+    private static final int MSG_SET_ACTIVE = 1;
+
+    private final Context mContext;
+    private final Handler mHandler;
+    private final SettingsObserver mSettingsObserver;
+
+    // To decouple the DisplayPowerController from the DisplayWhiteBalanceSettings, the DPC
+    // implements Callbacks and passes itself to the DWBS so it can call back into it without
+    // knowing about it.
+    private Callbacks mCallbacks;
+
+    private int mSetting;
+    private boolean mActive;
+
+    /**
+     * @param context
+     *      The context in which display white-balance is used.
+     * @param handler
+     *      The handler used to determine which thread to run on.
+     *
+     * @throws NullPointerException
+     *      - context is null;
+     *      - handler is null.
+     */
+    public DisplayWhiteBalanceSettings(@NonNull Context context, @NonNull Handler handler) {
+        validateArguments(context, handler);
+        mLoggingEnabled = false;
+        mContext = context;
+        mHandler = new DisplayWhiteBalanceSettingsHandler(handler.getLooper());
+        mSettingsObserver = new SettingsObserver(mHandler);
+        mSetting = getSetting();
+        mActive = false;
+        mCallbacks = null;
+
+        mContext.getContentResolver().registerContentObserver(
+                Secure.getUriFor(SETTING_URI), false /* notifyForDescendants */, mSettingsObserver,
+                UserHandle.USER_ALL);
+
+        ColorDisplayServiceInternal cds =
+                LocalServices.getService(ColorDisplayServiceInternal.class);
+        cds.setDisplayWhiteBalanceListener(this);
+    }
+
+    /**
+     * Set an object to call back to when the display white balance state should be updated.
+     *
+     * @param callbacks
+     *      The object to call back to.
+     *
+     * @return Whether the method suceeded or not.
+     */
+    public boolean setCallbacks(Callbacks callbacks) {
+        if (mCallbacks == callbacks) {
+            return false;
+        }
+        mCallbacks = callbacks;
+        return true;
+    }
+
+    /**
+     * Enable/disable logging.
+     *
+     * @param loggingEnabled
+     *      Whether logging should be on/off.
+     *
+     * @return Whether the method succeeded or not.
+     */
+    public boolean setLoggingEnabled(boolean loggingEnabled) {
+        if (mLoggingEnabled == loggingEnabled) {
+            return false;
+        }
+        mLoggingEnabled = loggingEnabled;
+        return true;
+    }
+
+    /**
+     * Returns whether display white-balance is enabled.
+     *
+     * @return Whether display white-balance is enabled.
+     */
+    public boolean isEnabled() {
+        return (mSetting == SETTING_ENABLED) && mActive;
+    }
+
+    /**
+     * Re-evaluate state after switching to a new user.
+     */
+    public void onSwitchUser() {
+        handleSettingChange();
+    }
+
+    /**
+     * Dump the state.
+     *
+     * @param writer
+     *      The writer used to dump the state.
+     */
+    public void dump(PrintWriter writer) {
+        writer.println("DisplayWhiteBalanceSettings");
+        writer.println("  mLoggingEnabled=" + mLoggingEnabled);
+        writer.println("  mContext=" + mContext);
+        writer.println("  mHandler=" + mHandler);
+        writer.println("  mSettingsObserver=" + mSettingsObserver);
+        writer.println("  mSetting=" + mSetting);
+        writer.println("  mActive=" + mActive);
+        writer.println("  mCallbacks=" + mCallbacks);
+    }
+
+    @Override
+    public void onDisplayWhiteBalanceStatusChanged(boolean active) {
+        Message msg = mHandler.obtainMessage(MSG_SET_ACTIVE, active ? 1 : 0, 0);
+        msg.sendToTarget();
+    }
+
+    private void validateArguments(Context context, Handler handler) {
+        Preconditions.checkNotNull(context, "context must not be null");
+        Preconditions.checkNotNull(handler, "handler must not be null");
+    }
+
+    private int getSetting() {
+        return Secure.getIntForUser(mContext.getContentResolver(), SETTING_URI, SETTING_DEFAULT,
+                UserHandle.USER_CURRENT);
+    }
+
+    private void handleSettingChange() {
+        final int setting = getSetting();
+        if (mSetting == setting) {
+            return;
+        }
+        if (mLoggingEnabled) {
+            Slog.d(TAG, "Setting: " + setting);
+        }
+        mSetting = setting;
+        if (mCallbacks != null) {
+            mCallbacks.updateWhiteBalance();
+        }
+    }
+
+    private void setActive(boolean active) {
+        if (mActive == active) {
+            return;
+        }
+        if (mLoggingEnabled) {
+            Slog.d(TAG, "Active: " + active);
+        }
+        mActive = active;
+        if (mCallbacks != null) {
+            mCallbacks.updateWhiteBalance();
+        }
+    }
+
+    private final class SettingsObserver extends ContentObserver {
+        SettingsObserver(Handler handler) {
+            super(handler);
+        }
+
+        @Override
+        public void onChange(boolean selfChange, Uri uri) {
+            handleSettingChange();
+        }
+    }
+
+    private final class DisplayWhiteBalanceSettingsHandler extends Handler {
+        DisplayWhiteBalanceSettingsHandler(Looper looper) {
+            super(looper, null, true /* async */);
+        }
+
+        @Override
+        public void handleMessage(Message msg) {
+            switch (msg.what) {
+                case MSG_SET_ACTIVE:
+                    setActive(msg.arg1 != 0);
+                    break;
+            }
+        }
+    }
+
+}
diff --git a/services/core/java/com/android/server/display/whitebalance/DisplayWhiteBalanceThrottler.java b/services/core/java/com/android/server/display/whitebalance/DisplayWhiteBalanceThrottler.java
new file mode 100644
index 0000000..c1f0e98
--- /dev/null
+++ b/services/core/java/com/android/server/display/whitebalance/DisplayWhiteBalanceThrottler.java
@@ -0,0 +1,227 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.display.whitebalance;
+
+import android.util.Slog;
+
+import java.io.PrintWriter;
+import java.util.Arrays;
+
+/**
+ * The DisplayWhiteBalanceController uses the DisplayWhiteBalanceThrottler to decide whether the
+ * screen color temperature should be updated, suppressing changes that are too frequent or too
+ * minor.
+ */
+class DisplayWhiteBalanceThrottler {
+
+    protected static final String TAG = "DisplayWhiteBalanceThrottler";
+    protected boolean mLoggingEnabled;
+
+    private int mIncreaseDebounce;  // Milliseconds
+    private int mDecreaseDebounce;  // Milliseconds
+    private long mLastTime;         // Milliseconds
+
+    private float[] mBaseThresholds;
+    private float[] mIncreaseThresholds;
+    private float[] mDecreaseThresholds;
+    private float mIncreaseThreshold;
+    private float mDecreaseThreshold;
+    private float mLastValue;
+
+    /**
+     * @param increaseDebounce
+     *      The debounce time for increasing (throttled if {@code time < lastTime + debounce}).
+     * @param decreaseDebounce
+     *      The debounce time for decreasing (throttled if {@code time < lastTime + debounce}).
+     * @param baseThresholds
+     *      The ambient color temperature values used to determine the threshold as the
+     *      corresponding value in increaseThresholds/decreaseThresholds.
+     * @param increaseThresholds
+     *      The increase threshold values (throttled if {@code value < value * (1 + threshold)}).
+     * @param decreaseThresholds
+     *      The decrease threshold values (throttled if {@code value > value * (1 - threshold)}).
+     *
+     * @throws IllegalArgumentException
+     *      - increaseDebounce is negative;
+     *      - decreaseDebounce is negative;
+     *      - baseThresholds to increaseThresholds is not a valid mapping*;
+     *      - baseThresholds to decreaseThresholds is not a valid mapping*;
+     *
+     *      (*) The x to y mapping is valid if:
+     *          - x and y are not null;
+     *          - x and y are not empty;
+     *          - x and y contain only non-negative numbers;
+     *          - x is strictly increasing.
+     */
+    DisplayWhiteBalanceThrottler(int increaseDebounce, int decreaseDebounce,
+            float[] baseThresholds, float[] increaseThresholds, float[] decreaseThresholds) {
+        validateArguments(increaseDebounce, decreaseDebounce, baseThresholds, increaseThresholds,
+                decreaseThresholds);
+        mLoggingEnabled = false;
+        mIncreaseDebounce = increaseDebounce;
+        mDecreaseDebounce = decreaseDebounce;
+        mBaseThresholds = baseThresholds;
+        mIncreaseThresholds = increaseThresholds;
+        mDecreaseThresholds = decreaseThresholds;
+        clear();
+    }
+
+    /**
+     * Check whether the ambient color temperature should be throttled.
+     *
+     * @param value
+     *      The ambient color temperature value.
+     *
+     * @return Whether the ambient color temperature should be throttled.
+     */
+    public boolean throttle(float value) {
+        if (mLastTime != -1 && (tooSoon(value) || tooClose(value))) {
+            return true;
+        }
+        computeThresholds(value);
+        mLastTime = System.currentTimeMillis();
+        mLastValue = value;
+        return false;
+    }
+
+    /**
+     * Clears the throttler state.
+     */
+    public void clear() {
+        mLastTime = -1;
+        mIncreaseThreshold = -1.0f;
+        mDecreaseThreshold = -1.0f;
+        mLastValue = -1.0f;
+    }
+
+    /**
+     * Enable/disable logging.
+     *
+     * @param loggingEnabled
+     *      Whether logging is on/off.
+     *
+     * @return Whether the method succeeded or not.
+     */
+    public boolean setLoggingEnabled(boolean loggingEnabled) {
+        if (mLoggingEnabled == loggingEnabled) {
+            return false;
+        }
+        mLoggingEnabled = loggingEnabled;
+        return true;
+    }
+
+    /**
+     * Dump the state.
+     *
+     * @param writer
+     *      The PrintWriter used to dump the state.
+     */
+    public void dump(PrintWriter writer) {
+        writer.println("  DisplayWhiteBalanceThrottler");
+        writer.println("    mLoggingEnabled=" + mLoggingEnabled);
+        writer.println("    mIncreaseDebounce=" + mIncreaseDebounce);
+        writer.println("    mDecreaseDebounce=" + mDecreaseDebounce);
+        writer.println("    mLastTime=" + mLastTime);
+        writer.println("    mBaseThresholds=" + Arrays.toString(mBaseThresholds));
+        writer.println("    mIncreaseThresholds=" + Arrays.toString(mIncreaseThresholds));
+        writer.println("    mDecreaseThresholds=" + Arrays.toString(mDecreaseThresholds));
+        writer.println("    mIncreaseThreshold=" + mIncreaseThreshold);
+        writer.println("    mDecreaseThreshold=" + mDecreaseThreshold);
+        writer.println("    mLastValue=" + mLastValue);
+    }
+
+    private void validateArguments(float increaseDebounce, float decreaseDebounce,
+            float[] baseThresholds, float[] increaseThresholds, float[] decreaseThresholds) {
+        if (Float.isNaN(increaseDebounce) || increaseDebounce < 0.0f) {
+            throw new IllegalArgumentException("increaseDebounce must be a non-negative number.");
+        }
+        if (Float.isNaN(decreaseDebounce) || decreaseDebounce < 0.0f) {
+            throw new IllegalArgumentException("decreaseDebounce must be a non-negative number.");
+        }
+        if (!isValidMapping(baseThresholds, increaseThresholds)) {
+            throw new IllegalArgumentException(
+                    "baseThresholds to increaseThresholds is not a valid mapping.");
+        }
+        if (!isValidMapping(baseThresholds, decreaseThresholds)) {
+            throw new IllegalArgumentException(
+                    "baseThresholds to decreaseThresholds is not a valid mapping.");
+        }
+    }
+
+    private static boolean isValidMapping(float[] x, float[] y) {
+        if (x == null || y == null || x.length == 0 || y.length == 0 || x.length != y.length) {
+            return false;
+        }
+        float prevX = -1.0f;
+        for (int i = 0; i < x.length; i++) {
+            if (Float.isNaN(x[i]) || Float.isNaN(y[i]) || x[i] < 0 || prevX >= x[i]) {
+                return false;
+            }
+            prevX = x[i];
+        }
+        return true;
+    }
+
+    private boolean tooSoon(float value) {
+        final long time = System.currentTimeMillis();
+        final long earliestTime;
+        if (value > mLastValue) {
+            earliestTime = mLastTime + mIncreaseDebounce;
+        } else { // value <= mLastValue
+            earliestTime = mLastTime + mDecreaseDebounce;
+        }
+        final boolean tooSoon = time < earliestTime;
+        if (mLoggingEnabled) {
+            Slog.d(TAG, (tooSoon ? "too soon: " : "late enough: ") + time
+                    + (tooSoon ? " < " : " > ") + earliestTime);
+        }
+        return tooSoon;
+    }
+
+    private boolean tooClose(float value) {
+        final float threshold;
+        final boolean tooClose;
+        if (value > mLastValue) {
+            threshold = mIncreaseThreshold;
+            tooClose = value < threshold;
+        } else { // value <= mLastValue
+            threshold = mDecreaseThreshold;
+            tooClose = value > threshold;
+        }
+        if (mLoggingEnabled) {
+            Slog.d(TAG, (tooClose ? "too close: " : "far enough: ") + value
+                    + (value > threshold ? " > " : " < ") + threshold);
+        }
+        return tooClose;
+    }
+
+    private void computeThresholds(float value) {
+        final int index = getHighestIndexBefore(value, mBaseThresholds);
+        mIncreaseThreshold = value * (1.0f + mIncreaseThresholds[index]);
+        mDecreaseThreshold = value * (1.0f - mDecreaseThresholds[index]);
+    }
+
+    private int getHighestIndexBefore(float value, float[] values) {
+        for (int i = 0; i < values.length; i++) {
+            if (values[i] >= value) {
+                return i;
+            }
+        }
+        return values.length - 1;
+    }
+
+}
diff --git a/services/core/java/com/android/server/dreams/OWNERS b/services/core/java/com/android/server/dreams/OWNERS
index 3c9bbf8..426f002 100644
--- a/services/core/java/com/android/server/dreams/OWNERS
+++ b/services/core/java/com/android/server/dreams/OWNERS
@@ -1,3 +1,3 @@
-dsandler@google.com
+dsandler@android.com
 michaelwr@google.com
 roosa@google.com
diff --git a/services/core/java/com/android/server/incident/IncidentCompanionService.java b/services/core/java/com/android/server/incident/IncidentCompanionService.java
index 3ebba00..6f2bfc3 100644
--- a/services/core/java/com/android/server/incident/IncidentCompanionService.java
+++ b/services/core/java/com/android/server/incident/IncidentCompanionService.java
@@ -16,38 +16,18 @@
 
 package com.android.server.incident;
 
-import android.app.ActivityManager;
-import android.app.AppOpsManager;
-import android.content.ComponentName;
 import android.content.Context;
-import android.content.Intent;
-import android.content.pm.PackageManager;
-import android.content.pm.ResolveInfo;
-import android.content.pm.UserInfo;
-import android.net.Uri;
 import android.os.Binder;
-import android.os.Handler;
 import android.os.IIncidentAuthListener;
 import android.os.IIncidentCompanion;
-import android.os.IncidentManager;
-import android.os.RemoteException;
-import android.os.SystemClock;
-import android.os.UserHandle;
-import android.os.UserManager;
-import android.util.Log;
 
 import com.android.internal.util.DumpUtils;
 import com.android.server.SystemService;
 
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
-import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.Date;
 import java.util.List;
 
-// TODO: User changes should deny everything that's pending.
-
 /**
  * Helper service for incidentd and dumpstated to provide user feedback
  * and authorization for bug and inicdent reports to be taken.
@@ -55,83 +35,26 @@
 public class IncidentCompanionService extends SystemService {
     static final String TAG = "IncidentCompanionService";
 
-    private final Handler mHandler = new Handler();
-    private final RequestQueue mRequestQueue = new RequestQueue(mHandler);
-    private final PackageManager mPackageManager;
-    private final AppOpsManager mAppOpsManager;
-
-    //
-    // All fields below must be protected by mLock
-    //
-    private final Object mLock = new Object();
-    private final ArrayList<PendingReportRec> mPending = new ArrayList();
-
     /**
-     * The next ID we'll use when we make a PendingReportRec.
+     * Tracker for reports pending approval.
      */
-    private int mNextPendingId = 1;
-
-    /**
-     * One for each authorization that's pending.
-     */
-    private final class PendingReportRec {
-        public int id;
-        public String callingPackage;
-        public int flags;
-        public IIncidentAuthListener listener;
-        public long addedRealtime;
-        public long addedWalltime;
-
-        /**
-         * Construct a PendingReportRec, with an auto-incremented id.
-         */
-        PendingReportRec(String callingPackage, int flags, IIncidentAuthListener listener) {
-            this.id = mNextPendingId++;
-            this.callingPackage = callingPackage;
-            this.flags = flags;
-            this.listener = listener;
-            this.addedRealtime = SystemClock.elapsedRealtime();
-            this.addedWalltime = System.currentTimeMillis();
-        }
-
-        /**
-         * Get the Uri that contains the flattened data.
-         */
-        Uri getUri() {
-            return (new Uri.Builder())
-                    .scheme(IncidentManager.URI_SCHEME)
-                    .authority(IncidentManager.URI_AUTHORITY)
-                    .path(IncidentManager.URI_PATH)
-                    .appendQueryParameter(IncidentManager.URI_PARAM_ID, Integer.toString(id))
-                    .appendQueryParameter(IncidentManager.URI_PARAM_CALLING_PACKAGE, callingPackage)
-                    .appendQueryParameter(IncidentManager.URI_PARAM_FLAGS, Integer.toString(flags))
-                    .appendQueryParameter(IncidentManager.URI_PARAM_TIMESTAMP,
-                            Long.toString(addedWalltime))
-                    .build();
-        }
-    }
+    private PendingReports mPendingReports;
 
     /**
      * Implementation of the IIncidentCompanion binder interface.
      */
     private final class BinderService extends IIncidentCompanion.Stub {
         /**
-         * ONEWAY binder call to initiate authorizing the report.  The actual logic is posted
-         * to mRequestQueue, and may happen later.  The security checks need to happen here.
+         * ONEWAY binder call to initiate authorizing the report.
          */
         @Override
-        public void authorizeReport(int callingUid, final String callingPackage, final int flags,
+        public void authorizeReport(int callingUid, final String callingPackage, int flags,
                 final IIncidentAuthListener listener) {
             enforceRequestAuthorizationPermission();
 
             final long ident = Binder.clearCallingIdentity();
             try {
-                // Starting the system server is complicated, and rather than try to
-                // have a complicated lifecycle that we share with dumpstated and incidentd,
-                // we will accept the request, and then display it whenever it becomes possible to.
-                mRequestQueue.enqueue(listener.asBinder(), true, () -> {
-                    authorizeReportImpl(callingUid, callingPackage, flags, listener);
-                });
+                mPendingReports.authorizeReport(callingUid, callingPackage, flags, listener);
             } finally {
                 Binder.restoreCallingIdentity(ident);
             }
@@ -152,9 +75,7 @@
             // authorize/cancel pairs.
             final long ident = Binder.clearCallingIdentity();
             try {
-                mRequestQueue.enqueue(listener.asBinder(), false, () -> {
-                    cancelReportImpl(listener);
-                });
+                mPendingReports.cancelAuthorization(listener);
             } finally {
                 Binder.restoreCallingIdentity(ident);
             }
@@ -167,19 +88,11 @@
         @Override
         public List<String> getPendingReports() {
             enforceAuthorizePermission();
-
-            synchronized (mLock) {
-                final int size = mPending.size();
-                final ArrayList<String> result = new ArrayList(size);
-                for (int i = 0; i < size; i++) {
-                    result.add(mPending.get(i).getUri().toString());
-                }
-                return result;
-            }
+            return mPendingReports.getPendingReports();
         }
 
         /**
-         * ONEWAY binder call to mark a report as approved.
+         * SYNCHRONOUS binder call to mark a report as approved.
          */
         @Override
         public void approveReport(String uri) {
@@ -187,32 +100,14 @@
 
             final long ident = Binder.clearCallingIdentity();
             try {
-                final PendingReportRec rec;
-                synchronized (mLock) {
-                    rec = findAndRemovePendingReportRecLocked(uri);
-                    if (rec == null) {
-                        Log.e(TAG, "confirmApproved: Couldn't find record for uri: " + uri);
-                        return;
-                    }
-                }
-
-                // Re-do the broadcast, so whoever is listening knows the list changed,
-                // in case another one was added in the meantime.
-                sendBroadcast();
-
-                Log.i(TAG, "Approved report: " + uri);
-                try {
-                    rec.listener.onReportApproved();
-                } catch (RemoteException ex) {
-                    Log.w(TAG, "Failed calling back for approval for: " + uri, ex);
-                }
+                mPendingReports.approveReport(uri);
             } finally {
                 Binder.restoreCallingIdentity(ident);
             }
         }
 
         /**
-         * ONEWAY binder call to mark a report as NOT approved.
+         * SYNCHRONOUS binder call to mark a report as NOT approved.
          */
         @Override
         public void denyReport(String uri) {
@@ -220,25 +115,7 @@
 
             final long ident = Binder.clearCallingIdentity();
             try {
-                final PendingReportRec rec;
-                synchronized (mLock) {
-                    rec = findAndRemovePendingReportRecLocked(uri);
-                    if (rec == null) {
-                        Log.e(TAG, "confirmDenied: Couldn't find record for uri: " + uri);
-                        return;
-                    }
-                }
-
-                // Re-do the broadcast, so whoever is listening knows the list changed,
-                // in case another one was added in the meantime.
-                sendBroadcast();
-
-                Log.i(TAG, "Denied report: " + uri);
-                try {
-                    rec.listener.onReportDenied();
-                } catch (RemoteException ex) {
-                    Log.w(TAG, "Failed calling back for denial for: " + uri, ex);
-                }
+                mPendingReports.denyReport(uri);
             } finally {
                 Binder.restoreCallingIdentity(ident);
             }
@@ -252,27 +129,22 @@
             if (!DumpUtils.checkDumpPermission(getContext(), TAG, writer)) {
                 return;
             }
-            if (args.length == 0) {
-                // Standard text dumpsys
-                final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
-                synchronized (mLock) {
-                    final int size = mPending.size();
-                    writer.println("mPending: (" + size + ")");
-                    for (int i = 0; i < size; i++) {
-                        final PendingReportRec entry = mPending.get(i);
-                        writer.println(String.format("  %11d %s: %s", entry.addedRealtime,
-                                    df.format(new Date(entry.addedWalltime)),
-                                    entry.getUri().toString()));
-                    }
-                }
-            }
+            mPendingReports.dump(fd, writer, args);
         }
 
+        /**
+         * Inside the binder interface class because we want to do all of the authorization
+         * here, before calling out to the helper objects.
+         */
         private void enforceRequestAuthorizationPermission() {
             getContext().enforceCallingOrSelfPermission(
                     android.Manifest.permission.REQUEST_INCIDENT_REPORT_APPROVAL, null);
         }
 
+        /**
+         * Inside the binder interface class because we want to do all of the authorization
+         * here, before calling out to the helper objects.
+         */
         private void enforceAuthorizePermission() {
             getContext().enforceCallingOrSelfPermission(
                     android.Manifest.permission.APPROVE_INCIDENT_REPORTS, null);
@@ -285,8 +157,7 @@
      */
     public IncidentCompanionService(Context context) {
         super(context);
-        mPackageManager = context.getPackageManager();
-        mAppOpsManager = context.getSystemService(AppOpsManager.class);
+        mPendingReports = new PendingReports(context);
     }
 
     /**
@@ -307,239 +178,9 @@
         super.onBootPhase(phase);
         switch (phase) {
             case SystemService.PHASE_BOOT_COMPLETED:
-                // Release the enqueued work.
-                mRequestQueue.start();
+                mPendingReports.onBootCompleted();
                 break;
         }
     }
-
-    /**
-     * Start the confirmation process.
-     */
-    private void authorizeReportImpl(int callingUid, final String callingPackage, int flags,
-            final IIncidentAuthListener listener) {
-        // Enforce that the calling package pertains to the callingUid.
-        if (!isPackageInUid(callingUid, callingPackage)) {
-            Log.w(TAG, "Calling uid " + callingUid + " doesn't match package "
-                    + callingPackage);
-            denyReportBeforeAddingRec(listener, callingPackage);
-            return;
-        }
-
-        // Find the primary user of this device.
-        final int primaryUser = getAndValidateUser();
-        if (primaryUser == UserHandle.USER_NULL) {
-            denyReportBeforeAddingRec(listener, callingPackage);
-            return;
-        }
-
-        // Find the approver app (hint: it's PermissionController).
-        final ComponentName receiver = getApproverComponent(primaryUser);
-        if (receiver == null) {
-            // We couldn't find an approver... so deny the request here and now, before we
-            // do anything else.
-            denyReportBeforeAddingRec(listener, callingPackage);
-            return;
-        }
-
-        // Save the record for when the PermissionController comes back to authorize it.
-        PendingReportRec rec = null;
-        synchronized (mLock) {
-            rec = new PendingReportRec(callingPackage, flags, listener);
-            mPending.add(rec);
-        }
-
-        try {
-            listener.asBinder().linkToDeath(() -> {
-                Log.i(TAG, "Got death notification listener=" + listener);
-                cancelReportImpl(listener, receiver, primaryUser);
-            }, 0);
-        } catch (RemoteException ex) {
-            Log.e(TAG, "Remote died while trying to register death listener: " + rec.getUri());
-            // First, remove from our list.
-            cancelReportImpl(listener, receiver, primaryUser);
-        }
-
-        // Go tell Permission controller to start asking the user.
-        sendBroadcast(receiver, primaryUser);
-    }
-
-    /**
-     * Cancel a pending report request (because of an explicit call to cancel)
-     */
-    private void cancelReportImpl(IIncidentAuthListener listener) {
-        final int primaryUser = getAndValidateUser();
-        final ComponentName receiver = getApproverComponent(primaryUser);
-        if (primaryUser != UserHandle.USER_NULL && receiver != null) {
-            cancelReportImpl(listener, receiver, primaryUser);
-        }
-    }
-
-    /**
-     * Cancel a pending report request (either because of an explicit call to cancel
-     * by the calling app, or because of a binder death).
-     */
-    private void cancelReportImpl(IIncidentAuthListener listener, ComponentName receiver,
-            int primaryUser) {
-        // First, remove from our list.
-        synchronized (mLock) {
-            removePendingReportRecLocked(listener);
-        }
-        // Second, call back to PermissionController to say it's canceled.
-        sendBroadcast(receiver, primaryUser);
-    }
-
-    /**
-     * Send an extra copy of the broadcast, to tell them that the list has changed
-     * because of an addition or removal.  This function is less aggressive than
-     * authorizeReportImpl in logging about failures, because this is for use in
-     * cleanup cases to keep the apps' list in sync with ours.
-     */
-    private void sendBroadcast() {
-        final int primaryUser = getAndValidateUser();
-        if (primaryUser == UserHandle.USER_NULL) {
-            return;
-        }
-        final ComponentName receiver = getApproverComponent(primaryUser);
-        if (receiver == null) {
-            return;
-        }
-        sendBroadcast(receiver, primaryUser);
-    }
-
-    /**
-     * Send the confirmation broadcast.
-     */
-    private void sendBroadcast(ComponentName receiver, int primaryUser) {
-        final Intent intent = new Intent(Intent.ACTION_PENDING_INCIDENT_REPORTS_CHANGED);
-        intent.setComponent(receiver);
-
-        // Send it to the primary user.
-        getContext().sendBroadcastAsUser(intent, UserHandle.getUserHandleForUid(primaryUser),
-                android.Manifest.permission.APPROVE_INCIDENT_REPORTS);
-    }
-
-    /**
-     * Remove a PendingReportRec keyed by uri, and return it.
-     */
-    private PendingReportRec findAndRemovePendingReportRecLocked(String uriString) {
-        final Uri uri = Uri.parse(uriString);
-        final int id;
-        try {
-            final String idStr = uri.getQueryParameter(IncidentManager.URI_PARAM_ID);
-            id = Integer.parseInt(idStr);
-        } catch (NumberFormatException ex) {
-            Log.w(TAG, "Can't parse id from: " + uriString);
-            return null;
-        }
-        final int size = mPending.size();
-        for (int i = 0; i < size; i++) {
-            final PendingReportRec rec = mPending.get(i);
-            if (rec.id == id) {
-                mPending.remove(i);
-                return rec;
-            }
-        }
-        return null;
-    }
-
-    /**
-     * Remove a PendingReportRec keyed by listener.
-     */
-    private void removePendingReportRecLocked(IIncidentAuthListener listener) {
-        final int size = mPending.size();
-        for (int i = 0; i < size; i++) {
-            final PendingReportRec rec = mPending.get(i);
-            if (rec.listener.asBinder() == listener.asBinder()) {
-                Log.i(TAG, "  ...Removed PendingReportRec index=" + i + ": " + rec.getUri());
-                mPending.remove(i);
-            }
-        }
-    }
-
-    /**
-     * Just call listener.deny() (wrapping the RemoteException), without try to
-     * add it to the list.
-     */
-    private void denyReportBeforeAddingRec(IIncidentAuthListener listener, String pkg) {
-        try {
-            listener.onReportDenied();
-        } catch (RemoteException ex) {
-            Log.w(TAG, "Failed calling back for denial for " + pkg, ex);
-        }
-    }
-
-    /**
-     * Check whether the current user is the primary user, and return the user id if they are.
-     * Returns UserHandle.USER_NULL if not valid.
-     */
-    private int getAndValidateUser() {
-        // Current user
-        UserInfo currentUser;
-        try {
-            currentUser = ActivityManager.getService().getCurrentUser();
-        } catch (RemoteException ex) {
-            // We're already inside the system process.
-            throw new RuntimeException(ex);
-        }
-
-        // Primary user
-        final UserManager um = UserManager.get(getContext());
-        final UserInfo primaryUser = um.getPrimaryUser();
-
-        // Check that we're using the right user.
-        if (currentUser == null) {
-            Log.w(TAG, "No current user.  Nobody to approve the report."
-                    + " The report will be denied.");
-            return UserHandle.USER_NULL;
-        }
-        if (primaryUser == null) {
-            Log.w(TAG, "No primary user.  Nobody to approve the report."
-                    + " The report will be denied.");
-            return UserHandle.USER_NULL;
-        }
-        if (primaryUser.id != currentUser.id) {
-            Log.w(TAG, "Only the primary user can approve bugreports, but they are not"
-                    + " the current user. The report will be denied.");
-            return UserHandle.USER_NULL;
-        }
-
-        return primaryUser.id;
-    }
-
-    /**
-     * Return the ComponentName of the BroadcastReceiver that will approve reports.
-     * The system must have zero or one of these installed.  We only look on the
-     * system partition.  When the broadcast happens, the component will also need
-     * have the APPROVE_INCIDENT_REPORTS permission.
-     */
-    private ComponentName getApproverComponent(int userId) {
-        // Find the one true BroadcastReceiver
-        final Intent intent = new Intent(Intent.ACTION_PENDING_INCIDENT_REPORTS_CHANGED);
-        final List<ResolveInfo> matches = mPackageManager.queryBroadcastReceiversAsUser(intent,
-                PackageManager.MATCH_SYSTEM_ONLY | PackageManager.MATCH_DIRECT_BOOT_AWARE
-                | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, userId);
-        if (matches.size() == 1) {
-            return matches.get(0).getComponentInfo().getComponentName();
-        } else {
-            Log.w(TAG, "Didn't find exactly one BroadcastReceiver to handle "
-                    + Intent.ACTION_PENDING_INCIDENT_REPORTS_CHANGED
-                    + ". The report will be denied. size="
-                    + matches.size() + ": matches=" + matches);
-            return null;
-        }
-    }
-
-    /**
-     * Return whether the package is one of the packages installed for the uid.
-     */
-    private boolean isPackageInUid(int uid, String packageName) {
-        try {
-            mAppOpsManager.checkPackage(uid, packageName);
-            return true;
-        } catch (SecurityException ex) {
-            return false;
-        }
-    }
 }
 
diff --git a/services/core/java/com/android/server/incident/PendingReports.java b/services/core/java/com/android/server/incident/PendingReports.java
new file mode 100644
index 0000000..519ed41
--- /dev/null
+++ b/services/core/java/com/android/server/incident/PendingReports.java
@@ -0,0 +1,477 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.incident;
+
+import android.app.ActivityManager;
+import android.app.AppOpsManager;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.content.pm.UserInfo;
+import android.net.Uri;
+import android.os.Handler;
+import android.os.IIncidentAuthListener;
+import android.os.IncidentManager;
+import android.os.RemoteException;
+import android.os.SystemClock;
+import android.os.UserHandle;
+import android.os.UserManager;
+import android.util.Log;
+
+import java.io.FileDescriptor;
+import java.io.PrintWriter;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+// TODO: User changes should deny everything that's pending.
+
+/**
+ * Tracker for reports pending approval.
+ */
+class PendingReports {
+    static final String TAG = IncidentCompanionService.TAG;
+
+    private final Handler mHandler = new Handler();
+    private final RequestQueue mRequestQueue = new RequestQueue(mHandler);
+    private final Context mContext;
+    private final PackageManager mPackageManager;
+    private final AppOpsManager mAppOpsManager;
+
+    //
+    // All fields below must be protected by mLock
+    //
+    private final Object mLock = new Object();
+    private final ArrayList<PendingReportRec> mPending = new ArrayList();
+
+    /**
+     * The next ID we'll use when we make a PendingReportRec.
+     */
+    private int mNextPendingId = 1;
+
+    /**
+     * One for each authorization that's pending.
+     */
+    private final class PendingReportRec {
+        public int id;
+        public String callingPackage;
+        public int flags;
+        public IIncidentAuthListener listener;
+        public long addedRealtime;
+        public long addedWalltime;
+
+        /**
+         * Construct a PendingReportRec, with an auto-incremented id.
+         */
+        PendingReportRec(String callingPackage, int flags, IIncidentAuthListener listener) {
+            this.id = mNextPendingId++;
+            this.callingPackage = callingPackage;
+            this.flags = flags;
+            this.listener = listener;
+            this.addedRealtime = SystemClock.elapsedRealtime();
+            this.addedWalltime = System.currentTimeMillis();
+        }
+
+        /**
+         * Get the Uri that contains the flattened data.
+         */
+        Uri getUri() {
+            return (new Uri.Builder())
+                    .scheme(IncidentManager.URI_SCHEME)
+                    .authority(IncidentManager.URI_AUTHORITY)
+                    .path(IncidentManager.URI_PATH)
+                    .appendQueryParameter(IncidentManager.URI_PARAM_ID, Integer.toString(id))
+                    .appendQueryParameter(IncidentManager.URI_PARAM_CALLING_PACKAGE, callingPackage)
+                    .appendQueryParameter(IncidentManager.URI_PARAM_FLAGS, Integer.toString(flags))
+                    .appendQueryParameter(IncidentManager.URI_PARAM_TIMESTAMP,
+                            Long.toString(addedWalltime))
+                    .build();
+        }
+    }
+
+    /**
+     * Construct new PendingReports with the context.
+     */
+    PendingReports(Context context) {
+        mContext = context;
+        mPackageManager = context.getPackageManager();
+        mAppOpsManager = context.getSystemService(AppOpsManager.class);
+    }
+
+    /**
+     * ONEWAY binder call to initiate authorizing the report.  The actual logic is posted
+     * to mRequestQueue, and may happen later.
+     * <p>
+     * The security checks are handled by IncidentCompanionService.
+     */
+    public void authorizeReport(int callingUid, final String callingPackage, final int flags,
+            final IIncidentAuthListener listener) {
+        // Starting the system server is complicated, and rather than try to
+        // have a complicated lifecycle that we share with dumpstated and incidentd,
+        // we will accept the request, and then display it whenever it becomes possible to.
+        mRequestQueue.enqueue(listener.asBinder(), true, () -> {
+            authorizeReportImpl(callingUid, callingPackage, flags, listener);
+        });
+    }
+
+    /**
+     * ONEWAY binder call to cancel the inbound authorization request.
+     * <p>
+     * This is a oneway call, and so is authorizeReport, so the
+     * caller's ordering is preserved.  The other calls on this object are synchronous, so
+     * their ordering is not guaranteed with respect to these calls.  So the implementation
+     * sends out extra broadcasts to allow for eventual consistency.
+     * <p>
+     * The security checks are handled by IncidentCompanionService.
+     */
+    public void cancelAuthorization(final IIncidentAuthListener listener) {
+        mRequestQueue.enqueue(listener.asBinder(), false, () -> {
+            cancelReportImpl(listener);
+        });
+    }
+
+    /**
+     * SYNCHRONOUS binder call to get the list of reports that are pending confirmation
+     * by the user.
+     * <p>
+     * The security checks are handled by IncidentCompanionService.
+     */
+    public List<String> getPendingReports() {
+        synchronized (mLock) {
+            final int size = mPending.size();
+            final ArrayList<String> result = new ArrayList(size);
+            for (int i = 0; i < size; i++) {
+                result.add(mPending.get(i).getUri().toString());
+            }
+            return result;
+        }
+    }
+
+    /**
+     * SYNCHRONOUS binder call to mark a report as approved.
+     * <p>
+     * The security checks are handled by IncidentCompanionService.
+     */
+    public void approveReport(String uri) {
+        final PendingReportRec rec;
+        synchronized (mLock) {
+            rec = findAndRemovePendingReportRecLocked(uri);
+            if (rec == null) {
+                Log.e(TAG, "confirmApproved: Couldn't find record for uri: " + uri);
+                return;
+            }
+        }
+
+        // Re-do the broadcast, so whoever is listening knows the list changed,
+        // in case another one was added in the meantime.
+        sendBroadcast();
+
+        Log.i(TAG, "Approved report: " + uri);
+        try {
+            rec.listener.onReportApproved();
+        } catch (RemoteException ex) {
+            Log.w(TAG, "Failed calling back for approval for: " + uri, ex);
+        }
+    }
+
+    /**
+     * SYNCHRONOUS binder call to mark a report as NOT approved.
+     */
+    public void denyReport(String uri) {
+        final PendingReportRec rec;
+        synchronized (mLock) {
+            rec = findAndRemovePendingReportRecLocked(uri);
+            if (rec == null) {
+                Log.e(TAG, "confirmDenied: Couldn't find record for uri: " + uri);
+                return;
+            }
+        }
+
+        // Re-do the broadcast, so whoever is listening knows the list changed,
+        // in case another one was added in the meantime.
+        sendBroadcast();
+
+        Log.i(TAG, "Denied report: " + uri);
+        try {
+            rec.listener.onReportDenied();
+        } catch (RemoteException ex) {
+            Log.w(TAG, "Failed calling back for denial for: " + uri, ex);
+        }
+    }
+
+    /**
+     * Implementation of adb shell dumpsys debugreportcompanion.
+     */
+    protected void dump(FileDescriptor fd, final PrintWriter writer, String[] args) {
+        if (args.length == 0) {
+            // Standard text dumpsys
+            final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
+            synchronized (mLock) {
+                final int size = mPending.size();
+                writer.println("mPending: (" + size + ")");
+                for (int i = 0; i < size; i++) {
+                    final PendingReportRec entry = mPending.get(i);
+                    writer.println(String.format("  %11d %s: %s", entry.addedRealtime,
+                                df.format(new Date(entry.addedWalltime)),
+                                entry.getUri().toString()));
+                }
+            }
+        }
+    }
+
+    /**
+     * Handle the boot process... Starts everything running once the system is
+     * up enough for us to do UI.
+     */
+    public void onBootCompleted() {
+        // Release the enqueued work.
+        mRequestQueue.start();
+    }
+
+    /**
+     * Start the confirmation process.
+     */
+    private void authorizeReportImpl(int callingUid, final String callingPackage, int flags,
+            final IIncidentAuthListener listener) {
+        // Enforce that the calling package pertains to the callingUid.
+        if (!isPackageInUid(callingUid, callingPackage)) {
+            Log.w(TAG, "Calling uid " + callingUid + " doesn't match package "
+                    + callingPackage);
+            denyReportBeforeAddingRec(listener, callingPackage);
+            return;
+        }
+
+        // Find the primary user of this device.
+        final int primaryUser = getAndValidateUser();
+        if (primaryUser == UserHandle.USER_NULL) {
+            denyReportBeforeAddingRec(listener, callingPackage);
+            return;
+        }
+
+        // Find the approver app (hint: it's PermissionController).
+        final ComponentName receiver = getApproverComponent(primaryUser);
+        if (receiver == null) {
+            // We couldn't find an approver... so deny the request here and now, before we
+            // do anything else.
+            denyReportBeforeAddingRec(listener, callingPackage);
+            return;
+        }
+
+        // Save the record for when the PermissionController comes back to authorize it.
+        PendingReportRec rec = null;
+        synchronized (mLock) {
+            rec = new PendingReportRec(callingPackage, flags, listener);
+            mPending.add(rec);
+        }
+
+        try {
+            listener.asBinder().linkToDeath(() -> {
+                Log.i(TAG, "Got death notification listener=" + listener);
+                cancelReportImpl(listener, receiver, primaryUser);
+            }, 0);
+        } catch (RemoteException ex) {
+            Log.e(TAG, "Remote died while trying to register death listener: " + rec.getUri());
+            // First, remove from our list.
+            cancelReportImpl(listener, receiver, primaryUser);
+        }
+
+        // Go tell Permission controller to start asking the user.
+        sendBroadcast(receiver, primaryUser);
+    }
+
+    /**
+     * Cancel a pending report request (because of an explicit call to cancel)
+     */
+    private void cancelReportImpl(IIncidentAuthListener listener) {
+        final int primaryUser = getAndValidateUser();
+        final ComponentName receiver = getApproverComponent(primaryUser);
+        if (primaryUser != UserHandle.USER_NULL && receiver != null) {
+            cancelReportImpl(listener, receiver, primaryUser);
+        }
+    }
+
+    /**
+     * Cancel a pending report request (either because of an explicit call to cancel
+     * by the calling app, or because of a binder death).
+     */
+    private void cancelReportImpl(IIncidentAuthListener listener, ComponentName receiver,
+            int primaryUser) {
+        // First, remove from our list.
+        synchronized (mLock) {
+            removePendingReportRecLocked(listener);
+        }
+        // Second, call back to PermissionController to say it's canceled.
+        sendBroadcast(receiver, primaryUser);
+    }
+
+    /**
+     * Send an extra copy of the broadcast, to tell them that the list has changed
+     * because of an addition or removal.  This function is less aggressive than
+     * authorizeReportImpl in logging about failures, because this is for use in
+     * cleanup cases to keep the apps' list in sync with ours.
+     */
+    private void sendBroadcast() {
+        final int primaryUser = getAndValidateUser();
+        if (primaryUser == UserHandle.USER_NULL) {
+            return;
+        }
+        final ComponentName receiver = getApproverComponent(primaryUser);
+        if (receiver == null) {
+            return;
+        }
+        sendBroadcast(receiver, primaryUser);
+    }
+
+    /**
+     * Send the confirmation broadcast.
+     */
+    private void sendBroadcast(ComponentName receiver, int primaryUser) {
+        final Intent intent = new Intent(Intent.ACTION_PENDING_INCIDENT_REPORTS_CHANGED);
+        intent.setComponent(receiver);
+
+        // Send it to the primary user.
+        mContext.sendBroadcastAsUser(intent, UserHandle.getUserHandleForUid(primaryUser),
+                android.Manifest.permission.APPROVE_INCIDENT_REPORTS);
+    }
+
+    /**
+     * Remove a PendingReportRec keyed by uri, and return it.
+     */
+    private PendingReportRec findAndRemovePendingReportRecLocked(String uriString) {
+        final Uri uri = Uri.parse(uriString);
+        final int id;
+        try {
+            final String idStr = uri.getQueryParameter(IncidentManager.URI_PARAM_ID);
+            id = Integer.parseInt(idStr);
+        } catch (NumberFormatException ex) {
+            Log.w(TAG, "Can't parse id from: " + uriString);
+            return null;
+        }
+        final int size = mPending.size();
+        for (int i = 0; i < size; i++) {
+            final PendingReportRec rec = mPending.get(i);
+            if (rec.id == id) {
+                mPending.remove(i);
+                return rec;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Remove a PendingReportRec keyed by listener.
+     */
+    private void removePendingReportRecLocked(IIncidentAuthListener listener) {
+        final int size = mPending.size();
+        for (int i = 0; i < size; i++) {
+            final PendingReportRec rec = mPending.get(i);
+            if (rec.listener.asBinder() == listener.asBinder()) {
+                Log.i(TAG, "  ...Removed PendingReportRec index=" + i + ": " + rec.getUri());
+                mPending.remove(i);
+            }
+        }
+    }
+
+    /**
+     * Just call listener.deny() (wrapping the RemoteException), without try to
+     * add it to the list.
+     */
+    private void denyReportBeforeAddingRec(IIncidentAuthListener listener, String pkg) {
+        try {
+            listener.onReportDenied();
+        } catch (RemoteException ex) {
+            Log.w(TAG, "Failed calling back for denial for " + pkg, ex);
+        }
+    }
+
+    /**
+     * Check whether the current user is the primary user, and return the user id if they are.
+     * Returns UserHandle.USER_NULL if not valid.
+     */
+    private int getAndValidateUser() {
+        // Current user
+        UserInfo currentUser;
+        try {
+            currentUser = ActivityManager.getService().getCurrentUser();
+        } catch (RemoteException ex) {
+            // We're already inside the system process.
+            throw new RuntimeException(ex);
+        }
+
+        // Primary user
+        final UserManager um = UserManager.get(mContext);
+        final UserInfo primaryUser = um.getPrimaryUser();
+
+        // Check that we're using the right user.
+        if (currentUser == null) {
+            Log.w(TAG, "No current user.  Nobody to approve the report."
+                    + " The report will be denied.");
+            return UserHandle.USER_NULL;
+        }
+        if (primaryUser == null) {
+            Log.w(TAG, "No primary user.  Nobody to approve the report."
+                    + " The report will be denied.");
+            return UserHandle.USER_NULL;
+        }
+        if (primaryUser.id != currentUser.id) {
+            Log.w(TAG, "Only the primary user can approve bugreports, but they are not"
+                    + " the current user. The report will be denied.");
+            return UserHandle.USER_NULL;
+        }
+
+        return primaryUser.id;
+    }
+
+    /**
+     * Return the ComponentName of the BroadcastReceiver that will approve reports.
+     * The system must have zero or one of these installed.  We only look on the
+     * system partition.  When the broadcast happens, the component will also need
+     * have the APPROVE_INCIDENT_REPORTS permission.
+     */
+    private ComponentName getApproverComponent(int userId) {
+        // Find the one true BroadcastReceiver
+        final Intent intent = new Intent(Intent.ACTION_PENDING_INCIDENT_REPORTS_CHANGED);
+        final List<ResolveInfo> matches = mPackageManager.queryBroadcastReceiversAsUser(intent,
+                PackageManager.MATCH_SYSTEM_ONLY | PackageManager.MATCH_DIRECT_BOOT_AWARE
+                | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, userId);
+        if (matches.size() == 1) {
+            return matches.get(0).getComponentInfo().getComponentName();
+        } else {
+            Log.w(TAG, "Didn't find exactly one BroadcastReceiver to handle "
+                    + Intent.ACTION_PENDING_INCIDENT_REPORTS_CHANGED
+                    + ". The report will be denied. size="
+                    + matches.size() + ": matches=" + matches);
+            return null;
+        }
+    }
+
+    /**
+     * Return whether the package is one of the packages installed for the uid.
+     */
+    private boolean isPackageInUid(int uid, String packageName) {
+        try {
+            mAppOpsManager.checkPackage(uid, packageName);
+            return true;
+        } catch (SecurityException ex) {
+            return false;
+        }
+    }
+}
+
diff --git a/services/core/java/com/android/server/location/ActivityRecognitionProxy.java b/services/core/java/com/android/server/location/ActivityRecognitionProxy.java
index 22fabb2..80ab790 100644
--- a/services/core/java/com/android/server/location/ActivityRecognitionProxy.java
+++ b/services/core/java/com/android/server/location/ActivityRecognitionProxy.java
@@ -24,7 +24,7 @@
 import android.os.RemoteException;
 import android.util.Log;
 
-import com.android.internal.os.BackgroundThread;
+import com.android.server.FgThread;
 import com.android.server.ServiceWatcher;
 
 /**
@@ -84,7 +84,7 @@
                 overlaySwitchResId,
                 defaultServicePackageNameResId,
                 initialPackageNameResId,
-                BackgroundThread.getHandler()) {
+                FgThread.getHandler()) {
             @Override
             protected void onBind() {
                 runOnBinder(ActivityRecognitionProxy.this::initializeService);
diff --git a/services/core/java/com/android/server/location/GeocoderProxy.java b/services/core/java/com/android/server/location/GeocoderProxy.java
index e6f0ed9..d9602b8 100644
--- a/services/core/java/com/android/server/location/GeocoderProxy.java
+++ b/services/core/java/com/android/server/location/GeocoderProxy.java
@@ -21,7 +21,7 @@
 import android.location.GeocoderParams;
 import android.location.IGeocodeProvider;
 
-import com.android.internal.os.BackgroundThread;
+import com.android.server.FgThread;
 import com.android.server.ServiceWatcher;
 
 import java.util.List;
@@ -53,7 +53,7 @@
             int initialPackageNamesResId) {
         mServiceWatcher = new ServiceWatcher(context, TAG, SERVICE_ACTION, overlaySwitchResId,
                 defaultServicePackageNameResId, initialPackageNamesResId,
-                BackgroundThread.getHandler());
+                FgThread.getHandler());
     }
 
     private boolean bind() {
diff --git a/services/core/java/com/android/server/location/GeofenceProxy.java b/services/core/java/com/android/server/location/GeofenceProxy.java
index 38c7d49..ce93661 100644
--- a/services/core/java/com/android/server/location/GeofenceProxy.java
+++ b/services/core/java/com/android/server/location/GeofenceProxy.java
@@ -30,7 +30,7 @@
 import android.os.UserHandle;
 import android.util.Log;
 
-import com.android.internal.os.BackgroundThread;
+import com.android.server.FgThread;
 import com.android.server.ServiceWatcher;
 
 /**
@@ -82,7 +82,7 @@
         mContext = context;
         mServiceWatcher = new ServiceWatcher(context, TAG, SERVICE_ACTION, overlaySwitchResId,
                 defaultServicePackageNameResId, initialPackageNamesResId,
-                BackgroundThread.getHandler()) {
+                FgThread.getHandler()) {
             @Override
             protected void onBind() {
                 runOnBinder(mUpdateGeofenceHardware);
diff --git a/services/core/java/com/android/server/location/LocationProviderProxy.java b/services/core/java/com/android/server/location/LocationProviderProxy.java
index 6b5b1be..776beb5 100644
--- a/services/core/java/com/android/server/location/LocationProviderProxy.java
+++ b/services/core/java/com/android/server/location/LocationProviderProxy.java
@@ -31,8 +31,8 @@
 import com.android.internal.location.ILocationProviderManager;
 import com.android.internal.location.ProviderProperties;
 import com.android.internal.location.ProviderRequest;
-import com.android.internal.os.BackgroundThread;
 import com.android.internal.os.TransferPipe;
+import com.android.server.FgThread;
 import com.android.server.LocationManagerService;
 import com.android.server.ServiceWatcher;
 
@@ -105,7 +105,7 @@
 
         mServiceWatcher = new ServiceWatcher(context, TAG, action, overlaySwitchResId,
                 defaultServicePackageNameResId, initialPackageNamesResId,
-                BackgroundThread.getHandler()) {
+                FgThread.getHandler()) {
 
             @Override
             protected void onBind() {
diff --git a/services/core/java/com/android/server/om/OverlayManagerServiceImpl.java b/services/core/java/com/android/server/om/OverlayManagerServiceImpl.java
index 1cbf0bf..b0d2704 100644
--- a/services/core/java/com/android/server/om/OverlayManagerServiceImpl.java
+++ b/services/core/java/com/android/server/om/OverlayManagerServiceImpl.java
@@ -270,9 +270,7 @@
             Slog.d(TAG, "onTargetPackageUpgraded packageName=" + packageName + " userId=" + userId);
         }
 
-        if (updateAllOverlaysForTarget(packageName, userId, 0)) {
-            mListener.onOverlaysChanged(packageName, userId);
-        }
+        updateAllOverlaysForTarget(packageName, userId, 0);
     }
 
     void onTargetPackageRemoved(@NonNull final String packageName, final int userId) {
diff --git a/services/core/java/com/android/server/pm/LauncherAppsService.java b/services/core/java/com/android/server/pm/LauncherAppsService.java
index c2ac27a..62c4815 100644
--- a/services/core/java/com/android/server/pm/LauncherAppsService.java
+++ b/services/core/java/com/android/server/pm/LauncherAppsService.java
@@ -34,9 +34,11 @@
 import android.content.pm.ApplicationInfo;
 import android.content.pm.ILauncherApps;
 import android.content.pm.IOnAppsChangedListener;
+import android.content.pm.IPackageInstallerCallback;
 import android.content.pm.LauncherApps;
 import android.content.pm.LauncherApps.ShortcutQuery;
 import android.content.pm.PackageInfo;
+import android.content.pm.PackageInstaller.SessionInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.PackageManagerInternal;
 import android.content.pm.ParceledListSlice;
@@ -56,6 +58,7 @@
 import android.os.ParcelFileDescriptor;
 import android.os.RemoteCallbackList;
 import android.os.RemoteException;
+import android.os.ServiceManager;
 import android.os.UserHandle;
 import android.os.UserManager;
 import android.os.UserManagerInternal;
@@ -153,6 +156,8 @@
 
         private final Object mVouchedSignaturesLocked = new Object();
 
+        private PackageInstallerService mPackageInstallerService;
+
         public LauncherAppsImpl(Context context) {
             mContext = context;
             mUm = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
@@ -204,8 +209,7 @@
         }
 
         /*
-         * @see android.content.pm.ILauncherApps#addOnAppsChangedListener(
-         *          android.content.pm.IOnAppsChangedListener)
+         * @see android.content.pm.ILauncherApps#addOnAppsChangedListener
          */
         @Override
         public void addOnAppsChangedListener(String callingPackage, IOnAppsChangedListener listener)
@@ -228,8 +232,7 @@
         }
 
         /*
-         * @see android.content.pm.ILauncherApps#removeOnAppsChangedListener(
-         *          android.content.pm.IOnAppsChangedListener)
+         * @see android.content.pm.ILauncherApps#removeOnAppsChangedListener
          */
         @Override
         public void removeOnAppsChangedListener(IOnAppsChangedListener listener)
@@ -246,6 +249,44 @@
         }
 
         /**
+         * @see android.content.pm.ILauncherApps#registerPackageInstallerCallback
+         */
+        @Override
+        public void registerPackageInstallerCallback(String callingPackage,
+                IPackageInstallerCallback callback) {
+            verifyCallingPackage(callingPackage);
+            UserHandle callingIdUserHandle = new UserHandle(getCallingUserId());
+            getPackageInstallerService().registerCallback(callback, eventUserId ->
+                            isEnabledProfileOf(callingIdUserHandle,
+                                    new UserHandle(eventUserId), "shouldReceiveEvent"));
+        }
+
+        @Override
+        public ParceledListSlice<SessionInfo> getAllSessions(String callingPackage) {
+            verifyCallingPackage(callingPackage);
+            List<SessionInfo> sessionInfos = new ArrayList<>();
+            int[] userIds = mUm.getEnabledProfileIds(getCallingUserId());
+            long token = Binder.clearCallingIdentity();
+            try {
+                for (int userId : userIds) {
+                    sessionInfos.addAll(getPackageInstallerService().getAllSessions(userId)
+                            .getList());
+                }
+            } finally {
+                Binder.restoreCallingIdentity(token);
+            }
+            return new ParceledListSlice<>(sessionInfos);
+        }
+
+        private PackageInstallerService getPackageInstallerService() {
+            if (mPackageInstallerService == null) {
+                mPackageInstallerService = ((PackageInstallerService) ((PackageManagerService)
+                        ServiceManager.getService("package")).getPackageInstaller());
+            }
+            return mPackageInstallerService;
+        }
+
+        /**
          * Register a receiver to watch for package broadcasts
          */
         private void startWatchingPackageBroadcasts() {
@@ -430,6 +471,9 @@
             if (!mVouchedSignaturesByUser.containsKey(user)) {
                 initVouchedSignatures(user);
             }
+            if (isManagedProfileAdmin(user, appInfo.packageName)) {
+                return false;
+            }
             if (mVouchProviders.contains(appInfo.packageName)) {
                 // If it's a vouching packages then we must show hidden app
                 return true;
@@ -453,6 +497,24 @@
             return true;
         }
 
+        private boolean isManagedProfileAdmin(UserHandle user, String packageName) {
+            final List<UserInfo> userInfoList = mUm.getProfiles(user.getIdentifier());
+            for (int i = 0; i < userInfoList.size(); i++) {
+                UserInfo userInfo = userInfoList.get(i);
+                if (!userInfo.isManagedProfile()) {
+                    continue;
+                }
+                ComponentName componentName = mDpm.getProfileOwnerAsUser(userInfo.getUserHandle());
+                if (componentName == null) {
+                    continue;
+                }
+                if (componentName.getPackageName().equals(packageName)) {
+                    return true;
+                }
+            }
+            return false;
+        }
+
         @VisibleForTesting
         static String computePackageCertDigest(Signature signature) {
             MessageDigest messageDigest;
@@ -848,6 +910,29 @@
         }
 
         @Override
+        public void startSessionDetailsActivityAsUser(IApplicationThread caller,
+                String callingPackage, SessionInfo sessionInfo, Rect sourceBounds,
+                Bundle opts, UserHandle userHandle) throws RemoteException {
+            int userId = userHandle.getIdentifier();
+            if (!canAccessProfile(userId, "Cannot start details activity")) {
+                return;
+            }
+
+            Intent i = new Intent(Intent.ACTION_VIEW)
+                    .setData(new Uri.Builder()
+                            .scheme("market")
+                            .authority("details")
+                            .appendQueryParameter("id", sessionInfo.appPackageName)
+                            .build())
+                    .putExtra(Intent.EXTRA_REFERRER, new Uri.Builder().scheme("android-app")
+                            .authority(callingPackage).build());
+            i.setSourceBounds(sourceBounds);
+
+            mActivityTaskManagerInternal.startActivityAsUser(caller, callingPackage, i, opts,
+                    userId);
+        }
+
+        @Override
         public void startActivityAsUser(IApplicationThread caller, String callingPackage,
                 ComponentName component, Rect sourceBounds,
                 Bundle opts, UserHandle user) throws RemoteException {
diff --git a/services/core/java/com/android/server/pm/OWNERS b/services/core/java/com/android/server/pm/OWNERS
index 60d7925..33b8641 100644
--- a/services/core/java/com/android/server/pm/OWNERS
+++ b/services/core/java/com/android/server/pm/OWNERS
@@ -51,6 +51,8 @@
 per-file UserManagerService.java = yamasani@google.com
 per-file UserRestrictionsUtils.java = omakoto@google.com
 per-file UserRestrictionsUtils.java = yamasani@google.com
+per-file UserRestrictionsUtils.java = rubinxu@google.com
+per-file UserRestrictionsUtils.java = sandness@google.com
 
 # security
 per-file KeySetHandle.java = cbrubaker@google.com
diff --git a/services/core/java/com/android/server/pm/PackageDexOptimizer.java b/services/core/java/com/android/server/pm/PackageDexOptimizer.java
index 94b1b36..5b38208 100644
--- a/services/core/java/com/android/server/pm/PackageDexOptimizer.java
+++ b/services/core/java/com/android/server/pm/PackageDexOptimizer.java
@@ -507,7 +507,7 @@
         int flags = info.flags;
         boolean vmSafeMode = (flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0;
         // When an app or priv app is configured to run out of box, only verify it.
-        if (info.isCodeIntegrityPreferred()
+        if (info.isEmbeddedDexUsed()
                 || (info.isPrivilegedApp()
                     && DexManager.isPackageSelectedToRunOob(info.packageName))) {
             return "verify";
diff --git a/services/core/java/com/android/server/pm/PackageInstallerService.java b/services/core/java/com/android/server/pm/PackageInstallerService.java
index 146a2f3..af50c87 100644
--- a/services/core/java/com/android/server/pm/PackageInstallerService.java
+++ b/services/core/java/com/android/server/pm/PackageInstallerService.java
@@ -59,7 +59,6 @@
 import android.os.RemoteCallbackList;
 import android.os.RemoteException;
 import android.os.SELinux;
-import android.os.UserHandle;
 import android.os.UserManager;
 import android.os.storage.StorageManager;
 import android.system.ErrnoException;
@@ -107,6 +106,7 @@
 import java.util.List;
 import java.util.Objects;
 import java.util.Random;
+import java.util.function.IntPredicate;
 
 /** The service responsible for installing packages. */
 public class PackageInstallerService extends IPackageInstaller.Stub implements
@@ -204,13 +204,17 @@
         mSessionsDir = new File(Environment.getDataSystemDirectory(), "install_sessions");
         mSessionsDir.mkdirs();
 
-        mStagingManager = new StagingManager(pm);
+        mStagingManager = new StagingManager(pm, this);
     }
 
     private void setBootCompleted()  {
         mBootCompleted = true;
     }
 
+    boolean isBootCompleted()  {
+        return mBootCompleted;
+    }
+
     public void systemReady() {
         mAppOps = mContext.getSystemService(AppOpsManager.class);
 
@@ -245,13 +249,24 @@
             // the updated information.
             writeSessionsLocked();
 
+        }
+    }
+
+    void restoreAndApplyStagedSessionIfNeeded() {
+        List<PackageInstallerSession> stagedSessionsToRestore = new ArrayList<>();
+        synchronized (mSessions) {
             for (int i = 0; i < mSessions.size(); i++) {
                 final PackageInstallerSession session = mSessions.valueAt(i);
                 if (session.isStaged()) {
-                    mStagingManager.restoreSession(session);
+                    stagedSessionsToRestore.add(session);
                 }
             }
         }
+        // Don't hold mSessions lock when calling restoreSession, since it might trigger an APK
+        // atomic install which needs to query sessions, which requires lock on mSessions.
+        for (PackageInstallerSession session : stagedSessionsToRestore) {
+            mStagingManager.restoreSession(session);
+        }
     }
 
     @GuardedBy("mSessions")
@@ -804,7 +819,14 @@
     public void registerCallback(IPackageInstallerCallback callback, int userId) {
         mPermissionManager.enforceCrossUserPermission(
                 Binder.getCallingUid(), userId, true, false, "registerCallback");
-        mCallbacks.register(callback, userId);
+        registerCallback(callback, eventUserId -> userId == eventUserId);
+    }
+
+    /**
+     * Assume permissions already checked and caller's identity cleared
+     */
+    public void registerCallback(IPackageInstallerCallback callback, IntPredicate userCheck) {
+        mCallbacks.register(callback, userCheck);
     }
 
     @Override
@@ -1026,8 +1048,8 @@
             super(looper);
         }
 
-        public void register(IPackageInstallerCallback callback, int userId) {
-            mCallbacks.register(callback, new UserHandle(userId));
+        public void register(IPackageInstallerCallback callback, IntPredicate userCheck) {
+            mCallbacks.register(callback, userCheck);
         }
 
         public void unregister(IPackageInstallerCallback callback) {
@@ -1040,9 +1062,8 @@
             final int n = mCallbacks.beginBroadcast();
             for (int i = 0; i < n; i++) {
                 final IPackageInstallerCallback callback = mCallbacks.getBroadcastItem(i);
-                final UserHandle user = (UserHandle) mCallbacks.getBroadcastCookie(i);
-                // TODO: dispatch notifications for slave profiles
-                if (userId == user.getIdentifier()) {
+                final IntPredicate userCheck = (IntPredicate) mCallbacks.getBroadcastCookie(i);
+                if (userCheck.test(userId)) {
                     try {
                         invokeCallback(callback, msg);
                     } catch (RemoteException ignored) {
diff --git a/services/core/java/com/android/server/pm/PackageInstallerSession.java b/services/core/java/com/android/server/pm/PackageInstallerSession.java
index 494ec3f..dc6751c 100644
--- a/services/core/java/com/android/server/pm/PackageInstallerSession.java
+++ b/services/core/java/com/android/server/pm/PackageInstallerSession.java
@@ -473,6 +473,7 @@
         final SessionInfo info = new SessionInfo();
         synchronized (mLock) {
             info.sessionId = sessionId;
+            info.userId = userId;
             info.installerPackageName = mInstallerPackageName;
             info.resolvedBaseCodePath = (mResolvedBaseFile != null) ?
                     mResolvedBaseFile.getAbsolutePath() : null;
@@ -1562,12 +1563,12 @@
                 }
             }
         }
-        if (baseApk.preferCodeIntegrity) {
+        if (baseApk.useEmbeddedDex) {
             for (File file : mResolvedStagedFiles) {
                 if (file.getName().endsWith(".apk")
-                        && !DexManager.auditUncompressedCodeInApk(file.getPath())) {
+                        && !DexManager.auditUncompressedDexInApk(file.getPath())) {
                     throw new PackageManagerException(INSTALL_FAILED_INVALID_APK,
-                            "Some code are not uncompressed and aligned correctly for "
+                            "Some dex are not uncompressed and aligned correctly for "
                             + mPackageName);
                 }
             }
@@ -1663,6 +1664,12 @@
         }
     }
 
+    String getInstallerPackageName() {
+        synchronized (mLock) {
+            return mInstallerPackageName;
+        }
+    }
+
     private static String getRelativePath(File file, File base) throws IOException {
         final String pathStr = file.getAbsolutePath();
         final String baseStr = base.getAbsolutePath();
@@ -1963,7 +1970,7 @@
 
         // Send broadcast to default launcher only if it's a new install
         final boolean isNewInstall = extras == null || !extras.getBoolean(Intent.EXTRA_REPLACING);
-        if (success && isNewInstall) {
+        if (success && isNewInstall && mPm.mInstallerService.isBootCompleted()) {
             mPm.sendSessionCommitBroadcast(generateInfo(), userId);
         }
 
diff --git a/services/core/java/com/android/server/pm/PackageManagerService.java b/services/core/java/com/android/server/pm/PackageManagerService.java
index d2547d9..e370e6b 100644
--- a/services/core/java/com/android/server/pm/PackageManagerService.java
+++ b/services/core/java/com/android/server/pm/PackageManagerService.java
@@ -7889,6 +7889,8 @@
                 }
             }
             if (listApex) {
+                // TODO(b/119767311): include uninstalled/inactive APEX if
+                //  MATCH_UNINSTALLED_PACKAGES is set.
                 final IApexService apex = IApexService.Stub.asInterface(
                         ServiceManager.getService("apexservice"));
                 if (apex != null) {
@@ -13883,6 +13885,9 @@
 
             final String packageName = res.pkg.applicationInfo.packageName;
             final String seInfo = res.pkg.applicationInfo.seInfo;
+            final int[] allUsers = sUserManager.getUserIds();
+            final int[] installedUsers;
+
             final PackageSetting ps;
             int appId = -1;
             long ceDataInode = -1;
@@ -13892,11 +13897,16 @@
                     appId = ps.appId;
                     ceDataInode = ps.getCeDataInode(userId);
                 }
+
+                // NOTE: We ignore the user specified in the InstallParam because we know this is
+                // an update, and hence need to restore data for all installed users.
+                installedUsers = ps.queryInstalledUsers(allUsers, true);
             }
 
             if (ps != null) {
                 try {
-                    rm.restoreUserData(packageName, userId, appId, ceDataInode, seInfo, token);
+                    rm.restoreUserData(packageName, installedUsers, appId, ceDataInode,
+                            seInfo, token);
                 } catch (RemoteException re) {
                     // Cannot happen, the RollbackManager is hosted in the same process.
                 }
@@ -14593,6 +14603,17 @@
                             TRACE_TAG_PACKAGE_MANAGER, "enable_rollback", enableRollbackToken);
                     mPendingEnableRollback.append(enableRollbackToken, this);
 
+                    final int[] installedUsers;
+                    synchronized (mPackages) {
+                        PackageSetting ps = mSettings.getPackageLPr(pkgLite.packageName);
+                        if (ps != null) {
+                            installedUsers = ps.queryInstalledUsers(sUserManager.getUserIds(),
+                                    true);
+                        } else {
+                            installedUsers = new int[0];
+                        }
+                    }
+
                     // TODO(ruhler) b/112431924: What user? Test for multi-user.
                     Intent enableRollbackIntent = new Intent(Intent.ACTION_PACKAGE_ENABLE_ROLLBACK);
                     enableRollbackIntent.putExtra(
@@ -14603,7 +14624,7 @@
                             installFlags);
                     enableRollbackIntent.putExtra(
                             PackageManagerInternal.EXTRA_ENABLE_ROLLBACK_INSTALLED_USERS,
-                            resolveUserIds(args.user.getIdentifier()));
+                            installedUsers);
                     enableRollbackIntent.setDataAndType(Uri.fromFile(new File(origin.resolvedPath)),
                             PACKAGE_MIME_TYPE);
                     enableRollbackIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
@@ -20733,6 +20754,11 @@
         }
 
         mModuleInfoProvider.systemReady();
+
+        // Installer service might attempt to install some packages that have been staged for
+        // installation on reboot. Make sure this is the last component to be call since the
+        // installation might require other components to be ready.
+        mInstallerService.restoreAndApplyStagedSessionIfNeeded();
     }
 
     public void waitForAppDataPrepared() {
diff --git a/services/core/java/com/android/server/pm/StagingManager.java b/services/core/java/com/android/server/pm/StagingManager.java
index c4d27e5..84fd01a 100644
--- a/services/core/java/com/android/server/pm/StagingManager.java
+++ b/services/core/java/com/android/server/pm/StagingManager.java
@@ -21,6 +21,10 @@
 import android.apex.ApexInfoList;
 import android.apex.ApexSessionInfo;
 import android.apex.IApexService;
+import android.content.IIntentReceiver;
+import android.content.IIntentSender;
+import android.content.Intent;
+import android.content.IntentSender;
 import android.content.pm.PackageInstaller;
 import android.content.pm.PackageInstaller.SessionInfo;
 import android.content.pm.PackageManager;
@@ -29,7 +33,10 @@
 import android.content.pm.PackageParser.SigningDetails.SignatureSchemeVersion;
 import android.content.pm.ParceledListSlice;
 import android.content.pm.Signature;
+import android.os.Bundle;
 import android.os.Handler;
+import android.os.IBinder;
+import android.os.ParcelFileDescriptor;
 import android.os.RemoteException;
 import android.os.ServiceManager;
 import android.text.TextUtils;
@@ -40,9 +47,13 @@
 import com.android.internal.annotations.GuardedBy;
 import com.android.internal.os.BackgroundThread;
 
+import java.io.File;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 
 /**
@@ -53,14 +64,16 @@
 
     private static final String TAG = "StagingManager";
 
+    private final PackageInstallerService mPi;
     private final PackageManagerService mPm;
     private final Handler mBgHandler;
 
     @GuardedBy("mStagedSessions")
     private final SparseArray<PackageInstallerSession> mStagedSessions = new SparseArray<>();
 
-    StagingManager(PackageManagerService pm) {
+    StagingManager(PackageManagerService pm, PackageInstallerService pi) {
         mPm = pm;
+        mPi = pi;
         mBgHandler = BackgroundThread.getHandler();
     }
 
@@ -85,7 +98,7 @@
         return new ParceledListSlice<>(result);
     }
 
-    private static boolean validateApexSignatureLocked(String apexPath, String packageName) {
+    private static boolean validateApexSignature(String apexPath, String packageName) {
         final SigningDetails signingDetails;
         try {
             signingDetails = ApkSignatureVerifier.verify(apexPath, SignatureSchemeVersion.JAR);
@@ -173,6 +186,13 @@
     private void preRebootVerification(@NonNull PackageInstallerSession session) {
         boolean success = true;
 
+        if (!sessionContainsApex(session)) {
+            // TODO: Decide whether we want to fail fast by detecting signature mismatches for APKs,
+            // right away.
+            session.setStagedSessionReady();
+            return;
+        }
+
         final ApexInfoList apexInfoList = new ApexInfoList();
         // APEX checks. For single-package sessions, check if they contain an APEX. For
         // multi-package sessions, find all the child sessions that contain an APEX.
@@ -199,16 +219,13 @@
                     "APEX staging failed, check logcat messages from apexd for more details.");
         }
 
-        if (apexInfoList.apexInfos.length > 0) {
+        if (apexInfoList.apexInfos != null && apexInfoList.apexInfos.length > 0) {
             // For APEXes, we validate the signature here before we mark the session as ready,
             // so we fail the session early if there is a signature mismatch. For APKs, the
             // signature verification will be done by the package manager at the point at which
             // it applies the staged install.
-            //
-            // TODO: Decide whether we want to fail fast by detecting signature mismatches for APKs,
-            // right away.
             for (ApexInfo apexPackage : apexInfoList.apexInfos) {
-                if (!validateApexSignatureLocked(apexPackage.packagePath,
+                if (!validateApexSignature(apexPackage.packagePath,
                         apexPackage.packageName)) {
                     session.setStagedSessionFailed(SessionInfo.VERIFICATION_FAILED,
                             "APK-container signature verification failed for package "
@@ -229,35 +246,182 @@
         }
     }
 
+    private boolean sessionContainsApex(@NonNull PackageInstallerSession session) {
+        if (!session.isMultiPackage()) {
+            return isApexSession(session);
+        }
+        synchronized (mStagedSessions) {
+            return !(Arrays.stream(session.getChildSessionIds())
+                    // Retrieve cached sessions matching ids.
+                    .mapToObj(i -> mStagedSessions.get(i))
+                    // Filter only the ones containing APEX.
+                    .filter(childSession -> isApexSession(childSession))
+                    .collect(Collectors.toList())
+                    .isEmpty());
+        }
+    }
+
     private void resumeSession(@NonNull PackageInstallerSession session) {
-        // Check with apexservice whether the apex
-        // packages have been activated.
-        final IApexService apex = IApexService.Stub.asInterface(
-                ServiceManager.getService("apexservice"));
-        ApexSessionInfo apexSessionInfo;
-        try {
-            apexSessionInfo = apex.getStagedSessionInfo(session.sessionId);
-        } catch (RemoteException re) {
-            Slog.e(TAG, "Unable to contact apexservice", re);
-            // TODO should we retry here? Mark the session as failed?
+        if (sessionContainsApex(session)) {
+            // Check with apexservice whether the apex
+            // packages have been activated.
+            final IApexService apex = IApexService.Stub.asInterface(
+                    ServiceManager.getService("apexservice"));
+            ApexSessionInfo apexSessionInfo;
+            try {
+                apexSessionInfo = apex.getStagedSessionInfo(session.sessionId);
+            } catch (RemoteException re) {
+                Slog.e(TAG, "Unable to contact apexservice", re);
+                // TODO should we retry here? Mark the session as failed?
+                return;
+            }
+            if (apexSessionInfo.isActivationFailed || apexSessionInfo.isUnknown) {
+                session.setStagedSessionFailed(SessionInfo.ACTIVATION_FAILED,
+                        "APEX activation failed. Check logcat messages from apexd for "
+                                + "more information.");
+                return;
+            }
+            if (apexSessionInfo.isVerified) {
+                // Session has been previously submitted to apexd, but didn't complete all the
+                // pre-reboot verification, perhaps because the device rebooted in the meantime.
+                // Greedily re-trigger the pre-reboot verification.
+                Slog.d(TAG, "Found pending staged session " + session.sessionId + " still to be "
+                        + "verified, resuming pre-reboot verification");
+                mBgHandler.post(() -> preRebootVerification(session));
+                return;
+            }
+            if (!apexSessionInfo.isActivated) {
+                // In all the remaining cases apexd will try to apply the session again at next
+                // boot. Nothing to do here for now.
+                Slog.w(TAG, "Staged session " + session.sessionId + " scheduled to be applied "
+                        + "at boot didn't activate nor fail. This usually means that apexd will "
+                        + "retry at next reboot.");
+                return;
+            }
+        }
+        // The APEX part of the session is activated, proceed with the installation of APKs.
+        if (!installApksInSession(session)) {
+            session.setStagedSessionFailed(SessionInfo.ACTIVATION_FAILED,
+                    "APK installation for staged session " + session.sessionId + " failed.");
             return;
         }
-        if (apexSessionInfo.isActivationFailed || apexSessionInfo.isUnknown) {
-            session.setStagedSessionFailed(SessionInfo.ACTIVATION_FAILED,
-                    "APEX activation failed. Check logcat messages from apexd for "
-                                  + "more information.");
+        session.setStagedSessionApplied();
+    }
+
+    private String findFirstAPKInDir(File stageDir) {
+        if (stageDir != null && stageDir.exists()) {
+            for (File file : stageDir.listFiles()) {
+                if (file.getAbsolutePath().toLowerCase().endsWith(".apk")) {
+                    return file.getAbsolutePath();
+                }
+            }
         }
-        if (apexSessionInfo.isVerified) {
-            // Session has been previously submitted to apexd, but didn't complete all the
-            // pre-reboot verification, perhaps because the device rebooted in the meantime.
-            // Greedily re-trigger the pre-reboot verification.
-            mBgHandler.post(() -> preRebootVerification(session));
+        return null;
+    }
+
+    private PackageInstallerSession createAndWriteApkSession(
+            @NonNull PackageInstallerSession originalSession) {
+        // TODO(b/123629153): support split APKs.
+        if (originalSession.stageDir == null) {
+            Slog.wtf(TAG, "Attempting to install a staged APK session with no staging dir");
+            return null;
         }
-        if (apexSessionInfo.isActivated) {
-            session.setStagedSessionApplied();
-            // TODO(b/118865310) if multi-package proceed with the installation of APKs.
+        String apkFilePath = findFirstAPKInDir(originalSession.stageDir);
+        if (apkFilePath == null) {
+            Slog.w(TAG, "Can't find staged APK in " + originalSession.stageDir.getAbsolutePath());
+            return null;
         }
-        // In every other case apexd will retry to apply the session at next boot.
+        File apkFile = new File(apkFilePath);
+
+        PackageInstaller.SessionParams params = originalSession.params.copy();
+        params.isStaged = false;
+        int apkSessionId = mPi.createSession(
+                params, originalSession.getInstallerPackageName(), originalSession.userId);
+        PackageInstallerSession apkSession = mPi.getSession(apkSessionId);
+
+        try {
+            apkSession.open();
+            ParcelFileDescriptor pfd = ParcelFileDescriptor.open(apkFile,
+                    ParcelFileDescriptor.MODE_READ_ONLY);
+            long sizeBytes = pfd.getStatSize();
+            if (sizeBytes < 0) {
+                Slog.e(TAG, "Unable to get size of: " + apkFilePath);
+                return null;
+            }
+            apkSession.write(apkFile.getName(), 0, sizeBytes, pfd);
+        } catch (IOException e) {
+            Slog.e(TAG, "Failure to install APK staged session " + originalSession.sessionId, e);
+            return null;
+        }
+        return apkSession;
+    }
+
+    private boolean commitApkSession(@NonNull PackageInstallerSession apkSession,
+                                     int originalSessionId) {
+        final LocalIntentReceiver receiver = new LocalIntentReceiver();
+        apkSession.commit(receiver.getIntentSender(), false);
+        final Intent result = receiver.getResult();
+        final int status = result.getIntExtra(PackageInstaller.EXTRA_STATUS,
+                PackageInstaller.STATUS_FAILURE);
+        if (status == PackageInstaller.STATUS_SUCCESS) {
+            return true;
+        }
+        Slog.e(TAG, "Failure to install APK staged session " + originalSessionId + " ["
+                + result.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE) + "]");
+        return false;
+    }
+
+    private boolean installApksInSession(@NonNull PackageInstallerSession session) {
+        if (!session.isMultiPackage() && !isApexSession(session)) {
+            // APK single-packaged staged session. Do a regular install.
+            PackageInstallerSession apkSession = createAndWriteApkSession(session);
+            if (apkSession == null) {
+                return false;
+            }
+            return commitApkSession(apkSession, session.sessionId);
+        } else if (session.isMultiPackage()) {
+            // For multi-package staged sessions containing APKs, we identify which child sessions
+            // contain an APK, and with those then create a new multi-package group of sessions,
+            // carrying over all the session parameters and unmarking them as staged. On commit the
+            // sessions will be installed atomically.
+            List<PackageInstallerSession> childSessions;
+            synchronized (mStagedSessions) {
+                childSessions =
+                        Arrays.stream(session.getChildSessionIds())
+                                // Retrieve cached sessions matching ids.
+                                .mapToObj(i -> mStagedSessions.get(i))
+                                // Filter only the ones containing APKs.s
+                                .filter(childSession -> !isApexSession(childSession))
+                                .collect(Collectors.toList());
+            }
+            if (childSessions.isEmpty()) {
+                // APEX-only multi-package staged session, nothing to do.
+                return true;
+            }
+            PackageInstaller.SessionParams params = session.params.copy();
+            params.isStaged = false;
+            int apkParentSessionId = mPi.createSession(
+                    params, session.getInstallerPackageName(), session.userId);
+            PackageInstallerSession apkParentSession = mPi.getSession(apkParentSessionId);
+            try {
+                apkParentSession.open();
+            } catch (IOException e) {
+                Slog.e(TAG, "Unable to prepare multi-package session for staged session "
+                        + session.sessionId);
+                return false;
+            }
+
+            for (PackageInstallerSession sessionToClone : childSessions) {
+                PackageInstallerSession apkChildSession = createAndWriteApkSession(sessionToClone);
+                if (apkChildSession == null) {
+                    return false;
+                }
+                apkParentSession.addChildSessionId(apkChildSession.sessionId);
+            }
+            return commitApkSession(apkParentSession, session.sessionId);
+        }
+        // APEX single-package staged session, nothing to do.
+        return true;
     }
 
     void commitSession(@NonNull PackageInstallerSession session) {
@@ -336,9 +500,36 @@
         } else {
             // Session had already being marked ready. Start the checks to verify if there is any
             // follow-up work.
-            // TODO(b/118865310): should this be synchronous to ensure it completes before
-            //                    systemReady() finishes?
-            mBgHandler.post(() -> resumeSession(session));
+            resumeSession(session);
+        }
+    }
+
+    private static class LocalIntentReceiver {
+        private final LinkedBlockingQueue<Intent> mResult = new LinkedBlockingQueue<>();
+
+        private IIntentSender.Stub mLocalSender = new IIntentSender.Stub() {
+            @Override
+            public void send(int code, Intent intent, String resolvedType, IBinder whitelistToken,
+                             IIntentReceiver finishedReceiver, String requiredPermission,
+                             Bundle options) {
+                try {
+                    mResult.offer(intent, 5, TimeUnit.SECONDS);
+                } catch (InterruptedException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+        };
+
+        public IntentSender getIntentSender() {
+            return new IntentSender((IIntentSender) mLocalSender);
+        }
+
+        public Intent getResult() {
+            try {
+                return mResult.take();
+            } catch (InterruptedException e) {
+                throw new RuntimeException(e);
+            }
         }
     }
 }
diff --git a/services/core/java/com/android/server/pm/UserManagerService.java b/services/core/java/com/android/server/pm/UserManagerService.java
index 4f20590..d0f192d 100644
--- a/services/core/java/com/android/server/pm/UserManagerService.java
+++ b/services/core/java/com/android/server/pm/UserManagerService.java
@@ -1109,14 +1109,7 @@
 
     @Override
     public int getManagedProfileBadge(@UserIdInt int userId) {
-        int callingUserId = UserHandle.getCallingUserId();
-        if (callingUserId != userId && !hasManageUsersPermission()) {
-            if (!isSameProfileGroupNoChecks(callingUserId, userId)) {
-                throw new SecurityException(
-                        "You need MANAGE_USERS permission to: check if specified user a " +
-                        "managed profile outside your profile group");
-            }
-        }
+        checkManageOrInteractPermIfCallerInOtherProfileGroup(userId, "getManagedProfileBadge");
         synchronized (mUsersLock) {
             UserInfo userInfo = getUserInfoLU(userId);
             return userInfo != null ? userInfo.profileBadge : 0;
@@ -1125,14 +1118,7 @@
 
     @Override
     public boolean isManagedProfile(int userId) {
-        int callingUserId = UserHandle.getCallingUserId();
-        if (callingUserId != userId && !hasManageUsersPermission()) {
-            if (!isSameProfileGroupNoChecks(callingUserId, userId)) {
-                throw new SecurityException(
-                        "You need MANAGE_USERS permission to: check if specified user a " +
-                        "managed profile outside your profile group");
-            }
-        }
+        checkManageOrInteractPermIfCallerInOtherProfileGroup(userId, "isManagedProfile");
         synchronized (mUsersLock) {
             UserInfo userInfo = getUserInfoLU(userId);
             return userInfo != null && userInfo.isManagedProfile();
diff --git a/services/core/java/com/android/server/pm/dex/ArtManagerService.java b/services/core/java/com/android/server/pm/dex/ArtManagerService.java
index 7d2dd65..23705db 100644
--- a/services/core/java/com/android/server/pm/dex/ArtManagerService.java
+++ b/services/core/java/com/android/server/pm/dex/ArtManagerService.java
@@ -480,7 +480,7 @@
             final String apkPath = pkg.baseCodePath;
             final ApplicationInfo appInfo = pkg.applicationInfo;
             final String outDexFile = appInfo.dataDir + "/code_cache/compiled_view.dex";
-            if (appInfo.isPrivilegedApp() || appInfo.isCodeIntegrityPreferred()) {
+            if (appInfo.isPrivilegedApp() || appInfo.isEmbeddedDexUsed()) {
                 // Privileged apps prefer to load trusted code so they don't use compiled views.
                 // If the app is not privileged but prefers code integrity, also avoid compiling
                 // views.
diff --git a/services/core/java/com/android/server/pm/dex/DexManager.java b/services/core/java/com/android/server/pm/dex/DexManager.java
index abbddf3..2213901 100644
--- a/services/core/java/com/android/server/pm/dex/DexManager.java
+++ b/services/core/java/com/android/server/pm/dex/DexManager.java
@@ -740,10 +740,10 @@
     }
 
     /**
-     * Generates log if the archive located at {@code fileName} has uncompressed dex file and so
-     * files that can be direclty mapped.
+     * Generates log if the archive located at {@code fileName} has uncompressed dex file that can
+     * be direclty mapped.
      */
-    public static boolean auditUncompressedCodeInApk(String fileName) {
+    public static boolean auditUncompressedDexInApk(String fileName) {
         StrictJarFile jarFile = null;
         try {
             jarFile = new StrictJarFile(fileName,
@@ -762,16 +762,6 @@
                         Slog.w(TAG, "APK " + fileName + " has unaligned dex code " +
                                 entry.getName());
                     }
-                } else if (entry.getName().endsWith(".so")) {
-                    if (entry.getMethod() != ZipEntry.STORED) {
-                        allCorrect = false;
-                        Slog.w(TAG, "APK " + fileName + " has compressed native code " +
-                                entry.getName());
-                    } else if ((entry.getDataOffset() & (0x1000 - 1)) != 0) {
-                        allCorrect = false;
-                        Slog.w(TAG, "APK " + fileName + " has unaligned native code " +
-                                entry.getName());
-                    }
                 }
             }
             return allCorrect;
diff --git a/services/core/java/com/android/server/power/AttentionDetector.java b/services/core/java/com/android/server/power/AttentionDetector.java
index a2c8dac..4186154 100644
--- a/services/core/java/com/android/server/power/AttentionDetector.java
+++ b/services/core/java/com/android/server/power/AttentionDetector.java
@@ -24,11 +24,13 @@
 import android.os.SystemClock;
 import android.service.attention.AttentionService;
 import android.util.Slog;
+import android.util.StatsLog;
 
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.server.LocalServices;
 
 import java.io.PrintWriter;
+import java.util.concurrent.atomic.AtomicLong;
 
 /**
  * Class responsible for checking if the user is currently paying attention to the phone and
@@ -79,6 +81,11 @@
      */
     private int mWakefulness;
 
+    /**
+     * Describes how many times in a row was the timeout extended.
+     */
+    private AtomicLong mConsecutiveTimeoutExtendedCount = new AtomicLong(0);
+
     @VisibleForTesting
     final AttentionCallbackInternal mCallback = new AttentionCallbackInternal() {
 
@@ -95,6 +102,8 @@
                     }
                     if (result == AttentionService.ATTENTION_SUCCESS_PRESENT) {
                         mOnUserAttention.run();
+                    } else {
+                        resetConsecutiveExtensionCount();
                     }
                 }
             }
@@ -176,6 +185,7 @@
     public int onUserActivity(long eventTime, int event) {
         switch (event) {
             case PowerManager.USER_ACTIVITY_EVENT_ATTENTION:
+                mConsecutiveTimeoutExtendedCount.incrementAndGet();
                 return 0;
             case PowerManager.USER_ACTIVITY_EVENT_OTHER:
             case PowerManager.USER_ACTIVITY_EVENT_BUTTON:
@@ -183,6 +193,7 @@
             case PowerManager.USER_ACTIVITY_EVENT_ACCESSIBILITY:
                 cancelCurrentRequestIfAny();
                 mLastUserActivityTime = eventTime;
+                resetConsecutiveExtensionCount();
                 return 1;
             default:
                 if (DEBUG) {
@@ -196,6 +207,7 @@
         mWakefulness = wakefulness;
         if (wakefulness != PowerManagerInternal.WAKEFULNESS_AWAKE) {
             cancelCurrentRequestIfAny();
+            resetConsecutiveExtensionCount();
         }
     }
 
@@ -206,6 +218,13 @@
         }
     }
 
+    private void resetConsecutiveExtensionCount() {
+        final long previousCount = mConsecutiveTimeoutExtendedCount.getAndSet(0);
+        if (previousCount > 0) {
+            StatsLog.write(StatsLog.SCREEN_TIMEOUT_EXTENSION_REPORTED, previousCount);
+        }
+    }
+
     @VisibleForTesting
     int getRequestCode() {
         return (int) (mLastUserActivityTime % Integer.MAX_VALUE);
diff --git a/services/core/java/com/android/server/power/PowerManagerService.java b/services/core/java/com/android/server/power/PowerManagerService.java
index 3be6480..3ccd234 100644
--- a/services/core/java/com/android/server/power/PowerManagerService.java
+++ b/services/core/java/com/android/server/power/PowerManagerService.java
@@ -22,6 +22,7 @@
 import static android.os.PowerManagerInternal.WAKEFULNESS_DREAMING;
 
 import android.annotation.IntDef;
+import android.annotation.NonNull;
 import android.annotation.UserIdInt;
 import android.app.ActivityManager;
 import android.app.SynchronousUserSwitchObserver;
@@ -42,6 +43,7 @@
 import android.net.Uri;
 import android.os.BatteryManager;
 import android.os.BatteryManagerInternal;
+import android.os.BatterySaverPolicyConfig;
 import android.os.Binder;
 import android.os.Handler;
 import android.os.IBinder;
@@ -2875,8 +2877,7 @@
     @VisibleForTesting
     void updatePowerRequestFromBatterySaverPolicy(DisplayPowerRequest displayPowerRequest) {
         PowerSaveState state = mBatterySaverPolicy.
-                getBatterySaverPolicy(ServiceType.SCREEN_BRIGHTNESS,
-                        mBatterySaverController.isEnabled());
+                getBatterySaverPolicy(ServiceType.SCREEN_BRIGHTNESS);
         displayPowerRequest.lowPowerMode = state.batterySaverEnabled;
         displayPowerRequest.screenLowPowerBrightnessFactor = state.brightnessFactor;
     }
@@ -4451,8 +4452,7 @@
         public PowerSaveState getPowerSaveState(@ServiceType int serviceType) {
             final long ident = Binder.clearCallingIdentity();
             try {
-                return mBatterySaverPolicy.getBatterySaverPolicy(
-                        serviceType, mBatterySaverController.isEnabled());
+                return mBatterySaverPolicy.getBatterySaverPolicy(serviceType);
             } finally {
                 Binder.restoreCallingIdentity(ident);
             }
@@ -4497,6 +4497,36 @@
         }
 
         @Override // Binder call
+        public boolean setAdaptivePowerSavePolicy(@NonNull BatterySaverPolicyConfig config) {
+            if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.POWER_SAVER)
+                    != PackageManager.PERMISSION_GRANTED) {
+                mContext.enforceCallingOrSelfPermission(
+                        android.Manifest.permission.DEVICE_POWER, "setAdaptivePowerSavePolicy");
+            }
+            final long ident = Binder.clearCallingIdentity();
+            try {
+                return mBatterySaverStateMachine.setAdaptiveBatterySaverPolicy(config);
+            } finally {
+                Binder.restoreCallingIdentity(ident);
+            }
+        }
+
+        @Override // Binder call
+        public boolean setAdaptivePowerSaveEnabled(boolean enabled) {
+            if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.POWER_SAVER)
+                    != PackageManager.PERMISSION_GRANTED) {
+                mContext.enforceCallingOrSelfPermission(
+                        android.Manifest.permission.DEVICE_POWER, "setAdaptivePowerSaveEnabled");
+            }
+            final long ident = Binder.clearCallingIdentity();
+            try {
+                return mBatterySaverStateMachine.setAdaptiveBatterySaverEnabled(enabled);
+            } finally {
+                Binder.restoreCallingIdentity(ident);
+            }
+        }
+
+        @Override // Binder call
         public int getPowerSaveMode() {
             mContext.enforceCallingOrSelfPermission(android.Manifest.permission.POWER_SAVER, null);
             final long ident = Binder.clearCallingIdentity();
@@ -4846,8 +4876,7 @@
 
         @Override
         public PowerSaveState getLowPowerState(@ServiceType int serviceType) {
-            return mBatterySaverPolicy.getBatterySaverPolicy(serviceType,
-                    mBatterySaverController.isEnabled());
+            return mBatterySaverPolicy.getBatterySaverPolicy(serviceType);
         }
 
         @Override
diff --git a/services/core/java/com/android/server/power/PowerManagerShellCommand.java b/services/core/java/com/android/server/power/PowerManagerShellCommand.java
index 46115d8..18b8f0e 100644
--- a/services/core/java/com/android/server/power/PowerManagerShellCommand.java
+++ b/services/core/java/com/android/server/power/PowerManagerShellCommand.java
@@ -41,6 +41,8 @@
         final PrintWriter pw = getOutPrintWriter();
         try {
             switch(cmd) {
+                case "set-adaptive-power-saver-enabled":
+                    return runSetAdaptiveEnabled();
                 case "set-mode":
                     return runSetMode();
                 default:
@@ -52,6 +54,11 @@
         return -1;
     }
 
+    private int runSetAdaptiveEnabled() throws RemoteException {
+        mInterface.setAdaptivePowerSaveEnabled(Boolean.parseBoolean(getNextArgRequired()));
+        return 0;
+    }
+
     private int runSetMode() throws RemoteException {
         final PrintWriter pw = getOutPrintWriter();
         int mode = -1;
@@ -72,6 +79,8 @@
         pw.println("  help");
         pw.println("    Print this help text.");
         pw.println("");
+        pw.println("  set-adaptive-power-saver-enabled [true|false]");
+        pw.println("    enables or disables adaptive power saver.");
         pw.println("  set-mode MODE");
         pw.println("    sets the power mode of the device to MODE.");
         pw.println("    1 turns low power mode on and 0 turns low power mode off.");
diff --git a/services/core/java/com/android/server/power/batterysaver/BatterySaverController.java b/services/core/java/com/android/server/power/batterysaver/BatterySaverController.java
index ab2807a..94bb3ea 100644
--- a/services/core/java/com/android/server/power/batterysaver/BatterySaverController.java
+++ b/services/core/java/com/android/server/power/batterysaver/BatterySaverController.java
@@ -23,6 +23,7 @@
 import android.content.IntentFilter;
 import android.hardware.power.V1_0.PowerHint;
 import android.os.BatteryManager;
+import android.os.BatterySaverPolicyConfig;
 import android.os.Handler;
 import android.os.Looper;
 import android.os.Message;
@@ -42,6 +43,7 @@
 import com.android.server.LocalServices;
 import com.android.server.power.PowerManagerService;
 import com.android.server.power.batterysaver.BatterySaverPolicy.BatterySaverPolicyListener;
+import com.android.server.power.batterysaver.BatterySaverPolicy.Policy;
 import com.android.server.power.batterysaver.BatterySavingStats.BatterySaverState;
 import com.android.server.power.batterysaver.BatterySavingStats.DozeState;
 import com.android.server.power.batterysaver.BatterySavingStats.InteractiveState;
@@ -74,16 +76,25 @@
     private final ArrayList<LowPowerModeListener> mListeners = new ArrayList<>();
 
     @GuardedBy("mLock")
-    private boolean mEnabled;
+    private boolean mFullEnabled;
+
+    @GuardedBy("mLock")
+    private boolean mAdaptiveEnabled;
 
     @GuardedBy("mLock")
     private boolean mIsPluggedIn;
 
     /**
-     * Previously enabled or not; only for the event logging. Only use it from
+     * Whether full was previously enabled or not; only for the event logging. Only use it from
      * {@link #handleBatterySaverStateChanged}.
      */
-    private boolean mPreviouslyEnabled;
+    private boolean mFullPreviouslyEnabled;
+
+    /**
+     * Whether adaptive was previously enabled or not; only for the event logging. Only use it from
+     * {@link #handleBatterySaverStateChanged}.
+     */
+    private boolean mAdaptivePreviouslyEnabled;
 
     @GuardedBy("mLock")
     private boolean mIsInteractive;
@@ -104,7 +115,9 @@
     public static final int REASON_SETTING_CHANGED = 8;
     public static final int REASON_DYNAMIC_POWER_SAVINGS_AUTOMATIC_ON = 9;
     public static final int REASON_DYNAMIC_POWER_SAVINGS_AUTOMATIC_OFF = 10;
-    public static final int REASON_STICKY_RESTORE_OFF = 13;
+    public static final int REASON_STICKY_RESTORE_OFF = 11;
+    public static final int REASON_ADAPTIVE_DYNAMIC_POWER_SAVINGS_CHANGED = 12;
+    public static final int REASON_TIMEOUT = 13;
 
     /**
      * Plugin interface. All methods are guaranteed to be called on the same (handler) thread.
@@ -124,7 +137,7 @@
             switch (intent.getAction()) {
                 case Intent.ACTION_SCREEN_ON:
                 case Intent.ACTION_SCREEN_OFF:
-                    if (!isEnabled()) {
+                    if (!isPolicyEnabled()) {
                         updateBatterySavingStats();
                         return; // No need to send it if not enabled.
                     }
@@ -199,7 +212,7 @@
 
     @Override
     public void onBatterySaverPolicyChanged(BatterySaverPolicy policy) {
-        if (!isEnabled()) {
+        if (!isPolicyEnabled()) {
             return; // No need to send it if not enabled.
         }
         mHandler.postStateChanged(/*sendBroadcast=*/ true, REASON_POLICY_CHANGED);
@@ -248,22 +261,98 @@
     @VisibleForTesting
     public void enableBatterySaver(boolean enable, int reason) {
         synchronized (mLock) {
-            if (mEnabled == enable) {
+            if (mFullEnabled == enable) {
                 return;
             }
-            mEnabled = enable;
+            mFullEnabled = enable;
 
-            mHandler.postStateChanged(/*sendBroadcast=*/ true, reason);
+            if (updatePolicyLevelLocked()) {
+                mHandler.postStateChanged(/*sendBroadcast=*/ true, reason);
+            }
         }
     }
 
-    /** @return whether battery saver is enabled or not. */
+    private boolean updatePolicyLevelLocked() {
+        if (mFullEnabled) {
+            return mBatterySaverPolicy.setPolicyLevel(BatterySaverPolicy.POLICY_LEVEL_FULL);
+        } else if (mAdaptiveEnabled) {
+            return mBatterySaverPolicy.setPolicyLevel(BatterySaverPolicy.POLICY_LEVEL_ADAPTIVE);
+        } else {
+            return mBatterySaverPolicy.setPolicyLevel(BatterySaverPolicy.POLICY_LEVEL_OFF);
+        }
+    }
+
+    /**
+     * @return whether battery saver is enabled or not. This takes into
+     * account whether a policy says to advertise isEnabled so this can be propagated externally.
+     */
     public boolean isEnabled() {
         synchronized (mLock) {
-            return mEnabled;
+            return mFullEnabled
+                    || (mAdaptiveEnabled && mBatterySaverPolicy.shouldAdvertiseIsEnabled());
         }
     }
 
+    /**
+     * @return whether battery saver policy is enabled or not. This does not take into account
+     * whether a policy says to advertise isEnabled, so this shouldn't be propagated externally.
+     */
+    private boolean isPolicyEnabled() {
+        synchronized (mLock) {
+            return mFullEnabled || mAdaptiveEnabled;
+        }
+    }
+
+    boolean isFullEnabled() {
+        synchronized (mLock) {
+            return mFullEnabled;
+        }
+    }
+
+    boolean isAdaptiveEnabled() {
+        synchronized (mLock) {
+            return mAdaptiveEnabled;
+        }
+    }
+
+    boolean setAdaptivePolicyLocked(String settings, String deviceSpecificSettings, int reason) {
+        return setAdaptivePolicyLocked(
+                BatterySaverPolicy.Policy.fromSettings(settings, deviceSpecificSettings),
+                reason);
+    }
+
+    boolean setAdaptivePolicyLocked(BatterySaverPolicyConfig config, int reason) {
+        return setAdaptivePolicyLocked(BatterySaverPolicy.Policy.fromConfig(config), reason);
+    }
+
+    boolean setAdaptivePolicyLocked(Policy policy, int reason) {
+        if (mBatterySaverPolicy.setAdaptivePolicyLocked(policy)) {
+            mHandler.postStateChanged(/*sendBroadcast=*/ true, reason);
+            return true;
+        }
+        return false;
+    }
+
+    boolean resetAdaptivePolicyLocked(int reason) {
+        if (mBatterySaverPolicy.resetAdaptivePolicyLocked()) {
+            mHandler.postStateChanged(/*sendBroadcast=*/ true, reason);
+            return true;
+        }
+        return false;
+    }
+
+    boolean setAdaptivePolicyEnabledLocked(boolean enabled, int reason) {
+        if (mAdaptiveEnabled == enabled) {
+            return false;
+        }
+        mAdaptiveEnabled = enabled;
+        if (updatePolicyLevelLocked()) {
+            mHandler.postStateChanged(/*sendBroadcast=*/ true, reason);
+            return true;
+        }
+        return false;
+    }
+
     /** @return whether device is in interactive state. */
     public boolean isInteractive() {
         synchronized (mLock) {
@@ -280,7 +369,7 @@
      * @return true if launch boost should currently be disabled.
      */
     public boolean isLaunchBoostDisabled() {
-        return isEnabled() && mBatterySaverPolicy.isLaunchBoostDisabled();
+        return isPolicyEnabled() && mBatterySaverPolicy.isLaunchBoostDisabled();
     }
 
     /**
@@ -293,6 +382,9 @@
      * - When battery saver becomes deactivated.
      * - When battery saver is on and the interactive state changes.
      * - When battery saver is on and the battery saver policy changes.
+     * - When adaptive battery saver becomes activated.
+     * - When adaptive battery saver becomes deactivated.
+     * - When adaptive battery saver policy changes.
      */
     void handleBatterySaverStateChanged(boolean sendBroadcast, int reason) {
         final LowPowerModeListener[] listeners;
@@ -302,17 +394,22 @@
         final ArrayMap<String, String> fileValues;
 
         synchronized (mLock) {
+            enabled = mFullEnabled || mAdaptiveEnabled;
+
             EventLogTags.writeBatterySaverMode(
-                    mPreviouslyEnabled ? 1 : 0, // Previously off or on.
-                    mEnabled ? 1 : 0, // Now off or on.
+                    mFullPreviouslyEnabled ? 1 : 0, // Previously off or on.
+                    mAdaptivePreviouslyEnabled ? 1 : 0, // Previously off or on.
+                    mFullEnabled ? 1 : 0, // Now off or on.
+                    mAdaptiveEnabled ? 1 : 0, // Now off or on.
                     isInteractive ?  1 : 0, // Device interactive state.
-                    mEnabled ? mBatterySaverPolicy.toEventLogString() : "",
+                    enabled ? mBatterySaverPolicy.toEventLogString() : "",
                     reason);
-            mPreviouslyEnabled = mEnabled;
+
+            mFullPreviouslyEnabled = mFullEnabled;
+            mAdaptivePreviouslyEnabled = mAdaptiveEnabled;
 
             listeners = mListeners.toArray(new LowPowerModeListener[0]);
 
-            enabled = mEnabled;
             mIsInteractive = isInteractive;
 
             if (enabled) {
@@ -324,7 +421,7 @@
 
         final PowerManagerInternal pmi = LocalServices.getService(PowerManagerInternal.class);
         if (pmi != null) {
-            pmi.powerHint(PowerHint.LOW_POWER, enabled ? 1 : 0);
+            pmi.powerHint(PowerHint.LOW_POWER, isEnabled() ? 1 : 0);
         }
 
         updateBatterySavingStats();
@@ -342,13 +439,13 @@
         if (sendBroadcast) {
 
             if (DEBUG) {
-                Slog.i(TAG, "Sending broadcasts for mode: " + enabled);
+                Slog.i(TAG, "Sending broadcasts for mode: " + isEnabled());
             }
 
             // Send the broadcasts and notify the listeners. We only do this when the battery saver
             // mode changes, but not when only the screen state changes.
             Intent intent = new Intent(PowerManager.ACTION_POWER_SAVE_MODE_CHANGING)
-                    .putExtra(PowerManager.EXTRA_POWER_SAVE_MODE, enabled)
+                    .putExtra(PowerManager.EXTRA_POWER_SAVE_MODE, isEnabled())
                     .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
             mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
 
@@ -364,8 +461,7 @@
 
             for (LowPowerModeListener listener : listeners) {
                 final PowerSaveState result =
-                        mBatterySaverPolicy.getBatterySaverPolicy(
-                                listener.getServiceType(), enabled);
+                        mBatterySaverPolicy.getBatterySaverPolicy(listener.getServiceType());
                 listener.onLowPowerModeChanged(result);
             }
         }
@@ -389,7 +485,8 @@
                 return;
             }
             mBatterySavingStats.transitionState(
-                    mEnabled ? BatterySaverState.ON : BatterySaverState.OFF,
+                    mFullEnabled ? BatterySaverState.ON :
+                            (mAdaptiveEnabled ? BatterySaverState.ADAPTIVE : BatterySaverState.OFF),
                     isInteractive ? InteractiveState.INTERACTIVE : InteractiveState.NON_INTERACTIVE,
                     dozeMode);
         }
diff --git a/services/core/java/com/android/server/power/batterysaver/BatterySaverLocationPlugin.java b/services/core/java/com/android/server/power/batterysaver/BatterySaverLocationPlugin.java
index bd8baeb..a77d133 100644
--- a/services/core/java/com/android/server/power/batterysaver/BatterySaverLocationPlugin.java
+++ b/services/core/java/com/android/server/power/batterysaver/BatterySaverLocationPlugin.java
@@ -53,8 +53,8 @@
     private void updateLocationState(BatterySaverController caller) {
         final boolean kill =
                 (caller.getBatterySaverPolicy().getGpsMode()
-                        == PowerManager.LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF) &&
-                caller.isEnabled() && !caller.isInteractive();
+                        == PowerManager.LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF)
+                        && !caller.isInteractive();
 
         if (DEBUG) {
             Slog.d(TAG, "Battery saver " + (kill ? "stopping" : "restoring") + " location.");
diff --git a/services/core/java/com/android/server/power/batterysaver/BatterySaverPolicy.java b/services/core/java/com/android/server/power/batterysaver/BatterySaverPolicy.java
index 48a041e..8550bc3 100644
--- a/services/core/java/com/android/server/power/batterysaver/BatterySaverPolicy.java
+++ b/services/core/java/com/android/server/power/batterysaver/BatterySaverPolicy.java
@@ -15,16 +15,17 @@
  */
 package com.android.server.power.batterysaver;
 
+import android.annotation.IntDef;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.database.ContentObserver;
 import android.net.Uri;
+import android.os.BatterySaverPolicyConfig;
 import android.os.Handler;
 import android.os.PowerManager;
 import android.os.PowerManager.ServiceType;
 import android.os.PowerSaveState;
 import android.provider.Settings;
-import android.provider.Settings.Global;
 import android.text.TextUtils;
 import android.util.ArrayMap;
 import android.util.KeyValueListParser;
@@ -39,8 +40,12 @@
 import com.android.server.power.PowerManagerService;
 
 import java.io.PrintWriter;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
+import java.util.Objects;
 
 /**
  * Class to decide whether to turn on battery saver mode for specific services.
@@ -48,12 +53,12 @@
  * IMPORTANT: This class shares the power manager lock, which is very low in the lock hierarchy.
  * Do not call out with the lock held, such as AccessibilityManager. (Settings provider is okay.)
  *
- * Test: atest com.android.server.power.batterysaver.BatterySaverPolicyTest.java
+ * Test: atest com.android.server.power.batterysaver.BatterySaverPolicyTest
  */
 public class BatterySaverPolicy extends ContentObserver {
     private static final String TAG = "BatterySaverPolicy";
 
-    public static final boolean DEBUG = false; // DO NOT SUBMIT WITH TRUE.
+    static final boolean DEBUG = false; // DO NOT SUBMIT WITH TRUE.
 
     private static final String KEY_GPS_MODE = "gps_mode";
     private static final String KEY_VIBRATION_DISABLED = "vibration_disabled";
@@ -81,6 +86,14 @@
      * If set to true, Data Saver WILL NOT be turned on when Battery Saver is turned on.
      */
     private static final String KEY_ACTIVATE_DATASAVER_DISABLED = "datasaver_disabled";
+
+    /**
+     * {@code true} if the Policy should advertise to the rest of the system that battery saver
+     * is enabled. This advertising could cause other system components to change their
+     * behavior. This will not affect other policy flags and what they change.
+     */
+    private static final String KEY_ADVERTISE_IS_ENABLED = "advertise_is_enabled";
+
     private static final String KEY_LAUNCH_BOOST_DISABLED = "launch_boost_disabled";
     private static final String KEY_ADJUST_BRIGHTNESS_FACTOR = "adjust_brightness_factor";
     private static final String KEY_FULLBACKUP_DEFERRED = "fullbackup_deferred";
@@ -96,8 +109,35 @@
     private static final String KEY_CPU_FREQ_INTERACTIVE = "cpufreq-i";
     private static final String KEY_CPU_FREQ_NONINTERACTIVE = "cpufreq-n";
 
-    private static final Policy sDefaultPolicy = new Policy(
+    @VisibleForTesting
+    static final Policy OFF_POLICY = new Policy(
+            1f,    /* adjustBrightnessFactor */
+            false, /* advertiseIsEnabled */
+            false, /* deferFullBackup */
+            false, /* deferKeyValueBackup */
+            false, /* disableAnimation */
+            false, /* disableAod */
+            false, /* disableLaunchBoost */
+            false, /* disableOptionalSensors */
+            false, /* disableSoundTrigger */
+            false, /* disableVibration */
+            false, /* enableAdjustBrightness */
+            false, /* enableDataSaver */
+            false, /* enableFireWall */
+            false, /* enableQuickDoze */
+            new ArrayMap<>(), /* filesForInteractive */
+            new ArrayMap<>(), /* filesForNoninteractive */
+            false, /* forceAllAppsStandby */
+            false, /* forceBackgroundCheck */
+            PowerManager.LOCATION_MODE_NO_CHANGE, /* gpsMode */
+            false  /* sendTronLog */
+    );
+
+    private static final Policy DEFAULT_ADAPTIVE_POLICY = OFF_POLICY;
+
+    private static final Policy DEFAULT_FULL_POLICY = new Policy(
             0.5f,  /* adjustBrightnessFactor */
+            true,  /* advertiseIsEnabled */
             true,  /* deferFullBackup */
             true,  /* deferKeyValueBackup */
             false, /* disableAnimation */
@@ -130,6 +170,12 @@
     @GuardedBy("mLock")
     private String mDeviceSpecificSettingsSource; // For dump() only.
 
+    @GuardedBy("mLock")
+    private String mAdaptiveSettings;
+
+    @GuardedBy("mLock")
+    private String mAdaptiveDeviceSpecificSettings;
+
     /**
      * A short string describing which battery saver is now enabled, which we dump in the eventlog.
      */
@@ -149,8 +195,32 @@
     @GuardedBy("mLock")
     private boolean mAccessibilityEnabled;
 
+    /** The current default adaptive policy. */
     @GuardedBy("mLock")
-    private Policy mCurrPolicy = sDefaultPolicy;
+    private Policy mDefaultAdaptivePolicy = DEFAULT_ADAPTIVE_POLICY;
+
+    /** The policy that will be used for adaptive battery saver. */
+    @GuardedBy("mLock")
+    private Policy mAdaptivePolicy = DEFAULT_ADAPTIVE_POLICY;
+
+    /** The policy to be used for full battery saver. */
+    @GuardedBy("mLock")
+    private Policy mFullPolicy = DEFAULT_FULL_POLICY;
+
+    @IntDef(prefix = {"POLICY_LEVEL_"}, value = {
+            POLICY_LEVEL_OFF,
+            POLICY_LEVEL_ADAPTIVE,
+            POLICY_LEVEL_FULL,
+    })
+    @Retention(RetentionPolicy.SOURCE)
+    @interface PolicyLevel {}
+
+    static final int POLICY_LEVEL_OFF = 0;
+    static final int POLICY_LEVEL_ADAPTIVE = 1;
+    static final int POLICY_LEVEL_FULL = 2;
+
+    @GuardedBy("mLock")
+    private int mPolicyLevel = POLICY_LEVEL_OFF;
 
     private final Context mContext;
     private final ContentResolver mContentResolver;
@@ -181,7 +251,11 @@
         mContentResolver.registerContentObserver(Settings.Global.getUriFor(
                 Settings.Global.BATTERY_SAVER_CONSTANTS), false, this);
         mContentResolver.registerContentObserver(Settings.Global.getUriFor(
-                Global.BATTERY_SAVER_DEVICE_SPECIFIC_CONSTANTS), false, this);
+                Settings.Global.BATTERY_SAVER_DEVICE_SPECIFIC_CONSTANTS), false, this);
+        mContentResolver.registerContentObserver(Settings.Global.getUriFor(
+                Settings.Global.BATTERY_SAVER_ADAPTIVE_CONSTANTS), false, this);
+        mContentResolver.registerContentObserver(Settings.Global.getUriFor(
+                Settings.Global.BATTERY_SAVER_ADAPTIVE_DEVICE_SPECIFIC_CONSTANTS), false, this);
 
         final AccessibilityManager acm = mContext.getSystemService(AccessibilityManager.class);
 
@@ -241,8 +315,16 @@
                 mDeviceSpecificSettingsSource = "(overlay)";
             }
 
-            // Update.
-            updateConstantsLocked(setting, deviceSpecificSetting);
+            final String adaptiveSetting =
+                    getGlobalSetting(Settings.Global.BATTERY_SAVER_ADAPTIVE_CONSTANTS);
+            final String adaptiveDeviceSpecificSetting = getGlobalSetting(
+                    Settings.Global.BATTERY_SAVER_ADAPTIVE_DEVICE_SPECIFIC_CONSTANTS);
+
+            if (!updateConstantsLocked(setting, deviceSpecificSetting,
+                    adaptiveSetting, adaptiveDeviceSpecificSetting)) {
+                // Nothing of note changed.
+                return;
+            }
 
             listeners = mListeners.toArray(new BatterySaverPolicyListener[0]);
         }
@@ -258,123 +340,94 @@
     @GuardedBy("mLock")
     @VisibleForTesting
     void updateConstantsLocked(final String setting, final String deviceSpecificSetting) {
+        updateConstantsLocked(setting, deviceSpecificSetting, "", "");
+    }
+
+    /** @return true if the currently active policy changed. */
+    private boolean updateConstantsLocked(String setting, String deviceSpecificSetting,
+            String adaptiveSetting, String adaptiveDeviceSpecificSetting) {
+        setting = TextUtils.emptyIfNull(setting);
+        deviceSpecificSetting = TextUtils.emptyIfNull(deviceSpecificSetting);
+        adaptiveSetting = TextUtils.emptyIfNull(adaptiveSetting);
+        adaptiveDeviceSpecificSetting = TextUtils.emptyIfNull(adaptiveDeviceSpecificSetting);
+
+        if (setting.equals(mSettings)
+                && deviceSpecificSetting.equals(mDeviceSpecificSettings)
+                && adaptiveSetting.equals(mAdaptiveSettings)
+                && adaptiveDeviceSpecificSetting.equals(mAdaptiveDeviceSpecificSettings)) {
+            return false;
+        }
+
         mSettings = setting;
         mDeviceSpecificSettings = deviceSpecificSetting;
+        mAdaptiveSettings = adaptiveSetting;
+        mAdaptiveDeviceSpecificSettings = adaptiveDeviceSpecificSetting;
 
         if (DEBUG) {
             Slog.i(TAG, "mSettings=" + mSettings);
             Slog.i(TAG, "mDeviceSpecificSettings=" + mDeviceSpecificSettings);
+            Slog.i(TAG, "mAdaptiveSettings=" + mAdaptiveSettings);
+            Slog.i(TAG, "mAdaptiveDeviceSpecificSettings=" + mAdaptiveDeviceSpecificSettings);
         }
 
-        final KeyValueListParser parser = new KeyValueListParser(',');
-
-        // Device-specific parameters.
-        try {
-            parser.setString(deviceSpecificSetting);
-        } catch (IllegalArgumentException e) {
-            Slog.wtf(TAG, "Bad device specific battery saver constants: "
-                    + deviceSpecificSetting);
+        boolean changed = false;
+        Policy newFullPolicy = Policy.fromSettings(setting, deviceSpecificSetting,
+                DEFAULT_FULL_POLICY);
+        if (mPolicyLevel == POLICY_LEVEL_FULL && !mFullPolicy.equals(newFullPolicy)) {
+            changed = true;
         }
+        mFullPolicy = newFullPolicy;
 
-        final String cpuFreqInteractive = parser.getString(KEY_CPU_FREQ_INTERACTIVE, "");
-        final String cpuFreqNoninteractive = parser.getString(KEY_CPU_FREQ_NONINTERACTIVE, "");
-
-        // Non-device-specific parameters.
-        try {
-            parser.setString(setting);
-        } catch (IllegalArgumentException e) {
-            Slog.wtf(TAG, "Bad battery saver constants: " + setting);
+        mDefaultAdaptivePolicy = Policy.fromSettings(adaptiveSetting, adaptiveDeviceSpecificSetting,
+                DEFAULT_ADAPTIVE_POLICY);
+        if (mPolicyLevel == POLICY_LEVEL_ADAPTIVE
+                && !mAdaptivePolicy.equals(mDefaultAdaptivePolicy)) {
+            changed = true;
         }
+        // This will override any config set by an external source. This should be fine for now.
+        // TODO: make sure it doesn't override what's set externally
+        mAdaptivePolicy = mDefaultAdaptivePolicy;
 
-        float adjustBrightnessFactor = parser.getFloat(KEY_ADJUST_BRIGHTNESS_FACTOR,
-                sDefaultPolicy.adjustBrightnessFactor);
-        boolean deferFullBackup = parser.getBoolean(KEY_FULLBACKUP_DEFERRED,
-                sDefaultPolicy.deferFullBackup);
-        boolean deferKeyValueBackup = parser.getBoolean(KEY_KEYVALUE_DEFERRED,
-                sDefaultPolicy.deferKeyValueBackup);
-        boolean disableAnimation = parser.getBoolean(KEY_ANIMATION_DISABLED,
-                sDefaultPolicy.disableAnimation);
-        boolean disableAod = parser.getBoolean(KEY_AOD_DISABLED, sDefaultPolicy.disableAod);
-        boolean disableLaunchBoost = parser.getBoolean(KEY_LAUNCH_BOOST_DISABLED,
-                sDefaultPolicy.disableLaunchBoost);
-        boolean disableOptionalSensors = parser.getBoolean(KEY_OPTIONAL_SENSORS_DISABLED,
-                sDefaultPolicy.disableOptionalSensors);
-        boolean disableSoundTrigger = parser.getBoolean(KEY_SOUNDTRIGGER_DISABLED,
-                sDefaultPolicy.disableSoundTrigger);
-        boolean disableVibrationConfig = parser.getBoolean(KEY_VIBRATION_DISABLED,
-                sDefaultPolicy.disableVibration);
-        boolean enableAdjustBrightness = !parser.getBoolean(KEY_ADJUST_BRIGHTNESS_DISABLED,
-                !sDefaultPolicy.enableAdjustBrightness);
-        boolean enableDataSaver = !parser.getBoolean(KEY_ACTIVATE_DATASAVER_DISABLED,
-                !sDefaultPolicy.enableDataSaver);
-        boolean enableFirewall = !parser.getBoolean(KEY_ACTIVATE_FIREWALL_DISABLED,
-                !sDefaultPolicy.enableFirewall);
-        boolean enableQuickDoze = parser.getBoolean(KEY_QUICK_DOZE_ENABLED,
-                sDefaultPolicy.enableQuickDoze);
-        boolean forceAllAppsStandby = parser.getBoolean(KEY_FORCE_ALL_APPS_STANDBY,
-                sDefaultPolicy.forceAllAppsStandby);
-        boolean forceBackgroundCheck = parser.getBoolean(KEY_FORCE_BACKGROUND_CHECK,
-                sDefaultPolicy.forceBackgroundCheck);
-        int gpsMode = parser.getInt(KEY_GPS_MODE, sDefaultPolicy.gpsMode);
-        boolean sendTronLog = parser.getBoolean(KEY_SEND_TRON_LOG, sDefaultPolicy.sendTronLog);
+        updatePolicyDependenciesLocked();
 
-        mCurrPolicy = new Policy(
-                adjustBrightnessFactor,
-                deferFullBackup,
-                deferKeyValueBackup,
-                disableAnimation,
-                disableAod,
-                disableLaunchBoost,
-                disableOptionalSensors,
-                disableSoundTrigger,
-                /* disableVibration */
-                disableVibrationConfig,
-                enableAdjustBrightness,
-                enableDataSaver,
-                enableFirewall,
-                enableQuickDoze,
-                /* filesForInteractive */
-                (new CpuFrequencies()).parseString(cpuFreqInteractive).toSysFileMap(),
-                /* filesForNoninteractive */
-                (new CpuFrequencies()).parseString(cpuFreqNoninteractive).toSysFileMap(),
-                forceAllAppsStandby,
-                forceBackgroundCheck,
-                gpsMode,
-                sendTronLog
-        );
+        return changed;
+    }
 
-        // Update the effective policy.
-        mDisableVibrationEffective = mCurrPolicy.disableVibration
+    @GuardedBy("mLock")
+    private void updatePolicyDependenciesLocked() {
+        final Policy currPolicy = getCurrentPolicyLocked();
+        // Update the effective vibration policy.
+        mDisableVibrationEffective = currPolicy.disableVibration
                 && !mAccessibilityEnabled; // Don't disable vibration when accessibility is on.
 
         final StringBuilder sb = new StringBuilder();
 
-        if (mCurrPolicy.forceAllAppsStandby) sb.append("A");
-        if (mCurrPolicy.forceBackgroundCheck) sb.append("B");
+        if (currPolicy.forceAllAppsStandby) sb.append("A");
+        if (currPolicy.forceBackgroundCheck) sb.append("B");
 
         if (mDisableVibrationEffective) sb.append("v");
-        if (mCurrPolicy.disableAnimation) sb.append("a");
-        if (mCurrPolicy.disableSoundTrigger) sb.append("s");
-        if (mCurrPolicy.deferFullBackup) sb.append("F");
-        if (mCurrPolicy.deferKeyValueBackup) sb.append("K");
-        if (mCurrPolicy.enableFirewall) sb.append("f");
-        if (mCurrPolicy.enableDataSaver) sb.append("d");
-        if (mCurrPolicy.enableAdjustBrightness) sb.append("b");
+        if (currPolicy.disableAnimation) sb.append("a");
+        if (currPolicy.disableSoundTrigger) sb.append("s");
+        if (currPolicy.deferFullBackup) sb.append("F");
+        if (currPolicy.deferKeyValueBackup) sb.append("K");
+        if (currPolicy.enableFirewall) sb.append("f");
+        if (currPolicy.enableDataSaver) sb.append("d");
+        if (currPolicy.enableAdjustBrightness) sb.append("b");
 
-        if (mCurrPolicy.disableLaunchBoost) sb.append("l");
-        if (mCurrPolicy.disableOptionalSensors) sb.append("S");
-        if (mCurrPolicy.disableAod) sb.append("o");
-        if (mCurrPolicy.enableQuickDoze) sb.append("q");
-        if (mCurrPolicy.sendTronLog) sb.append("t");
+        if (currPolicy.disableLaunchBoost) sb.append("l");
+        if (currPolicy.disableOptionalSensors) sb.append("S");
+        if (currPolicy.disableAod) sb.append("o");
+        if (currPolicy.enableQuickDoze) sb.append("q");
+        if (currPolicy.sendTronLog) sb.append("t");
 
-        sb.append(mCurrPolicy.gpsMode);
+        sb.append(currPolicy.gpsMode);
 
         mEventLogKeys = sb.toString();
 
-        mBatterySavingStats.setSendTronLog(mCurrPolicy.sendTronLog);
+        mBatterySavingStats.setSendTronLog(currPolicy.sendTronLog);
     }
 
-    private static class Policy {
+    static class Policy {
         /**
          * This is the flag to decide the how much to adjust the screen brightness. This is
          * the float value from 0 to 1 where 1 means don't change brightness.
@@ -385,6 +438,16 @@
         public final float adjustBrightnessFactor;
 
         /**
+         * {@code true} if the Policy should advertise to the rest of the system that battery saver
+         * is enabled. This advertising could cause other system components to change their
+         * behavior. This will not affect other policy flags and what they change.
+         *
+         * @see Settings.Global#BATTERY_SAVER_CONSTANTS
+         * @see #KEY_ADVERTISE_IS_ENABLED
+         */
+        public final boolean advertiseIsEnabled;
+
+        /**
          * {@code true} if full backup is deferred in battery saver mode.
          *
          * @see Settings.Global#BATTERY_SAVER_CONSTANTS
@@ -509,8 +572,11 @@
          */
         public final boolean sendTronLog;
 
+        private final int mHashCode;
+
         Policy(
                 float adjustBrightnessFactor,
+                boolean advertiseIsEnabled,
                 boolean deferFullBackup,
                 boolean deferKeyValueBackup,
                 boolean disableAnimation,
@@ -531,6 +597,7 @@
                 boolean sendTronLog) {
 
             this.adjustBrightnessFactor = adjustBrightnessFactor;
+            this.advertiseIsEnabled = advertiseIsEnabled;
             this.deferFullBackup = deferFullBackup;
             this.deferKeyValueBackup = deferKeyValueBackup;
             this.disableAnimation = disableAnimation;
@@ -549,94 +616,338 @@
             this.forceBackgroundCheck = forceBackgroundCheck;
             this.gpsMode = gpsMode;
             this.sendTronLog = sendTronLog;
+
+            mHashCode = Objects.hash(
+                    adjustBrightnessFactor,
+                    advertiseIsEnabled,
+                    deferFullBackup,
+                    deferKeyValueBackup,
+                    disableAnimation,
+                    disableAod,
+                    disableLaunchBoost,
+                    disableOptionalSensors,
+                    disableSoundTrigger,
+                    disableVibration,
+                    enableAdjustBrightness,
+                    enableDataSaver,
+                    enableFirewall,
+                    enableQuickDoze,
+                    filesForInteractive,
+                    filesForNoninteractive,
+                    forceAllAppsStandby,
+                    forceBackgroundCheck,
+                    gpsMode,
+                    sendTronLog);
+        }
+
+        static Policy fromConfig(BatterySaverPolicyConfig config) {
+            if (config == null) {
+                Slog.e(TAG, "Null config passed down to BatterySaverPolicy");
+                return OFF_POLICY;
+            }
+
+            // Device-specific parameters.
+            Map<String, String> deviceSpecificSettings = config.getDeviceSpecificSettings();
+            final String cpuFreqInteractive =
+                    deviceSpecificSettings.getOrDefault(KEY_CPU_FREQ_INTERACTIVE, "");
+            final String cpuFreqNoninteractive =
+                    deviceSpecificSettings.getOrDefault(KEY_CPU_FREQ_NONINTERACTIVE, "");
+
+            return new Policy(
+                    config.getAdjustBrightnessFactor(),
+                    config.getAdvertiseIsEnabled(),
+                    config.getDeferFullBackup(),
+                    config.getDeferKeyValueBackup(),
+                    config.getDisableAnimation(),
+                    config.getDisableAod(),
+                    config.getDisableLaunchBoost(),
+                    config.getDisableOptionalSensors(),
+                    config.getDisableSoundTrigger(),
+                    config.getDisableVibration(),
+                    config.getEnableAdjustBrightness(),
+                    config.getEnableDataSaver(),
+                    config.getEnableFirewall(),
+                    config.getEnableQuickDoze(),
+                    /* filesForInteractive */
+                    (new CpuFrequencies()).parseString(cpuFreqInteractive).toSysFileMap(),
+                    /* filesForNoninteractive */
+                    (new CpuFrequencies()).parseString(cpuFreqNoninteractive).toSysFileMap(),
+                    config.getForceAllAppsStandby(),
+                    config.getForceBackgroundCheck(),
+                    config.getGpsMode(),
+                    OFF_POLICY.sendTronLog
+            );
+        }
+
+        static Policy fromSettings(String settings, String deviceSpecificSettings) {
+            return fromSettings(settings, deviceSpecificSettings, OFF_POLICY);
+        }
+
+        static Policy fromSettings(String settings, String deviceSpecificSettings,
+                Policy defaultPolicy) {
+            final KeyValueListParser parser = new KeyValueListParser(',');
+
+            // Device-specific parameters.
+            try {
+                parser.setString(deviceSpecificSettings == null ? "" : deviceSpecificSettings);
+            } catch (IllegalArgumentException e) {
+                Slog.wtf(TAG, "Bad device specific battery saver constants: "
+                        + deviceSpecificSettings);
+            }
+
+            final String cpuFreqInteractive = parser.getString(KEY_CPU_FREQ_INTERACTIVE, "");
+            final String cpuFreqNoninteractive = parser.getString(KEY_CPU_FREQ_NONINTERACTIVE, "");
+
+            // Non-device-specific parameters.
+            try {
+                parser.setString(settings == null ? "" : settings);
+            } catch (IllegalArgumentException e) {
+                Slog.wtf(TAG, "Bad battery saver constants: " + settings);
+            }
+
+            float adjustBrightnessFactor = parser.getFloat(KEY_ADJUST_BRIGHTNESS_FACTOR,
+                    defaultPolicy.adjustBrightnessFactor);
+            boolean advertiseIsEnabled = parser.getBoolean(KEY_ADVERTISE_IS_ENABLED,
+                    defaultPolicy.advertiseIsEnabled);
+            boolean deferFullBackup = parser.getBoolean(KEY_FULLBACKUP_DEFERRED,
+                    defaultPolicy.deferFullBackup);
+            boolean deferKeyValueBackup = parser.getBoolean(KEY_KEYVALUE_DEFERRED,
+                    defaultPolicy.deferKeyValueBackup);
+            boolean disableAnimation = parser.getBoolean(KEY_ANIMATION_DISABLED,
+                    defaultPolicy.disableAnimation);
+            boolean disableAod = parser.getBoolean(KEY_AOD_DISABLED, defaultPolicy.disableAod);
+            boolean disableLaunchBoost = parser.getBoolean(KEY_LAUNCH_BOOST_DISABLED,
+                    defaultPolicy.disableLaunchBoost);
+            boolean disableOptionalSensors = parser.getBoolean(KEY_OPTIONAL_SENSORS_DISABLED,
+                    defaultPolicy.disableOptionalSensors);
+            boolean disableSoundTrigger = parser.getBoolean(KEY_SOUNDTRIGGER_DISABLED,
+                    defaultPolicy.disableSoundTrigger);
+            boolean disableVibrationConfig = parser.getBoolean(KEY_VIBRATION_DISABLED,
+                    defaultPolicy.disableVibration);
+            boolean enableAdjustBrightness = !parser.getBoolean(KEY_ADJUST_BRIGHTNESS_DISABLED,
+                    !defaultPolicy.enableAdjustBrightness);
+            boolean enableDataSaver = !parser.getBoolean(KEY_ACTIVATE_DATASAVER_DISABLED,
+                    !defaultPolicy.enableDataSaver);
+            boolean enableFirewall = !parser.getBoolean(KEY_ACTIVATE_FIREWALL_DISABLED,
+                    !defaultPolicy.enableFirewall);
+            boolean enableQuickDoze = parser.getBoolean(KEY_QUICK_DOZE_ENABLED,
+                    defaultPolicy.enableQuickDoze);
+            boolean forceAllAppsStandby = parser.getBoolean(KEY_FORCE_ALL_APPS_STANDBY,
+                    defaultPolicy.forceAllAppsStandby);
+            boolean forceBackgroundCheck = parser.getBoolean(KEY_FORCE_BACKGROUND_CHECK,
+                    defaultPolicy.forceBackgroundCheck);
+            int gpsMode = parser.getInt(KEY_GPS_MODE, defaultPolicy.gpsMode);
+            boolean sendTronLog = parser.getBoolean(KEY_SEND_TRON_LOG, defaultPolicy.sendTronLog);
+
+            return new Policy(
+                    adjustBrightnessFactor,
+                    advertiseIsEnabled,
+                    deferFullBackup,
+                    deferKeyValueBackup,
+                    disableAnimation,
+                    disableAod,
+                    disableLaunchBoost,
+                    disableOptionalSensors,
+                    disableSoundTrigger,
+                    /* disableVibration */
+                    disableVibrationConfig,
+                    enableAdjustBrightness,
+                    enableDataSaver,
+                    enableFirewall,
+                    enableQuickDoze,
+                    /* filesForInteractive */
+                    (new CpuFrequencies()).parseString(cpuFreqInteractive).toSysFileMap(),
+                    /* filesForNoninteractive */
+                    (new CpuFrequencies()).parseString(cpuFreqNoninteractive).toSysFileMap(),
+                    forceAllAppsStandby,
+                    forceBackgroundCheck,
+                    gpsMode,
+                    sendTronLog
+            );
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (this == obj) return true;
+            if (!(obj instanceof Policy)) return false;
+            Policy other = (Policy) obj;
+            return Float.compare(other.adjustBrightnessFactor, adjustBrightnessFactor) == 0
+                    && advertiseIsEnabled == other.advertiseIsEnabled
+                    && deferFullBackup == other.deferFullBackup
+                    && deferKeyValueBackup == other.deferKeyValueBackup
+                    && disableAnimation == other.disableAnimation
+                    && disableAod == other.disableAod
+                    && disableLaunchBoost == other.disableLaunchBoost
+                    && disableOptionalSensors == other.disableOptionalSensors
+                    && disableSoundTrigger == other.disableSoundTrigger
+                    && disableVibration == other.disableVibration
+                    && enableAdjustBrightness == other.enableAdjustBrightness
+                    && enableDataSaver == other.enableDataSaver
+                    && enableFirewall == other.enableFirewall
+                    && enableQuickDoze == other.enableQuickDoze
+                    && forceAllAppsStandby == other.forceAllAppsStandby
+                    && forceBackgroundCheck == other.forceBackgroundCheck
+                    && gpsMode == other.gpsMode
+                    && sendTronLog == other.sendTronLog
+                    && filesForInteractive.equals(other.filesForInteractive)
+                    && filesForNoninteractive.equals(other.filesForNoninteractive);
+        }
+
+        @Override
+        public int hashCode() {
+            return mHashCode;
         }
     }
 
     /**
-     * Get the {@link PowerSaveState} based on {@paramref type} and {@paramref realMode}.
+     * Get the {@link PowerSaveState} based on the current policy level.
      * The result will have {@link PowerSaveState#batterySaverEnabled} and some other
      * parameters when necessary.
      *
-     * @param type     type of the service, one of {@link ServiceType}
-     * @param realMode whether the battery saver is on by default
+     * @param type   type of the service, one of {@link ServiceType}
      * @return State data that contains battery saver data
      */
-    public PowerSaveState getBatterySaverPolicy(@ServiceType int type, boolean realMode) {
+    public PowerSaveState getBatterySaverPolicy(@ServiceType int type) {
         synchronized (mLock) {
+            final Policy currPolicy = getCurrentPolicyLocked();
             final PowerSaveState.Builder builder = new PowerSaveState.Builder()
-                    .setGlobalBatterySaverEnabled(realMode);
-            if (!realMode) {
-                return builder.setBatterySaverEnabled(realMode)
-                        .build();
-            }
+                    .setGlobalBatterySaverEnabled(currPolicy.advertiseIsEnabled);
             switch (type) {
                 case ServiceType.GPS:
-                    return builder.setBatterySaverEnabled(realMode)
-                            .setGpsMode(mCurrPolicy.gpsMode)
+                    boolean isEnabled = currPolicy.advertiseIsEnabled
+                            || currPolicy.gpsMode != PowerManager.LOCATION_MODE_NO_CHANGE;
+                    return builder.setBatterySaverEnabled(isEnabled)
+                            .setGpsMode(currPolicy.gpsMode)
                             .build();
                 case ServiceType.ANIMATION:
-                    return builder.setBatterySaverEnabled(mCurrPolicy.disableAnimation)
+                    return builder.setBatterySaverEnabled(currPolicy.disableAnimation)
                             .build();
                 case ServiceType.FULL_BACKUP:
-                    return builder.setBatterySaverEnabled(mCurrPolicy.deferFullBackup)
+                    return builder.setBatterySaverEnabled(currPolicy.deferFullBackup)
                             .build();
                 case ServiceType.KEYVALUE_BACKUP:
-                    return builder.setBatterySaverEnabled(mCurrPolicy.deferKeyValueBackup)
+                    return builder.setBatterySaverEnabled(currPolicy.deferKeyValueBackup)
                             .build();
                 case ServiceType.NETWORK_FIREWALL:
-                    return builder.setBatterySaverEnabled(mCurrPolicy.enableFirewall)
+                    return builder.setBatterySaverEnabled(currPolicy.enableFirewall)
                             .build();
                 case ServiceType.SCREEN_BRIGHTNESS:
-                    return builder.setBatterySaverEnabled(mCurrPolicy.enableAdjustBrightness)
-                            .setBrightnessFactor(mCurrPolicy.adjustBrightnessFactor)
+                    return builder.setBatterySaverEnabled(currPolicy.enableAdjustBrightness)
+                            .setBrightnessFactor(currPolicy.adjustBrightnessFactor)
                             .build();
                 case ServiceType.DATA_SAVER:
-                    return builder.setBatterySaverEnabled(mCurrPolicy.enableDataSaver)
+                    return builder.setBatterySaverEnabled(currPolicy.enableDataSaver)
                             .build();
                 case ServiceType.SOUND:
-                    return builder.setBatterySaverEnabled(mCurrPolicy.disableSoundTrigger)
+                    return builder.setBatterySaverEnabled(currPolicy.disableSoundTrigger)
                             .build();
                 case ServiceType.VIBRATION:
                     return builder.setBatterySaverEnabled(mDisableVibrationEffective)
                             .build();
                 case ServiceType.FORCE_ALL_APPS_STANDBY:
-                    return builder.setBatterySaverEnabled(mCurrPolicy.forceAllAppsStandby)
+                    return builder.setBatterySaverEnabled(currPolicy.forceAllAppsStandby)
                             .build();
                 case ServiceType.FORCE_BACKGROUND_CHECK:
-                    return builder.setBatterySaverEnabled(mCurrPolicy.forceBackgroundCheck)
+                    return builder.setBatterySaverEnabled(currPolicy.forceBackgroundCheck)
                             .build();
                 case ServiceType.OPTIONAL_SENSORS:
-                    return builder.setBatterySaverEnabled(mCurrPolicy.disableOptionalSensors)
+                    return builder.setBatterySaverEnabled(currPolicy.disableOptionalSensors)
                             .build();
                 case ServiceType.AOD:
-                    return builder.setBatterySaverEnabled(mCurrPolicy.disableAod)
+                    return builder.setBatterySaverEnabled(currPolicy.disableAod)
                             .build();
                 case ServiceType.QUICK_DOZE:
-                    return builder.setBatterySaverEnabled(mCurrPolicy.enableQuickDoze)
+                    return builder.setBatterySaverEnabled(currPolicy.enableQuickDoze)
                             .build();
                 default:
-                    return builder.setBatterySaverEnabled(realMode)
+                    return builder.setBatterySaverEnabled(currPolicy.advertiseIsEnabled)
                             .build();
             }
         }
     }
 
+    /**
+     * Sets the current policy.
+     *
+     * @return true if the policy level was changed.
+     */
+    boolean setPolicyLevel(@PolicyLevel int level) {
+        synchronized (mLock) {
+            if (mPolicyLevel == level) {
+                return false;
+            }
+            switch (level) {
+                case POLICY_LEVEL_FULL:
+                case POLICY_LEVEL_ADAPTIVE:
+                case POLICY_LEVEL_OFF:
+                    mPolicyLevel = level;
+                    break;
+                default:
+                    Slog.wtf(TAG, "setPolicyLevel invalid level given: " + level);
+                    return false;
+            }
+            updatePolicyDependenciesLocked();
+            return true;
+        }
+    }
+
+    /** @return true if the current policy changed and the policy level is ADAPTIVE. */
+    boolean setAdaptivePolicyLocked(Policy p) {
+        if (p == null) {
+            Slog.wtf(TAG, "setAdaptivePolicy given null policy");
+            return false;
+        }
+        if (mAdaptivePolicy.equals(p)) {
+            return false;
+        }
+
+        mAdaptivePolicy = p;
+        if (mPolicyLevel == POLICY_LEVEL_ADAPTIVE) {
+            updatePolicyDependenciesLocked();
+            return true;
+        }
+        return false;
+    }
+
+    /** @return true if the current policy changed and the policy level is ADAPTIVE. */
+    boolean resetAdaptivePolicyLocked() {
+        return setAdaptivePolicyLocked(mDefaultAdaptivePolicy);
+    }
+
+    private Policy getCurrentPolicyLocked() {
+        switch (mPolicyLevel) {
+            case POLICY_LEVEL_FULL:
+                return mFullPolicy;
+            case POLICY_LEVEL_ADAPTIVE:
+                return mAdaptivePolicy;
+            case POLICY_LEVEL_OFF:
+            default:
+                return OFF_POLICY;
+        }
+    }
+
     public int getGpsMode() {
         synchronized (mLock) {
-            return mCurrPolicy.gpsMode;
+            return getCurrentPolicyLocked().gpsMode;
         }
     }
 
     public ArrayMap<String, String> getFileValues(boolean interactive) {
         synchronized (mLock) {
-            return interactive ? mCurrPolicy.filesForInteractive
-                    : mCurrPolicy.filesForNoninteractive;
+            return interactive ? getCurrentPolicyLocked().filesForInteractive
+                    : getCurrentPolicyLocked().filesForNoninteractive;
         }
     }
 
     public boolean isLaunchBoostDisabled() {
         synchronized (mLock) {
-            return mCurrPolicy.disableLaunchBoost;
+            return getCurrentPolicyLocked().disableLaunchBoost;
+        }
+    }
+
+    boolean shouldAdvertiseIsEnabled() {
+        synchronized (mLock) {
+            return getCurrentPolicyLocked().advertiseIsEnabled;
         }
     }
 
@@ -658,40 +969,75 @@
             pw.println("  Settings: " + mDeviceSpecificSettingsSource);
             pw.println("    value: " + mDeviceSpecificSettings);
 
-            pw.println();
+            pw.println("  Adaptive Settings: " + Settings.Global.BATTERY_SAVER_ADAPTIVE_CONSTANTS);
+            pw.println("    value: " + mAdaptiveSettings);
+            pw.println("  Adaptive Device Specific Settings: "
+                    + Settings.Global.BATTERY_SAVER_ADAPTIVE_DEVICE_SPECIFIC_CONSTANTS);
+            pw.println("    value: " + mAdaptiveDeviceSpecificSettings);
+
             pw.println("  mAccessibilityEnabled=" + mAccessibilityEnabled);
-            pw.println("  " + KEY_VIBRATION_DISABLED + ":config=" + mCurrPolicy.disableVibration);
-            pw.println("  " + KEY_VIBRATION_DISABLED + ":effective=" + mDisableVibrationEffective);
-            pw.println("  " + KEY_ANIMATION_DISABLED + "=" + mCurrPolicy.disableAnimation);
-            pw.println("  " + KEY_FULLBACKUP_DEFERRED + "=" + mCurrPolicy.deferFullBackup);
-            pw.println("  " + KEY_KEYVALUE_DEFERRED + "=" + mCurrPolicy.deferKeyValueBackup);
-            pw.println("  " + KEY_ACTIVATE_FIREWALL_DISABLED + "=" + !mCurrPolicy.enableFirewall);
-            pw.println("  " + KEY_ACTIVATE_DATASAVER_DISABLED + "=" + !mCurrPolicy.enableDataSaver);
-            pw.println("  " + KEY_LAUNCH_BOOST_DISABLED + "=" + mCurrPolicy.disableLaunchBoost);
-            pw.println("  " + KEY_ADJUST_BRIGHTNESS_DISABLED + "="
-                    + !mCurrPolicy.enableAdjustBrightness);
-            pw.println(
-                    "  " + KEY_ADJUST_BRIGHTNESS_FACTOR + "=" + mCurrPolicy.adjustBrightnessFactor);
-            pw.println("  " + KEY_GPS_MODE + "=" + mCurrPolicy.gpsMode);
-            pw.println("  " + KEY_FORCE_ALL_APPS_STANDBY + "=" + mCurrPolicy.forceAllAppsStandby);
-            pw.println("  " + KEY_FORCE_BACKGROUND_CHECK + "=" + mCurrPolicy.forceBackgroundCheck);
-            pw.println("  " + KEY_OPTIONAL_SENSORS_DISABLED + "="
-                    + mCurrPolicy.disableOptionalSensors);
-            pw.println("  " + KEY_AOD_DISABLED + "=" + mCurrPolicy.disableAod);
-            pw.println("  " + KEY_SOUNDTRIGGER_DISABLED + "=" + mCurrPolicy.disableSoundTrigger);
-            pw.println("  " + KEY_QUICK_DOZE_ENABLED + "=" + mCurrPolicy.enableQuickDoze);
-            pw.println("  " + KEY_SEND_TRON_LOG + "=" + mCurrPolicy.sendTronLog);
-            pw.println();
+            pw.println("  mPolicyLevel=" + mPolicyLevel);
 
-            pw.print("  Interactive File values:\n");
-            dumpMap(pw, "    ", mCurrPolicy.filesForInteractive);
-            pw.println();
-
-            pw.print("  Noninteractive File values:\n");
-            dumpMap(pw, "    ", mCurrPolicy.filesForNoninteractive);
+            dumpPolicyLocked(pw, "  ", "full", mFullPolicy);
+            dumpPolicyLocked(pw, "  ", "default adaptive", mDefaultAdaptivePolicy);
+            dumpPolicyLocked(pw, "  ", "current adaptive", mAdaptivePolicy);
         }
     }
 
+    private void dumpPolicyLocked(PrintWriter pw, String indent, String label, Policy p) {
+        pw.println();
+        pw.print(indent);
+        pw.println("Policy '" + label + "'");
+        pw.print(indent);
+        pw.println("  " + KEY_ADVERTISE_IS_ENABLED + "=" + p.advertiseIsEnabled);
+        pw.print(indent);
+        pw.println("  " + KEY_VIBRATION_DISABLED + ":config=" + p.disableVibration);
+        // mDisableVibrationEffective is based on the currently selected policy
+        pw.print(indent);
+        pw.println("  " + KEY_VIBRATION_DISABLED + ":effective=" + (p.disableVibration
+                && !mAccessibilityEnabled));
+        pw.print(indent);
+        pw.println("  " + KEY_ANIMATION_DISABLED + "=" + p.disableAnimation);
+        pw.print(indent);
+        pw.println("  " + KEY_FULLBACKUP_DEFERRED + "=" + p.deferFullBackup);
+        pw.print(indent);
+        pw.println("  " + KEY_KEYVALUE_DEFERRED + "=" + p.deferKeyValueBackup);
+        pw.print(indent);
+        pw.println("  " + KEY_ACTIVATE_FIREWALL_DISABLED + "=" + !p.enableFirewall);
+        pw.print(indent);
+        pw.println("  " + KEY_ACTIVATE_DATASAVER_DISABLED + "=" + !p.enableDataSaver);
+        pw.print(indent);
+        pw.println("  " + KEY_LAUNCH_BOOST_DISABLED + "=" + p.disableLaunchBoost);
+        pw.println(
+                "    " + KEY_ADJUST_BRIGHTNESS_DISABLED + "=" + !p.enableAdjustBrightness);
+        pw.print(indent);
+        pw.println("  " + KEY_ADJUST_BRIGHTNESS_FACTOR + "=" + p.adjustBrightnessFactor);
+        pw.print(indent);
+        pw.println("  " + KEY_GPS_MODE + "=" + p.gpsMode);
+        pw.print(indent);
+        pw.println("  " + KEY_FORCE_ALL_APPS_STANDBY + "=" + p.forceAllAppsStandby);
+        pw.print(indent);
+        pw.println("  " + KEY_FORCE_BACKGROUND_CHECK + "=" + p.forceBackgroundCheck);
+        pw.println(
+                "    " + KEY_OPTIONAL_SENSORS_DISABLED + "=" + p.disableOptionalSensors);
+        pw.print(indent);
+        pw.println("  " + KEY_AOD_DISABLED + "=" + p.disableAod);
+        pw.print(indent);
+        pw.println("  " + KEY_SOUNDTRIGGER_DISABLED + "=" + p.disableSoundTrigger);
+        pw.print(indent);
+        pw.println("  " + KEY_QUICK_DOZE_ENABLED + "=" + p.enableQuickDoze);
+        pw.print(indent);
+        pw.println("  " + KEY_SEND_TRON_LOG + "=" + p.sendTronLog);
+        pw.println();
+
+        pw.print("    Interactive File values:\n");
+        dumpMap(pw, "      ", p.filesForInteractive);
+        pw.println();
+
+        pw.print("    Noninteractive File values:\n");
+        dumpMap(pw, "      ", p.filesForNoninteractive);
+    }
+
     private void dumpMap(PrintWriter pw, String prefix, ArrayMap<String, String> map) {
         if (map == null) {
             return;
@@ -710,6 +1056,7 @@
     public void setAccessibilityEnabledForTest(boolean enabled) {
         synchronized (mLock) {
             mAccessibilityEnabled = enabled;
+            updatePolicyDependenciesLocked();
         }
     }
 }
diff --git a/services/core/java/com/android/server/power/batterysaver/BatterySaverStateMachine.java b/services/core/java/com/android/server/power/batterysaver/BatterySaverStateMachine.java
index 404e450..e6fa500 100644
--- a/services/core/java/com/android/server/power/batterysaver/BatterySaverStateMachine.java
+++ b/services/core/java/com/android/server/power/batterysaver/BatterySaverStateMachine.java
@@ -24,8 +24,10 @@
 import android.content.Intent;
 import android.content.res.Resources;
 import android.database.ContentObserver;
+import android.os.BatterySaverPolicyConfig;
 import android.os.Handler;
 import android.os.PowerManager;
+import android.os.SystemClock;
 import android.os.UserHandle;
 import android.provider.Settings;
 import android.util.Slog;
@@ -46,8 +48,7 @@
  * IMPORTANT: This class shares the power manager lock, which is very low in the lock hierarchy.
  * Do not call out with the lock held. (Settings provider is okay.)
  *
- * Test:
-  atest $ANDROID_BUILD_TOP/frameworks/base/services/tests/servicestests/src/com/android/server/power/batterysaver/BatterySaverStateMachineTest.java
+ * Test: atest com.android.server.power.batterysaver.BatterySaverStateMachineTest
  */
 public class BatterySaverStateMachine {
     private static final String TAG = "BatterySaverStateMachine";
@@ -57,6 +58,8 @@
 
     private static final boolean DEBUG = BatterySaverPolicy.DEBUG;
 
+    private static final long ADAPTIVE_CHANGE_TIMEOUT_MS = 24 * 60 * 60 * 1000L;
+
     private final Context mContext;
     private final BatterySaverController mBatterySaverController;
 
@@ -157,6 +160,13 @@
     @GuardedBy("mLock")
     private String mLastChangedStrReason;
 
+    /**
+     * The last time adaptive battery saver was changed by an external service, using elapsed
+     * realtime as the timebase.
+     */
+    @GuardedBy("mLock")
+    private long mLastAdaptiveBatterySaverChangedExternallyElapsed;
+
     private final ContentObserver mSettingsObserver = new ContentObserver(null) {
         @Override
         public void onChange(boolean selfChange) {
@@ -178,10 +188,6 @@
                 com.android.internal.R.integer.config_dynamicPowerSavingsDefaultDisableThreshold);
     }
 
-    private boolean isBatterySaverEnabled() {
-        return mBatterySaverController.isEnabled();
-    }
-
     private boolean isAutoBatterySaverConfiguredLocked() {
         return mSettingBatterySaverTriggerThreshold > 0;
     }
@@ -392,6 +398,36 @@
         }
     }
 
+    /**
+     * Enable or disable the current adaptive battery saver policy. This may not change what's in
+     * effect if full battery saver is also enabled.
+     */
+    public boolean setAdaptiveBatterySaverEnabled(boolean enabled) {
+        if (DEBUG) {
+            Slog.d(TAG, "setAdaptiveBatterySaverEnabled: enabled=" + enabled);
+        }
+        synchronized (mLock) {
+            mLastAdaptiveBatterySaverChangedExternallyElapsed = SystemClock.elapsedRealtime();
+            return mBatterySaverController.setAdaptivePolicyEnabledLocked(
+                    enabled, BatterySaverController.REASON_ADAPTIVE_DYNAMIC_POWER_SAVINGS_CHANGED);
+        }
+    }
+
+    /**
+     * Change the adaptive battery saver policy.
+     */
+    public boolean setAdaptiveBatterySaverPolicy(BatterySaverPolicyConfig config) {
+        if (DEBUG) {
+            Slog.d(TAG, "setAdaptiveBatterySaverPolicy: config=" + config);
+        }
+
+        synchronized (mLock) {
+            mLastAdaptiveBatterySaverChangedExternallyElapsed = SystemClock.elapsedRealtime();
+            return mBatterySaverController.setAdaptivePolicyLocked(config,
+                    BatterySaverController.REASON_ADAPTIVE_DYNAMIC_POWER_SAVINGS_CHANGED);
+        }
+    }
+
     @GuardedBy("mLock")
     private boolean isBatteryLowLocked() {
         final boolean percentageLow =
@@ -427,12 +463,26 @@
         if (!isBatteryLowLocked()) {
             updateSnoozingLocked(false, "Battery not low");
         }
+
+        if (SystemClock.elapsedRealtime() - mLastAdaptiveBatterySaverChangedExternallyElapsed
+                > ADAPTIVE_CHANGE_TIMEOUT_MS) {
+            mBatterySaverController.setAdaptivePolicyEnabledLocked(
+                    false, BatterySaverController.REASON_TIMEOUT);
+            mBatterySaverController.resetAdaptivePolicyLocked(
+                    BatterySaverController.REASON_TIMEOUT);
+        }
+
         if (mIsPowered) {
             updateSnoozingLocked(false, "Plugged in");
             enableBatterySaverLocked(/*enable=*/ false, /*manual=*/ false,
                     BatterySaverController.REASON_PLUGGED_IN,
                     "Plugged in");
 
+            if (mBatteryLevel >= 80 /* Arbitrary level */) {
+                mBatterySaverController.setAdaptivePolicyEnabledLocked(
+                        false, BatterySaverController.REASON_PLUGGED_IN);
+            }
+
         } else if (mSettingBatterySaverEnabledSticky && !mBatterySaverStickyBehaviourDisabled) {
             if (mSettingBatterySaverStickyAutoDisableEnabled
                     && mBatteryLevel >= mSettingBatterySaverStickyAutoDisableThreshold) {
@@ -472,7 +522,7 @@
         // do nothing if automatic battery saver mode = PERCENTAGE and low warning threshold = 0%
     }
 
-  /**
+    /**
      * {@link com.android.server.power.PowerManagerService} calls it when
      * {@link android.os.PowerManager#setPowerSaveMode} is called.
      *
@@ -501,7 +551,7 @@
             Slog.d(TAG, "enableBatterySaver: enable=" + enable + " manual=" + manual
                     + " reason=" + strReason + "(" + intReason + ")");
         }
-        final boolean wasEnabled = mBatterySaverController.isEnabled();
+        final boolean wasEnabled = mBatterySaverController.isFullEnabled();
 
         if (wasEnabled == enable) {
             if (DEBUG) {
@@ -523,9 +573,10 @@
                 // When battery saver is disabled manually (while battery saver is enabled)
                 // when the battery level is low, we "snooze" BS -- i.e. disable auto battery saver.
                 // We resume auto-BS once the battery level is not low, or the device is plugged in.
-                if (isBatterySaverEnabled() && isBatteryLowLocked()) {
+                if (mBatterySaverController.isFullEnabled() && isBatteryLowLocked()) {
                     updateSnoozingLocked(true, "Manual snooze");
                 }
+                // TODO: maybe turn off adaptive if it's on and advertiseIsEnabled is true
             }
         }
 
@@ -622,6 +673,17 @@
 
             pw.print("  Enabled=");
             pw.println(mBatterySaverController.isEnabled());
+            pw.print("    full=");
+            pw.println(mBatterySaverController.isFullEnabled());
+            pw.print("    adaptive=");
+            pw.print(mBatterySaverController.isAdaptiveEnabled());
+            if (mBatterySaverController.isAdaptiveEnabled()) {
+                pw.print(" (advertise=");
+                pw.print(
+                        mBatterySaverController.getBatterySaverPolicy().shouldAdvertiseIsEnabled());
+                pw.print(")");
+            }
+            pw.println();
 
             pw.print("  mLastChangedIntReason=");
             pw.println(mLastChangedIntReason);
@@ -657,6 +719,9 @@
             pw.println(mSettingBatterySaverTriggerThreshold);
             pw.print("  mBatterySaverStickyBehaviourDisabled=");
             pw.println(mBatterySaverStickyBehaviourDisabled);
+
+            pw.print("  mLastAdaptiveBatterySaverChangedExternallyElapsed=");
+            pw.println(mLastAdaptiveBatterySaverChangedExternallyElapsed);
         }
     }
 
@@ -666,6 +731,12 @@
 
             proto.write(BatterySaverStateMachineProto.ENABLED,
                     mBatterySaverController.isEnabled());
+            proto.write(BatterySaverStateMachineProto.IS_FULL_ENABLED,
+                    mBatterySaverController.isFullEnabled());
+            proto.write(BatterySaverStateMachineProto.IS_ADAPTIVE_ENABLED,
+                    mBatterySaverController.isAdaptiveEnabled());
+            proto.write(BatterySaverStateMachineProto.SHOULD_ADVERTISE_IS_ENABLED,
+                    mBatterySaverController.getBatterySaverPolicy().shouldAdvertiseIsEnabled());
 
             proto.write(BatterySaverStateMachineProto.BOOT_COMPLETED, mBootCompleted);
             proto.write(BatterySaverStateMachineProto.SETTINGS_LOADED, mSettingsLoaded);
@@ -692,6 +763,11 @@
                             .SETTING_BATTERY_SAVER_STICKY_AUTO_DISABLE_THRESHOLD,
                     mSettingBatterySaverStickyAutoDisableThreshold);
 
+            proto.write(
+                    BatterySaverStateMachineProto
+                            .LAST_ADAPTIVE_BATTERY_SAVER_CHANGED_EXTERNALLY_ELAPSED,
+                    mLastAdaptiveBatterySaverChangedExternallyElapsed);
+
             proto.end(token);
         }
     }
diff --git a/services/core/java/com/android/server/power/batterysaver/BatterySavingStats.java b/services/core/java/com/android/server/power/batterysaver/BatterySavingStats.java
index 79b44eb..1d30a03 100644
--- a/services/core/java/com/android/server/power/batterysaver/BatterySavingStats.java
+++ b/services/core/java/com/android/server/power/batterysaver/BatterySavingStats.java
@@ -57,9 +57,10 @@
     interface BatterySaverState {
         int OFF = 0;
         int ON = 1;
+        int ADAPTIVE = 2;
 
         int SHIFT = 0;
-        int BITS = 1;
+        int BITS = 2;
         int MASK = (1 << BITS) - 1;
 
         static int fromIndex(int index) {
diff --git a/services/core/java/com/android/server/role/RoleManagerService.java b/services/core/java/com/android/server/role/RoleManagerService.java
index 03ec57b..d72270e 100644
--- a/services/core/java/com/android/server/role/RoleManagerService.java
+++ b/services/core/java/com/android/server/role/RoleManagerService.java
@@ -52,6 +52,7 @@
 import android.service.sms.FinancialSmsService;
 import android.telephony.IFinancialSmsCallback;
 import android.text.TextUtils;
+import android.util.ArrayMap;
 import android.util.ArraySet;
 import android.util.PackageUtils;
 import android.util.Slog;
@@ -145,6 +146,9 @@
         mUserManagerInternal = LocalServices.getService(UserManagerInternal.class);
         mAppOpsManager = context.getSystemService(AppOpsManager.class);
 
+        LocalServices.addService(RoleManagerServiceInternal.class,
+                new RoleManagerServiceInternalImpl());
+
         registerUserRemovedReceiver();
     }
 
@@ -382,6 +386,19 @@
         }
     }
 
+    /**
+     * Get all roles and packages hold them.
+     *
+     * @param user The user to query to roles for
+     *
+     * @return The roles and their holders
+     */
+    @NonNull
+    private ArrayMap<String, ArraySet<String>> getRoleHoldersAsUser(@NonNull UserHandle user) {
+        RoleUserState userState = getOrCreateUserState(user.getIdentifier());
+        return userState.getRoleHolders();
+    }
+
     private class Stub extends IRoleManager.Stub {
 
         @Override
@@ -676,4 +693,16 @@
             }
         }
     }
+
+    /**
+     * Entry point for internal calls into role manager
+     */
+    private final class RoleManagerServiceInternalImpl extends RoleManagerServiceInternal {
+
+        @NonNull
+        @Override
+        public ArrayMap<String, ArraySet<String>> getRoleHoldersAsUser(@NonNull UserHandle user) {
+            return RoleManagerService.this.getRoleHoldersAsUser(user);
+        }
+    }
 }
diff --git a/services/core/java/com/android/server/role/RoleManagerServiceInternal.java b/services/core/java/com/android/server/role/RoleManagerServiceInternal.java
new file mode 100644
index 0000000..3afc3f7
--- /dev/null
+++ b/services/core/java/com/android/server/role/RoleManagerServiceInternal.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.role;
+
+import android.annotation.NonNull;
+import android.os.UserHandle;
+import android.util.ArrayMap;
+import android.util.ArraySet;
+
+/**
+ * Internal calls into {@link RoleManagerService}
+ */
+public abstract class RoleManagerServiceInternal {
+    /**
+     * Get all roles and packages hold them.
+     *
+     * @param user The user to query to roles for
+     *
+     * @return The roles and their holders
+     */
+    @NonNull
+    public abstract ArrayMap<String, ArraySet<String>> getRoleHoldersAsUser(
+            @NonNull UserHandle user);
+}
diff --git a/services/core/java/com/android/server/role/RoleUserState.java b/services/core/java/com/android/server/role/RoleUserState.java
index 02dcc49..bc68dde 100644
--- a/services/core/java/com/android/server/role/RoleUserState.java
+++ b/services/core/java/com/android/server/role/RoleUserState.java
@@ -376,7 +376,7 @@
 
             version = mVersion;
             packagesHash = mPackagesHash;
-            roles = snapshotRolesLocked();
+            roles = getRoleHolders();
         }
 
         AtomicFile atomicFile = new AtomicFile(getFile(mUserId), "roles-" + mUserId);
@@ -541,7 +541,7 @@
 
             version = mVersion;
             packagesHash = mPackagesHash;
-            roles = snapshotRolesLocked();
+            roles = getRoleHolders();
         }
 
         long fieldToken = dumpOutputStream.start(fieldName, fieldId);
@@ -570,17 +570,24 @@
         dumpOutputStream.end(fieldToken);
     }
 
-    @GuardedBy("mLock")
-    private ArrayMap<String, ArraySet<String>> snapshotRolesLocked() {
-        ArrayMap<String, ArraySet<String>> roles = new ArrayMap<>();
-        for (int i = 0, size = CollectionUtils.size(mRoles); i < size; ++i) {
-            String roleName = mRoles.keyAt(i);
-            ArraySet<String> roleHolders = mRoles.valueAt(i);
+    /**
+     * Get the roles and their holders.
+     *
+     * @return A copy of the roles and their holders
+     */
+    @NonNull
+    public ArrayMap<String, ArraySet<String>> getRoleHolders() {
+        synchronized (mLock) {
+            ArrayMap<String, ArraySet<String>> roles = new ArrayMap<>();
+            for (int i = 0, size = CollectionUtils.size(mRoles); i < size; ++i) {
+                String roleName = mRoles.keyAt(i);
+                ArraySet<String> roleHolders = mRoles.valueAt(i);
 
-            roleHolders = new ArraySet<>(roleHolders);
-            roles.put(roleName, roleHolders);
+                roleHolders = new ArraySet<>(roleHolders);
+                roles.put(roleName, roleHolders);
+            }
+            return roles;
         }
-        return roles;
     }
 
     /**
diff --git a/services/core/java/com/android/server/rollback/RollbackManagerServiceImpl.java b/services/core/java/com/android/server/rollback/RollbackManagerServiceImpl.java
index 5eb137b..4613400 100644
--- a/services/core/java/com/android/server/rollback/RollbackManagerServiceImpl.java
+++ b/services/core/java/com/android/server/rollback/RollbackManagerServiceImpl.java
@@ -783,7 +783,7 @@
     }
 
     @Override
-    public void restoreUserData(String packageName, int userId, int appId, long ceDataInode,
+    public void restoreUserData(String packageName, int[] userIds, int appId, long ceDataInode,
             String seInfo, int token) {
         if (Binder.getCallingUid() != Process.SYSTEM_UID) {
             throw new SecurityException("restoureUserData may only be called by the system.");
@@ -791,23 +791,26 @@
 
         getHandler().post(() -> {
             final RollbackData rollbackData = getRollbackForPackage(packageName);
-            final boolean changedRollbackData = mUserdataHelper.restoreAppData(packageName,
-                    rollbackData, userId, appId, ceDataInode, seInfo);
+            for (int userId : userIds) {
+                final boolean changedRollbackData = mUserdataHelper.restoreAppData(packageName,
+                        rollbackData, userId, appId, ceDataInode, seInfo);
+
+                // We've updated metadata about this rollback, so save it to flash.
+                if (changedRollbackData) {
+                    try {
+                        mRollbackStore.saveAvailableRollback(rollbackData);
+                    } catch (IOException ioe) {
+                        // TODO(narayan): What is the right thing to do here ? This isn't a fatal
+                        // error, since it will only result in us trying to restore data again,
+                        // which will be a no-op if there's no data available.
+                        Log.e(TAG, "Unable to save available rollback: " + packageName, ioe);
+                    }
+                }
+            }
+
             final PackageManagerInternal pmi = LocalServices.getService(
                     PackageManagerInternal.class);
             pmi.finishPackageInstall(token, false);
-
-            // We've updated metadata about this rollback, so save it to flash.
-            if (changedRollbackData) {
-                try {
-                    mRollbackStore.saveAvailableRollback(rollbackData);
-                } catch (IOException ioe) {
-                    // TODO(narayan): What is the right thing to do here ? This isn't a fatal error,
-                    // since it will only result in us trying to restore data again, which will be
-                    // a no-op if there's no data available.
-                    Log.e(TAG, "Unable to save available rollback: " + packageName, ioe);
-                }
-            }
         });
     }
 
diff --git a/services/core/java/com/android/server/stats/StatsCompanionService.java b/services/core/java/com/android/server/stats/StatsCompanionService.java
index c6d2870..2be78fe3 100644
--- a/services/core/java/com/android/server/stats/StatsCompanionService.java
+++ b/services/core/java/com/android/server/stats/StatsCompanionService.java
@@ -23,6 +23,7 @@
 import static com.android.server.am.MemoryStatUtil.readMemoryStatFromProcfs;
 import static com.android.server.am.MemoryStatUtil.readRssHighWaterMarkFromProcfs;
 
+import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.app.ActivityManagerInternal;
 import android.app.AlarmManager;
@@ -83,6 +84,7 @@
 import android.telephony.ModemActivityInfo;
 import android.telephony.TelephonyManager;
 import android.util.ArrayMap;
+import android.util.ArraySet;
 import android.util.Log;
 import android.util.Slog;
 import android.util.StatsLog;
@@ -112,6 +114,7 @@
 import com.android.server.SystemService;
 import com.android.server.SystemServiceManager;
 import com.android.server.am.MemoryStatUtil.MemoryStat;
+import com.android.server.role.RoleManagerServiceInternal;
 import com.android.server.storage.DiskStatsFileLogger;
 import com.android.server.storage.DiskStatsLoggingService;
 
@@ -1781,6 +1784,60 @@
     }
 
     /**
+     * Add a RoleHolder atom for each package that holds a role.
+     *
+     * @param elapsedNanos the time since boot
+     * @param wallClockNanos the time on the clock
+     * @param pulledData the data sink to write to
+     */
+    private void pullRoleHolders(long elapsedNanos, final long wallClockNanos,
+            @NonNull List<StatsLogEventWrapper> pulledData) {
+        long callingToken = Binder.clearCallingIdentity();
+        try {
+            PackageManager pm = mContext.getPackageManager();
+            RoleManagerServiceInternal rm =
+                    LocalServices.getService(RoleManagerServiceInternal.class);
+
+            List<UserInfo> users = mContext.getSystemService(UserManager.class).getUsers();
+
+            int numUsers = users.size();
+            for (int userNum = 0; userNum < numUsers; userNum++) {
+                UserHandle user = users.get(userNum).getUserHandle();
+
+                ArrayMap<String, ArraySet<String>> roles = rm.getRoleHoldersAsUser(user);
+
+                int numRoles = roles.size();
+                for (int roleNum = 0; roleNum < numRoles; roleNum++) {
+                    String roleName = roles.keyAt(roleNum);
+                    ArraySet<String> holders = roles.valueAt(roleNum);
+
+                    int numHolders = holders.size();
+                    for (int holderNum = 0; holderNum < numHolders; holderNum++) {
+                        String holderName = holders.valueAt(holderNum);
+
+                        PackageInfo pkg;
+                        try {
+                            pkg = pm.getPackageInfoAsUser(holderName, 0, user.getIdentifier());
+                        } catch (PackageManager.NameNotFoundException e) {
+                            Log.w(TAG, "Role holder " + holderName + " not found");
+                            return;
+                        }
+
+                        StatsLogEventWrapper e = new StatsLogEventWrapper(StatsLog.ROLE_HOLDER,
+                                elapsedNanos, wallClockNanos);
+                        e.writeInt(pkg.applicationInfo.uid);
+                        e.writeString(holderName);
+                        e.writeString(roleName);
+                        pulledData.add(e);
+                    }
+                }
+            }
+        } finally {
+            Binder.restoreCallingIdentity(callingToken);
+        }
+    }
+
+    /**
      * Pulls various data.
      */
     @Override // Binder call
@@ -1954,6 +2011,10 @@
                 pullDebugFailingElapsedClock(tagId, elapsedNanos, wallClockNanos, ret);
                 break;
             }
+            case StatsLog.ROLE_HOLDER: {
+                pullRoleHolders(elapsedNanos, wallClockNanos, ret);
+                break;
+            }
             default:
                 Slog.w(TAG, "No such tagId data as " + tagId);
                 return null;
diff --git a/services/core/java/com/android/server/wallpaper/WallpaperManagerService.java b/services/core/java/com/android/server/wallpaper/WallpaperManagerService.java
index b0ef8a0..071dde7 100644
--- a/services/core/java/com/android/server/wallpaper/WallpaperManagerService.java
+++ b/services/core/java/com/android/server/wallpaper/WallpaperManagerService.java
@@ -2243,12 +2243,9 @@
         synchronized (mLock) {
             mInAmbientMode = inAmbientMode;
             final WallpaperData data = mWallpaperMap.get(mCurrentUserId);
-            final boolean hasConnection = data != null && data.connection != null;
-            final WallpaperInfo info = hasConnection ? data.connection.mInfo : null;
-
             // The wallpaper info is null for image wallpaper, also use the engine in this case.
-            if (hasConnection && (info == null && isAodImageWallpaperEnabled()
-                    || info != null && info.supportsAmbientMode())) {
+            if (data != null && data.connection != null && (data.connection.mInfo == null
+                    || data.connection.mInfo.supportsAmbientMode())) {
                 // TODO(multi-display) Extends this method with specific display.
                 engine = data.connection.getDisplayConnectorOrCreate(DEFAULT_DISPLAY).mEngine;
             } else {
diff --git a/services/core/java/com/android/server/wm/ActivityStarter.java b/services/core/java/com/android/server/wm/ActivityStarter.java
index 95a6f71..d1cd1db 100644
--- a/services/core/java/com/android/server/wm/ActivityStarter.java
+++ b/services/core/java/com/android/server/wm/ActivityStarter.java
@@ -926,6 +926,10 @@
         if (callerApp != null && callerApp.hasForegroundActivities()) {
             return false;
         }
+        // don't abort if the callerApp is instrumenting with background activity starts privileges
+        if (callerApp != null && callerApp.isInstrumentingWithBackgroundActivityStartPrivileges()) {
+            return false;
+        }
         // don't abort if the callingUid is in the foreground or is a persistent system process
         final int callingUidProcState = mService.getUidStateLocked(callingUid);
         final boolean callingUidHasAnyVisibleWindow =
diff --git a/services/core/java/com/android/server/wm/ActivityTaskManagerService.java b/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
index 5fabde4..09ef4b9 100644
--- a/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
+++ b/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
@@ -349,6 +349,14 @@
 
     /* Global service lock used by the package the owns this service. */
     final WindowManagerGlobalLock mGlobalLock = new WindowManagerGlobalLock();
+    /**
+     * It is the same instance as {@link mGlobalLock}, just declared as a type that the
+     * locked-region-code-injection does't recognize it. It is used to skip wrapping priority
+     * booster for places that are already in the scope of another booster (e.g. computing oom-adj).
+     *
+     * @see WindowManagerThreadPriorityBooster
+     */
+    final Object mGlobalLockWithoutBoost = mGlobalLock;
     ActivityStackSupervisor mStackSupervisor;
     RootActivityContainer mRootActivityContainer;
     WindowManagerService mWindowManager;
@@ -6837,7 +6845,7 @@
 
         @Override
         public WindowProcessController getTopApp() {
-            synchronized (mGlobalLock) {
+            synchronized (mGlobalLockWithoutBoost) {
                 final ActivityRecord top = mRootActivityContainer.getTopResumedActivity();
                 return top != null ? top.app : null;
             }
@@ -6845,7 +6853,7 @@
 
         @Override
         public void rankTaskLayersIfNeeded() {
-            synchronized (mGlobalLock) {
+            synchronized (mGlobalLockWithoutBoost) {
                 if (mRootActivityContainer != null) {
                     mRootActivityContainer.rankTaskLayersIfNeeded();
                 }
@@ -6889,28 +6897,28 @@
 
         @Override
         public void onUidActive(int uid, int procState) {
-            synchronized (mGlobalLock) {
+            synchronized (mGlobalLockWithoutBoost) {
                 mActiveUids.put(uid, procState);
             }
         }
 
         @Override
         public void onUidInactive(int uid) {
-            synchronized (mGlobalLock) {
+            synchronized (mGlobalLockWithoutBoost) {
                 mActiveUids.remove(uid);
             }
         }
 
         @Override
         public void onActiveUidsCleared() {
-            synchronized (mGlobalLock) {
+            synchronized (mGlobalLockWithoutBoost) {
                 mActiveUids.clear();
             }
         }
 
         @Override
         public void onUidProcStateChanged(int uid, int procState) {
-            synchronized (mGlobalLock) {
+            synchronized (mGlobalLockWithoutBoost) {
                 if (mActiveUids.get(uid) != null) {
                     mActiveUids.put(uid, procState);
                 }
@@ -6919,14 +6927,14 @@
 
         @Override
         public void onUidAddedToPendingTempWhitelist(int uid, String tag) {
-            synchronized (mGlobalLock) {
+            synchronized (mGlobalLockWithoutBoost) {
                 mPendingTempWhitelist.put(uid, tag);
             }
         }
 
         @Override
         public void onUidRemovedFromPendingTempWhitelist(int uid) {
-            synchronized (mGlobalLock) {
+            synchronized (mGlobalLockWithoutBoost) {
                 mPendingTempWhitelist.remove(uid);
             }
         }
diff --git a/services/core/java/com/android/server/wm/BlackFrame.java b/services/core/java/com/android/server/wm/BlackFrame.java
index fd72a4a..c90f5bf 100644
--- a/services/core/java/com/android/server/wm/BlackFrame.java
+++ b/services/core/java/com/android/server/wm/BlackFrame.java
@@ -48,7 +48,7 @@
 
             surface = dc.makeOverlay()
                     .setName("BlackSurface")
-                    .setColorLayer()
+                    .setColorLayer(true)
                     .setParent(null) // TODO: Work-around for b/69259549
                     .build();
             transaction.setWindowCrop(surface, w, h);
diff --git a/services/core/java/com/android/server/wm/Dimmer.java b/services/core/java/com/android/server/wm/Dimmer.java
index 8f6b67a..c39060e 100644
--- a/services/core/java/com/android/server/wm/Dimmer.java
+++ b/services/core/java/com/android/server/wm/Dimmer.java
@@ -164,7 +164,7 @@
     private SurfaceControl makeDimLayer() {
         return mHost.makeChildSurface(null)
                 .setParent(mHost.getSurfaceControl())
-                .setColorLayer()
+                .setColorLayer(true)
                 .setName("Dim Layer for - " + mHost.getName())
                 .build();
     }
diff --git a/services/core/java/com/android/server/wm/DisplayContent.java b/services/core/java/com/android/server/wm/DisplayContent.java
index 080f965..df32df0 100644
--- a/services/core/java/com/android/server/wm/DisplayContent.java
+++ b/services/core/java/com/android/server/wm/DisplayContent.java
@@ -31,11 +31,14 @@
 import static android.view.Display.FLAG_PRIVATE;
 import static android.view.Display.INVALID_DISPLAY;
 import static android.view.InsetsState.TYPE_IME;
+import static android.view.InsetsState.TYPE_NAVIGATION_BAR;
+import static android.view.InsetsState.TYPE_TOP_BAR;
 import static android.view.Surface.ROTATION_0;
 import static android.view.Surface.ROTATION_180;
 import static android.view.Surface.ROTATION_270;
 import static android.view.Surface.ROTATION_90;
 import static android.view.View.GONE;
+import static android.view.ViewRootImpl.NEW_INSETS_MODE_FULL;
 import static android.view.WindowManager.DOCKED_BOTTOM;
 import static android.view.WindowManager.DOCKED_INVALID;
 import static android.view.WindowManager.DOCKED_TOP;
@@ -170,6 +173,7 @@
 import android.view.SurfaceControl.Transaction;
 import android.view.SurfaceSession;
 import android.view.View;
+import android.view.ViewRootImpl;
 import android.view.WindowManager;
 import android.view.WindowManagerPolicyConstants.PointerEventListener;
 
@@ -892,9 +896,7 @@
         mDividerControllerLocked = new DockedStackDividerController(service, this);
         mPinnedStackControllerLocked = new PinnedStackController(service, this);
 
-        final SurfaceControl.Builder b = mWmService.makeSurfaceBuilder(mSession)
-                .setOpaque(true)
-                .setContainerLayer();
+        final SurfaceControl.Builder b = mWmService.makeSurfaceBuilder(mSession).setOpaque(true);
         mWindowingLayer = b.setName("Display Root").build();
         mOverlayLayer = b.setName("Display Overlays").build();
 
@@ -4598,7 +4600,7 @@
     @Override
     SurfaceControl.Builder makeChildSurface(WindowContainer child) {
         SurfaceSession s = child != null ? child.getSession() : getSession();
-        final SurfaceControl.Builder b = mWmService.makeSurfaceBuilder(s).setContainerLayer();
+        final SurfaceControl.Builder b = mWmService.makeSurfaceBuilder(s);
         if (child == null) {
             return b;
         }
diff --git a/services/core/java/com/android/server/wm/DragState.java b/services/core/java/com/android/server/wm/DragState.java
index 2b2231a..3f77e1c 100644
--- a/services/core/java/com/android/server/wm/DragState.java
+++ b/services/core/java/com/android/server/wm/DragState.java
@@ -164,7 +164,7 @@
 
         if (mInputSurface == null) {
             mInputSurface = mService.makeSurfaceBuilder(mService.mRoot.getDisplayContent(displayId)
-                    .getSession()).setContainerLayer()
+                    .getSession()).setContainerLayer(true)
                     .setName("Drag and Drop Input Consumer").build();
         }
         final InputWindowHandle h = getInputWindowHandle();
diff --git a/services/core/java/com/android/server/wm/InputConsumerImpl.java b/services/core/java/com/android/server/wm/InputConsumerImpl.java
index ab95e4b..4df5a0b 100644
--- a/services/core/java/com/android/server/wm/InputConsumerImpl.java
+++ b/services/core/java/com/android/server/wm/InputConsumerImpl.java
@@ -22,11 +22,13 @@
 import android.os.Process;
 import android.os.RemoteException;
 import android.os.UserHandle;
-import android.view.InputApplicationHandle;
 import android.view.InputChannel;
+import android.view.WindowManager;
+
+import android.view.InputApplicationHandle;
 import android.view.InputWindowHandle;
 import android.view.SurfaceControl;
-import android.view.WindowManager;
+import android.util.Slog;
 
 import java.io.PrintWriter;
 
@@ -87,7 +89,7 @@
         mWindowHandle.scaleFactor = 1.0f;
 
         mInputSurface = mService.makeSurfaceBuilder(mService.mRoot.getDisplayContent(displayId)
-                .getSession()).setContainerLayer().setName("Input Consumer " + name)
+                .getSession()).setContainerLayer(true).setName("Input Consumer " + name)
                 .build();
     }
 
diff --git a/services/core/java/com/android/server/wm/Letterbox.java b/services/core/java/com/android/server/wm/Letterbox.java
index 18fce67..434084c 100644
--- a/services/core/java/com/android/server/wm/Letterbox.java
+++ b/services/core/java/com/android/server/wm/Letterbox.java
@@ -149,7 +149,7 @@
 
         private void createSurface() {
             mSurface = mFactory.get().setName("Letterbox - " + mType)
-                    .setFlags(HIDDEN).setColorLayer().build();
+                    .setFlags(HIDDEN).setColorLayer(true).build();
             mSurface.setLayer(-1);
             mSurface.setColor(new float[]{0, 0, 0});
         }
diff --git a/services/core/java/com/android/server/wm/TaskPositioningController.java b/services/core/java/com/android/server/wm/TaskPositioningController.java
index cdcb857..9163165 100644
--- a/services/core/java/com/android/server/wm/TaskPositioningController.java
+++ b/services/core/java/com/android/server/wm/TaskPositioningController.java
@@ -83,7 +83,7 @@
         final DisplayContent dc = mService.mRoot.getDisplayContent(displayId);
         if (mInputSurface == null) {
             mInputSurface = mService.makeSurfaceBuilder(dc.getSession())
-                    .setContainerLayer()
+                    .setContainerLayer(true)
                     .setName("Drag and Drop Input Consumer").build();
         }
 
diff --git a/services/core/java/com/android/server/wm/TaskSnapshotPersister.java b/services/core/java/com/android/server/wm/TaskSnapshotPersister.java
index e6d646c..3c9a46b 100644
--- a/services/core/java/com/android/server/wm/TaskSnapshotPersister.java
+++ b/services/core/java/com/android/server/wm/TaskSnapshotPersister.java
@@ -26,6 +26,7 @@
 import android.app.ActivityManager.TaskSnapshot;
 import android.graphics.Bitmap;
 import android.graphics.Bitmap.Config;
+import android.hardware.HardwareBuffer;
 import android.os.Process;
 import android.os.SystemClock;
 import android.util.ArraySet;
@@ -359,7 +360,11 @@
         }
 
         boolean writeBuffer() {
-            final Bitmap bitmap = Bitmap.createHardwareBitmap(mSnapshot.getSnapshot());
+            // TODO(b/116112787) TaskSnapshot needs bookkeep the ColorSpace of the
+            // hardware bitmap when created.
+            final Bitmap bitmap = Bitmap.wrapHardwareBuffer(
+                    HardwareBuffer.createFromGraphicBuffer(mSnapshot.getSnapshot()),
+                    mSnapshot.getColorSpace());
             if (bitmap == null) {
                 Slog.e(TAG, "Invalid task snapshot hw bitmap");
                 return false;
diff --git a/services/core/java/com/android/server/wm/TaskSnapshotSurface.java b/services/core/java/com/android/server/wm/TaskSnapshotSurface.java
index 938c8b4..2d3e3ae 100644
--- a/services/core/java/com/android/server/wm/TaskSnapshotSurface.java
+++ b/services/core/java/com/android/server/wm/TaskSnapshotSurface.java
@@ -61,12 +61,12 @@
 import android.util.Slog;
 import android.view.DisplayCutout;
 import android.view.IWindowSession;
-import android.view.InsetsState;
 import android.view.Surface;
 import android.view.SurfaceControl;
 import android.view.SurfaceSession;
 import android.view.View;
 import android.view.ViewGroup.LayoutParams;
+import android.view.InsetsState;
 import android.view.WindowManager;
 import android.view.WindowManagerGlobal;
 
diff --git a/services/core/java/com/android/server/wm/TaskStack.java b/services/core/java/com/android/server/wm/TaskStack.java
index 59549e0..bfef1e9 100644
--- a/services/core/java/com/android/server/wm/TaskStack.java
+++ b/services/core/java/com/android/server/wm/TaskStack.java
@@ -818,7 +818,7 @@
 
         updateSurfaceBounds();
         if (mAnimationBackgroundSurface == null) {
-            mAnimationBackgroundSurface = makeChildSurface(null).setColorLayer()
+            mAnimationBackgroundSurface = makeChildSurface(null).setColorLayer(true)
                     .setName("animation background stackId=" + mStackId)
                     .build();
         }
diff --git a/services/core/java/com/android/server/wm/WallpaperController.java b/services/core/java/com/android/server/wm/WallpaperController.java
index c15afc5..74fb3fa 100644
--- a/services/core/java/com/android/server/wm/WallpaperController.java
+++ b/services/core/java/com/android/server/wm/WallpaperController.java
@@ -34,8 +34,10 @@
 import static com.android.server.wm.WindowManagerService.H.WALLPAPER_DRAW_PENDING_TIMEOUT;
 
 import android.graphics.Bitmap;
+import android.graphics.ColorSpace;
 import android.graphics.GraphicBuffer;
 import android.graphics.Rect;
+import android.hardware.HardwareBuffer;
 import android.os.Bundle;
 import android.os.Debug;
 import android.os.IBinder;
@@ -729,7 +731,10 @@
             Slog.w(TAG_WM, "Failed to screenshot wallpaper");
             return null;
         }
-        return Bitmap.createHardwareBitmap(wallpaperBuffer);
+        // TODO(b/116112787) Now that hardware bitmap creation can take color space, we
+        // should continue to fix screenshot.
+        return Bitmap.wrapHardwareBuffer(HardwareBuffer.createFromGraphicBuffer(wallpaperBuffer),
+                                         ColorSpace.get(ColorSpace.Named.SRGB));
     }
 
     private WindowState getTopVisibleWallpaper() {
diff --git a/services/core/java/com/android/server/wm/WindowProcessController.java b/services/core/java/com/android/server/wm/WindowProcessController.java
index 030cc05..8da39b6 100644
--- a/services/core/java/com/android/server/wm/WindowProcessController.java
+++ b/services/core/java/com/android/server/wm/WindowProcessController.java
@@ -138,6 +138,8 @@
     private volatile boolean mDebugging;
     // Active instrumentation running in process?
     private volatile boolean mInstrumenting;
+    // Active instrumentation with background activity starts privilege running in process?
+    private volatile boolean mInstrumentingWithBackgroundActivityStartPrivileges;
     // This process it perceptible by the user.
     private volatile boolean mPerceptible;
     // Set to true when process was launched with a wrapper attached
@@ -370,6 +372,23 @@
         return mInstrumenting;
     }
 
+    /**
+     * {@see isInstrumentingWithBackgroundActivityStartPrivileges}
+     */
+    public void setInstrumentingWithBackgroundActivityStartPrivileges(
+            boolean instrumentingWithBackgroundActivityStartPrivileges) {
+        mInstrumentingWithBackgroundActivityStartPrivileges =
+                instrumentingWithBackgroundActivityStartPrivileges;
+    }
+
+    /**
+     * @return true if the instrumentation was started by a holder of
+     * START_ACTIVITIES_FROM_BACKGROUND permission
+     */
+    boolean isInstrumentingWithBackgroundActivityStartPrivileges() {
+        return mInstrumentingWithBackgroundActivityStartPrivileges;
+    }
+
     public void setPerceptible(boolean perceptible) {
         mPerceptible = perceptible;
     }
@@ -394,13 +413,13 @@
     }
 
     public void addPackage(String packageName) {
-        synchronized (mAtm.mGlobalLock) {
+        synchronized (mAtm.mGlobalLockWithoutBoost) {
             mPkgList.add(packageName);
         }
     }
 
     public void clearPackageList() {
-        synchronized (mAtm.mGlobalLock) {
+        synchronized (mAtm.mGlobalLockWithoutBoost) {
             mPkgList.clear();
         }
     }
@@ -422,20 +441,18 @@
         }
     }
 
-    public void clearActivities() {
-        synchronized (mAtm.mGlobalLock) {
-            mActivities.clear();
-        }
+    void clearActivities() {
+        mActivities.clear();
     }
 
     public boolean hasActivities() {
-        synchronized (mAtm.mGlobalLock) {
+        synchronized (mAtm.mGlobalLockWithoutBoost) {
             return !mActivities.isEmpty();
         }
     }
 
     public boolean hasVisibleActivities() {
-        synchronized (mAtm.mGlobalLock) {
+        synchronized (mAtm.mGlobalLockWithoutBoost) {
             for (int i = mActivities.size() - 1; i >= 0; --i) {
                 final ActivityRecord r = mActivities.get(i);
                 if (r.visible) {
@@ -447,7 +464,7 @@
     }
 
     public boolean hasActivitiesOrRecentTasks() {
-        synchronized (mAtm.mGlobalLock) {
+        synchronized (mAtm.mGlobalLockWithoutBoost) {
             return !mActivities.isEmpty() || !mRecentTasks.isEmpty();
         }
     }
@@ -462,15 +479,13 @@
         }
     }
 
-    public void finishActivities() {
-        synchronized (mAtm.mGlobalLock) {
-            ArrayList<ActivityRecord> activities = new ArrayList<>(mActivities);
-            for (int i = 0; i < activities.size(); i++) {
-                final ActivityRecord r = activities.get(i);
-                if (!r.finishing && r.isInStackLocked()) {
-                    r.getActivityStack().finishActivityLocked(r, Activity.RESULT_CANCELED,
-                            null, "finish-heavy", true);
-                }
+    void finishActivities() {
+        ArrayList<ActivityRecord> activities = new ArrayList<>(mActivities);
+        for (int i = 0; i < activities.size(); i++) {
+            final ActivityRecord r = activities.get(i);
+            if (!r.finishing && r.isInStackLocked()) {
+                r.getActivityStack().finishActivityLocked(r, Activity.RESULT_CANCELED,
+                        null, "finish-heavy", true);
             }
         }
     }
@@ -531,15 +546,13 @@
     }
 
 
-    public void updateIntentForHeavyWeightActivity(Intent intent) {
-        synchronized (mAtm.mGlobalLock) {
-            if (mActivities.isEmpty()) {
-                return;
-            }
-            ActivityRecord hist = mActivities.get(0);
-            intent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP, hist.packageName);
-            intent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK, hist.getTaskRecord().taskId);
+    void updateIntentForHeavyWeightActivity(Intent intent) {
+        if (mActivities.isEmpty()) {
+            return;
         }
+        ActivityRecord hist = mActivities.get(0);
+        intent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP, hist.packageName);
+        intent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK, hist.getTaskRecord().taskId);
     }
 
     boolean shouldKillProcessForRemovedTask(TaskRecord tr) {
@@ -609,7 +622,7 @@
     }
 
     public int computeOomAdjFromActivities(int minTaskLayer, ComputeOomAdjCallback callback) {
-        synchronized (mAtm.mGlobalLock) {
+        synchronized (mAtm.mGlobalLockWithoutBoost) {
             final int activitiesSize = mActivities.size();
             for (int j = 0; j < activitiesSize; j++) {
                 final ActivityRecord r = mActivities.get(j);
@@ -842,18 +855,16 @@
     }
 
     public boolean hasRecentTasks() {
-        synchronized (mAtm.mGlobalLock) {
+        synchronized (mAtm.mGlobalLockWithoutBoost) {
             return !mRecentTasks.isEmpty();
         }
     }
 
-    public void clearRecentTasks() {
-        synchronized (mAtm.mGlobalLock) {
-            for (int i = mRecentTasks.size() - 1; i >= 0; i--) {
-                mRecentTasks.get(i).clearRootProcess();
-            }
-            mRecentTasks.clear();
+    void clearRecentTasks() {
+        for (int i = mRecentTasks.size() - 1; i >= 0; i--) {
+            mRecentTasks.get(i).clearRootProcess();
         }
+        mRecentTasks.clear();
     }
 
     public void appEarlyNotResponding(String annotation, Runnable killAppCallback) {
@@ -907,19 +918,19 @@
     }
 
     public void onTopProcChanged() {
-        synchronized (mAtm.mGlobalLock) {
+        synchronized (mAtm.mGlobalLockWithoutBoost) {
             mAtm.mVrController.onTopProcChangedLocked(this);
         }
     }
 
     public boolean isHomeProcess() {
-        synchronized (mAtm.mGlobalLock) {
+        synchronized (mAtm.mGlobalLockWithoutBoost) {
             return this == mAtm.mHomeProcess;
         }
     }
 
     public boolean isPreviousProcess() {
-        synchronized (mAtm.mGlobalLock) {
+        synchronized (mAtm.mGlobalLockWithoutBoost) {
             return this == mAtm.mPreviousProcess;
         }
     }
diff --git a/services/core/jni/com_android_server_security_VerityUtils.cpp b/services/core/jni/com_android_server_security_VerityUtils.cpp
index 0d888dc..988d75c 100644
--- a/services/core/jni/com_android_server_security_VerityUtils.cpp
+++ b/services/core/jni/com_android_server_security_VerityUtils.cpp
@@ -32,9 +32,67 @@
 // TODO(112037636): Always include once fsverity.h is upstreamed.
 #if __has_include(<linux/fsverity.h>)
 #include <linux/fsverity.h>
-const int kSha256Bytes = 32;
+#else
+
+// Before fs-verity is upstreamed, use the current snapshot for development.
+// https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git/tree/include/uapi/linux/fsverity.h?h=fsverity
+
+#include <linux/limits.h>
+#include <linux/ioctl.h>
+#include <linux/types.h>
+
+struct fsverity_digest {
+    __u16 digest_algorithm;
+    __u16 digest_size; /* input/output */
+    __u8 digest[];
+};
+
+#define FS_IOC_ENABLE_VERITY	_IO('f', 133)
+#define FS_IOC_MEASURE_VERITY	_IOWR('f', 134, struct fsverity_digest)
+
+#define FS_VERITY_MAGIC		"FSVerity"
+
+#define FS_VERITY_ALG_SHA256	1
+
+struct fsverity_descriptor {
+    __u8 magic[8];		/* must be FS_VERITY_MAGIC */
+    __u8 major_version;	/* must be 1 */
+    __u8 minor_version;	/* must be 0 */
+    __u8 log_data_blocksize;/* log2(data-bytes-per-hash), e.g. 12 for 4KB */
+    __u8 log_tree_blocksize;/* log2(tree-bytes-per-hash), e.g. 12 for 4KB */
+    __le16 data_algorithm;	/* hash algorithm for data blocks */
+    __le16 tree_algorithm;	/* hash algorithm for tree blocks */
+    __le32 flags;		/* flags */
+    __le32 __reserved1;	/* must be 0 */
+    __le64 orig_file_size;	/* size of the original file data */
+    __le16 auth_ext_count;	/* number of authenticated extensions */
+    __u8 __reserved2[30];	/* must be 0 */
+};
+
+#define FS_VERITY_EXT_ROOT_HASH		1
+#define FS_VERITY_EXT_PKCS7_SIGNATURE	3
+
+struct fsverity_extension {
+    __le32 length;
+    __le16 type;		/* Type of this extension (see codes above) */
+    __le16 __reserved;	/* Reserved, must be 0 */
+};
+
+struct fsverity_digest_disk {
+    __le16 digest_algorithm;
+    __le16 digest_size;
+    __u8 digest[];
+};
+
+struct fsverity_footer {
+    __le32 desc_reverse_offset;	/* distance to fsverity_descriptor */
+    __u8 magic[8];			/* FS_VERITY_MAGIC */
+} __packed;
+
 #endif
 
+const int kSha256Bytes = 32;
+
 namespace android {
 
 namespace {
@@ -73,7 +131,6 @@
 };
 
 int enableFsverity(JNIEnv* env, jobject /* clazz */, jstring filePath) {
-#if __has_include(<linux/fsverity.h>)
     const char* path = env->GetStringUTFChars(filePath, nullptr);
     ::android::base::unique_fd rfd(open(path, O_RDONLY | O_CLOEXEC));
     if (rfd.get() < 0) {
@@ -83,14 +140,9 @@
       return errno;
     }
     return 0;
-#else
-    LOG_ALWAYS_FATAL("fs-verity is used while not enabled");
-    return ENOSYS;
-#endif
 }
 
 int measureFsverity(JNIEnv* env, jobject /* clazz */, jstring filePath) {
-#if __has_include(<linux/fsverity.h>)
     auto raii = JavaByteArrayHolder::newArray(env, sizeof(fsverity_digest) + kSha256Bytes);
     fsverity_digest* data = reinterpret_cast<fsverity_digest*>(raii->getRaw());
     data->digest_size = kSha256Bytes;  // the only input/output parameter
@@ -104,14 +156,9 @@
       return errno;
     }
     return 0;
-#else
-    LOG_ALWAYS_FATAL("fs-verity is used while not enabled");
-    return ENOSYS;
-#endif
 }
 
 jbyteArray constructFsveritySignedData(JNIEnv* env, jobject /* clazz */, jbyteArray digest) {
-#if __has_include(<linux/fsverity.h>)
     auto raii = JavaByteArrayHolder::newArray(env, sizeof(fsverity_digest_disk) + kSha256Bytes);
     fsverity_digest_disk* data = reinterpret_cast<fsverity_digest_disk*>(raii->getRaw());
 
@@ -126,15 +173,10 @@
     memcpy(data->digest, src, kSha256Bytes);
 
     return raii->release();
-#else
-    LOG_ALWAYS_FATAL("fs-verity is used while not enabled");
-    return 0;
-#endif
 }
 
 
 jbyteArray constructFsverityDescriptor(JNIEnv* env, jobject /* clazz */, jlong fileSize) {
-#if __has_include(<linux/fsverity.h>)
     auto raii = JavaByteArrayHolder::newArray(env, sizeof(fsverity_descriptor));
     fsverity_descriptor* desc = reinterpret_cast<fsverity_descriptor*>(raii->getRaw());
 
@@ -150,15 +192,10 @@
     desc->auth_ext_count = 1;
 
     return raii->release();
-#else
-    LOG_ALWAYS_FATAL("fs-verity is used while not enabled");
-    return 0;
-#endif
 }
 
 jbyteArray constructFsverityExtension(JNIEnv* env, jobject /* clazz */, jshort extensionId,
         jint extensionDataSize) {
-#if __has_include(<linux/fsverity.h>)
     auto raii = JavaByteArrayHolder::newArray(env, sizeof(fsverity_extension));
     fsverity_extension* ext = reinterpret_cast<fsverity_extension*>(raii->getRaw());
 
@@ -166,15 +203,10 @@
     ext->type = extensionId;
 
     return raii->release();
-#else
-    LOG_ALWAYS_FATAL("fs-verity is used while not enabled");
-    return 0;
-#endif
 }
 
 jbyteArray constructFsverityFooter(JNIEnv* env, jobject /* clazz */,
         jint offsetToDescriptorHead) {
-#if __has_include(<linux/fsverity.h>)
     auto raii = JavaByteArrayHolder::newArray(env, sizeof(fsverity_footer));
     fsverity_footer* footer = reinterpret_cast<fsverity_footer*>(raii->getRaw());
 
@@ -182,10 +214,6 @@
     memcpy(footer->magic, FS_VERITY_MAGIC, sizeof(footer->magic));
 
     return raii->release();
-#else
-    LOG_ALWAYS_FATAL("fs-verity is used while not enabled");
-    return 0;
-#endif
 }
 
 const JNINativeMethod sMethods[] = {
diff --git a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java
index a01a026..8f5ce74 100644
--- a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java
+++ b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java
@@ -8364,16 +8364,22 @@
                 != PackageManager.PERMISSION_GRANTED) {
             return false;
         }
-        // Allow access to the device owner.
+
+        // Allow access to the device owner or delegate cert installer.
         ComponentName deviceOwner = getDeviceOwnerComponent(true);
-        if (deviceOwner != null && deviceOwner.getPackageName().equals(packageName)) {
+        if (deviceOwner != null && (deviceOwner.getPackageName().equals(packageName)
+                    || isCallerDelegate(packageName, uid, DELEGATION_CERT_INSTALL))) {
             return true;
         }
-        // Allow access to the profile owner for the specified user.
+        // Allow access to the profile owner for the specified user, or delegate cert installer
         ComponentName profileOwner = getProfileOwnerAsUser(userHandle);
-        if (profileOwner != null && profileOwner.getPackageName().equals(packageName)) {
+        if (profileOwner != null && (profileOwner.getPackageName().equals(packageName)
+                    || isCallerDelegate(packageName, uid, DELEGATION_CERT_INSTALL))) {
             return true;
         }
+
+        Log.w(LOG_TAG, String.format("Package if %s (uid=%d, pid=%d) cannot access Device IDs",
+                    packageName, uid, pid));
         return false;
     }
 
@@ -14072,6 +14078,10 @@
 
         enforceCrossUsersPermission(userHandle);
         synchronized (getLockObject()) {
+            if (mInjector.settingsSecureGetIntForUser(
+                    Settings.Secure.CROSS_PROFILE_CALENDAR_ENABLED, 0, userHandle) == 0) {
+                return false;
+            }
             final ActiveAdmin admin = getProfileOwnerAdminLocked(userHandle);
             if (admin != null) {
                 if (admin.mCrossProfileCalendarPackages == null) {
diff --git a/services/java/com/android/server/SystemServer.java b/services/java/com/android/server/SystemServer.java
index d030fa5..6733440 100644
--- a/services/java/com/android/server/SystemServer.java
+++ b/services/java/com/android/server/SystemServer.java
@@ -827,6 +827,7 @@
     private void startOtherServices() {
         final Context context = mSystemContext;
         VibratorService vibrator = null;
+        DynamicAndroidService dynamicAndroid = null;
         IStorageManager storageManager = null;
         NetworkManagementService networkManagement = null;
         IpSecService ipSecService = null;
@@ -945,6 +946,11 @@
             ServiceManager.addService("vibrator", vibrator);
             traceEnd();
 
+            traceBeginAndSlog("StartDynamicAndroidService");
+            dynamicAndroid = new DynamicAndroidService(context);
+            ServiceManager.addService("dynamic_android", dynamicAndroid);
+            traceEnd();
+
             if (!isWatch) {
                 traceBeginAndSlog("StartConsumerIrService");
                 consumerIr = new ConsumerIrService(context);
diff --git a/services/tests/mockingservicestests/src/com/android/server/power/batterysaver/BatterySaverStateMachineTest.java b/services/tests/mockingservicestests/src/com/android/server/power/batterysaver/BatterySaverStateMachineTest.java
index 5009d64..86e8598 100644
--- a/services/tests/mockingservicestests/src/com/android/server/power/batterysaver/BatterySaverStateMachineTest.java
+++ b/services/tests/mockingservicestests/src/com/android/server/power/batterysaver/BatterySaverStateMachineTest.java
@@ -16,6 +16,7 @@
 package com.android.server.power.batterysaver;
 
 import static org.junit.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyBoolean;
 import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.Mockito.doAnswer;
@@ -178,8 +179,12 @@
                 .getSystemService(NotificationManager.class);
         doAnswer((inv) -> mDevice.batterySaverEnabled = inv.getArgument(0))
                 .when(mMockBatterySaverController).enableBatterySaver(anyBoolean(), anyInt());
+        doReturn(true).when(mMockBatterySaverController)
+                .setAdaptivePolicyLocked(any(BatterySaverPolicy.Policy.class), anyInt());
         when(mMockBatterySaverController.isEnabled())
                 .thenAnswer((inv) -> mDevice.batterySaverEnabled);
+        when(mMockBatterySaverController.isFullEnabled())
+                .thenAnswer((inv) -> mDevice.batterySaverEnabled);
         when(mMockResources.getBoolean(
                 com.android.internal.R.bool.config_batterySaverStickyBehaviourDisabled))
                 .thenReturn(false);
diff --git a/services/tests/servicestests/src/com/android/server/DynamicAndroidServiceTest.java b/services/tests/servicestests/src/com/android/server/DynamicAndroidServiceTest.java
new file mode 100644
index 0000000..1494284
--- /dev/null
+++ b/services/tests/servicestests/src/com/android/server/DynamicAndroidServiceTest.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server;
+
+import android.os.IDynamicAndroidService;
+import android.os.ServiceManager;
+import android.test.AndroidTestCase;
+import android.test.suitebuilder.annotation.LargeTest;
+
+public class DynamicAndroidServiceTest extends AndroidTestCase {
+    private static final String TAG = "DynamicAndroidServiceTests";
+    private IDynamicAndroidService mService;
+
+    @Override
+    protected void setUp() throws Exception {
+        mService =
+                IDynamicAndroidService.Stub.asInterface(
+                        ServiceManager.getService("dynamic_android"));
+    }
+
+    @LargeTest
+    public void test1() {
+        assertTrue("dynamic_android service available", mService != null);
+        try {
+            mService.startInstallation(1 << 20, 8 << 30);
+            fail("DynamicAndroidService did not throw SecurityException as expected");
+        } catch (SecurityException e) {
+            // expected
+        } catch (Exception e) {
+            fail(e.toString());
+        }
+    }
+}
diff --git a/services/tests/servicestests/src/com/android/server/am/ActivityManagerInternalTest.java b/services/tests/servicestests/src/com/android/server/am/ActivityManagerInternalTest.java
index 00a60b9..5dafe07 100644
--- a/services/tests/servicestests/src/com/android/server/am/ActivityManagerInternalTest.java
+++ b/services/tests/servicestests/src/com/android/server/am/ActivityManagerInternalTest.java
@@ -122,7 +122,7 @@
 
     private UidRecord addActiveUidRecord(int uid, long curProcStateSeq,
             long lastNetworkUpdatedProcStateSeq) {
-        final UidRecord record = new UidRecord(uid, null /* atmInternal */);
+        final UidRecord record = new UidRecord(uid);
         record.lastNetworkUpdatedProcStateSeq = lastNetworkUpdatedProcStateSeq;
         record.curProcStateSeq = curProcStateSeq;
         record.waitingForNetwork = true;
diff --git a/services/tests/servicestests/src/com/android/server/am/ActivityManagerServiceTest.java b/services/tests/servicestests/src/com/android/server/am/ActivityManagerServiceTest.java
index 419c736..4e21fd0c 100644
--- a/services/tests/servicestests/src/com/android/server/am/ActivityManagerServiceTest.java
+++ b/services/tests/servicestests/src/com/android/server/am/ActivityManagerServiceTest.java
@@ -250,7 +250,7 @@
     }
 
     private UidRecord addUidRecord(int uid) {
-        final UidRecord uidRec = new UidRecord(uid, null /* atmInternal */);
+        final UidRecord uidRec = new UidRecord(uid);
         uidRec.waitingForNetwork = true;
         uidRec.hasInternetPermission = true;
         mAms.mProcessList.mActiveUids.put(uid, uidRec);
@@ -405,7 +405,7 @@
 
     @Test
     public void testBlockStateForUid() {
-        final UidRecord uidRec = new UidRecord(TEST_UID, null /* atmInternal */);
+        final UidRecord uidRec = new UidRecord(TEST_UID);
         int expectedBlockState;
 
         final String errorTemplate = "Block state should be %s, prevState: %s, curState: %s";
@@ -732,7 +732,7 @@
 
     @Test
     public void testEnqueueUidChangeLocked_procStateSeqUpdated() {
-        final UidRecord uidRecord = new UidRecord(TEST_UID, null /* atmInternal */);
+        final UidRecord uidRecord = new UidRecord(TEST_UID);
         uidRecord.curProcStateSeq = TEST_PROC_STATE_SEQ1;
 
         // Verify with no pending changes for TEST_UID.
@@ -778,7 +778,7 @@
     @MediumTest
     @Test
     public void testEnqueueUidChangeLocked_dispatchUidsChanged() {
-        final UidRecord uidRecord = new UidRecord(TEST_UID, null /* atmInternal */);
+        final UidRecord uidRecord = new UidRecord(TEST_UID);
         final int expectedProcState = PROCESS_STATE_SERVICE;
         uidRecord.setProcState = expectedProcState;
         uidRecord.curProcStateSeq = TEST_PROC_STATE_SEQ1;
@@ -850,7 +850,7 @@
     private void verifyWaitingForNetworkStateUpdate(long curProcStateSeq,
             long lastDispatchedProcStateSeq, long lastNetworkUpdatedProcStateSeq,
             final long procStateSeqToWait, boolean expectWait) throws Exception {
-        final UidRecord record = new UidRecord(Process.myUid(), null /* atmInternal */);
+        final UidRecord record = new UidRecord(Process.myUid());
         record.curProcStateSeq = curProcStateSeq;
         record.lastDispatchedProcStateSeq = lastDispatchedProcStateSeq;
         record.lastNetworkUpdatedProcStateSeq = lastNetworkUpdatedProcStateSeq;
diff --git a/services/tests/servicestests/src/com/android/server/am/AppCompactorTest.java b/services/tests/servicestests/src/com/android/server/am/AppCompactorTest.java
index 1a231cf..2f8e545 100644
--- a/services/tests/servicestests/src/com/android/server/am/AppCompactorTest.java
+++ b/services/tests/servicestests/src/com/android/server/am/AppCompactorTest.java
@@ -18,6 +18,7 @@
 
 import static android.provider.DeviceConfig.ActivityManager.KEY_COMPACT_ACTION_1;
 import static android.provider.DeviceConfig.ActivityManager.KEY_COMPACT_ACTION_2;
+import static android.provider.DeviceConfig.ActivityManager.KEY_COMPACT_STATSD_SAMPLE_RATE;
 import static android.provider.DeviceConfig.ActivityManager.KEY_COMPACT_THROTTLE_1;
 import static android.provider.DeviceConfig.ActivityManager.KEY_COMPACT_THROTTLE_2;
 import static android.provider.DeviceConfig.ActivityManager.KEY_COMPACT_THROTTLE_3;
@@ -61,6 +62,9 @@
 @RunWith(AndroidJUnit4.class)
 public final class AppCompactorTest {
 
+    private static final String CLEAR_DEVICE_CONFIG_KEY_CMD =
+            "device_config delete activity_manager";
+
     @Mock private AppOpsService mAppOpsService;
     private AppCompactor mCompactorUnderTest;
     private HandlerThread mHandlerThread;
@@ -70,19 +74,21 @@
     private static void clearDeviceConfig() throws IOException  {
         UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
         uiDevice.executeShellCommand(
-                "device_config delete activity_manager " + KEY_USE_COMPACTION);
+                CLEAR_DEVICE_CONFIG_KEY_CMD + " " + KEY_USE_COMPACTION);
         uiDevice.executeShellCommand(
-                "device_config delete activity_manager " + KEY_COMPACT_ACTION_1);
+                CLEAR_DEVICE_CONFIG_KEY_CMD + " " + KEY_COMPACT_ACTION_1);
         uiDevice.executeShellCommand(
-                "device_config delete activity_manager " + KEY_COMPACT_ACTION_2);
+                CLEAR_DEVICE_CONFIG_KEY_CMD + " " + KEY_COMPACT_ACTION_2);
         uiDevice.executeShellCommand(
-                "device_config delete activity_manager " + KEY_COMPACT_THROTTLE_1);
+                CLEAR_DEVICE_CONFIG_KEY_CMD + " " + KEY_COMPACT_THROTTLE_1);
         uiDevice.executeShellCommand(
-                "device_config delete activity_manager " + KEY_COMPACT_THROTTLE_2);
+                CLEAR_DEVICE_CONFIG_KEY_CMD + " " + KEY_COMPACT_THROTTLE_2);
         uiDevice.executeShellCommand(
-                "device_config delete activity_manager " + KEY_COMPACT_THROTTLE_3);
+                CLEAR_DEVICE_CONFIG_KEY_CMD + " " + KEY_COMPACT_THROTTLE_3);
         uiDevice.executeShellCommand(
-                "device_config delete activity_manager " + KEY_COMPACT_THROTTLE_4);
+                CLEAR_DEVICE_CONFIG_KEY_CMD + " " + KEY_COMPACT_THROTTLE_4);
+        uiDevice.executeShellCommand(
+                CLEAR_DEVICE_CONFIG_KEY_CMD + " " + KEY_COMPACT_STATSD_SAMPLE_RATE);
     }
 
     @Before
@@ -128,6 +134,8 @@
                 is(AppCompactor.DEFAULT_COMPACT_THROTTLE_3));
         assertThat(mCompactorUnderTest.mCompactThrottleFullFull,
                 is(AppCompactor.DEFAULT_COMPACT_THROTTLE_4));
+        assertThat(mCompactorUnderTest.mStatsdSampleRate,
+                is(AppCompactor.DEFAULT_STATSD_SAMPLE_RATE));
     }
 
     @Test
@@ -155,6 +163,9 @@
         DeviceConfig.setProperty(DeviceConfig.ActivityManager.NAMESPACE,
                 KEY_COMPACT_THROTTLE_4,
                 Long.toString(AppCompactor.DEFAULT_COMPACT_THROTTLE_4 + 1), false);
+        DeviceConfig.setProperty(DeviceConfig.ActivityManager.NAMESPACE,
+                KEY_COMPACT_STATSD_SAMPLE_RATE,
+                Float.toString(AppCompactor.DEFAULT_STATSD_SAMPLE_RATE + 0.1f), false);
 
         // Then calling init will read and set that flag.
         mCompactorUnderTest.init();
@@ -173,6 +184,8 @@
                 is(AppCompactor.DEFAULT_COMPACT_THROTTLE_3 + 1));
         assertThat(mCompactorUnderTest.mCompactThrottleFullFull,
                 is(AppCompactor.DEFAULT_COMPACT_THROTTLE_4 + 1));
+        assertThat(mCompactorUnderTest.mStatsdSampleRate,
+                is(AppCompactor.DEFAULT_STATSD_SAMPLE_RATE + 0.1f));
     }
 
     @Test
@@ -365,6 +378,63 @@
                 is(AppCompactor.DEFAULT_COMPACT_THROTTLE_4));
     }
 
+    @Test
+    public void statsdSampleRate_listensToDeviceConfigChanges() throws InterruptedException {
+        mCompactorUnderTest.init();
+
+        // When we override mStatsdSampleRate with a reasonable values ...
+        mCountDown = new CountDownLatch(1);
+        DeviceConfig.setProperty(DeviceConfig.ActivityManager.NAMESPACE,
+                KEY_COMPACT_STATSD_SAMPLE_RATE,
+                Float.toString(AppCompactor.DEFAULT_STATSD_SAMPLE_RATE + 0.1f), false);
+        assertThat(mCountDown.await(5, TimeUnit.SECONDS), is(true));
+
+        // Then that override is reflected in the compactor.
+        assertThat(mCompactorUnderTest.mStatsdSampleRate,
+                is(AppCompactor.DEFAULT_STATSD_SAMPLE_RATE + 0.1f));
+    }
+
+    @Test
+    public void statsdSanokeRate_listensToDeviceConfigChangesBadValues()
+            throws InterruptedException {
+        mCompactorUnderTest.init();
+
+        // When we override mStatsdSampleRate with a reasonable values ...
+        mCountDown = new CountDownLatch(1);
+        DeviceConfig.setProperty(DeviceConfig.ActivityManager.NAMESPACE,
+                KEY_COMPACT_STATSD_SAMPLE_RATE, "foo", false);
+        assertThat(mCountDown.await(5, TimeUnit.SECONDS), is(true));
+
+        // Then that override is reflected in the compactor.
+        assertThat(mCompactorUnderTest.mStatsdSampleRate,
+                is(AppCompactor.DEFAULT_STATSD_SAMPLE_RATE));
+    }
+
+    @Test
+    public void statsdSanokeRate_listensToDeviceConfigChangesOutOfRangeValues()
+            throws InterruptedException {
+        mCompactorUnderTest.init();
+
+        // When we override mStatsdSampleRate with an value outside of [0..1]...
+        mCountDown = new CountDownLatch(1);
+        DeviceConfig.setProperty(DeviceConfig.ActivityManager.NAMESPACE,
+                KEY_COMPACT_STATSD_SAMPLE_RATE,
+                Float.toString(-1.0f), false);
+        assertThat(mCountDown.await(5, TimeUnit.SECONDS), is(true));
+
+        // Then the values is capped in the range.
+        assertThat(mCompactorUnderTest.mStatsdSampleRate, is(0.0f));
+
+        mCountDown = new CountDownLatch(1);
+        DeviceConfig.setProperty(DeviceConfig.ActivityManager.NAMESPACE,
+                KEY_COMPACT_STATSD_SAMPLE_RATE,
+                Float.toString(1.01f), false);
+        assertThat(mCountDown.await(5, TimeUnit.SECONDS), is(true));
+
+        // Then the values is capped in the range.
+        assertThat(mCompactorUnderTest.mStatsdSampleRate, is(1.0f));
+    }
+
     private class TestInjector extends Injector {
         @Override
         public AppOpsService getAppOpsService(File file, Handler handler) {
diff --git a/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java b/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java
index 535198b..9ac91dd 100644
--- a/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java
+++ b/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java
@@ -5278,6 +5278,35 @@
         assertTrue(actual.containsAll(expected));
     }
 
+    public void testIsPackageAllowedToAccessCalendar_adminNotAllowed() {
+        setAsProfileOwner(admin1);
+        dpm.setCrossProfileCalendarPackages(admin1, Collections.emptySet());
+        when(getServices().settings.settingsSecureGetIntForUser(
+                Settings.Secure.CROSS_PROFILE_CALENDAR_ENABLED,
+                0, DpmMockContext.CALLER_USER_HANDLE)).thenReturn(1);
+        assertFalse(dpm.isPackageAllowedToAccessCalendar("TEST_PACKAGE"));
+    }
+
+    public void testIsPackageAllowedToAccessCalendar_settingOff() {
+        final String testPackage = "TEST_PACKAGE";
+        setAsProfileOwner(admin1);
+        dpm.setCrossProfileCalendarPackages(admin1, Collections.singleton(testPackage));
+        when(getServices().settings.settingsSecureGetIntForUser(
+                Settings.Secure.CROSS_PROFILE_CALENDAR_ENABLED,
+                0, DpmMockContext.CALLER_USER_HANDLE)).thenReturn(0);
+        assertFalse(dpm.isPackageAllowedToAccessCalendar(testPackage));
+    }
+
+    public void testIsPackageAllowedToAccessCalendar_bothAllowed() {
+        final String testPackage = "TEST_PACKAGE";
+        setAsProfileOwner(admin1);
+        dpm.setCrossProfileCalendarPackages(admin1, null);
+        when(getServices().settings.settingsSecureGetIntForUser(
+                Settings.Secure.CROSS_PROFILE_CALENDAR_ENABLED,
+                0, DpmMockContext.CALLER_USER_HANDLE)).thenReturn(1);
+        assertTrue(dpm.isPackageAllowedToAccessCalendar(testPackage));
+    }
+
     private void configureProfileOwnerForDeviceIdAccess(ComponentName who, int userId) {
         final long ident = mServiceContext.binder.clearCallingIdentity();
         mServiceContext.binder.callingUid =
diff --git a/services/tests/servicestests/src/com/android/server/power/PowerManagerServiceTest.java b/services/tests/servicestests/src/com/android/server/power/PowerManagerServiceTest.java
index 0dff03f..bfa0b74 100644
--- a/services/tests/servicestests/src/com/android/server/power/PowerManagerServiceTest.java
+++ b/services/tests/servicestests/src/com/android/server/power/PowerManagerServiceTest.java
@@ -21,7 +21,6 @@
 
 import static com.google.common.truth.Truth.assertThat;
 
-import static org.mockito.ArgumentMatchers.anyBoolean;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
@@ -86,7 +85,7 @@
                 .setBrightnessFactor(BRIGHTNESS_FACTOR)
                 .build();
         when(mBatterySaverPolicyMock.getBatterySaverPolicy(
-                eq(PowerManager.ServiceType.SCREEN_BRIGHTNESS), anyBoolean()))
+                eq(PowerManager.ServiceType.SCREEN_BRIGHTNESS)))
                 .thenReturn(mPowerSaveState);
 
         mDisplayPowerRequest = new DisplayPowerRequest();
diff --git a/services/tests/servicestests/src/com/android/server/power/batterysaver/BatterySaverPolicyTest.java b/services/tests/servicestests/src/com/android/server/power/batterysaver/BatterySaverPolicyTest.java
index 73eefcf..9cc2b10 100644
--- a/services/tests/servicestests/src/com/android/server/power/batterysaver/BatterySaverPolicyTest.java
+++ b/services/tests/servicestests/src/com/android/server/power/batterysaver/BatterySaverPolicyTest.java
@@ -15,6 +15,10 @@
  */
 package com.android.server.power.batterysaver;
 
+import static com.android.server.power.batterysaver.BatterySaverPolicy.POLICY_LEVEL_ADAPTIVE;
+import static com.android.server.power.batterysaver.BatterySaverPolicy.POLICY_LEVEL_FULL;
+import static com.android.server.power.batterysaver.BatterySaverPolicy.POLICY_LEVEL_OFF;
+
 import static com.google.common.truth.Truth.assertThat;
 
 import static org.mockito.Mockito.mock;
@@ -31,6 +35,7 @@
 import com.android.frameworks.servicestests.R;
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.logging.MetricsLogger;
+import com.android.server.power.batterysaver.BatterySaverPolicy.Policy;
 
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
@@ -39,15 +44,15 @@
  * Tests for {@link com.android.server.power.batterysaver.BatterySaverPolicy}
  */
 public class BatterySaverPolicyTest extends AndroidTestCase {
-    private static final boolean BATTERY_SAVER_ON = true;
-    private static final boolean BATTERY_SAVER_OFF = false;
+    private static final int MAX_SERVICE_TYPE = 15;
     private static final float BRIGHTNESS_FACTOR = 0.7f;
     private static final float DEFAULT_BRIGHTNESS_FACTOR = 0.5f;
     private static final float PRECISION = 0.001f;
-    private static final int GPS_MODE = 0;
+    private static final int GPS_MODE = 0; // LOCATION_MODE_NO_CHANGE
     private static final int DEFAULT_GPS_MODE =
             PowerManager.LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF;
     private static final String BATTERY_SAVER_CONSTANTS = "vibration_disabled=true,"
+            + "advertise_is_enabled=true,"
             + "animation_disabled=false,"
             + "soundtrigger_disabled=true,"
             + "firewall_disabled=false,"
@@ -56,7 +61,7 @@
             + "adjust_brightness_factor=0.7,"
             + "fullbackup_deferred=true,"
             + "keyvaluebackup_deferred=false,"
-            + "gps_mode=0,"
+            + "gps_mode=0," // LOCATION_MODE_NO_CHANGE
             + "quick_doze_enabled=true";
     private static final String BATTERY_SAVER_INCORRECT_CONSTANTS = "vi*,!=,,true";
 
@@ -97,6 +102,8 @@
         mBatterySaverPolicy = new BatterySaverPolicyForTest(lock, getContext(),
                 new BatterySavingStats(lock, mMetricsLogger));
         mBatterySaverPolicy.systemReady();
+
+        mBatterySaverPolicy.setPolicyLevel(POLICY_LEVEL_FULL);
     }
 
     @SmallTest
@@ -148,12 +155,14 @@
     @SmallTest
     public void testGetBatterySaverPolicy_PolicyDataSaver_DefaultValueCorrect() {
         mBatterySaverPolicy.updateConstantsLocked("", "");
+        mBatterySaverPolicy.setPolicyLevel(POLICY_LEVEL_FULL);
         final PowerSaveState batterySaverStateOn =
-                mBatterySaverPolicy.getBatterySaverPolicy(ServiceType.DATA_SAVER, BATTERY_SAVER_ON);
+                mBatterySaverPolicy.getBatterySaverPolicy(ServiceType.DATA_SAVER);
         assertThat(batterySaverStateOn.batterySaverEnabled).isFalse();
 
+        mBatterySaverPolicy.setPolicyLevel(POLICY_LEVEL_OFF);
         final PowerSaveState batterySaverStateOff = mBatterySaverPolicy.getBatterySaverPolicy(
-                ServiceType.DATA_SAVER, BATTERY_SAVER_OFF);
+                ServiceType.DATA_SAVER);
         assertThat(batterySaverStateOff.batterySaverEnabled).isFalse();
     }
 
@@ -166,8 +175,9 @@
     public void testGetBatterySaverPolicy_PolicyGps_DefaultValueCorrect() {
         testServiceDefaultValue_On(ServiceType.GPS);
 
+        mBatterySaverPolicy.setPolicyLevel(POLICY_LEVEL_FULL);
         PowerSaveState stateOn =
-                mBatterySaverPolicy.getBatterySaverPolicy(ServiceType.GPS, true);
+                mBatterySaverPolicy.getBatterySaverPolicy(ServiceType.GPS);
         assertThat(stateOn.gpsMode).isEqualTo(DEFAULT_GPS_MODE);
     }
 
@@ -180,47 +190,51 @@
     public void testUpdateConstants_getCorrectData() {
         mBatterySaverPolicy.updateConstantsLocked(BATTERY_SAVER_CONSTANTS, "");
 
+        mBatterySaverPolicy.setPolicyLevel(POLICY_LEVEL_FULL);
+        verifyBatterySaverConstantsUpdated();
+    }
+
+    private void verifyBatterySaverConstantsUpdated() {
         final PowerSaveState vibrationState =
-                mBatterySaverPolicy.getBatterySaverPolicy(ServiceType.VIBRATION, BATTERY_SAVER_ON);
+                mBatterySaverPolicy.getBatterySaverPolicy(ServiceType.VIBRATION);
         assertThat(vibrationState.batterySaverEnabled).isTrue();
 
         final PowerSaveState animationState =
-                mBatterySaverPolicy.getBatterySaverPolicy(ServiceType.ANIMATION, BATTERY_SAVER_ON);
+                mBatterySaverPolicy.getBatterySaverPolicy(ServiceType.ANIMATION);
         assertThat(animationState.batterySaverEnabled).isFalse();
 
         final PowerSaveState soundState =
-                mBatterySaverPolicy.getBatterySaverPolicy(ServiceType.SOUND, BATTERY_SAVER_ON);
+                mBatterySaverPolicy.getBatterySaverPolicy(ServiceType.SOUND);
         assertThat(soundState.batterySaverEnabled).isTrue();
 
         final PowerSaveState networkState = mBatterySaverPolicy.getBatterySaverPolicy(
-                ServiceType.NETWORK_FIREWALL, BATTERY_SAVER_ON);
+                ServiceType.NETWORK_FIREWALL);
         assertThat(networkState.batterySaverEnabled).isTrue();
 
         final PowerSaveState screenState =
-                mBatterySaverPolicy.getBatterySaverPolicy(ServiceType.SCREEN_BRIGHTNESS,
-                        BATTERY_SAVER_ON);
+                mBatterySaverPolicy.getBatterySaverPolicy(ServiceType.SCREEN_BRIGHTNESS);
         assertThat(screenState.batterySaverEnabled).isFalse();
         assertThat(screenState.brightnessFactor).isWithin(PRECISION).of(BRIGHTNESS_FACTOR);
 
         final PowerSaveState fullBackupState = mBatterySaverPolicy.getBatterySaverPolicy(
-                ServiceType.FULL_BACKUP, BATTERY_SAVER_ON);
+                ServiceType.FULL_BACKUP);
         assertThat(fullBackupState.batterySaverEnabled).isTrue();
 
         final PowerSaveState keyValueBackupState = mBatterySaverPolicy.getBatterySaverPolicy(
-                ServiceType.KEYVALUE_BACKUP, BATTERY_SAVER_ON);
+                ServiceType.KEYVALUE_BACKUP);
         assertThat(keyValueBackupState.batterySaverEnabled).isFalse();
 
         final PowerSaveState dataSaverState = mBatterySaverPolicy.getBatterySaverPolicy(
-                ServiceType.DATA_SAVER, BATTERY_SAVER_ON);
+                ServiceType.DATA_SAVER);
         assertThat(dataSaverState.batterySaverEnabled).isTrue();
 
         final PowerSaveState gpsState =
-                mBatterySaverPolicy.getBatterySaverPolicy(ServiceType.GPS, BATTERY_SAVER_ON);
+                mBatterySaverPolicy.getBatterySaverPolicy(ServiceType.GPS);
         assertThat(gpsState.batterySaverEnabled).isTrue();
         assertThat(gpsState.gpsMode).isEqualTo(GPS_MODE);
 
         final PowerSaveState quickDozeState = mBatterySaverPolicy.getBatterySaverPolicy(
-                ServiceType.QUICK_DOZE, BATTERY_SAVER_ON);
+                ServiceType.QUICK_DOZE);
         assertThat(quickDozeState.batterySaverEnabled).isTrue();
     }
 
@@ -233,23 +247,27 @@
 
     private void testServiceDefaultValue_On(@ServiceType int type) {
         mBatterySaverPolicy.updateConstantsLocked("", "");
+        mBatterySaverPolicy.setPolicyLevel(POLICY_LEVEL_FULL);
         final PowerSaveState batterySaverStateOn =
-                mBatterySaverPolicy.getBatterySaverPolicy(type, BATTERY_SAVER_ON);
+                mBatterySaverPolicy.getBatterySaverPolicy(type);
         assertThat(batterySaverStateOn.batterySaverEnabled).isTrue();
 
+        mBatterySaverPolicy.setPolicyLevel(POLICY_LEVEL_OFF);
         final PowerSaveState batterySaverStateOff =
-                mBatterySaverPolicy.getBatterySaverPolicy(type, BATTERY_SAVER_OFF);
+                mBatterySaverPolicy.getBatterySaverPolicy(type);
         assertThat(batterySaverStateOff.batterySaverEnabled).isFalse();
     }
 
     private void testServiceDefaultValue_Off(@ServiceType int type) {
         mBatterySaverPolicy.updateConstantsLocked("", "");
+        mBatterySaverPolicy.setPolicyLevel(POLICY_LEVEL_FULL);
         final PowerSaveState batterySaverStateOn =
-                mBatterySaverPolicy.getBatterySaverPolicy(type, BATTERY_SAVER_ON);
+                mBatterySaverPolicy.getBatterySaverPolicy(type);
         assertThat(batterySaverStateOn.batterySaverEnabled).isFalse();
 
+        mBatterySaverPolicy.setPolicyLevel(POLICY_LEVEL_OFF);
         final PowerSaveState batterySaverStateOff =
-                mBatterySaverPolicy.getBatterySaverPolicy(type, BATTERY_SAVER_OFF);
+                mBatterySaverPolicy.getBatterySaverPolicy(type);
         assertThat(batterySaverStateOff.batterySaverEnabled).isFalse();
     }
 
@@ -291,4 +309,26 @@
                         + "/sys/devices/system/cpu/cpu5/cpufreq/scaling_max_freq=15}");
         assertThat(mBatterySaverPolicy.getFileValues(false).toString()).isEqualTo("{}");
     }
+
+    public void testSetPolicyLevel_Off() {
+        mBatterySaverPolicy.setPolicyLevel(POLICY_LEVEL_OFF);
+
+        // +1 to make sure the default value is off as well.
+        for (int i = 0; i < MAX_SERVICE_TYPE + 1; ++i) {
+            assertThat(mBatterySaverPolicy.getBatterySaverPolicy(i).batterySaverEnabled).isFalse();
+        }
+    }
+
+    public void testSetPolicyLevel_Adaptive() {
+        mBatterySaverPolicy.setPolicyLevel(POLICY_LEVEL_ADAPTIVE);
+
+        mBatterySaverPolicy.setAdaptivePolicyLocked(BatterySaverPolicy.OFF_POLICY);
+        for (int i = 0; i < MAX_SERVICE_TYPE + 1; ++i) {
+            assertThat(mBatterySaverPolicy.getBatterySaverPolicy(i).batterySaverEnabled).isFalse();
+        }
+
+        mBatterySaverPolicy.setAdaptivePolicyLocked(
+                Policy.fromSettings(BATTERY_SAVER_CONSTANTS, ""));
+        verifyBatterySaverConstantsUpdated();
+    }
 }
diff --git a/services/tests/wmtests/src/com/android/server/wm/ActivityStarterTests.java b/services/tests/wmtests/src/com/android/server/wm/ActivityStarterTests.java
index ace965b..62ec838 100644
--- a/services/tests/wmtests/src/com/android/server/wm/ActivityStarterTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/ActivityStarterTests.java
@@ -561,7 +561,7 @@
         runAndVerifyBackgroundActivityStartsSubtest("allowed_noStartsAborted", false,
                 UNIMPORTANT_UID, false, PROCESS_STATE_TOP + 1,
                 UNIMPORTANT_UID2, false, PROCESS_STATE_TOP + 1,
-                false, false, false);
+                false, false, false, false);
     }
 
     /**
@@ -576,7 +576,7 @@
                 "disallowed_unsupportedUsecase_aborted", true,
                 UNIMPORTANT_UID, false, PROCESS_STATE_TOP + 1,
                 UNIMPORTANT_UID2, false, PROCESS_STATE_TOP + 1,
-                false, false, false);
+                false, false, false, false);
     }
 
     /**
@@ -591,57 +591,64 @@
         runAndVerifyBackgroundActivityStartsSubtest("disallowed_rootUid_notAborted", false,
                 Process.ROOT_UID, false, PROCESS_STATE_TOP + 1,
                 UNIMPORTANT_UID2, false, PROCESS_STATE_TOP + 1,
-                false, false, false);
+                false, false, false, false);
         runAndVerifyBackgroundActivityStartsSubtest("disallowed_systemUid_notAborted", false,
                 Process.SYSTEM_UID, false, PROCESS_STATE_TOP + 1,
                 UNIMPORTANT_UID2, false, PROCESS_STATE_TOP + 1,
-                false, false, false);
+                false, false, false, false);
         runAndVerifyBackgroundActivityStartsSubtest("disallowed_nfcUid_notAborted", false,
                 Process.NFC_UID, false, PROCESS_STATE_TOP + 1,
                 UNIMPORTANT_UID2, false, PROCESS_STATE_TOP + 1,
-                false, false, false);
+                false, false, false, false);
         runAndVerifyBackgroundActivityStartsSubtest(
                 "disallowed_callingUidHasVisibleWindow_notAborted", false,
                 UNIMPORTANT_UID, true, PROCESS_STATE_TOP + 1,
                 UNIMPORTANT_UID2, false, PROCESS_STATE_TOP + 1,
-                false, false, false);
+                false, false, false, false);
         runAndVerifyBackgroundActivityStartsSubtest(
                 "disallowed_callingUidProcessStateTop_notAborted", false,
                 UNIMPORTANT_UID, false, PROCESS_STATE_TOP,
                 UNIMPORTANT_UID2, false, PROCESS_STATE_TOP + 1,
-                false, false, false);
+                false, false, false, false);
         runAndVerifyBackgroundActivityStartsSubtest(
                 "disallowed_realCallingUidHasVisibleWindow_notAborted", false,
                 UNIMPORTANT_UID, false, PROCESS_STATE_TOP + 1,
                 UNIMPORTANT_UID2, true, PROCESS_STATE_TOP + 1,
-                false, false, false);
+                false, false, false, false);
         runAndVerifyBackgroundActivityStartsSubtest(
                 "disallowed_realCallingUidProcessStateTop_notAborted", false,
                 UNIMPORTANT_UID, false, PROCESS_STATE_TOP + 1,
                 UNIMPORTANT_UID2, false, PROCESS_STATE_TOP,
-                false, false, false);
+                false, false, false, false);
         runAndVerifyBackgroundActivityStartsSubtest(
                 "disallowed_hasForegroundActivities_notAborted", false,
                 UNIMPORTANT_UID, false, PROCESS_STATE_TOP + 1,
                 UNIMPORTANT_UID2, false, PROCESS_STATE_TOP + 1,
-                true, false, false);
+                true, false, false, false);
         runAndVerifyBackgroundActivityStartsSubtest(
                 "disallowed_callerIsRecents_notAborted", false,
                 UNIMPORTANT_UID, false, PROCESS_STATE_TOP + 1,
                 UNIMPORTANT_UID2, false, PROCESS_STATE_TOP + 1,
-                false, true, false);
+                false, true, false, false);
         runAndVerifyBackgroundActivityStartsSubtest(
                 "disallowed_callerIsWhitelisted_notAborted", false,
                 UNIMPORTANT_UID, false, PROCESS_STATE_TOP + 1,
                 UNIMPORTANT_UID2, false, PROCESS_STATE_TOP + 1,
-                false, false, true);
+                false, false, true, false);
+        runAndVerifyBackgroundActivityStartsSubtest(
+                "disallowed_callerIsInstrumentingWithBackgroundActivityStartPrivileges_notAborted",
+                false,
+                UNIMPORTANT_UID, false, PROCESS_STATE_TOP + 1,
+                UNIMPORTANT_UID2, false, PROCESS_STATE_TOP + 1,
+                false, false, false, true);
     }
 
     private void runAndVerifyBackgroundActivityStartsSubtest(String name, boolean shouldHaveAborted,
             int callingUid, boolean callingUidHasVisibleWindow, int callingUidProcState,
             int realCallingUid, boolean realCallingUidHasVisibleWindow, int realCallingUidProcState,
             boolean hasForegroundActivities, boolean callerIsRecents,
-            boolean callerIsTempWhitelisted) {
+            boolean callerIsTempWhitelisted,
+            boolean callerIsInstrumentingWithBackgroundActivityStartPrivileges) {
         // window visibility
         doReturn(callingUidHasVisibleWindow).when(mService.mWindowManager.mRoot)
                 .isAnyNonToastWindowVisibleForUid(callingUid);
@@ -664,6 +671,9 @@
         doReturn(callerIsRecents).when(recentTasks).isCallerRecents(callingUid);
         // caller is temp whitelisted
         callerApp.setAllowBackgroundActivityStarts(callerIsTempWhitelisted);
+        // caller is instrumenting with background activity starts privileges
+        callerApp.setInstrumentingWithBackgroundActivityStartPrivileges(
+                callerIsInstrumentingWithBackgroundActivityStartPrivileges);
 
         final ActivityOptions options = spy(ActivityOptions.makeBasic());
         ActivityStarter starter = prepareStarter(FLAG_ACTIVITY_NEW_TASK)
diff --git a/startop/iorap/TEST_MAPPING b/startop/iorap/DISABLED_TEST_MAPPING
similarity index 100%
rename from startop/iorap/TEST_MAPPING
rename to startop/iorap/DISABLED_TEST_MAPPING
diff --git a/startop/iorap/src/com/google/android/startop/iorap/IorapForwardingService.java b/startop/iorap/src/com/google/android/startop/iorap/IorapForwardingService.java
index 9a30b35..22fc159 100644
--- a/startop/iorap/src/com/google/android/startop/iorap/IorapForwardingService.java
+++ b/startop/iorap/src/com/google/android/startop/iorap/IorapForwardingService.java
@@ -50,8 +50,8 @@
     public static final String TAG = "IorapForwardingService";
     /** $> adb shell 'setprop log.tag.IorapdForwardingService VERBOSE' */
     public static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
-    /** $> adb shell 'setprop iorapd.enable true' */
-    private static boolean IS_ENABLED = SystemProperties.getBoolean("iorapd.enable", true);
+    /** $> adb shell 'setprop ro.iorapd.enable true' */
+    private static boolean IS_ENABLED = SystemProperties.getBoolean("ro.iorapd.enable", true);
     /** $> adb shell 'setprop iorapd.forwarding_service.wtf_crash true' */
     private static boolean WTF_CRASH = SystemProperties.getBoolean(
             "iorapd.forwarding_service.wtf_crash", false);
diff --git a/telephony/java/android/telephony/NetworkService.java b/telephony/java/android/telephony/NetworkService.java
index 4bca404..6c45cc4 100644
--- a/telephony/java/android/telephony/NetworkService.java
+++ b/telephony/java/android/telephony/NetworkService.java
@@ -16,6 +16,7 @@
 
 package android.telephony;
 
+import android.annotation.NonNull;
 import android.annotation.SystemApi;
 import android.app.Service;
 import android.content.Intent;
@@ -112,13 +113,13 @@
                     mSlotId, 0, null).sendToTarget();
         }
 
-        private void registerForStateChanged(INetworkServiceCallback callback) {
+        private void registerForStateChanged(@NonNull INetworkServiceCallback callback) {
             synchronized (mNetworkRegistrationStateChangedCallbacks) {
                 mNetworkRegistrationStateChangedCallbacks.add(callback);
             }
         }
 
-        private void unregisterForStateChanged(INetworkServiceCallback callback) {
+        private void unregisterForStateChanged(@NonNull INetworkServiceCallback callback) {
             synchronized (mNetworkRegistrationStateChangedCallbacks) {
                 mNetworkRegistrationStateChangedCallbacks.remove(callback);
             }
diff --git a/telephony/java/android/telephony/SubscriptionManager.java b/telephony/java/android/telephony/SubscriptionManager.java
index 869cf1c..dfe36ef 100644
--- a/telephony/java/android/telephony/SubscriptionManager.java
+++ b/telephony/java/android/telephony/SubscriptionManager.java
@@ -2577,8 +2577,14 @@
     @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
     public void setPreferredDataSubscriptionId(int subId) {
         if (VDBG) logd("[setPreferredDataSubscriptionId]+ subId:" + subId);
-        setSubscriptionPropertyHelper(DEFAULT_SUBSCRIPTION_ID, "setPreferredDataSubscriptionId",
-                (iSub)-> iSub.setPreferredDataSubscriptionId(subId));
+        try {
+            ISub iSub = ISub.Stub.asInterface(ServiceManager.getService("isub"));
+            if (iSub != null) {
+                iSub.setPreferredDataSubscriptionId(subId);
+            }
+        } catch (RemoteException ex) {
+            // ignore it
+        }
     }
 
     /**
diff --git a/telephony/java/android/telephony/TelephonyManager.java b/telephony/java/android/telephony/TelephonyManager.java
index dfdec4d..a1fb090 100644
--- a/telephony/java/android/telephony/TelephonyManager.java
+++ b/telephony/java/android/telephony/TelephonyManager.java
@@ -72,6 +72,7 @@
 import android.telephony.ims.stub.ImsRegistrationImplBase;
 import android.text.TextUtils;
 import android.util.Log;
+import android.util.Pair;
 
 import com.android.ims.internal.IImsServiceFeatureCallback;
 import com.android.internal.annotations.VisibleForTesting;
@@ -3264,6 +3265,35 @@
         }
     }
 
+    /**
+     * Get the mapping from logical slots to physical slots. The mapping represent by a pair list.
+     * The key of the piar is the logical slot id and the value of the pair is the physical
+     * slots id mapped to this logical slot id.
+     *
+     * @return an pair list indicates the mapping from logical slots to physical slots. The size of
+     * the list should be {@link #getPhoneCount()} if success, otherwise return an empty list.
+     *
+     * @hide
+     */
+    @SystemApi
+    @RequiresPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
+    @NonNull
+    public List<Pair<Integer, Integer>> getLogicalToPhysicalSlotMapping() {
+        List<Pair<Integer, Integer>> slotMapping = new ArrayList<>();
+        try {
+            ITelephony telephony = getITelephony();
+            if (telephony != null) {
+                int[] slotMappingArray = telephony.getSlotsMapping();
+                for (int i = 0; i < slotMappingArray.length; i++) {
+                    slotMapping.add(new Pair(i, slotMappingArray[i]));
+                }
+            }
+        } catch (RemoteException e) {
+            Log.e(TAG, "getSlotsMapping RemoteException", e);
+        }
+        return slotMapping;
+    }
+
     //
     //
     // Subscriber Info
@@ -10239,4 +10269,24 @@
             Rlog.e(TAG, "switchMultiSimConfig RemoteException", ex);
         }
     }
+
+    /**
+     * Get whether reboot is required or not after making changes to modem configurations.
+     * @Return {@code True} if reboot is required after making changes to modem configurations,
+     * otherwise return {@code False}.
+     *
+     * @hide
+     */
+    @RequiresPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
+    public boolean isRebootRequiredForModemConfigChange() {
+        try {
+            ITelephony service = getITelephony();
+            if (service != null) {
+                return service.isRebootRequiredForModemConfigChange();
+            }
+        } catch (RemoteException e) {
+            Log.e(TAG, "isRebootRequiredForModemConfigChange RemoteException", e);
+        }
+        return false;
+    }
 }
diff --git a/telephony/java/android/telephony/data/DataService.java b/telephony/java/android/telephony/data/DataService.java
index 74d1e83..79572b9 100644
--- a/telephony/java/android/telephony/data/DataService.java
+++ b/telephony/java/android/telephony/data/DataService.java
@@ -157,7 +157,10 @@
                                   @Nullable LinkProperties linkProperties,
                                   @Nullable DataServiceCallback callback) {
             // The default implementation is to return unsupported.
-            callback.onSetupDataCallComplete(DataServiceCallback.RESULT_ERROR_UNSUPPORTED, null);
+            if (callback != null) {
+                callback.onSetupDataCallComplete(DataServiceCallback.RESULT_ERROR_UNSUPPORTED,
+                        null);
+            }
         }
 
         /**
@@ -176,7 +179,9 @@
         public void deactivateDataCall(int cid, @DeactivateDataReason int reason,
                                        @Nullable DataServiceCallback callback) {
             // The default implementation is to return unsupported.
-            callback.onDeactivateDataCallComplete(DataServiceCallback.RESULT_ERROR_UNSUPPORTED);
+            if (callback != null) {
+                callback.onDeactivateDataCallComplete(DataServiceCallback.RESULT_ERROR_UNSUPPORTED);
+            }
         }
 
         /**
@@ -190,7 +195,10 @@
         public void setInitialAttachApn(DataProfile dataProfile, boolean isRoaming,
                                         @Nullable DataServiceCallback callback) {
             // The default implementation is to return unsupported.
-            callback.onSetInitialAttachApnComplete(DataServiceCallback.RESULT_ERROR_UNSUPPORTED);
+            if (callback != null) {
+                callback.onSetInitialAttachApnComplete(
+                        DataServiceCallback.RESULT_ERROR_UNSUPPORTED);
+            }
         }
 
         /**
@@ -206,7 +214,9 @@
         public void setDataProfile(List<DataProfile> dps, boolean isRoaming,
                                    @Nullable DataServiceCallback callback) {
             // The default implementation is to return unsupported.
-            callback.onSetDataProfileComplete(DataServiceCallback.RESULT_ERROR_UNSUPPORTED);
+            if (callback != null) {
+                callback.onSetDataProfileComplete(DataServiceCallback.RESULT_ERROR_UNSUPPORTED);
+            }
         }
 
         /**
diff --git a/telephony/java/android/telephony/euicc/EuiccManager.java b/telephony/java/android/telephony/euicc/EuiccManager.java
index bca088e..b2f5802 100644
--- a/telephony/java/android/telephony/euicc/EuiccManager.java
+++ b/telephony/java/android/telephony/euicc/EuiccManager.java
@@ -464,7 +464,7 @@
             return null;
         }
         try {
-            return getIEuiccController().getEid(mCardId);
+            return getIEuiccController().getEid(mCardId, mContext.getOpPackageName());
         } catch (RemoteException e) {
             throw e.rethrowFromSystemServer();
         }
@@ -502,6 +502,15 @@
      * Without the former, an {@link #EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR} will be
      * returned in the callback intent to prompt the user to accept the download.
      *
+     * <p>On a multi-active SIM device, requires the
+     * {@code android.Manifest.permission#WRITE_EMBEDDED_SUBSCRIPTIONS} permission, or a calling app
+     * only if the targeted eUICC does not currently have an active subscription or the calling app
+     * is authorized to manage the active subscription on the target eUICC, and the calling app is
+     * authorized to manage any active subscription on any SIM. Without it, an
+     * {@link #EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR} will be returned in the callback
+     * intent to prompt the user to accept the download. The caller should also be authorized to
+     * manage the subscription to be downloaded.
+     *
      * @param subscription the subscription to download.
      * @param switchAfterDownload if true, the profile will be activated upon successful download.
      * @param callbackIntent a PendingIntent to launch when the operation completes.
@@ -704,9 +713,21 @@
      * an {@link #EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR} will be returned in the callback
      * intent to prompt the user to accept the download.
      *
+     * <p>On a multi-active SIM device, requires the
+     * {@code android.Manifest.permission#WRITE_EMBEDDED_SUBSCRIPTIONS} permission, or a calling app
+     *  only if the targeted eUICC does not currently have an active subscription or the calling app
+     * is authorized to manage the active subscription on the target eUICC, and the calling app is
+     * authorized to manage any active subscription on any SIM. Without it, an
+     * {@link #EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR} will be returned in the callback
+     * intent to prompt the user to accept the download. The caller should also be authorized to
+     * manage the subscription to be enabled.
+     *
      * @param subscriptionId the ID of the subscription to enable. May be
      *     {@link android.telephony.SubscriptionManager#INVALID_SUBSCRIPTION_ID} to deactivate the
-     *     current profile without activating another profile to replace it.
+     *     current profile without activating another profile to replace it. If it's a disable
+     *     operation, requires the {@code android.Manifest.permission#WRITE_EMBEDDED_SUBSCRIPTIONS}
+     *     permission, or the calling app must be authorized to manage the active subscription on
+     *     the target eUICC.
      * @param callbackIntent a PendingIntent to launch when the operation completes.
      */
     @RequiresPermission(Manifest.permission.WRITE_EMBEDDED_SUBSCRIPTIONS)
diff --git a/telephony/java/com/android/internal/telephony/ISub.aidl b/telephony/java/com/android/internal/telephony/ISub.aidl
index a49d2d9..6ce9de4 100755
--- a/telephony/java/com/android/internal/telephony/ISub.aidl
+++ b/telephony/java/com/android/internal/telephony/ISub.aidl
@@ -220,7 +220,7 @@
      * @hide
      *
      */
-    int setPreferredDataSubscriptionId(int subId);
+    void setPreferredDataSubscriptionId(int subId);
 
     /**
      * Get which subscription is preferred for cellular data.
diff --git a/telephony/java/com/android/internal/telephony/ITelephony.aidl b/telephony/java/com/android/internal/telephony/ITelephony.aidl
index 927c676..762d886 100644
--- a/telephony/java/com/android/internal/telephony/ITelephony.aidl
+++ b/telephony/java/com/android/internal/telephony/ITelephony.aidl
@@ -1848,4 +1848,14 @@
      * @hide
      */
     int getNumOfActiveSims();
+
+    /**
+     * Get if reboot is required upon altering modems configurations
+     */
+    boolean isRebootRequiredForModemConfigChange();
+
+    /**
+     * Get the mapping from logical slots to physical slots.
+     */
+    int[] getSlotsMapping();
 }
diff --git a/telephony/java/com/android/internal/telephony/TelephonyProperties.java b/telephony/java/com/android/internal/telephony/TelephonyProperties.java
index 6567ea7..603c4c2 100644
--- a/telephony/java/com/android/internal/telephony/TelephonyProperties.java
+++ b/telephony/java/com/android/internal/telephony/TelephonyProperties.java
@@ -194,6 +194,13 @@
      */
     static final String PROPERTY_MULTI_SIM_CONFIG = "persist.radio.multisim.config";
 
+     /**
+     * Property to indicate if reboot is required when changing modems configurations
+     * Type:  String(true, false) default is false; most devices don't need reboot
+     */
+    String PROPERTY_REBOOT_REQUIRED_ON_MODEM_CHANGE =
+             "persist.radio.reboot_on_modem_change";
+
     /**
      * Property to store default subscription.
      */
diff --git a/telephony/java/com/android/internal/telephony/euicc/IEuiccController.aidl b/telephony/java/com/android/internal/telephony/euicc/IEuiccController.aidl
index 14a36c8..2016915 100644
--- a/telephony/java/com/android/internal/telephony/euicc/IEuiccController.aidl
+++ b/telephony/java/com/android/internal/telephony/euicc/IEuiccController.aidl
@@ -31,7 +31,7 @@
         String callingPackage, in PendingIntent callbackIntent);
     oneway void getDefaultDownloadableSubscriptionList(int cardId,
         String callingPackage, in PendingIntent callbackIntent);
-    String getEid(int cardId);
+    String getEid(int cardId, String callingPackage);
     int getOtaStatus(int cardId);
     oneway void downloadSubscription(int cardId, in DownloadableSubscription subscription,
         boolean switchAfterDownload, String callingPackage, in Bundle resolvedBundle,
diff --git a/tests/FlickerTests/lib/src/com/android/server/wm/flicker/AutomationUtils.java b/tests/FlickerTests/lib/src/com/android/server/wm/flicker/AutomationUtils.java
index 6bac675..6306f0e 100644
--- a/tests/FlickerTests/lib/src/com/android/server/wm/flicker/AutomationUtils.java
+++ b/tests/FlickerTests/lib/src/com/android/server/wm/flicker/AutomationUtils.java
@@ -173,7 +173,7 @@
             BySelector splitscreenButtonSelector = By.text("Split screen");
             UiObject2 splitscreenButton = device.wait(Until.findObject(splitscreenButtonSelector),
                     FIND_TIMEOUT);
-            assertNotNull("Unable to find Split screen button in Overview", overviewIcon);
+            assertNotNull("Unable to find Split screen button in Overview", splitscreenButton);
             splitscreenButton.click();
         } else {
             // Classic long press recents
diff --git a/tests/FrameworkPerf/AndroidManifest.xml b/tests/FrameworkPerf/AndroidManifest.xml
index ca25386..4fd2043 100644
--- a/tests/FrameworkPerf/AndroidManifest.xml
+++ b/tests/FrameworkPerf/AndroidManifest.xml
@@ -14,7 +14,7 @@
             </intent-filter>
         </activity>
         <service android:name="SchedulerService"
-            android:foregroundServiceType="sync">
+                 android:foregroundServiceType="dataSync|mediaPlayback|phoneCall|location|connectedDevice">
         </service>
         <service android:name="TestService" android:process=":test">
         </service>
@@ -23,7 +23,6 @@
         <receiver android:name="Receiver" android:exported="true">
         </receiver>
     </application>
-
     <instrumentation android:name="android.test.InstrumentationTestRunner"
         android:targetPackage="com.android.frameworkperf"
         android:label="Framework Perf Runner"
diff --git a/tests/HwAccelerationTest/AndroidManifest.xml b/tests/HwAccelerationTest/AndroidManifest.xml
index f330b83..1a4ec94 100644
--- a/tests/HwAccelerationTest/AndroidManifest.xml
+++ b/tests/HwAccelerationTest/AndroidManifest.xml
@@ -1027,5 +1027,15 @@
             </intent-filter>
         </activity>
 
+        <activity
+            android:name="PositionListenerActivity"
+            android:label="RenderNode/PositionListener"
+            android:screenOrientation="fullSensor">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="com.android.test.hwui.TEST" />
+            </intent-filter>
+        </activity>
+
     </application>
 </manifest>
diff --git a/tests/HwAccelerationTest/src/com/android/test/hwui/PositionListenerActivity.java b/tests/HwAccelerationTest/src/com/android/test/hwui/PositionListenerActivity.java
new file mode 100644
index 0000000..316aad3
--- /dev/null
+++ b/tests/HwAccelerationTest/src/com/android/test/hwui/PositionListenerActivity.java
@@ -0,0 +1,98 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.test.hwui;
+
+import android.app.Activity;
+import android.content.Context;
+import android.graphics.Canvas;
+import android.graphics.RenderNode;
+import android.os.Bundle;
+import android.widget.LinearLayout;
+import android.widget.ProgressBar;
+import android.widget.ScrollView;
+import android.widget.TextView;
+
+@SuppressWarnings({"UnusedDeclaration"})
+public class PositionListenerActivity extends Activity {
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        final LinearLayout layout = new LinearLayout(this);
+        layout.setOrientation(LinearLayout.VERTICAL);
+
+        ProgressBar spinner = new ProgressBar(this, null, android.R.attr.progressBarStyleLarge);
+        layout.addView(spinner);
+
+        ScrollView scrollingThing = new ScrollView(this);
+        scrollingThing.addView(new MyPositionReporter(this));
+        layout.addView(scrollingThing);
+
+        setContentView(layout);
+    }
+
+    static class MyPositionReporter extends TextView implements RenderNode.PositionUpdateListener {
+        RenderNode mNode;
+        int mCurrentCount = 0;
+        int mTranslateY = 0;
+
+        MyPositionReporter(Context c) {
+            super(c);
+            mNode = new RenderNode("positionListener");
+            mNode.requestPositionUpdates(this);
+            setTextAlignment(TEXT_ALIGNMENT_VIEW_START);
+        }
+
+        @Override
+        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
+            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
+            setMeasuredDimension(getMeasuredWidth(), 10000);
+        }
+
+        @Override
+        protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
+            mNode.setLeftTopRightBottom(left, top, right, bottom);
+        }
+
+        @Override
+        protected void onDraw(Canvas canvas) {
+            ScrollView parent = (ScrollView) getParent();
+            canvas.translate(0, parent.getScrollY());
+            super.onDraw(canvas);
+            canvas.translate(0, -parent.getScrollY());
+            // Inject our listener proxy
+            canvas.drawRenderNode(mNode);
+        }
+
+        @Override
+        public void positionChanged(long frameNumber, int left, int top, int right, int bottom) {
+            post(() -> {
+                mCurrentCount++;
+                setText(String.format("%d: Position [%d, %d, %d, %d]", mCurrentCount,
+                        left, top, right, bottom));
+            });
+        }
+
+        @Override
+        public void positionLost(long frameNumber) {
+            post(() -> {
+                mCurrentCount++;
+                setText(mCurrentCount + " No position");
+            });
+        }
+    }
+}
diff --git a/tests/RollbackTest/Android.bp b/tests/RollbackTest/Android.bp
new file mode 100644
index 0000000..3d57d56
--- /dev/null
+++ b/tests/RollbackTest/Android.bp
@@ -0,0 +1,64 @@
+// Copyright (C) 2018 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+android_app {
+    name: "RollbackTestAppAv1",
+    manifest: "TestApp/Av1.xml",
+    sdk_version: "current",
+    srcs: ["TestApp/src/**/*.java"],
+}
+
+android_app {
+    name: "RollbackTestAppAv2",
+    manifest: "TestApp/Av2.xml",
+    sdk_version: "current",
+    srcs: ["TestApp/src/**/*.java"],
+}
+
+android_app {
+    name: "RollbackTestAppACrashingV2",
+    manifest: "TestApp/ACrashingV2.xml",
+    sdk_version: "current",
+    srcs: ["TestApp/src/**/*.java"],
+}
+
+android_app {
+    name: "RollbackTestAppBv1",
+    manifest: "TestApp/Bv1.xml",
+    sdk_version: "current",
+    srcs: ["TestApp/src/**/*.java"],
+}
+
+android_app {
+    name: "RollbackTestAppBv2",
+    manifest: "TestApp/Bv2.xml",
+    sdk_version: "current",
+    srcs: ["TestApp/src/**/*.java"],
+}
+
+android_test {
+    name: "RollbackTest",
+    srcs: ["src/**/*.java"],
+    static_libs: ["android-support-test"],
+    test_suites: ["general-tests"],
+    java_resources: [
+        ":RollbackTestAppAv1",
+        ":RollbackTestAppAv2",
+        ":RollbackTestAppACrashingV2",
+        ":RollbackTestAppBv1",
+        ":RollbackTestAppBv2",
+    ],
+    test_config: "RollbackTest.xml",
+    sdk_version: "system_current",
+}
diff --git a/tests/RollbackTest/Android.mk b/tests/RollbackTest/Android.mk
deleted file mode 100644
index 780bb24..0000000
--- a/tests/RollbackTest/Android.mk
+++ /dev/null
@@ -1,94 +0,0 @@
-# Copyright (C) 2018 The Android Open Source Project
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#      http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-LOCAL_PATH:= $(call my-dir)
-
-# RollbackTestAppAv1.apk
-include $(CLEAR_VARS)
-LOCAL_MODULE_TAGS := optional
-LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
-LOCAL_SDK_VERSION := current
-LOCAL_SRC_FILES := $(call all-java-files-under, TestApp/src)
-LOCAL_MANIFEST_FILE := TestApp/Av1.xml
-LOCAL_PACKAGE_NAME := RollbackTestAppAv1
-include $(BUILD_PACKAGE)
-ROLLBACK_TEST_APP_AV1 := $(LOCAL_INSTALLED_MODULE)
-
-# RollbackTestAppAv2.apk
-include $(CLEAR_VARS)
-LOCAL_MODULE_TAGS := optional
-LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
-LOCAL_SDK_VERSION := current
-LOCAL_SRC_FILES := $(call all-java-files-under, TestApp/src)
-LOCAL_MANIFEST_FILE := TestApp/Av2.xml
-LOCAL_PACKAGE_NAME := RollbackTestAppAv2
-include $(BUILD_PACKAGE)
-ROLLBACK_TEST_APP_AV2 := $(LOCAL_INSTALLED_MODULE)
-
-# RollbackTestAppACrashingV2.apk
-include $(CLEAR_VARS)
-LOCAL_MODULE_TAGS := optional
-LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
-LOCAL_SDK_VERSION := current
-LOCAL_SRC_FILES := $(call all-java-files-under, TestApp/src)
-LOCAL_MANIFEST_FILE := TestApp/ACrashingV2.xml
-LOCAL_PACKAGE_NAME := RollbackTestAppACrashingV2
-include $(BUILD_PACKAGE)
-ROLLBACK_TEST_APP_A_CRASHING_V2 := $(LOCAL_INSTALLED_MODULE)
-
-# RollbackTestAppBv1.apk
-include $(CLEAR_VARS)
-LOCAL_MODULE_TAGS := optional
-LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
-LOCAL_SDK_VERSION := current
-LOCAL_SRC_FILES := $(call all-java-files-under, TestApp/src)
-LOCAL_MANIFEST_FILE := TestApp/Bv1.xml
-LOCAL_PACKAGE_NAME := RollbackTestAppBv1
-include $(BUILD_PACKAGE)
-ROLLBACK_TEST_APP_BV1 := $(LOCAL_INSTALLED_MODULE)
-
-# RollbackTestAppBv2.apk
-include $(CLEAR_VARS)
-LOCAL_MODULE_TAGS := optional
-LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
-LOCAL_SDK_VERSION := current
-LOCAL_SRC_FILES := $(call all-java-files-under, TestApp/src)
-LOCAL_MANIFEST_FILE := TestApp/Bv2.xml
-LOCAL_PACKAGE_NAME := RollbackTestAppBv2
-include $(BUILD_PACKAGE)
-ROLLBACK_TEST_APP_BV2 := $(LOCAL_INSTALLED_MODULE)
-
-# RollbackTest
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES := $(call all-java-files-under, src)
-LOCAL_PACKAGE_NAME := RollbackTest
-LOCAL_MODULE_TAGS := tests
-LOCAL_STATIC_JAVA_LIBRARIES := android-support-test
-LOCAL_COMPATIBILITY_SUITE := general-tests
-LOCAL_JAVA_RESOURCE_FILES := \
-  $(ROLLBACK_TEST_APP_AV1) \
-  $(ROLLBACK_TEST_APP_AV2) \
-  $(ROLLBACK_TEST_APP_A_CRASHING_V2) \
-  $(ROLLBACK_TEST_APP_BV1) \
-  $(ROLLBACK_TEST_APP_BV2)
-LOCAL_SDK_VERSION := system_current
-LOCAL_TEST_CONFIG := RollbackTest.xml
-include $(BUILD_PACKAGE)
-
-# Clean up local variables
-ROLLBACK_TEST_APP_AV1 :=
-ROLLBACK_TEST_APP_AV2 :=
-ROLLBACK_TEST_APP_A_CRASHING_V2 :=
-ROLLBACK_TEST_APP_BV1 :=
-ROLLBACK_TEST_APP_BV2 :=
diff --git a/tests/net/java/com/android/server/ConnectivityServiceTest.java b/tests/net/java/com/android/server/ConnectivityServiceTest.java
index 3127d74..50468cb 100644
--- a/tests/net/java/com/android/server/ConnectivityServiceTest.java
+++ b/tests/net/java/com/android/server/ConnectivityServiceTest.java
@@ -922,6 +922,7 @@
             mNetworkCapabilities.set(mMockNetworkAgent.getNetworkCapabilities());
             mConnected = true;
             mConfig = new VpnConfig();
+            mConfig.isMetered = false;
         }
 
         @Override
diff --git a/tests/net/java/com/android/server/connectivity/VpnTest.java b/tests/net/java/com/android/server/connectivity/VpnTest.java
index 5b17224..46de3d0 100644
--- a/tests/net/java/com/android/server/connectivity/VpnTest.java
+++ b/tests/net/java/com/android/server/connectivity/VpnTest.java
@@ -168,6 +168,8 @@
         ApplicationInfo applicationInfo = new ApplicationInfo();
         applicationInfo.targetSdkVersion = VERSION_CODES.CUR_DEVELOPMENT;
         when(mContext.getApplicationInfo()).thenReturn(applicationInfo);
+        when(mPackageManager.getApplicationInfoAsUser(anyString(), anyInt(), anyInt()))
+                .thenReturn(applicationInfo);
 
         doNothing().when(mNetService).registerObserver(any());
     }
@@ -544,23 +546,28 @@
         final Network wifi = new Network(2);
 
         final Map<Network, NetworkCapabilities> networks = new HashMap<>();
-        networks.put(mobile, new NetworkCapabilities()
-                .addTransportType(TRANSPORT_CELLULAR)
-                .addCapability(NET_CAPABILITY_INTERNET)
-                .addCapability(NET_CAPABILITY_NOT_METERED)
-                .addCapability(NET_CAPABILITY_NOT_CONGESTED)
-                .setLinkDownstreamBandwidthKbps(10));
-        networks.put(wifi, new NetworkCapabilities()
-                .addTransportType(TRANSPORT_WIFI)
-                .addCapability(NET_CAPABILITY_INTERNET)
-                .addCapability(NET_CAPABILITY_NOT_ROAMING)
-                .addCapability(NET_CAPABILITY_NOT_CONGESTED)
-                .setLinkUpstreamBandwidthKbps(20));
+        networks.put(
+                mobile,
+                new NetworkCapabilities()
+                        .addTransportType(TRANSPORT_CELLULAR)
+                        .addCapability(NET_CAPABILITY_INTERNET)
+                        .addCapability(NET_CAPABILITY_NOT_CONGESTED)
+                        .setLinkDownstreamBandwidthKbps(10));
+        networks.put(
+                wifi,
+                new NetworkCapabilities()
+                        .addTransportType(TRANSPORT_WIFI)
+                        .addCapability(NET_CAPABILITY_INTERNET)
+                        .addCapability(NET_CAPABILITY_NOT_METERED)
+                        .addCapability(NET_CAPABILITY_NOT_ROAMING)
+                        .addCapability(NET_CAPABILITY_NOT_CONGESTED)
+                        .setLinkUpstreamBandwidthKbps(20));
         setMockedNetworks(networks);
 
         final NetworkCapabilities caps = new NetworkCapabilities();
 
-        Vpn.updateCapabilities(mConnectivityManager, new Network[] { }, caps);
+        Vpn.updateCapabilities(
+                mConnectivityManager, new Network[] {}, caps, false /* isAlwaysMetered */);
         assertTrue(caps.hasTransport(TRANSPORT_VPN));
         assertFalse(caps.hasTransport(TRANSPORT_CELLULAR));
         assertFalse(caps.hasTransport(TRANSPORT_WIFI));
@@ -570,17 +577,33 @@
         assertTrue(caps.hasCapability(NET_CAPABILITY_NOT_ROAMING));
         assertTrue(caps.hasCapability(NET_CAPABILITY_NOT_CONGESTED));
 
-        Vpn.updateCapabilities(mConnectivityManager, new Network[] { mobile }, caps);
+        Vpn.updateCapabilities(
+                mConnectivityManager,
+                new Network[] {mobile},
+                caps,
+                false /* isAlwaysMetered */);
         assertTrue(caps.hasTransport(TRANSPORT_VPN));
         assertTrue(caps.hasTransport(TRANSPORT_CELLULAR));
         assertFalse(caps.hasTransport(TRANSPORT_WIFI));
         assertEquals(10, caps.getLinkDownstreamBandwidthKbps());
         assertEquals(LINK_BANDWIDTH_UNSPECIFIED, caps.getLinkUpstreamBandwidthKbps());
-        assertTrue(caps.hasCapability(NET_CAPABILITY_NOT_METERED));
+        assertFalse(caps.hasCapability(NET_CAPABILITY_NOT_METERED));
         assertFalse(caps.hasCapability(NET_CAPABILITY_NOT_ROAMING));
         assertTrue(caps.hasCapability(NET_CAPABILITY_NOT_CONGESTED));
 
-        Vpn.updateCapabilities(mConnectivityManager, new Network[] { wifi }, caps);
+        Vpn.updateCapabilities(
+                mConnectivityManager, new Network[] {wifi}, caps, false /* isAlwaysMetered */);
+        assertTrue(caps.hasTransport(TRANSPORT_VPN));
+        assertFalse(caps.hasTransport(TRANSPORT_CELLULAR));
+        assertTrue(caps.hasTransport(TRANSPORT_WIFI));
+        assertEquals(LINK_BANDWIDTH_UNSPECIFIED, caps.getLinkDownstreamBandwidthKbps());
+        assertEquals(20, caps.getLinkUpstreamBandwidthKbps());
+        assertTrue(caps.hasCapability(NET_CAPABILITY_NOT_METERED));
+        assertTrue(caps.hasCapability(NET_CAPABILITY_NOT_ROAMING));
+        assertTrue(caps.hasCapability(NET_CAPABILITY_NOT_CONGESTED));
+
+        Vpn.updateCapabilities(
+                mConnectivityManager, new Network[] {wifi}, caps, true /* isAlwaysMetered */);
         assertTrue(caps.hasTransport(TRANSPORT_VPN));
         assertFalse(caps.hasTransport(TRANSPORT_CELLULAR));
         assertTrue(caps.hasTransport(TRANSPORT_WIFI));
@@ -590,7 +613,11 @@
         assertTrue(caps.hasCapability(NET_CAPABILITY_NOT_ROAMING));
         assertTrue(caps.hasCapability(NET_CAPABILITY_NOT_CONGESTED));
 
-        Vpn.updateCapabilities(mConnectivityManager, new Network[] { mobile, wifi }, caps);
+        Vpn.updateCapabilities(
+                mConnectivityManager,
+                new Network[] {mobile, wifi},
+                caps,
+                false /* isAlwaysMetered */);
         assertTrue(caps.hasTransport(TRANSPORT_VPN));
         assertTrue(caps.hasTransport(TRANSPORT_CELLULAR));
         assertTrue(caps.hasTransport(TRANSPORT_WIFI));
diff --git a/tests/net/java/com/android/server/connectivity/tethering/EntitlementManagerTest.java b/tests/net/java/com/android/server/connectivity/tethering/EntitlementManagerTest.java
index 0f72229..ec286759 100644
--- a/tests/net/java/com/android/server/connectivity/tethering/EntitlementManagerTest.java
+++ b/tests/net/java/com/android/server/connectivity/tethering/EntitlementManagerTest.java
@@ -16,8 +16,16 @@
 
 package com.android.server.connectivity.tethering;
 
+import static android.net.ConnectivityManager.TETHERING_USB;
+import static android.net.ConnectivityManager.TETHERING_WIFI;
+import static android.net.ConnectivityManager.TETHER_ERROR_ENTITLEMENT_UNKONWN;
+import static android.net.ConnectivityManager.TETHER_ERROR_NO_ERROR;
+import static android.net.ConnectivityManager.TETHER_ERROR_PROVISION_FAILED;
+
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 import static org.mockito.Matchers.anyBoolean;
 import static org.mockito.Matchers.anyString;
 import static org.mockito.Matchers.eq;
@@ -27,12 +35,22 @@
 import android.content.Context;
 import android.content.res.Resources;
 import android.net.util.SharedLog;
+import android.os.Bundle;
+import android.os.Message;
 import android.os.PersistableBundle;
+import android.os.ResultReceiver;
+import android.os.test.TestLooper;
+import android.provider.Settings;
 import android.support.test.filters.SmallTest;
 import android.support.test.runner.AndroidJUnit4;
 import android.telephony.CarrierConfigManager;
+import android.test.mock.MockContentResolver;
 
 import com.android.internal.R;
+import com.android.internal.util.State;
+import com.android.internal.util.StateMachine;
+import com.android.internal.util.test.BroadcastInterceptingContext;
+import com.android.internal.util.test.FakeSettingsProvider;
 import com.android.server.connectivity.MockableSystemProperties;
 
 import org.junit.After;
@@ -42,6 +60,10 @@
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 
+import java.util.ArrayList;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
 @RunWith(AndroidJUnit4.class)
 @SmallTest
 public final class EntitlementManagerTest {
@@ -51,7 +73,6 @@
 
     @Mock private CarrierConfigManager mCarrierConfigManager;
     @Mock private Context mContext;
-    @Mock private ContentResolver mContent;
     @Mock private MockableSystemProperties mSystemProperties;
     @Mock private Resources mResources;
     @Mock private SharedLog mLog;
@@ -59,15 +80,49 @@
     // Like so many Android system APIs, these cannot be mocked because it is marked final.
     // We have to use the real versions.
     private final PersistableBundle mCarrierConfig = new PersistableBundle();
+    private final TestLooper mLooper = new TestLooper();
+    private Context mMockContext;
+    private MockContentResolver mContentResolver;
 
-    private EntitlementManager mEnMgr;
+    private TestStateMachine mSM;
+    private WrappedEntitlementManager mEnMgr;
+
+    private class MockContext extends BroadcastInterceptingContext {
+        MockContext(Context base) {
+            super(base);
+        }
+
+        @Override
+        public Resources getResources() {
+            return mResources;
+        }
+
+        @Override
+        public ContentResolver getContentResolver() {
+            return mContentResolver;
+        }
+    }
+
+    public class WrappedEntitlementManager extends EntitlementManager {
+        public int fakeEntitlementResult = TETHER_ERROR_ENTITLEMENT_UNKONWN;
+        public boolean everRunUiEntitlement = false;
+
+        public WrappedEntitlementManager(Context ctx, StateMachine target,
+                SharedLog log, MockableSystemProperties systemProperties) {
+            super(ctx, target, log, systemProperties);
+        }
+
+        @Override
+        protected void runUiTetherProvisioning(int type, ResultReceiver receiver) {
+            everRunUiEntitlement = true;
+            receiver.send(fakeEntitlementResult, null);
+        }
+    }
 
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
 
-        when(mContext.getResources()).thenReturn(mResources);
-        when(mContext.getContentResolver()).thenReturn(mContent);
         when(mResources.getStringArray(R.array.config_tether_dhcp_range))
             .thenReturn(new String[0]);
         when(mResources.getStringArray(R.array.config_tether_usb_regexs))
@@ -80,12 +135,21 @@
             .thenReturn(new int[0]);
         when(mLog.forSubComponent(anyString())).thenReturn(mLog);
 
-        mEnMgr = new EntitlementManager(mContext, mLog, mSystemProperties);
-        mEnMgr.updateConfiguration(new TetheringConfiguration(mContext, mLog));
+        mContentResolver = new MockContentResolver();
+        mContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider());
+        mMockContext = new MockContext(mContext);
+        mSM = new TestStateMachine();
+        mEnMgr = new WrappedEntitlementManager(mMockContext, mSM, mLog, mSystemProperties);
+        mEnMgr.updateConfiguration(new TetheringConfiguration(mMockContext, mLog));
     }
 
     @After
-    public void tearDown() throws Exception {}
+    public void tearDown() throws Exception {
+        if (mSM != null) {
+            mSM.quit();
+            mSM = null;
+        }
+    }
 
     private void setupForRequiredProvisioning() {
         // Produce some acceptable looking provision app setting if requested.
@@ -104,7 +168,7 @@
     @Test
     public void canRequireProvisioning() {
         setupForRequiredProvisioning();
-        mEnMgr.updateConfiguration(new TetheringConfiguration(mContext, mLog));
+        mEnMgr.updateConfiguration(new TetheringConfiguration(mMockContext, mLog));
         assertTrue(mEnMgr.isTetherProvisioningRequired());
     }
 
@@ -113,7 +177,7 @@
         setupForRequiredProvisioning();
         when(mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE))
             .thenReturn(null);
-        mEnMgr.updateConfiguration(new TetheringConfiguration(mContext, mLog));
+        mEnMgr.updateConfiguration(new TetheringConfiguration(mMockContext, mLog));
         // Couldn't get the CarrierConfigManager, but still had a declared provisioning app.
         // Therefore provisioning still be required.
         assertTrue(mEnMgr.isTetherProvisioningRequired());
@@ -123,7 +187,7 @@
     public void toleratesCarrierConfigMissing() {
         setupForRequiredProvisioning();
         when(mCarrierConfigManager.getConfig()).thenReturn(null);
-        mEnMgr.updateConfiguration(new TetheringConfiguration(mContext, mLog));
+        mEnMgr.updateConfiguration(new TetheringConfiguration(mMockContext, mLog));
         // We still have a provisioning app configured, so still require provisioning.
         assertTrue(mEnMgr.isTetherProvisioningRequired());
     }
@@ -133,12 +197,143 @@
         setupForRequiredProvisioning();
         when(mResources.getStringArray(R.array.config_mobile_hotspot_provision_app))
             .thenReturn(null);
-        mEnMgr.updateConfiguration(new TetheringConfiguration(mContext, mLog));
+        mEnMgr.updateConfiguration(new TetheringConfiguration(mMockContext, mLog));
         assertFalse(mEnMgr.isTetherProvisioningRequired());
         when(mResources.getStringArray(R.array.config_mobile_hotspot_provision_app))
             .thenReturn(new String[] {"malformedApp"});
-        mEnMgr.updateConfiguration(new TetheringConfiguration(mContext, mLog));
+        mEnMgr.updateConfiguration(new TetheringConfiguration(mMockContext, mLog));
         assertFalse(mEnMgr.isTetherProvisioningRequired());
     }
 
+    @Test
+    public void testGetLastEntitlementCacheValue() throws Exception {
+        final CountDownLatch mCallbacklatch = new CountDownLatch(1);
+        // 1. Entitlement check is not required.
+        mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR;
+        mEnMgr.everRunUiEntitlement = false;
+        ResultReceiver receiver = new ResultReceiver(null) {
+            @Override
+            protected void onReceiveResult(int resultCode, Bundle resultData) {
+                assertEquals(TETHER_ERROR_NO_ERROR, resultCode);
+                mCallbacklatch.countDown();
+            }
+        };
+        mEnMgr.getLatestTetheringEntitlementValue(TETHERING_WIFI, receiver, true);
+        callbackTimeoutHelper(mCallbacklatch);
+        assertFalse(mEnMgr.everRunUiEntitlement);
+
+        setupForRequiredProvisioning();
+        mEnMgr.updateConfiguration(new TetheringConfiguration(mMockContext, mLog));
+        // 2. No cache value and don't need to run entitlement check.
+        mEnMgr.everRunUiEntitlement = false;
+        receiver = new ResultReceiver(null) {
+            @Override
+            protected void onReceiveResult(int resultCode, Bundle resultData) {
+                assertEquals(TETHER_ERROR_ENTITLEMENT_UNKONWN, resultCode);
+                mCallbacklatch.countDown();
+            }
+        };
+        mEnMgr.getLatestTetheringEntitlementValue(TETHERING_WIFI, receiver, false);
+        callbackTimeoutHelper(mCallbacklatch);
+        assertFalse(mEnMgr.everRunUiEntitlement);
+        // 3. No cache value and ui entitlement check is needed.
+        mEnMgr.fakeEntitlementResult = TETHER_ERROR_PROVISION_FAILED;
+        mEnMgr.everRunUiEntitlement = false;
+        receiver = new ResultReceiver(null) {
+            @Override
+            protected void onReceiveResult(int resultCode, Bundle resultData) {
+                assertEquals(TETHER_ERROR_PROVISION_FAILED, resultCode);
+                mCallbacklatch.countDown();
+            }
+        };
+        mEnMgr.getLatestTetheringEntitlementValue(TETHERING_WIFI, receiver, true);
+        mLooper.dispatchAll();
+        callbackTimeoutHelper(mCallbacklatch);
+        assertTrue(mEnMgr.everRunUiEntitlement);
+        // 4. Cache value is TETHER_ERROR_PROVISION_FAILED and don't need to run entitlement check.
+        mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR;
+        mEnMgr.everRunUiEntitlement = false;
+        receiver = new ResultReceiver(null) {
+            @Override
+            protected void onReceiveResult(int resultCode, Bundle resultData) {
+                assertEquals(TETHER_ERROR_PROVISION_FAILED, resultCode);
+                mCallbacklatch.countDown();
+            }
+        };
+        mEnMgr.getLatestTetheringEntitlementValue(TETHERING_WIFI, receiver, false);
+        callbackTimeoutHelper(mCallbacklatch);
+        assertFalse(mEnMgr.everRunUiEntitlement);
+        // 5. Cache value is TETHER_ERROR_PROVISION_FAILED and ui entitlement check is needed.
+        mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR;
+        mEnMgr.everRunUiEntitlement = false;
+        receiver = new ResultReceiver(null) {
+            @Override
+            protected void onReceiveResult(int resultCode, Bundle resultData) {
+                assertEquals(TETHER_ERROR_NO_ERROR, resultCode);
+                mCallbacklatch.countDown();
+            }
+        };
+        mEnMgr.getLatestTetheringEntitlementValue(TETHERING_WIFI, receiver, true);
+        mLooper.dispatchAll();
+        callbackTimeoutHelper(mCallbacklatch);
+        assertTrue(mEnMgr.everRunUiEntitlement);
+        // 6. Cache value is TETHER_ERROR_NO_ERROR.
+        mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR;
+        mEnMgr.everRunUiEntitlement = false;
+        receiver = new ResultReceiver(null) {
+            @Override
+            protected void onReceiveResult(int resultCode, Bundle resultData) {
+                assertEquals(TETHER_ERROR_NO_ERROR, resultCode);
+                mCallbacklatch.countDown();
+            }
+        };
+        mEnMgr.getLatestTetheringEntitlementValue(TETHERING_WIFI, receiver, true);
+        callbackTimeoutHelper(mCallbacklatch);
+        assertFalse(mEnMgr.everRunUiEntitlement);
+        // 7. Test get value for other downstream type.
+        mEnMgr.everRunUiEntitlement = false;
+        receiver = new ResultReceiver(null) {
+            @Override
+            protected void onReceiveResult(int resultCode, Bundle resultData) {
+                assertEquals(TETHER_ERROR_ENTITLEMENT_UNKONWN, resultCode);
+                mCallbacklatch.countDown();
+            }
+        };
+        mEnMgr.getLatestTetheringEntitlementValue(TETHERING_USB, receiver, false);
+        callbackTimeoutHelper(mCallbacklatch);
+        assertFalse(mEnMgr.everRunUiEntitlement);
+    }
+
+    void callbackTimeoutHelper(final CountDownLatch latch) throws Exception {
+        if (!latch.await(1, TimeUnit.SECONDS)) {
+            fail("Timout, fail to recieve callback");
+        }
+    }
+    public class TestStateMachine extends StateMachine {
+        public final ArrayList<Message> messages = new ArrayList<>();
+        private final State mLoggingState =
+                new EntitlementManagerTest.TestStateMachine.LoggingState();
+
+        class LoggingState extends State {
+            @Override public void enter() {
+                messages.clear();
+            }
+
+            @Override public void exit() {
+                messages.clear();
+            }
+
+            @Override public boolean processMessage(Message msg) {
+                messages.add(msg);
+                return false;
+            }
+        }
+
+        public TestStateMachine() {
+            super("EntitlementManagerTest.TestStateMachine", mLooper.getLooper());
+            addState(mLoggingState);
+            setInitialState(mLoggingState);
+            super.start();
+        }
+    }
 }
diff --git a/tools/aapt/AaptAssets.cpp b/tools/aapt/AaptAssets.cpp
index 3c3edda..672731c 100644
--- a/tools/aapt/AaptAssets.cpp
+++ b/tools/aapt/AaptAssets.cpp
@@ -301,6 +301,7 @@
                         break;
                     }
                     // This is not alphabetical, so we fall through to variant
+                    [[fallthrough]];
                 case 5:
                 case 6:
                 case 7: